Image Handling in C

getbiplab's Avatar, Join Date: Sep 2008
Newbie Member
I have a .bmp image..I want to read the intensities of the pixels in a 2d array...how can i do that??plz help...
0
oogabooga's Avatar
Ambitious contributor
Look in the MSDN Bitmap reference.
0
oogabooga's Avatar
Ambitious contributor
Here's an example program that displays the data for a 24-bit bitmap.
(It will not work for bitmaps with a color palette.)
Code: c
#include <stdio.h>
#include <windows.h>

int main()
{
  BITMAPFILEHEADER bmfh;
  BITMAPINFOHEADER bmih;
  RGBTRIPLE rgbt;
  FILE *f;
  int i;

  f = fopen ("test.bmp", "rb");

  printf ("File Header\n");
  fread (&bmfh, sizeof(bmfh), 1, f);
  printf ("  Size     : %u\n", bmfh.bfSize);
  printf ("  OffBits  : %u\n", bmfh.bfOffBits);

  printf ("Info Header\n");
  fread (&bmih, sizeof(bmih), 1, f);
  printf ("  Size     : %u\n", bmih.biSize);
  printf ("  Width    : %u\n", bmih.biWidth);
  printf ("  Height   : %u\n", bmih.biHeight);
  printf ("  Planes   : %u\n", bmih.biPlanes);
  printf ("  BitCount : %u\n", bmih.biBitCount);
  printf ("  SizeImage: %u\n", bmih.biSizeImage);
  printf ("  ClrUsed  : %u\n", bmih.biClrUsed);

  printf ("RGB Triples\n");
  for (i = 0; i < bmih.biWidth * bmih.biHeight; ++i) {
    fread (&rgbt, sizeof(rgbt), 1, f);
    printf ("%02x %02x %02x\n",
            rgbt.rgbtRed, rgbt.rgbtGreen, rgbt.rgbtBlue);
  }

  fclose (f);
  return 0;
}