Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- var pixels:Vector.<uint> = image.getVector(image.rect);
- for (var ypos:int=0; ypos<height; ypos+=8)
- {
- for (var xpos:int=0; xpos<width; xpos+=8)
- {
- var pos:int=0;
- const I8:int = 8;
- for (var y:int=0; y<I8; ++y) {
- var pixelRowIndex:int = ((ypos + y) * width);
- for (var x:int=0; x<I8; ++x) {
- //var P:uint = image.getPixel32(xpos+x,ypos+y);
- var P:uint = pixels[pixelRowIndex + xpos + x];
- var R:int = (P>>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;
- }
- }
- DCY = processDU(YDU, fdtbl_Y, DCY, YDC_HT, YAC_HT);
- DCU = processDU(UDU, fdtbl_UV, DCU, UVDC_HT, UVAC_HT);
- DCV = processDU(VDU, fdtbl_UV, DCV, UVDC_HT, UVAC_HT);
- }
- }
- // 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);
- }
- byteout.writeShort(0xFFD9); //EOI
- return byteout;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement