/Users/lyon/j4p/src/ip/gif/gifAnimation/DirectGif89Frame.java

1    //****************************************************************************** 
2    // DirectGif89Frame.java 
3    //****************************************************************************** 
4    package ip.gif.gifAnimation; 
5     
6    import java.awt.*; 
7    import java.awt.image.PixelGrabber; 
8    import java.io.IOException; 
9     
10   //============================================================================== 
11    
12   /** Instances of this Gif89Frame subclass are constructed from RGB image info, 
13    *  either in the form of an Image object or a pixel array. 
14    *  <p> 
15    *  There is an important restriction to note.  It is only permissible to add 
16    *  DirectGif89Frame objects to a Gif89Encoder constructed without an explicit 
17    *  color map.  The GIF color table will be automatically generated from pixel 
18    *  information. 
19    * 
20    * @version 0.90 beta (15-Jul-2000) 
21    * @author J. M. G. Elliott (tep@jmge.net) 
22    * @see Gif89Encoder 
23    * @see Gif89Frame 
24    * @see IndexGif89Frame 
25    */ 
26   public class DirectGif89Frame extends Gif89Frame { 
27    
28       private int[] argbPixels; 
29    
30       //---------------------------------------------------------------------------- 
31       /** Construct an DirectGif89Frame from a Java image. 
32        * 
33        * @param img 
34        *   A java.awt.Image object that supports pixel-grabbing. 
35        * @exception IOException 
36        *   If the image is unencodable due to failure of pixel-grabbing. 
37        */ 
38       public DirectGif89Frame(Image img) throws IOException { 
39           PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, true); 
40    
41           String errmsg = null; 
42           try { 
43               if (!pg.grabPixels()) 
44                   errmsg = "can't grab pixels from image"; 
45           } catch (InterruptedException e) { 
46               errmsg = "interrupted grabbing pixels from image"; 
47           } 
48    
49           if (errmsg != null) 
50               throw new IOException(errmsg + " (" + getClass().getName() + ")"); 
51    
52           theWidth = pg.getWidth(); 
53           theHeight = pg.getHeight(); 
54           argbPixels = (int[]) pg.getPixels(); 
55           ciPixels = new byte[argbPixels.length]; 
56       } 
57    
58       //---------------------------------------------------------------------------- 
59       /** Construct an DirectGif89Frame from ARGB pixel data. 
60        * 
61        * @param width 
62        *   Width of the bitmap. 
63        * @param height 
64        *   Height of the bitmap. 
65        * @param argb_pixels 
66        *   Array containing at least width*height pixels in the format returned by 
67        *   java.awt.Color.getRGB(). 
68        */ 
69       public DirectGif89Frame(int width, int height, int argb_pixels[]) { 
70           theWidth = width; 
71           theHeight = height; 
72           argbPixels = new int[theWidth * theHeight]; 
73           System.arraycopy(argb_pixels, 0, argbPixels, 0, argbPixels.length); 
74           ciPixels = new byte[argbPixels.length]; 
75       } 
76    
77       //---------------------------------------------------------------------------- 
78       Object getPixelSource() { 
79           return argbPixels; 
80       } 
81   }