diff options
Diffstat (limited to 'gnu/javax/imageio/gif/GIFFile.java')
-rw-r--r-- | gnu/javax/imageio/gif/GIFFile.java | 296 |
1 files changed, 148 insertions, 148 deletions
diff --git a/gnu/javax/imageio/gif/GIFFile.java b/gnu/javax/imageio/gif/GIFFile.java index ec0a3103d..941397a0e 100644 --- a/gnu/javax/imageio/gif/GIFFile.java +++ b/gnu/javax/imageio/gif/GIFFile.java @@ -43,14 +43,14 @@ import java.util.Vector; /** * GIFFile - reads a GIF file. - * + * * This class only does the bare minimum work, and returns the data in raw * formats (described below). The class is J2ME compatible, and hopefully * we can keep it that way without any significant overhead. * - * @author Sven de Marothy. + * @author Sven de Marothy. */ -public class GIFFile +public class GIFFile { // "NETSCAPE2.0" - identifier private final static byte[] nsBlock = new byte[] @@ -67,7 +67,7 @@ public class GIFFile * Extension block types */ private final static int EXTENSION_COMMENT = 254; - private final static int EXTENSION_GCONTROL = 249; + private final static int EXTENSION_GCONTROL = 249; private final static int EXTENSION_APPLICATION = 255; /** @@ -75,7 +75,7 @@ public class GIFFile */ private final static int UNDRAW_OVERWRITE = 1; private final static int UNDRAW_RESTORE_BACKGROUND = 2; - private final static int UNDRAW_RESTORE_PREVIOUS = 3; + private final static int UNDRAW_RESTORE_PREVIOUS = 3; /** * Image position and dimensions (images may be partial) @@ -121,7 +121,7 @@ public class GIFFile * Has transparency? */ private boolean hasTransparency; - + /** * Undraw mode (animations) */ @@ -181,7 +181,7 @@ public class GIFFile * @throws IOException if an I/O error occured. * @throws GIFException if some file parsing error occured */ - public GIFFile(InputStream in) throws IOException, GIFException + public GIFFile(InputStream in) throws IOException, GIFException { // Validate the signature if( !readSignature( in ) ) @@ -190,7 +190,7 @@ public class GIFFile { byte[] data = new byte[7]; if (in.read(data) != 7) - throw new IOException("Couldn't read global descriptor."); + throw new IOException("Couldn't read global descriptor."); globalWidth = ((data[1] & 0xFF) << 8) | (data[0] & 0xFF); globalHeight = ((data[3] & 0xFF) << 8) | (data[2] & 0xFF); @@ -202,37 +202,37 @@ public class GIFFile if( hasGlobalColorMap ) { - globalPalette = new byte[ nColors * 3 ]; - if( in.read( globalPalette ) != nColors * 3 ) - throw new IOException("Couldn't read color map."); + globalPalette = new byte[ nColors * 3 ]; + if( in.read( globalPalette ) != nColors * 3 ) + throw new IOException("Couldn't read color map."); } - + int c = in.read(); while( c == EXTENSION ) { - readExtension( in ); - c = in.read(); + readExtension( in ); + c = in.read(); } - + if( c != LOCAL ) throw new GIFException("Extension blocks not followed by a local descriptor ("+c+")"); loadImage( in ); - c = in.read(); + c = in.read(); if( c == TERMINATOR ) // Not an animated GIF. return; - // Load animation frames. Just quit if an error occurs instead + // Load animation frames. Just quit if an error occurs instead // of throwing an exception. animationFrames = new Vector(); - try + try { - while( c != TERMINATOR ) - { - animationFrames.add( new GIFFile( this, in, c ) ); - c = in.read(); - } + while( c != TERMINATOR ) + { + animationFrames.add( new GIFFile( this, in, c ) ); + c = in.read(); + } } catch(IOException ioe) { @@ -245,8 +245,8 @@ public class GIFFile /** * Constructor for additional animation frames. */ - private GIFFile(GIFFile parent, InputStream in, int c) - throws IOException, GIFException + private GIFFile(GIFFile parent, InputStream in, int c) + throws IOException, GIFException { // Copy global properties. globalWidth = parent.globalWidth; @@ -261,10 +261,10 @@ public class GIFFile while( c == EXTENSION ) { - readExtension( in ); + readExtension( in ); c = in.read(); } - + if( c != LOCAL ) throw new GIFException("Extension blocks not followed by a local descriptor ("+c+")"); @@ -279,21 +279,21 @@ public class GIFFile * @throws IOException if the signature could not be read. */ public static boolean readSignature( InputStream in ) throws IOException - { + { byte[] data = new byte[6]; if (in.read(data) != 6) throw new IOException("Couldn't read signature."); if( data[0] != 0x47 || data[1] != 0x49 || data[2] != 0x46 || - data[3] != 0x38 ) // GIF8 + data[3] != 0x38 ) // GIF8 return false; if( (data[4] != 0x39 && data[4] != 0x37) || // 7 | 9 - (data[5] != 0x61 && data[5] != 0x62) ) // 'a' or 'b' + (data[5] != 0x61 && data[5] != 0x62) ) // 'a' or 'b' return false; return true; } - + /** * Loads the image local descriptor and then loads/decodes the image raster, @@ -306,13 +306,13 @@ public class GIFFile try { - decodeRaster( in ); + decodeRaster( in ); } catch(ArrayIndexOutOfBoundsException aioobe) { - throw new GIFException("Error decompressing image."); + throw new GIFException("Error decompressing image."); } - + if( interlaced ) // Clean up deinterlace(); packPixels(); @@ -330,31 +330,31 @@ public class GIFFile return; int nbits = 1; - int ppbyte = 8; + int ppbyte = 8; if( nColors == 4 ) { - nbits = 2; - ppbyte = 4; + nbits = 2; + ppbyte = 4; } else if( nColors == 16 ) { - nbits = 4; - ppbyte = 2; + nbits = 4; + ppbyte = 2; } int rem = (width & (ppbyte - 1)); - int w = ( rem == 0 ) ? (width / ppbyte) : - ((width + ppbyte - rem) / ppbyte); + int w = ( rem == 0 ) ? (width / ppbyte) : + ((width + ppbyte - rem) / ppbyte); byte[] nr = new byte[ w * height ]; for(int j = 0; j < height; j++) { - for(int i = 0; i < width - ppbyte; i += ppbyte) - for(int k = 0; k < ppbyte; k++) - nr[ j * w + (i / ppbyte) ] |= (byte)((raster[ width * j + i + k ] - << (8 - nbits * (1 + k)))); - for(int i = 0; i < rem; i++) - nr[ j * w + w - 1 ] |= (byte)((raster[ width * j + width - rem + i ] - << (nbits * (rem - i)))); + for(int i = 0; i < width - ppbyte; i += ppbyte) + for(int k = 0; k < ppbyte; k++) + nr[ j * w + (i / ppbyte) ] |= (byte)((raster[ width * j + i + k ] + << (8 - nbits * (1 + k)))); + for(int i = 0; i < rem; i++) + nr[ j * w + w - 1 ] |= (byte)((raster[ width * j + width - rem + i ] + << (nbits * (rem - i)))); } raster = nr; } @@ -424,23 +424,23 @@ public class GIFFile int n = 0; for(int i = 0; i < ((height + 7) >> 3); i++) { - System.arraycopy( raster, n, nr, width * i * 8, width ); - n += width; + System.arraycopy( raster, n, nr, width * i * 8, width ); + n += width; } for(int i = 0; i < ((height + 3) >> 3); i++) { - System.arraycopy( raster, n, nr, width * ( 8 * i + 4 ), width ); - n += width; + System.arraycopy( raster, n, nr, width * ( 8 * i + 4 ), width ); + n += width; } for(int i = 0; i < (height >> 2); i++) { - System.arraycopy( raster, n, nr, width * (4 * i + 2), width ); - n += width; + System.arraycopy( raster, n, nr, width * (4 * i + 2), width ); + n += width; } for(int i = 0; i < (height >> 1); i++) { - System.arraycopy( raster, n, nr, width * (2 * i + 1), width ); - n += width; + System.arraycopy( raster, n, nr, width * (2 * i + 1), width ); + n += width; } raster = nr; } @@ -461,17 +461,17 @@ public class GIFFile interlaced = (( flags & 0x40 ) != 0); if( (flags & 0x80) != 0 ) { // has a local color map - int nLocalColors = (1 << (( flags & 0x07) + 1)); - if( !hasGlobalColorMap ) - nColors = nLocalColors; - localPalette = new byte[ nLocalColors * 3 ]; - if( in.read( localPalette ) != nLocalColors * 3 ) - throw new IOException("Couldn't read color map."); + int nLocalColors = (1 << (( flags & 0x07) + 1)); + if( !hasGlobalColorMap ) + nColors = nLocalColors; + localPalette = new byte[ nLocalColors * 3 ]; + if( in.read( localPalette ) != nLocalColors * 3 ) + throw new IOException("Couldn't read color map."); } - } + } /** - * Returns the image's palette in raw format + * Returns the image's palette in raw format * (r0,g0,b0,r1,g1,b2..r(Ncolors-1),g(Ncolors-1),b(Ncolors-1)) */ public byte[] getRawPalette() @@ -514,41 +514,41 @@ public class GIFFile /** * Handles extension blocks. */ - private void readExtension(InputStream in) throws IOException, GIFException + private void readExtension(InputStream in) throws IOException, GIFException { int functionCode = in.read(); byte[] data = readData(in); switch( functionCode ) { case EXTENSION_COMMENT: // comment block - comment = new String(data, "8859_1"); - break; + comment = new String(data, "8859_1"); + break; case EXTENSION_GCONTROL: // Graphics control extension - undraw = (data[0] & 0x1C) >> 2; - // allegedly there can be bad values of this. - if( undraw < 1 && undraw > 3 ) undraw = 1; - hasTransparency = ((data[0] & 0x01) == 1); - transparentIndex = (data[3] & 0xFF); - duration = ((data[2] & 0xFF) << 8) | (data[1] & 0xFF); - break; - - // Application extension. We only parse the Netscape animation - // extension here. Which is the only one most use anyway. + undraw = (data[0] & 0x1C) >> 2; + // allegedly there can be bad values of this. + if( undraw < 1 && undraw > 3 ) undraw = 1; + hasTransparency = ((data[0] & 0x01) == 1); + transparentIndex = (data[3] & 0xFF); + duration = ((data[2] & 0xFF) << 8) | (data[1] & 0xFF); + break; + + // Application extension. We only parse the Netscape animation + // extension here. Which is the only one most use anyway. case EXTENSION_APPLICATION: - boolean isNS = true; - for(int i = 0; i < nsBlock.length; i++ ) - if( nsBlock[i] != data[i] ) - isNS = false; - if( isNS ) - { - isLooped = true; - loops = ((data[12] & 0xFF) << 8) | (data[13] & 0xFF); - } - break; + boolean isNS = true; + for(int i = 0; i < nsBlock.length; i++ ) + if( nsBlock[i] != data[i] ) + isNS = false; + if( isNS ) + { + isLooped = true; + loops = ((data[12] & 0xFF) << 8) | (data[13] & 0xFF); + } + break; default: - break; + break; } } @@ -563,11 +563,11 @@ public class GIFFile int n = in.read(); do { - totalBytes += n; - byte[] block = new byte[ n ]; - in.read(block); - v.add(block); - n = in.read(); + totalBytes += n; + byte[] block = new byte[ n ]; + in.read(block); + v.add(block); + n = in.read(); } while( n > 0 ); @@ -575,9 +575,9 @@ public class GIFFile byte[] bigBuffer = new byte[ totalBytes ]; for( int i = 0; i < v.size(); i++ ) { - byte[] block = (byte[])v.elementAt(i); - System.arraycopy(block, 0, bigBuffer, n, block.length); - n += block.length; + byte[] block = (byte[])v.elementAt(i); + System.arraycopy(block, 0, bigBuffer, n, block.length); + n += block.length; } return bigBuffer; } @@ -617,65 +617,65 @@ public class GIFFile for(short i = 0; i < nColors; i ++ ) { - dictionary[i][0] = i; // color index - dictionary[i][1] = -1; // parent - dictionary[i][2] = i; // first - dictionary[i][3] = 1; // depth + dictionary[i][0] = i; // color index + dictionary[i][1] = -1; // parent + dictionary[i][2] = i; // first + dictionary[i][3] = 1; // depth } code = getBits( codeSize ); // get second code raster[ rasterIndex++ ] = (byte)dictionary[code][0]; - int old = code; + int old = code; code = getBits( codeSize ); // start at the third code int c; do { - if( code == clearCode ) - { - codeSize = initialCodeSize + 1; - nextCode = endCode + 1; - // get and output second code - code = getBits( codeSize ); - raster[ rasterIndex++ ] = (byte)dictionary[code][0]; - old = code; - } - else - { - dictionary[nextCode][1] = (short)old; // parent = old - dictionary[nextCode][2] = dictionary[old][2]; // first pixel - dictionary[nextCode][3] = (short)(dictionary[old][3] + 1); // depth - - // appended pixel = first pixel of c - if( code < nextCode ) - { - dictionary[nextCode][0] = dictionary[code][2]; - old = code; - } - else // first of old - { - dictionary[nextCode][0] = dictionary[old][2]; - old = nextCode; - } - - c = old; - // output the code c - int depth = dictionary[c][3]; - for( int i = depth - 1; i >= 0; i-- ) - { - raster[ rasterIndex + i ] = (byte)dictionary[c][0]; - c = dictionary[c][1]; // go to parent. - } - rasterIndex += depth; - nextCode ++; - - if( codeSize < 12 && nextCode >= (1 << codeSize) ) - codeSize++; - } - code = getBits( codeSize ); + if( code == clearCode ) + { + codeSize = initialCodeSize + 1; + nextCode = endCode + 1; + // get and output second code + code = getBits( codeSize ); + raster[ rasterIndex++ ] = (byte)dictionary[code][0]; + old = code; + } + else + { + dictionary[nextCode][1] = (short)old; // parent = old + dictionary[nextCode][2] = dictionary[old][2]; // first pixel + dictionary[nextCode][3] = (short)(dictionary[old][3] + 1); // depth + + // appended pixel = first pixel of c + if( code < nextCode ) + { + dictionary[nextCode][0] = dictionary[code][2]; + old = code; + } + else // first of old + { + dictionary[nextCode][0] = dictionary[old][2]; + old = nextCode; + } + + c = old; + // output the code c + int depth = dictionary[c][3]; + for( int i = depth - 1; i >= 0; i-- ) + { + raster[ rasterIndex + i ] = (byte)dictionary[c][0]; + c = dictionary[c][1]; // go to parent. + } + rasterIndex += depth; + nextCode ++; + + if( codeSize < 12 && nextCode >= (1 << codeSize) ) + codeSize++; + } + code = getBits( codeSize ); } while( code != endCode && dataBlockIndex < compressedData.length ); - + compressedData = null; // throw away compressed data. } @@ -686,9 +686,9 @@ public class GIFFile { while( nbits > remainingBits ) { - int c = (compressedData[ dataBlockIndex++ ] & 0xFF) << remainingBits; - currentBits |= c; - remainingBits += 8; + int c = (compressedData[ dataBlockIndex++ ] & 0xFF) << remainingBits; + currentBits |= c; + remainingBits += 8; } int rval = (currentBits & ((1 << nbits) - 1)); currentBits = (currentBits >> nbits); @@ -699,7 +699,7 @@ public class GIFFile /** * Generic exception used by GIFFile to report decoding errors. */ - public static class GIFException extends Exception + public static class GIFException extends Exception { public GIFException(String message) { |