|
I used ifstream to read a 5m BMP file and read 4 bytes at a time, but when I read 3712 × 4 bytes, it was eof,
Stop there every time. I do not know why.
Ask for help
///////////////////////////////////////////////////////////////// /// //////////////////////////////////////
// Generate a bmp difference graphic file. In this file, all points that have changes are white: FF FF FF
// The points that have not changed are all black: 00 00 00
// Generate another difference file. That is, for each point, use the S8 mode and retain the difference
//
///////////////////////////////////////////////////////// //////////////////////////////////////////////
void MakeDisFile (char * filepath, int filenum) // filenum: number of files, file name, corresponding to 1. bmp ~ filenum. bmp
{
int headlength = 54;
char BMPfilename0 [100], BMPfilename1 [100], Bisfilename [100], Disfilename [100];
char FileHeader [54];
unsigned char FF [] = {255,255,255,255};
unsigned char OO [] = {0,0,0,0};
int sizePixel;
int countpixel;
typedef unsigned char Pixel [4];
Pixel pixel0, pixel1, Dispixel;
for (int i = 1; i <= filenum; i ++)
{
sprintf (BMPfilename0, "% s\\% d.bmp", filepath, i); // The current file
sprintf (BMPfilename1, "% s\\% d.bmp", filepath, i); // next file
sprintf (Bisfilename, "% s\\Bis% d.bmp", filepath, i); // Bisfilename: difference graphic file
sprintf (Disfilename, "% s\\Dis% d.dat", filepath, i); // Disfilename: difference value file
ifstream ifr0 (BMPfilename0);
ifstream ifr1 (BMPfilename1);
ofstream ofrBMP (Bisfilename);
ofstream ofrDis (Disfilename);
ifr0.read (FileHeader, headlength); // read out the file header, and then write to the Bisfilename
ofrBMP.write (FileHeader, headlength);
ifr1.seekg (headlength);
countpixel = 0;
Ranch
sizePixel = FileHeader [28] / 8; // bitperpixel / 8 = byteperpixel
while (! ifr1.bad ())
{
countpixel ++;
ifr0.read ((char *) pixel0, sizePixel);
ifr1.read ((char *) pixel1, sizePixel);
int dis = 0;
for (int tem_i = 0; tem_i <sizePixel; tem_i ++)
{
Dispixel [tem_i] = pixel0 [tem_i] -pixel1 [tem_i]; // for the distance
dis + = Dispixel [tem_i]; // dis accumulate the entire dis value, when 0: no change
}
// Dispixel difference written to file Disfile
ofrDis.write ((const char *) Dispixel, sizePixel);
Ranch
// If Dispixel is 000, indicating that this point has not changed, write Bisfile to 00 00 00
// if the dispixel is not 000 then write 'FF FF FF'to Bisfile
if (dis)
{// As long as one is non-zero, it means there is a change
ofrBMP.write ((const char *) FF, sizePixel);
}
else // no change
{
ofrBMP.write ((const char *) OO, sizePixel);
}
if (ifr0.eof ())
{
int temp;
temp = countpixel;
}
}
ifr0.close ();
ifr1.close ();
ofrBMP.close ();
ofrDis.close ();
}
} |
|