KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > api > image > LLBitmap


1 /*
2  * $Id: LLBitmap.java,v 1.3 2002/02/24 02:10:19 skavish Exp $
3  *
4  * ==========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.api.image;
52
53 import java.io.*;
54 import java.awt.image.*;
55 import java.awt.geom.*;
56 import java.util.zip.*;
57 import org.openlaszlo.iv.flash.util.*;
58 import org.openlaszlo.iv.flash.url.*;
59 import org.openlaszlo.iv.flash.parser.*;
60 import org.openlaszlo.iv.flash.api.*;
61 import com.sun.image.codec.jpeg.*;
62
63 public class LLBitmap extends Bitmap {
64
65     protected DataMarker zlibData;
66     protected Rectangle2D bounds;
67     protected int format; // 3 - 8 bit, 4 - 16 bit, 5 - 32 bit
68
protected int colorTableSize; // This value is one less than the actual size of the color table
69

70     public LLBitmap() {}
71
72     public static Bitmap parse( Parser p ) {
73         LLBitmap o = new LLBitmap();
74         o.setID( p.getUWord() );
75         o.tagCode = p.getTagCode();
76         o.format = p.getUByte();
77         int width = p.getUWord();
78         int height = p.getUWord();
79         o.bounds = GeomHelper.newRectangle(0, 0, width, height);
80         // in the other cases there is no color table, thus no color table size !
81
if( o.format == 3 ) {
82             o.colorTableSize = p.getUByte();
83         }
84         o.zlibData = new DataMarker( p.getBuf(), p.getPos(), p.getTagEndPos() );
85         return o;
86     }
87
88     public void write( FlashOutput fob ) {
89
90         int tagSize = 2+1+4+((colorTableSize>=0&&format==3)?1:0)+zlibData.length();
91         // always write long tag?
92
fob.writeLongTag( tagCode, tagSize );
93         fob.writeDefID(this);
94         fob.writeByte(format);
95         fob.writeWord(getWidth());
96         fob.writeWord(getHeight());
97         // the colorTableSize should not be written if the color table is empty !
98
if( colorTableSize>=0 && format == 3 ) {
99             fob.writeByte( colorTableSize );
100         }
101         zlibData.write(fob);
102     }
103
104     public int getSize() {
105         return zlibData.buffer.length;
106     }
107
108     public Rectangle2D getBounds() { return bounds; }
109     public void setBounds( Rectangle2D rect ) { this.bounds = rect; }
110
111     public void setWidth( int width ) {
112         if( bounds == null ) bounds = GeomHelper.newRectangle();
113         bounds.setFrame( 0, 0, width, getHeight() );
114     }
115
116     public void setHeight( int height ) {
117         if( bounds == null ) bounds = GeomHelper.newRectangle();
118         bounds.setFrame( 0, 0, getWidth(), height );
119     }
120
121     public int getFormat() { return format; }
122     public void setFormat( int format ) { this.format = format; }
123
124     public int getColorTableSize() { return colorTableSize; }
125     public void setColorTableSize( int colorTableSize ) { this.colorTableSize = colorTableSize; }
126
127     public DataMarker getZLibData() { return zlibData; }
128     public void setZLibData( DataMarker data ) { this.zlibData = data; }
129
130     public void setAlpha( boolean withAlpha ) {
131         if( withAlpha ) {
132             tagCode = Tag.DEFINEBITSLOSSLESS2;
133         } else {
134             tagCode = Tag.DEFINEBITSLOSSLESS;
135         }
136     }
137
138     public boolean isAlpha() {
139         return tagCode == Tag.DEFINEBITSLOSSLESS2;
140     }
141
142     public static LLBitmap newGIFBitmap( String JavaDoc fileName )
143         throws IVException, IOException
144     {
145         return newGIFBitmap( IVUrl.newUrl(fileName) );
146     }
147
148     public static LLBitmap newGIFBitmap( IVUrl url )
149         throws IVException, IOException
150     {
151         return newGIFBitmap( Util.readUrl(url) );
152     }
153
154     public static LLBitmap newGIFBitmap( FlashBuffer fob ) throws IVException {
155         LLBitmap bitmap = new LLBitmap();
156         try {
157             GIFHelper gh = new GIFHelper();
158             gh.doRead(fob);
159             bitmap.setWidth(gh.getWidth());
160             bitmap.setHeight(gh.getHeight());
161             bitmap.setFormat(3); // GIF image are always 8 bits
162
bitmap.setAlpha(gh.isAlpha()); // but can have transparency
163
bitmap.setColorTableSize(gh.getColorTableSize());
164             bitmap.setZLibData(gh.getZlibData());
165         } catch( IOException e ) {
166             throw new IVException(Resource.ERRREADINGGIF, e);
167         }
168         return bitmap;
169     }
170
171     public static LLBitmap newPNGBitmap( String JavaDoc fileName )
172         throws IVException, IOException
173     {
174         return newPNGBitmap( IVUrl.newUrl(fileName) );
175     }
176
177     public static LLBitmap newPNGBitmap( IVUrl url )
178         throws IVException, IOException
179     {
180         return newPNGBitmap( Util.readUrl(url) );
181     }
182
183     public static LLBitmap newPNGBitmap( FlashBuffer fob ) throws IVException {
184         LLBitmap bitmap = new LLBitmap();
185         try {
186             PNGHelper pnh = new PNGHelper();
187             pnh.setInputBuffer(fob);
188             bitmap.setZLibData(pnh.getZlibData());
189             bitmap.setWidth(pnh.getWidth());
190             bitmap.setHeight(pnh.getHeight());
191             bitmap.setFormat(pnh.getFormat());
192             bitmap.setAlpha(pnh.hasTransparency());
193             bitmap.setColorTableSize(pnh.getColorTableSize());
194         } catch( IOException e ) {
195             throw new IVException(Resource.ERRREADINGPNG, e);
196         }
197         return bitmap;
198     }
199
200     public void printContent( PrintStream out, String JavaDoc indent ) {
201         out.println( indent+"LossLessBitmap("+Tag.tagNames[tagCode]+"): id="+getID()+" name='"+getName()+"'");
202         out.print( indent+" format="+(format==3?"8 bit":(format==4?"16 bit":"32 bit")) );
203         out.println( ", colorTableSize="+colorTableSize+", width="+getWidth()+", height="+getHeight() );
204
205         // print zlib data info
206
/* try {
207             InflaterInputStream zin = new InflaterInputStream( zlibData.getInputStream() );
208             FlashBuffer fob = new FlashBuffer(zin);
209             System.out.println( "size="+fob.getSize() );
210             out.println( indent+" colortable: RGB"+(isAlpha()?"A":"")+"["+(colorTableSize+1)+"]" );
211             int size = (isAlpha()?4:3)*(colorTableSize+1);
212             Util.dump(fob.getBuf(),0,size,out);
213
214             int datasize = fob.getSize()-size;
215             int width = bounds.getWidth();
216             int height = bounds.getHeight();
217             int datasize1 = ((width+3)&~0x03)*height;
218             out.println( indent+" colordata: UI8["+datasize+"] (rounded(width)*height="+datasize1+")" );
219             Util.dump(fob.getBuf(),size,datasize,out);
220         } catch( IOException e ) {
221         }*/

222     }
223
224     protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) {
225         super.copyInto( item, copier );
226         ((LLBitmap)item).zlibData = (DataMarker) zlibData.getCopy();
227         ((LLBitmap)item).bounds = (Rectangle2D) bounds.clone();
228         ((LLBitmap)item).format = format;
229         ((LLBitmap)item).colorTableSize = colorTableSize;
230         return item;
231     }
232
233     public FlashItem getCopy( ScriptCopier copier ) {
234         return copyInto( new LLBitmap(), copier );
235     }
236
237 /*
238     public JPEGBitmap convertToJPEG( float quality ) {
239
240         if( isAlpha() ) {
241             Log.logRB( Resource.STR, "Alpha channel has been stripped from image during conversion to JPEG" );
242         }
243
244         InflaterInputStream zin = new InflaterInputStream( zlibData.getInputStream() );
245         FlashBuffer fob = new FlashBuffer(zin);
246
247         int width = bounds.getWidth();
248         int height = bounds.getHeight();
249         int scansize = (width+3)&~0x03;
250         int datasize = scansize*height;
251
252         int pixelsize = isAlpha()?4:3;
253         int bitsperpixel;
254
255         // if 8bit image, then get rid of color table
256         if( format == 3 ) {
257             bitsperpixel = 24;
258             //int newscansize = (width*3+3)&~0x03;
259             int newscansize = scansize*3;
260             FrashBuffer fb = new FlashBuffer(newscansize*height);
261             byte[] newtable = fb.getBuf();
262             byte[] table = fob.getBuf();
263             int dataend = fob.getSize();
264             int pos = (colorTableSize+1)*pixelsize;
265             int newpos = 0;
266             for( ; pos<dataend; pos++ ) {
267                 int idx = table[pos];
268                 newtable[newpos++] = table[idx]; // R
269                 newtable[newpos++] = table[idx+1]; // G
270                 newtable[newpos++] = table[idx+2]; // B
271             }
272             fob = fb;
273         } else if( isAlpha() ) {
274             // get rid of alpha
275         }
276
277         DataBuffer db = new DataBufferByte(fob.getBuf(), fob.getSize());
278         WritableRaster raster = Raster.createPackedRaster(db,width,height,,null);
279         Raster raster = Raster.createRaster(sm,db,null);
280
281         fob = new FlashBuffer(fob.getSize()/4);
282         OutputStream os = fob.getOutputStream();
283         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
284         JPEGEncodeParam params = encoder.getDefaultJPEGEncodeParam(raster,colorID);
285         params.setQuality(quality,true);
286
287         encoder.encode(raster,params);
288
289     }*/

290 }
291
292
Popular Tags