0N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include "splashscreen_impl.h"
0N/A
0N/A#include "jinclude.h"
0N/A#include "jpeglib.h"
0N/A#include "jerror.h"
0N/A
0N/A#include <setjmp.h>
0N/A
0N/A/* stream input handling */
0N/A
0N/Atypedef struct
0N/A{
0N/A struct jpeg_source_mgr pub; /* public fields */
0N/A SplashStream * stream; /* source stream */
0N/A JOCTET *buffer; /* start of buffer */
0N/A boolean start_of_file; /* have we gotten any data yet? */
0N/A} stream_source_mgr;
0N/A
0N/Atypedef stream_source_mgr *stream_src_ptr;
0N/A
0N/A#define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
0N/A
0N/AMETHODDEF(void)
0N/Astream_init_source(j_decompress_ptr cinfo)
0N/A{
0N/A stream_src_ptr src = (stream_src_ptr) cinfo->src;
0N/A
0N/A src->start_of_file = TRUE;
0N/A}
0N/A
0N/AMETHODDEF(boolean)
0N/Astream_fill_input_buffer(j_decompress_ptr cinfo)
0N/A{
0N/A stream_src_ptr src = (stream_src_ptr) cinfo->src;
0N/A size_t nbytes;
0N/A
0N/A
0N/A nbytes = src->stream->read(src->stream, src->buffer, INPUT_BUF_SIZE);
0N/A
0N/A if (nbytes <= 0) {
0N/A if (src->start_of_file) /* Treat empty input file as fatal error */
0N/A ERREXIT(cinfo, JERR_INPUT_EMPTY);
0N/A WARNMS(cinfo, JWRN_JPEG_EOF);
0N/A /* Insert a fake EOI marker */
0N/A src->buffer[0] = (JOCTET) 0xFF;
0N/A src->buffer[1] = (JOCTET) JPEG_EOI;
0N/A nbytes = 2;
0N/A }
0N/A
0N/A src->pub.next_input_byte = src->buffer;
0N/A src->pub.bytes_in_buffer = nbytes;
0N/A src->start_of_file = FALSE;
0N/A
0N/A return TRUE;
0N/A}
0N/A
0N/AMETHODDEF(void)
0N/A stream_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
0N/A{
0N/A stream_src_ptr src = (stream_src_ptr) cinfo->src;
0N/A
0N/A if (num_bytes > 0) {
0N/A while (num_bytes > (long) src->pub.bytes_in_buffer) {
0N/A num_bytes -= (long) src->pub.bytes_in_buffer;
0N/A (void) stream_fill_input_buffer(cinfo);
0N/A }
0N/A src->pub.next_input_byte += (size_t) num_bytes;
0N/A src->pub.bytes_in_buffer -= (size_t) num_bytes;
0N/A }
0N/A}
0N/A
0N/AMETHODDEF(void)
0N/Astream_term_source(j_decompress_ptr cinfo)
0N/A{
0N/A}
0N/A
0N/Astatic void
0N/Aset_stream_src(j_decompress_ptr cinfo, SplashStream * stream)
0N/A{
0N/A stream_src_ptr src;
0N/A
0N/A if (cinfo->src == NULL) { /* first time for this JPEG object? */
0N/A cinfo->src = (struct jpeg_source_mgr *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
0N/A JPOOL_PERMANENT, SIZEOF(stream_source_mgr));
0N/A src = (stream_src_ptr) cinfo->src;
0N/A src->buffer = (JOCTET *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
0N/A JPOOL_PERMANENT, INPUT_BUF_SIZE * SIZEOF(JOCTET));
0N/A }
0N/A
0N/A src = (stream_src_ptr) cinfo->src;
0N/A src->pub.init_source = stream_init_source;
0N/A src->pub.fill_input_buffer = stream_fill_input_buffer;
0N/A src->pub.skip_input_data = stream_skip_input_data;
0N/A src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
0N/A src->pub.term_source = stream_term_source;
0N/A src->stream = stream;
0N/A src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
0N/A src->pub.next_input_byte = NULL; /* until buffer loaded */
0N/A}
0N/A
0N/Aint
0N/ASplashDecodeJpeg(Splash * splash, struct jpeg_decompress_struct *cinfo)
0N/A{
0N/A int rowStride, stride;
0N/A JSAMPARRAY buffer;
0N/A ImageFormat srcFormat;
0N/A
0N/A jpeg_read_header(cinfo, TRUE);
5685N/A
5685N/A // SplashScreen jpeg converter expects data in RGB format only
5685N/A cinfo->out_color_space = JCS_RGB;
5685N/A
0N/A jpeg_start_decompress(cinfo);
0N/A
0N/A SplashCleanup(splash);
0N/A
0N/A splash->width = cinfo->output_width;
0N/A splash->height = cinfo->output_height;
1502N/A
1502N/A if (!SAFE_TO_ALLOC(splash->imageFormat.depthBytes, splash->width)) {
1502N/A return 0;
1502N/A }
0N/A stride = splash->width * splash->imageFormat.depthBytes;
0N/A
1502N/A if (!SAFE_TO_ALLOC(stride, splash->height)) {
1502N/A return 0;
1502N/A }
1502N/A if (!SAFE_TO_ALLOC(cinfo->output_width, cinfo->output_components)) {
1502N/A return 0;
1502N/A }
1502N/A
0N/A splash->frameCount = 1;
0N/A splash->frames = (SplashImage *) malloc(sizeof(SplashImage) *
0N/A splash->frameCount);
1502N/A if (splash->frames == NULL) {
1502N/A return 0;
1502N/A }
0N/A memset(splash->frames, 0, sizeof(SplashImage) *
0N/A splash->frameCount);
1502N/A
0N/A splash->loopCount = 1;
1502N/A splash->frames[0].delay = 0;
0N/A splash->frames[0].bitmapBits = malloc(stride * splash->height);
1502N/A if (splash->frames[0].bitmapBits == NULL) {
1502N/A free(splash->frames);
1502N/A return 0;
1502N/A }
0N/A
0N/A rowStride = cinfo->output_width * cinfo->output_components;
0N/A
0N/A buffer = (*cinfo->mem->alloc_sarray)
0N/A ((j_common_ptr) cinfo, JPOOL_IMAGE, rowStride, 1);
1502N/A if (buffer == NULL) {
1502N/A free(splash->frames[0].bitmapBits);
1502N/A free(splash->frames);
1502N/A return 0;
1502N/A }
0N/A
0N/A initFormat(&srcFormat, 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000);
0N/A srcFormat.byteOrder = BYTE_ORDER_LSBFIRST;
0N/A srcFormat.depthBytes = 3;
0N/A srcFormat.fixedBits = 0xFF000000;
0N/A
0N/A splash->maskRequired = 0; // reset maskRequired as JPEG can't be transparent
0N/A
0N/A while (cinfo->output_scanline < cinfo->output_height) {
0N/A rgbquad_t *out =
0N/A (rgbquad_t *) ((byte_t *) splash->frames[0].bitmapBits +
0N/A cinfo->output_scanline * stride);
0N/A
0N/A jpeg_read_scanlines(cinfo, buffer, 1);
0N/A convertLine(buffer[0], sizeof(JSAMPLE) * 3, out,
0N/A splash->imageFormat.depthBytes, cinfo->output_width, &srcFormat,
0N/A &splash->imageFormat, CVT_COPY, NULL, 0, NULL,
0N/A cinfo->output_scanline, 0);
0N/A }
0N/A jpeg_finish_decompress(cinfo);
0N/A
0N/A return 1;
0N/A}
0N/A
0N/Astruct my_error_mgr
0N/A{
0N/A struct jpeg_error_mgr pub; /* "public" fields */
0N/A jmp_buf setjmp_buffer; /* for return to caller */
0N/A};
0N/A
0N/Atypedef struct my_error_mgr *my_error_ptr;
0N/A
0N/Astatic void
0N/Amy_error_exit(j_common_ptr cinfo)
0N/A{
0N/A /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
0N/A my_error_ptr myerr = (my_error_ptr) cinfo->err;
0N/A
0N/A /* Always display the message. */
0N/A /* We could postpone this until after returning, if we chose. */
0N/A (*cinfo->err->output_message) (cinfo);
0N/A
0N/A /* Return control to the setjmp point */
0N/A longjmp(myerr->setjmp_buffer, 1);
0N/A}
0N/A
0N/Aint
0N/ASplashDecodeJpegStream(Splash * splash, SplashStream * stream)
0N/A{
0N/A struct jpeg_decompress_struct cinfo;
0N/A int success = 0;
0N/A struct my_error_mgr jerr;
0N/A
0N/A cinfo.err = jpeg_std_error(&jerr.pub);
0N/A jerr.pub.error_exit = my_error_exit;
0N/A
0N/A if (setjmp(jerr.setjmp_buffer)) {
0N/A goto done;
0N/A }
0N/A jpeg_create_decompress(&cinfo);
0N/A set_stream_src(&cinfo, stream);
0N/A success = SplashDecodeJpeg(splash, &cinfo);
0N/A
0N/A done:
0N/A jpeg_destroy_decompress(&cinfo);
0N/A return success;
0N/A}