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