home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-09-28 | 7.4 KB | 238 lines |
- /*
- ** The code for this class is based on code from Thomas' Boutell's
- ** gd GIF graphics library. The condition upon using the code is that
- ** the following notices appear.
- **
- ** David Griffiths 28th September, 1999.
- */
-
- /*
- ** gd 1.2 is copyright 1994, 1995, Quest Protein Database Center, Cold Spring Harbor Labs.
- ** Permission granted to copy and distribute this work provided that this notice remains intact.
- ** Credit for the library must be given to the Quest Protein Database Center, Cold Spring
- ** Harbor Labs, in all derived works. This does not affect your ownership of the derived work
- ** itself, and the intent is to assure proper credit for Quest, not to interfere with your use of gd.
- ** If you have questions, ask. ("Derived works" includes all programs that utilize the library.
- ** Credit must be given in user-visible documentation.)
- ** gd 1.2 was written by Thomas Boutell and is currently distributed by boutell.com, Inc.
- ** If you wish to release modifications to gd, please clear them first by sending email to
- ** boutell@boutell.com; if this is not done, any modified version of the gd library must be clearly
- ** labeled as such.
- ** The Quest Protein Database Center is funded under Grant P41-RR02188 by the National
- ** Institutes of Health.
- ** Written by Thomas Boutell (http://sunsite.unc.edu/boutell/index.html), 2/94 - 8/95.
- ** The GIF compression code is based on that found in the pbmplus utilities, which in turn is based
- ** on GIFENCOD by David Rowley. See the notice below:
- */
-
- /*
- ** Based on GIFENCOD by David Rowley .A
- ** Lempel-Zim compression based on "compress".
- **
- ** Modified by Marcel Wijkstra
- **
- ** Copyright (C) 1989 by Jef Poskanzer.
- **
- ** Permission to use, copy, modify, and distribute this software and its
- ** documentation for any purpose and without fee is hereby granted, provided
- ** that the above copyright notice appear in all copies and that both that
- ** copyright notice and this permission notice appear in supporting
- ** documentation. This software is provided "as is" without express or
- ** implied warranty.
- **
- ** The Graphics Interchange Format(c) is the Copyright property of
- ** CompuServe Incorporated. GIF(sm) is a Service Mark property of
- ** CompuServe Incorporated.
- */
-
- /*
- ** The GIF decompression is based on that found in the pbmplus utilities,
- ** which in turn is based on GIFDECOD by David Koblas. See the notice below:
- */
-
- /* +---------------------------------------------------------------------+ */
- /* | Copyright 1990, 1991, 1993, David Koblas. (koblas@netcom.com) | */
- /* | Permission to use, copy, modify, and distribute this software | */
- /* | and its documentation for any purpose and without fee is hereby | */
- /* | granted, provided that the above copyright notice appear in all | */
- /* | copies and that both that copyright notice and this permission | */
- /* | notice appear in supporting documentation. This software is | */
- /* | provided "as is" without express or implied warranty. | */
- /* +---------------------------------------------------------------------+ */
-
- import java.lang.String;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
- import java.applet.AppletContext;
- import java.io.*;
- import java.net.*;
- import java.awt.image.*;
-
-
- public class GIFImage implements Animation {
- int Width, Height;
- boolean interlace = false;
- boolean looping = true;
- GIFGraphics graphics[] = new GIFGraphics[500];
- int frameCount;
- LZWCompressor compressor;
-
- GIFImage(int Width, int Height) {
- this.Width = Width;
- this.Height = Height;
- frameCount = 0;
- }
-
- // Attributes
-
- private int fps = 10;
-
- public void setFPS(int newFps) {
- this.fps = newFps;
- }
-
- public int getFPS() {
- return this.fps;
- }
-
- public int addImage(GIFGraphics gg) {
- graphics[frameCount] = gg;
- return frameCount++;
- }
-
- public void serializeTo(PrintStream pout) {
- encodeGIF(pout, 0, 0, 8);
- }
-
- void encodeGIF(PrintStream pout, int Background, int Transparent, int BitsPerPixel) {
- int InitCodeSize;
-
- if (BitsPerPixel <= 1)
- InitCodeSize = 2;
- else
- InitCodeSize = BitsPerPixel;
-
- serializeHeader(pout, Transparent);
-
- serializeLogicalScreen(pout, Background, BitsPerPixel, InitCodeSize);
-
- serializeData(pout, Transparent, InitCodeSize);
-
- serializeTrailer(pout);
- }
-
- private void serializeHeader(PrintStream pout, int Transparent) {
- pout.print((Transparent < 0 ? "GIF87a" : "GIF89a").substring(0, 6));
- }
-
- private void serializeLogicalScreen(PrintStream pout, int Background,
- int BitsPerPixel, int InitCodeSize) {
- int B, Resolution;
- int ColorMapSize;
-
- ColorMapSize = 1 << BitsPerPixel;
- Resolution = BitsPerPixel;
-
- putWord(pout, Width);
- putWord(pout, Height);
- B = 0x80; // Yes, there is a global colour table
- B |= (Resolution - 1) << 5; // Colour resolution in bits 6-4
- B |= (BitsPerPixel - 1); // Log size of global colour table (= 2 ^(n+1))
- pout.write (B); // Write out packed data
- pout.write (Background); // Background colour index
- pout.write (0); // Pixel aspect ratio - 0 = no info given
- serializeGlobalColourTable(pout, ColorMapSize);
- }
-
- private void serializeGlobalColourTable(PrintStream pout, int ColorMapSize) {
- for (int i = 0; i<ColorMapSize; i++) {
- pout.write ((byte)GIFGraphics.Red[i]);
- pout.write ((byte)GIFGraphics.Green[i]);
- pout.write ((byte)GIFGraphics.Blue[i]);
- }
- }
-
- private void serializeData(PrintStream pout, int Transparent,
- int InitCodeSize) {
- LZWCompressor compressor = new LZWCompressor(pout, interlace);
-
- // Loop
- if (looping) {
- pout.write ('!');
- pout.write (0xff);
- pout.write (0x0b);
- pout.print ("NETSCAPE2.0");
- pout.write (0x03);
- pout.write (0x01);
- putWord(pout, 0); // Loop count: 0 = infinite
- pout.write (0x00);
- }
-
- for (int j = 0; j < frameCount; j++)
- serializeFrame(pout, compressor, false,
- 100 / getFPS(), Transparent, InitCodeSize, j);
- }
-
- private void serializeFrame(PrintStream pout, LZWCompressor compressor,
- boolean clearFirst, int delay, int transparent,
- int InitCodeSize, int frameNo) {
- if (transparent >= 0) {
- pout.write ('!');
- pout.write (0xf9);
- pout.write (4);
-
- if (clearFirst)
- pout.write (9);
- else
- pout.write (1);
-
- putWord(pout, delay);
-
- pout.write (transparent);
- pout.write (0);
- }
-
- /*
- * Image descriptor
- */
- pout.write(','); // Image separator
- putWord(pout, graphics[frameNo].left); // Image left position
- putWord(pout, graphics[frameNo].top); // Image top position
- putWord(pout, graphics[frameNo].width); // Image width
- putWord(pout, graphics[frameNo].height); // Image height
-
- // Packed fields
- // -- Flags for features like interlace
- if (interlace)
- pout.write (0x40);
- else
- pout.write (0x00);
-
-
- /*
- * Table based image data
- */
- compressor.compress (
- InitCodeSize + 1,
- graphics[frameNo].width,
- graphics[frameNo].height,
- graphics[frameNo].getPixels()
- ); // Image data
-
- pout.write ((byte)0);
- }
-
- private void serializeTrailer(PrintStream pout) {
- pout.write (';');
- pout.flush();
- }
-
- void putWord(PrintStream pout, int w) {
- pout.write ((byte)(w & 0xff));
- pout.write ((byte)((w >> 8) & 0xff));
- }
- }
-