http://bbs.9ria.com/viewthread.php?tid=96796&extra=page%3D1%26amp%3Borderby%3Ddateline%26amp%3Bfilter%3D2592000

分享下处理bmp图片的as, 网上看到的拿来了
package hy.com
{
import flash.display.BitmapData;
import flash.errors.IOError;
import flash.utils.ByteArray;
import flash.utils.Endian;

public class BMPDecoder {
//___________________________________________________________ const
private const BITMAP_HEADER_TYPE:String = "BM";
private const BITMAP_FILE_HEADER_SIZE:int = 14;
private const BITMAP_CORE_HEADER_SIZE:int = 12;
private const BITMAP_INFO_HEADER_SIZE:int = 40;
private const COMP_RGB :int = 0;
private const COMP_RLE8 :int = 1;
private const COMP_RLE4 :int = 2;
private const COMP_BITFIELDS:int = 3;
private const BIT1 :int = 1;
private const BIT4 :int = 4;
private const BIT8 :int = 8;
private const BIT16:int = 16;
private const BIT24:int = 24;
private const BIT32:int = 32;
//___________________________________________________________ vars
private var bytes:ByteArray;
private var palette:Array;
private var bd:BitmapData;
private var nFileSize:uint;
private var nReserved1:uint;
private var nReserved2:uint;
private var nOffbits:uint;
private var nInfoSize:uint;
private var nWidth:int;
private var nHeight:int;
private var nPlains:uint;
private var nBitsPerPixel:uint;
private var nCompression:uint;
private var nSizeImage:uint;
private var nXPixPerMeter:int;
private var nYPixPerMeter:int;
private var nColorUsed:uint;
private var nColorImportant:uint;
private var nRMask:uint;
private var nGMask:uint;
private var nBMask:uint;
private var nRPos:uint;
private var nGPos:uint;
private var nBPos:uint;
private var nRMax:uint;
private var nGMax:uint;
private var nBMax:uint;

public function BMPDecoder(){
nRPos = 0;
nGPos = 0;
nBPos = 0;
}

/**
* decoder
*
* @param BMP file ByteArray
*/
public function decode( data:ByteArray ):BitmapData {
bytes = data;
bytes.endian = Endian.LITTLE_ENDIAN;
bytes.position = 0;
readFileHeader();
nInfoSize = bytes.readUnsignedInt();
switch ( nInfoSize ) {
case BITMAP_CORE_HEADER_SIZE:
readCoreHeader();
break;
case BITMAP_INFO_HEADER_SIZE:
readInfoHeader();
break;
default:
readExtendedInfoHeader();
break;
}
bd = new BitmapData( nWidth, nHeight );
switch ( nBitsPerPixel ){
case BIT1:
readColorPalette();
decode1BitBMP();
break;
case BIT4:
readColorPalette();
if ( nCompression == COMP_RLE4 ){
decode4bitRLE();
} else {
decode4BitBMP();
}
break;
case BIT8:
readColorPalette();
if ( nCompression == COMP_RLE8 ){
decode8BitRLE();
} else {
decode8BitBMP();
}
break;
case BIT16:
readBitFields();
checkColorMask();
decode16BitBMP();
break;
case BIT24:
decode24BitBMP();
break;
case BIT32:
readBitFields();
checkColorMask();
decode32BitBMP();
break;
default:
throw new VerifyError("invalid bits per pixel : " + nBitsPerPixel );
}
return bd;
}
/**
* read BITMAP FILE HEADER
*/
private function readFileHeader():void {
var fileHeader:ByteArray = new ByteArray();
fileHeader.endian = Endian.LITTLE_ENDIAN;
try {
bytes.readBytes( fileHeader, 0, BITMAP_FILE_HEADER_SIZE );
if ( fileHeader.readUTFBytes( 2 ) != BITMAP_HEADER_TYPE ){
throw new VerifyError("invalid bitmap header type");
}
nFileSize = fileHeader.readUnsignedInt();
nReserved1 = fileHeader.readUnsignedShort();
nReserved2 = fileHeader.readUnsignedShort();
nOffbits = fileHeader.readUnsignedInt();
} catch ( e:IOError ) {
throw new VerifyError("invalid file header");
}
}
/**
* read BITMAP CORE HEADER
*/
private function readCoreHeader():void {
var coreHeader:ByteArray = new ByteArray();
coreHeader.endian = Endian.LITTLE_ENDIAN;
try {
bytes.readBytes( coreHeader, 0, BITMAP_CORE_HEADER_SIZE - 4 );
nWidth = coreHeader.readShort();
nHeight = coreHeader.readShort();
nPlains = coreHeader.readUnsignedShort();
nBitsPerPixel = coreHeader.readUnsignedShort();
} catch ( e:IOError ) {
throw new VerifyError("invalid core header");
}
}
/**
* read BITMAP INFO HEADER
*/
private function readInfoHeader():void {
var infoHeader:ByteArray = new ByteArray();
infoHeader.endian = Endian.LITTLE_ENDIAN;
try {
bytes.readBytes( infoHeader, 0, BITMAP_INFO_HEADER_SIZE - 4 );
nWidth = infoHeader.readInt();
nHeight = infoHeader.readInt();
nPlains = infoHeader.readUnsignedShort();
nBitsPerPixel = infoHeader.readUnsignedShort();
nCompression = infoHeader.readUnsignedInt();
nSizeImage = infoHeader.readUnsignedInt();
nXPixPerMeter = infoHeader.readInt();
nYPixPerMeter = infoHeader.readInt();
nColorUsed = infoHeader.readUnsignedInt();
nColorImportant = infoHeader.readUnsignedInt();
} catch ( e:IOError ) {
throw new VerifyError("invalid info header");
}
}
/**
* read the extend info of BITMAP INFO HEADER
*/
private function readExtendedInfoHeader():void {
var infoHeader:ByteArray = new ByteArray();
infoHeader.endian = Endian.LITTLE_ENDIAN;
try {
bytes.readBytes( infoHeader, 0, nInfoSize - 4 );
nWidth = infoHeader.readInt();
nHeight = infoHeader.readInt();
nPlains = infoHeader.readUnsignedShort();
nBitsPerPixel = infoHeader.readUnsignedShort();
nCompression = infoHeader.readUnsignedInt();
nSizeImage = infoHeader.readUnsignedInt();
nXPixPerMeter = infoHeader.readInt();
nYPixPerMeter = infoHeader.readInt();
nColorUsed = infoHeader.readUnsignedInt();
nColorImportant = infoHeader.readUnsignedInt();
if ( infoHeader.bytesAvailable >= 4 ) nRMask = infoHeader.readUnsignedInt();
if ( infoHeader.bytesAvailable >= 4 ) nGMask = infoHeader.readUnsignedInt();
if ( infoHeader.bytesAvailable >= 4 ) nBMask = infoHeader.readUnsignedInt();
} catch ( e:IOError ) {
throw new VerifyError("invalid info header");
}
}
/**
* read bitfields
*/
private function readBitFields():void {
if ( nCompression == COMP_RGB ){
if ( nBitsPerPixel == BIT16 ){
// RGB555
nRMask = 0x00007c00;
nGMask = 0x000003e0;
nBMask = 0x0000001f;
} else {
//RGB888;
nRMask = 0x00ff0000;
nGMask = 0x0000ff00;
nBMask = 0x000000ff;
}
} else if ( ( nCompression == COMP_BITFIELDS ) && ( nInfoSize < 52 ) ){
try {
nRMask = bytes.readUnsignedInt();
nGMask = bytes.readUnsignedInt();
nBMask = bytes.readUnsignedInt();
} catch ( e:IOError ) {
throw new VerifyError("invalid bit fields");
}
}
}
/**
* read color palette
*/
private function readColorPalette():void {
var i:int;
var len:int = ( nColorUsed > 0 ) ? nColorUsed : Math.pow( 2, nBitsPerPixel );
palette = new Array( len );
for ( i = 0; i < len; ++i ){
palette[ i ] = bytes.readUnsignedInt();
}
}
/**
* decode 1 bit BMP
*/
private function decode1BitBMP():void {
var x:int;
var y:int;
var i:int;
var col:int;
var buf:ByteArray = new ByteArray();
var line:int = nWidth / 8;
if ( line % 4 > 0 ){
line = ( ( line / 4 | 0 ) + 1 ) * 4;
}
try {
for ( y = nHeight - 1; y >= 0; --y ){
buf.length = 0;
bytes.readBytes( buf, 0, line );
for ( x = 0; x < nWidth; x += 8 ){
col = buf.readUnsignedByte();
for ( i = 0; i < 8; ++i ){
bd.setPixel( x + i, y, palette[ col >> ( 7 - i ) & 0x01 ] );
}
}
}
} catch ( e:IOError ) {
throw new VerifyError("invalid image data");
}
}
/**
* decode 4bit RLE
*/
private function decode4bitRLE():void {
var x:int;
var y:int;
var i:int;
var n:int;
var col:int;
var data:uint;
var buf:ByteArray = new ByteArray();
try {
for ( y = nHeight - 1; y >= 0; --y ){
buf.length = 0;
while ( bytes.bytesAvailable > 0 ){
n = bytes.readUnsignedByte();
if ( n > 0 ){
// encode data
data = bytes.readUnsignedByte();
for ( i = 0; i < n/2; ++i ){
buf.writeByte( data );
}
} else {
n = bytes.readUnsignedByte();
if ( n > 0 ){
// abs mode
bytes.readBytes( buf, buf.length, n/2 );
buf.position += n/2;
if ( n/2 + 1 >> 1 << 1 != n/2 ){
bytes.readUnsignedByte();
}
} else {
// EOL
break;
}
}
}
buf.position = 0;
for ( x = 0; x < nWidth; x += 2 ){
col = buf.readUnsignedByte();
bd.setPixel( x, y, palette[ col >> 4 ] );
bd.setPixel( x + 1, y, palette[ col & 0x0f ] );
}
}
} catch ( e:IOError ) {
throw new VerifyError("invalid image data");
}
}
/**
* decode 4bit (no Compression)
*/
private function decode4BitBMP():void {
var x:int;
var y:int;
var i:int;
var col:int;
var buf:ByteArray = new ByteArray();
var line:int = nWidth / 2;
if ( line % 4 > 0 ){
line = ( ( line / 4 | 0 ) + 1 ) * 4;
}
try {
for ( y = nHeight - 1; y >= 0; --y ){
buf.length = 0;
bytes.readBytes( buf, 0, line );
for ( x = 0; x < nWidth; x += 2 ){
col = buf.readUnsignedByte();
bd.setPixel( x, y, palette[ col >> 4 ] );
bd.setPixel( x + 1, y, palette[ col & 0x0f ] );
}
}
} catch ( e:IOError ) {
throw new VerifyError("invalid image data");
}
}
/**
* decode 8bit( RLE Compression )
*/
private function decode8BitRLE():void {
var x:int;
var y:int;
var i:int;
var n:int;
var col:int;
var data:uint;
var buf:ByteArray = new ByteArray();
try {
for ( y = nHeight - 1; y >= 0; --y ){
buf.length = 0;
while ( bytes.bytesAvailable > 0 ){
n = bytes.readUnsignedByte();
if ( n > 0 ){
// encode data
data = bytes.readUnsignedByte();
for ( i = 0; i < n; ++i ){
buf.writeByte( data );
}
} else {
n = bytes.readUnsignedByte();
if ( n > 0 ){
// abs mode data
bytes.readBytes( buf, buf.length, n );
buf.position += n;
if ( n + 1 >> 1 << 1 != n ){
bytes.readUnsignedByte();
}
} else {
// EOL
break;
}
}
}
buf.position = 0;
for ( x = 0; x < nWidth; ++x ){
bd.setPixel( x, y, palette[ buf.readUnsignedByte() ] );
}
}
} catch ( e:IOError ) {
throw new VerifyError("invalid image data");
}
}
/**
* decode 8bit(no Compression)
*/
private function decode8BitBMP():void {
var x:int;
var y:int;
var i:int;
var col:int;
var buf:ByteArray = new ByteArray();
var line:int = nWidth;
if ( line % 4 > 0 ){
line = ( ( line / 4 | 0 ) + 1 ) * 4;
}
try {
for ( y = nHeight - 1; y >= 0; --y ){
buf.length = 0;
bytes.readBytes( buf, 0, line );
for ( x = 0; x < nWidth; ++x ){
bd.setPixel( x, y, palette[ buf.readUnsignedByte() ] );
}
}
} catch ( e:IOError ) {
throw new VerifyError("invalid image data");
}
}
/**
* decode 16bit
*/
private function decode16BitBMP():void {
var x:int;
var y:int;
var col:int;
try {
for ( y = nHeight - 1; y >= 0; --y ){
for ( x = 0; x < nWidth; ++x ){
col = bytes.readUnsignedShort();
bd.setPixel( x, y, ( ( ( col & nRMask ) >> nRPos )*0xff/nRMax << 16 ) + ( ( ( col & nGMask ) >> nGPos )*0xff/nGMax << 8 ) + ( ( ( col & nBMask ) >> nBPos )*0xff/nBMax << 0 ) );
}
}
} catch ( e:IOError ) {
throw new VerifyError("invalid image data");
}
}
/**
* decode 24bit BMP
*/
private function decode24BitBMP():void {
var x:int;
var y:int;
var col:int;
var buf:ByteArray = new ByteArray();
var line:int = nWidth * 3;
if ( line % 4 > 0 ){
line = ( ( line / 4 | 0 ) + 1 ) * 4;
}
try {
for ( y = nHeight - 1; y >= 0; --y ){
buf.length = 0;
bytes.readBytes( buf, 0, line );
for ( x = 0; x < nWidth; ++x ){
bd.setPixel( x, y, buf.readUnsignedByte() + ( buf.readUnsignedByte() << 8 ) + ( buf.readUnsignedByte() << 16 ) );
}
}
} catch ( e:IOError ) {
throw new VerifyError("invalid image data");
}
}
/**
* decode 32bit BMP
*/
private function decode32BitBMP():void {
var x:int;
var y:int;
var col:int;
try {
for ( y = nHeight - 1; y >= 0; --y ){
for ( x = 0; x < nWidth; ++x ){
col = bytes.readUnsignedInt();
bd.setPixel( x, y, ( ( ( col & nRMask ) >> nRPos )*0xff/nRMax << 16 ) + ( ( ( col & nGMask ) >> nGPos )*0xff/nGMax << 8 ) + ( ( ( col & nBMask ) >> nBPos )*0xff/nBMax << 0 ) );
}
}
} catch ( e:IOError ) {
throw new VerifyError("invalid image data");
}
}
/**
* check color mask
*/
private function checkColorMask():void {
if ( ( nRMask & nGMask ) | ( nGMask & nBMask ) | ( nBMask & nRMask ) ){
throw new VerifyError("invalid bit fields");
}
while ( ( ( nRMask >> nRPos ) & 0x00000001 ) == 0 ){
nRPos++;
}
while ( ( ( nGMask >> nGPos ) & 0x00000001 ) == 0 ){
nGPos++;
}
while ( ( ( nBMask >> nBPos ) & 0x00000001 ) == 0 ){
nBPos++;
}
nRMax = nRMask >> nRPos;
nGMax = nGMask >> nGPos;
nBMax = nBMask >> nBPos;
}
/**
* print information
*/
public function traceInfo():void {
trace("---- FILE HEADER ----");
trace("nFileSize: " + nFileSize );
trace("nReserved1: " + nReserved1 );
trace("nReserved2: " + nReserved2 );
trace("nOffbits: " + nOffbits );
trace("---- INFO HEADER ----");
trace("nWidth: " + nWidth );
trace("nHeight: " + nHeight );
trace("nPlains: " + nPlains );
trace("nBitsPerPixel: " + nBitsPerPixel );
if ( nInfoSize >= 40 ){
trace("nCompression: " + nCompression );
trace("nSizeImage: " + nSizeImage );
trace("nXPixPerMeter: " + nXPixPerMeter );
trace("nYPixPerMeter: " + nYPixPerMeter );
trace("nColorUsed: " + nColorUsed );
trace("nColorUsed: " + nColorImportant );
}
if ( nInfoSize >= 52 ){
trace("nRMask: " + nRMask.toString( 2 ) );
trace("nGMask: " + nGMask.toString( 2 ) );
trace("nBMask: " + nBMask.toString( 2 ) );
}
}
}
}

[转]Flex 处理bmp图片as相关推荐

  1. bmp文件头_「正点原子FPGA连载」第十九章SD卡读BMP图片LCD显示

    1)摘自[正点原子]领航者 ZYNQ 之嵌入式开发指南 2)实验平台:正点原子领航者ZYNQ开发板 3)平台购买地址:https://item.taobao.com/item.htm?&id= ...

  2. python运行完不能显示图_【已解决】Python中通过Image的open之后,去show结果打不开bmp图片,无法正常显示图片...

    [问题] 在windows的cmd命令行下,使用Python的PIL库打开并显示一个jpg图片:openedImg = Image.open(saveToFile); print "open ...

  3. BMP格式知识之二:16位,24位,32位的BMP图片算法是如何运算的

    BMP格式知识之二:16位,24位,32位的BMP图片算法是如何运算的 原文:http://blog.csdn.net/qq445803843/article/details/46476433 这段代 ...

  4. BMP图片的解析,关于压缩方式

    在做一个显示bmp图片到lcd屏的时候,发现有些bmp图显示不对. 同样是16bit bmp却有差异. 就查了一下格式. bmp文件格式 位图文件的组成 结构名称 符号 位图文件头(bitmap-fi ...

  5. framebuffer 保存 bmp图片格式

    最近需要完成一个从framebuffer中进行读取,然后将内存的东西保存为bmp图片格式,我的其他博客内容对framebuffer进行详细的讲解,以及bmp的格式进行详细的讲解. 之前从网上看到了一些 ...

  6. java bmp_JAVA实现对BMP图片的读取

    BMP图片格式,是windows自带的一个图片格式,(*bmp),在windows的系统下都支持这种格式,bmp格式与设备无关的位图(DIB)格式,BMP简称位图,BMP的原始数据是没有经过压缩处理的 ...

  7. [译] APT分析报告:10.Lazarus以ThreatNeedle家族攻击工业事件还原(BMP图片隐藏RAT)

    这是作者新开的一个专栏,主要翻译国外知名安全厂商的APT报告,了解它们的安全技术,学习它们溯源APT组织和恶意代码分析的方法,希望对您有所帮助.当然,由于作者英语有限,会借助机翻进行校验,还请包涵!前 ...

  8. MFC 对话框Picture Control(图片控件)中静态和动态显示Bmp图片

    最近有同学问我如何实现MFC基于对话框在图片控件中加载图片?其实使用MFC显示图片的方法各种各样,但是还是有些同学不知道怎样显示.以前在<数字图像处理>课程中完成的软件都是基于单文档的程序 ...

  9. 使用C++实现多张BMP图片转换为YUV动画----附加淡入淡出转场(逐渐变明变暗),及垂直滑像转场(逐行渐变)

    使用C++实现多张BMP图片转换为YUV动画----附加淡入淡出转场(逐渐变明变暗),及垂直滑像转场(逐行渐变) 一.BMP图像简介 1.BMP图像是什么? 2.BMP图像文件结构 1)图象文件头 2 ...

最新文章

  1. webpack 配置 babel
  2. Windows Phone 7第一次亲密接触
  3. 深度解析 H.265 视频解决方案
  4. php 自定义格式化,PHP自定义函数格式化json数据示例
  5. 一文详解枚举器和迭代器!
  6. zemax---透镜基础篇
  7. Error: Packagesfrx7 and VCLDB all Contains DBGrids
  8. 【MySQL 8.0】导入 .frm .MYD .MYI
  9. whisper数据库
  10. ERROR: configuration failed for package ‘stringi’
  11. C语言hannoi汉诺塔
  12. Activiti,自定义表单,外置表单,工作流,微服务,子系统
  13. java基础-1 基础知识
  14. c语言中int 3.6 =,(C语言中的常量)乐创DIY C语言讲义​——3.6节
  15. android导航地图,地图导航-Android平台-开发指南-高德地图车机版 | 高德地图API
  16. java 应用 知乎_GitHub - liangsonghua/zhihu: JAVA仿知乎与redis应用
  17. 安卓开发资料大集合,很多都是51CTO中的推荐材料,值得学习
  18. 每日思考第 68 期:如何致富,不靠运气2
  19. SpaceSniffer 下载与使用——扫描磁盘空间利器
  20. openwrt开启smb共享

热门文章

  1. Linux下基于GTK人脸识别界面设计
  2. vue项目开发心得和一些最佳实践
  3. ActionEnglish Notes
  4. C语言static关键字的作用(有三个作用)
  5. 解决蓝牙鼠标和电脑连接出现卡顿的情况
  6. OOM系列之一:java.lang.OutOfMemoryError: Java堆空间问题详解
  7. LTE学习笔记 ——PLMN选择
  8. Python VTK计算曲面的高斯曲率和平均曲率
  9. ESP8266学习之路 十二 (读写文件)
  10. 顾往前行,我的前端之路系列(二)