C language read BMP picture of RGB data

Source: Internet
Author: User
Tags fread

BMP picture is a bitmap (bitmap), generally uncompressed, to read BMP file As long as you know its file structure can be, the specific format can be Baidu or Google, do not say, a few important points in the code there are comments.

/** C language reads bitmap information **/#include <stdio.h> #include <malloc.h> #define BM 19778//Bitmap logo #define PATH "d:\\test.
	BMP "//Open File path//judgment is bitmap, in 0-1-byte int isbitmap (file *fp) {unsigned short S;
	Fread (&AMP;S,1,2,FP);
	if (S==BM) return 1;
else return 0;
	///Get the width of the picture, in 18-21-byte long getwidth (FILE *fp) {long;
	Fseek (Fp,18,seek_set);
	Fread (&AMP;WIDTH,1,4,FP);	
return width;
	///Get the height of the picture, in 22-25-byte long getheight (FILE *fp) {long;
	Fseek (Fp,22,seek_set);
	Fread (&AMP;HEIGHT,1,4,FP);	
return height;
	//Get the number of bits per pixel in the 28-29-byte unsigned short getbit (FILE *fp) {unsigned short bit;
	Fseek (Fp,28,seek_set);
	Fread (&AMP;BIT,1,2,FP);
return bit;
	///Get the starting position of the data unsigned int getoffset (FILE *fp) {unsigned int OffSet;
	Fseek (Fp,10l,seek_set);
	Fread (&AMP;OFFSET,1,4,FP);
return OffSet;
	}//Obtain the image data void GetData (file* fp,unsigned char *r,unsigned char *g,unsigned char *b) {file* FPR;
	File* FPG;
	
	File* FPB;
	int I, j=0;
	int stride;
	unsigned char* pix=null; LOng Height,width;
	Height=getheight (FP);

	Width=getwidth (FP);	Fseek (FP, GetOffset (FP), seek_set);	Locate the data area of the bitmap stride= (24*width+31)/8;		Align to calculate how many 8-bit stride=stride/4*4 a width is;
	Take a multiple of four R,g,b,alph//write array pix= (unsigned char *) malloc (stride);
	   for (j=0;j 


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.