diff options
author | Sam Judd <judds@google.com> | 2014-07-02 14:14:58 -0700 |
---|---|---|
committer | Sam Judd <judds@google.com> | 2014-07-02 16:30:42 -0700 |
commit | fe090f50f3040f4d478143a3e0ffa8cdf813fefc (patch) | |
tree | d1ad99c1b58aa5a583a1d1bc63817d915e6d1701 /third_party/gif_decoder/src/main/java/com/bumptech/glide | |
parent | f59f35a759798ea0a5751e34a2585606fc9796af (diff) | |
download | glide-fe090f50f3040f4d478143a3e0ffa8cdf813fefc.tar.gz |
Add checkstyle plugin and and fix style issues.
Diffstat (limited to 'third_party/gif_decoder/src/main/java/com/bumptech/glide')
4 files changed, 254 insertions, 223 deletions
diff --git a/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifDecoder.java b/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifDecoder.java index 2381a7a6..4d653cb5 100644 --- a/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifDecoder.java +++ b/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifDecoder.java @@ -59,7 +59,7 @@ public class GifDecoder { */ public static final int STATUS_OK = 0; /** - * File read status: Error decoding file (may be partially decoded) + * File read status: Error decoding file (may be partially decoded). */ public static final int STATUS_FORMAT_ERROR = 1; /** @@ -67,36 +67,37 @@ public class GifDecoder { */ public static final int STATUS_OPEN_ERROR = 2; /** - * max decoder pixel stack size + * max decoder pixel stack size. */ private static final int MAX_STACK_SIZE = 4096; /** - * GIF Disposal Method meaning take no action + * GIF Disposal Method meaning take no action. */ private static final int DISPOSAL_UNSPECIFIED = 0; /** - * GIF Disposal Method meaning leave canvas from previous frame + * GIF Disposal Method meaning leave canvas from previous frame. */ - private static final int DISPOSAL_NONE = 1; +// private static final int DISPOSAL_NONE = 1; /** - * GIF Disposal Method meaning clear canvas to background color + * GIF Disposal Method meaning clear canvas to background color. */ private static final int DISPOSAL_BACKGROUND = 2; /** - * GIF Disposal Method meaning clear canvas to frame before last + * GIF Disposal Method meaning clear canvas to frame before last. */ - private static final int DISPOSAL_PREVIOUS = 3; +// private static final int DISPOSAL_PREVIOUS = 3; - //Global File Header values and parsing flags - private int[] act; // active color table + // Global File Header values and parsing flags. + // Active color table. + private int[] act; - // Raw GIF data from input source + // Raw GIF data from input source. private ByteBuffer rawData; - // Raw data read working array - private byte[] block = new byte[256]; // current data block - // LZW decoder working arrays + // Raw data read working array. + private byte[] block = new byte[256]; + // LZW decoder working arrays. private short[] prefix; private byte[] suffix; private byte[] pixelStack; @@ -130,10 +131,6 @@ public class GifDecoder { return header.isTransparent; } - public int getGifByteSize() { - return data.length; - } - public byte[] getData() { return data; } @@ -144,7 +141,7 @@ public class GifDecoder { } /** - * Move the animation frame counter forward + * Move the animation frame counter forward. */ public void advance() { framePointer = (framePointer + 1) % header.frameCount; @@ -153,8 +150,8 @@ public class GifDecoder { /** * Gets display duration for specified frame. * - * @param n int index of frame - * @return delay in milliseconds + * @param n int index of frame. + * @return delay in milliseconds. */ public int getDelay(int n) { int delay = -1; @@ -165,7 +162,7 @@ public class GifDecoder { } /** - * Gets display duration for the upcoming frame + * Gets display duration for the upcoming frame. */ public int getNextDelay() { if (header.frameCount <= 0 || framePointer < 0) { @@ -178,23 +175,23 @@ public class GifDecoder { /** * Gets the number of frames read from file. * - * @return frame count + * @return frame count. */ public int getFrameCount() { return header.frameCount; } /** - * Gets the current index of the animation frame, or -1 if animation hasn't not yet started + * Gets the current index of the animation frame, or -1 if animation hasn't not yet started. * - * @return frame index + * @return frame index. */ public int getCurrentFrameIndex() { return framePointer; } /** - * Gets the "Netscape" iteration count, if any. A count of 0 means repeat indefinitiely. + * Gets the "Netscape" iteration count, if any. A count of 0 means repeat indefinitely. * * @return iteration count if one was specified, else 1. */ @@ -209,16 +206,16 @@ public class GifDecoder { /** * Get the next frame in the animation sequence. * - * @return Bitmap representation of frame + * @return Bitmap representation of frame. */ public Bitmap getNextFrame() { - if (header.frameCount <= 0 || framePointer < 0 ) { + if (header.frameCount <= 0 || framePointer < 0) { return null; } GifFrame frame = header.frames.get(framePointer); - //Set the appropriate color table + // Set the appropriate color table. if (frame.lct == null) { act = header.gct; } else { @@ -231,15 +228,18 @@ public class GifDecoder { int save = 0; if (frame.transparency) { save = act[frame.transIndex]; - act[frame.transIndex] = 0; // set transparent color if specified + // Set transparent color if specified. + act[frame.transIndex] = 0; } if (act == null) { Log.w(TAG, "No Valid Color Table"); - header.status = STATUS_FORMAT_ERROR; // no color table defined + // No color table defined. + header.status = STATUS_FORMAT_ERROR; return null; } - Bitmap result = setPixels(framePointer); // transfer pixel data to image + // Transfer pixel data to image. + Bitmap result = setPixels(framePointer); // Reset the transparent pixel in the color table if (frame.transparency) { @@ -250,10 +250,10 @@ public class GifDecoder { } /** - * Reads GIF image from stream + * Reads GIF image from stream. * * @param is containing GIF file. - * @return read status code (0 = no errors) + * @return read status code (0 = no errors). */ public int read(InputStream is, int contentLength) { if (is != null) { @@ -290,32 +290,32 @@ public class GifDecoder { this.id = id; this.header = header; this.data = data; - //Initialize the raw data buffer + // Initialize the raw data buffer. rawData = ByteBuffer.wrap(data); rawData.rewind(); rawData.order(ByteOrder.LITTLE_ENDIAN); - //Now that we know the size, init scratch arrays + // Now that we know the size, init scratch arrays. mainPixels = new byte[header.width * header.height]; mainScratch = new int[header.width * header.height]; } /** - * Reads GIF image from byte array + * Reads GIF image from byte array. * * @param data containing GIF file. - * @return read status code (0 = no errors) + * @return read status code (0 = no errors). */ public int read(byte[] data) { this.data = data; this.header = new GifHeaderParser(data).parseHeader(); if (data != null) { - //Initialize the raw data buffer + // Initialize the raw data buffer. rawData = ByteBuffer.wrap(data); rawData.rewind(); rawData.order(ByteOrder.LITTLE_ENDIAN); - //Now that we know the size, init scratch arrays + // Now that we know the size, init scratch arrays. mainPixels = new byte[header.width * header.height]; mainScratch = new int[header.width * header.height]; } @@ -334,16 +334,11 @@ public class GifDecoder { previousFrame = header.frames.get(previousIndex); } - // final location of blended pixels + // Final location of blended pixels. final int[] dest = mainScratch; - // fill in starting image contents based on last image's dispose code + // Fill in starting image contents based on last image's dispose code. if (previousFrame != null && previousFrame.dispose > DISPOSAL_UNSPECIFIED) { -// if (previousFrame.dispose == DISPOSAL_NONE) { -// We don't need to do anything for this case, mainScratch should already have the pixels of the -// previous image. -// currentImage.getPixels(dest, 0, header.width, 0, 0, header.width, header.height); -// } if (previousFrame.dispose == DISPOSAL_BACKGROUND) { // Start with a canvas filled with the background color int c = 0; @@ -368,10 +363,10 @@ public class GifDecoder { } } - // Decode pixels for this frame into the global pixels[] scratch - decodeBitmapData(currentFrame, mainPixels); // decode pixel data + // Decode pixels for this frame into the global pixels[] scratch. + decodeBitmapData(currentFrame); - // copy each source line to the appropriate place in the destination + // Copy each source line to the appropriate place in the destination. int pass = 1; int inc = 8; int iline = 0; @@ -402,14 +397,18 @@ public class GifDecoder { line += currentFrame.iy; if (line < header.height) { int k = line * header.width; - int dx = k + currentFrame.ix; // start of line in dest - int dlim = dx + currentFrame.iw; // end of dest line + // Start of line in dest. + int dx = k + currentFrame.ix; + // End of dest line. + int dlim = dx + currentFrame.iw; if ((k + header.width) < dlim) { - dlim = k + header.width; // past dest edge + // Past dest edge. + dlim = k + header.width; } - int sx = i * currentFrame.iw; // start of line in source + // Start of line in source. + int sx = i * currentFrame.iw; while (dx < dlim) { - // map color and insert in destination + // Map color and insert in destination. int index = ((int) mainPixels[sx++]) & 0xff; int c = act[index]; if (c != 0) { @@ -420,7 +419,7 @@ public class GifDecoder { } } - //Set pixels for current image + // Set pixels for current image. Bitmap result = getNextBitmap(); result.setPixels(dest, 0, header.width, 0, 0, header.width, header.height); return result; @@ -429,18 +428,20 @@ public class GifDecoder { /** * Decodes LZW image data into pixel array. Adapted from John Cristy's BitmapMagick. */ - private void decodeBitmapData(GifFrame frame, byte[] dstPixels) { + private void decodeBitmapData(GifFrame frame) { if (frame != null) { - //Jump to the frame start position + // Jump to the frame start position. rawData.position(frame.bufferFrameStart); } int nullCode = -1; int npix = (frame == null) ? header.width * header.height : frame.iw * frame.ih; - int available, clear, code_mask, code_size, end_of_information, in_code, old_code, bits, code, count, i, datum, data_size, first, top, bi, pi; + int available, clear, codeMask, codeSize, endOfInformation, inCode, oldCode, bits, code, count, i, datum, + dataSize, first, top, bi, pi; - if (dstPixels == null || dstPixels.length < npix) { - dstPixels = new byte[npix]; // allocate new pixel array + if (mainPixels == null || mainPixels.length < npix) { + // Allocate new pixel array. + mainPixels = new byte[npix]; } if (prefix == null) { prefix = new short[MAX_STACK_SIZE]; @@ -453,15 +454,16 @@ public class GifDecoder { } // Initialize GIF data stream decoder. - data_size = read(); - clear = 1 << data_size; - end_of_information = clear + 1; + dataSize = read(); + clear = 1 << dataSize; + endOfInformation = clear + 1; available = clear + 2; - old_code = nullCode; - code_size = data_size + 1; - code_mask = (1 << code_size) - 1; + oldCode = nullCode; + codeSize = dataSize + 1; + codeMask = (1 << codeSize) - 1; for (code = 0; code < clear; code++) { - prefix[code] = 0; // XXX ArrayIndexOutOfBoundsException + // XXX ArrayIndexOutOfBoundsException. + prefix[code] = 0; suffix[code] = (byte) code; } @@ -469,7 +471,7 @@ public class GifDecoder { datum = bits = count = first = top = pi = bi = 0; for (i = 0; i < npix; ) { if (top == 0) { - if (bits < code_size) { + if (bits < codeSize) { // Load bytes until there are enough bits for a code. if (count == 0) { // Read a new data block. @@ -486,59 +488,60 @@ public class GifDecoder { continue; } // Get the next code. - code = datum & code_mask; - datum >>= code_size; - bits -= code_size; - // Interpret the code - if ((code > available) || (code == end_of_information)) { + code = datum & codeMask; + datum >>= codeSize; + bits -= codeSize; + // Interpret the code. + if ((code > available) || (code == endOfInformation)) { break; } if (code == clear) { // Reset decoder. - code_size = data_size + 1; - code_mask = (1 << code_size) - 1; + codeSize = dataSize + 1; + codeMask = (1 << codeSize) - 1; available = clear + 2; - old_code = nullCode; + oldCode = nullCode; continue; } - if (old_code == nullCode) { + if (oldCode == nullCode) { pixelStack[top++] = suffix[code]; - old_code = code; + oldCode = code; first = code; continue; } - in_code = code; + inCode = code; if (code == available) { pixelStack[top++] = (byte) first; - code = old_code; + code = oldCode; } while (code > clear) { pixelStack[top++] = suffix[code]; code = prefix[code]; } first = ((int) suffix[code]) & 0xff; - // Add a new string to the string table, + // Add a new string to the string table. if (available >= MAX_STACK_SIZE) { break; } pixelStack[top++] = (byte) first; - prefix[available] = (short) old_code; + prefix[available] = (short) oldCode; suffix[available] = (byte) first; available++; - if (((available & code_mask) == 0) && (available < MAX_STACK_SIZE)) { - code_size++; - code_mask += available; + if (((available & codeMask) == 0) && (available < MAX_STACK_SIZE)) { + codeSize++; + codeMask += available; } - old_code = in_code; + oldCode = inCode; } // Pop a pixel off the pixel stack. top--; - dstPixels[pi++] = pixelStack[top]; + mainPixels[pi++] = pixelStack[top]; i++; } + // Clear missing pixels. for (i = pi; i < npix; i++) { - dstPixels[i] = 0; // clear missing pixels + mainPixels[i] = 0; } } @@ -548,7 +551,7 @@ public class GifDecoder { private int read() { int curByte = 0; try { - curByte = (rawData.get() & 0xFF); + curByte = rawData.get() & 0xFF; } catch (Exception e) { header.status = STATUS_FORMAT_ERROR; } @@ -558,7 +561,7 @@ public class GifDecoder { /** * Reads next variable length block from input. * - * @return number of bytes stored in "buffer" + * @return number of bytes stored in "buffer". */ private int readBlock() { int blockSize = read(); diff --git a/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifFrame.java b/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifFrame.java index a315350c..aebe6d64 100644 --- a/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifFrame.java +++ b/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifFrame.java @@ -1,21 +1,21 @@ package com.bumptech.glide.gifdecoder; /** - * Inner model class housing metadata for each frame + * Inner model class housing metadata for each frame. */ class GifFrame { - public int ix, iy, iw, ih; - /* Control Flags */ - public boolean interlace; - public boolean transparency; - /* Disposal Method */ - public int dispose; - /* Transparency Index */ - public int transIndex; - /* Delay, in ms, to next frame */ - public int delay; - /* Index in the raw buffer where we need to start reading to decode */ - public int bufferFrameStart; - /* Local Color Table */ - public int[] lct; + int ix, iy, iw, ih; + /* Control Flags. */ + boolean interlace; + boolean transparency; + /* Disposal Method. */ + int dispose; + /* Transparency Index. */ + int transIndex; + /* Delay, in ms, to next frame. */ + int delay; + /* Index in the raw buffer where we need to start reading to decode. */ + int bufferFrameStart; + /* Local Color Table. */ + int[] lct; } diff --git a/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifHeader.java b/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifHeader.java index 4c062088..85604029 100644 --- a/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifHeader.java +++ b/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifHeader.java @@ -5,27 +5,33 @@ import java.util.List; public class GifHeader { - public int[] gct = null; + int[] gct = null; /** - * Global status code of GIF data parsing + * Global status code of GIF data parsing. */ - public int status = GifDecoder.STATUS_OK; - public int frameCount = 0; + int status = GifDecoder.STATUS_OK; + int frameCount = 0; - public GifFrame currentFrame; - public List<GifFrame> frames = new ArrayList<GifFrame>(); - // logical screen size - public int width; // full image width - public int height; // full image height + GifFrame currentFrame; + List<GifFrame> frames = new ArrayList<GifFrame>(); + // Logical screen size. + // Full image width. + int width; + // Full image height. + int height; - public boolean gctFlag; // 1 : global color table flag - // 2-4 : color resolution - // 5 : gct sort flag - public int gctSize; // 6-8 : gct size - public int bgIndex; // background color index - public int pixelAspect; // pixel aspect ratio + // 1 : global color table flag. + boolean gctFlag; + // 2-4 : color resolution. + // 5 : gct sort flag. + // 6-8 : gct size. + int gctSize; + // Background color index. + int bgIndex; + // Pixel aspect ratio. + int pixelAspect; //TODO: this is set both during reading the header and while decoding frames... - public int bgColor; - public boolean isTransparent; - public int loopCount; + int bgColor; + boolean isTransparent; + int loopCount; } diff --git a/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifHeaderParser.java b/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifHeaderParser.java index 6237c3cf..af1844d9 100644 --- a/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifHeaderParser.java +++ b/third_party/gif_decoder/src/main/java/com/bumptech/glide/gifdecoder/GifHeaderParser.java @@ -11,18 +11,17 @@ import static com.bumptech.glide.gifdecoder.GifDecoder.STATUS_FORMAT_ERROR; public class GifHeaderParser { public static final String TAG = "GifHeaderParser"; /** - * max decoder pixel stack size + * Max decoder pixel stack size. */ private static final int MAX_STACK_SIZE = 4096; private final ByteBuffer rawData; private GifHeader header = new GifHeader(); - // Raw data read working array - protected byte[] block = new byte[256]; // current data block - protected int blockSize = 0; // block size last graphic control extension info - protected boolean lctFlag; // local color table flag - protected int lctSize; // local color table size + // Raw data read working array. + private byte[] block = new byte[256]; + // Block size last graphic control extension info. + private int blockSize = 0; private short[] prefix; private byte[] suffix; private byte[] pixelStack; @@ -58,23 +57,27 @@ public class GifHeaderParser { * Main file parser. Reads GIF content blocks. */ protected void readContents() { - // read GIF file content blocks + // Read GIF file content blocks. boolean done = false; while (!(done || err())) { int code = read(); switch (code) { - case 0x2C: // image separator + // Image separator. + case 0x2C: readBitmap(); break; - case 0x21: // extension + // Extension. + case 0x21: code = read(); switch (code) { - case 0xf9: // graphics control extension - //Start a new frame + // Graphics control extension. + case 0xf9: + // Start a new frame. header.currentFrame = new GifFrame(); readGraphicControlExt(); break; - case 0xff: // application extension + // Application extension. + case 0xff: readBlock(); String app = ""; for (int i = 0; i < 11; i++) { @@ -83,23 +86,29 @@ public class GifHeaderParser { if (app.equals("NETSCAPE2.0")) { readNetscapeExt(); } else { - skip(); // don't care + // Don't care. + skip(); } break; - case 0xfe:// comment extension + // Comment extension. + case 0xfe: skip(); break; - case 0x01:// plain text extension + // Plain text extension. + case 0x01: skip(); break; - default: // uninteresting extension + // Uninteresting extension. + default: skip(); } break; - case 0x3b: // terminator + // Terminator. + case 0x3b: done = true; break; - case 0x00: // bad byte, but keep going and see what happens break; + // Bad byte, but keep going and see what happens break; + case 0x00: default: header.status = STATUS_FORMAT_ERROR; } @@ -107,47 +116,60 @@ public class GifHeaderParser { } /** - * Reads Graphics Control Extension values + * Reads Graphics Control Extension values. */ protected void readGraphicControlExt() { - read(); // block size - int packed = read(); // packed fields - header.currentFrame.dispose = (packed & 0x1c) >> 2; // disposal method + // Block size. + read(); + // Packed fields. + int packed = read(); + // Disposal method. + header.currentFrame.dispose = (packed & 0x1c) >> 2; if (header.currentFrame.dispose == 0) { - header.currentFrame.dispose = 1; // elect to keep old image if discretionary + // Elect to keep old image if discretionary. + header.currentFrame.dispose = 1; } header.currentFrame.transparency = (packed & 1) != 0; header.isTransparent |= header.currentFrame.transparency; - header.currentFrame.delay = readShort() * 10; // delay in milliseconds - header.currentFrame.transIndex = read(); // transparent color index - read(); // block terminator + // Delay in milliseconds. + header.currentFrame.delay = readShort() * 10; + // Transparent color index + header.currentFrame.transIndex = read(); + // Block terminator + read(); } - /** - * Reads next frame image + /** + * Reads next frame image. */ protected void readBitmap() { - header.currentFrame.ix = readShort(); // (sub)image position & size + // (sub)image position & size. + header.currentFrame.ix = readShort(); header.currentFrame.iy = readShort(); header.currentFrame.iw = readShort(); header.currentFrame.ih = readShort(); int packed = read(); - lctFlag = (packed & 0x80) != 0; // 1 - local color table flag interlace - lctSize = (int) Math.pow(2, (packed & 0x07) + 1); + // 1 - local color table flag interlace + boolean lctFlag = (packed & 0x80) != 0; + int lctSize = (int) Math.pow(2, (packed & 0x07) + 1); // 3 - sort flag // 4-5 - reserved lctSize = 2 << (packed & 7); // 6-8 - local color // table size header.currentFrame.interlace = (packed & 0x40) != 0; if (lctFlag) { - header.currentFrame.lct = readColorTable(lctSize); // read table + // Read table. + header.currentFrame.lct = readColorTable(lctSize); } else { - header.currentFrame.lct = null; //No local color table + // No local color table. + header.currentFrame.lct = null; } - header.currentFrame.bufferFrameStart = rawData.position(); //Save this as the decoding position pointer + // Save this as the decoding position pointer. + header.currentFrame.bufferFrameStart = rawData.position(); - skipBitmapData(); // false decode pixel data to advance buffer. + // False decode pixel data to advance buffer. + skipBitmapData(); skip(); if (err()) { @@ -155,16 +177,17 @@ public class GifHeaderParser { } header.frameCount++; - header.frames.add(header.currentFrame); // add image to frame + // Add image to frame. + header.frames.add(header.currentFrame); } - /** - * Reads Netscape extenstion to obtain iteration count + /** + * Reads Netscape extension to obtain iteration count. */ protected void readNetscapeExt() { do { readBlock(); if (block[0] == 1) { - // loop count sub-block + // Loop count sub-block. int b1 = ((int) block[1]) & 0xff; int b2 = ((int) block[2]) & 0xff; header.loopCount = (b2 << 8) | b1; @@ -173,7 +196,7 @@ public class GifHeaderParser { } - /** + /** * Reads GIF file header information. */ private void readHeader() { @@ -191,32 +214,32 @@ public class GifHeaderParser { header.bgColor = header.gct[header.bgIndex]; } } - /** - * Reads Logical Screen Descriptor + /** + * Reads Logical Screen Descriptor. */ protected void readLSD() { - // logical screen size + // Logical screen size. header.width = readShort(); header.height = readShort(); - // packed fields + // Packed fields int packed = read(); - header.gctFlag = (packed & 0x80) != 0; // 1 : global color table flag - // 2-4 : color resolution - // 5 : gct sort flag - header.gctSize = 2 << (packed & 7); // 6-8 : gct size - header.bgIndex = read(); // background color index - header.pixelAspect = read(); // pixel aspect ratio - - //Now that we know the size, init scratch arrays - //TODO: these shouldn't go here. -// mainPixels = new byte[header.width * header.height]; -// mainScratch = new int[header.width * header.height]; + // 1 : global color table flag. + header.gctFlag = (packed & 0x80) != 0; + // 2-4 : color resolution. + // 5 : gct sort flag. + // 6-8 : gct size. + header.gctSize = 2 << (packed & 7); + // Background color index. + header.bgIndex = read(); + // Pixel aspect ratio + header.pixelAspect = read(); } - /** - * Reads color table as 256 RGB integer values + + /** + * Reads color table as 256 RGB integer values. * - * @param ncolors int number of colors to read - * @return int array containing 256 colors (packed ARGB with full alpha) + * @param ncolors int number of colors to read. + * @return int array containing 256 colors (packed ARGB with full alpha). */ protected int[] readColorTable(int ncolors) { int nbytes = 3 * ncolors; @@ -226,7 +249,8 @@ public class GifHeaderParser { try { rawData.get(c); - tab = new int[256]; // max size to avoid bounds checks + // Max size to avoid bounds checks. + tab = new int[256]; int i = 0; int j = 0; while (i < ncolors) { @@ -243,13 +267,14 @@ public class GifHeaderParser { return tab; } - /** + /** * Decodes LZW image data into pixel array. Adapted from John Cristy's BitmapMagick. */ protected void skipBitmapData() { int nullCode = -1; int npix = header.width * header.height; - int available, clear, code_mask, code_size, end_of_information, in_code, old_code, bits, code, count, i, datum, data_size, first, top, bi, pi; + int available, clear, codeMask, codeSize, endOfInformation, inCode, oldCode, bits, code, count, i, datum, + dataSize, first, top, bi; if (prefix == null) { prefix = new short[MAX_STACK_SIZE]; @@ -262,27 +287,24 @@ public class GifHeaderParser { } // Initialize GIF data stream decoder. - data_size = read(); - clear = 1 << data_size; - end_of_information = clear + 1; + dataSize = read(); + clear = 1 << dataSize; + endOfInformation = clear + 1; available = clear + 2; - old_code = nullCode; - code_size = data_size + 1; - code_mask = (1 << code_size) - 1; - long start = System.currentTimeMillis(); + oldCode = nullCode; + codeSize = dataSize + 1; + codeMask = (1 << codeSize) - 1; for (code = 0; code < clear; code++) { - prefix[code] = 0; // XXX ArrayIndexOutOfBoundsException + // XXX ArrayIndexOutOfBoundsException. + prefix[code] = 0; suffix[code] = (byte) code; } - start = System.currentTimeMillis(); // Decode GIF pixel stream. - datum = bits = count = first = top = pi = bi = 0; - int iterations = 0; + datum = bits = count = first = top = bi = 0; for (i = 0; i < npix; ) { - iterations++; if (top == 0) { - if (bits < code_size) { + if (bits < codeSize) { // Load bytes until there are enough bits for a code. if (count == 0) { // Read a new data block. @@ -299,50 +321,50 @@ public class GifHeaderParser { continue; } // Get the next code. - code = datum & code_mask; - datum >>= code_size; - bits -= code_size; - // Interpret the code - if ((code > available) || (code == end_of_information)) { + code = datum & codeMask; + datum >>= codeSize; + bits -= codeSize; + // Interpret the code. + if ((code > available) || (code == endOfInformation)) { break; } if (code == clear) { // Reset decoder. - code_size = data_size + 1; - code_mask = (1 << code_size) - 1; + codeSize = dataSize + 1; + codeMask = (1 << codeSize) - 1; available = clear + 2; - old_code = nullCode; + oldCode = nullCode; continue; } - if (old_code == nullCode) { + if (oldCode == nullCode) { pixelStack[top++] = suffix[code]; - old_code = code; + oldCode = code; first = code; continue; } - in_code = code; + inCode = code; if (code == available) { pixelStack[top++] = (byte) first; - code = old_code; + code = oldCode; } while (code > clear) { pixelStack[top++] = suffix[code]; code = prefix[code]; } first = ((int) suffix[code]) & 0xff; - // Add a new string to the string table, + // Add a new string to the string table. if (available >= MAX_STACK_SIZE) { break; } pixelStack[top++] = (byte) first; - prefix[available] = (short) old_code; + prefix[available] = (short) oldCode; suffix[available] = (byte) first; available++; - if (((available & code_mask) == 0) && (available < MAX_STACK_SIZE)) { - code_size++; - code_mask += available; + if (((available & codeMask) == 0) && (available < MAX_STACK_SIZE)) { + codeSize++; + codeMask += available; } - old_code = in_code; + oldCode = inCode; } // Pop a pixel off the pixel stack. top--; @@ -350,7 +372,7 @@ public class GifHeaderParser { } } - /** + /** * Skips variable length blocks up to and including next zero length block. */ protected void skip() { @@ -359,7 +381,7 @@ public class GifHeaderParser { } while ((blockSize > 0) && !err()); } - /** + /** * Reads next variable length block from input. * * @return number of bytes stored in "buffer" @@ -384,13 +406,13 @@ public class GifHeaderParser { return n; } - /** + /** * Reads a single byte from the input stream. */ private int read() { int curByte = 0; try { - curByte = (rawData.get() & 0xFF); + curByte = rawData.get() & 0xFF; } catch (Exception e) { header.status = STATUS_FORMAT_ERROR; } @@ -398,10 +420,10 @@ public class GifHeaderParser { } /** - * Reads next 16-bit value, LSB first + * Reads next 16-bit value, LSB first. */ protected int readShort() { - // read 16-bit value + // Read 16-bit value. return rawData.getShort(); } |