日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當前位置:首頁 > 芯聞號 > 充電吧
[導讀]在網(wǎng)上看到一段讀寫bmp格式圖像的代碼,本文對這段代碼分成兩個函數(shù)封裝起來方便使用,一個函數(shù)是讀取bmp格式的圖像,一個是向指定文件寫入bmp格式的圖像。前提我們不需要知道這段代碼是如何讀取bmp格式

在網(wǎng)上看到一段讀寫bmp格式圖像的代碼,本文對這段代碼分成兩個函數(shù)封裝起來方便使用,一個函數(shù)是讀取bmp格式的圖像,一個是向指定文件寫入bmp格式的圖像。


前提

我們不需要知道這段代碼是如何讀取bmp格式圖像的,不需要知道bmp格式的圖像時如何存儲的,我們只需要知道有三個參數(shù)可以確定圖像的尺寸大小,他們是圖像的寬度、高度、通道數(shù)(例如灰度圖像有一個通道,rgb圖像有三個通道(rgb))。圖像包含高度X寬度個像素,每個像素有相同的通道,他們在內(nèi)存中按照一定的順序存儲,例如三通道bmp圖像,在內(nèi)存中圖像行存儲,第一個像素存儲圖像左下角的像素,第二個像素存儲圖像左下角向右移動一個單位后的像素,依次類推。


讀圖像操作

函數(shù)定義如下:

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;
}

使用此函數(shù)必須要包含頭文件:io_bmp.h,這個頭文件以及他聲明的函數(shù)或者類型的實現(xiàn)可以在這里下載到。

讀圖像函數(shù)輸入:

imFileName:輸入圖像的文件名。

讀圖像函數(shù)輸出:

rows:圖像的行數(shù),或者說圖像的高度。

cols:圖像的列數(shù),或者說圖像的寬度。

nChannels:圖像的通道數(shù)(1或者3,暫時不支持其他的通道)。

imData:存儲圖像像素的數(shù)組,注意,這個數(shù)組的內(nèi)存是在函數(shù)內(nèi)部申請的,在使用完圖像后,記得釋放掉這塊內(nèi)存。


寫圖像操作

函數(shù)定義如下:

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;
}

使用此函數(shù)必須要包含頭文件:io_bmp.h,這個頭文件以及他聲明的函數(shù)或者類型的實現(xiàn)可以在這里下載到。

寫圖像函數(shù)輸入:

imFileName:要寫入磁盤的圖像文件名。

rows:圖像的行數(shù),或者說圖像的高度。

cols:圖像的列數(shù),或者說圖像的寬度。

nChannels:圖像的通道數(shù)(1或者3,暫時不支持其他的通道)。

imData:存儲圖像像素的數(shù)組。

實驗說明

根據(jù)你使用的編譯相關工具不同,給出幾點說明:

1、MSVS。 在你使用這兩個函數(shù)的項目中要添加你下載的io_bmp.h和io_bmp.cpp。這是比較簡單的一種使用方法。

2、如果你在linux上編譯。記得將你下載的兩個文件加入到你的工程中,還要記得對文件的格式進行下轉換(可以使用dos2unix這樣的小工具)。



本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關閉