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

前提

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

读图像操作

函数定义如下:

bool abReadImage(int &rows, int &cols, int &nChannels, io_byte *&imData, const char *imFileName)
{
  imData = NULL;
  int err_code=0;
  try {
    int n;
    bmp_in in;
    if ((err_code = bmp_in__open(&in,imFileName)) != 0)
      throw err_code;
    cols = in.cols;  rows = in.rows;  nChannels = in.num_components;
    io_byte *dp;
    imData = new io_byte[cols*rows*nChannels];
    for (dp=imData, n=rows; n > 0; n--, dp+=cols*nChannels)
      if ((err_code = bmp_in__get_line(&in,dp)) != 0)
        throw err_code;
    bmp_in__close(&in);
  }
  catch (int exc) {
    if (exc == IO_ERR_NO_FILE)
      fprintf(stderr,"Cannot open input file <%s>.\n", imFileName);
    else if (exc == IO_ERR_FILE_HEADER)
      fprintf(stderr,"Error encountered while parsing BMP file header.\n");
    else if (exc == IO_ERR_UNSUPPORTED)
      fprintf(stderr,"Input uses an unsupported BMP file format.\n  Current "
      "simple example supports only 8-bit and 24-bit data.\n");
    else if (exc == IO_ERR_FILE_TRUNC)
      fprintf(stderr,"Input file <%s> truncated unexpectedly.\n", imFileName);
    else if (exc == IO_ERR_FILE_NOT_OPEN)
      fprintf(stderr,"Trying to access a file which is not open!(?)\n");
    return false;
  }
  return true;
}

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

读图像函数输入:

imFileName:输入图像的文件名。

读图像函数输出:

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

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

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

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

写图像操作

函数定义如下:

bool abWriteImage(const int rows, const int cols, const int nChannels, io_byte *imData, const char *imFileName)
{
  int err_code=0;
  try {
    bmp_out out;
    if ((err_code = bmp_out__open(&out,imFileName,cols,rows,nChannels)) != 0)
      throw err_code;

    io_byte *dp;
    int n;
    for (dp=imData, n=rows; n > 0; n--, dp+=cols*nChannels)
      bmp_out__put_line(&out,dp);
    bmp_out__close(&out);
  }
  catch (int exc) {
    if (exc == IO_ERR_NO_FILE)
      fprintf(stderr,"Cannot open the output file <%s>.\n", imFileName);
    else if (exc == IO_ERR_FILE_HEADER)
      fprintf(stderr,"Error encountered while parsing BMP file header.\n");
    else if (exc == IO_ERR_UNSUPPORTED)
      fprintf(stderr,"Input uses an unsupported BMP file format.\n  Current "
      "simple example supports only 8-bit and 24-bit data.\n");
    else if (exc == IO_ERR_FILE_TRUNC)
      fprintf(stderr,"output file <%s> truncated unexpectedly.\n", imFileName);
    else if (exc == IO_ERR_FILE_NOT_OPEN)
      fprintf(stderr,"Trying to access a file which is not open!(?)\n");
    return false;
  }
  return true;
}

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

写图像函数输入:

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

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

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

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

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

实验说明

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

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

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

最新文章

  1. Linux学习之八--关闭firewall防火墙安装iptables并配置
  2. n个结点,不同形态的二叉树(数目+生成)
  3. android VelocityTracker 速度追踪器的使用及创建
  4. bug提交模板
  5. php监听客户端连接状态
  6. linux各种查看端口号
  7. Linux数据流重定向
  8. 开始学习python
  9. FZU 2129 子序列个数
  10. How to Change Password Complexity Requirements in Windows XP
  11. 拥抱模块化的JavaScript
  12. Vim C/C++的一键编译
  13. mysql----innodb统计信息
  14. c语言下多线程
  15. Java程序单元测试工具对比——Parasoft Jtest与Junit
  16. centos 6.6 ios镜像文件 下载 官网和阿里云两种方式教你下载
  17. HTML5基本标签
  18. UNIX环境高级编程——环境变量表读取/添加/修改/删除
  19. android studio2.0出现的gradle问题,instant Run即时运行不了.
  20. Django_cookie_session

热门文章

  1. [LeetCode] Maximum Average Subarray I 子数组的最大平均值
  2. [LeetCode] Lonely Pixel I 孤独的像素之一
  3. nginx 安装及简单配置(适用 小白)
  4. Discuz! 7.x 反射型xss
  5. [Codeforces 946F]Fibonacci String Subsequences
  6. [HNOI 2017]抛硬币
  7. [Luogu 2817]宋荣子的城堡
  8. ●BZOJ 3238 [Ahoi2013]差异
  9. [bzoj4828][Ah/Hnoi2017]大佬
  10. Hash算法入门指南(聊点不一样的算法人生)