bmp格式图像的读写函数(对一个开源代码的封装)

转自:bmp格式图像的读写函数(对一个开源代码的封装)

网上看到一段读写bmp格式图像的代码,本文对这段代码分成两个函数封装起来方便使用,一个函数是读取bmp格式的图像,一个是向指定文件写入bmp格式的图像。


前提

我们不需要知道这段代码是如何读取bmp格式图像的,不需要知道bmp格式的图像时如何存储的,我们只需要知道有三个参数可以确定图像的尺寸大小,他们是图像的宽度、高度、通道数(例如灰度图像有一个通道,rgb图像有三个通道(rgb))。图像包含高度X宽度个像素,每个像素有相同的通道,他们在内存中按照一定的顺序存储,例如三通道bmp图像,在内存中图像行存储,第一个像素存储图像左下角的像素,第二个像素存储图像左下角向右移动一个单位后的像素,依次类推。


读图像操作

函数定义如下:

  1. bool abReadImage(int &rows, int &cols, int &nChannels, io_byte *&imData, const char *imFileName)  
  2. {  
  3.   imData = NULL;  
  4.   int err_code=0;  
  5.   try {  
  6.     int n;  
  7.     bmp_in in;  
  8.     if ((err_code = bmp_in__open(&in,imFileName)) != 0)  
  9.       throw err_code;  
  10.     cols = in.cols;  rows = in.rows;  nChannels = in.num_components;  
  11.     io_byte *dp;  
  12.     imData = new io_byte[cols*rows*nChannels];  
  13.     for (dp=imData, n=rows; n > 0; n--, dp+=cols*nChannels)  
  14.       if ((err_code = bmp_in__get_line(&in,dp)) != 0)  
  15.         throw err_code;  
  16.     bmp_in__close(&in);  
  17.   }  
  18.   catch (int exc) {  
  19.     if (exc == IO_ERR_NO_FILE)  
  20.       fprintf(stderr,"Cannot open input file <%s>.\n", imFileName);  
  21.     else if (exc == IO_ERR_FILE_HEADER)  
  22.       fprintf(stderr,"Error encountered while parsing BMP file header.\n");  
  23.     else if (exc == IO_ERR_UNSUPPORTED)  
  24.       fprintf(stderr,"Input uses an unsupported BMP file format.\n  Current "  
  25.       "simple example supports only 8-bit and 24-bit data.\n");  
  26.     else if (exc == IO_ERR_FILE_TRUNC)  
  27.       fprintf(stderr,"Input file <%s> truncated unexpectedly.\n", imFileName);  
  28.     else if (exc == IO_ERR_FILE_NOT_OPEN)  
  29.       fprintf(stderr,"Trying to access a file which is not open!(?)\n");  
  30.     return false;  
  31.   }  
  32.   return true;  
  33. }  

使用此函数必须要包含头文件:io_bmp.h,这个头文件以及他声明的函数或者类型的实现可以在 这里下载到。

读图像函数输入:

imFileName:输入图像的文件名。

读图像函数输出:

rows:图像的行数,或者说图像的高度。

cols:图像的列数,或者说图像的宽度。

nChannels:图像的通道数(1或者3,暂时不支持其他的通道)。

imData:存储图像像素的数组,注意,这个数组的内存是在函数内部申请的,在使用完图像后,记得释放掉这块内存。


写图像操作

函数定义如下:

  1. bool abWriteImage(const int rows, const int cols, const int nChannels, io_byte *imData, const char *imFileName)  
  2. {  
  3.   int err_code=0;  
  4.   try {  
  5.     bmp_out out;  
  6.     if ((err_code = bmp_out__open(&out,imFileName,cols,rows,nChannels)) != 0)  
  7.       throw err_code;  
  8.   
  9.     io_byte *dp;  
  10.     int n;  
  11.     for (dp=imData, n=rows; n > 0; n--, dp+=cols*nChannels)  
  12.       bmp_out__put_line(&out,dp);  
  13.     bmp_out__close(&out);  
  14.   }  
  15.   catch (int exc) {  
  16.     if (exc == IO_ERR_NO_FILE)  
  17.       fprintf(stderr,"Cannot open the output file <%s>.\n", imFileName);  
  18.     else if (exc == IO_ERR_FILE_HEADER)  
  19.       fprintf(stderr,"Error encountered while parsing BMP file header.\n");  
  20.     else if (exc == IO_ERR_UNSUPPORTED)  
  21.       fprintf(stderr,"Input uses an unsupported BMP file format.\n  Current "  
  22.       "simple example supports only 8-bit and 24-bit data.\n");  
  23.     else if (exc == IO_ERR_FILE_TRUNC)  
  24.       fprintf(stderr,"output file <%s> truncated unexpectedly.\n", imFileName);  
  25.     else if (exc == IO_ERR_FILE_NOT_OPEN)  
  26.       fprintf(stderr,"Trying to access a file which is not open!(?)\n");  
  27.     return false;  
  28.   }  
  29.   return true;  
  30. }  

使用此函数必须要包含头文件:io_bmp.h,这个头文件以及他声明的函数或者类型的实现可以在这里下载到。

写图像函数输入:

imFileName:要写入磁盘的图像文件名。

rows:图像的行数,或者说图像的高度。

cols:图像的列数,或者说图像的宽度。

nChannels:图像的通道数(1或者3,暂时不支持其他的通道)。

imData:存储图像像素的数组。

实验说明

根据你使用的编译相关工具不同,给出几点说明:

1、MSVS。 在你使用这两个函数的项目中要添加你下载的io_bmp.h和io_bmp.cpp。这是比较简单的一种使用方法。

2、如果你在linux上编译。记得将你下载的两个文件加入到你的工程中,还要记得对文件的格式进行下转换(可以使用dos2unix这样的小工具)。


相关:BMP格式详解



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数字图像获取处理及实践应用源代码\ImageProcessing\cdib.cpp ................................\...............\cdib.h ................................\...............\ChildFrm.cpp ................................\...............\ChildFrm.h ................................\...............\ColorTable.h ................................\...............\Default.SUP ................................\...............\DIBPrcs.cpp ................................\...............\DibShow.cpp ................................\...............\DlgAftReg.cpp ................................\...............\DlgAftReg.h ................................\...............\DlgArith.cpp ................................\...............\DlgBitPlane.cpp ................................\...............\DlgCoding.h ................................\...............\DlgCodingHuffman.cpp ................................\...............\DlgEhnLinTrans.cpp ................................\...............\DlgEhnLinTrans.h ................................\...............\DlgEnhColor.cpp ................................\...............\DlgEnhColor.h ................................\...............\DlgHistShow.cpp ................................\...............\DlgHistShow.h ................................\...............\DlgHistShow1.cpp ................................\...............\DlgHistShow1.h ................................\...............\DlgHuffman.cpp ................................\...............\DlgMedian.cpp ................................\...............\DlgMedian.h ................................\...............\DlgRecMatch.cpp ................................\...............\DlgRecMatch.h ................................\...............\DlgReg.cpp ................................\...............\DlgReg.h ................................\...............\DlgShannon.cpp ................................\...............\DlgSmooth.cpp ................................\...............\DlgSmooth.h ................................\...............\DWT.CPP ................................\...............\Enhance.cpp ................................\...............\FreTrans.cpp ................................\...............\GlobalApi.h ................................\...............\ImageAnalysis.cpp ................................\...............\ImageCoding.cpp ................................\...............\ImageProcessing.aps ................................\...............\ImageProcessing.clw ................................\...............\ImageProcessing.cpp ................................\...............\ImageProcessing.dsp ................................\...............\ImageProcessing.dsw ................................\...............\ImageProcessing.h ................................\...............\ImageProcessing.ncb ................................\...............\ImageProcessing.opt ................................\...............\ImageProcessing.plg ................................\...............\ImageProcessing.rc ................................\...............\ImageProcessingDoc.cpp ................................\...............\ImageProcessingDoc.h ................................\...............\ImageProcessingView.cpp ................................\...............\ImageProcessingView.h ................................\...............\ImageView.cpp ................................\...............\MainFrm.cpp ................................\...............\MainFrm.h ................................\...............\Motion.cpp ................................\...............\ReadMe.txt ................................\...............\Recog.asp ................................\...............\Recog.cpp ................................\...............\res\ImageProcessing.ico ................................\...............\...\ImageProcessing.rc2 ................................\...............\...\ImageProcessingDoc.ico ................................\...............\...\Toolbar.bmp ................................\...............\resource.h ................................\...............\restore.cpp ................................\...............\SegApi.cpp ................................\...............\StdAfx.cpp ................................\...............\StdAfx.h ................................\lena.bmp ................................\ImageProcessing\Debug ................................\...............\Release ................................\...............\res ................................\ImageProcessing

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值