home *** CD-ROM | disk | FTP | other *** search
- package sun.awt.image;
-
- 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 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 INTERLACEMASK = 64;
- private static final int COLORMAPMASK = 128;
- PixelStore8 store;
- int num_colors;
- byte[] colormap;
- IndexColorModel model;
- Hashtable props = new Hashtable();
- private static final int normalflags = 30;
- private static final int interlaceflags = 29;
-
- public GifImageDecoder(InputStreamImageSource src, InputStream is) {
- super(src, is);
- }
-
- public synchronized boolean catchupConsumer(InputStreamImageSource src, ImageConsumer ic) {
- return this.store == null || this.store.replay(src, ic);
- }
-
- public synchronized void makeStore(int width, int height) {
- this.store = new PixelStore8(width, height);
- }
-
- private static void error(String s1) throws ImageFormatException {
- throw new ImageFormatException(s1);
- }
-
- boolean readBytes(byte[] buf, int off, int len) {
- while(len > 0) {
- int n;
- try {
- n = super.input.read(buf, off, len);
- } catch (IOException var5) {
- n = -1;
- }
-
- if (n < 0) {
- return false;
- }
-
- off += n;
- len -= n;
- }
-
- return true;
- }
-
- public void produceImage() throws IOException, ImageFormatException {
- int trans_pixel = -1;
-
- try {
- this.readHeader();
-
- label248:
- while(true) {
- switch (super.input.read()) {
- case 59:
- return;
- case 44:
- try {
- this.model = new IndexColorModel(8, this.num_colors, this.colormap, 0, false, trans_pixel);
- this.colormap = null;
- if (this.readImage()) {
- this.store.imageComplete();
- if (this.store.getBitState() != 2) {
- super.source.setPixelStore(this.store);
- }
-
- super.source.imageComplete(3);
- return;
- }
- } catch (ArrayIndexOutOfBoundsException var19) {
- }
-
- return;
- case -1:
- return;
- case 33:
- int code;
- switch (code = super.input.read()) {
- case 255:
- case 254:
- default:
- String comment = "";
-
- while(true) {
- int n = super.input.read();
- if (n == 0) {
- if (code == 254) {
- this.props.put("comment", comment);
- }
- continue label248;
- }
-
- byte[] buf = new byte[n];
- if (!this.readBytes(buf, 0, n)) {
- return;
- }
-
- if (code == 254) {
- comment = comment + new String(buf, 0);
- }
- }
- case 249:
- byte[] buf = new byte[6];
- if (!this.readBytes(buf, 0, 6)) {
- return;
- }
-
- if (buf[0] != 4 || buf[5] != 0) {
- return;
- }
-
- trans_pixel = buf[4] & 255;
- continue;
- }
- default:
- return;
- }
- }
- } finally {
- try {
- super.input.close();
- } catch (IOException var18) {
- }
-
- }
- }
-
- private void readHeader() throws IOException, ImageFormatException {
- byte[] buf = new byte[13];
- if (!this.readBytes(buf, 0, 13)) {
- throw new IOException();
- } else {
- if (buf[0] != 71 || buf[1] != 73 || buf[2] != 70) {
- error("not a GIF file.");
- }
-
- int ch = buf[10] & 255;
- if ((ch & 128) == 0) {
- error("no global colormap in GIF file.");
- }
-
- this.num_colors = 1 << (ch & 7) + 1;
- if (buf[12] != 0) {
- error("corrupt GIF file (no null).");
- }
-
- this.colormap = new byte[this.num_colors * 3];
- if (!this.readBytes(this.colormap, 0, this.num_colors * 3)) {
- throw new IOException();
- }
- }
- }
-
- private native boolean parseImage(int var1, int var2, boolean var3, int var4, byte[] var5, byte[] var6);
-
- private int sendPixels(int y, int width, byte[] rasline) {
- int count = super.source.setPixels(0, y, width, 1, this.model, rasline, 0, width);
- if (this.store.setPixels(0, y, width, 1, rasline, 0, width)) {
- ++count;
- }
-
- return count;
- }
-
- private boolean readImage() throws IOException {
- long tm = 0L;
- byte[] block = new byte[259];
- if (!this.readBytes(block, 0, 10)) {
- throw new IOException();
- } else {
- int width = block[4] & 255 | block[5] << 8;
- int height = block[6] & 255 | block[7] << 8;
- boolean interlace = (block[8] & 64) != 0;
- int initCodeSize = block[9] & 255;
- super.source.setDimensions(width, height);
- this.makeStore(width, height);
- super.source.setProperties(this.props);
- this.store.setProperties(this.props);
- super.source.setColorModel(this.model);
- this.store.setColorModel(this.model);
- int hints = interlace ? 29 : 30;
- super.source.setHints(hints);
- this.store.setHints(hints);
- byte[] rasline = new byte[width];
- boolean ret = this.parseImage(width, height, interlace, initCodeSize, block, rasline);
- return ret;
- }
- }
- }
-