Read/write functions of BMP images (encapsulation of an open source code)

Source: Internet
Author: User
Tags unsupported truncated

I can see a piece of code for reading and writing BMP format images on the Internet. This article divides this code into two functions for ease of use. One function is to read BMP format images, one is to write BMP format images to a specified file.


Prerequisites

We don't need to know how this Code reads BMP-format images, but how to store BMP-format images, we only need to know that there are three parameters to determine the size of the image. They are the image width, height, and number of channels (for example, a grayscale image has a channel, RGB Images have three channels (RGB )). Images contain X-width pixels in height, and each pixel has the same channel. They are stored in a certain order in the memory, such as three-channel BMP images, which are stored in the image row in the memory, the first pixel stores the pixels in the lower left corner of the image, the second pixel stores the pixels that move one unit to the right in the lower left corner of the image, and so on.


Image read Operations

The function is defined as follows:

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

To use this function, you must include the header file io_bmp .h. the header file and its declared functions or types can be downloaded here.

Read image function input:

Imfilename: name of the input image.

Read image function output:

Rows: number of rows of the image, or the height of the image.

Cols: the number of columns or width of the image.

Nchannels: number of channels for the image (1 or 3, other channels are not supported currently ).

Imdata: An array storing image pixels. Note that the memory of this array is applied within the function. After using the image, remember to release the memory.


Image write operations

The function is defined as follows:

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

To use this function, you must include the header file io_bmp .h. the header file and its declared functions or types can be downloaded here.

Write image function input:

Imfilename: name of the image file to be written to the disk.

Rows: number of rows of the image, or the height of the image.

Cols: the number of columns or width of the image.

Nchannels: number of channels for the image (1 or 3, other channels are not supported currently ).

Imdata: An array that stores image pixels.

Lab description

Based on the Compilation tools you are using, you can provide the following instructions:

1. msvs. Add the io_bmp .h and io_bmp .cpp you downloaded to your project that uses these two functions. This is a simple method to use.

2. If you compile on Linux. Remember to add the two files you downloaded to your project, and also remember to convert the file format (you can use a tool such as dos2unix ).



Read/write functions of BMP images (encapsulation of an open source code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.