| 001 | /**
|
| 002 | * Copyright 04.09.2009 Christian Sobolewski
|
| 003 | *
|
| 004 | * usage : com.cs.imageProcessor
|
| 005 | *
|
| 006 | * Parameters:
|
| 007 | *
|
| 008 | * Version:
|
| 009 | *
|
| 010 | * History:
|
| 011 | *
|
| 012 | * com.cs.imageProcessor.ImageProcessor
|
| 013 | * @author Christian Sobolewski
|
| 014 | */
|
| 015 | package com.cs.imageProcessor {
|
| 016 | import com.adobe.images.JPGEncoder;
|
| 017 | import com.adobe.images.PNGEncoder;
|
| 018 | import com.cs.logging.SOS;
|
| 019 | |
| 020 | import org.libspark.swfassist.io.ByteArrayOutputStream;
|
| 021 | import org.libspark.swfassist.swf.io.SWFWriter;
|
| 022 | import org.libspark.swfassist.swf.io.WritingContext;
|
| 023 | import org.libspark.swfassist.swf.structures.FillStyle;
|
| 024 | import org.libspark.swfassist.swf.structures.FillStyleTypeConstants;
|
| 025 | import org.libspark.swfassist.swf.structures.SWF;
|
| 026 | import org.libspark.swfassist.swf.structures.ShapeWithStyle;
|
| 027 | import org.libspark.swfassist.swf.structures.StraightEdgeRecord;
|
| 028 | import org.libspark.swfassist.swf.structures.StyleChangeRecord;
|
| 029 | import org.libspark.swfassist.swf.tags.DefineBitsJPEG3;
|
| 030 | import org.libspark.swfassist.swf.tags.DefineShape;
|
| 031 | import org.libspark.swfassist.swf.tags.PlaceObject2;
|
| 032 | import org.libspark.swfassist.swf.tags.SetBackgroundColor;
|
| 033 | import org.libspark.swfassist.swf.tags.ShowFrame;
|
| 034 | |
| 035 | import flash.display.BitmapData;
|
| 036 | import flash.display.Sprite;
|
| 037 | import flash.events.Event;
|
| 038 | import flash.events.EventDispatcher;
|
| 039 | import flash.events.IOErrorEvent;
|
| 040 | import flash.events.MouseEvent;
|
| 041 | import flash.events.ProgressEvent;
|
| 042 | import flash.events.SecurityErrorEvent;
|
| 043 | import flash.net.FileReference;
|
| 044 | import flash.utils.ByteArray;
|
| 045 | |
| 046 | /**
|
| 047 | * @author Christian Sobolewski
|
| 048 | */
|
| 049 | public class ImageToSwfProcessor extends EventDispatcher {
|
| 050 | |
| 051 | private var _swf : SWF;
|
| 052 | private var _bytes : ByteArray;
|
| 053 | private var _fileRef : FileReference;
|
| 054 | private var _name : String;
|
| 055 | private var _eof : Sprite;
|
| 056 | |
| 057 | public function ImageToSwfProcessor() {
|
| 058 | }
|
| 059 | |
| 060 | public function writeSwfFromImage(image : *, quality : int, type : String , frames : int = 1, bgColor : int = 0x000066) : BitmapData {
|
| 061 | _name = image.name.split(","+type).join("").toString();
|
| 062 | |
| 063 | var w : Number = image.width;
|
| 064 | var h : Number = image.height;
|
| 065 | |
| 066 | _swf = new SWF();
|
| 067 | _swf.header.version = 9;
|
| 068 | _swf.header.isCompressed = false;
|
| 069 | _swf.header.frameSize.xMax = image.width;
|
| 070 | _swf.header.frameSize.yMax = image.height;
|
| 071 | _swf.header.frameRate = 1;
|
| 072 | _swf.header.numFrames = 1;
|
| 073 | |
| 074 | var bg : SetBackgroundColor = new SetBackgroundColor();
|
| 075 | bg.backgroundColor.fromUint(bgColor);
|
| 076 | _swf.tags.addTag(bg);
|
| 077 | |
| 078 | var bmd : BitmapData;
|
| 079 | |
| 080 | if (type == "png") {
|
| 081 | bmd = new BitmapData(w, h, true, 0x000000ff);
|
| 082 | bmd.draw(image);
|
| 083 | _bytes = PNGEncoder.encode(bmd);
|
| 084 | } else {
|
| 085 | bmd = new BitmapData(w, h, false);
|
| 086 | bmd.draw(image);
|
| 087 | _bytes = new JPGEncoder(quality).encode(bmd);
|
| 088 | }
|
| 089 | |
| 090 | // SOS.alert("image.width = " + image.width);
|
| 091 | // SOS.alert("image.height= " + image.height);
|
| 092 | |
| 093 | for (var i : uint = 0;i < frames;++i) {
|
| 094 | |
| 095 | var defineJPEG : DefineBitsJPEG3 = new DefineBitsJPEG3();
|
| 096 | defineJPEG.characterId = (i * 2) + 1;
|
| 097 | defineJPEG.jpegData = _bytes;
|
| 098 | |
| 099 | _swf.tags.addTag(defineJPEG);
|
| 100 | |
| 101 | var defineShape : DefineShape = new DefineShape();
|
| 102 | defineShape.shapeId = (i * 2 + 1) + 1;
|
| 103 | defineShape.shapeBounds.xMax = w;
|
| 104 | defineShape.shapeBounds.yMax = h;
|
| 105 | var fillStyle : FillStyle = new FillStyle();
|
| 106 | fillStyle.fillStyleType = FillStyleTypeConstants.CLIPPED_BITMAP_FILL;
|
| 107 | fillStyle.bitmapId = (i * 2) + 1;
|
| 108 | fillStyle.bitmapMatrix.hasScale = true;
|
| 109 | fillStyle.bitmapMatrix.scaleX = 0;
|
| 110 | fillStyle.bitmapMatrix.scaleY = 0;
|
| 111 | var shape : ShapeWithStyle = defineShape.shapes;
|
| 112 | shape.fillStyles.fillStyles.push(fillStyle);
|
| 113 | var r1 : StyleChangeRecord = new StyleChangeRecord();
|
| 114 | r1.fillStyle0 = 1;
|
| 115 | r1.moveDeltaX = 0;
|
| 116 | r1.moveDeltaY = 0;
|
| 117 | r1.stateFillStyle0 = true;
|
| 118 | r1.stateMoveTo = true;
|
| 119 | var r2 : StraightEdgeRecord = new StraightEdgeRecord();
|
| 120 | r2.verticalLine = true;
|
| 121 | r2.deltaY = h;
|
| 122 | var r3 : StraightEdgeRecord = new StraightEdgeRecord();
|
| 123 | r3.horizontalLine = true;
|
| 124 | r3.deltaX = w;
|
| 125 | var r4 : StraightEdgeRecord = new StraightEdgeRecord();
|
| 126 | r4.verticalLine = true;
|
| 127 | r4.deltaY = -h;
|
| 128 | var r5 : StraightEdgeRecord = new StraightEdgeRecord();
|
| 129 | r5.horizontalLine = true;
|
| 130 | r5.deltaX = -w;
|
| 131 | shape.shapeRecords.push(r1, r2, r3, r4, r5);
|
| 132 | |
| 133 | _swf.tags.addTag(defineShape);
|
| 134 | |
| 135 | var placeObject : PlaceObject2 = new PlaceObject2();
|
| 136 | placeObject.characterId = (i * 2 + 1) + 1;
|
| 137 | placeObject.depth = i + 1;
|
| 138 | placeObject.hasCharacter = true;
|
| 139 | |
| 140 | _swf.tags.addTag(placeObject);
|
| 141 | _swf.tags.addTag(new ShowFrame());
|
| 142 | _swf.header.numFrames++;
|
| 143 | }
|
| 144 | |
| 145 | |
| 146 | var swfBytes : ByteArray = new ByteArray();
|
| 147 | |
| 148 | var wc : WritingContext = new WritingContext();
|
| 149 | // wc.length = 5000;
|
| 150 | |
| 151 | var write : SWFWriter = new SWFWriter();
|
| 152 | write.writeSWF(new ByteArrayOutputStream(swfBytes), wc, _swf);
|
| 153 | |
| 154 | _fileRef = new FileReference();
|
| 155 | _fileRef.addEventListener(Event.COMPLETE, handleCompleteEvent, false, 0, true);
|
| 156 | _fileRef.addEventListener(ProgressEvent.PROGRESS, handleProgressEvent, false, 0, true);
|
| 157 | _fileRef.addEventListener(Event.CANCEL, handleCancelEvent, false, 0, true);
|
| 158 | _fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurityEvent, false, 0, true);
|
| 159 | _fileRef.addEventListener(IOErrorEvent.IO_ERROR, onSaveError, false, 0, true);
|
| 160 | _fileRef.addEventListener(Event.OPEN, onOpen, false, 0, true);
|
| 161 | |
| 162 | _fileRef.save(swfBytes, _name + ".swf");
|
| 163 | |
| 164 | /**
|
| 165 | * strange things happen here. if you dont activate Event.ENTER_FRAME no Event.COMPLETE will fire !!!
|
| 166 | */
|
| 167 | |
| 168 | _eof = new Sprite();
|
| 169 | _eof.addEventListener(Event.ENTER_FRAME, handleEof, false, 0, true);
|
| 170 | |
| 171 | return bmd;
|
| 172 | }
|
| 173 | |
| 174 | private function handleEof(event : Event) : void {
|
| 175 | if(_fileRef.data) SOS.alert(_fileRef.data);
|
| 176 | }
|
| 177 | |
| 178 | private function onOpen(event : Event) : void {
|
| 179 | // SOS.alert('onOpen: ' + (onOpen));
|
| 180 | }
|
| 181 | |
| 182 | private function handleClick(event : MouseEvent) : void {
|
| 183 | // SOS.alert('handleClick: ' + (handleClick));
|
| 184 | }
|
| 185 | |
| 186 | private function onSaveError(event : IOErrorEvent) : void {
|
| 187 | // SOS.alert('onSaveError: ' + (onSaveError));
|
| 188 | }
|
| 189 | |
| 190 | private function handleSecurityEvent(event : SecurityErrorEvent) : void {
|
| 191 | // SOS.alert('handleSecurityEvent: ' + (handleSecurityEvent));
|
| 192 | }
|
| 193 | |
| 194 | private function handleCancelEvent(event : Event) : void {
|
| 195 | // SOS.alert("cancel event");
|
| 196 | }
|
| 197 | |
| 198 | private function handleProgressEvent(event : ProgressEvent) : void {
|
| 199 | SOS.alert(event.bytesLoaded + " | " + event.bytesTotal);
|
| 200 | }
|
| 201 | |
| 202 | private function handleCompleteEvent(event : Event) : void {
|
| 203 | if (_eof.hasEventListener(Event.ENTER_FRAME)) _eof.removeEventListener(Event.ENTER_FRAME, handleEof);
|
| 204 | dispatchEvent(new ImageProcessorEvent(ImageProcessorEvent.NEXT_FILE))
|
| 205 | // SOS.alert("save complete");
|
| 206 | }
|
| 207 | }
|
| 208 | }
|
| 209 | |