代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
读写BMP图像的像素
作者:
Dream0303
/ 发布于
2013/12/26
/
774
#ifndef DLOVE_BMP_H #define DLOVE_BMP_H namespace dlove{ typedef unsigned int uint32; typedef unsigned char uint8; class Bmp{ uint32 height; //高度 uint32 width; //宽度 uint32 size; //总大小 uint32 xpelsPerMeter; //水平分辨率 uint32 ypelsPerMeter; //竖直分辨率 uint8 alignBytes; //每一行为了是4的倍数而补充的字节 mutable uint8 rgbs[3]; //getPixelAt()把返回值放在这里,setPixelAt()从这里读取 uint8 *rgbArray; uint32 bytesArow; //每一行的字节数 public: enum Attri{HEIGHT,WIDTH,SIZE,XPELS,YPELS}; public: Bmp(uint32 width,uint32 height,uint32 xpels,uint32 ypels); Bmp(const char *filename); ~Bmp(); uint8 *getPixelAt(int x,int y)const; void setPixelAt(uint8 *rgbs,int x,int y); uint32 get(Attri attr)const; void store(const char *storein)const; private: inline static uint8 getAlignBytes(uint32 _width) { return 4-(_width*3 & 0x03) & 0x03; }//计算每一行像素为了是4的倍数而补加的字节; inline uint32 getIndex(int x,int y)const{ //返回bmp图像上,坐标在(x,y)处的像素点在 rgbArray 中的下标; return (height-(++y))*bytesArow+x*3; } }; } #endif #include "dloveBmp.h" #include <stdio.h> #include <malloc.h> #include <dlovemacros.h> using namespace dlove; Bmp::Bmp(uint32 _width,uint32 _height,uint32 _xpels,uint32 _ypels): width(_width), height(_height), xpelsPerMeter(_xpels), ypelsPerMeter(_ypels) { this->alignBytes=( getAlignBytes(_width) ); this->bytesArow=_width*3+this->alignBytes; int bmpPixelSize=this->bytesArow*height; this->size=bmpPixelSize+54; rgbArray=(uint8*)malloc(bmpPixelSize); } #define mReadAttr(attr) \ fread((void*)&attr,sizeof attr,1,bmpfile); #define mGoto(index) \ fseek(bmpfile,index,SEEK_SET); Bmp::Bmp(const char *filename){ FILE *bmpfile=fopen(filename,"rb"); short bitCount; //图像每个像素的位数 uint32 compression; //图像的压缩类型 uint32 pixelSize; //bmp中像素总大小 mGoto(0x02);mReadAttr(size); mGoto(0x12);mReadAttr(width);mReadAttr(height); mGoto(0x1c);mReadAttr(bitCount);mReadAttr(compression);mReadAttr(pixelSize); mGoto(0x26);mReadAttr(xpelsPerMeter);mReadAttr(ypelsPerMeter); if(compression!=0) throw 0x001; if(bitCount!=24) throw 0x002; this->alignBytes=getAlignBytes(this->width); this->bytesArow=pixelSize/this->height; this->rgbArray=(uint8*)malloc(pixelSize); mGoto(0x36); fread((void*)rgbArray,sizeof(uint8),pixelSize,bmpfile); fclose(bmpfile); } Bmp::~Bmp(){ free(this->rgbArray); } uint8* Bmp::getPixelAt(int x,int y)const{ int index=getIndex(x,y); // printf("index: %u \n",index); //debug for(register int ci(2);ci>=0;--ci,++index) *(rgbs+ci)=*(rgbArray+index); return rgbs; } void Bmp::setPixelAt(uint8 *_rgbs,int x,int y){ int index=getIndex(x,y)+2; for(register int ci=2;ci>=0;--ci,++_rgbs,--index) *(rgbArray+index)=*_rgbs; return ; } uint32 Bmp::get(Attri attri)const{ switch(attri){ case WIDTH:return this->width; case HEIGHT:return this->height; case XPELS:return this->xpelsPerMeter; case YPELS:return this->ypelsPerMeter; case SIZE:return this->size; } return -1; } #define mDefine(type,num,value) \ type mLink(attri,num)=value; #define mWriteNum(num) \ fwrite((void*)&(mLink(attri,num)),sizeof(mLink(attri,num)),1,bmpfile); #define mWrite(attr) \ fwrite((void*)&attr,sizeof attr,1,bmpfile); void Bmp::store(const char *filename)const{ FILE *bmpfile=fopen(filename,"wb"); mDefine(unsigned short,1,0x4d42); mDefine(unsigned short,3,0); mDefine(unsigned short,4,0); mDefine(uint32,5,0x0036); mDefine(uint32,6,0x0028); mDefine(unsigned short,9,1); mDefine(unsigned short,10,0x0018); mDefine(uint32,11,0); mDefine(uint32,15,0); mDefine(uint32,16,0); mWriteNum(1); mWrite(this->size); mWriteNum(3); mWriteNum(4); mWriteNum(5); mWriteNum(6); mWrite(this->width); mWrite(this->height); mWriteNum(9); mWriteNum(10); mWriteNum(11); int pixelSize=size-54; mWrite(pixelSize); mWrite(this->xpelsPerMeter); mWrite(this->ypelsPerMeter); mWriteNum(15); mWriteNum(16); fwrite((void*)rgbArray,sizeof(uint8),pixelSize,bmpfile); fclose(bmpfile); return ; }
试试其它关键字
BMP图像
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dream0303
贡献的其它代码
(
4
)
.
数组内所有元素都除以2
.
canvas雪花飘啊飘
.
构建与发送HTTP请求报文
.
读写BMP图像的像素
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3