C. Read and Write the BMP file code.

Source: Internet
Author: User

 

It is easy to read and write BMP file code in pure C code.

/*************************************** * ******************** <Br/> ** copyright (c) 2007, Shanghai <br/> ** file name: read_write_bmp _with_c.c <br/> ** Compilation: visual c ++/ansi c/iso c ++ <br/> ** Date: 2007.10.24 <br/> *********************************** * *************************/<br/> # include "stdio. H "<br/> # include" stdlib. H "<br/> # define pixpline 320 <br/> typedef struct tagrgbquad {// define the data type of each pixel <B R/> unsigned char rgbblue; <br/> unsigned char rgbgreen; <br/> unsigned char rgbred; <br/>} rgbquad; <br/> int BMP _read (unsigned char * image, int xsize, int ysize, char * filename) {<br/> char fname_bmp [128]; <br/> sprintf (fname_bmp, "%s.bmp", filename); </P> <p> file * FP; <br/> If (! (FP = fopen (fname_bmp, "rb") <br/> return-1; </P> <p> unsigned char header [54]; <br/> fread (header, sizeof (unsigned char), 54, FP); <br/> fread (image, sizeof (unsigned char), (size_t) (long) xsize * ysize * 3, FP); </P> <p> fclose (FP); <br/> return 0; <br/>}< br/> int BMP _write (unsigned char * image, int xsize, int ysize, char * filename) {<br/> unsigned char header [54] = {<br/> 0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0, 0, <br/> 54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0, <br/> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, <br/> 0, 0, 0, 0 <br/> }; </P> <p> long file_size = (long) xsize * (long) ysize * 3 + 54; <br/> header [2] = (unsigned char) (file_size & 0x000000ff); <br/> header [3] = (file_size> 8) & 0x000000ff; <br/> header [4] = (file_size> 16) & 0x000000ff; <br/> head Er [5] = (file_size> 24) & 0x000000ff; </P> <p> long width = xsize; <br/> header [18] = width & 0x000000ff; <br/> header [19] = (width> 8) & 0x000000ff; <br/> header [20] = (width> 16) & 0x000000ff; <br/> header [21] = (width> 24) & 0x000000ff; </P> <p> long Height = ysize; <br/> header [22] = height & 0x000000ff; <br/> header [23] = (height> 8) & 0x000000ff; <br/> header [24] = (height> 16) & 0x000000f F; <br/> header [25] = (height> 24) & 0x000000ff; <br/> char fname_bmp [128]; <br/> sprintf (fname_bmp, "maid", filename); </P> <p> file * FP; <br/> If (! (FP = fopen (fname_bmp, "WB") <br/> return-1; </P> <p> fwrite (header, sizeof (unsigned char), 54, FP); <br/> fwrite (image, sizeof (unsigned char), (size_t) (long) xsize * ysize * 3, FP ); </P> <p> fclose (FP); <br/> return 0; <br/>}< br/> void clonebmp (unsigned char * image, int xsize, int ysize) <br/>{< br/> BMP _read (image, xsize, ysize, "orgbmp "); // orgbmp is the BMP file name in the current directory <br/> BMP _write (image, xsize, ysize, "C Lone_bmp "); // clone_bmp is the cloned BMP file name <br/>}< br/> /********************** **************************************** * ************* <br/> * Name: youwritetobmp () <br/> * function: Write a BMP file <br/> * entry parameter: rgbquad * pixarr ---- pointer to the pixel array to be written, int xsize ---- image width, int ysize ---- Image Height, char * filename -- image name <br/> * exit parameter: None <br/> * return value:-1: error; 0: correct <br/> *********************************** **************************************** */<br /> Int youwritetobmp (rgbquad * pixarr, int xsize, int ysize, char * filename) {<br/> unsigned char header [54] = {<br/> 0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0, <br/> 54, 0, 0, 0, 40, 0, 0, 0, 0, 0, <br/> 0, 0, 0, 0, 0, 0, 1, 0, 24, 0, <br/> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, <br/> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, <br/> 0, 0, 0, 0 <br/>}; <br/> int I; <br/> Int J; <br/> long file_size = (long) xsize * (long) ysiz E * 3 + 54; <br/> header [2] = (unsigned char) (file_size & 0x000000ff); <br/> header [3] = (file_size> 8) & 0x000000ff; <br/> header [4] = (file_size> 16) & 0x000000ff; <br/> header [5] = (file_size> 24) & 0x000000ff; </P> <p> long width; <br/> If (! (Xsize % 4) width = xsize; <br/> else width = xsize + (4-xsize % 4); // if it is not a multiple of 4, convert to a multiple of 4 <br/> header [18] = width & 0x000000ff; <br/> header [19] = (width> 8) & 0x000000ff; <br/> header [20] = (width> 16) & 0x000000ff; <br/> header [21] = (width> 24) & 0x000000ff; </P> <p> long Height = ysize; <br/> header [22] = height & 0x000000ff; <br/> header [23] = (height> 8) & 0x000000ff; <br/> header [24] = (height> 16) & 0x000000ff; <br/> header [25] = (height> 24) & 0x000000ff; <br/> char fname_bmp [128]; <br/> sprintf (fname_bmp, "maid", filename); </P> <p> file * FP; <br/> If (! (FP = fopen (fname_bmp, "WB") <br/> return-1; </P> <p> fwrite (header, sizeof (unsigned char), 54, FP); <br/> rgbquad zero = {0, 0}; // insufficient bytes, filled with zero </P> <p> for (j = 0; j <ysize; j ++) {<br/> If (! (Xsize % 4) {<br/> for (I = 0; I <xsize; I ++) {<br/> fwrite (pixarr + I, sizeof (rgbquad), 1, FP); <br/>}< br/> else <br/>{< br/> for (I = 0; I <xsize; I ++) {<br/> fwrite (pixarr + I, sizeof (rgbquad), 1, FP ); <br/>}< br/> for (I = xsize; I <xsize + (4-xsize % 4); I ++) {<br/> fwrite (& zero, sizeof (rgbquad), 1, FP); <br/>}</P> <p> fclose (FP ); <br/> return 0; <br/>}< br/> int main () {<br/> unsigned char * image; <br/> int xsize = 320; <br/> int ysize = 163; <br/> rgbquad pixarray [pixpline]; // array of a row of pixel values <br/> image = (unsigned char *) malloc (size_t) xsize * ysize * 3); <br/> If (image = NULL) <br/> return-1; <br/> clonebmp (image, xsize, ysize); // function 1: copy an image <br/> youwritetobmp (pixarray, xsize, ysize, "yourimage"); // function 2: accurately draws pixels. pixel values are in the pixarray </P> <p> free (image); <br/> return-1; <br/>} 

 

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.