Path: blob/master/src/java.desktop/share/native/libsplashscreen/splashscreen_jpeg.c
41152 views
/*1* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "splashscreen_impl.h"2627#include "jpeglib.h"28#include "jerror.h"2930#include <setjmp.h>3132#ifdef __APPLE__33/* use setjmp/longjmp versions that do not save/restore the signal mask */34#define setjmp _setjmp35#define longjmp _longjmp36#endif3738/* stream input handling */3940typedef struct41{42struct jpeg_source_mgr pub; /* public fields */43SplashStream * stream; /* source stream */44JOCTET *buffer; /* start of buffer */45boolean start_of_file; /* have we gotten any data yet? */46} stream_source_mgr;4748typedef stream_source_mgr *stream_src_ptr;4950#define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */5152METHODDEF(void)53stream_init_source(j_decompress_ptr cinfo)54{55stream_src_ptr src = (stream_src_ptr) cinfo->src;5657src->start_of_file = TRUE;58}5960METHODDEF(boolean)61stream_fill_input_buffer(j_decompress_ptr cinfo)62{63stream_src_ptr src = (stream_src_ptr) cinfo->src;64size_t nbytes;656667nbytes = src->stream->read(src->stream, src->buffer, INPUT_BUF_SIZE);6869if (nbytes <= 0) {70if (src->start_of_file) /* Treat empty input file as fatal error */71ERREXIT(cinfo, JERR_INPUT_EMPTY);72WARNMS(cinfo, JWRN_JPEG_EOF);73/* Insert a fake EOI marker */74src->buffer[0] = (JOCTET) 0xFF;75src->buffer[1] = (JOCTET) JPEG_EOI;76nbytes = 2;77}7879src->pub.next_input_byte = src->buffer;80src->pub.bytes_in_buffer = nbytes;81src->start_of_file = FALSE;8283return TRUE;84}8586METHODDEF(void)87stream_skip_input_data(j_decompress_ptr cinfo, long num_bytes)88{89stream_src_ptr src = (stream_src_ptr) cinfo->src;9091if (num_bytes > 0) {92while (num_bytes > (long) src->pub.bytes_in_buffer) {93num_bytes -= (long) src->pub.bytes_in_buffer;94(void) stream_fill_input_buffer(cinfo);95}96src->pub.next_input_byte += (size_t) num_bytes;97src->pub.bytes_in_buffer -= (size_t) num_bytes;98}99}100101METHODDEF(void)102stream_term_source(j_decompress_ptr cinfo)103{104}105106static void107set_stream_src(j_decompress_ptr cinfo, SplashStream * stream)108{109stream_src_ptr src;110111if (cinfo->src == NULL) { /* first time for this JPEG object? */112cinfo->src = (struct jpeg_source_mgr *)113(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,114JPOOL_PERMANENT, sizeof(stream_source_mgr));115src = (stream_src_ptr) cinfo->src;116src->buffer = (JOCTET *)117(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,118JPOOL_PERMANENT, INPUT_BUF_SIZE * sizeof(JOCTET));119}120121src = (stream_src_ptr) cinfo->src;122src->pub.init_source = stream_init_source;123src->pub.fill_input_buffer = stream_fill_input_buffer;124src->pub.skip_input_data = stream_skip_input_data;125src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */126src->pub.term_source = stream_term_source;127src->stream = stream;128src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */129src->pub.next_input_byte = NULL; /* until buffer loaded */130}131132int133SplashDecodeJpeg(Splash * splash, struct jpeg_decompress_struct *cinfo)134{135int rowStride, stride;136JSAMPARRAY buffer;137ImageFormat srcFormat;138139jpeg_read_header(cinfo, TRUE);140141// SplashScreen jpeg converter expects data in RGB format only142cinfo->out_color_space = JCS_RGB;143144jpeg_start_decompress(cinfo);145146SplashCleanup(splash);147148splash->width = cinfo->output_width;149splash->height = cinfo->output_height;150151if (!SAFE_TO_ALLOC(splash->imageFormat.depthBytes, splash->width)) {152return 0;153}154stride = splash->width * splash->imageFormat.depthBytes;155156if (!SAFE_TO_ALLOC(stride, splash->height)) {157return 0;158}159if (!SAFE_TO_ALLOC(cinfo->output_width, cinfo->output_components)) {160return 0;161}162163splash->frameCount = 1;164splash->frames = (SplashImage *) malloc(sizeof(SplashImage) *165splash->frameCount);166if (splash->frames == NULL) {167return 0;168}169memset(splash->frames, 0, sizeof(SplashImage) *170splash->frameCount);171172splash->loopCount = 1;173splash->frames[0].delay = 0;174splash->frames[0].bitmapBits = malloc(stride * splash->height);175if (splash->frames[0].bitmapBits == NULL) {176free(splash->frames);177return 0;178}179180rowStride = cinfo->output_width * cinfo->output_components;181182buffer = (*cinfo->mem->alloc_sarray)183((j_common_ptr) cinfo, JPOOL_IMAGE, rowStride, 1);184if (buffer == NULL) {185free(splash->frames[0].bitmapBits);186free(splash->frames);187return 0;188}189190initFormat(&srcFormat, 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000);191srcFormat.byteOrder = BYTE_ORDER_LSBFIRST;192srcFormat.depthBytes = 3;193srcFormat.fixedBits = 0xFF000000;194195splash->maskRequired = 0; // reset maskRequired as JPEG can't be transparent196197while (cinfo->output_scanline < cinfo->output_height) {198rgbquad_t *out =199(rgbquad_t *) ((byte_t *) splash->frames[0].bitmapBits +200cinfo->output_scanline * stride);201202jpeg_read_scanlines(cinfo, buffer, 1);203convertLine(buffer[0], sizeof(JSAMPLE) * 3, out,204splash->imageFormat.depthBytes, cinfo->output_width, &srcFormat,205&splash->imageFormat, CVT_COPY, NULL, 0, NULL,206cinfo->output_scanline, 0);207}208jpeg_finish_decompress(cinfo);209210return 1;211}212213struct my_error_mgr214{215struct jpeg_error_mgr pub; /* "public" fields */216jmp_buf setjmp_buffer; /* for return to caller */217};218219typedef struct my_error_mgr *my_error_ptr;220221static void222my_error_exit(j_common_ptr cinfo)223{224/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */225my_error_ptr myerr = (my_error_ptr) cinfo->err;226227/* Always display the message. */228/* We could postpone this until after returning, if we chose. */229(*cinfo->err->output_message) (cinfo);230231/* Return control to the setjmp point */232longjmp(myerr->setjmp_buffer, 1);233}234235int236SplashDecodeJpegStream(Splash * splash, SplashStream * stream)237{238struct jpeg_decompress_struct cinfo;239int success;240struct my_error_mgr jerr;241242cinfo.err = jpeg_std_error(&jerr.pub);243jerr.pub.error_exit = my_error_exit;244245if (setjmp(jerr.setjmp_buffer)) {246success = 0;247goto done;248}249jpeg_create_decompress(&cinfo);250set_stream_src(&cinfo, stream);251success = SplashDecodeJpeg(splash, &cinfo);252253done:254jpeg_destroy_decompress(&cinfo);255return success;256}257258259