bitmap解码
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #define BYTE unsigned char
#define WORD short
#define DWORD long #define SUCCESS 0
#define FAILURE 0xFFFFFFFF
#define VOID void #define CHECK_NULL_POINT(x) { if(NULL == (x)) return FAILURE;}
#define CHECK_NULL_RETURN(x) { if(0 == (x)) return FAILURE; }
#define CHECK_FALSE_RETURN(x) { if(!(x)) return FAILURE; }
#define CHECK_FAIL_RETURN(x) { if(FAILURE == (x)) return FAILURE; }
#define GET_RGB_QUAD_SIZE(x) (1 == (x) ? 2 : (4 == (x) ? 16 : (8 == (x) ? 256 : (16 == (x) ? 65536 : 0)))) #define LOG_ERR(format, ...) do { printf("[ ERR ]: "); printf(format, ##__VA_ARGS__); } while (0)
#define LOG_DBG(format, ...) do { printf("[ DBG ]: "); printf(format, ##__VA_ARGS__); } while (0)
#define LOG_INF(format, ...) do { printf("[ INF ]: "); printf(format, ##__VA_ARGS__); } while (0) typedef struct tagBitmapFileHeader
{
WORD bfAlign; //字节对齐
WORD bfType;//位图文件的类型,必须为BM(1-2字节)
DWORD bfSize;//位图文件的大小,以字节为单位(3-6字节,低位在前)
WORD bfReserved1;//位图文件保留字,必须为0(7-8字节)
WORD bfReserved2;//位图文件保留字,必须为0(9-10字节)
DWORD bfOffBits;//位图数据的起始位置,以相对于位图(11-14字节,低位在前)
//文件头的偏移量表示,以字节为单位
}BitmapFileHeader; typedef struct tagBitmapInfoHeader{
DWORD biSize;//本结构所占用字节数(15-18字节)
DWORD biWidth;//位图的宽度,以像素为单位(19-22字节)
DWORD biHeight;//位图的高度,以像素为单位(23-26字节)
WORD biPlanes;//目标设备的级别,必须为1(27-28字节)
WORD biBitCount;//每个像素所需的位数,必须是1(双色),(29-30字节)
//4(16色),8(256色)16(高彩色)或24(真彩色)之一
DWORD biCompression;//位图压缩类型,必须是0(不压缩),(31-34字节)
//1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一
DWORD biSizeImage;//位图的大小(其中包含了为了补齐行数是4的倍数而添加的空字节),以字节为单位(35-38字节)
DWORD biXPelsPerMeter;//位图水平分辨率,每米像素数(39-42字节)
DWORD biYPelsPerMeter;//位图垂直分辨率,每米像素数(43-46字节)
DWORD biClrUsed;//位图实际使用的颜色表中的颜色数(47-50字节)
DWORD biClrImportant;//位图显示过程中重要的颜色数(51-54字节)
}BitmapInfoHeader;
/**
* 当biBitCount = 1, 4, 8时,分别有2, 16, 256个表项;
* 当biBitCount = 24时,没有颜色表项。
*/
typedef struct tagRgbQuad{
BYTE rgbBlue;//蓝色的亮度(值范围为0-255)
BYTE rgbGreen;//绿色的亮度(值范围为0-255)
BYTE rgbRed;//红色的亮度(值范围为0-255)
BYTE rgbReserved;//保留,必须为0
}RgbQuad; typedef struct tagBitmapInfo{
BitmapInfoHeader biInfoHeader;//位图信息头
RgbQuad *rgbQuad; //颜色表
WORD rgbQuadSize; // 颜色表大小
}BitmapInfo; typedef struct tagBitmapHandle{
BitmapFileHeader biFileHeader; // 文件头
BitmapInfo biInfo; // 位图信息头 + 颜色表
BYTE **bPixelMatrix; // 像素矩阵
}BitmapHandle; DWORD InitBitmapHandle(BitmapHandle *biHandle)
{
CHECK_NULL_POINT(biHandle);
memset(biHandle, 0, sizeof(BitmapHandle)); return SUCCESS;
} DWORD CheckBitmapFileHeader(BitmapFileHeader *biFileHeader)
{
CHECK_NULL_POINT(biFileHeader);
CHECK_FALSE_RETURN(0x4D42 == biFileHeader->bfType);
return SUCCESS;
} DWORD CheckBitmapInfoHeader(BitmapInfoHeader *biInfoHeader)
{
WORD biBitCount;
CHECK_NULL_POINT(biInfoHeader);
CHECK_FALSE_RETURN(1 == biInfoHeader->biPlanes);
biBitCount = biInfoHeader->biBitCount;
CHECK_FALSE_RETURN(1 == biBitCount || 4 == biBitCount || 8 == biBitCount
|| 16 == biBitCount || 24 == biBitCount);
CHECK_FALSE_RETURN(0 == biInfoHeader->biCompression);
return SUCCESS;
} DWORD ReadBmpInfo(FILE *pBmpFile, BitmapHandle *biHandle)
{
DWORD dRet;
BitmapFileHeader *biFileHeader;
BitmapInfo *biInfo;
BitmapInfoHeader *biInfoHeader; LOG_DBG("....start ReadBmpInfo....\n");
CHECK_NULL_POINT(pBmpFile);
CHECK_NULL_POINT(biHandle); biFileHeader = &biHandle->biFileHeader;
biInfo = &biHandle->biInfo;
biInfoHeader = &biInfo->biInfoHeader; dRet = fread(&biFileHeader->bfType, sizeof(BitmapFileHeader)-sizeof(WORD), 1, pBmpFile);
CHECK_NULL_RETURN(dRet);
LOG_DBG("ReadBmpInfo: read bitmap file header success!\n");
dRet = CheckBitmapFileHeader(biFileHeader);
CHECK_FAIL_RETURN(dRet);
LOG_DBG("ReadBmpInfo: check bitmap file header success!\n"); dRet = fread(biInfoHeader, sizeof(BitmapInfoHeader), 1, pBmpFile);
CHECK_NULL_RETURN(dRet);
LOG_DBG("ReadBmpInfo: read bitmap info header success!\n");
dRet = CheckBitmapInfoHeader(biInfoHeader);
CHECK_FAIL_RETURN(dRet);
LOG_DBG("ReadBmpInfo: check bitmap info header success!\n"); biInfo->rgbQuadSize = GET_RGB_QUAD_SIZE(biInfoHeader->biBitCount);
biInfo->rgbQuad = (biInfo->rgbQuadSize == 0) ? NULL : ((RgbQuad*)malloc(biInfo->rgbQuadSize*sizeof(RgbQuad)));
CHECK_NULL_POINT(biInfo->rgbQuad);
LOG_DBG("ReadBmpInfo: start read RGB quad...\n");
dRet = fread(biInfo->rgbQuad, sizeof(RgbQuad), biInfo->rgbQuadSize, pBmpFile);
CHECK_FAIL_RETURN(dRet);
LOG_DBG("ReadBmpInfo: read RGB quad success!\n");
LOG_DBG("....end ReadBmpInfo....\n"); return SUCCESS;
} DWORD ReadBmpData(FILE *pBmpFile, BitmapHandle *biHandle)
{
DWORD biWidth;
DWORD biHeight;
WORD biBitCount;
DWORD dwRowBits;
DWORD dwRowBytes;
WORD loop;
BYTE *buff;
DWORD dRet;
BitmapInfo *biInfo;
BitmapInfoHeader *biInfoHeader; LOG_DBG("....start ReadBmpData....\n");
CHECK_NULL_POINT(pBmpFile);
CHECK_NULL_POINT(biHandle); biInfo = &biHandle->biInfo;
CHECK_NULL_POINT(biInfo);
biInfoHeader = &biHandle->biInfo.biInfoHeader;
CHECK_NULL_POINT(biInfoHeader); biWidth = biInfoHeader->biWidth;
biHeight = biInfoHeader->biHeight;
biBitCount = biInfoHeader->biBitCount;
dwRowBits = biWidth * biBitCount; dwRowBytes = dwRowBits >> 3 + ((dwRowBits & 0x7) == 0 ? 0 : 1); // dwRowBits / 8 + (dwRowBits % 8 == 0 ? 0 : 1)
dwRowBytes = dwRowBytes + ((4 - (dwRowBytes & 0x3)) & 0x3); // dwRowBytes + (4 - dwRowBytes % 4) % 4 buff = (BYTE*)malloc(dwRowBytes*biHeight*sizeof(BYTE));
CHECK_NULL_POINT(buff);
LOG_DBG("ReadBmpData: alloc buff for pixel matrix success!\n");
LOG_DBG("ReadBmpData: Width|%d,RowBytes|%d,Height|%d,Size|%d\n", biWidth, dwRowBytes, biHeight, dwRowBytes*biHeight); dRet = fread(buff, dwRowBytes, biHeight, pBmpFile);
CHECK_NULL_RETURN(dRet);
LOG_DBG("ReadBmpData: read pixel matrix success! dRet|%d\n", dRet); biHandle->bPixelMatrix = (BYTE**)malloc(biHeight*sizeof(BYTE*));
CHECK_NULL_POINT(biHandle->bPixelMatrix); for(loop = 0; loop < biHeight; loop++)
{
biHandle->bPixelMatrix[loop] = &buff[loop*dwRowBytes];
} LOG_DBG("....end ReadBmpData....\n"); return SUCCESS;
} DWORD ReleaseBmpSpace(BitmapHandle *biHandle)
{
LOG_DBG("....start ReleaseBmpSpace....\n");
CHECK_NULL_POINT(biHandle); if(biHandle->biInfo.rgbQuad != NULL) free(biHandle->biInfo.rgbQuad);
if(biHandle->bPixelMatrix != NULL)
{
if(biHandle->bPixelMatrix[0] != NULL)
free(biHandle->bPixelMatrix[0]);
free(biHandle->bPixelMatrix);
}
// free(biHandle);
LOG_DBG("....end ReleaseBmpSpace....\n"); return SUCCESS;
} DWORD LogBmpInfo(BitmapHandle *biHandle)
{
BitmapFileHeader *biFileHeader;
BitmapInfo *biInfo;
BitmapInfoHeader *biInfoHeader;
RgbQuad * rgbQuad; CHECK_NULL_POINT(biHandle);
biFileHeader = &biHandle->biFileHeader;
biInfo = &biHandle->biInfo;
biInfoHeader = &biInfo->biInfoHeader;
rgbQuad = biInfo->rgbQuad; LOG_INF("BitmapFileHeader: Type|%x,Size|%d,Offset|%d\n",biFileHeader->bfType, biFileHeader->bfSize, biFileHeader->bfOffBits);
LOG_INF("BitmapInfoHeader: Size|%d,Width|%d,Height|%d,Planes|%d,BitCount|%d,Compression|%d\n",
biInfoHeader->biSize,biInfoHeader->biWidth,biInfoHeader->biHeight,biInfoHeader->biPlanes,biInfoHeader->biBitCount,biInfoHeader->biCompression);
LOG_INF("BitmapInfoHeader: SizeImage|%d,XPelsPerM|%d,YPelsPerM|%d,ClrUsed|%d,ClrImportant|%d\n",
biInfoHeader->biSizeImage,biInfoHeader->biXPelsPerMeter,biInfoHeader->biYPelsPerMeter,biInfoHeader->biClrUsed,biInfoHeader->biClrImportant);
LOG_INF("RgbQuad: %d\n", (rgbQuad == NULL ? 0 : sizeof(rgbQuad)/sizeof(RgbQuad))); return SUCCESS;
} int main(int argc, char *argv[])
{
FILE *pBmpFile;
BitmapHandle biHandle;
/*
printf("short:%d\n", sizeof(short));
printf("long: %d\n", sizeof(long));
printf("BitmapFileHeader:%d\n", sizeof(BitmapFileHeader));
printf("BitmapInfoHeader:%d\n", sizeof(BitmapInfoHeader));
printf("RgbQuad:%d\n", sizeof(RgbQuad));
DWORD dwRowBits;
DWORD dwRowBytes;
dwRowBits = 8448;
dwRowBytes = (dwRowBits & 0x7 == 0 ? 0 : 1) + (dwRowBits >> 3);
printf("dwRowBytes:%d,%d\n", ((dwRowBits & 0x7) == 0 ? 0 : 1), (dwRowBits >> 3));
system("pause");
*/
// CHECK_FAIL_RETURN(2 == argc);
// pBmpFile = fopen(argv[1], "rb");
pBmpFile = fopen("C:\\Users\\10207695\\Documents\\Visual Studio 2010\\Projects\\desp\\Debug\\01.bmp", "rb"); CHECK_NULL_POINT(pBmpFile);
LOG_DBG("Main: open bitmap file success!\n"); ReadBmpInfo(pBmpFile, &biHandle);
ReadBmpData(pBmpFile, &biHandle);
fclose(pBmpFile); LogBmpInfo(&biHandle); system("pause"); ReleaseBmpSpace(&biHandle);
return 0;
}
bitmap解码的更多相关文章
- Android性能优化之Bitmap的内存优化
1.BitmapFactory解析Bitmap的原理 BitmapFactory提供的解析Bitmap的静态工厂方法有以下五种: Bitmap decodeFile(...) Bitmap decod ...
- Android性能优化系列之Bitmap图片优化
https://blog.csdn.net/u012124438/article/details/66087785 在Android开发过程中,Bitmap往往会给开发者带来一些困扰,因为对Bitma ...
- fackbook的Fresco (FaceBook推出的Android图片加载库-Fresco)
[Android开发经验]FaceBook推出的Android图片加载库-Fresco 欢迎关注ndroid-tech-frontier开源项目,定期翻译国外Android优质的技术.开源库.软件 ...
- OOM总结
本文主要信息是来自互联网,我只是自己做了一点总结和摘要. OOM发生的原因 简单的说通过不同的内存分配方式对不同的对象进行操作,会因为android系统版本的差异而产生不同的行为.主要是2.0和4.0 ...
- Android 调用jepg库进行图片压缩,保持图片不失真
1. 浅谈为什么Android和iOS图片质量差距那么大? 首先来说,作为一个安卓狗,机器当然用的是安卓的手机.现在的安卓手机大多数都会以高清拍照,动不动就几千万柔光相机来吸引各种买家.买来后,拍照发 ...
- Fresco-FaceBook推出的Android图片加载库
在Android设备上面,快速高效的显示图片是极为重要的.过去的几年里,我们在如何高效的存储图像这方面遇到了很多问题.图片太大,但是手机的内存却很小.每一个像素的R.G.B和alpha通道总共要占用4 ...
- Android图片加载库Fresco
在Android设备上面,快速高效的显示图片是极为重要的.过去的几年里,我们在如何高效的存储图像这方面遇到了很多问题.图片太大,但是手机的内存却很小.每一个像素的R.G.B和alpha通道总共要占用4 ...
- Android性能优化之图片压缩优化
1 分类Android图片压缩结合多种压缩方式,常用的有尺寸压缩.质量压缩.采样率压缩以及通过JNI调用libjpeg库来进行压缩. 参考此方法:Android-BitherCompress 备注:对 ...
- Android 关于虹软人脸识别SDK引擎使用总结
虹软 最近开放了人脸识别的SDK引擎(免费的哦),刚好有Android版的,就体验了一波.下面来说说Android版的SDK使用心得: ArcFace 虹软人脸认知引擎简介 目前开放的版本有人脸比对( ...
随机推荐
- 【tomcat】HTTPS访问配置 + restful调用远程HTTPS绕过验证
单向验证: 第一步: 生成key: keytool -genkey -alias mykey -keyalg RSA -keystore d:/key/testkey keytool -export ...
- 快速开发框架CRL3.0发布,附带最新的项目示例CRLShoppingDemo
继上次使用CRL实现大数据分库分表方案升级到2.4,时隔不久又升级到了大版本号3.0,主要是因为结构发生了一些更改 ORM和业务封装Package分开了,增加了实例项目演示代码CRLShoppingD ...
- 学习总结 之 WebApi服务监控 log4net记录监控日志
在请求WebApi 的时候,我们更想知道在请求数据的时候,调用了哪个接口传了什么参数过来,调用这个Action花了多少时间,有没有人恶意请求.我们可以通过记录日志,对Action进行优化,可以通过日志 ...
- Python基础(一)
本章内容: Python 的种类 Python 的环境 Python 入门(解释器.编码.pyc文件.脚步传入参数.变量.输入.流程控制与缩进.while循环) 练习题 Python 的种类 Cpyt ...
- 基于HTML5实现3D监控应用流动效果
http://www.hightopo.com/guide/guide/core/lighting/examples/example_flowing.html 流动效果在3D领域有着广泛的应用场景,如 ...
- 基于NodeJS微信公众号
最近重新研究了微信公众号的高级接口,原来也利用C#或JAVA写过微信公众号,主要是消息的基础接口. 由于当时不知道微信公众号可以申请测试公众号,微信测试公众号基本上没有任何限制,对于开发来说是一个不错 ...
- C# 之 EXCEL导入导出
以下方式是本人总结的一些经验,肯定有很多种方法,在此先记下,留待以后补充... 希望朋友们一起来探讨相关想法,请在下方留言. A-1:EXCEL模板导出 非常简单,将EXCEL模板上传到项目中后,将其 ...
- 9.2.4 .net core 通过ViewComponent封装控件
我们在.net core中还使用了ViewComponent方式生成控件.ViewComponent也是asp.net core的新特性,是对页面部分的渲染,以前PartialView的功能,可以使用 ...
- Mac下安装ElasticSearch
简单记录一下安装ES的过程,给小小白们提供一下参考: 下载安装包 https://www.elastic.co/downloads/elasticsearch建议下载2.3.2版本,最新的5.0.0版 ...
- jvm内存溢出分析
概述 jvm中除了程序计数器,其他的区域都有可能会发生内存溢出 内存溢出是什么? 当程序需要申请内存的时候,由于没有足够的内存,此时就会抛出OutOfMemoryError,这就是内存溢出 内存溢出和 ...