blob: 09d38fcb4223d4456085916327c35d7142942403 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2006 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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
36typedef 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
44typedef stream_source_mgr *stream_src_ptr;
45
46#define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
47
48METHODDEF(void)
49stream_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
56METHODDEF(boolean)
57stream_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
82METHODDEF(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
97METHODDEF(void)
98stream_term_source(j_decompress_ptr cinfo)
99{
100}
101
102static void
103set_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
128int
129SplashDecodeJpeg(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 jpeg_start_decompress(cinfo);
137
138 SplashCleanup(splash);
139
140 splash->width = cinfo->output_width;
141 splash->height = cinfo->output_height;
142 stride = splash->width * splash->imageFormat.depthBytes;
143
144 splash->frameCount = 1;
145 splash->frames = (SplashImage *) malloc(sizeof(SplashImage) *
146 splash->frameCount);
147 memset(splash->frames, 0, sizeof(SplashImage) *
148 splash->frameCount);
149 splash->loopCount = 1;
150 splash->frames[0].bitmapBits = malloc(stride * splash->height);
151 splash->frames[0].delay = 0;
152
153 rowStride = cinfo->output_width * cinfo->output_components;
154
155 buffer = (*cinfo->mem->alloc_sarray)
156 ((j_common_ptr) cinfo, JPOOL_IMAGE, rowStride, 1);
157
158 initFormat(&srcFormat, 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000);
159 srcFormat.byteOrder = BYTE_ORDER_LSBFIRST;
160 srcFormat.depthBytes = 3;
161 srcFormat.fixedBits = 0xFF000000;
162
163 splash->maskRequired = 0; // reset maskRequired as JPEG can't be transparent
164
165 while (cinfo->output_scanline < cinfo->output_height) {
166 rgbquad_t *out =
167 (rgbquad_t *) ((byte_t *) splash->frames[0].bitmapBits +
168 cinfo->output_scanline * stride);
169
170 jpeg_read_scanlines(cinfo, buffer, 1);
171 convertLine(buffer[0], sizeof(JSAMPLE) * 3, out,
172 splash->imageFormat.depthBytes, cinfo->output_width, &srcFormat,
173 &splash->imageFormat, CVT_COPY, NULL, 0, NULL,
174 cinfo->output_scanline, 0);
175 }
176 jpeg_finish_decompress(cinfo);
177
178 return 1;
179}
180
181struct my_error_mgr
182{
183 struct jpeg_error_mgr pub; /* "public" fields */
184 jmp_buf setjmp_buffer; /* for return to caller */
185};
186
187typedef struct my_error_mgr *my_error_ptr;
188
189static void
190my_error_exit(j_common_ptr cinfo)
191{
192 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
193 my_error_ptr myerr = (my_error_ptr) cinfo->err;
194
195 /* Always display the message. */
196 /* We could postpone this until after returning, if we chose. */
197 (*cinfo->err->output_message) (cinfo);
198
199 /* Return control to the setjmp point */
200 longjmp(myerr->setjmp_buffer, 1);
201}
202
203int
204SplashDecodeJpegStream(Splash * splash, SplashStream * stream)
205{
206 struct jpeg_decompress_struct cinfo;
207 int success = 0;
208 struct my_error_mgr jerr;
209
210 cinfo.err = jpeg_std_error(&jerr.pub);
211 jerr.pub.error_exit = my_error_exit;
212
213 if (setjmp(jerr.setjmp_buffer)) {
214 goto done;
215 }
216 jpeg_create_decompress(&cinfo);
217 set_stream_src(&cinfo, stream);
218 success = SplashDecodeJpeg(splash, &cinfo);
219
220 done:
221 jpeg_destroy_decompress(&cinfo);
222 return success;
223}