open and read bmp image files

This is a discussion on open and read bmp image files within the C Programming forums, part of the General Programming Boards category; I need to read 8 bit indexed color bmp image files and have access to their color pixel data so ...

  1. #1
    Registered User
    Join Date
    Feb 2008

    open and read bmp image files

    I need to read 8 bit indexed color bmp image files and have access to their color pixel data so as to plot histogram. i tried to come up with a part of the code:
    #include <iostream>
    #include <stdio.h>
    #include "windows.h"
    using namespace std;
    int main()
                #define MAXW 500
    			#define MAXH 500
                FILE *ptrBmpFile, *ptBmpFile;
                int num;
                unsigned char data[320][240][3]; 
                BITMAPFILEHEADER bMapFileHeader;
                BITMAPINFOHEADER bMapInfoHeader;
                ptrBmpFile = fopen("file.bmp","rb");
                num = fread(&bMapFileHeader,sizeof(BITMAPFILEHEADER),1,ptrBmpFile);
                num = fread(&bMapInfoHeader,sizeof(BITMAPINFOHEADER),1,ptrBmpFile);
    		    num = fwrite(&bMapFileHeader,sizeof(BITMAPFILEHEADER),1,ptBmpFile);
                num = fwrite(&bMapInfoHeader,sizeof(BITMAPINFOHEADER),1,ptBmpFile);
                //seek beginning of data in bitmap
                //read in bitmap file to data
                //copy image to "copy.bmp"
                return 0;
    however, my output image is unable to be opened. it can only be opened by paint and the image shown is different from the original image. i also need to construct a loop for multiple file reading. the strcuture of the loop i wrote is:
    for(n=1; n<200;n++)
    { filename=n;
    would that be able to work? please help me. thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    The easiest way to deal with the filename loop thing is probably to print to a string:
    sprintf(filename, "%d.bmp", n);
    As to the other if you "rb", you should "wb". (Or at least that's the only thing I'm seeing on a first pass.) I also hope that 320*240*3 is enough size for your data buffer.

  3. #3
    Registered User
    Join Date
    May 2009
    Modify the size of the data array according to the size of your image.
    Worked for me,copy is same as original image.
    The only problem is that for some odd reason windows picture and fax viewer won't open it,all other picture viewers seem to be opening it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 05:17 AM
  2. Replies: 1
    Last Post: 05-27-2009, 01:46 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Replies: 8
    Last Post: 12-27-2003, 02:30 PM
  5. open and read data from files
    By ethorn10 in forum C Programming
    Replies: 5
    Last Post: 02-04-2003, 04:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21