1 /*
   2  * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "splashscreen_impl.h"
  27 
  28 #include "jpeglib.h"
  29 #include "jerror.h"
  30 
  31 #include <setjmp.h>
  32 
  33 /* stream input handling */
  34 
  35 typedef struct
  36 {
  37     struct jpeg_source_mgr pub; /* public fields */
  38     SplashStream * stream;      /* source stream */
  39     JOCTET *buffer;             /* start of buffer */
  40     boolean start_of_file;      /* have we gotten any data yet? */
  41 } stream_source_mgr;
  42 
  43 typedef stream_source_mgr *stream_src_ptr;
  44 
  45 #define INPUT_BUF_SIZE  4096    /* choose an efficiently fread'able size */
  46 
  47 METHODDEF(void)
  48 stream_init_source(j_decompress_ptr cinfo)
  49 {
  50     stream_src_ptr src = (stream_src_ptr) cinfo->src;
  51 
  52     src->start_of_file = TRUE;
  53 }
  54 
  55 METHODDEF(boolean)
  56 stream_fill_input_buffer(j_decompress_ptr cinfo)
  57 {
  58     stream_src_ptr src = (stream_src_ptr) cinfo->src;
  59     size_t nbytes;
  60 
  61 
  62     nbytes = src->stream->read(src->stream, src->buffer, INPUT_BUF_SIZE);
  63 
  64     if (nbytes <= 0) {
  65         if (src->start_of_file) /* Treat empty input file as fatal error */
  66             ERREXIT(cinfo, JERR_INPUT_EMPTY);
  67         WARNMS(cinfo, JWRN_JPEG_EOF);
  68         /* Insert a fake EOI marker */
  69         src->buffer[0] = (JOCTET) 0xFF;
  70         src->buffer[1] = (JOCTET) JPEG_EOI;
  71         nbytes = 2;
  72     }
  73 
  74     src->pub.next_input_byte = src->buffer;
  75     src->pub.bytes_in_buffer = nbytes;
  76     src->start_of_file = FALSE;
  77 
  78     return TRUE;
  79 }
  80 
  81 METHODDEF(void)
  82     stream_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
  83 {
  84     stream_src_ptr src = (stream_src_ptr) cinfo->src;
  85 
  86     if (num_bytes > 0) {
  87         while (num_bytes > (long) src->pub.bytes_in_buffer) {
  88             num_bytes -= (long) src->pub.bytes_in_buffer;
  89             (void) stream_fill_input_buffer(cinfo);
  90         }
  91         src->pub.next_input_byte += (size_t) num_bytes;
  92         src->pub.bytes_in_buffer -= (size_t) num_bytes;
  93     }
  94 }
  95 
  96 METHODDEF(void)
  97 stream_term_source(j_decompress_ptr cinfo)
  98 {
  99 }
 100 
 101 static void
 102 set_stream_src(j_decompress_ptr cinfo, SplashStream * stream)
 103 {
 104     stream_src_ptr src;
 105 
 106     if (cinfo->src == NULL) {   /* first time for this JPEG object? */
 107         cinfo->src = (struct jpeg_source_mgr *)
 108             (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
 109             JPOOL_PERMANENT, sizeof(stream_source_mgr));
 110         src = (stream_src_ptr) cinfo->src;
 111         src->buffer = (JOCTET *)
 112             (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
 113             JPOOL_PERMANENT, INPUT_BUF_SIZE * sizeof(JOCTET));
 114     }
 115 
 116     src = (stream_src_ptr) cinfo->src;
 117     src->pub.init_source = stream_init_source;
 118     src->pub.fill_input_buffer = stream_fill_input_buffer;
 119     src->pub.skip_input_data = stream_skip_input_data;
 120     src->pub.resync_to_restart = jpeg_resync_to_restart;        /* use default method */
 121     src->pub.term_source = stream_term_source;
 122     src->stream = stream;
 123     src->pub.bytes_in_buffer = 0;       /* forces fill_input_buffer on first read */
 124     src->pub.next_input_byte = NULL;    /* until buffer loaded */
 125 }
 126 
 127 int
 128 SplashDecodeJpeg(Splash * splash, struct jpeg_decompress_struct *cinfo)
 129 {
 130     int rowStride, stride;
 131     JSAMPARRAY buffer;
 132     ImageFormat srcFormat;
 133 
 134     jpeg_read_header(cinfo, TRUE);
 135 
 136     // SplashScreen jpeg converter expects data in RGB format only
 137     cinfo->out_color_space = JCS_RGB;
 138 
 139     jpeg_start_decompress(cinfo);
 140 
 141     SplashCleanup(splash);
 142 
 143     splash->width = cinfo->output_width;
 144     splash->height = cinfo->output_height;
 145 
 146     if (!SAFE_TO_ALLOC(splash->imageFormat.depthBytes, splash->width)) {
 147         return 0;
 148     }
 149     stride = splash->width * splash->imageFormat.depthBytes;
 150 
 151     if (!SAFE_TO_ALLOC(stride, splash->height)) {
 152         return 0;
 153     }
 154     if (!SAFE_TO_ALLOC(cinfo->output_width, cinfo->output_components)) {
 155         return 0;
 156     }
 157 
 158     splash->frameCount = 1;
 159     splash->frames = (SplashImage *) malloc(sizeof(SplashImage) *
 160         splash->frameCount);
 161     if (splash->frames == NULL) {
 162         return 0;
 163     }
 164     memset(splash->frames, 0, sizeof(SplashImage) *
 165         splash->frameCount);
 166 
 167     splash->loopCount = 1;
 168     splash->frames[0].delay = 0;
 169     splash->frames[0].bitmapBits = malloc(stride * splash->height);
 170     if (splash->frames[0].bitmapBits == NULL) {
 171         free(splash->frames);
 172         return 0;
 173     }
 174 
 175     rowStride = cinfo->output_width * cinfo->output_components;
 176 
 177     buffer = (*cinfo->mem->alloc_sarray)
 178         ((j_common_ptr) cinfo, JPOOL_IMAGE, rowStride, 1);
 179     if (buffer == NULL) {
 180         free(splash->frames[0].bitmapBits);
 181         free(splash->frames);
 182         return 0;
 183     }
 184 
 185     initFormat(&srcFormat, 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000);
 186     srcFormat.byteOrder = BYTE_ORDER_LSBFIRST;
 187     srcFormat.depthBytes = 3;
 188     srcFormat.fixedBits = 0xFF000000;
 189 
 190     splash->maskRequired = 0;   // reset maskRequired as JPEG can't be transparent
 191 
 192     while (cinfo->output_scanline < cinfo->output_height) {
 193         rgbquad_t *out =
 194             (rgbquad_t *) ((byte_t *) splash->frames[0].bitmapBits +
 195                 cinfo->output_scanline * stride);
 196 
 197         jpeg_read_scanlines(cinfo, buffer, 1);
 198         convertLine(buffer[0], sizeof(JSAMPLE) * 3, out,
 199             splash->imageFormat.depthBytes, cinfo->output_width, &srcFormat,
 200             &splash->imageFormat, CVT_COPY, NULL, 0, NULL,
 201             cinfo->output_scanline, 0);
 202     }
 203     jpeg_finish_decompress(cinfo);
 204 
 205     return 1;
 206 }
 207 
 208 struct my_error_mgr
 209 {
 210     struct jpeg_error_mgr pub;  /* "public" fields */
 211     jmp_buf setjmp_buffer;      /* for return to caller */
 212 };
 213 
 214 typedef struct my_error_mgr *my_error_ptr;
 215 
 216 static void
 217 my_error_exit(j_common_ptr cinfo)
 218 {
 219     /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
 220     my_error_ptr myerr = (my_error_ptr) cinfo->err;
 221 
 222     /* Always display the message. */
 223     /* We could postpone this until after returning, if we chose. */
 224     (*cinfo->err->output_message) (cinfo);
 225 
 226     /* Return control to the setjmp point */
 227     longjmp(myerr->setjmp_buffer, 1);
 228 }
 229 
 230 int
 231 SplashDecodeJpegStream(Splash * splash, SplashStream * stream)
 232 {
 233     struct jpeg_decompress_struct cinfo;
 234     int success = 0;
 235     struct my_error_mgr jerr;
 236 
 237     cinfo.err = jpeg_std_error(&jerr.pub);
 238     jerr.pub.error_exit = my_error_exit;
 239 
 240     if (setjmp(jerr.setjmp_buffer)) {
 241         goto done;
 242     }
 243     jpeg_create_decompress(&cinfo);
 244     set_stream_src(&cinfo, stream);
 245     success = SplashDecodeJpeg(splash, &cinfo);
 246 
 247   done:
 248     jpeg_destroy_decompress(&cinfo);
 249     return success;
 250 }