c libraries needed to deal with bitmaps
-
You are messing around with stuff you do not have to do on Windows and there is no advantage in doing what you are doing other than learning. You can load a bitmap with one line of code using the API LoadBitmap() LoadBitmapA function (winuser.h) - Win32 apps | Microsoft Docs[^] One line of code will load it for you and it doesn't care what the bitmap format is.
HBITMAP MyBmp = LoadBitmap(0, "Yourbitmapname.bmp");
Once you have a HBITMAP (handle to a bitmap) in Windows you can basically do everything with it. If you want the details from the bitmap once you have it loaded you just ask windows to extract them for you So with my handle above I can extract the header with 2 lines of code the 3rd line is just to throw the details out (it assumes you are doing a console app).
BITMAP bm;
GetObject(MyBmp, sizeof(bm), &bm);
printf("Bitmap is wth: %u ht: %u bitdepth: %u\r\n", bm.bmWidth, bm.bmHeight, bm.bmBitsPixel);So I can load the bitmap and extract the header with 3 lines in absolute safety. If you are messing around with the actual header etc in Windows you are doing it all wrong :-) The only time you might play with a BMP file header is writing a BMP in a specific format. That is why there is no need for libraries on Windows to handle bitmaps. Now JPEGS are a little trickier it's about 20 lines of code to get windows to load one of those most of the issue the filename has to be in UNICODE. Let me know if you need to know how to do it.
In vino veritas
leon de boer wrote:
One line of code will load it for you and it doesn't care what the bitmap format is.
HBITMAP MyBmp = LoadBitmap(0, "Yourbitmapname.bmp");
You probaly meant the [LoadImageA function (winuser.h) - Win32 apps | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadimagea) function that can load the bitmap from the file. LoadBitmap only loads the bitmap from resources.
-
leon de boer wrote:
One line of code will load it for you and it doesn't care what the bitmap format is.
HBITMAP MyBmp = LoadBitmap(0, "Yourbitmapname.bmp");
You probaly meant the [LoadImageA function (winuser.h) - Win32 apps | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadimagea) function that can load the bitmap from the file. LoadBitmap only loads the bitmap from resources.
Yep sorry needed more coffee last night, clearly :-)
In vino veritas
-
It is all explained at Bitmap Header Types - Win32 apps | Microsoft Docs[^]. But take special care of the different header types that may exist.
I was only using a part of the bitmap processing code I was using as reference. I don`t need the palette part so I trimmed out a bit too much.
m_pDib = pDib;
m_dwDibSize = dwDibSize;// Pointer our BITMAPINFOHEADER and RGBQUAD // variables to the correct place in the Dib data. m\_pBIH = (BITMAPINFOHEADER \*) m\_pDib; m\_pPalette = (RGBQUAD \*) &m\_pDib\[sizeof(BITMAPINFOHEADER)\]; // get image width and height m\_width = m\_pBIH->biWidth; m\_height = m\_pBIH->biHeight; // Calculate the number of palette entries. m\_nPaletteEntries = 1 << m\_pBIH->biBitCount; if( m\_pBIH->biBitCount > 8 ) m\_nPaletteEntries = 0; else if( m\_pBIH->biClrUsed != 0 ) m\_nPaletteEntries = m\_pBIH->biClrUsed; // Point m\_pDibBits to the actual Dib bits data. m\_pDibBits = &m\_pDib\[sizeof(BITMAPINFOHEADER)+ m\_nPaletteEntries\*sizeof(RGBQUAD)\]; // If we have a valid palette, delete it. if( m\_Palette.GetSafeHandle() != NULL ) m\_Palette.DeleteObject();
I know I must go past the bitmapinfoheader to get to the pixels
-
I was only using a part of the bitmap processing code I was using as reference. I don`t need the palette part so I trimmed out a bit too much.
m_pDib = pDib;
m_dwDibSize = dwDibSize;// Pointer our BITMAPINFOHEADER and RGBQUAD // variables to the correct place in the Dib data. m\_pBIH = (BITMAPINFOHEADER \*) m\_pDib; m\_pPalette = (RGBQUAD \*) &m\_pDib\[sizeof(BITMAPINFOHEADER)\]; // get image width and height m\_width = m\_pBIH->biWidth; m\_height = m\_pBIH->biHeight; // Calculate the number of palette entries. m\_nPaletteEntries = 1 << m\_pBIH->biBitCount; if( m\_pBIH->biBitCount > 8 ) m\_nPaletteEntries = 0; else if( m\_pBIH->biClrUsed != 0 ) m\_nPaletteEntries = m\_pBIH->biClrUsed; // Point m\_pDibBits to the actual Dib bits data. m\_pDibBits = &m\_pDib\[sizeof(BITMAPINFOHEADER)+ m\_nPaletteEntries\*sizeof(RGBQUAD)\]; // If we have a valid palette, delete it. if( m\_Palette.GetSafeHandle() != NULL ) m\_Palette.DeleteObject();
I know I must go past the bitmapinfoheader to get to the pixels
Please use proper <pre> tags around code blocks (and proper indentation), not Quoted text. The following code sample is preceded by <pre lang="c++"> and followed by </pre>
// Calculate the number of palette entries.
m_nPaletteEntries = 1 << m_pBIH->biBitCount;
if( m_pBIH->biBitCount > 8 )
m_nPaletteEntries = 0;
else if( m_pBIH->biClrUsed != 0 )
m_nPaletteEntries = m_pBIH->biClrUsed;Much more readable.
-
Please use proper <pre> tags around code blocks (and proper indentation), not Quoted text. The following code sample is preceded by <pre lang="c++"> and followed by </pre>
// Calculate the number of palette entries.
m_nPaletteEntries = 1 << m_pBIH->biBitCount;
if( m_pBIH->biBitCount > 8 )
m_nPaletteEntries = 0;
else if( m_pBIH->biClrUsed != 0 )
m_nPaletteEntries = m_pBIH->biClrUsed;Much more readable.
I`m having trouble understanding how this code works. The problem is I`m not a c expert.
cf.Read( &BFH, sizeof( BITMAPFILEHEADER )
For instance how does the portion of data read from my file fits into a structure. It`s not a structure of identical items BITMAPFILEHEADER is a mix of WORDs and DWORDS
-
I`m having trouble understanding how this code works. The problem is I`m not a c expert.
cf.Read( &BFH, sizeof( BITMAPFILEHEADER )
For instance how does the portion of data read from my file fits into a structure. It`s not a structure of identical items BITMAPFILEHEADER is a mix of WORDs and DWORDS
The
sizeof
operator gives the number of bytes in the object referred to. So in your case it will be the size of aBITMAPFILEHEADER
, and the read operation will read the correct number of bytes into the structure. When complete each field of the structure should be correctly aligned*. *There are conditions where this may not be true, so beware if you have a structure containing single byte elements. You may ignore that for the moment. -
The
sizeof
operator gives the number of bytes in the object referred to. So in your case it will be the size of aBITMAPFILEHEADER
, and the read operation will read the correct number of bytes into the structure. When complete each field of the structure should be correctly aligned*. *There are conditions where this may not be true, so beware if you have a structure containing single byte elements. You may ignore that for the moment.thanks that makes sense. In the code above do I need m_nPaletteEntries at all? Is used to calculate the offset at which individual pixels are stored?
-
thanks that makes sense. In the code above do I need m_nPaletteEntries at all? Is used to calculate the offset at which individual pixels are stored?
-
You need to examine the different items in the info table as described at BITMAPINFOHEADER structure (Windows) | Microsoft Docs[^]. The values of the items related to colours will tell you whether a colour table is included or not.
I have to take a short break. Your insight (last post included) is priceless Richard, thanks
`DreamLand Page` is my projects facebook page.
-
I have to take a short break. Your insight (last post included) is priceless Richard, thanks
`DreamLand Page` is my projects facebook page.
-
Sorry to disappoint you, but my 'insight' is actually just a matter of reading the documentation.
the code you quoted is the bit that throws me into fog. What does the line that has the overload operator do?
-
the code you quoted is the bit that throws me into fog. What does the line that has the overload operator do?
-
m_nPaletteEntries = 1 << m_pBIH->biBitCount;
-
m_nPaletteEntries = 1 << m_pBIH->biBitCount;
This is a bitwise left-shift operation which is equivalent to
m_nPaletteEntries = pow(2, m_pBIH->biBitCount); // 2 to the power of bpp
What are shift operators in C++?[^] This trick is used quite often, since a bitwise shift operation is way quicker than the
pow()
method, which is rather intense on processing time. Edit: here's an interesting list of useful bitwise tricks: Bit Twiddling Hacks[^]"Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."
-
This is a bitwise left-shift operation which is equivalent to
m_nPaletteEntries = pow(2, m_pBIH->biBitCount); // 2 to the power of bpp
What are shift operators in C++?[^] This trick is used quite often, since a bitwise shift operation is way quicker than the
pow()
method, which is rather intense on processing time. Edit: here's an interesting list of useful bitwise tricks: Bit Twiddling Hacks[^]"Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."
-
For a left-shift by a single bit, yes, a multiplication by 2. But a left-shift by n bits, in the end, is the same as a multiplication by 2 to the power of n; since original value is 1, this leads to 2 to the power of n. Or am I missing something?
"Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."
-
For a left-shift by a single bit, yes, a multiplication by 2. But a left-shift by n bits, in the end, is the same as a multiplication by 2 to the power of n; since original value is 1, this leads to 2 to the power of n. Or am I missing something?
"Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."