Thread: open and read bmp image files

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    1

    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:
    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]; 
    		
                memset(data,0,320*240*3);
        
    
    
                BITMAPFILEHEADER bMapFileHeader;
                BITMAPINFOHEADER bMapInfoHeader;
    
                
                ptrBmpFile = fopen("file.bmp","rb");
                
                fseek(ptrBmpFile,0,SEEK_SET);            
                num = fread(&bMapFileHeader,sizeof(BITMAPFILEHEADER),1,ptrBmpFile);
                num = fread(&bMapInfoHeader,sizeof(BITMAPINFOHEADER),1,ptrBmpFile);
              
    			 ptBmpFile=fopen("copy.bmp","w");
    		fseek(ptBmpFile,0,SEEK_SET);
    		
    		    num = fwrite(&bMapFileHeader,sizeof(BITMAPFILEHEADER),1,ptBmpFile);
                num = fwrite(&bMapInfoHeader,sizeof(BITMAPINFOHEADER),1,ptBmpFile);
    			
                //seek beginning of data in bitmap
                fseek(ptrBmpFile,54,SEEK_SET);
    			
    
                
                //read in bitmap file to data
                fread(data,bMapInfoHeader.biSizeImage,1,ptrBmpFile);
     
                fclose(ptrBmpFile);
    
                //copy image to "copy.bmp"
               fseek(ptrBmpFile,54,SEEK_SET);
    			fwrite(data,bMapInfoHeader.biSizeImage,1,ptBmpFile);
    			fclose(ptBmpFile);
    			
                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:
    Code:
    for(n=1; n<200;n++)
    { filename=n;
    fopen(n.bmp,"rb");
    }
    would that be able to work? please help me. thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The easiest way to deal with the filename loop thing is probably to print to a string:
    Code:
    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
    Posts
    1
    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, 04:17 AM
  2. Replies: 1
    Last Post: 05-27-2009, 12:46 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09: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