Advertisement
davidqhogan

5%ish faster as3 JPEGEncoder

Sep 11th, 2011
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         public function encode(image:BitmapData):ByteArray
  2.         {
  3.             // Initialize bit writer
  4.             byteout = new ByteArray();
  5.            
  6.             bytenew=0;
  7.             bytepos=7;
  8.    
  9.             // Add JPEG headers
  10.             byteout.writeShort(0xFFD8); // SOI
  11.             writeAPP0();
  12.             writeDQT();
  13.             writeSOF0(image.width,image.height);
  14.             writeDHT();
  15.             writeSOS();
  16.            
  17.             // Encode 8x8 macroblocks
  18.             var DCY:Number=0;
  19.             var DCU:Number=0;
  20.             var DCV:Number=0;
  21.             bytenew=0;
  22.             bytepos=7;
  23.            
  24.             var width:int = image.width;
  25.             var height:int = image.height;
  26.            
  27.             var pixels:Vector.<uint> = image.getVector(image.rect);
  28.            
  29.             for (var ypos:int=0; ypos<height; ypos+=8)
  30.             {
  31.                 for (var xpos:int=0; xpos<width; xpos+=8)
  32.                 {
  33.                     var pos:int=0;
  34.                     const I8:int = 8;
  35.                     for (var y:int=0; y<I8; ++y) {
  36.                         var pixelRowIndex:int = ((ypos + y) * width);
  37.                         for (var x:int=0; x<I8; ++x) {
  38.                            
  39.                             //var P:uint = image.getPixel32(xpos+x,ypos+y);
  40.                             var P:uint = pixels[pixelRowIndex + xpos + x];
  41.                             var R:int = (P>>16)&0xFF;
  42.                             var G:int = (P>> 8)&0xFF;
  43.                             var B:int = (P    )&0xFF;
  44.                            
  45.                             YDU[int(pos)]=((( 0.29900)*R+( 0.58700)*G+( 0.11400)*B))-0x80;
  46.                             UDU[int(pos)]=(((-0.16874)*R+(-0.33126)*G+( 0.50000)*B));
  47.                             VDU[int(pos)]=((( 0.50000)*R+(-0.41869)*G+(-0.08131)*B));
  48.                             ++pos;
  49.                         }
  50.                     }
  51.            
  52.                     DCY = processDU(YDU, fdtbl_Y, DCY, YDC_HT, YAC_HT);
  53.                     DCU = processDU(UDU, fdtbl_UV, DCU, UVDC_HT, UVAC_HT);
  54.                     DCV = processDU(VDU, fdtbl_UV, DCV, UVDC_HT, UVAC_HT);
  55.                 }
  56.             }
  57.    
  58.             // Do the bit alignment of the EOI marker
  59.             if ( bytepos >= 0 )
  60.             {
  61.                 var fillbits:BitString = new BitString();
  62.                 fillbits.len = bytepos+1;
  63.                 fillbits.val = (1<<(bytepos+1))-1;
  64.                 writeBits(fillbits);
  65.             }
  66.             byteout.writeShort(0xFFD9); //EOI
  67.             return byteout;
  68.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement