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;
- 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 global_width;
- int global_height;
- int global_bgpixel;
- GifFrame curframe;
- 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 void replayConsumer(ImageConsumer var1) {
- if (this.store != null) {
- this.store.replay(super.source, var1, false);
- }
-
- }
-
- 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;
- int var5 = -1;
- int var6 = 0;
- int var7 = -1;
-
- label302:
- while(!super.aborted) {
- switch (super.input.read()) {
- case -1:
- default:
- if (var4 == 0) {
- return;
- }
- break;
- case 33:
- int var8;
- String var10;
- boolean var19;
- switch (var8 = super.input.read()) {
- case -1:
- return;
- case 249:
- byte[] var9 = new byte[6];
- if (this.readBytes(var9, 0, 6) != 0) {
- return;
- }
-
- if (var9[0] != 4 || var9[5] != 0) {
- return;
- }
-
- var7 = ExtractWord(var9, 2) * 10;
- if (var7 > 0) {
- this.cancatchup = false;
- this.store = null;
- ImageFetcher.startingAnimation();
- }
-
- var6 = var9[1] >> 2 & 7;
- if ((var9[1] & 1) != 0) {
- this.trans_pixel = ExtractByte(var9, 4);
- } else {
- this.trans_pixel = -1;
- }
- continue;
- case 254:
- case 255:
- default:
- var19 = false;
- var10 = "";
- }
-
- while(true) {
- int var11 = super.input.read();
- if (var11 <= 0) {
- if (var8 == 254) {
- this.props.put("comment", var10);
- }
-
- if (var19) {
- super.input.mark(1000000);
- this.cancatchup = false;
- this.store = null;
- ImageFetcher.startingAnimation();
- }
- continue label302;
- }
-
- byte[] var12 = new byte[var11];
- if (this.readBytes(var12, 0, var11) != 0) {
- return;
- }
-
- if (var8 == 254) {
- var10 = var10 + new String(var12, 0);
- } else if (var8 == 255) {
- if (var19) {
- if (var11 == 3 && var12[0] == 1) {
- var5 = ExtractWord(var12, 1);
- } else {
- var19 = false;
- }
- }
-
- if ("NETSCAPE2.0".equals(new String(var12, 0))) {
- var19 = true;
- }
- }
- }
- case 44:
- try {
- if (!this.readImage(var3 == 0, var6, var7)) {
- this.cancatchup = false;
- return;
- }
- } catch (Exception var16) {
- return;
- }
-
- ++var4;
- ++var3;
- continue;
- case 59:
- }
-
- if (var5 == 0 || var5-- >= 0) {
- try {
- if (this.curframe != null) {
- this.curframe.dispose();
- this.curframe = null;
- }
-
- super.input.reset();
- this.saved_image = null;
- this.saved_model = null;
- var4 = 0;
- continue;
- } catch (IOException var17) {
- }
- }
-
- if (this.store != null) {
- this.store.imageComplete();
- if (this.store.getBitState() != 2) {
- super.source.setPixelStore(this, this.store);
- }
- }
-
- ((ImageDecoder)this).imageComplete(3, true);
- return;
- }
-
- } finally {
- ((ImageDecoder)this).close();
- }
- }
-
- 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;
- boolean var11 = this.curframe.disposal_method == 1;
- if (this.trans_pixel >= 0 && !this.curframe.initialframe) {
- if (this.saved_image == null || this.saved_model != var6) {
- int var17 = -1;
- int var18 = 1;
-
- for(int var14 = var7; var14 < var8; ++var10) {
- byte var15 = var5[var14];
- if ((var15 & 255) == this.trans_pixel) {
- if (var17 >= 0) {
- var18 = ((ImageDecoder)this).setPixels(var1 + var17, var2, var14 - var17, var4, var6, var5, var17, 0);
- if (var18 == 0) {
- break;
- }
- }
-
- var17 = -1;
- } else {
- if (var17 < 0) {
- var17 = var14;
- }
-
- if (var11) {
- this.saved_image[var10] = var15;
- }
- }
-
- ++var14;
- }
-
- if (var17 >= 0) {
- var18 = ((ImageDecoder)this).setPixels(var1 + var17, var2, var8 - var17, var4, var6, var5, var17, 0);
- }
-
- return var18;
- }
-
- for(int var12 = var7; var12 < var8; ++var10) {
- byte var13 = var5[var12];
- if ((var13 & 255) == this.trans_pixel) {
- var5[var12] = this.saved_image[var10];
- } else if (var11) {
- this.saved_image[var10] = var13;
- }
-
- ++var12;
- }
- } else if (var11) {
- System.arraycopy(var5, var7, this.saved_image, var10, var3);
- }
-
- int var16 = ((ImageDecoder)this).setPixels(var9, var2, var3, var4, var6, var5, var7, 0);
- if (this.store != null && !this.store.setPixels(var9, var2, var3, var4, var5, var7, 0)) {
- this.store = null;
- }
-
- return var16;
- }
- }
- }
-
- private boolean readImage(boolean var1, int var2, int var3) throws IOException {
- if (this.curframe != null && !this.curframe.dispose()) {
- ((ImageDecoder)this).abort();
- this.store = null;
- return false;
- } else {
- long var4 = 0L;
- byte[] var6 = new byte[259];
- if (this.readBytes(var6, 0, 10) != 0) {
- throw new IOException();
- } else {
- int var7 = ExtractWord(var6, 0);
- int var8 = ExtractWord(var6, 2);
- int var9 = ExtractWord(var6, 4);
- int var10 = ExtractWord(var6, 6);
- boolean var11 = (var6[8] & 64) != 0;
- IndexColorModel var12 = this.global_model;
- if ((var6[8] & 128) != 0) {
- int var13 = 1 << (var6[8] & 7) + 1;
- byte[] var14 = new byte[var13 * 3];
- var14[0] = var6[9];
- if (this.readBytes(var14, 1, var13 * 3 - 1) != 0) {
- throw new IOException();
- }
-
- if (this.readBytes(var6, 9, 1) != 0) {
- throw new IOException();
- }
-
- var12 = new IndexColorModel(8, var13, var14, 0, false, this.trans_pixel);
- } else if (var12 == null || this.trans_pixel != var12.getTransparentPixel()) {
- var12 = new IndexColorModel(8, this.num_global_colors, this.global_colormap, 0, false, this.trans_pixel);
- this.global_model = var12;
- }
-
- if (var1) {
- this.makeStore(this.global_width, this.global_height);
- if (this.store != null) {
- this.store.setProperties(this.props);
- this.store.setColorModel(var12);
- }
-
- ((ImageDecoder)this).setDimensions(this.global_width, this.global_height);
- ((ImageDecoder)this).setProperties(this.props);
- ((ImageDecoder)this).setColorModel(var12);
- ((ImageDecoder)this).headerComplete();
- } else if (this.store != null && var12 != this.store.getColorModel()) {
- this.cancatchup = false;
- this.store = null;
- }
-
- if (var2 == 1 && this.saved_image == null) {
- this.saved_image = new byte[this.global_width * this.global_height];
- }
-
- int var16 = var11 ? 29 : 30;
- ((ImageDecoder)this).setHints(var16);
- if (this.store != null) {
- this.store.setHints(var16);
- }
-
- this.curframe = new GifFrame(this, var2, var3, this.curframe == null, var12, var7, var8, var9, var10);
- byte[] var17 = new byte[var9];
- boolean var15 = this.parseImage(var7, var8, var9, var10, var11, ExtractByte(var6, 9), var6, var17, var12);
- if (!var15) {
- ((ImageDecoder)this).abort();
- this.store = null;
- }
-
- return var15;
- }
- }
- }
- }
-