Home › Forums › C Programming › Errors in my program
- This topic has 5 replies, 2 voices, and was last updated 16 years, 9 months ago by Anonymous.
- AuthorPosts
- February 23, 2008 at 9:26 am #2067AnonymousInactive
I m working on small project which is bitmap compression. In this project I have to load a bitmap file and then compress it accoring to run length encoding, bitmap should be in project folder. Before compressing it I have to show the related data like size of bitmap and after compressing I have to show size of image,
syntax for loading the bitmap ifstream file(“test.bmp”, ios::in);
C123if(!file){cerr<<"File can not be opened"<<endl;exit(1);when file is loaded successfully i have to implement it compression and for compressing i implement these two functions
C123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134EndOfLine(int pos){if ( 0 != m_dib.dsBmih.biWidth % 4 ){int scanline_width = m_dib.dsBmih.biWidth;scanline_width = scanline_width + ( 4 - (scanline_width%4));pos %= scanline_width;}return 0 == ((pos+1)% m_dib.dsBmih.biWidth);}CompressInRLE8(BYTE* pSrcBits, CByteArray& pRLEBits, int& RLE_size){int line;int src_index = 0, dst_index = 0, counter, i;// in RLE8 every pixel is one bytefor ( line = 0; line < m_dib.dsBmih.biHeight; line++){state_start: // just at the start of a blockif ( EndOfLine(src_index)) // this is the last pixel of the line{pRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index];src_index++;goto end_of_line;}// now the block length is at least 2, look ahead to decide next state// next two same pixels are the same, enter compress modeif (pSrcBits[src_index] == pSrcBits[src_index+1])goto state_compress;if ( EndOfLine(src_index+1)) // the last 2 pixel of the line{ // these 2 pixel are not the samepRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index++];pRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index++];goto end_of_line;}// now the block length is at least 3// in next 3 pixels, the last 2 consecutive pixel are the sameif (pSrcBits[src_index+1] == pSrcBits[src_index+2]){pRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index++];goto state_compress;}else // in these 3 pixel, no 2 consecutive pixel are the samegoto state_no_compress; // un-compressed block need at least 3 pixelstate_compress: // compress mode// src_index is the first pixel of a compressd block// counter is the number of additional pixels following currrent pixel// (src_index+counter) point to the last pixel of current blockfor ( counter = 1; counter <= 254; counter++){// must check this condition first, or a BUG occur!if ( pSrcBits[src_index+counter] != pSrcBits[src_index] )break;if ( EndOfLine(src_index+counter) ) // reach end of line{pRLEBits[dst_index++] = counter+1; // block length is (counter+1)pRLEBits[dst_index++] = pSrcBits[src_index];src_index += counter +1;goto end_of_line;}}// now pSrcBits[src_index+counter] falls out of blockpRLEBits[dst_index++] = counter; // block length is (counter)pRLEBits[dst_index++] = pSrcBits[src_index];src_index += counter;goto state_start;state_no_compress:// src_index is the first pixel of a un-compressd block// un-compressed block need at least 3 pixel// counter is the number of additional pixels following currrent pixelfor (counter = 2; counter <= 254; counter++){if ( EndOfLine(src_index+counter) ) // reach end of line{pRLEBits[dst_index++] = 0; // escape character for un-compress blockpRLEBits[dst_index++] = counter + 1; // block length is (counter+1)for (i = counter + 1; i > 0; i--)pRLEBits[dst_index++] = pSrcBits[src_index++];if ( 0 != ((counter+1) % 2) ) // block length is oddpRLEBits[dst_index++]; // padd with a zero bytegoto end_of_line;}if ( EndOfLine(src_index+counter+1) ||pSrcBits[src_index+counter] != pSrcBits[src_index+counter+1] )continue; // still no 2 consecutive pixel are the same,// therefore continue to extend the un-compress block// we found two pixels are the sameif ( EndOfLine(src_index+counter+2) ||pSrcBits[src_index+counter+1] != pSrcBits[src_index+counter+2] )continue; // the third pixel is not the same, no need to exit un-compressed modeelse{ // // now pSrcBits[src_index+counter] and following 2 pixel are the same// now we need to exit the un-compressed modeif ( counter > 2)counter--; // we can shrink the block one pixel (go backward)pRLEBits[dst_index++] = 0; // escape character for un-compress blockpRLEBits[dst_index++] = counter+1; // block length is (counter+1)for (i = counter+1; i > 0; i--)pRLEBits[dst_index++] = pSrcBits[src_index++];if ( 0 != ((counter+1) % 2) ) // block length is oddpRLEBits[dst_index++]; // padd with a zero bytegoto state_compress;}} // end of for (counter = 2; counter <= 254; counter++)// now we have a full block of 255 pixelspRLEBits[dst_index++] = 0; // escape character for un-compress blockpRLEBits[dst_index++] = counter; // block length is (counter)for (i = counter; i > 0; i--)pRLEBits[dst_index++] = pSrcBits[src_index++];if ( 0 != ((counter) % 2) ) // block length is oddpRLEBits[dst_index++]; // padd with a zero bytegoto state_start;end_of_line:if ( 0 != (src_index % 4 )) // each scan line is dword align{int pad = 4 - (src_index%4);src_index += pad;}// src_index already point to the start of next linepRLEBits[dst_index++] = 0; // mark end-of-linepRLEBits[dst_index++] = 0;}pRLEBits[dst_index++] = 0; // mark end-of-bitmappRLEBits[dst_index++] = 1;RLE_size = dst_index; // image size}my complete program is
C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195#include<conio.h>#include <iostream>#include <fstream>#include <windows.h>using namespace std;int CompressInRLE8(BYTE* pSrcBits, CByteArray& pRLE_Bits, int& RLE_size);BOOL EndOfLine(int pos);int main(int argc, char *argv[]){BITMAPFILEHEADER bitmapFileHeader;BITMAPINFOHEADER bitmapInfoHeader;ifstream file("test.bmp", ios::in);if(!file){cerr<<"File can not be opened"<<endl;exit(1);}file.read(reinterpret_cast<char *> (&bitmapFileHeader),sizeof(BITMAPFILEHEADER));if(bitmapFileHeader.bfType != 19778 )cout <<" This is not a bitmap (.bmp) file ....";else{cout << "sizeof Bitmap File Header =" << sizeof(bitmapFileHeader) << endl;cout << "size of Bitmap Image =" << bitmapFileHeader.bfSize << endl;}file.read(reinterpret_cast<char *> (&bitmapInfoHeader),sizeof(BITMAPINFOHEADER));if( bitmapInfoHeader.biBitCount != 8){cout <<" This is not a 8 bit bitmap (.bmp) file ....";}else{cout << "sizeof Bitmap Info Header =" << sizeof(bitmapInfoHeader) << endl;cout << "Height of this Bitmap Image =" << bitmapInfoHeader.biHeight << endl;cout << "Widht of this Bitmap Image =" << bitmapInfoHeader.biWidth << endl;int numColors = 1<<bitmapInfoHeader.biBitCount;RGBQUAD rgbQuad[numColors];file.read(reinterpret_cast<char *> (&rgbQuad),sizeof(rgbQuad));}}BOOL EndOfLine(int pos){DIBSECTION m_dib;if ( 0 != m_dib.dsBmih.biWidth % 4 ){int scanline_width = m_dib.dsBmih.biWidth;scanline_width = scanline_width + ( 4 - (scanline_width%4));pos %= scanline_width;}return 0 == ((pos+1)% m_dib.dsBmih.biWidth);}int CompressInRLE8(BYTE* pSrcBits, CByteArray& pRLEBits, int& RLE_size){DIBSECTION m_dib;int line;int src_index = 0, dst_index = 0, counter, i;// in RLE8 every pixel is one bytefor ( line = 0; line < m_dib.dsBmih.biHeight; line++){state_start: // just at the start of a blockif ( EndOfLine(src_index)) // this is the last pixel of the line{pRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index];src_index++;goto end_of_line;}// now the block length is at least 2, look ahead to decide next state// next two same pixels are the same, enter compress modeif (pSrcBits[src_index] == pSrcBits[src_index+1])goto state_compress;if ( EndOfLine(src_index+1)) // the last 2 pixel of the line{ // these 2 pixel are not the samepRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index++];pRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index++];goto end_of_line;}// now the block length is at least 3// in next 3 pixels, the last 2 consecutive pixel are the sameif (pSrcBits[src_index+1] == pSrcBits[src_index+2]){pRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index++];goto state_compress;}else // in these 3 pixel, no 2 consecutive pixel are the samegoto state_no_compress; // un-compressed block need at least 3 pixelstate_compress: // compress mode// src_index is the first pixel of a compressd block// counter is the number of additional pixels following currrent pixel// (src_index+counter) point to the last pixel of current blockfor ( counter = 1; counter <= 254; counter++){// must check this condition first, or a BUG occur!if ( pSrcBits[src_index+counter] != pSrcBits[src_index] )break;if ( EndOfLine(src_index+counter) ) // reach end of line{pRLEBits[dst_index++] = counter+1; // block length is (counter+1)pRLEBits[dst_index++] = pSrcBits[src_index];src_index += counter +1;goto end_of_line;}}// now pSrcBits[src_index+counter] falls out of blockpRLEBits[dst_index++] = counter; // block length is (counter)pRLEBits[dst_index++] = pSrcBits[src_index];src_index += counter;goto state_start;state_no_compress:// src_index is the first pixel of a un-compressd block// un-compressed block need at least 3 pixel// counter is the number of additional pixels following currrent pixelfor (counter = 2; counter <= 254; counter++){if ( EndOfLine(src_index+counter) ) // reach end of line{pRLEBits[dst_index++] = 0; // escape character for un-compress blockpRLEBits[dst_index++] = counter + 1; // block length is (counter+1)for (i = counter + 1; i > 0; i--)pRLEBits[dst_index++] = pSrcBits[src_index++];if ( 0 != ((counter+1) % 2) ) // block length is oddpRLEBits[dst_index++]; // padd with a zero bytegoto end_of_line;}if ( EndOfLine(src_index+counter+1) ||pSrcBits[src_index+counter] != pSrcBits[src_index+counter+1] )continue; // still no 2 consecutive pixel are the same,// therefore continue to extend the un-compress block// we found two pixels are the sameif ( EndOfLine(src_index+counter+2) ||pSrcBits[src_index+counter+1] != pSrcBits[src_index+counter+2] )continue; // the third pixel is not the same, no need to exit un-compressed modeelse{ // // now pSrcBits[src_index+counter] and following 2 pixel are the same// now we need to exit the un-compressed modeif ( counter > 2)counter--; // we can shrink the block one pixel (go backward)pRLEBits[dst_index++] = 0; // escape character for un-compress blockpRLEBits[dst_index++] = counter+1; // block length is (counter+1)for (i = counter+1; i > 0; i--)pRLEBits[dst_index++] = pSrcBits[src_index++];if ( 0 != ((counter+1) % 2) ) // block length is oddpRLEBits[dst_index++]; // padd with a zero bytegoto state_compress;}} // end of for (counter = 2; counter <= 254; counter++)// now we have a full block of 255 pixelspRLEBits[dst_index++] = 0; // escape character for un-compress blockpRLEBits[dst_index++] = counter; // block length is (counter)for (i = counter; i > 0; i--)pRLEBits[dst_index++] = pSrcBits[src_index++];if ( 0 != ((counter) % 2) ) // block length is oddpRLEBits[dst_index++]; // padd with a zero bytegoto state_start;end_of_line:if ( 0 != (src_index % 4 )) // each scan line is dword align{int pad = 4 - (src_index%4);src_index += pad;}// src_index already point to the start of next linepRLEBits[dst_index++] = 0; // mark end-of-linepRLEBits[dst_index++] = 0;}pRLEBits[dst_index++] = 0; // mark end-of-bitmappRLEBits[dst_index++] = 1;RLE_size = dst_index; // image size}return 0;}So this program shows errors and does not run correctly
First error is on this line and says”7 E:MCSFALL_2007CS301ProjectImage Compressionmain.cpp
type specifier omitted for parameter” int CompressInRLE8(BYTE* pSrcBits, CByteArray& pRLE_Bits, int& RLE_size);
2nd error is also on this line and says”7 E:MCSFALL_2007CS301ProjectImage Compressionmain.cpp
parse error before &'”
3rd error is also on this line when i implement this function into main function and this error says”72 E:MCSFALL_2007CS301ProjectImage Compressionmain.cpp
type specifier omitted for parameter”
4th error is also on this line and says”72 E:MCSFALL_2007CS301ProjectImage Compressionmain.cpp
parse error before ‘&'”
5th error is on this line
pRLEBits[dst_index++] = 1; // block of length 1
and says”86 E:MCSFALL_2007CS301ProjectImage Compressionmain.cpp
pRLEBits’ undeclared (first use this function)”
6th error is on this line
pRLEBits[dst_index++] = pSrcBits[src_index];
and this error says
“87 E:MCSFALL_2007CS301ProjectImage Compressionmain.cpppSrcBits’ undeclared (first use this function)”
7th error is on this line
RLE_size = dst_index; // image size
and this error says”208 E:MCSFALL_2007CS301ProjectImage Compressionmain.cpp
RLE_size’ undeclared (first use this function)”
8th error is on this line
return 0;
and this error says”212 E:MCSFALL_2007CS301ProjectImage Compressionmain.cpp
parse error before `return'”I have not so much experience to do that kindly implement it on ur compiler and then help me to resolve these issues and so that I can implement it correctly thanks
- February 24, 2008 at 12:39 pm #3327msaqibParticipant
Hello
I have made few changes to your program and its working fine and shows all the details about the bitmap image. What I think is that you have copied and pasted the code from some other source, and there were some errors (might be pasting errors), that was the reason your program was not working.
OK! I have tested your program on MS visual C++ 6 compiler and working fine. Below you can find the working code for your program. As it was an MFc application so “stdafx.h” header file has been added. Also if you goto the line (54-56) you will find that some code is commented, I think that was not a necessary code and was also creating some errors of type casting. If you really need that code to use then you will have to get around with it through some other ways.
Because123int numColors = 1<<bitmapInfoHeader.biBitCount;<br />RGBQUAD rgbQuad[numColors];<br />you can not declare an array variable with dynamic size which can change at run time. You can use some other techniques like type casting. Rest of the code is working fine.
Oh one more thing, by default MS visual Studio environment creates a single threaded application where as the libraries used in this program are multithreaded so you might get some errors at run time. To run the program smoothly you will need to make few changes in project settings. You will need to perform the following tasks before running the project.
Change the application to multithreaded. To do this:
1. Go to Project Settings and select the ‘C/C++’ tab.
2. Select ‘Code Generation’ in Category.
3. Set Use run-time library to either Debug Multithreaded (for debug builds) or Multithreaded (for release builds).
Best of luck with your project.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195#include "conio.h"#include "stdafx.h"#include "iostream"#include "fstream"#include "windows.h"using namespace std;int CompressInRLE8(BYTE* pSrcBits, CByteArray& pRLE_Bits, int& RLE_size);BOOL EndOfLine(int pos);int main(int argc, char *argv[]){BITMAPFILEHEADER bitmapFileHeader;BITMAPINFOHEADER bitmapInfoHeader;ifstream file("test.bmp", ios::in);if(!file){cerr<<"File can not be opened"<<endl;exit(1);}file.read(reinterpret_cast<char *> (&bitmapFileHeader),sizeof(BITMAPFILEHEADER));if(bitmapFileHeader.bfType != 19778 )cout <<" This is not a bitmap (.bmp) file ....";else{cout << "sizeof Bitmap File Header =" << sizeof(bitmapFileHeader) << endl;cout << "size of Bitmap Image =" << bitmapFileHeader.bfSize << endl;}file.read(reinterpret_cast</char><char *> (&bitmapInfoHeader),sizeof(BITMAPINFOHEADER));if( bitmapInfoHeader.biBitCount != 8){cout <<" This is not a 8 bit bitmap (.bmp) file ....";}else{cout << "sizeof Bitmap Info Header =" << sizeof(bitmapInfoHeader) << endl;cout << "Height of this Bitmap Image =" << bitmapInfoHeader.biHeight << endl;cout << "Widht of this Bitmap Image =" << bitmapInfoHeader.biWidth << endl;int numColors = 1<<bitmapInfoHeader.biBitCount;//RGBQUAD rgbQuad[numColors];//file.read(reinterpret_cast</char><char *> (&rgbQuad),sizeof(rgbQuad));}return 0;}BOOL EndOfLine(int pos){DIBSECTION m_dib;if ( 0 != m_dib.dsBmih.biWidth % 4 ){int scanline_width = m_dib.dsBmih.biWidth;scanline_width = scanline_width + ( 4 - (scanline_width%4));pos %= scanline_width;}return 0 == ((pos+1)% m_dib.dsBmih.biWidth);}int CompressInRLE8(BYTE* pSrcBits, CByteArray& pRLEBits, int& RLE_size){DIBSECTION m_dib;int line;int src_index = 0, dst_index = 0, counter, i;// in RLE8 every pixel is one bytefor ( line = 0; line < m_dib.dsBmih.biHeight; line++){state_start: // just at the start of a blockif ( EndOfLine(src_index)) // this is the last pixel of the line{pRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index];src_index++;goto end_of_line;}// now the block length is at least 2, look ahead to decide next state// next two same pixels are the same, enter compress modeif (pSrcBits[src_index] == pSrcBits[src_index+1])goto state_compress;if ( EndOfLine(src_index+1)) // the last 2 pixel of the line{ // these 2 pixel are not the samepRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index++];pRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index++];goto end_of_line;}// now the block length is at least 3// in next 3 pixels, the last 2 consecutive pixel are the sameif (pSrcBits[src_index+1] == pSrcBits[src_index+2]){pRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index++];goto state_compress;}else // in these 3 pixel, no 2 consecutive pixel are the samegoto state_no_compress; // un-compressed block need at least 3 pixelstate_compress: // compress mode// src_index is the first pixel of a compressd block// counter is the number of additional pixels following currrent pixel// (src_index+counter) point to the last pixel of current blockfor ( counter = 1; counter <= 254; counter++){// must check this condition first, or a BUG occur!if ( pSrcBits[src_index+counter] != pSrcBits[src_index] )break;if ( EndOfLine(src_index+counter) ) // reach end of line{pRLEBits[dst_index++] = counter+1; // block length is (counter+1)pRLEBits[dst_index++] = pSrcBits[src_index];src_index += counter +1;goto end_of_line;}}// now pSrcBits[src_index+counter] falls out of blockpRLEBits[dst_index++] = counter; // block length is (counter)pRLEBits[dst_index++] = pSrcBits[src_index];src_index += counter;goto state_start;state_no_compress:// src_index is the first pixel of a un-compressd block// un-compressed block need at least 3 pixel// counter is the number of additional pixels following currrent pixelfor (counter = 2; counter <= 254; counter++){if ( EndOfLine(src_index+counter) ) // reach end of line{pRLEBits[dst_index++] = 0; // escape character for un-compress blockpRLEBits[dst_index++] = counter + 1; // block length is (counter+1)for (i = counter + 1; i > 0; i--)pRLEBits[dst_index++] = pSrcBits[src_index++];if ( 0 != ((counter+1) % 2) ) // block length is oddpRLEBits[dst_index++]; // padd with a zero bytegoto end_of_line;}if ( EndOfLine(src_index+counter+1) ||pSrcBits[src_index+counter] != pSrcBits[src_index+counter+1] )continue; // still no 2 consecutive pixel are the same,// therefore continue to extend the un-compress block// we found two pixels are the sameif ( EndOfLine(src_index+counter+2) ||pSrcBits[src_index+counter+1] != pSrcBits[src_index+counter+2] )continue; // the third pixel is not the same, no need to exit un-compressed modeelse{ // // now pSrcBits[src_index+counter] and following 2 pixel are the same// now we need to exit the un-compressed modeif ( counter > 2)counter--; // we can shrink the block one pixel (go backward)pRLEBits[dst_index++] = 0; // escape character for un-compress blockpRLEBits[dst_index++] = counter+1; // block length is (counter+1)for (i = counter+1; i > 0; i--)pRLEBits[dst_index++] = pSrcBits[src_index++];if ( 0 != ((counter+1) % 2) ) // block length is oddpRLEBits[dst_index++]; // padd with a zero bytegoto state_compress;}} // end of for (counter = 2; counter <= 254; counter++)// now we have a full block of 255 pixelspRLEBits[dst_index++] = 0; // escape character for un-compress blockpRLEBits[dst_index++] = counter; // block length is (counter)for (i = counter; i > 0; i--)pRLEBits[dst_index++] = pSrcBits[src_index++];if ( 0 != ((counter) % 2) ) // block length is oddpRLEBits[dst_index++]; // padd with a zero bytegoto state_start;end_of_line:if ( 0 != (src_index % 4 )) // each scan line is dword align{int pad = 4 - (src_index%4);src_index += pad;}// src_index already point to the start of next linepRLEBits[dst_index++] = 0; // mark end-of-linepRLEBits[dst_index++] = 0;}pRLEBits[dst_index++] = 0; // mark end-of-bitmappRLEBits[dst_index++] = 1;RLE_size = dst_index; // image sizereturn 0;}</char> - February 25, 2008 at 1:27 am #3328AnonymousInactive
slam
thanks dear
u have helped me alot
u have done goood duty as being a goood pakistani programmer
i m also pakistani
so thanks alot! - February 25, 2008 at 4:14 am #3329AnonymousInactive
ok now my program is giving no errors and it is showing out put as follow
C12345sizeof Bitmap File Header=14size of Bitmap Image=2410sizeof Bitmap Info Header=40Height of this Bitmap Image=37Width of this Bitmap Image=34Now I want to modify such that it should dispaly size of compressed Bitmap
Now I want to show size of image after compressing on the screen but I don’t know how to do it
Out put should be as FollowBefore Compressing
C12345sizeof Bitmap File Header=14size of Bitmap Image=2410sizeof Bitmap Info Header=40Height of this Bitmap Image=37Width of this Bitmap Image=34After Compressing
C12345sizeof Bitmap File Header=14size of Bitmap Image=2410sizeof Bitmap Info Header=40Height of this Bitmap Image=37Width of this Bitmap Image=34Code of full program is as follow
I m using Dev C++ Compiler and this project is a basically Console program.C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210#include<conio.h>#include <iostream>#include <fstream>#include <windows.h>using namespace std;int CompressInRLE8(BYTE* pSrcBits, BYTE * pRLE_Bits, int& RLE_size);BOOL EndOfLine(int pos);int main(int argc, char *argv[]){BITMAPFILEHEADER bitmapFileHeader;BITMAPINFOHEADER bitmapInfoHeader;ifstream file("test.bmp", ios::in);if(!file){cerr<<"File can not be opened"<<endl;exit(1);}file.read(reinterpret_cast<char *> (&bitmapFileHeader),sizeof(BITMAPFILEHEADER));if(bitmapFileHeader.bfType != 19778 )cout <<" This is not a bitmap (.bmp) file ....";else{cout << "sizeof Bitmap File Header =" << sizeof(bitmapFileHeader) << endl;cout << "size of Bitmap Image =" << bitmapFileHeader.bfSize << endl;}file.read(reinterpret_cast<char *> (&bitmapInfoHeader),sizeof(BITMAPINFOHEADER));if( bitmapInfoHeader.biBitCount != 8){cout <<" This is not a 8 bit bitmap (.bmp) file ....";}else{cout << "sizeof Bitmap Info Header =" << sizeof(bitmapInfoHeader) << endl;cout << "Height of this Bitmap Image =" << bitmapInfoHeader.biHeight << endl;cout << "Widht of this Bitmap Image =" << bitmapInfoHeader.biWidth << endl;int numColors = 1<<bitmapInfoHeader.biBitCount;RGBQUAD rgbQuad[numColors];file.read(reinterpret_cast<char *> (&rgbQuad),sizeof(rgbQuad));}getche();}BOOL EndOfLine(int pos){DIBSECTION m_dib;if ( 0 != m_dib.dsBmih.biWidth % 4 ){int scanline_width = m_dib.dsBmih.biWidth;scanline_width = scanline_width + ( 4 - (scanline_width%4));pos %= scanline_width;}return 0 == ((pos+1)% m_dib.dsBmih.biWidth);}int CompressInRLE8(BYTE* pSrcBits, BYTE * pRLEBits, int& RLE_size){DIBSECTION m_dib;int line;int src_index = 0, dst_index = 0, counter, i;// in RLE8 every pixel is one bytefor ( line = 0; line < m_dib.dsBmih.biHeight; line++){state_start: // just at the start of a blockif ( EndOfLine(src_index)) // this is the last pixel of the line{pRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index];src_index++;goto end_of_line;}// now the block length is at least 2, look ahead to decide next state// next two same pixels are the same, enter compress modeif (pSrcBits[src_index] == pSrcBits[src_index+1])goto state_compress;if ( EndOfLine(src_index+1)) // the last 2 pixel of the line{ // these 2 pixel are not the samepRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index++];pRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index++];goto end_of_line;}// now the block length is at least 3// in next 3 pixels, the last 2 consecutive pixel are the sameif (pSrcBits[src_index+1] == pSrcBits[src_index+2]){pRLEBits[dst_index++] = 1; // block of length 1pRLEBits[dst_index++] = pSrcBits[src_index++];goto state_compress;}else // in these 3 pixel, no 2 consecutive pixel are the samegoto state_no_compress; // un-compressed block need at least 3 pixelstate_compress: // compress mode// src_index is the first pixel of a compressd block// counter is the number of additional pixels following currrent pixel// (src_index+counter) point to the last pixel of current blockfor ( counter = 1; counter <= 254; counter++){// must check this condition first, or a BUG occur!if ( pSrcBits[src_index+counter] != pSrcBits[src_index] )break;if ( EndOfLine(src_index+counter) ) // reach end of line{pRLEBits[dst_index++] = counter+1; // block length is (counter+1)pRLEBits[dst_index++] = pSrcBits[src_index];src_index += counter +1;goto end_of_line;}}// now pSrcBits[src_index+counter] falls out of blockpRLEBits[dst_index++] = counter; // block length is (counter)pRLEBits[dst_index++] = pSrcBits[src_index];src_index += counter;goto state_start;state_no_compress:// src_index is the first pixel of a un-compressd block// un-compressed block need at least 3 pixel// counter is the number of additional pixels following currrent pixelfor (counter = 2; counter <= 254; counter++){if ( EndOfLine(src_index+counter) ) // reach end of line{pRLEBits[dst_index++] = 0; // escape character for un-compress blockpRLEBits[dst_index++] = counter + 1; // block length is (counter+1)for (i = counter + 1; i > 0; i--)pRLEBits[dst_index++] = pSrcBits[src_index++];if ( 0 != ((counter+1) % 2) ) // block length is oddpRLEBits[dst_index++]; // padd with a zero bytegoto end_of_line;}if ( EndOfLine(src_index+counter+1) ||pSrcBits[src_index+counter] != pSrcBits[src_index+counter+1] )continue; // still no 2 consecutive pixel are the same,// therefore continue to extend the un-compress block// we found two pixels are the sameif ( EndOfLine(src_index+counter+2) ||pSrcBits[src_index+counter+1] != pSrcBits[src_index+counter+2] )continue; // the third pixel is not the same, no need to exit un-compressed modeelse{ // // now pSrcBits[src_index+counter] and following 2 pixel are the same// now we need to exit the un-compressed modeif ( counter > 2)counter--; // we can shrink the block one pixel (go backward)pRLEBits[dst_index++] = 0; // escape character for un-compress blockpRLEBits[dst_index++] = counter+1; // block length is (counter+1)for (i = counter+1; i > 0; i--)pRLEBits[dst_index++] = pSrcBits[src_index++];if ( 0 != ((counter+1) % 2) ) // block length is oddpRLEBits[dst_index++]; // padd with a zero bytegoto state_compress;}} // end of for (counter = 2; counter <= 254; counter++)// now we have a full block of 255 pixelspRLEBits[dst_index++] = 0; // escape character for un-compress blockpRLEBits[dst_index++] = counter; // block length is (counter)for (i = counter; i > 0; i--)pRLEBits[dst_index++] = pSrcBits[src_index++];if ( 0 != ((counter) % 2) ) // block length is oddpRLEBits[dst_index++]; // padd with a zero bytegoto state_start;end_of_line:if ( 0 != (src_index % 4 )) // each scan line is dword align{int pad = 4 - (src_index%4);src_index += pad;}// src_index already point to the start of next linepRLEBits[dst_index++] = 0; // mark end-of-linepRLEBits[dst_index++] = 0;}pRLEBits[dst_index++] = 0; // mark end-of-bitmappRLEBits[dst_index++] = 1;RLE_size = dst_index; // image size}so help me to do that as quick as possible so that I can do that project very quickly Waiting for ur immediate response.
- February 25, 2008 at 9:20 am #3330AnonymousInactive
kindly reply me soon as possible becoz tommorow is last date of submision of my project
- February 25, 2008 at 9:43 am #3331AnonymousInactive
can u give me ur personal mail address
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.