Advertisement
Guest User

ts

a guest
May 29th, 2009
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.display.BitmapData;
  4.     import flash.utils.ByteArray;
  5.    
  6.     public final class JPEGEncoderTS
  7.     {
  8.         // Static table initialization
  9.         private const ZigZag:Vector.<int> = Vector.<int>([
  10.              0, 1, 5, 6,14,15,27,28,
  11.              2, 4, 7,13,16,26,29,42,
  12.              3, 8,12,17,25,30,41,43,
  13.              9,11,18,24,31,40,44,53,
  14.             10,19,23,32,39,45,52,54,
  15.             20,22,33,38,46,51,55,60,
  16.             21,34,37,47,50,56,59,61,
  17.             35,36,48,49,57,58,62,63
  18.         ]);
  19.         private var YTable:Vector.<int> = new Vector.<int>(64, true);
  20.         private var UVTable:Vector.<int> = new Vector.<int>(64, true);
  21.         private var outputfDCTQuant:Vector.<int> = new Vector.<int>(64, true);
  22.         private var fdtbl_Y:Vector.<Number> = new Vector.<Number>(64, true);
  23.         private var fdtbl_UV:Vector.<Number> = new Vector.<Number>(64, true);
  24.         private var sf:int;
  25.        
  26.         private const aasf:Vector.<Number> = Vector.<Number>([
  27.                 1.0, 1.387039845, 1.306562965, 1.175875602,
  28.                 1.0, 0.785694958, 0.541196100, 0.275899379
  29.             ]);
  30.        
  31.         private var YQT:Vector.<int> = Vector.<int>([
  32.                 16, 11, 10, 16, 24, 40, 51, 61,
  33.                 12, 12, 14, 19, 26, 58, 60, 55,
  34.                 14, 13, 16, 24, 40, 57, 69, 56,
  35.                 14, 17, 22, 29, 51, 87, 80, 62,
  36.                 18, 22, 37, 56, 68,109,103, 77,
  37.                 24, 35, 55, 64, 81,104,113, 92,
  38.                 49, 64, 78, 87,103,121,120,101,
  39.                 72, 92, 95, 98,112,100,103, 99
  40.             ]);
  41.        
  42.         private const UVQT:Vector.<int> = Vector.<int>([
  43.                 17, 18, 24, 47, 99, 99, 99, 99,
  44.                 18, 21, 26, 66, 99, 99, 99, 99,
  45.                 24, 26, 56, 99, 99, 99, 99, 99,
  46.                 47, 66, 99, 99, 99, 99, 99, 99,
  47.                 99, 99, 99, 99, 99, 99, 99, 99,
  48.                 99, 99, 99, 99, 99, 99, 99, 99,
  49.                 99, 99, 99, 99, 99, 99, 99, 99,
  50.                 99, 99, 99, 99, 99, 99, 99, 99
  51.             ]);
  52.    
  53.         private function initQuantTables(sf:int):void
  54.         {
  55.             var i:int;
  56.             const I64:int = 64;
  57.             const I8:int = 8;
  58.             for (i = 0; i < I64; ++i)
  59.             {
  60.                 var t:int = int((YQT[i]*sf+50)*0.01);
  61.                 if (t < 1) {
  62.                     t = 1;
  63.                 } else if (t > 255) {
  64.                     t = 255;
  65.                 }
  66.                 YTable[ZigZag[i]] = t;
  67.             }
  68.  
  69.             for (i = 0; i < I64; i++)
  70.             {
  71.                 var u:int = int((UVQT[i]*sf+50)*0.01);
  72.                 if (u < 1) {
  73.                     u = 1;
  74.                 } else if (u > 255) {
  75.                     u = 255;
  76.                 }
  77.                 UVTable[ZigZag[i]] = u;
  78.             }
  79.             i = 0;
  80.             for (var row:int = 0; row < I8; ++row)
  81.             {
  82.                 for (var col:int = 0; col < I8; ++col)
  83.                 {
  84.                     fdtbl_Y[i]  = (1 / (YTable [ZigZag[i]] * aasf[row] * aasf[col] * I8));
  85.                     fdtbl_UV[i] = (1 / (UVTable[ZigZag[i]] * aasf[row] * aasf[col] * I8));
  86.                     i++;
  87.                 }
  88.             }
  89.         }
  90.    
  91.         private var YDC_HT:Vector.<BitString>;
  92.         private var UVDC_HT:Vector.<BitString>;
  93.         private var YAC_HT:Vector.<BitString>;
  94.         private var UVAC_HT:Vector.<BitString>;
  95.    
  96.         private function computeHuffmanTbl(nrcodes:Vector.<int>, std_table:Vector.<int>):Vector.<BitString>
  97.         {
  98.             var codevalue:int = 0;
  99.             var pos_in_table:int = 0;
  100.             var HT:Vector.<BitString> = new Vector.<BitString>(251, true);
  101.             var bitString:BitString;
  102.             for (var k:int=1; k<=16; ++k)
  103.             {
  104.                 for (var j:int=1; j<=nrcodes[k]; ++j)
  105.                 {
  106.                     HT[std_table[pos_in_table]] = bitString = new BitString();
  107.                     bitString.val = codevalue;
  108.                     bitString.len = k;
  109.                     pos_in_table++;
  110.                     codevalue++;
  111.                 }
  112.                 codevalue<<=1;
  113.             }
  114.             return HT;
  115.         }
  116.    
  117.         private var std_dc_luminance_nrcodes:Vector.<int> = Vector.<int>([0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0]);
  118.         private var std_dc_luminance_values:Vector.<int> = Vector.<int>([0,1,2,3,4,5,6,7,8,9,10,11]);
  119.         private var std_ac_luminance_nrcodes:Vector.<int> = Vector.<int>([0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d]);
  120.         private var std_ac_luminance_values:Vector.<int> = Vector.<int>([0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,
  121.                                                                                 0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,
  122.                                                                                 0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08,
  123.                                                                                 0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,
  124.                                                                                 0x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16,
  125.                                                                                 0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28,
  126.                                                                                 0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,
  127.                                                                                 0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,
  128.                                                                                 0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
  129.                                                                                 0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,
  130.                                                                                 0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,
  131.                                                                                 0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89,
  132.                                                                                 0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,
  133.                                                                                 0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,
  134.                                                                                 0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6,
  135.                                                                                 0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,
  136.                                                                                 0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,
  137.                                                                                 0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2,
  138.                                                                                 0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,
  139.                                                                                 0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,
  140.                                                                                 0xf9,0xfa]);
  141.    
  142.         private var std_dc_chrominance_nrcodes:Vector.<int> = Vector.<int>([0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0]);
  143.         private var std_dc_chrominance_values:Vector.<int> = Vector.<int>([0,1,2,3,4,5,6,7,8,9,10,11]);
  144.         private var std_ac_chrominance_nrcodes:Vector.<int> = Vector.<int>([0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77]);
  145.         private var std_ac_chrominance_values:Vector.<int> = Vector.<int>([0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,
  146.                                                                                 0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,
  147.                                                                                 0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,
  148.                                                                                 0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,
  149.                                                                                 0x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34,
  150.                                                                                 0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26,
  151.                                                                                 0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,
  152.                                                                                 0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,
  153.                                                                                 0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,
  154.                                                                                 0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,
  155.                                                                                 0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,
  156.                                                                                 0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87,
  157.                                                                                 0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,
  158.                                                                                 0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,
  159.                                                                                 0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,
  160.                                                                                 0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,
  161.                                                                                 0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,
  162.                                                                                 0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,
  163.                                                                                 0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,
  164.                                                                                 0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,
  165.                                                                                 0xf9,0xfa
  166.                                                                             ]);
  167.  
  168.         private function initHuffmanTbl():void
  169.         {
  170.             YDC_HT = computeHuffmanTbl(std_dc_luminance_nrcodes,std_dc_luminance_values);
  171.             UVDC_HT = computeHuffmanTbl(std_dc_chrominance_nrcodes,std_dc_chrominance_values);
  172.             YAC_HT = computeHuffmanTbl(std_ac_luminance_nrcodes,std_ac_luminance_values);
  173.             UVAC_HT = computeHuffmanTbl(std_ac_chrominance_nrcodes,std_ac_chrominance_values);
  174.         }
  175.    
  176.         private var bitcode:Vector.<BitString> = new Vector.<BitString>(65535, true);
  177.         private var category:Vector.<int> = new Vector.<int>(65535, true);
  178.    
  179.         private function initCategoryNumber():void
  180.         {
  181.             var nrlower:int = 1;
  182.             var nrupper:int = 2;
  183.             var bitString:BitString;
  184.             const I15:int = 15;
  185.             var pos:int;
  186.             for (var cat:int=1; cat<=I15; ++cat)
  187.             {
  188.                 //Positive numbers
  189.                 for (var nr:int=nrlower; nr<nrupper; ++nr)
  190.                 {
  191.                     pos = int(32767+nr);
  192.                     category[pos] = cat;
  193.                     bitcode[pos] = bitString = new BitString();
  194.                     bitString.len = cat;
  195.                     bitString.val = nr;
  196.                 }
  197.                 //Negative numbers
  198.                 for (var nrneg:int=-(nrupper-1); nrneg<=-nrlower; ++nrneg)
  199.                 {
  200.                     pos = int(32767+nrneg);
  201.                     category[pos] = cat;
  202.                     bitcode[pos] = bitString = new BitString();
  203.                     bitString.len = cat;
  204.                     bitString.val = nrupper-1+nrneg;
  205.                 }
  206.                 nrlower <<= 1;
  207.                 nrupper <<= 1;
  208.             }
  209.         }
  210.    
  211.         // IO functions
  212.    
  213.         private var byteout:ByteArray;
  214.         private var bytenew:int = 0;
  215.         private var bytepos:int = 7;
  216.    
  217.         private function writeBits(bs:BitString):void
  218.         {
  219.             var value:int = bs.val;
  220.             var posval:int = bs.len-1;
  221.             while ( posval >= 0 )
  222.             {
  223.                 if (value & uint(1 << posval) )
  224.                     bytenew |= uint(1 << bytepos);
  225.                 posval--;
  226.                 bytepos--;
  227.                 if (bytepos < 0)
  228.                 {
  229.                     if (bytenew == 0xFF)
  230.                     {
  231.                         byteout.writeByte(0xFF);
  232.                         byteout.writeByte(0);
  233.                     }
  234.                     else byteout.writeByte(bytenew);
  235.                     bytepos=7;
  236.                     bytenew=0;
  237.                 }
  238.             }
  239.         }
  240.    
  241.         // DCT & quantization core
  242.    
  243.         private function fDCTQuant(data:Vector.<Number>, fdtbl:Vector.<Number>):Vector.<int>
  244.         {
  245.             var d0:Number, d1:Number, d2:Number, d3:Number, d4:Number, d5:Number, d6:Number, d7:Number;
  246.             /* Pass 1: process rows. */
  247.             var dataOff:int=0;
  248.             var i:int;
  249.             const I8:int = 8;
  250.             const I64:int = 64;
  251.             for (i=0; i<I8; ++i)
  252.             {
  253.                 d0 = data[int(dataOff)];
  254.                 d1 = data[int(dataOff+1)];
  255.                 d2 = data[int(dataOff+2)];
  256.                 d3 = data[int(dataOff+3)];
  257.                 d4 = data[int(dataOff+4)];
  258.                 d5 = data[int(dataOff+5)];
  259.                 d6 = data[int(dataOff+6)];
  260.                 d7 = data[int(dataOff+7)];
  261.                
  262.                 var tmp0:Number = d0 + d7;
  263.                 var tmp7:Number = d0 - d7;
  264.                 var tmp1:Number = d1 + d6;
  265.                 var tmp6:Number = d1 - d6;
  266.                 var tmp2:Number = d2 + d5;
  267.                 var tmp5:Number = d2 - d5;
  268.                 var tmp3:Number = d3 + d4;
  269.                 var tmp4:Number = d3 - d4;
  270.    
  271.                 /* Even part */
  272.                 var tmp10:Number = tmp0 + tmp3; /* phase 2 */
  273.                 var tmp13:Number = tmp0 - tmp3;
  274.                 var tmp11:Number = tmp1 + tmp2;
  275.                 var tmp12:Number = tmp1 - tmp2;
  276.    
  277.                 data[int(dataOff)] = tmp10 + tmp11; /* phase 3 */
  278.                 data[int(dataOff+4)] = tmp10 - tmp11;
  279.    
  280.                 var z1:Number = (tmp12 + tmp13) * 0.707106781; /* c4 */
  281.                 data[int(dataOff+2)] = tmp13 + z1; /* phase 5 */
  282.                 data[int(dataOff+6)] = tmp13 - z1;
  283.    
  284.                 /* Odd part */
  285.                 tmp10 = tmp4 + tmp5; /* phase 2 */
  286.                 tmp11 = tmp5 + tmp6;
  287.                 tmp12 = tmp6 + tmp7;
  288.    
  289.                 /* The rotator is modified from fig 4-8 to avoid extra negations. */
  290.                 var z5:Number = (tmp10 - tmp12) * 0.382683433; /* c6 */
  291.                 var z2:Number = 0.541196100 * tmp10 + z5; /* c2-c6 */
  292.                 var z4:Number = 1.306562965 * tmp12 + z5; /* c2+c6 */
  293.                 var z3:Number = tmp11 * 0.707106781; /* c4 */
  294.    
  295.                 var z11:Number = tmp7 + z3; /* phase 5 */
  296.                 var z13:Number = tmp7 - z3;
  297.    
  298.                 data[int(dataOff+5)] = z13 + z2;    /* phase 6 */
  299.                 data[int(dataOff+3)] = z13 - z2;
  300.                 data[int(dataOff+1)] = z11 + z4;
  301.                 data[int(dataOff+7)] = z11 - z4;
  302.    
  303.                 dataOff += 8; /* advance pointer to next row */
  304.             }
  305.    
  306.             /* Pass 2: process columns. */
  307.             dataOff = 0;
  308.             for (i=0; i<I8; ++i)
  309.             {
  310.                 d0 = data[int(dataOff)];
  311.                 d1 = data[int(dataOff + 8)];
  312.                 d2 = data[int(dataOff + 16)];
  313.                 d3 = data[int(dataOff + 24)];
  314.                 d4 = data[int(dataOff + 32)];
  315.                 d5 = data[int(dataOff + 40)];
  316.                 d6 = data[int(dataOff + 48)];
  317.                 d7 = data[int(dataOff + 56)];
  318.                
  319.                 var tmp0p2:Number = d0 + d7;
  320.                 var tmp7p2:Number = d0 - d7;
  321.                 var tmp1p2:Number = d1 + d6;
  322.                 var tmp6p2:Number = d1 - d6;
  323.                 var tmp2p2:Number = d2 + d5;
  324.                 var tmp5p2:Number = d2 - d5;
  325.                 var tmp3p2:Number = d3 + d4;
  326.                 var tmp4p2:Number = d3 - d4;
  327.    
  328.                 /* Even part */
  329.                 var tmp10p2:Number = tmp0p2 + tmp3p2;   /* phase 2 */
  330.                 var tmp13p2:Number = tmp0p2 - tmp3p2;
  331.                 var tmp11p2:Number = tmp1p2 + tmp2p2;
  332.                 var tmp12p2:Number = tmp1p2 - tmp2p2;
  333.    
  334.                 data[int(dataOff)] = tmp10p2 + tmp11p2; /* phase 3 */
  335.                 data[int(dataOff+32)] = tmp10p2 - tmp11p2;
  336.    
  337.                 var z1p2:Number = (tmp12p2 + tmp13p2) * 0.707106781; /* c4 */
  338.                 data[int(dataOff+16)] = tmp13p2 + z1p2; /* phase 5 */
  339.                 data[int(dataOff+48)] = tmp13p2 - z1p2;
  340.    
  341.                 /* Odd part */
  342.                 tmp10p2 = tmp4p2 + tmp5p2; /* phase 2 */
  343.                 tmp11p2 = tmp5p2 + tmp6p2;
  344.                 tmp12p2 = tmp6p2 + tmp7p2;
  345.    
  346.                 /* The rotator is modified from fig 4-8 to avoid extra negations. */
  347.                 var z5p2:Number = (tmp10p2 - tmp12p2) * 0.382683433; /* c6 */
  348.                 var z2p2:Number = 0.541196100 * tmp10p2 + z5p2; /* c2-c6 */
  349.                 var z4p2:Number = 1.306562965 * tmp12p2 + z5p2; /* c2+c6 */
  350.                 var z3p2:Number= tmp11p2 * 0.707106781; /* c4 */
  351.    
  352.                 var z11p2:Number = tmp7p2 + z3p2;   /* phase 5 */
  353.                 var z13p2:Number = tmp7p2 - z3p2;
  354.    
  355.                 data[int(dataOff+40)] = z13p2 + z2p2; /* phase 6 */
  356.                 data[int(dataOff+24)] = z13p2 - z2p2;
  357.                 data[int(dataOff+ 8)] = z11p2 + z4p2;
  358.                 data[int(dataOff+56)] = z11p2 - z4p2;
  359.    
  360.                 dataOff++; /* advance pointer to next column */
  361.             }
  362.    
  363.             // Quantize/descale the coefficients
  364.             var fDCTQuant:Number;
  365.             for (i=0; i<I64; ++i)
  366.             {
  367.                 // Apply the quantization and scaling factor & Round to nearest integer
  368.                 fDCTQuant = data[int(i)]*fdtbl[int(i)];
  369.                 outputfDCTQuant[int(i)] = (fDCTQuant > 0.0) ? int(fDCTQuant + 0.5) : int(fDCTQuant - 0.5);
  370.             }
  371.             return outputfDCTQuant;
  372.         }
  373.    
  374.         // Chunk writing
  375.         private function writeAPP0():void
  376.         {
  377.             byteout.writeShort(0xFFE0); // marker
  378.             byteout.writeShort(16); // length
  379.             byteout.writeByte(0x4A); // J
  380.             byteout.writeByte(0x46); // F
  381.             byteout.writeByte(0x49); // I
  382.             byteout.writeByte(0x46); // F
  383.             byteout.writeByte(0); // = "JFIF",'\0'
  384.             byteout.writeByte(1); // versionhi
  385.             byteout.writeByte(1); // versionlo
  386.             byteout.writeByte(0); // xyunits
  387.             byteout.writeShort(1); // xdensity
  388.             byteout.writeShort(1); // ydensity
  389.             byteout.writeByte(0); // thumbnwidth
  390.             byteout.writeByte(0); // thumbnheight
  391.         }
  392.    
  393.         private function writeSOF0(width:int, height:int):void
  394.         {
  395.             byteout.writeShort(0xFFC0); // marker
  396.             byteout.writeShort(17);   // length, truecolor YUV JPG
  397.             byteout.writeByte(8);    // precision
  398.             byteout.writeShort(height);
  399.             byteout.writeShort(width);
  400.             byteout.writeByte(3);    // nrofcomponents
  401.             byteout.writeByte(1);    // IdY
  402.             byteout.writeByte(0x11); // HVY
  403.             byteout.writeByte(0);    // QTY
  404.             byteout.writeByte(2);    // IdU
  405.             byteout.writeByte(0x11); // HVU
  406.             byteout.writeByte(1);    // QTU
  407.             byteout.writeByte(3);    // IdV
  408.             byteout.writeByte(0x11); // HVV
  409.             byteout.writeByte(1);    // QTV
  410.         }
  411.    
  412.         private function writeDQT():void
  413.         {
  414.             byteout.writeShort(0xFFDB); // marker
  415.             byteout.writeShort(132);       // length
  416.             byteout.writeByte(0);
  417.            
  418.             var i:int;
  419.             const I64:int = 64;
  420.             for (i=0; i<I64; ++i)
  421.                 byteout.writeByte(YTable[i]);
  422.                
  423.             byteout.writeByte(1);
  424.            
  425.             for (i=0; i<I64; ++i)
  426.                 byteout.writeByte(UVTable[i]);
  427.         }
  428.    
  429.         private function writeDHT():void
  430.         {
  431.             byteout.writeShort(0xFFC4); // marker
  432.             byteout.writeShort(0x01A2); // length
  433.    
  434.             byteout.writeByte(0); // HTYDCinfo
  435.             var i:int;
  436.             const I11:int = 11;
  437.             const I16:int = 16;
  438.             const I161:int = 161;
  439.             for (i=0; i<I16; ++i)
  440.                 byteout.writeByte(std_dc_luminance_nrcodes[int(i+1)]);
  441.  
  442.             for (i=0; i<=I11; ++i)
  443.                 byteout.writeByte(std_dc_luminance_values[i]);
  444.    
  445.             byteout.writeByte(0x10); // HTYACinfo
  446.            
  447.             for (i=0; i<I16; ++i)
  448.                 byteout.writeByte(std_ac_luminance_nrcodes[int(i+1)]);
  449.  
  450.             for (i=0; i<=I161; ++i)
  451.                 byteout.writeByte(std_ac_luminance_values[i]);
  452.  
  453.             byteout.writeByte(1); // HTUDCinfo
  454.            
  455.             for (i=0; i<I16; ++i)
  456.                 byteout.writeByte(std_dc_chrominance_nrcodes[int(i+1)]);
  457.  
  458.             for (i=0; i<=I11; ++i)
  459.                 byteout.writeByte(std_dc_chrominance_values[i]);
  460.  
  461.             byteout.writeByte(0x11); // HTUACinfo
  462.            
  463.             for (i=0; i<I16; ++i)
  464.                 byteout.writeByte(std_ac_chrominance_nrcodes[int(i+1)]);
  465.                
  466.             for (i=0; i<=I161; ++i)
  467.                 byteout.writeByte(std_ac_chrominance_values[i]);
  468.         }
  469.    
  470.         private function writeSOS():void
  471.         {
  472.             byteout.writeShort(0xFFDA); // marker
  473.             byteout.writeShort(12); // length
  474.             byteout.writeByte(3); // nrofcomponents
  475.             byteout.writeByte(1); // IdY
  476.             byteout.writeByte(0); // HTY
  477.             byteout.writeByte(2); // IdU
  478.             byteout.writeByte(0x11); // HTU
  479.             byteout.writeByte(3); // IdV
  480.             byteout.writeByte(0x11); // HTV
  481.             byteout.writeByte(0); // Ss
  482.             byteout.writeByte(0x3f); // Se
  483.             byteout.writeByte(0); // Bf
  484.         }
  485.    
  486.         // Core processing
  487.         internal var DU:Vector.<int> = new Vector.<int>(64, true);
  488.    
  489.         private function processDU(CDU:Vector.<Number>, fdtbl:Vector.<Number>, DC:Number, HTDC:Vector.<BitString>, HTAC:Vector.<BitString>):Number
  490.         {
  491.             var EOB:BitString = HTAC[0x00];
  492.             var M16zeroes:BitString = HTAC[0xF0];
  493.             var pos:int;
  494.             const I16:int = 16;
  495.             const I63:int = 63;
  496.             const I64:int = 64;
  497.             var DU_DCT:Vector.<int> = fDCTQuant(CDU, fdtbl);
  498.             //ZigZag reorder
  499.             for (var j:int=0;j<I64;++j) {
  500.                 DU[ZigZag[j]]=DU_DCT[j];
  501.             }
  502.             var Diff:int = DU[0] - DC; DC = DU[0];
  503.             //Encode DC
  504.             if (Diff==0) {
  505.                 writeBits(HTDC[0]); // Diff might be 0
  506.             } else {
  507.                 pos = int(32767+Diff);
  508.                 writeBits(HTDC[category[pos]]);
  509.                 writeBits(bitcode[pos]);
  510.             }
  511.             //Encode ACs
  512.             const end0pos:int = 63;
  513.             for (; (end0pos>0)&&(DU[end0pos]==0); end0pos--) {};
  514.             //end0pos = first element in reverse order !=0
  515.             if ( end0pos == 0) {
  516.                 writeBits(EOB);
  517.                 return DC;
  518.             }
  519.             var i:int = 1;
  520.             var lng:int;
  521.             while ( i <= end0pos ) {
  522.                 var startpos:int = i;
  523.                 for (; (DU[i]==0) && (i<=end0pos); ++i) {}
  524.                 var nrzeroes:int = i-startpos;
  525.                 if ( nrzeroes >= I16 ) {
  526.                     lng = nrzeroes>>4;
  527.                     for (var nrmarker:int=1; nrmarker <= lng; ++nrmarker)
  528.                         writeBits(M16zeroes);
  529.                     nrzeroes = int(nrzeroes&0xF);
  530.                 }
  531.                 pos = int(32767+DU[i]);
  532.                 writeBits(HTAC[int((nrzeroes<<4)+category[pos])]);
  533.                 writeBits(bitcode[pos]);
  534.                 i++;
  535.             }
  536.             if ( end0pos != I63 ) {
  537.                 writeBits(EOB);
  538.             }
  539.             return DC;
  540.         }
  541.    
  542.         private var YDU:Vector.<Number> = new Vector.<Number>(64, true);
  543.         private var UDU:Vector.<Number> = new Vector.<Number>(64, true);
  544.         private var VDU:Vector.<Number> = new Vector.<Number>(64, true);
  545.    
  546.         private function RGB2YUV(img:BitmapData, xpos:int, ypos:int):void
  547.         {
  548.             var pos:int=0;
  549.             const I8:int = 8;
  550.             for (var y:int=0; y<I8; ++y) {
  551.                 for (var x:int=0; x<I8; ++x) {
  552.                     var P:uint = img.getPixel32(xpos+x,ypos+y);
  553.                     var R:Number = (P>>16)&0xFF;
  554.                     var G:Number = (P>> 8)&0xFF;
  555.                     var B:Number = (P    )&0xFF;
  556.                     YDU[int(pos)]=((( 0.29900)*R+( 0.58700)*G+( 0.11400)*B))-0x80;
  557.                     UDU[int(pos)]=(((-0.16874)*R+(-0.33126)*G+( 0.50000)*B));
  558.                     VDU[int(pos)]=((( 0.50000)*R+(-0.41869)*G+(-0.08131)*B));
  559.                     ++pos;
  560.                 }
  561.             }
  562.         }
  563.        
  564.         public function JPEGEncoderTS(quality:int=50)
  565.         {
  566.             if (quality <= 0)
  567.                 quality = 1;
  568.        
  569.             if (quality > 100)
  570.                 quality = 100;
  571.                
  572.             sf = quality < 50 ? int(5000 / quality) : int(200 - (quality<<1));
  573.             init();
  574.         }
  575.        
  576.         private function init():void
  577.         {
  578.             ZigZag.fixed = true;
  579.             aasf.fixed = true;
  580.             YQT.fixed = true;
  581.             UVQT.fixed = true;
  582.             std_ac_chrominance_nrcodes.fixed = true;
  583.             std_ac_chrominance_values.fixed = true;
  584.             std_ac_luminance_nrcodes.fixed = true;
  585.             std_ac_luminance_values.fixed = true;
  586.             std_dc_chrominance_nrcodes.fixed = true;
  587.             std_dc_chrominance_values.fixed = true;
  588.             std_dc_luminance_nrcodes.fixed = true;
  589.             std_dc_luminance_values.fixed = true;
  590.             // Create tables
  591.             initHuffmanTbl();
  592.             initCategoryNumber();
  593.             initQuantTables(sf);
  594.         }
  595.    
  596.         public function encode(image:BitmapData):ByteArray
  597.         {
  598.             // Initialize bit writer
  599.             byteout = new ByteArray();
  600.            
  601.             bytenew=0;
  602.             bytepos=7;
  603.    
  604.             // Add JPEG headers
  605.             byteout.writeShort(0xFFD8); // SOI
  606.             writeAPP0();
  607.             writeDQT();
  608.             writeSOF0(image.width,image.height);
  609.             writeDHT();
  610.             writeSOS();
  611.            
  612.             // Encode 8x8 macroblocks
  613.             var DCY:Number=0;
  614.             var DCU:Number=0;
  615.             var DCV:Number=0;
  616.             bytenew=0;
  617.             bytepos=7;
  618.            
  619.             var width:int = image.width;
  620.             var height:int = image.height;
  621.            
  622.             for (var ypos:int=0; ypos<height; ypos+=8)
  623.             {
  624.                 for (var xpos:int=0; xpos<width; xpos+=8)
  625.                 {
  626.                     RGB2YUV(image, xpos, ypos);
  627.                     DCY = processDU(YDU, fdtbl_Y, DCY, YDC_HT, YAC_HT);
  628.                     DCU = processDU(UDU, fdtbl_UV, DCU, UVDC_HT, UVAC_HT);
  629.                     DCV = processDU(VDU, fdtbl_UV, DCV, UVDC_HT, UVAC_HT);
  630.                 }
  631.             }
  632.    
  633.             // Do the bit alignment of the EOI marker
  634.             if ( bytepos >= 0 )
  635.             {
  636.                 var fillbits:BitString = new BitString();
  637.                 fillbits.len = bytepos+1;
  638.                 fillbits.val = (1<<(bytepos+1))-1;
  639.                 writeBits(fillbits);
  640.             }
  641.             byteout.writeShort(0xFFD9); //EOI
  642.             return byteout;
  643.         }
  644.     }
  645. }
  646.  
  647. final class BitString
  648. {
  649.     public var len:int = 0;
  650.     public var val:int = 0;
  651. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement