/*
Copyright (c) 2008, Adobe Systems Incorporated
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Adobe Systems Incorporated nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.alibaba.image
{
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.ProgressEvent;
import flash.events.TimerEvent;
import flash.utils.ByteArray;
import flash.utils.Timer;
/**
*
* Class that converts BitmapData into a valid JPEG (can be asynchronous)
* Flash的BitmapData对象为JPEG编码二进制数据,可以异步方式
*
* @author Created by Adobe,
* @author Edited by Thibault Imbert (http://bytearray.org), convert to fully vector version
* @author Edited by Alibaba UED team
*/
public final class JPGEncoderIMP extends EventDispatcher
{
// Static table initialization
private const ZigZag:Vector. = Vector.([
0, 1, 5, 6,14,15,27,28,
2, 4, 7,13,16,26,29,42,
3, 8,12,17,25,30,41,43,
9,11,18,24,31,40,44,53,
10,19,23,32,39,45,52,54,
20,22,33,38,46,51,55,60,
21,34,37,47,50,56,59,61,
35,36,48,49,57,58,62,63
]);
private var YTable:Vector. = new Vector.(64, true);
private var UVTable:Vector. = new Vector.(64, true);
private var outputfDCTQuant:Vector. = new Vector.(64, true);
private var fdtbl_Y:Vector. = new Vector.(64, true);
private var fdtbl_UV:Vector. = new Vector.(64, true);
private var sf:int;
private const aasf:Vector. = Vector.([
1.0, 1.387039845, 1.306562965, 1.175875602,
1.0, 0.785694958, 0.541196100, 0.275899379
]);
private var YQT:Vector. = Vector.([
16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 56, 68,109,103, 77,
24, 35, 55, 64, 81,104,113, 92,
49, 64, 78, 87,103,121,120,101,
72, 92, 95, 98,112,100,103, 99
]);
private const UVQT:Vector. = Vector.([
17, 18, 24, 47, 99, 99, 99, 99,
18, 21, 26, 66, 99, 99, 99, 99,
24, 26, 56, 99, 99, 99, 99, 99,
47, 66, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99
]);
private function initQuantTables(sf:int):void
{
var i:int;
const I64:int = 64;
const I8:int = 8;
for (i = 0; i < I64; ++i)
{
var t:int = int((YQT[i]*sf+50)*0.01);
if (t < 1) {
t = 1;
} else if (t > 255) {
t = 255;
}
YTable[ZigZag[i]] = t;
}
for (i = 0; i < I64; i++)
{
var u:int = int((UVQT[i]*sf+50)*0.01);
if (u < 1) {
u = 1;
} else if (u > 255) {
u = 255;
}
UVTable[ZigZag[i]] = u;
}
i = 0;
for (var row:int = 0; row < I8; ++row)
{
for (var col:int = 0; col < I8; ++col)
{
fdtbl_Y[i] = (1 / (YTable [ZigZag[i]] * aasf[row] * aasf[col] * I8));
fdtbl_UV[i] = (1 / (UVTable[ZigZag[i]] * aasf[row] * aasf[col] * I8));
i++;
}
}
}
private var YDC_HT:Vector.;
private var UVDC_HT:Vector.;
private var YAC_HT:Vector.;
private var UVAC_HT:Vector.;
private function computeHuffmanTbl(nrcodes:Vector., std_table:Vector.):Vector.
{
var codevalue:int = 0;
var pos_in_table:int = 0;
var HT:Vector. = new Vector.(251, true);
var bitString:BitString;
for (var k:int=1; k<=16; ++k)
{
for (var j:int=1; j<=nrcodes[k]; ++j)
{
HT[std_table[pos_in_table]] = bitString = new BitString();
bitString.val = codevalue;
bitString.len = k;
pos_in_table++;
codevalue++;
}
codevalue<<=1;
}
return HT;
}
private var std_dc_luminance_nrcodes:Vector. = Vector.([0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0]);
private var std_dc_luminance_values:Vector. = Vector.([0,1,2,3,4,5,6,7,8,9,10,11]);
private var std_ac_luminance_nrcodes:Vector. = Vector.([0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d]);
private var std_ac_luminance_values:Vector. = Vector.([0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,
0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,
0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08,
0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,
0x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16,
0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28,
0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,
0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,
0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,
0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,
0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89,
0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,
0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,
0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6,
0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,
0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,
0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2,
0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,
0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,
0xf9,0xfa]);
private var std_dc_chrominance_nrcodes:Vector. = Vector.([0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0]);
private var std_dc_chrominance_values:Vector. = Vector.([0,1,2,3,4,5,6,7,8,9,10,11]);
private var std_ac_chrominance_nrcodes:Vector. = Vector.([0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77]);
private var std_ac_chrominance_values:Vector. = Vector.([0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,
0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,
0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,
0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,
0x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34,
0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26,
0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,
0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,
0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,
0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,
0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,
0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87,
0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,
0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,
0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,
0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,
0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,
0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,
0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,
0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,
0xf9,0xfa
]);
private function initHuffmanTbl():void
{
YDC_HT = computeHuffmanTbl(std_dc_luminance_nrcodes,std_dc_luminance_values);
UVDC_HT = computeHuffmanTbl(std_dc_chrominance_nrcodes,std_dc_chrominance_values);
YAC_HT = computeHuffmanTbl(std_ac_luminance_nrcodes,std_ac_luminance_values);
UVAC_HT = computeHuffmanTbl(std_ac_chrominance_nrcodes,std_ac_chrominance_values);
}
private var bitcode:Vector. = new Vector.(65535, true);
private var category:Vector. = new Vector.(65535, true);
private function initCategoryNumber():void
{
var nrlower:int = 1;
var nrupper:int = 2;
var bitString:BitString;
const I15:int = 15;
var pos:int;
for (var cat:int=1; cat<=I15; ++cat)
{
//Positive numbers
for (var nr:int=nrlower; nr= 0 )
{
if (value & uint(1 << posval) )
bytenew |= uint(1 << bytepos);
posval--;
bytepos--;
if (bytepos < 0)
{
if (bytenew == 0xFF)
{
byteout.writeByte(0xFF);
byteout.writeByte(0);
}
else byteout.writeByte(bytenew);
bytepos=7;
bytenew=0;
}
}
}
// DCT & quantization core
private function fDCTQuant(data:Vector., fdtbl:Vector.):Vector.
{
/* Pass 1: process rows. */
var dataOff:int=0;
var d0:Number, d1:Number, d2:Number, d3:Number, d4:Number, d5:Number, d6:Number, d7:Number;
var i:int;
const I8:int = 8;
const I64:int = 64;
for (i=0; i 0.0) ? int(fDCTQuant + 0.5) : int(fDCTQuant - 0.5);
}
return outputfDCTQuant;
}
// Chunk writing
private function writeAPP0():void
{
byteout.writeShort(0xFFE0); // marker
byteout.writeShort(16); // length
byteout.writeByte(0x4A); // J
byteout.writeByte(0x46); // F
byteout.writeByte(0x49); // I
byteout.writeByte(0x46); // F
byteout.writeByte(0); // = "JFIF",'\0'
byteout.writeByte(1); // versionhi
byteout.writeByte(1); // versionlo
byteout.writeByte(0); // xyunits
byteout.writeShort(1); // xdensity
byteout.writeShort(1); // ydensity
byteout.writeByte(0); // thumbnwidth
byteout.writeByte(0); // thumbnheight
}
private function writeSOF0(width:int, height:int):void
{
byteout.writeShort(0xFFC0); // marker
byteout.writeShort(17); // length, truecolor YUV JPG
byteout.writeByte(8); // precision
byteout.writeShort(height);
byteout.writeShort(width);
byteout.writeByte(3); // nrofcomponents
byteout.writeByte(1); // IdY
byteout.writeByte(0x11); // HVY
byteout.writeByte(0); // QTY
byteout.writeByte(2); // IdU
byteout.writeByte(0x11); // HVU
byteout.writeByte(1); // QTU
byteout.writeByte(3); // IdV
byteout.writeByte(0x11); // HVV
byteout.writeByte(1); // QTV
}
private function writeDQT():void
{
byteout.writeShort(0xFFDB); // marker
byteout.writeShort(132); // length
byteout.writeByte(0);
var i:int;
const I64:int = 64;
for (i=0; i = new Vector.(64, true);
private function processDU(CDU:Vector., fdtbl:Vector., DC:Number, HTDC:Vector., HTAC:Vector.):Number
{
var EOB:BitString = HTAC[0x00];
var M16zeroes:BitString = HTAC[0xF0];
var pos:int;
const I16:int = 16;
const I63:int = 63;
const I64:int = 64;
var DU_DCT:Vector. = fDCTQuant(CDU, fdtbl);
//ZigZag reorder
for (var j:int=0;j0)&&(DU[end0pos]==0); end0pos--) {};
//end0pos = first element in reverse order !=0
if ( end0pos == 0) {
writeBits(EOB);
return DC;
}
var i:int = 1;
var lng:int;
while ( i <= end0pos ) {
var startpos:int = i;
for (; (DU[i]==0) && (i<=end0pos); ++i) {}
var nrzeroes:int = i-startpos;
if ( nrzeroes >= I16 ) {
lng = nrzeroes>>4;
for (var nrmarker:int=1; nrmarker <= lng; ++nrmarker)
writeBits(M16zeroes);
nrzeroes = int(nrzeroes&0xF);
}
pos = int(32767+DU[i]);
writeBits(HTAC[int((nrzeroes<<4)+category[pos])]);
writeBits(bitcode[pos]);
i++;
}
if ( end0pos != I63 ) {
writeBits(EOB);
}
return DC;
}
private var YDU:Vector. = new Vector.(64, true);
private var UDU:Vector. = new Vector.(64, true);
private var VDU:Vector. = new Vector.(64, true);
private function RGB2YUV(img:BitmapData, xpos:int, ypos:int):void
{
var pos:int=0;
const I8:int = 8;
for (var y:int=0; y>16)&0xFF;
var G:int = (P>> 8)&0xFF;
var B:int = (P )&0xFF;
YDU[int(pos)]=((( 0.29900)*R+( 0.58700)*G+( 0.11400)*B))-0x80;
UDU[int(pos)]=(((-0.16874)*R+(-0.33126)*G+( 0.50000)*B));
VDU[int(pos)]=((( 0.50000)*R+(-0.41869)*G+(-0.08131)*B));
++pos;
}
}
}
public function JPGEncoderIMP(quality:int=50)
{
if (quality <= 0)
quality = 1;
if (quality > 100)
quality = 100;
sf = quality < 50 ? int(5000 / quality) : int(200 - (quality<<1));
init();
}
private function init():void
{
ZigZag.fixed = true;
aasf.fixed = true;
YQT.fixed = true;
UVQT.fixed = true;
std_ac_chrominance_nrcodes.fixed = true;
std_ac_chrominance_values.fixed = true;
std_ac_luminance_nrcodes.fixed = true;
std_ac_luminance_values.fixed = true;
std_dc_chrominance_nrcodes.fixed = true;
std_dc_chrominance_values.fixed = true;
std_dc_luminance_nrcodes.fixed = true;
std_dc_luminance_values.fixed = true;
// Create tables
initHuffmanTbl();
initCategoryNumber();
initQuantTables(sf);
}
public function encode(image:BitmapData):ByteArray
{
// Initialize bit writer
byteout = new ByteArray();
bytenew=0;
bytepos=7;
// Add JPEG headers
byteout.writeShort(0xFFD8); // SOI
writeAPP0();
writeDQT();
writeSOF0(image.width,image.height);
writeDHT();
writeSOS();
// Encode 8x8 macroblocks
var DCY:Number=0;
var DCU:Number=0;
var DCV:Number=0;
bytenew=0;
bytepos=7;
var width:int = image.width;
var height:int = image.height;
for (var ypos:int=0; ypos= 0 )
{
var fillbits:BitString = new BitString();
fillbits.len = bytepos+1;
fillbits.val = (1<<(bytepos+1))-1;
writeBits(fillbits);
}
byteout.writeShort(0xFFD9); //EOI
return byteout;
}
public var cur:int = 0;
public var maxCur:int;
private var t:Timer;
private var image:BitmapData;
/**
* Created a JPEG image from the specified BitmapData (async)
*
* @param image The BitmapData that will be converted into the JPEG format.
* @param _mode
* @langversion ActionScript 3.0
* @playerversion Flash 9.0
* @tiptext
*/
public function encodeAsync(_image:BitmapData,_mode:String=""):void
{
image = new BitmapData(_image.width, _image.height, true);
image = _image;
// Initialize bit writer
byteout = new ByteArray();
bytenew=0;
bytepos=7;
// Add JPEG headers
writeWord(0xFFD8); // SOI
writeAPP0();
writeDQT();
writeSOF0(image.width,image.height);
writeDHT();
writeSOS();
// Encode 8x8 macroblocks
DCY=0;
DCU=0;
DCV=0;
bytenew=0;
bytepos=7;
maxCur = image.height * image.width;
t = new Timer(10, 0);
t.addEventListener(TimerEvent.TIMER, progressingRepeatX);
t.start();
}
private function finished():void
{
// Do the bit alignment of the EOI marker
if ( bytepos >= 0 ) {
var fillbits:BitString = new BitString();
fillbits.len = bytepos+1;
fillbits.val = (1<<(bytepos+1))-1;
writeBits(fillbits);
}
writeWord(0xFFD9); //EOI
}
private function writeByte(value:int):void
{
byteout.writeByte(value);
}
private function writeWord(value:int):void
{
writeByte((value>>8)&0xFF);
writeByte((value )&0xFF);
}
private var xpos:int=0;
private var ypos:int=0;
private var DCY:Number=0;
private var DCU:Number=0;
private var DCV:Number = 0;
/** 输出的ByteArray对象 **/
public var ba:ByteArray;
/*
* width < height
*/
/**
* 事件:压缩进度
* @eventType "progressing"
* @eventType "complete"
* @param e
*/
private function progressingRepeatX(e:TimerEvent):void
{
for (var xpos:int=0; xpos= image.height)
{
dispatchEvent(new ProgressEvent("progressing", false, false, cur, maxCur));
t.stop();
t.removeEventListener(TimerEvent.TIMER, progressingRepeatX);
finished();
ba = new ByteArray();
ba = byteout;
dispatchEvent(new Event(Event.COMPLETE));
}
else dispatchEvent(new ProgressEvent("progressing", false, false, cur, maxCur));
}
}
}
final class BitString
{
public var len:int = 0;
public var val:int = 0;
}