1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
#include <fstream>
#include "im_bmp.h"
using namespace std;
void read_bmp(const char* f)
{
pict I; unsigned char pix[3]; px pxl; int i = 0;
FILE* fl = fopen(f, "rb");
fread(&I, sizeof(pict), 1, fl);
printf("%d\n%d\n",I.im.bpp,I.fhd.f_off); // A test to show the bit per pixel and the offset(where image data begin)
while(i<2)
{
fread(&pix,1,3,fl);
printf("%d %d %d ",pix[2],pix[1],pix[0]); // A test to show the first two pixels
i++;
}
// The code below read binary files in the C++ way
/*
ifstream ifs;
ifs.open(f,ios::binary);
ifs.read((char *)&I,sizeof(pict));
cout << I.im.bpp << endl; // It works here. It's headers
ifs.read((char *)&pxl,sizeof(pxl));
cout << pxl.r << endl; // It fails here to read the first pixel
*/
fclose(fl);
}
|