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 "jinclude.h" 29 #include "jpeglib.h" 30 #include "jerror.h" 31 32 #include <setjmp.h> 33 34 /* stream input handling */ 35 36 typedef struct 37 { 38 struct jpeg_source_mgr pub; /* public fields */ 39 SplashStream * stream; /* source stream */ 40 JOCTET *buffer; /* start of buffer */ 41 boolean start_of_file; /* have we gotten any data yet? */ 42 } stream_source_mgr; 43 44 typedef stream_source_mgr *stream_src_ptr; 45 46 #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */ 47 48 METHODDEF(void) 49 stream_init_source(j_decompress_ptr cinfo) 50 { 51 stream_src_ptr src = (stream_src_ptr) cinfo->src; 52 53 src->start_of_file = TRUE; 54 } 55 56 METHODDEF(boolean) 57 stream_fill_input_buffer(j_decompress_ptr cinfo) 58 { 59 stream_src_ptr src = (stream_src_ptr) cinfo->src; 60 size_t nbytes; 61 62 63 nbytes = src->stream->read(src->stream, src->buffer, INPUT_BUF_SIZE); 64 65 if (nbytes <= 0) { 66 if (src->start_of_file) /* Treat empty input file as fatal error */ 67 ERREXIT(cinfo, JERR_INPUT_EMPTY); 68 WARNMS(cinfo, JWRN_JPEG_EOF); 69 /* Insert a fake EOI marker */ 70 src->buffer[0] = (JOCTET) 0xFF; 71 src->buffer[1] = (JOCTET) JPEG_EOI; 72 nbytes = 2; 73 } 74 75 src->pub.next_input_byte = src->buffer; 76 src->pub.bytes_in_buffer = nbytes; 77 src->start_of_file = FALSE; 78 79 return TRUE; 80 } 81 82 METHODDEF(void) 83 stream_skip_input_data(j_decompress_ptr cinfo, long num_bytes) 84 { 85 stream_src_ptr src = (stream_src_ptr) cinfo->src; 86 87 if (num_bytes > 0) { 88 while (num_bytes > (long) src->pub.bytes_in_buffer) { 89 num_bytes -= (long) src->pub.bytes_in_buffer; 90 (void) stream_fill_input_buffer(cinfo); 91 } 92 src->pub.next_input_byte += (size_t) num_bytes; 93 src->pub.bytes_in_buffer -= (size_t) num_bytes; 94 } 95 } 96 97 METHODDEF(void) 98 stream_term_source(j_decompress_ptr cinfo) 99 { 100 } 101 102 static void 103 set_stream_src(j_decompress_ptr cinfo, SplashStream * stream) 104 { 105 stream_src_ptr src; 106 107 if (cinfo->src == NULL) { /* first time for this JPEG object? */ 108 cinfo->src = (struct jpeg_source_mgr *) 109 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, 110 JPOOL_PERMANENT, SIZEOF(stream_source_mgr)); 111 src = (stream_src_ptr) cinfo->src; 112 src->buffer = (JOCTET *) 113 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, 114 JPOOL_PERMANENT, INPUT_BUF_SIZE * SIZEOF(JOCTET)); 115 } 116 117 src = (stream_src_ptr) cinfo->src; 118 src->pub.init_source = stream_init_source; 119 src->pub.fill_input_buffer = stream_fill_input_buffer; 120 src->pub.skip_input_data = stream_skip_input_data; 121 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ 122 src->pub.term_source = stream_term_source; 123 src->stream = stream; 124 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ 125 src->pub.next_input_byte = NULL; /* until buffer loaded */ 126 } 127 128 int 129 SplashDecodeJpeg(Splash * splash, struct jpeg_decompress_struct *cinfo) 130 { 131 int rowStride, stride; 132 JSAMPARRAY buffer; 133 ImageFormat srcFormat; 134 135 jpeg_read_header(cinfo, TRUE); 136 137 // SplashScreen jpeg converter expects data in RGB format only 138 cinfo->out_color_space = JCS_RGB; 139 140 jpeg_start_decompress(cinfo); 141 142 SplashCleanup(splash); 143 144 splash->width = cinfo->output_width; 145 splash->height = cinfo->output_height; 146 147 if (!SAFE_TO_ALLOC(splash->imageFormat.depthBytes, splash->width)) { 148 return 0; 149 } 150 stride = splash->width * splash->imageFormat.depthBytes; 151 152 if (!SAFE_TO_ALLOC(stride, splash->height)) { 153 return 0; 154 } 155 if (!SAFE_TO_ALLOC(cinfo->output_width, cinfo->output_components)) { 156 return 0; 157 } 158 159 splash->frameCount = 1; 160 splash->frames = (SplashImage *) malloc(sizeof(SplashImage) * 161 splash->frameCount); 162 if (splash->frames == NULL) { 163 return 0; 164 } 165 memset(splash->frames, 0, sizeof(SplashImage) * 166 splash->frameCount); 167 168 splash->loopCount = 1; 169 splash->frames[0].delay = 0; 170 splash->frames[0].bitmapBits = malloc(stride * splash->height); 171 if (splash->frames[0].bitmapBits == NULL) { 172 free(splash->frames); 173 return 0; 174 } 175 176 rowStride = cinfo->output_width * cinfo->output_components; 177 178 buffer = (*cinfo->mem->alloc_sarray) 179 ((j_common_ptr) cinfo, JPOOL_IMAGE, rowStride, 1); 180 if (buffer == NULL) { 181 free(splash->frames[0].bitmapBits); 182 free(splash->frames); 183 return 0; 184 } 185 186 initFormat(&srcFormat, 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000); 187 srcFormat.byteOrder = BYTE_ORDER_LSBFIRST; 188 srcFormat.depthBytes = 3; 189 srcFormat.fixedBits = 0xFF000000; 190 191 splash->maskRequired = 0; // reset maskRequired as JPEG can't be transparent 192 193 while (cinfo->output_scanline < cinfo->output_height) { 194 rgbquad_t *out = 195 (rgbquad_t *) ((byte_t *) splash->frames[0].bitmapBits + 196 cinfo->output_scanline * stride); 197 198 jpeg_read_scanlines(cinfo, buffer, 1); 199 convertLine(buffer[0], sizeof(JSAMPLE) * 3, out, 200 splash->imageFormat.depthBytes, cinfo->output_width, &srcFormat, 201 &splash->imageFormat, CVT_COPY, NULL, 0, NULL, 202 cinfo->output_scanline, 0); 203 } 204 jpeg_finish_decompress(cinfo); 205 206 return 1; 207 } 208 209 struct my_error_mgr 210 { 211 struct jpeg_error_mgr pub; /* "public" fields */ 212 jmp_buf setjmp_buffer; /* for return to caller */ 213 }; 214 215 typedef struct my_error_mgr *my_error_ptr; 216 217 static void 218 my_error_exit(j_common_ptr cinfo) 219 { 220 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */ 221 my_error_ptr myerr = (my_error_ptr) cinfo->err; 222 223 /* Always display the message. */ 224 /* We could postpone this until after returning, if we chose. */ 225 (*cinfo->err->output_message) (cinfo); 226 227 /* Return control to the setjmp point */ 228 longjmp(myerr->setjmp_buffer, 1); 229 } 230 231 int 232 SplashDecodeJpegStream(Splash * splash, SplashStream * stream) 233 { 234 struct jpeg_decompress_struct cinfo; 235 int success = 0; 236 struct my_error_mgr jerr; 237 238 cinfo.err = jpeg_std_error(&jerr.pub); 239 jerr.pub.error_exit = my_error_exit; 240 241 if (setjmp(jerr.setjmp_buffer)) { 242 goto done; 243 } 244 jpeg_create_decompress(&cinfo); 245 set_stream_src(&cinfo, stream); 246 success = SplashDecodeJpeg(splash, &cinfo); 247 248 done: 249 jpeg_destroy_decompress(&cinfo); 250 return success; 251 }