1 50 51 package org.openlaszlo.iv.flash.api.image; 52 53 import java.io.*; 54 import java.awt.geom.*; 55 import org.openlaszlo.iv.flash.util.*; 56 import org.openlaszlo.iv.flash.cache.*; 57 import org.openlaszlo.iv.flash.url.*; 58 import org.openlaszlo.iv.flash.parser.*; 59 import org.openlaszlo.iv.flash.api.*; 60 import org.openlaszlo.iv.flash.api.shape.*; 61 62 65 public abstract class Bitmap extends FlashDef { 66 67 protected int tagCode; 68 69 public int getTag() { 70 return tagCode; 71 } 72 73 public void setTag( int tagCode ) { 74 this.tagCode = tagCode; 75 } 76 77 public boolean isConstant() { 78 return true; 79 } 80 81 84 public int getWidth() { return (int) getBounds().getWidth(); } 85 86 89 public int getHeight() { return (int) getBounds().getHeight(); } 90 91 94 public abstract int getSize(); 95 96 102 public Instance newInstance() { 103 return newInstance( 0, 0, false, false ); 104 } 105 106 115 public Instance newInstance( int width, int height, boolean scale, boolean center ) { 116 117 int bmWidth = getWidth()*20; 118 int bmHeight = getHeight()*20; 119 120 Shape shape = new Shape(); 121 shape.setFillStyle0( 0 ); 122 123 AffineTransform fillMatrix = AffineTransform.getTranslateInstance(-10, -10); 126 fillMatrix.scale(20, 20); 128 shape.setFillStyle1( FillStyle.newClippedBitmap(this, fillMatrix) ); 129 130 shape.drawRectangle(0, 0, bmWidth, bmHeight); 131 shape.setBounds( GeomHelper.newRectangle(0, 0, bmWidth, bmHeight) ); 132 133 AffineTransform myMatrix; 134 if( scale ) { 135 myMatrix = AffineTransform.getScaleInstance( (double)width/bmWidth, (double)height/bmHeight ); 136 } else { 137 myMatrix = center? new AffineTransform(): null; 138 } 139 if( center ) { 140 myMatrix.translate( -bmWidth/2, -bmHeight/2 ); 141 } 142 143 Instance inst = new Instance(); 144 inst.def = shape; 145 inst.matrix = myMatrix; 146 147 return inst; 148 } 149 150 public static Bitmap newBitmap( String fileName ) 151 throws IVException, IOException 152 { 153 return newBitmap( IVUrl.newUrl(fileName) ); 154 } 155 156 public static Bitmap newBitmap( IVUrl url ) 157 throws IVException, IOException 158 { 159 Bitmap bitmap = newBitmap( Util.readUrl(url) ); 160 if( bitmap != null ) return bitmap; 161 throw new IVException( Resource.UNSUPMEDIA, new Object [] {url.getName()} ); 162 } 163 164 168 public static Bitmap newBitmap( FlashBuffer fb ) throws IVException { 169 fb.setPos(0); 171 int b0 = fb.getUByte(); 172 int b1 = fb.getUByte(); 173 int b2 = fb.getUByte(); 174 int b3 = fb.getUByte(); 175 176 if( b0 == 0xff && b1 == 0xd8 ) { return JPEGBitmap.newJPEGBitmap(fb); 178 } else if( b0 == 'G' && b1 == 'I' && b2 == 'F' ) { return LLBitmap.newGIFBitmap(fb); 180 } else if( b0 == 0x89 && b1 == 0x50 && b2 == 0x4e && b3 == 0x47 ) { return LLBitmap.newPNGBitmap(fb); 182 } else { 183 return null; 184 } 185 } 186 187 protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) { 188 super.copyInto( item, copier ); 189 ((Bitmap)item).tagCode = tagCode; 190 return item; 191 } 192 193 } 194 195 | Popular Tags |