2362N/A * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A * the need to copy buffer elements. 0N/A * - carefully insert pairs of RELEASE_ARRAYS and GET_ARRAYS around 0N/A * callbacks to Java. 0N/A * - call RELEASE_ARRAYS before returning to Java. 0N/A * Otherwise things will go horribly wrong. There may be memory leaks, 0N/A * excessive pinning, or even VM crashes! 0N/A * Note that GetPrimitiveArrayCritical may fail! 0N/A * Note that it is important to check whether the arrays are NULL, 0N/A * because GetPrimitiveArrayCritical might have failed. 0N/A/* Place holders for the old native interface. */ 0N/A /* Patrick Naughton: 0N/A * Note that I ignore the possible existence of a local color map. 0N/A * I'm told there aren't many files around that use them, and the 0N/A * spec says it's defined for future use. This could lead to an 0N/A * error reading some files. 0N/A * Start reading the image data. First we get the intial code size 0N/A * and compute decompressor constant values, based on this code 0N/A * The GIF spec has it that the code size is the code size used to 0N/A * compute the above values is the code size given in the file, 0N/A * size given in the file plus one. (thus the ++). 0N/A * The following narly code reads LZW compressed data blocks and 0N/A * dumps it into the image data. The input stream is broken up into 0N/A * blocks of 1-255 characters, each preceded by a length byte. 0N/A * 3-12 bit codes are read from these blocks. The codes correspond to 0N/A * entry is the hashtable (the prefix, suffix stuff), and the appropriate 0N/A * pixels are written to the image. 0N/A /* Variables used to form reading data */ 0N/A /* Variables used to decompress the data */ 0N/A /* Temproray storage for decompression */ 0N/A /* Variables used for writing pixels */ 1125N/A /* We have verified the initial code size on the java layer. 1125N/A * Here we just check bounds for particular indexes. */ 0N/A /* Fix for bugid 4216605 Some animated GIFs display corrupted. */ 0N/A /* Read codes until the eofCode is encountered */ 0N/A /* fill the buffer if needed */ 0N/A /* Sometimes we have one last byte to process... */ 0N/A /* quietly accept truncated GIF images */ 0N/A /* move remaining bytes to the beginning of the buffer */ 0N/A /* fill the block */ 0N/A /* 2 bytes at a time saves checking for accumbits < codeSize. 0N/A * We know we'll get enough and also that we can't overflow 0N/A * since codeSize <= 12. 0N/A /* Compute the code */ 0N/A * Interpret the code 0N/A /* Clear code sets everything back to its initial value, then 0N/A * reads the immediately subsequent code as uncompressed data. 0N/A /* Note that freeCode is one less than it is supposed to be, 0N/A * this is because it will be incremented next time round the loop 0N/A /* Continue if we've NOT reached the end, some Gif images 0N/A * contain bogus codes after the last clear code. 0N/A /* pretend we've reached the end of the data */ 0N/A /* make sure we read the whole block of pixels. */ 0N/A /* quietly accept truncated GIF images */ 0N/A /* It must be data: save code in CurCode */ 0N/A /* If greater or equal to freeCode, not in the hash table 0N/A * yet; repeat the last character decoded 0N/A * if we get a code too far outside our range, it 0N/A * could case the parser to start traversing parts 0N/A * of our data structure that are out of range... 0N/A /* Unless this code is raw data, pursue the chain pointed 0N/A * to by curCode through the hash table to its end; each 0N/A * code in the chain puts its associated output code on 0N/A * In theory this should never happen since our 0N/A * prefix and suffix arrays are monotonically 0N/A * decreasing and so outCode will only be filled 0N/A * as much as those arrays, but I don't want to 0N/A * take that chance and the test is probably 0N/A * cheap compared to the read and write operations. 0N/A * If we ever do overflow the array, we will just 0N/A * flush the rest of the data and quietly accept 0N/A * the GIF as truncated here. 0N/A /* The last code in the chain is treated as raw data. */ 0N/A /* Now we put the data out to the Output routine. It's 0N/A * been stacked LIFO, so deal with it that way... 0N/A * Note that for some malformed images we have to skip 0N/A * current frame and continue with rest of data 0N/A * because we may have not enough info to interpret 0N/A * corrupted frame correctly. 0N/A * However, we can not skip frame without decoding it 0N/A * and therefore we have to continue looping through data 0N/A * but skip internal output loop. 0N/A * In particular this is is possible when 0N/A * width of the frame is set to zero. If 0N/A * global width (i.e. width of the logical screen) 0N/A * is zero too then zero-length scanline buffer 0N/A * is allocated in java code and we have no buffer to 0N/A * store decoded data in. 0N/A /* Update the X-coordinate, and if it overflows, update the 0N/A /* If a non-interlaced picture, just increment y to the next 0N/A * scan line. If it's interlaced, deal with the interlace as 0N/A * described in the GIF spec. Put the decoded scan line out 0N/A * to the screen if we haven't gone past the bottom of it 0N/A /* Nobody is listening any more. */ 0N/A /* pass inc ht ystart */ 0N/A /* Build the hash table on-the-fly. No table is stored in the file. */ 0N/A /* Point to the next slot in the table. If we exceed the 0N/A * maxCode, increment the code size unless 0N/A * it's already 12. If it is, do nothing: the next code 0N/A * decompressed better be CLEAR