2
|
1 |
/*
|
|
2 |
* Copyright 1995-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 <stdio.h>
|
|
27 |
#include "jni.h"
|
|
28 |
#include "jni_util.h"
|
|
29 |
|
|
30 |
#define OUTCODELENGTH 4097
|
|
31 |
|
|
32 |
/* We use Get/ReleasePrimitiveArrayCritical functions to avoid
|
|
33 |
* the need to copy buffer elements.
|
|
34 |
*
|
|
35 |
* MAKE SURE TO:
|
|
36 |
*
|
|
37 |
* - carefully insert pairs of RELEASE_ARRAYS and GET_ARRAYS around
|
|
38 |
* callbacks to Java.
|
|
39 |
* - call RELEASE_ARRAYS before returning to Java.
|
|
40 |
*
|
|
41 |
* Otherwise things will go horribly wrong. There may be memory leaks,
|
|
42 |
* excessive pinning, or even VM crashes!
|
|
43 |
*
|
|
44 |
* Note that GetPrimitiveArrayCritical may fail!
|
|
45 |
*/
|
|
46 |
|
|
47 |
#define GET_ARRAYS() \
|
|
48 |
prefix = (short *) \
|
|
49 |
(*env)->GetPrimitiveArrayCritical(env, prefixh, 0); \
|
|
50 |
if (prefix == 0) \
|
|
51 |
goto out_of_memory; \
|
|
52 |
suffix = (unsigned char *) \
|
|
53 |
(*env)->GetPrimitiveArrayCritical(env, suffixh, 0); \
|
|
54 |
if (suffix == 0) \
|
|
55 |
goto out_of_memory; \
|
|
56 |
outCode = (unsigned char *) \
|
|
57 |
(*env)->GetPrimitiveArrayCritical(env, outCodeh, 0); \
|
|
58 |
if (outCode == 0) \
|
|
59 |
goto out_of_memory; \
|
|
60 |
rasline = (unsigned char *) \
|
|
61 |
(*env)->GetPrimitiveArrayCritical(env, raslineh, 0); \
|
|
62 |
if (rasline == 0) \
|
|
63 |
goto out_of_memory; \
|
|
64 |
block = (unsigned char *) \
|
|
65 |
(*env)->GetPrimitiveArrayCritical(env, blockh, 0); \
|
|
66 |
if (block == 0) \
|
|
67 |
goto out_of_memory
|
|
68 |
|
|
69 |
/*
|
|
70 |
* Note that it is important to check whether the arrays are NULL,
|
|
71 |
* because GetPrimitiveArrayCritical might have failed.
|
|
72 |
*/
|
|
73 |
#define RELEASE_ARRAYS() \
|
|
74 |
if (prefix) \
|
|
75 |
(*env)->ReleasePrimitiveArrayCritical(env, prefixh, prefix, 0); \
|
|
76 |
if (suffix) \
|
|
77 |
(*env)->ReleasePrimitiveArrayCritical(env, suffixh, suffix, 0); \
|
|
78 |
if (outCode) \
|
|
79 |
(*env)->ReleasePrimitiveArrayCritical(env, outCodeh, outCode, 0); \
|
|
80 |
if (rasline) \
|
|
81 |
(*env)->ReleasePrimitiveArrayCritical(env, raslineh, rasline, 0); \
|
|
82 |
if (block) \
|
|
83 |
(*env)->ReleasePrimitiveArrayCritical(env, blockh, block, 0)
|
|
84 |
|
|
85 |
/* Place holders for the old native interface. */
|
|
86 |
|
|
87 |
long
|
|
88 |
sun_awt_image_GifImageDecoder_parseImage()
|
|
89 |
{
|
|
90 |
return 0;
|
|
91 |
}
|
|
92 |
|
|
93 |
void
|
|
94 |
sun_awt_image_GifImageDecoder_initIDs()
|
|
95 |
{
|
|
96 |
}
|
|
97 |
|
|
98 |
static jmethodID readID;
|
|
99 |
static jmethodID sendID;
|
|
100 |
static jfieldID prefixID;
|
|
101 |
static jfieldID suffixID;
|
|
102 |
static jfieldID outCodeID;
|
|
103 |
|
|
104 |
JNIEXPORT void JNICALL
|
|
105 |
Java_sun_awt_image_GifImageDecoder_initIDs(JNIEnv *env, jclass this)
|
|
106 |
{
|
|
107 |
readID = (*env)->GetMethodID(env, this, "readBytes", "([BII)I");
|
|
108 |
sendID = (*env)->GetMethodID(env, this, "sendPixels",
|
|
109 |
"(IIII[BLjava/awt/image/ColorModel;)I");
|
|
110 |
prefixID = (*env)->GetFieldID(env, this, "prefix", "[S");
|
|
111 |
suffixID = (*env)->GetFieldID(env, this, "suffix", "[B");
|
|
112 |
outCodeID = (*env)->GetFieldID(env, this, "outCode", "[B");
|
|
113 |
}
|
|
114 |
|
|
115 |
JNIEXPORT jboolean JNICALL
|
|
116 |
Java_sun_awt_image_GifImageDecoder_parseImage(JNIEnv *env,
|
|
117 |
jobject this,
|
|
118 |
jint relx, jint rely,
|
|
119 |
jint width, jint height,
|
|
120 |
jint interlace,
|
|
121 |
jint initCodeSize,
|
|
122 |
jbyteArray blockh,
|
|
123 |
jbyteArray raslineh,
|
|
124 |
jobject cmh)
|
|
125 |
{
|
|
126 |
/* Patrick Naughton:
|
|
127 |
* Note that I ignore the possible existence of a local color map.
|
|
128 |
* I'm told there aren't many files around that use them, and the
|
|
129 |
* spec says it's defined for future use. This could lead to an
|
|
130 |
* error reading some files.
|
|
131 |
*
|
|
132 |
* Start reading the image data. First we get the intial code size
|
|
133 |
* and compute decompressor constant values, based on this code
|
|
134 |
* size.
|
|
135 |
*
|
|
136 |
* The GIF spec has it that the code size is the code size used to
|
|
137 |
* compute the above values is the code size given in the file,
|
|
138 |
* but the code size used in compression/decompression is the code
|
|
139 |
* size given in the file plus one. (thus the ++).
|
|
140 |
*
|
|
141 |
* Arthur van Hoff:
|
|
142 |
* The following narly code reads LZW compressed data blocks and
|
|
143 |
* dumps it into the image data. The input stream is broken up into
|
|
144 |
* blocks of 1-255 characters, each preceded by a length byte.
|
|
145 |
* 3-12 bit codes are read from these blocks. The codes correspond to
|
|
146 |
* entry is the hashtable (the prefix, suffix stuff), and the appropriate
|
|
147 |
* pixels are written to the image.
|
|
148 |
*/
|
|
149 |
static int verbose = 0;
|
|
150 |
|
|
151 |
int clearCode = (1 << initCodeSize);
|
|
152 |
int eofCode = clearCode + 1;
|
|
153 |
int bitMask;
|
|
154 |
int curCode;
|
|
155 |
int outCount;
|
|
156 |
|
|
157 |
/* Variables used to form reading data */
|
|
158 |
int blockEnd = 0;
|
|
159 |
int remain = 0;
|
|
160 |
int byteoff = 0;
|
|
161 |
int accumbits = 0;
|
|
162 |
int accumdata = 0;
|
|
163 |
|
|
164 |
/* Variables used to decompress the data */
|
|
165 |
int codeSize = initCodeSize + 1;
|
|
166 |
int maxCode = 1 << codeSize;
|
|
167 |
int codeMask = maxCode - 1;
|
|
168 |
int freeCode = clearCode + 2;
|
|
169 |
int code = 0;
|
|
170 |
int oldCode = 0;
|
|
171 |
unsigned char prevChar = 0;
|
|
172 |
|
|
173 |
/* Temproray storage for decompression */
|
|
174 |
short *prefix;
|
|
175 |
unsigned char *suffix = NULL;
|
|
176 |
unsigned char *outCode = NULL;
|
|
177 |
unsigned char *rasline = NULL;
|
|
178 |
unsigned char *block = NULL;
|
|
179 |
|
|
180 |
jshortArray prefixh = (*env)->GetObjectField(env, this, prefixID);
|
|
181 |
jbyteArray suffixh = (*env)->GetObjectField(env, this, suffixID);
|
|
182 |
jbyteArray outCodeh = (*env)->GetObjectField(env, this, outCodeID);
|
|
183 |
|
|
184 |
int blockLength = 0;
|
|
185 |
|
|
186 |
/* Variables used for writing pixels */
|
|
187 |
int x = width;
|
|
188 |
int y = 0;
|
|
189 |
int off = 0;
|
|
190 |
int passinc = interlace ? 8 : 1;
|
|
191 |
int passht = passinc;
|
|
192 |
int len;
|
|
193 |
|
2610
|
194 |
/* We have verified the initial code size on the java layer.
|
|
195 |
* Here we just check bounds for particular indexes. */
|
|
196 |
if (freeCode >= 4096 || maxCode >= 4096) {
|
|
197 |
return 0;
|
|
198 |
}
|
2
|
199 |
if (blockh == 0 || raslineh == 0
|
|
200 |
|| prefixh == 0 || suffixh == 0
|
|
201 |
|| outCodeh == 0)
|
|
202 |
{
|
|
203 |
JNU_ThrowNullPointerException(env, 0);
|
|
204 |
return 0;
|
|
205 |
}
|
|
206 |
if (((*env)->GetArrayLength(env, prefixh) != 4096) ||
|
|
207 |
((*env)->GetArrayLength(env, suffixh) != 4096) ||
|
|
208 |
((*env)->GetArrayLength(env, outCodeh) != OUTCODELENGTH))
|
|
209 |
{
|
|
210 |
JNU_ThrowArrayIndexOutOfBoundsException(env, 0);
|
|
211 |
return 0;
|
|
212 |
}
|
|
213 |
|
|
214 |
if (verbose) {
|
|
215 |
fprintf(stdout, "Decompressing...");
|
|
216 |
}
|
|
217 |
|
|
218 |
/* Fix for bugid 4216605 Some animated GIFs display corrupted. */
|
|
219 |
bitMask = clearCode - 1;
|
|
220 |
|
|
221 |
GET_ARRAYS();
|
|
222 |
|
|
223 |
/* Read codes until the eofCode is encountered */
|
|
224 |
for (;;) {
|
|
225 |
if (accumbits < codeSize) {
|
|
226 |
/* fill the buffer if needed */
|
|
227 |
while (remain < 2) {
|
|
228 |
if (blockEnd) {
|
|
229 |
/* Sometimes we have one last byte to process... */
|
|
230 |
if (remain == 1 && accumbits + 8 >= codeSize) {
|
|
231 |
remain--;
|
|
232 |
goto last_byte;
|
|
233 |
}
|
|
234 |
RELEASE_ARRAYS();
|
|
235 |
if (off > 0) {
|
|
236 |
(*env)->CallIntMethod(env, this, sendID,
|
|
237 |
relx, rely + y,
|
|
238 |
width, passht,
|
|
239 |
raslineh, cmh);
|
|
240 |
}
|
|
241 |
/* quietly accept truncated GIF images */
|
|
242 |
return 1;
|
|
243 |
}
|
|
244 |
/* move remaining bytes to the beginning of the buffer */
|
|
245 |
block[0] = block[byteoff];
|
|
246 |
byteoff = 0;
|
|
247 |
|
|
248 |
RELEASE_ARRAYS();
|
|
249 |
/* fill the block */
|
|
250 |
len = (*env)->CallIntMethod(env, this, readID,
|
|
251 |
blockh, remain, blockLength + 1);
|
|
252 |
if ((*env)->ExceptionOccurred(env)) {
|
|
253 |
return 0;
|
|
254 |
}
|
|
255 |
GET_ARRAYS();
|
|
256 |
|
|
257 |
remain += blockLength;
|
|
258 |
if (len > 0) {
|
|
259 |
remain -= (len - 1);
|
|
260 |
blockLength = 0;
|
|
261 |
} else {
|
|
262 |
blockLength = block[remain];
|
|
263 |
}
|
|
264 |
if (blockLength == 0) {
|
|
265 |
blockEnd = 1;
|
|
266 |
}
|
|
267 |
}
|
|
268 |
remain -= 2;
|
|
269 |
|
|
270 |
/* 2 bytes at a time saves checking for accumbits < codeSize.
|
|
271 |
* We know we'll get enough and also that we can't overflow
|
|
272 |
* since codeSize <= 12.
|
|
273 |
*/
|
|
274 |
accumdata += (block[byteoff++] & 0xff) << accumbits;
|
|
275 |
accumbits += 8;
|
|
276 |
last_byte:
|
|
277 |
accumdata += (block[byteoff++] & 0xff) << accumbits;
|
|
278 |
accumbits += 8;
|
|
279 |
}
|
|
280 |
|
|
281 |
/* Compute the code */
|
|
282 |
code = accumdata & codeMask;
|
|
283 |
accumdata >>= codeSize;
|
|
284 |
accumbits -= codeSize;
|
|
285 |
|
|
286 |
/*
|
|
287 |
* Interpret the code
|
|
288 |
*/
|
|
289 |
if (code == clearCode) {
|
|
290 |
/* Clear code sets everything back to its initial value, then
|
|
291 |
* reads the immediately subsequent code as uncompressed data.
|
|
292 |
*/
|
|
293 |
if (verbose) {
|
|
294 |
fprintf(stdout, ".");
|
|
295 |
fflush(stdout);
|
|
296 |
}
|
|
297 |
|
|
298 |
/* Note that freeCode is one less than it is supposed to be,
|
|
299 |
* this is because it will be incremented next time round the loop
|
|
300 |
*/
|
|
301 |
freeCode = clearCode + 1;
|
|
302 |
codeSize = initCodeSize + 1;
|
|
303 |
maxCode = 1 << codeSize;
|
|
304 |
codeMask = maxCode - 1;
|
|
305 |
|
|
306 |
/* Continue if we've NOT reached the end, some Gif images
|
|
307 |
* contain bogus codes after the last clear code.
|
|
308 |
*/
|
|
309 |
if (y < height) {
|
|
310 |
continue;
|
|
311 |
}
|
|
312 |
|
|
313 |
/* pretend we've reached the end of the data */
|
|
314 |
code = eofCode;
|
|
315 |
}
|
|
316 |
|
|
317 |
if (code == eofCode) {
|
|
318 |
/* make sure we read the whole block of pixels. */
|
|
319 |
flushit:
|
|
320 |
while (!blockEnd) {
|
|
321 |
if (verbose) {
|
|
322 |
fprintf(stdout, "flushing %d bytes\n", blockLength);
|
|
323 |
}
|
|
324 |
RELEASE_ARRAYS();
|
|
325 |
if ((*env)->CallIntMethod(env, this, readID,
|
|
326 |
blockh, 0, blockLength + 1) != 0
|
|
327 |
|| (*env)->ExceptionOccurred(env))
|
|
328 |
{
|
|
329 |
/* quietly accept truncated GIF images */
|
|
330 |
return (!(*env)->ExceptionOccurred(env));
|
|
331 |
}
|
|
332 |
GET_ARRAYS();
|
|
333 |
blockLength = block[blockLength];
|
|
334 |
blockEnd = (blockLength == 0);
|
|
335 |
}
|
|
336 |
RELEASE_ARRAYS();
|
|
337 |
return 1;
|
|
338 |
}
|
|
339 |
|
|
340 |
/* It must be data: save code in CurCode */
|
|
341 |
curCode = code;
|
|
342 |
outCount = OUTCODELENGTH;
|
|
343 |
|
|
344 |
/* If greater or equal to freeCode, not in the hash table
|
|
345 |
* yet; repeat the last character decoded
|
|
346 |
*/
|
|
347 |
if (curCode >= freeCode) {
|
|
348 |
if (curCode > freeCode) {
|
|
349 |
/*
|
|
350 |
* if we get a code too far outside our range, it
|
|
351 |
* could case the parser to start traversing parts
|
|
352 |
* of our data structure that are out of range...
|
|
353 |
*/
|
|
354 |
goto flushit;
|
|
355 |
}
|
|
356 |
curCode = oldCode;
|
|
357 |
outCode[--outCount] = prevChar;
|
|
358 |
}
|
|
359 |
|
|
360 |
/* Unless this code is raw data, pursue the chain pointed
|
|
361 |
* to by curCode through the hash table to its end; each
|
|
362 |
* code in the chain puts its associated output code on
|
|
363 |
* the output queue.
|
|
364 |
*/
|
|
365 |
while (curCode > bitMask) {
|
|
366 |
outCode[--outCount] = suffix[curCode];
|
|
367 |
if (outCount == 0) {
|
|
368 |
/*
|
|
369 |
* In theory this should never happen since our
|
|
370 |
* prefix and suffix arrays are monotonically
|
|
371 |
* decreasing and so outCode will only be filled
|
|
372 |
* as much as those arrays, but I don't want to
|
|
373 |
* take that chance and the test is probably
|
|
374 |
* cheap compared to the read and write operations.
|
|
375 |
* If we ever do overflow the array, we will just
|
|
376 |
* flush the rest of the data and quietly accept
|
|
377 |
* the GIF as truncated here.
|
|
378 |
*/
|
|
379 |
goto flushit;
|
|
380 |
}
|
|
381 |
curCode = prefix[curCode];
|
|
382 |
}
|
|
383 |
|
|
384 |
/* The last code in the chain is treated as raw data. */
|
|
385 |
prevChar = (unsigned char)curCode;
|
|
386 |
outCode[--outCount] = prevChar;
|
|
387 |
|
|
388 |
/* Now we put the data out to the Output routine. It's
|
|
389 |
* been stacked LIFO, so deal with it that way...
|
|
390 |
*
|
|
391 |
* Note that for some malformed images we have to skip
|
|
392 |
* current frame and continue with rest of data
|
|
393 |
* because we may have not enough info to interpret
|
|
394 |
* corrupted frame correctly.
|
|
395 |
* However, we can not skip frame without decoding it
|
|
396 |
* and therefore we have to continue looping through data
|
|
397 |
* but skip internal output loop.
|
|
398 |
*
|
|
399 |
* In particular this is is possible when
|
|
400 |
* width of the frame is set to zero. If
|
|
401 |
* global width (i.e. width of the logical screen)
|
|
402 |
* is zero too then zero-length scanline buffer
|
|
403 |
* is allocated in java code and we have no buffer to
|
|
404 |
* store decoded data in.
|
|
405 |
*/
|
|
406 |
len = OUTCODELENGTH - outCount;
|
|
407 |
while ((width > 0) && (--len >= 0)) {
|
|
408 |
rasline[off++] = outCode[outCount++];
|
|
409 |
|
|
410 |
/* Update the X-coordinate, and if it overflows, update the
|
|
411 |
* Y-coordinate
|
|
412 |
*/
|
|
413 |
if (--x == 0) {
|
|
414 |
/* If a non-interlaced picture, just increment y to the next
|
|
415 |
* scan line. If it's interlaced, deal with the interlace as
|
|
416 |
* described in the GIF spec. Put the decoded scan line out
|
|
417 |
* to the screen if we haven't gone past the bottom of it
|
|
418 |
*/
|
|
419 |
int count;
|
|
420 |
RELEASE_ARRAYS();
|
|
421 |
count = (*env)->CallIntMethod(env, this, sendID,
|
|
422 |
relx, rely + y,
|
|
423 |
width, passht,
|
|
424 |
raslineh, cmh);
|
|
425 |
if (count <= 0 || (*env)->ExceptionOccurred(env)) {
|
|
426 |
/* Nobody is listening any more. */
|
|
427 |
if (verbose) {
|
|
428 |
fprintf(stdout, "Orphan gif decoder quitting\n");
|
|
429 |
}
|
|
430 |
return 0;
|
|
431 |
}
|
|
432 |
GET_ARRAYS();
|
|
433 |
x = width;
|
|
434 |
off = 0;
|
|
435 |
/* pass inc ht ystart */
|
|
436 |
/* 0 8 8 0 */
|
|
437 |
/* 1 8 4 4 */
|
|
438 |
/* 2 4 2 2 */
|
|
439 |
/* 3 2 1 1 */
|
|
440 |
y += passinc;
|
|
441 |
while (y >= height) {
|
|
442 |
passinc = passht;
|
|
443 |
passht >>= 1;
|
|
444 |
y = passht;
|
|
445 |
if (passht == 0) {
|
|
446 |
goto flushit;
|
|
447 |
}
|
|
448 |
}
|
|
449 |
}
|
|
450 |
}
|
|
451 |
|
|
452 |
/* Build the hash table on-the-fly. No table is stored in the file. */
|
|
453 |
prefix[freeCode] = (short)oldCode;
|
|
454 |
suffix[freeCode] = prevChar;
|
|
455 |
oldCode = code;
|
|
456 |
|
|
457 |
/* Point to the next slot in the table. If we exceed the
|
|
458 |
* maxCode, increment the code size unless
|
|
459 |
* it's already 12. If it is, do nothing: the next code
|
|
460 |
* decompressed better be CLEAR
|
|
461 |
*/
|
|
462 |
if (++freeCode >= maxCode) {
|
|
463 |
if (codeSize < 12) {
|
|
464 |
codeSize++;
|
|
465 |
maxCode <<= 1;
|
|
466 |
codeMask = maxCode - 1;
|
|
467 |
} else {
|
|
468 |
/* Just in case */
|
|
469 |
freeCode = maxCode - 1;
|
|
470 |
}
|
|
471 |
}
|
|
472 |
}
|
|
473 |
out_of_memory:
|
|
474 |
RELEASE_ARRAYS();
|
|
475 |
return 0;
|
|
476 |
}
|