home *** CD-ROM | disk | FTP | other *** search
- package sun.awt.image;
-
- import java.awt.image.ColorModel;
- import java.awt.image.ImageConsumer;
- import java.awt.image.IndexColorModel;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Hashtable;
-
- public class GifImageDecoder extends ImageDecoder {
- private static final boolean verbose = false;
- private static final int IMAGESEP = 44;
- private static final int EXBLOCK = 33;
- private static final int EX_GRAPHICS_CONTROL = 249;
- private static final int EX_COMMENT = 254;
- private static final int EX_APPLICATION = 255;
- private static final int TERMINATOR = 59;
- private static final int TRANSPARENCYMASK = 1;
- private static final int INTERLACEMASK = 64;
- private static final int COLORMAPMASK = 128;
- private static final int DISPOSAL_NONE = 0;
- private static final int DISPOSAL_SAVE = 1;
- private static final int DISPOSAL_BGCOLOR = 2;
- private static final int DISPOSAL_PREVIOUS = 3;
- private static IndexColorModel trans_model;
- PixelStore8 store;
- boolean cancatchup = true;
- int num_global_colors;
- byte[] global_colormap;
- int trans_pixel = -1;
- IndexColorModel global_model;
- Hashtable props = new Hashtable();
- byte[] saved_image;
- IndexColorModel saved_model;
- int disposal_method;
- int global_width;
- int global_height;
- int global_bgpixel;
- boolean firstframe = true;
- int nloops = -1;
- int delay = -1;
- private static final int normalflags = 30;
- private static final int interlaceflags = 29;
- private short[] prefix = new short[4096];
- private byte[] suffix = new byte[4096];
- private byte[] outCode = new byte[4097];
-
- public GifImageDecoder(InputStreamImageSource var1, InputStream var2) {
- super(var1, var2);
- }
-
- public synchronized boolean catchupConsumer(InputStreamImageSource var1, ImageConsumer var2) {
- return this.cancatchup && (this.store == null || this.store.replay(var1, var2));
- }
-
- public synchronized void makeStore(int var1, int var2) {
- if (this.cancatchup) {
- this.store = new PixelStore8(var1, var2);
- }
-
- }
-
- private static void error(String var0) throws ImageFormatException {
- throw new ImageFormatException(var0);
- }
-
- private int readBytes(byte[] var1, int var2, int var3) {
- while(true) {
- if (var3 > 0) {
- try {
- int var4 = super.input.read(var1, var2, var3);
- if (var4 >= 0) {
- var2 += var4;
- var3 -= var4;
- continue;
- }
- } catch (IOException var5) {
- }
- }
-
- return var3;
- }
- }
-
- private static final int ExtractByte(byte[] var0, int var1) {
- return var0[var1] & 255;
- }
-
- private static final int ExtractWord(byte[] var0, int var1) {
- return var0[var1] & 255 | (var0[var1 + 1] & 255) << 8;
- }
-
- public void produceImage() throws IOException, ImageFormatException {
- try {
- this.readHeader();
- int var3 = 0;
- int var4 = 0;
-
- label357:
- while(true) {
- switch (super.input.read()) {
- case -1:
- default:
- if (var4 == 0) {
- return;
- }
- break;
- case 33:
- int var5;
- String var7;
- boolean var26;
- switch (var5 = super.input.read()) {
- case -1:
- return;
- case 249:
- byte[] var6 = new byte[6];
- if (this.readBytes(var6, 0, 6) != 0) {
- return;
- }
-
- if (var6[0] != 4 || var6[5] != 0) {
- return;
- }
-
- this.delay = ExtractWord(var6, 2) * 10;
- if (this.delay > 0) {
- this.cancatchup = false;
- this.store = null;
- ImageFetcher.startingAnimation();
- }
-
- this.disposal_method = var6[1] >> 2 & 7;
- if ((var6[1] & 1) != 0) {
- this.trans_pixel = ExtractByte(var6, 4);
- } else {
- this.trans_pixel = -1;
- }
- continue;
- case 254:
- case 255:
- default:
- var26 = false;
- var7 = "";
- }
-
- while(true) {
- int var8 = super.input.read();
- if (var8 <= 0) {
- if (var5 == 254) {
- this.props.put("comment", var7);
- }
-
- if (var26) {
- super.input.mark(1000000);
- this.cancatchup = false;
- this.store = null;
- ImageFetcher.startingAnimation();
- }
- continue label357;
- }
-
- byte[] var9 = new byte[var8];
- if (this.readBytes(var9, 0, var8) != 0) {
- return;
- }
-
- if (var5 == 254) {
- var7 = var7 + new String(var9, 0);
- } else if (var5 == 255) {
- if (var26) {
- if (var8 == 3 && var9[0] == 1) {
- this.nloops = ExtractWord(var9, 1);
- } else {
- var26 = false;
- }
- }
-
- if ("NETSCAPE2.0".equals(new String(var9, 0))) {
- var26 = true;
- }
- }
- }
- case 44:
- try {
- if (!this.readImage(var3 == 0)) {
- this.cancatchup = false;
- return;
- }
- } catch (Exception var23) {
- return;
- }
-
- ++var4;
- ++var3;
- this.firstframe = false;
- continue;
- case 59:
- }
-
- if (this.nloops == 0 || this.nloops-- >= 0) {
- try {
- super.input.reset();
- this.saved_image = null;
- this.saved_model = null;
- this.firstframe = true;
- var4 = 0;
- continue;
- } catch (IOException var24) {
- }
- }
-
- if (this.store != null) {
- this.store.imageComplete();
- if (this.store.getBitState() != 2) {
- super.source.setPixelStore(this.store);
- }
- }
-
- super.source.imageComplete(3);
- return;
- }
- } finally {
- try {
- super.input.close();
- } catch (IOException var22) {
- }
-
- }
- }
-
- private void readHeader() throws IOException, ImageFormatException {
- byte[] var1 = new byte[13];
- if (this.readBytes(var1, 0, 13) != 0) {
- throw new IOException();
- } else {
- if (var1[0] != 71 || var1[1] != 73 || var1[2] != 70) {
- error("not a GIF file.");
- }
-
- this.global_width = ExtractWord(var1, 6);
- this.global_height = ExtractWord(var1, 8);
- int var2 = ExtractByte(var1, 10);
- if ((var2 & 128) == 0) {
- error("no global colormap in GIF file.");
- }
-
- this.num_global_colors = 1 << (var2 & 7) + 1;
- this.global_bgpixel = ExtractByte(var1, 11);
- if (var1[12] != 0) {
- this.props.put("aspectratio", String.valueOf((double)(ExtractByte(var1, 12) + 15) / (double)64.0F));
- }
-
- this.global_colormap = new byte[this.num_global_colors * 3];
- if (this.readBytes(this.global_colormap, 0, this.num_global_colors * 3) != 0) {
- throw new IOException();
- }
- }
- }
-
- private native boolean parseImage(int var1, int var2, int var3, int var4, boolean var5, int var6, byte[] var7, byte[] var8, IndexColorModel var9);
-
- private int sendPixels(int var1, int var2, int var3, int var4, byte[] var5, ColorModel var6) {
- if (var2 < 0) {
- var4 += var2;
- var2 = 0;
- }
-
- if (var2 + var4 > this.global_height) {
- var4 = this.global_height - var2;
- }
-
- if (var4 <= 0) {
- return 1;
- } else {
- int var7;
- int var9;
- if (var1 < 0) {
- var7 = -var1;
- var3 += var1;
- var9 = 0;
- } else {
- var7 = 0;
- var9 = var1;
- }
-
- if (var9 + var3 > this.global_width) {
- var3 = this.global_width - var9;
- }
-
- if (var3 <= 0) {
- return 1;
- } else {
- int var8 = var7 + var3;
- int var10 = var2 * this.global_width + var9;
- if (this.trans_pixel >= 0 && !this.firstframe) {
- if (this.saved_image == null || this.saved_model != var6) {
- int var16 = -1;
- int var17 = 1;
-
- for(int var13 = var7; var13 < var8; ++var10) {
- byte var14 = var5[var13];
- if ((var14 & 255) == this.trans_pixel) {
- if (var16 >= 0) {
- var17 = super.source.setPixels(var1 + var16, var2, var13 - var16, var4, var6, var5, var16, 0);
- if (var17 == 0) {
- break;
- }
- }
-
- var16 = -1;
- } else {
- if (var16 < 0) {
- var16 = var13;
- }
-
- if (this.disposal_method == 1) {
- this.saved_image[var10] = var14;
- }
- }
-
- ++var13;
- }
-
- if (var16 >= 0) {
- var17 = super.source.setPixels(var1 + var16, var2, var8 - var16, var4, var6, var5, var16, 0);
- }
-
- return var17;
- }
-
- for(int var11 = var7; var11 < var8; ++var10) {
- byte var12 = var5[var11];
- if ((var12 & 255) == this.trans_pixel) {
- var5[var11] = this.saved_image[var10];
- } else if (this.disposal_method == 1) {
- this.saved_image[var10] = var12;
- }
-
- ++var11;
- }
- } else if (this.disposal_method == 1) {
- System.arraycopy(var5, var7, this.saved_image, var10, var3);
- }
-
- int var15 = super.source.setPixels(var9, var2, var3, var4, var6, var5, var7, 0);
- if (this.store != null && this.store.setPixels(var9, var2, var3, var4, var5, var7, 0)) {
- ++var15;
- }
-
- return var15;
- }
- }
- }
-
- private boolean readImage(boolean var1) throws IOException {
- long var2 = 0L;
- byte[] var4 = new byte[259];
- if (this.readBytes(var4, 0, 10) != 0) {
- throw new IOException();
- } else {
- int var5 = ExtractWord(var4, 0);
- int var6 = ExtractWord(var4, 2);
- int var7 = ExtractWord(var4, 4);
- int var8 = ExtractWord(var4, 6);
- boolean var9 = (var4[8] & 64) != 0;
- IndexColorModel var10 = this.global_model;
- if ((var4[8] & 128) != 0) {
- int var11 = 1 << (var4[8] & 7) + 1;
- byte[] var12 = new byte[var11 * 3];
- var12[0] = var4[9];
- if (this.readBytes(var12, 1, var11 * 3 - 1) != 0) {
- throw new IOException();
- }
-
- if (this.readBytes(var4, 9, 1) != 0) {
- throw new IOException();
- }
-
- var10 = new IndexColorModel(8, var11, var12, 0, false, this.trans_pixel);
- } else if (var10 == null || this.trans_pixel != var10.getTransparentPixel()) {
- var10 = new IndexColorModel(8, this.num_global_colors, this.global_colormap, 0, false, this.trans_pixel);
- this.global_model = var10;
- }
-
- if (var1) {
- this.makeStore(this.global_width, this.global_height);
- if (this.store != null) {
- this.store.setProperties(this.props);
- this.store.setColorModel(var10);
- }
-
- super.source.setDimensions(this.global_width, this.global_height);
- super.source.setProperties(this.props);
- super.source.setColorModel(var10);
- } else if (this.store != null && var10 != this.store.getColorModel()) {
- this.cancatchup = false;
- this.store = null;
- }
-
- if (this.disposal_method == 1 && this.saved_image == null) {
- this.saved_image = new byte[this.global_width * this.global_height];
- }
-
- int var17 = var9 ? 29 : 30;
- super.source.setHints(var17);
- if (this.store != null) {
- this.store.setHints(var17);
- }
-
- byte[] var18 = new byte[var7];
- boolean var13 = this.parseImage(var5, var6, var7, var8, var9, ExtractByte(var4, 9), var4, var18, var10);
- if (var13) {
- super.source.imageComplete(2);
- if (this.delay > 0) {
- try {
- Thread.sleep((long)this.delay);
- } catch (InterruptedException var16) {
- return false;
- }
- }
-
- if (var5 < 0) {
- var7 += var5;
- var5 = 0;
- }
-
- if (var5 + var7 > this.global_width) {
- var7 = this.global_width - var5;
- }
-
- if (var7 <= 0) {
- this.disposal_method = 0;
- } else {
- if (var6 < 0) {
- var8 += var6;
- var6 = 0;
- }
-
- if (var6 + var8 > this.global_height) {
- var8 = this.global_height - var6;
- }
-
- if (var8 <= 0) {
- this.disposal_method = 0;
- }
- }
-
- switch (this.disposal_method) {
- case 1:
- this.saved_model = var10;
- break;
- case 2:
- byte var14;
- if (var10.getTransparentPixel() < 0) {
- this.cancatchup = false;
- this.store = null;
- var10 = trans_model;
- if (var10 == null) {
- var10 = new IndexColorModel(8, 1, new byte[4], 0, true);
- trans_model = var10;
- }
-
- var14 = 0;
- } else {
- var14 = (byte)var10.getTransparentPixel();
- }
-
- for(int var15 = 0; var15 < var7; ++var15) {
- var18[var15] = var14;
- }
-
- super.source.setPixels(var5, var6, var7, var8, var10, var18, 0, 0);
- if (this.store != null) {
- this.store.setPixels(var5, var6, var7, var8, var18, 0, 0);
- }
- break;
- case 3:
- if (this.saved_image != null) {
- super.source.setPixels(var5, var6, var7, var8, this.saved_model, this.saved_image, var6 * this.global_width + var5, this.global_width);
- if (this.store != null) {
- this.store.setPixels(var5, var6, var7, var8, this.saved_image, var6 * this.global_width + var5, this.global_width);
- }
- }
- }
- }
-
- return var13;
- }
- }
- }
-