1 50 51 package org.openlaszlo.iv.flash.api.image; 52 53 import java.io.*; 54 import java.awt.color.ColorSpace ; 55 import java.awt.image.ColorConvertOp ; 56 import java.awt.image.BufferedImage ; 57 import java.awt.image.Raster ; 58 import java.awt.geom.*; 59 import org.openlaszlo.iv.flash.url.*; 60 import org.openlaszlo.iv.flash.util.*; 61 import org.openlaszlo.iv.flash.parser.*; 62 import org.openlaszlo.iv.flash.api.*; 63 64 import com.sun.image.codec.jpeg.*; 65 66 public class JPEGBitmap extends Bitmap { 67 68 private static final String jpegTablesName = "%JPEGTABLES$JPEGTABLES%"; 69 private DataMarker data; 70 private FlashItem jpegTables; 71 private Rectangle2D bounds; 72 private JPEGInfo info; 73 74 75 private boolean readyToGen; 76 77 public JPEGBitmap() {} 78 79 public void setData( DataMarker data ) { 80 this.data = data; 81 this.bounds = null; 82 this.readyToGen = false; 83 } 84 85 public static Bitmap parse( Parser p ) { 86 JPEGBitmap o = new JPEGBitmap(); 87 o.setID( p.getUWord() ); 88 o.data = new DataMarker( p.getBuf(), p.getPos(), p.getTagEndPos() ); 89 o.readyToGen = true; 90 o.tagCode = p.getTagCode(); 91 if( o.tagCode == Tag.DEFINEBITS ) { 92 o.jpegTables = p.getDefFromLibrary(jpegTablesName); 93 } 94 return o; 95 } 96 97 public static void parseJPegTables( Parser p ) { 98 JPEGTables tables = new JPEGTables(); 99 tables.setData( new DataMarker( p.getBuf(), p.getPos(), p.getTagEndPos() ) ); tables.setName( jpegTablesName ); 101 p.addDefToLibrary( jpegTablesName, tables ); 102 } 103 104 public int getSize() { 105 return data.buffer.length; 106 } 107 108 public Rectangle2D getBounds() { 109 if( bounds != null ) return bounds; 110 parseData(); 111 return bounds; 112 } 113 114 public void collectDeps( DepsCollector dc ) { 115 116 121 } 122 123 public static JPEGBitmap newJPEGBitmap( String fileName ) 124 throws IVException, IOException 125 { 126 return newJPEGBitmap( IVUrl.newUrl(fileName)); 127 } 128 129 public static JPEGBitmap newJPEGBitmap( IVUrl url ) 130 throws IVException, IOException 131 { 132 return newJPEGBitmap( Util.readUrl(url) ); 133 } 134 135 public static JPEGBitmap newJPEGBitmap( FlashBuffer fob ) throws IVException { 136 JPEGBitmap bitmap = new JPEGBitmap(); 137 bitmap.setTag( Tag.DEFINEBITSJPEG2 ); 138 DataMarker data = new DataMarker(fob.getBuf(), 0, fob.getSize()); 139 bitmap.setData(data); 140 return bitmap; 142 } 143 144 private void parseData() { 145 info = JPEGHelper.getInfo(data.buffer, data.start, data.end); 146 bounds = GeomHelper.newRectangle(0,0,info.width,info.height); 147 148 if( info.type == 0 && info.num_comps == 3 ) { 150 readyToGen = true; 151 } else { 152 readyToGen = false; 153 } 154 } 155 156 164 public void processImage( float quality ) { 165 try { 166 Log.logRB( Resource.REENCODINGJPEG, new Object [] {new Float (quality)} ); 167 int size = data.end-data.start; 168 InputStream is = data.getInputStream(); 169 JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder( is ); 170 BufferedImage image = decoder.decodeAsBufferedImage(); 171 172 info = JPEGHelper.getInfo(data.buffer, data.start, data.end); 173 if (info.num_comps != 3) { 175 ColorConvertOp op = new ColorConvertOp (image.getColorModel().getColorSpace(), 176 ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB), null); 177 image = op.filter(image, null); 178 } 179 180 bounds = GeomHelper.newRectangle(0,0,image.getWidth(),image.getHeight()); 181 182 FlashBuffer fob = new FlashBuffer(size); 183 OutputStream os = fob.getOutputStream(); 184 185 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); 186 JPEGEncodeParam params = encoder.getDefaultJPEGEncodeParam(image); 187 params.setQuality(quality,true); 188 encoder.encode(image,params); 189 190 data.buffer = fob.getBuf(); 191 data.end = fob.getSize(); 192 data.start = 0; 193 readyToGen = true; 194 } catch( IOException e ) { 195 Log.log( e ); 196 } 197 } 198 199 206 public void rescale( int width, int height, float quality ) { 207 try { 208 Log.logRB(Resource.RESCALINGJPEG, 209 new Object [] {new Integer (width), new Integer (height), new Float (quality)}); 210 int size = data.end-data.start; 211 InputStream is = data.getInputStream(); 212 JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(is); 213 BufferedImage image = decoder.decodeAsBufferedImage(); 214 image = (BufferedImage ) image.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH); 215 216 bounds = GeomHelper.newRectangle(0,0,image.getWidth(),image.getHeight()); 217 218 FlashBuffer fob = new FlashBuffer(size); 219 OutputStream os = fob.getOutputStream(); 220 221 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); 222 JPEGEncodeParam params = encoder.getDefaultJPEGEncodeParam(image); 223 params.setQuality(quality,true); 224 encoder.encode(image,params); 225 226 data.buffer = fob.getBuf(); 227 data.end = fob.getSize(); 228 data.start = 0; 229 readyToGen = true; 230 } catch( IOException e ) { 231 Log.log( e ); 232 } 233 } 234 235 245 public void write( FlashOutput fob ) { 246 if( !readyToGen ) { 247 processImage(.75f); 248 } 249 if (tagCode == Tag.DEFINEBITS) { 251 JPEGTables tables = (JPEGTables) jpegTables; 252 fob.writeTag(Tag.DEFINEBITSJPEG2, 2 + data.length() + tables.getDataLength()); 253 fob.writeDefID( this ); 254 tables.writeData(fob); 255 } else { 256 fob.writeTag(tagCode, 2+data.length()); 257 fob.writeDefID( this ); 258 } 259 data.write(fob); 260 } 261 262 public void printContent( PrintStream out, String indent ) { 263 out.println( indent+"JPEGBitmap("+Tag.tagNames[tagCode]+"): id="+getID()+" size="+data.length()+" name='"+getName()+"'" ); 264 } 265 266 protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) { 267 super.copyInto( item, copier ); 268 ((JPEGBitmap)item).data = (DataMarker) data.getCopy(); 269 ((JPEGBitmap)item).jpegTables = jpegTables; 270 ((JPEGBitmap)item).bounds = (Rectangle2D) bounds.clone(); 271 ((JPEGBitmap)item).readyToGen = readyToGen; 272 ((JPEGBitmap)item).info = info!=null?info.getCopy():null; 273 return item; 274 } 275 276 public FlashItem getCopy( ScriptCopier copier ) { 277 return copyInto( new JPEGBitmap(), copier ); 278 } 279 280 public static class JPEGInfo { 281 public int type; public int width; 283 public int height; 284 public int num_comps; 285 public int precision; 286 public JPEGInfo getCopy() { 287 JPEGInfo info = new JPEGInfo(); 288 info.type = type; 289 info.width = width; 290 info.height = height; 291 info.num_comps = num_comps; 292 info.precision = precision; 293 return info; 294 } 295 } 296 297 private static class JPEGTables extends FlashDef { 298 DataMarker data; 299 public JPEGTables() {} 300 public int getTag() { return Tag.JPEGTABLES; } 301 public void setData( DataMarker data ) { this.data = data; } 302 public void write( FlashOutput fob ) { 303 fob.writeTag(Tag.JPEGTABLES, data.length()); 304 data.write(fob); 305 } 306 307 public void writeData( FlashOutput fob ) { 308 data.write(fob); 309 } 310 public int getDataLength( ) { 311 return data.length(); 312 } 313 } 314 315 } 316 317 | Popular Tags |