Guest User

Untitled

a guest
Nov 22nd, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. package{
  2. import flash.display.BitmapData;
  3. import flash.display.JPEGEncoderOptions;
  4. import flash.display.PNGEncoderOptions;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. import flash.events.MouseEvent;
  8. import flash.geom.Rectangle;
  9. import flash.net.FileReference;
  10. import flash.text.TextField;
  11. import flash.text.TextFieldAutoSize;
  12. import flash.text.TextFieldType;
  13. import flash.utils.ByteArray;
  14. import flash.utils.Endian;
  15.  
  16. /**
  17. * ...
  18. * @author www0z0k
  19. */
  20. public class Main extends Sprite {
  21. private var _tf:TextField = new TextField();
  22. public function Main() {
  23. if (stage) init();
  24. else addEventListener(Event.ADDED_TO_STAGE, init);
  25. }
  26.  
  27. private function init(e:Event = null):void {
  28. removeEventListener(Event.ADDED_TO_STAGE, init);
  29. // entry point
  30. addChild(_tf);
  31. _tf.autoSize = TextFieldAutoSize.LEFT;
  32. _tf.multiline = true;
  33. _tf.type = TextFieldType.INPUT;
  34. var btn:Sprite = new Sprite();
  35. btn.graphics.beginFill(0x0000ff);
  36. btn.graphics.drawRect(0, 0, 100, 40);
  37. addChild(btn);
  38. btn.y = 400;
  39. btn.buttonMode = true;
  40. btn.addEventListener(MouseEvent.CLICK, draw);
  41. }
  42.  
  43. private function draw(e:MouseEvent):void {
  44. var bmpd:BitmapData = new BitmapData(_tf.width, _tf.height);
  45. bmpd.draw(_tf);
  46. encode(bmpd);
  47. }
  48.  
  49.  
  50.  
  51. /*
  52. * Create a 8 bit BMP image as bytearray, with 256 color ( grayscale ).
  53. *
  54. */
  55. private static function encode( bitmapData:BitmapData ):void {
  56. // bit depth configuration
  57. var bytesPerPixel:int = 1;
  58. var bitDepth:int = 8;
  59.  
  60. // image/file properties
  61. var bmpWidth:int = bitmapData.width;
  62. var bmpHeight:int = bitmapData.height;
  63. var imageBytes:ByteArray = bitmapData.getPixels( bitmapData.rect );
  64.  
  65. /* Image from Preview size */
  66. var imageSize:int = bmpWidth * bmpHeight * bytesPerPixel;
  67.  
  68. /* Image offset */
  69. var imageDataOffset:int = 0x436;
  70.  
  71. /* File size */
  72. var fileSize:int = imageSize + imageDataOffset;
  73.  
  74. // binary BMP data
  75. var bmpBytes:ByteArray = new ByteArray();
  76. bmpBytes.endian = Endian.LITTLE_ENDIAN; // byte order
  77.  
  78. // header information
  79. bmpBytes.length = fileSize;
  80. bmpBytes.writeByte(0x42); // B //0
  81. bmpBytes.writeByte(0x4D); // M (BMP identifier) //1
  82. bmpBytes.writeInt(fileSize); // file size //2
  83. bmpBytes.position = 0x0A; // offset to image data
  84. bmpBytes.writeInt( imageDataOffset ); //10 4 Bytes
  85. bmpBytes.writeInt(0x28); // header size //14 4 Bytes
  86. bmpBytes.position = 0x12; // width, height
  87. bmpBytes.writeInt( bmpWidth ); //18 4 Bytes
  88. bmpBytes.writeInt( bmpHeight ); //22 4 Bytes
  89. bmpBytes.writeShort( 1 ); // planes (1) //26 2 Bytes
  90. bmpBytes.writeShort( bitDepth ); // color depth //28 2 Bytes
  91. bmpBytes.writeInt( 0 ); // compression type //30 4 Bytes
  92. bmpBytes.writeInt( imageSize ); // image data size //34 4 Bytes
  93.  
  94. bmpBytes.writeInt( 0x2E23 ); // Horizontal resolution //38 4 Bytes
  95. bmpBytes.writeInt( 0x2E23 ); // Vertical resolution //42 4 Bytes
  96.  
  97. bmpBytes.writeInt( 0x100 ); // Color in the palette
  98.  
  99. bmpBytes.position = 0x36; // start of color table
  100.  
  101. /* COLOR TABLE */
  102. var table:uint = 256 * 4;
  103. for (var i:uint = 0; i < table; i++) {
  104. bmpBytes.writeByte( i ); //B
  105. bmpBytes.writeByte( i ); //G
  106. bmpBytes.writeByte( i ); //R
  107. bmpBytes.writeByte( 0 ); //A
  108. /*
  109. * Grays are made of equal bytes, for example: #AAAAAA is gray.
  110. */
  111. }
  112.  
  113. bmpBytes.position = imageDataOffset; // start of image data... byte 310 // 1078
  114.  
  115. // write pixel bytes in upside-down order
  116. // ( as per BMP format )
  117. var col:int = bmpWidth;
  118. var row:int = bmpHeight;
  119. var rowLength:int = col * bytesPerPixel; // Bytes per column based on Bit depth
  120.  
  121. // Writing bytes to new image vars
  122. var writingOffset:int = 4 - ( bitDepth / 8 );
  123.  
  124. try {
  125. // make sure we're starting at the
  126. // beginning of the image data
  127. imageBytes.position = 0;
  128.  
  129. // Tmp ByteArray to extract 32 bits per pixel
  130. var tmpBytes:ByteArray;
  131.  
  132. // bottom row up
  133. while (row--) {
  134. /* hey += "LINE\n"; */
  135.  
  136. // from end of file up to imageDataOffset
  137. tmpBytes = new ByteArray();
  138. bmpBytes.position = imageDataOffset + ( row * rowLength );
  139.  
  140. // read through each column writing
  141. // those bits to the image in normal
  142. // left to rightorder
  143. col = bmpWidth;
  144. while (col--) {
  145. // Extracting the 32 bits corresponding
  146. // to a pixel per getPixels method ( always the same ).
  147. imageBytes.readBytes( tmpBytes, 0, 4 );
  148.  
  149. // We just need one BYTE of the 4 that are in this array.
  150. tmpBytes.position = 3;
  151.  
  152. // THIS IS THE INDEX ON OUR COLOR TABLE ( GRAYSCALE ).
  153. bmpBytes.writeByte( tmpBytes.readUnsignedByte() );
  154. }
  155. }
  156. } catch(error:Error) {
  157. // end of file
  158. trace( error.toString(), "I/O BMP ERROR" );
  159. }
  160.  
  161. // return BMP file
  162. //return bmpBytes;
  163. new FileReference().save(bmpBytes, '1.bmp');
  164. }
  165.  
  166. }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment