免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
* 打开和保存bmp文件,这里使用自定义数据结构Bitmap,相关函数定义如下: bmp.h: int CreateBitmap(Bitmap* bmp, int width, int height, int bitCount); void ReleaseBitmap(Bitmap* bmp); int CheckPath(char *path); int ReadBitmap(char* path, Bitmap* bmp); int SaveBitmap(char* path, Bitmap* bmp);* 图像格式转换 basicprocess.h: int RGB2Gray(Bitmap* src, Bitmap* dst); int Gray2RGB(Bitmap* src, Bitmap* dst); 6*- File Info - 29 * 位图文件结构及基本函数定义 打开和保存bmp文件 31typedef unsigned short WORD; 32typedef unsigned long DWORD; 33typedef long LONG; 34typedef unsigned char BYTE;* 位图文件头结构 14字节 */ 37typedef struct tagBITMAPFILEHEADER 38 WORD bfType; DWORD bfSize;WORD bfReserved1;WORD bfReserved2; DWORD bfOffBits; 43 BITMAPFILEHEADER; 45/* 位图信息头结构 40字节 */ 46typedef struct tagBITMAPINFOHEADER 47 DWORD biSize; / 结构长度 40BLONG biWidth;LONG biHeight;WORD biPlanes; / 1 51 WORD biBitCount; / 表示颜色要用到的位数 52 DWORD biCompression; / 压缩格式 53 DWORD biSizeImage; / 位图占用字节数=biWidth(4的整倍数)*biHeight 54 LONG biXPelsPerMeter; / 水平分辨率 55 LONG biYPelsPerMeter; / 垂直分辨率 56 DWORD biClrUsed; / 本图像用到的颜色数 DWORD biClrImportant; / 本图像的重要颜色数 58 BITMAPINFOHEADER; 60/* 调色板 4字节 */ 61typedef struct tagRGBQUAD 62 BYTE rgbBlue;BYTE rgbGreen;BYTE rgbRed;BYTE rgbReserved; RGBQUAD; 68/* 定义图像信息 */ 69typedef struct tagBITMAPINFO 70 BITMAPINFOHEADER bmiHeader; 71 RGBQUAD bmiColors1; 72 BITMAPINFO; 74/* 定义位图图像 */ 75typedef struct _Bitmap 76 77 BITMAPFILEHEADER bmfh; BITMAPINFOHEADER bmih; 79 int width;int height;int bitCount; / 8 或者24 int imageSize; / 图像数据大小(imageSize=height*widthStep)字节 83 BYTE* imageData;/排列的图像数据 84 int widthStep; /排列的图像行大小 85Bitmap; 88 * 位图创建函数 创建一个Bitmap结构,并为图像数据分配空间 90 * 使用方法: 91 * Bitmap *bmp=(Bitmap*)malloc(sizeof(Bitmap); 92 * ret=CreateBitmap(bmp,50,50,3); 94int CreateBitmap(Bitmap* bmp, int width, int height, int bitCount) bmp-width=width; bmp-height=height; bmp-bmih.biWidth=width; bmp-bmih.biHeight=height; bmp-widthStep=(int)(width*bitCount+31)/32)*4; /计算排列的宽度 bmp-imageSize=bmp-height*bmp-widthStep*sizeof(BYTE);/计算排列的图像大小 if(bitCount=8) bmp-bitCount=8; bmp-bmfh.bfType=0x4d42; /注意是4d42 等等 else if (bitCount=24) 处理 else printf(Error(CreateBitmap): only supported 8 or 24 bits bitmap.n); return -1; bmp-imageData=(BYTE*)malloc(bmp-imageSize); /分配数据空间 if(!(bmp-imageData) printf(Error(CreateBitmap): can not allocate bitmap memory.n); return -1; return 0;* 位图指针释放函数 释放位图数据空间 * 使用方法: ReleaseBitmap(bmp);163void ReleaseBitmap(Bitmap* bmp) free(bmp-imageData); bmp-imageData=NULL; free(bmp);bmp=NULL; * 路径检查函数:是否为BMP文件,是否可读* 正确返回0,错误返回-1使用方法 ret=CheckPath(path);int CheckPath(char *path) FILE *fd; int len = strlen(path) / sizeof(char); char ext3; /check whether the path include the characters bmp at end strncpy(ext, &pathlen - 3, 3); if (!(ext0 = b & ext1 = m & ext2 = p) printf(Error(CheckPath): the extension of the file is not bmp.n); return -1; /check whether the file can be read or not fd = fopen(path, r); if (!fd) printf(Error(CheckPath): can not open the file.n); return -1; fclose(fd);return 0; * 从文件中读取位图函数 * 正确返回0,错误返回-1* 使用方法: bmp=(Bitmap*)malloc(sizeof(Bitmap);* ret=ReadBitmap(path, bmp);210int ReadBitmap(char* path, Bitmap* bmp)211 int ret; FILE *fd;/检查路径是否可读216 ret=CheckPath(path);217 if(ret=-1)218 219 printf(Error(ReadBitmap): the path of the image is invalid.n);220 return -1;/打开文件224 fd=fopen(path,rb);225 if(fd=0)226 printf(Error(ReadBitmap): can not open the image.n);228 return -1; /读取文件信息头 14字节232 fread(&(bmp-bmfh.bfType),sizeof(WORD),1,fd);/读取位图信息头 40字节239 fread(&(bmp-bmih.biSize),sizeof(DWORD),1,fd);251 /创建位图结构252 ret=CreateBitmap(bmp, bmp-bmih.biWidth, bmp-bmih.biHeight, bmp-bmih.biBitCount);253 if(ret=-1)254 printf(Error(CreateBitmap): can not CreateBitmap.n);256 return -1; /读取图像数据/由于4字节对齐格式261 fseek(fd,bmp-bmfh.bfOffBits,SEEK_SET); /定位到图像数据区262 ret=fread(bmp-imageData,bmp-imageSize,1,fd);263 if(ret=0)264 if(feof(fd) /if the file pointer point to the end of the file266 268 if(ferror(fd) /if error happened while read the pixel data269 printf(Error(ReadBitmap): can not read the pixel data.n);271 fclose(fd);272 return -1;276 /关闭文件fclose(fd);return 0;282 * 保存位图到文件中去283 * 正确返回0,错误返回-1 使用方法:286 * bmp=(Bitmap*)malloc(sizeof(Bitmap);287 * ret=SaveBitmap(path, bmp);289int SaveBitmap(char* path, Bitmap* bmp)290291 int ret;292 FILE *fd;293294 /检查路径是否正确295 int len = strlen(path) / sizeof(char);296 char ext3;297 /check whether the path include the characters bmp at end298 strncpy(ext, &pathlen - 3, 3);299 if (!(ext0 = b & ext1 = m & ext2 = p)300 301 printf(Error(SaveBitmap): the extension of the file is not bmp.n);302 return -1;303 305 /打开文件306 fd=fopen(path,wb);307 if(fd=0)308 309 printf(Error(SaveBitmap): can not open the image.n);310 return -1;311 313 /保存文件信息头 14字节314 fwrite(&(bmp-bmfh.bfType),sizeof(WORD),1,fd);/保存位图信息头 40字节321 fwrite(&(bmp-bmih.biSize),sizeof(DWORD),1,fd);/如果为8位,则 保存调色板334 RGBQUAD pal256;335 int i;336 if(bmp-bitCount=8)337 338 for(i=0;iimageData,bmp-imageSize,1,fd);355 if(ret!=1)356 357 printf(Error(SaveBitmap): can not save the pixel data.n);358 return -1;359 /关闭文件fclose(fd); return 0;#endif / BMP_H_INCLUDED379* File name: basicprocess.h381* Last Version: 1.0382* Descriptions: 位图图像基本处理函数 图像格式转换392#ifndef BASICPROCESS_H_393#define BASICPROCESS_H_395#include bmp.h396#include 398 * 位图图像基本处理函数 图像格式转换400int RGB2Gray(Bitmap* src, Bitmap* dst)401402 int ret;403 int n=0,i,j;404 BYTE r,g,b,gray;406 /检查图像格式是否合法407 if(src-bitCount!=24)408 409 printf(Error(RGB2Gray): the source image must be in RGB format.n);410 return -1;413 /为dst图像分配数据空间414 ret=CreateBitmap(dst,src-width,src-height,8);415 if(ret=-1)416 417 printf(Error(RGB2Gray): cant create target image.n);418 return -1;419 /计算灰度数据422 for(i=0;iheight;i+)423 424 n=0;425 for(j=0;jwidth*3;j+,n+)426 427 b=*(src-imageData+src-widthStep*(src-height-1-i)+j);428 j+;429 g=*(src-imageData+src-widthStep*(src-height-1-i)+j);430 j+;431 r=*(src-imageData+src-widthStep*(src-height-1-i)+j);432 gray=(r*19595 + g*38469 + b*7472) 16;433 *(dst-imageData+dst-widthStep*(dst-height-1-i)+n)=gray;434 437 return 0;441 * Gray2RGB443 * 使用方法:444 * bmp=(Bitmap*)malloc(sizeof(Bitmap);445 * ret=ReadBitmap(path, bmp);446 * dstbmp=(Bitmap*)malloc(sizeof(Bitmap);447 * re
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 26年护理压力缓解课件
- 26年基础护理校企合作教学课件
- 煤矿设备安全风险点管控措施清单
- 2025年度中国美术馆社会公开招聘笔试参考题库附带答案详解
- 纳税申报管理不规范问题排查整改报告
- 2025年设备监理师职业资格考试设备工程质量管理与检验题库及答案
- 儿科护士长工作总结
- 公司精准扶贫工作经验介绍演讲稿
- 《氯化铵》氯化铵
- 复杂剖宫产手术专家共识2026
- 2025年电工(高级)考试练习题库(1000题)含答案
- 重症肌无力危象患者呼吸道管理的护理查房
- 机关宣传稿培训
- 2025年抗肿瘤药物临床合理应用培训试题及答案
- 小学图形与几何教学课件
- 铁路线路起道作业课件
- 新22G01 砌体房屋结构构造(烧结普通砖、烧结多孔砖)
- DBJ50-T-291-2018 建设工程施工现场安全资料管理标准
- 2025卫生职称(副高)考试小儿内科学高级职称(副高)历年考试真题及答案
- 2025年托育园考试题库及答案
- 中国南水北调集团文旅发展有限公司(新闻宣传中心)招聘笔试题库2025
评论
0/150
提交评论