001: /*
002: * $Id: LLBitmap.java,v 1.3 2002/02/24 02:10:19 skavish Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.api.image;
052:
053: import java.io.*;
054: import java.awt.image.*;
055: import java.awt.geom.*;
056: import java.util.zip.*;
057: import org.openlaszlo.iv.flash.util.*;
058: import org.openlaszlo.iv.flash.url.*;
059: import org.openlaszlo.iv.flash.parser.*;
060: import org.openlaszlo.iv.flash.api.*;
061: import com.sun.image.codec.jpeg.*;
062:
063: public class LLBitmap extends Bitmap {
064:
065: protected DataMarker zlibData;
066: protected Rectangle2D bounds;
067: protected int format; // 3 - 8 bit, 4 - 16 bit, 5 - 32 bit
068: protected int colorTableSize; // This value is one less than the actual size of the color table
069:
070: public LLBitmap() {
071: }
072:
073: public static Bitmap parse(Parser p) {
074: LLBitmap o = new LLBitmap();
075: o.setID(p.getUWord());
076: o.tagCode = p.getTagCode();
077: o.format = p.getUByte();
078: int width = p.getUWord();
079: int height = p.getUWord();
080: o.bounds = GeomHelper.newRectangle(0, 0, width, height);
081: // in the other cases there is no color table, thus no color table size !
082: if (o.format == 3) {
083: o.colorTableSize = p.getUByte();
084: }
085: o.zlibData = new DataMarker(p.getBuf(), p.getPos(), p
086: .getTagEndPos());
087: return o;
088: }
089:
090: public void write(FlashOutput fob) {
091:
092: int tagSize = 2 + 1 + 4
093: + ((colorTableSize >= 0 && format == 3) ? 1 : 0)
094: + zlibData.length();
095: // always write long tag?
096: fob.writeLongTag(tagCode, tagSize);
097: fob.writeDefID(this );
098: fob.writeByte(format);
099: fob.writeWord(getWidth());
100: fob.writeWord(getHeight());
101: // the colorTableSize should not be written if the color table is empty !
102: if (colorTableSize >= 0 && format == 3) {
103: fob.writeByte(colorTableSize);
104: }
105: zlibData.write(fob);
106: }
107:
108: public int getSize() {
109: return zlibData.buffer.length;
110: }
111:
112: public Rectangle2D getBounds() {
113: return bounds;
114: }
115:
116: public void setBounds(Rectangle2D rect) {
117: this .bounds = rect;
118: }
119:
120: public void setWidth(int width) {
121: if (bounds == null)
122: bounds = GeomHelper.newRectangle();
123: bounds.setFrame(0, 0, width, getHeight());
124: }
125:
126: public void setHeight(int height) {
127: if (bounds == null)
128: bounds = GeomHelper.newRectangle();
129: bounds.setFrame(0, 0, getWidth(), height);
130: }
131:
132: public int getFormat() {
133: return format;
134: }
135:
136: public void setFormat(int format) {
137: this .format = format;
138: }
139:
140: public int getColorTableSize() {
141: return colorTableSize;
142: }
143:
144: public void setColorTableSize(int colorTableSize) {
145: this .colorTableSize = colorTableSize;
146: }
147:
148: public DataMarker getZLibData() {
149: return zlibData;
150: }
151:
152: public void setZLibData(DataMarker data) {
153: this .zlibData = data;
154: }
155:
156: public void setAlpha(boolean withAlpha) {
157: if (withAlpha) {
158: tagCode = Tag.DEFINEBITSLOSSLESS2;
159: } else {
160: tagCode = Tag.DEFINEBITSLOSSLESS;
161: }
162: }
163:
164: public boolean isAlpha() {
165: return tagCode == Tag.DEFINEBITSLOSSLESS2;
166: }
167:
168: public static LLBitmap newGIFBitmap(String fileName)
169: throws IVException, IOException {
170: return newGIFBitmap(IVUrl.newUrl(fileName));
171: }
172:
173: public static LLBitmap newGIFBitmap(IVUrl url) throws IVException,
174: IOException {
175: return newGIFBitmap(Util.readUrl(url));
176: }
177:
178: public static LLBitmap newGIFBitmap(FlashBuffer fob)
179: throws IVException {
180: LLBitmap bitmap = new LLBitmap();
181: try {
182: GIFHelper gh = new GIFHelper();
183: gh.doRead(fob);
184: bitmap.setWidth(gh.getWidth());
185: bitmap.setHeight(gh.getHeight());
186: bitmap.setFormat(3); // GIF image are always 8 bits
187: bitmap.setAlpha(gh.isAlpha()); // but can have transparency
188: bitmap.setColorTableSize(gh.getColorTableSize());
189: bitmap.setZLibData(gh.getZlibData());
190: } catch (IOException e) {
191: throw new IVException(Resource.ERRREADINGGIF, e);
192: }
193: return bitmap;
194: }
195:
196: public static LLBitmap newPNGBitmap(String fileName)
197: throws IVException, IOException {
198: return newPNGBitmap(IVUrl.newUrl(fileName));
199: }
200:
201: public static LLBitmap newPNGBitmap(IVUrl url) throws IVException,
202: IOException {
203: return newPNGBitmap(Util.readUrl(url));
204: }
205:
206: public static LLBitmap newPNGBitmap(FlashBuffer fob)
207: throws IVException {
208: LLBitmap bitmap = new LLBitmap();
209: try {
210: PNGHelper pnh = new PNGHelper();
211: pnh.setInputBuffer(fob);
212: bitmap.setZLibData(pnh.getZlibData());
213: bitmap.setWidth(pnh.getWidth());
214: bitmap.setHeight(pnh.getHeight());
215: bitmap.setFormat(pnh.getFormat());
216: bitmap.setAlpha(pnh.hasTransparency());
217: bitmap.setColorTableSize(pnh.getColorTableSize());
218: } catch (IOException e) {
219: throw new IVException(Resource.ERRREADINGPNG, e);
220: }
221: return bitmap;
222: }
223:
224: public void printContent(PrintStream out, String indent) {
225: out.println(indent + "LossLessBitmap(" + Tag.tagNames[tagCode]
226: + "): id=" + getID() + " name='" + getName() + "'");
227: out.print(indent
228: + " format="
229: + (format == 3 ? "8 bit" : (format == 4 ? "16 bit"
230: : "32 bit")));
231: out.println(", colorTableSize=" + colorTableSize + ", width="
232: + getWidth() + ", height=" + getHeight());
233:
234: // print zlib data info
235: /* try {
236: InflaterInputStream zin = new InflaterInputStream( zlibData.getInputStream() );
237: FlashBuffer fob = new FlashBuffer(zin);
238: System.out.println( "size="+fob.getSize() );
239: out.println( indent+" colortable: RGB"+(isAlpha()?"A":"")+"["+(colorTableSize+1)+"]" );
240: int size = (isAlpha()?4:3)*(colorTableSize+1);
241: Util.dump(fob.getBuf(),0,size,out);
242:
243: int datasize = fob.getSize()-size;
244: int width = bounds.getWidth();
245: int height = bounds.getHeight();
246: int datasize1 = ((width+3)&~0x03)*height;
247: out.println( indent+" colordata: UI8["+datasize+"] (rounded(width)*height="+datasize1+")" );
248: Util.dump(fob.getBuf(),size,datasize,out);
249: } catch( IOException e ) {
250: }*/
251: }
252:
253: protected FlashItem copyInto(FlashItem item, ScriptCopier copier) {
254: super .copyInto(item, copier);
255: ((LLBitmap) item).zlibData = (DataMarker) zlibData.getCopy();
256: ((LLBitmap) item).bounds = (Rectangle2D) bounds.clone();
257: ((LLBitmap) item).format = format;
258: ((LLBitmap) item).colorTableSize = colorTableSize;
259: return item;
260: }
261:
262: public FlashItem getCopy(ScriptCopier copier) {
263: return copyInto(new LLBitmap(), copier);
264: }
265:
266: /*
267: public JPEGBitmap convertToJPEG( float quality ) {
268:
269: if( isAlpha() ) {
270: Log.logRB( Resource.STR, "Alpha channel has been stripped from image during conversion to JPEG" );
271: }
272:
273: InflaterInputStream zin = new InflaterInputStream( zlibData.getInputStream() );
274: FlashBuffer fob = new FlashBuffer(zin);
275:
276: int width = bounds.getWidth();
277: int height = bounds.getHeight();
278: int scansize = (width+3)&~0x03;
279: int datasize = scansize*height;
280:
281: int pixelsize = isAlpha()?4:3;
282: int bitsperpixel;
283:
284: // if 8bit image, then get rid of color table
285: if( format == 3 ) {
286: bitsperpixel = 24;
287: //int newscansize = (width*3+3)&~0x03;
288: int newscansize = scansize*3;
289: FrashBuffer fb = new FlashBuffer(newscansize*height);
290: byte[] newtable = fb.getBuf();
291: byte[] table = fob.getBuf();
292: int dataend = fob.getSize();
293: int pos = (colorTableSize+1)*pixelsize;
294: int newpos = 0;
295: for( ; pos<dataend; pos++ ) {
296: int idx = table[pos];
297: newtable[newpos++] = table[idx]; // R
298: newtable[newpos++] = table[idx+1]; // G
299: newtable[newpos++] = table[idx+2]; // B
300: }
301: fob = fb;
302: } else if( isAlpha() ) {
303: // get rid of alpha
304: }
305:
306: DataBuffer db = new DataBufferByte(fob.getBuf(), fob.getSize());
307: WritableRaster raster = Raster.createPackedRaster(db,width,height,,null);
308: Raster raster = Raster.createRaster(sm,db,null);
309:
310: fob = new FlashBuffer(fob.getSize()/4);
311: OutputStream os = fob.getOutputStream();
312: JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
313: JPEGEncodeParam params = encoder.getDefaultJPEGEncodeParam(raster,colorID);
314: params.setQuality(quality,true);
315:
316: encoder.encode(raster,params);
317:
318: }*/
319: }
|