home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue159 / files / copyjava.exe / lib / GIFGraphics.java < prev    next >
Encoding:
Java Source  |  1999-10-02  |  6.3 KB  |  248 lines

  1. /*
  2.  * @(#)GIFGraphics.java    1999/09/28
  3.  * 
  4.  * Written by David Griffiths, 1999. 
  5.  * 
  6.  * The parts of this software that were specifically written by David Griffiths
  7.  * are released into the Public Domain. However, you should note that some parts are
  8.  * based on other libraries and you should read and be aware of the additional 
  9.  * conditions that are specified in some of the attached files 
  10.  * (notably GIFImage.java and LZWCompressor.java). The GIF format is the copyright
  11.  * of CompuServe Incorporated.
  12.  * 
  13.  * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY 
  14.  * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  15.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  16.  * PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES
  17.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  18.  * THIS SOFTWARE OR ITS DERIVATIVES.
  19.  */
  20.  
  21. /**
  22.  * GIFGraphics stores the pixel information for a single frame of animation.
  23.  * @version    1.0 1999/09/28
  24.  * @author     David Griffiths 
  25.  */
  26.  
  27. import java.util.Hashtable;
  28.  
  29. import java.lang.String;
  30. import java.net.MalformedURLException;
  31. import java.net.URL;
  32. import java.awt.*;
  33. import java.awt.event.*;
  34. import java.applet.Applet;
  35. import java.applet.AppletContext;
  36. import java.io.*;
  37. import java.net.*;
  38. import java.awt.image.*;
  39.  
  40. public class GIFGraphics {
  41.     public int pixels[];
  42.     public int width, height;
  43.     int colour;
  44.     static int Red[] = new int[256];
  45.     static int Green[] = new int[256];
  46.     static int Blue[] = new int[256];
  47.     int left, top;
  48.  
  49.     static {
  50.         int nextPalCol = 1;
  51.         Red[0] = 0;
  52.         Green[0] = 0;
  53.         Blue[0] = 0;
  54.         for (int r = 0; r < 6; r++)
  55.             for (int g = 0; g < 6; g++)
  56.                 for (int b = 0; b < 6; b++) {
  57.                     Red[nextPalCol] = r * 51;
  58.                     Green[nextPalCol] = g * 51;
  59.                     Blue[nextPalCol] = b * 51;
  60.                     nextPalCol++;
  61.                 }
  62.         for (; nextPalCol < 256; nextPalCol++) {
  63.             Red[nextPalCol] = 0;
  64.             Green[nextPalCol] = 0;
  65.             Blue[nextPalCol] = 0;
  66.         }
  67.     }
  68.  
  69.     GIFGraphics(Image image, Component c) {
  70.         this(image, c, 0, 0);
  71.     }
  72.  
  73.     GIFGraphics(Image image, Component c, int left, int top) {
  74.         int countPixels;
  75.         int srcPixels[];
  76.  
  77.         this.left = left;
  78.         this.top = top;
  79.  
  80.         MediaTracker mediaTracker = new MediaTracker(c);
  81.         mediaTracker.addImage(image, 1);
  82.         try {
  83.             mediaTracker.waitForID(1);
  84.             width = image.getWidth(c);
  85.             height = image.getHeight(c);
  86.  
  87.             countPixels = width * height;
  88.             srcPixels = new int[countPixels];
  89.             pixels = new int[width * height];
  90.             PixelGrabber pixelGrabber
  91.                 = new PixelGrabber(image,
  92.                         0, 0,
  93.                         width, height,
  94.                         srcPixels, 0,
  95.                         width);
  96.             try {
  97.                 pixelGrabber.grabPixels();
  98.                 for (int x = 0; x < width; x++)
  99.                     for (int y = 0; y < height; y++) {
  100.                         int r, g, b;
  101.                         int re, ge, be;
  102.                         int r2, g2, b2;
  103.  
  104.                         r = (srcPixels[y * width + x] & (255 << 16)) >> 16;
  105.                         g = (srcPixels[y * width + x] & (255 << 8)) >> 8;
  106.                         b = (srcPixels[y * width + x] & 255);
  107.                         re = r % 51;
  108.                         ge = g % 51;
  109.                         be = b % 51;
  110.  
  111.                         if (x < (width - 1)) {
  112.                             r2 = (srcPixels[y * width + x + 1]
  113.                                 & (255 << 16)) >> 16;
  114.                             r2 += (7 * re / 16);
  115.                             if (r2 > 255)
  116.                                 r2 = 255;
  117.                             g2 = (srcPixels[y * width + x + 1]
  118.                                 & (255 << 8)) >> 8;
  119.                             g2 += (7 * ge / 16);
  120.                             if (g2 > 255)
  121.                                 g2 = 255;
  122.                             b2 = (srcPixels[y * width + x + 1] & 255);
  123.                             b2 += (7 * be / 16);
  124.                             if (b2 > 255)
  125.                                 b2 = 255;
  126.                             srcPixels[y * width + x + 1] 
  127.                                 = (r2 << 16)
  128.                                          + (g2 << 8)
  129.                                          + b2;
  130.                         }
  131.  
  132.                         if ((x > 0) && (y < (height - 1))) {
  133.                             r2 = (srcPixels[(y + 1) * width + x - 1]
  134.                                 & (255 << 16)) >> 16;
  135.                             r2 += (3 * re / 16);
  136.                             if (r2 > 255)
  137.                                 r2 = 255;
  138.                             g2 = (srcPixels[(y + 1) * width + x - 1]
  139.                                 & (255 << 8)) >> 8;
  140.                             g2 += (3 * ge / 16);
  141.                             if (g2 > 255)
  142.                                 g2 = 255;
  143.                             b2 = (srcPixels[(y + 1) * width + x - 1] & 255);
  144.                             b2 += (3 * be / 16);
  145.                             if (b2 > 255)
  146.                                 b2 = 255;
  147.                             srcPixels[(y + 1) * width + x - 1] 
  148.                                 = (r2 << 16)
  149.                                          + (g2 << 8)
  150.                                          + b2;
  151.                         }
  152.  
  153.                         if (y < (height - 1)) {
  154.                             r2 = (srcPixels[(y + 1) * width + x]
  155.                                 & (255 << 16)) >> 16;
  156.                             r2 += (5 * re / 16);
  157.                             if (r2 > 255)
  158.                                 r2 = 255;
  159.                             g2 = (srcPixels[(y + 1) * width + x]
  160.                                 & (255 << 8)) >> 8;
  161.                             g2 += (5 * ge / 16);
  162.                             if (g2 > 255)
  163.                                 g2 = 255;
  164.                             b2 = (srcPixels[(y + 1) * width + x] & 255);
  165.                             b2 += (5 * be / 16);
  166.                             if (b2 > 255)
  167.                                 b2 = 255;
  168.                             srcPixels[(y + 1) * width + x] 
  169.                                 = (r2 << 16)
  170.                                          + (g2 << 8)
  171.                                          + b2;
  172.                         }
  173.  
  174.                         if ((x < (width - 1)) && (y < (height - 1))) {
  175.                             r2 = (srcPixels[(y + 1) * width + x + 1]
  176.                                 & (255 << 16)) >> 16;
  177.                             r2 += (re / 16);
  178.                             if (r2 > 255)
  179.                                 r2 = 255;
  180.                             g2 = (srcPixels[(y + 1) * width + x + 1]
  181.                                 & (255 << 8)) >> 8;
  182.                             g2 += (ge / 16);
  183.                             if (g2 > 255)
  184.                                 g2 = 255;
  185.                             b2 = (srcPixels[(y + 1) * width + x + 1] & 255);
  186.                             b2 += (be / 16);
  187.                             if (b2 > 255)
  188.                                 b2 = 255;
  189.                             srcPixels[(y + 1) * width + x + 1] 
  190.                                 = (r2 << 16)
  191.                                          + (g2 << 8)
  192.                                          + b2;
  193.                         }
  194.  
  195.         
  196.                         if (((r - re) == 0)
  197.                             && ((g - ge) == 0)
  198.                             && ((b - be) == 0))
  199.                             pixels[y * width + x] = 255;
  200.                         else
  201.                             pixels[y * width + x] = (36 * (r - re) / 51)
  202.                                 + (6 * (g - ge) / 51)
  203.                                 + ((b - be) / 51) + 1;
  204.                     }
  205.             }
  206.             catch (Exception e) {
  207.                 System.out.println("Unable to grab pixels: " + e);
  208.             }
  209.         }
  210.         catch (Exception e) {
  211.             System.out.println("Unable to load image: " + e);
  212.         }
  213.     }
  214.  
  215.     public void cropTo(Rectangle rect) {
  216.         int le = rect.x;
  217.         int t = rect.y;
  218.         int w = rect.width;
  219.         int h = rect.height;
  220.  
  221.         int newPixels[] = new int[w * h];
  222.  
  223.         int p = 0;
  224.  
  225.         for (int j = t; j < (t + h); j++)
  226.             for (int i = le; i < (le + w); i++) {
  227.                 newPixels[p] = pixels[j * width + i];
  228.                 p++;
  229.             }
  230.  
  231.         this.top = t;
  232.         this.left = le;
  233.         this.width = w;
  234.         this.height = h;
  235.  
  236.         pixels = newPixels;
  237.     }
  238.  
  239.     public int getPixel(int x, int y) {
  240.         return pixels[y * width + x];
  241.     }
  242.  
  243.     public int[] getPixels() {
  244.         return pixels;
  245.     }
  246. }
  247.  
  248.