Lossless Image Compression - Image Recovery Fault
My prof. gave me two programs written in C: a lossless compression and a decompression one. I have to implement the Integer Wavelet Transform (I was using the lifting scheme of Haar's function but I changed to the Daubechies 5/3 int-to-int later) to the code blocks to improve the compression ratio.
Everything was going fine in the modified programs except one mistake which also exists in the original programs he handed out.
Among several medical-type images of *.bmp extension, there are two images that cannot be recovered to their initial forms (i.e. just seeing some blocks of black and white instead of seeing a human face) although checking function informs that there is no difference between the files before and after the compressing process.
Remember that the rest images after decompressing look identical to the original ones.
You can take a look of these images here: http://s844.photobucket.com/home/miroseh. Moon surface and Zelda (girl) are not good. X-rayed chest is perfect.
I hope to hear solutions from you. I've stuck with this for weeks.
I attach the create_matrix_image (in compress.cpp) and restorebmp function (in decompress.cpp) as following.
If these still are ambiguous, you can download the source codes and grey images here
Using Borland C/C++ 5.02 compiler is better off: http://www.mediafire.com/?njmm1ovenmm
Thank you for your consid开发者_如何学Goeration.
What do you mean by "checking function informs that there is no difference between the files before and after the compressing process"? I guess you're comparing the raw data between the two images. If the raw image data are identical but files end up being different, this may have to do with an incorrect BMP header. Try comparing the BMP files as well. I see that the Zelda images only differ on the first 1088 bytes, whereas the chest images only differ on the first 64 bytes.
Take a look at the BMP file spec here, you may find some info about the header:
http://en.wikipedia.org/wiki/BMP_file_format#BMP_File_Header
The checking function compares the grayscale matrix of the initial image to its relevant decompressed one. It shows no differences but somehow some images are decompressed well, while some are not.
But I don't think that a mistake occurred in the HEADER.
They're all comply with stdvalues.
struct tagBITMAPFILEHEADER bmfh;
// Create BITMAPFILEHEADER
bmfh.wType = 0x4d42;
bmfh.dwSize = wid*hei+1078;
bmfh.wReserved1 = 0;
bmfh.wReserved2 = 0;
bmfh.dwOffBits = 0x0436;
What do you mean by 'Try comparing the BMP files' and 'Zelda images only differ on the first 1088 bytes, whereas the chest images only differ on the first 64 bytes'?
This is the whole restorebmp function:
void restorebmp(int way)
{
struct tagBITMAPFILEHEADER bmfh;
struct tagBITMAPINFOHEADER bmih;
struct tagRGBQUAD rgb[256];
char outfile_name[MAXFILE+MAXEXT];
char ext[10];
int i,j;
char gray_bit;
FILE *restore;
for(i=0;i
for(j=0;j
{ int temp=0;
int bit[8];
for (bit_no = 7; bit_no >= 0; bit_no--)
{ bit[bit_no]=*((char )bit_plane[bit_no]+iwid+j);
if(way!=0 && bit_no<7)
bit[bit_no] = bit[bit_no]^bit[bit_no + 1];
// convert them to decimal values to be saved to pict[i][j]
temp+=bit[bit_no]*pow(2,bit_no);
}
pict[i][j]=temp;
}
// change gray value
for(i=0;i
for(j=0;j
pict[i][j]=p1[pict[i][j]];
unsigned char temp;
for (i = 0; i < hei/2; i++)
for (j = 0; j < wid; j++)
{
temp = pict[i][j];
pict[i][j] = pict[hei-i-1][j]; pict[hei-i-1][j] = temp;
}
// Create BITMAPFILEHEADER
bmfh.wType = 0x4d42;
bmfh.dwSize = wid*hei+1078;
bmfh.wReserved1 = 0;
bmfh.wReserved2 = 0;
bmfh.dwOffBits = 0x0436;
// Create BITMAPINFOHEADER
bmih.dwSize = 40;
bmih.dwWidth = wid;
bmih.dwHeight = hei;
bmih.wPlanes = 1;
bmih.wBitCount = 8;
bmih.dwCompression = 0;
bmih.dwSizeImage = hei*wid;
bmih.dwXPelsPerMeter = 0;
bmih.dwYPelsPerMeter = 0;
bmih.dwClrUsed = 0;
bmih.dwClrImportant = 0;
// Create color palette
char tempc[256];
int count=0;
for(i=0;i<32;i++)
for(j=0;j<8;j++)
{tempc[8*i+j]=bit(j,intensity[i]);
if (tempc[8*i+j]==1)
{ rgb[count].bBlue=(unsigned char)(8*i+j);
rgb[count].bGreen=(unsigned char)(8*i+j); rgb[count].bRed=(unsigned char)(8*i+j); rgb[count].bReserved=0;
count++;
}
}
if(count<256)
for(i=count;i<256;i++)
{ rgb[i].bBlue=0;
rgb[i].bGreen=0; rgb[i].bRed=0; rgb[i].bReserved=0;
}
// Create bmp file
strcpy(outfile_name,orig_name);
sprintf(ext,"%d_gn.bmp",way);
strcat(outfile_name,ext);
if ( (restore = fopen (outfile_name, "wb")) == NULL )
{ printf ("Cannot create bmp file"); getch(); exit (1); }
fwrite (&bmfh, sizeof(struct tagBITMAPFILEHEADER), 1, restore);
fwrite (&bmih, sizeof(struct tagBITMAPINFOHEADER), 1,restore);
for (i=0; i<256; i++)
fwrite (&rgb[i], sizeof(struct tagRGBQUAD), 1, restore);
fseek(restore,0x0436,SEEK_SET);
for(i=0;i
for(j=0;j
fwrite(&pict[i][j],sizeof(pict[i][j]),1,restore);
fclose(restore);
cprintf("Finish restore bmp file\n\r");
for(i=0;i<8;i++)
free(bit_plane[i]);
for(i=0;i<7;i++)
free(refer_plane[i]);
}
精彩评论