home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-10-02 | 6.3 KB | 248 lines |
- /*
- * @(#)GIFGraphics.java 1999/09/28
- *
- * Written by David Griffiths, 1999.
- *
- * The parts of this software that were specifically written by David Griffiths
- * are released into the Public Domain. However, you should note that some parts are
- * based on other libraries and you should read and be aware of the additional
- * conditions that are specified in some of the attached files
- * (notably GIFImage.java and LZWCompressor.java). The GIF format is the copyright
- * of CompuServe Incorporated.
- *
- * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
- * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
- /**
- * GIFGraphics stores the pixel information for a single frame of animation.
- * @version 1.0 1999/09/28
- * @author David Griffiths
- */
-
- import java.util.Hashtable;
-
- 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 GIFGraphics {
- public int pixels[];
- public int width, height;
- int colour;
- static int Red[] = new int[256];
- static int Green[] = new int[256];
- static int Blue[] = new int[256];
- int left, top;
-
- static {
- int nextPalCol = 1;
- Red[0] = 0;
- Green[0] = 0;
- Blue[0] = 0;
- for (int r = 0; r < 6; r++)
- for (int g = 0; g < 6; g++)
- for (int b = 0; b < 6; b++) {
- Red[nextPalCol] = r * 51;
- Green[nextPalCol] = g * 51;
- Blue[nextPalCol] = b * 51;
- nextPalCol++;
- }
- for (; nextPalCol < 256; nextPalCol++) {
- Red[nextPalCol] = 0;
- Green[nextPalCol] = 0;
- Blue[nextPalCol] = 0;
- }
- }
-
- GIFGraphics(Image image, Component c) {
- this(image, c, 0, 0);
- }
-
- GIFGraphics(Image image, Component c, int left, int top) {
- int countPixels;
- int srcPixels[];
-
- this.left = left;
- this.top = top;
-
- MediaTracker mediaTracker = new MediaTracker(c);
- mediaTracker.addImage(image, 1);
- try {
- mediaTracker.waitForID(1);
- width = image.getWidth(c);
- height = image.getHeight(c);
-
- countPixels = width * height;
- srcPixels = new int[countPixels];
- pixels = new int[width * height];
- PixelGrabber pixelGrabber
- = new PixelGrabber(image,
- 0, 0,
- width, height,
- srcPixels, 0,
- width);
- try {
- pixelGrabber.grabPixels();
- for (int x = 0; x < width; x++)
- for (int y = 0; y < height; y++) {
- int r, g, b;
- int re, ge, be;
- int r2, g2, b2;
-
- r = (srcPixels[y * width + x] & (255 << 16)) >> 16;
- g = (srcPixels[y * width + x] & (255 << 8)) >> 8;
- b = (srcPixels[y * width + x] & 255);
- re = r % 51;
- ge = g % 51;
- be = b % 51;
-
- if (x < (width - 1)) {
- r2 = (srcPixels[y * width + x + 1]
- & (255 << 16)) >> 16;
- r2 += (7 * re / 16);
- if (r2 > 255)
- r2 = 255;
- g2 = (srcPixels[y * width + x + 1]
- & (255 << 8)) >> 8;
- g2 += (7 * ge / 16);
- if (g2 > 255)
- g2 = 255;
- b2 = (srcPixels[y * width + x + 1] & 255);
- b2 += (7 * be / 16);
- if (b2 > 255)
- b2 = 255;
- srcPixels[y * width + x + 1]
- = (r2 << 16)
- + (g2 << 8)
- + b2;
- }
-
- if ((x > 0) && (y < (height - 1))) {
- r2 = (srcPixels[(y + 1) * width + x - 1]
- & (255 << 16)) >> 16;
- r2 += (3 * re / 16);
- if (r2 > 255)
- r2 = 255;
- g2 = (srcPixels[(y + 1) * width + x - 1]
- & (255 << 8)) >> 8;
- g2 += (3 * ge / 16);
- if (g2 > 255)
- g2 = 255;
- b2 = (srcPixels[(y + 1) * width + x - 1] & 255);
- b2 += (3 * be / 16);
- if (b2 > 255)
- b2 = 255;
- srcPixels[(y + 1) * width + x - 1]
- = (r2 << 16)
- + (g2 << 8)
- + b2;
- }
-
- if (y < (height - 1)) {
- r2 = (srcPixels[(y + 1) * width + x]
- & (255 << 16)) >> 16;
- r2 += (5 * re / 16);
- if (r2 > 255)
- r2 = 255;
- g2 = (srcPixels[(y + 1) * width + x]
- & (255 << 8)) >> 8;
- g2 += (5 * ge / 16);
- if (g2 > 255)
- g2 = 255;
- b2 = (srcPixels[(y + 1) * width + x] & 255);
- b2 += (5 * be / 16);
- if (b2 > 255)
- b2 = 255;
- srcPixels[(y + 1) * width + x]
- = (r2 << 16)
- + (g2 << 8)
- + b2;
- }
-
- if ((x < (width - 1)) && (y < (height - 1))) {
- r2 = (srcPixels[(y + 1) * width + x + 1]
- & (255 << 16)) >> 16;
- r2 += (re / 16);
- if (r2 > 255)
- r2 = 255;
- g2 = (srcPixels[(y + 1) * width + x + 1]
- & (255 << 8)) >> 8;
- g2 += (ge / 16);
- if (g2 > 255)
- g2 = 255;
- b2 = (srcPixels[(y + 1) * width + x + 1] & 255);
- b2 += (be / 16);
- if (b2 > 255)
- b2 = 255;
- srcPixels[(y + 1) * width + x + 1]
- = (r2 << 16)
- + (g2 << 8)
- + b2;
- }
-
-
- if (((r - re) == 0)
- && ((g - ge) == 0)
- && ((b - be) == 0))
- pixels[y * width + x] = 255;
- else
- pixels[y * width + x] = (36 * (r - re) / 51)
- + (6 * (g - ge) / 51)
- + ((b - be) / 51) + 1;
- }
- }
- catch (Exception e) {
- System.out.println("Unable to grab pixels: " + e);
- }
- }
- catch (Exception e) {
- System.out.println("Unable to load image: " + e);
- }
- }
-
- public void cropTo(Rectangle rect) {
- int le = rect.x;
- int t = rect.y;
- int w = rect.width;
- int h = rect.height;
-
- int newPixels[] = new int[w * h];
-
- int p = 0;
-
- for (int j = t; j < (t + h); j++)
- for (int i = le; i < (le + w); i++) {
- newPixels[p] = pixels[j * width + i];
- p++;
- }
-
- this.top = t;
- this.left = le;
- this.width = w;
- this.height = h;
-
- pixels = newPixels;
- }
-
- public int getPixel(int x, int y) {
- return pixels[y * width + x];
- }
-
- public int[] getPixels() {
- return pixels;
- }
- }
-
-