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