Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/awt/splashscreen/splashscreen_jpeg.c
38918 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 "jinclude.h"28#include "jpeglib.h"29#include "jerror.h"3031#include <setjmp.h>3233#ifdef __APPLE__34/* use setjmp/longjmp versions that do not save/restore the signal mask */35#define setjmp _setjmp36#define longjmp _longjmp37#endif3839/* stream input handling */4041typedef struct42{43struct jpeg_source_mgr pub; /* public fields */44SplashStream * stream; /* source stream */45JOCTET *buffer; /* start of buffer */46boolean start_of_file; /* have we gotten any data yet? */47} stream_source_mgr;4849typedef stream_source_mgr *stream_src_ptr;5051#define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */5253METHODDEF(void)54stream_init_source(j_decompress_ptr cinfo)55{56stream_src_ptr src = (stream_src_ptr) cinfo->src;5758src->start_of_file = TRUE;59}6061METHODDEF(boolean)62stream_fill_input_buffer(j_decompress_ptr cinfo)63{64stream_src_ptr src = (stream_src_ptr) cinfo->src;65size_t nbytes;666768nbytes = src->stream->read(src->stream, src->buffer, INPUT_BUF_SIZE);6970if (nbytes <= 0) {71if (src->start_of_file) /* Treat empty input file as fatal error */72ERREXIT(cinfo, JERR_INPUT_EMPTY);73WARNMS(cinfo, JWRN_JPEG_EOF);74/* Insert a fake EOI marker */75src->buffer[0] = (JOCTET) 0xFF;76src->buffer[1] = (JOCTET) JPEG_EOI;77nbytes = 2;78}7980src->pub.next_input_byte = src->buffer;81src->pub.bytes_in_buffer = nbytes;82src->start_of_file = FALSE;8384return TRUE;85}8687METHODDEF(void)88stream_skip_input_data(j_decompress_ptr cinfo, long num_bytes)89{90stream_src_ptr src = (stream_src_ptr) cinfo->src;9192if (num_bytes > 0) {93while (num_bytes > (long) src->pub.bytes_in_buffer) {94num_bytes -= (long) src->pub.bytes_in_buffer;95(void) stream_fill_input_buffer(cinfo);96}97src->pub.next_input_byte += (size_t) num_bytes;98src->pub.bytes_in_buffer -= (size_t) num_bytes;99}100}101102METHODDEF(void)103stream_term_source(j_decompress_ptr cinfo)104{105}106107static void108set_stream_src(j_decompress_ptr cinfo, SplashStream * stream)109{110stream_src_ptr src;111112if (cinfo->src == NULL) { /* first time for this JPEG object? */113cinfo->src = (struct jpeg_source_mgr *)114(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,115JPOOL_PERMANENT, SIZEOF(stream_source_mgr));116src = (stream_src_ptr) cinfo->src;117src->buffer = (JOCTET *)118(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,119JPOOL_PERMANENT, INPUT_BUF_SIZE * SIZEOF(JOCTET));120}121122src = (stream_src_ptr) cinfo->src;123src->pub.init_source = stream_init_source;124src->pub.fill_input_buffer = stream_fill_input_buffer;125src->pub.skip_input_data = stream_skip_input_data;126src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */127src->pub.term_source = stream_term_source;128src->stream = stream;129src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */130src->pub.next_input_byte = NULL; /* until buffer loaded */131}132133int134SplashDecodeJpeg(Splash * splash, struct jpeg_decompress_struct *cinfo)135{136int rowStride, stride;137JSAMPARRAY buffer;138ImageFormat srcFormat;139140jpeg_read_header(cinfo, TRUE);141142// SplashScreen jpeg converter expects data in RGB format only143cinfo->out_color_space = JCS_RGB;144145jpeg_start_decompress(cinfo);146147SplashCleanup(splash);148149splash->width = cinfo->output_width;150splash->height = cinfo->output_height;151152if (!SAFE_TO_ALLOC(splash->imageFormat.depthBytes, splash->width)) {153return 0;154}155stride = splash->width * splash->imageFormat.depthBytes;156157if (!SAFE_TO_ALLOC(stride, splash->height)) {158return 0;159}160if (!SAFE_TO_ALLOC(cinfo->output_width, cinfo->output_components)) {161return 0;162}163164splash->frameCount = 1;165splash->frames = (SplashImage *) malloc(sizeof(SplashImage) *166splash->frameCount);167if (splash->frames == NULL) {168return 0;169}170memset(splash->frames, 0, sizeof(SplashImage) *171splash->frameCount);172173splash->loopCount = 1;174splash->frames[0].delay = 0;175splash->frames[0].bitmapBits = malloc(stride * splash->height);176if (splash->frames[0].bitmapBits == NULL) {177free(splash->frames);178return 0;179}180181rowStride = cinfo->output_width * cinfo->output_components;182183buffer = (*cinfo->mem->alloc_sarray)184((j_common_ptr) cinfo, JPOOL_IMAGE, rowStride, 1);185if (buffer == NULL) {186free(splash->frames[0].bitmapBits);187free(splash->frames);188return 0;189}190191initFormat(&srcFormat, 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000);192srcFormat.byteOrder = BYTE_ORDER_LSBFIRST;193srcFormat.depthBytes = 3;194srcFormat.fixedBits = 0xFF000000;195196splash->maskRequired = 0; // reset maskRequired as JPEG can't be transparent197198while (cinfo->output_scanline < cinfo->output_height) {199rgbquad_t *out =200(rgbquad_t *) ((byte_t *) splash->frames[0].bitmapBits +201cinfo->output_scanline * stride);202203jpeg_read_scanlines(cinfo, buffer, 1);204convertLine(buffer[0], sizeof(JSAMPLE) * 3, out,205splash->imageFormat.depthBytes, cinfo->output_width, &srcFormat,206&splash->imageFormat, CVT_COPY, NULL, 0, NULL,207cinfo->output_scanline, 0);208}209jpeg_finish_decompress(cinfo);210211return 1;212}213214struct my_error_mgr215{216struct jpeg_error_mgr pub; /* "public" fields */217jmp_buf setjmp_buffer; /* for return to caller */218};219220typedef struct my_error_mgr *my_error_ptr;221222static void223my_error_exit(j_common_ptr cinfo)224{225/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */226my_error_ptr myerr = (my_error_ptr) cinfo->err;227228/* Always display the message. */229/* We could postpone this until after returning, if we chose. */230(*cinfo->err->output_message) (cinfo);231232/* Return control to the setjmp point */233longjmp(myerr->setjmp_buffer, 1);234}235236int237SplashDecodeJpegStream(Splash * splash, SplashStream * stream)238{239struct jpeg_decompress_struct cinfo;240int success = 0;241struct my_error_mgr jerr;242243cinfo.err = jpeg_std_error(&jerr.pub);244jerr.pub.error_exit = my_error_exit;245246if (setjmp(jerr.setjmp_buffer)) {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