1 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; protected int colorTableSize; 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 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 fob.writeLongTag( tagCode, tagSize ); 93 fob.writeDefID(this); 94 fob.writeByte(format); 95 fob.writeWord(getWidth()); 96 fob.writeWord(getHeight()); 97 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 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); bitmap.setAlpha(gh.isAlpha()); 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 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 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 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 290 } 291 292 | Popular Tags |