C language to read BMP image data source _c language

Source: Internet
Author: User
Tags bmp image fread

Copy Code code as follows:

/* File name:bmptest.c
Author:wanchuan Xiansheng
date:oct 01, 2011
Description:show all Info a BMP file has. Including
Fileheader info, Infoheader info and Data part.

Reference:bmp image data in C language reading source
*/

#include <stdio.h>
#include <stdlib.h>

#define BITMAPFILEHEADERLENGTH//The BMP Fileheader length is 14
#define BM 19778//The ASCII code for BM

/* Test The file is BMP file or not * *
void Bmpfiletest (file* fpbmp);
/* To get the OffSet of header to data part * *
void Bmpheaderpartlength (file* fpbmp);
/* To get the width and height of the BMP file */
void Bmpwidthheight (file* fpbmp);
/* Show BMP File Tagbitmapfileheader Info * * *
void Bmpfileheader (file* fpbmp);
/* Show BMP File Tagbitmapinfoheader Info * * *
void bmpInfoHeader (file* fpbmp);
/* Show the Data part of BMP file */
void Bmpdatapart (file* fpbmp);

unsigned int OffSet = 0; OffSet from Header part to Data part
Long bmpwidth = 0; The Width of the Data part
Long bmpheight = 0; The Height of the Data part


int main (int argc, char* argv[])
{
/* Open BMP File */
FILE *fpbmp = fopen ("Lena.bmp", "r+");
if (fpbmp = NULL)
{
fprintf (stderr, "Open lena.bmp failed!!! \ n ");
return 1;
}

     bmpfiletest (fpbmp);                //test The file is BMP file or not
     bmpheaderpartlength (fpbmp );       //get The length of Header part
     Bmpwidthheight (fpbmp);            //Get the width and Width of the Data part
    //bmpfileheader (fpbmp);            //show The Fileheader information
    //bmpinfoheader (fpbmp) ;           //show the Infoheader Information
      Bmpdatapart (fpbmp);                //reserve the data to file

Fclose (fpbmp);
return 0;
}

/* Test The file is BMP file or not * *
void Bmpfiletest (file* fpbmp)
{
unsigned short bftype = 0;
Fseek (Fpbmp, 0L, Seek_set);
Fread (&bftype, sizeof (char), 2, fpbmp);
if (BM!= bftype)
{
fprintf (stderr, "This file isn't BMP file.!!! \ n ");
Exit (1);
}
}

/* To get the OffSet of header to data part * *
void Bmpheaderpartlength (file* fpbmp)
{
Fseek (Fpbmp, 10L, Seek_set);
Fread (&offset, sizeof (char), 4, fpbmp);
printf ("The Header part is of length%d.\n", OffSet);
}

/* To get the width and height of the BMP file */
void Bmpwidthheight (file* fpbmp)
{
     Fseek (Fpbmp, 18L, Seek_set);
     fread (&bmpwidth, sizeof (char), 4, fpbmp);
     fread (&bmpheight, sizeof (char), 4, fpbmp);
    //printf ("The Width of the BMP file is%ld.\n", bmpwidth);
    //printf ("The Height of the BMP file is%ld.\n", bmpheight);
}

/* Show bmp File Tagbitmapfileheader info */
void Bmpfileheader (file* fpbmp)
{
     unsigned short bftype;             //UNIT         Bftype;
     unsigned int   bfsize;              //dword       bfsize;
     unsigned short bfreserved1;        //uint         bfReserved1;
     unsigned short bfreserved2;        //uint         BfReserved2;
     unsigned int   bfoffbits;           //dword       bfoffbits;

Fseek (Fpbmp, 0L, Seek_set);

Fread (&bftype, sizeof (char), 2, fpbmp);
Fread (&bfsize, sizeof (char), 4, fpbmp);
Fread (&bfreserved1, sizeof (char), 2, fpbmp);
Fread (&bfreserved2, sizeof (char), 2, fpbmp);
Fread (&bfoffbits, sizeof (char), 4, fpbmp);

printf ("************************************************\n");
printf ("*************tagbitmapfileheader info***********\n");
printf ("************************************************\n");
printf ("Bftype is%d.\n", bftype);
printf ("Bfsize is%d.\n", bfsize);
printf ("BfReserved1 is%d.\n", bfReserved1);
printf ("BfReserved2 is%d.\n", bfReserved2);
printf ("Bfoffbits is%d.\n", bfoffbits);
}

/* Show BMP File Tagbitmapinfoheader Info * * *
void bmpInfoHeader (file* fpbmp)
{
unsigned int bisize; DWORD bisize;
Long biwidth; LONG Biwidth;
Long biheight; LONG Biheight;
unsigned int biplanes; WORD biplanes;
unsigned int bibitcount; WORD biBitCount;
unsigned int bicompression; DWORD bicompression;
unsigned int bisizeimage; DWORD biSizeImage;
Long Bixpelspermerer; LONG Bixpelspermerer;
Long Biypelspermerer; LONG Biypelspermerer;
unsigned int biclrused; DWORD biclrused;
unsigned int biclrimportant; DWORD biclrimportant;

Fseek (Fpbmp, 14L, Seek_set);

Fread (&bisize, sizeof (char), 4, fpbmp);
Fread (&biwidth, sizeof (char), 4, fpbmp);
Fread (&biheight, sizeof (char), 4, fpbmp);
Fread (&biplanes, sizeof (char), 4, fpbmp);
Fread (&bibitcount, sizeof (char), 4, fpbmp);
Fread (&bicompression, sizeof (char), 4, fpbmp);
Fread (&bisizeimage, sizeof (char), 4, fpbmp);
Fread (&bixpelspermerer, sizeof (char), 4, fpbmp);
Fread (&biypelspermerer, sizeof (char), 4, fpbmp);
Fread (&biclrused, sizeof (char), 4, fpbmp);
Fread (&biclrimportant, sizeof (char), 4, fpbmp);

printf ("************************************************\n");
printf ("*************tagbitmapinfoheader info***********\n");
printf ("************************************************\n");
printf ("bisize is%d. \ n", bisize);
printf ("Biwidth is%ld.\n", biwidth);
printf ("Biheight is%ld.\n", biheight);
printf ("biplanes is%d. \ n", biplanes);
printf ("biBitCount is%d. \ n", biBitCount);
printf ("bicompression is%d. \ n", bicompression);
printf ("biSizeImage is%d. \ n", bisizeimage);
printf ("Bixpelspermerer is%ld.\n", bixpelspermerer);
printf ("Biypelspermerer is%ld.\n", biypelspermerer);
printf ("biclrused is%d. \ n", biclrused);
printf ("biclrimportant is%d. \ n", biclrimportant);
}

/* Show the Data part of BMP file */
void Bmpdatapart (file* fpbmp)
{
int I, J;
unsigned char bmppixel[bmpwidth][bmpheight];
unsigned char* bmppixeltmp = NULL;
File* fpdatabmp;

/* New a file to save the data matrix * *
if ((Fpdatabmp=fopen ("BmpData.dat", "w+")) = = NULL)
{
fprintf (stderr, "Failed to construct file BMPDATA.DAT.!!!");
Exit (1);
}

Fseek (Fpbmp, OffSet, Seek_set);
if ((bmppixeltmp= (unsigned char*) malloc (sizeof (char) *bmpwidth*bmpheight)) ==null)
{
fprintf (stderr, "Data allocation failed.!!! \ n ");
Exit (1);
}
Fread (bmppixeltmp, sizeof (char), bmpwidth*bmpheight, fpbmp);

/* Read the data to Matrix and save it in file BmpData.dat * *
for (I =0 < bmpheight; i++)
{
fprintf (Fpdatabmp, "the data in line%-3d:\n", i+1);
for (j = 0; J < Bmpwidth; J + +)
{
BMPPIXEL[I][J] = bmppixeltmp[bmpwidth* (bmpheight-1-i) +j];
Fwrite (&chartmp, sizeof (char), 1, fpdatabmp);
fprintf (fpdatabmp, "%-3d", Bmppixel[i][j]);
if ((j+1)%32 = = 0)
{
fprintf (fpdatabmp, "\ n");
}
}
}
/* Used to test the data read. True or False
Can open the file using Matlab to compare the data * * *
printf ("bmppixel[2][3] is%d.\n", bmppixel[2][3]);
printf ("bmppixel[20][30] is%d.\n", bmppixel[20][30]);

Free (bmppixeltmp);
Fclose (fpdatabmp);
}

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.