Home › Forums › C Programming › Errors in my program › Reply To: Errors in my program
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.
Because
1 2 3 | int 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.
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | #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 byte for ( line = 0; line < m_dib.dsBmih.biHeight; line++) { state_start: // just at the start of a block if ( EndOfLine(src_index)) // this is the last pixel of the line { pRLEBits[dst_index++] = 1; // block of length 1 pRLEBits[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 mode if (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 same pRLEBits[dst_index++] = 1; // block of length 1 pRLEBits[dst_index++] = pSrcBits[src_index++]; pRLEBits[dst_index++] = 1; // block of length 1 pRLEBits[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 same if (pSrcBits[src_index+1] == pSrcBits[src_index+2]) { pRLEBits[dst_index++] = 1; // block of length 1 pRLEBits[dst_index++] = pSrcBits[src_index++]; goto state_compress; } else // in these 3 pixel, no 2 consecutive pixel are the same goto state_no_compress; // un-compressed block need at least 3 pixel state_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 block for ( 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 block pRLEBits[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 pixel for (counter = 2; counter <= 254; counter++) { if ( EndOfLine(src_index+counter) ) // reach end of line { pRLEBits[dst_index++] = 0; // escape character for un-compress block pRLEBits[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 odd pRLEBits[dst_index++]; // padd with a zero byte goto 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 same if ( 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 mode else { // // now pSrcBits[src_index+counter] and following 2 pixel are the same // now we need to exit the un-compressed mode if ( counter > 2) counter--; // we can shrink the block one pixel (go backward) pRLEBits[dst_index++] = 0; // escape character for un-compress block pRLEBits[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 odd pRLEBits[dst_index++]; // padd with a zero byte goto state_compress; } } // end of for (counter = 2; counter <= 254; counter++) // now we have a full block of 255 pixels pRLEBits[dst_index++] = 0; // escape character for un-compress block pRLEBits[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 odd pRLEBits[dst_index++]; // padd with a zero byte goto 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 line pRLEBits[dst_index++] = 0; // mark end-of-line pRLEBits[dst_index++] = 0; } pRLEBits[dst_index++] = 0; // mark end-of-bitmap pRLEBits[dst_index++] = 1; RLE_size = dst_index; // image size return 0; } </char> |