Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. c libraries needed to deal with bitmaps

c libraries needed to deal with bitmaps

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
32 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #1

    I fount a class to read and write bitmaps, it`s old source code that uses MFC. it uses CFile to read and write, when I try to compile I get a

    Quote:

    WINDOWS.H already included

    message. Is there a way to work around the problem?

    L L 2 Replies Last reply
    0
    • C Calin Negru

      I fount a class to read and write bitmaps, it`s old source code that uses MFC. it uses CFile to read and write, when I try to compile I get a

      Quote:

      WINDOWS.H already included

      message. Is there a way to work around the problem?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      fearless_ wrote:

      Is there a way to work around the problem?

      Yes, remove the duplicate include statement.

      C 2 Replies Last reply
      0
      • L Lost User

        fearless_ wrote:

        Is there a way to work around the problem?

        Yes, remove the duplicate include statement.

        C Offline
        C Offline
        Calin Negru
        wrote on last edited by
        #3

        thanks, with a simple tweak at headers it worked

        1 Reply Last reply
        0
        • L Lost User

          fearless_ wrote:

          Is there a way to work around the problem?

          Yes, remove the duplicate include statement.

          C Offline
          C Offline
          Calin Negru
          wrote on last edited by
          #4

          Would you mind helping me further? I loaded the bitmap, however when I check the values from the array into which the bitmap is loaded they show up as 0 (null).

          L V 2 Replies Last reply
          0
          • C Calin Negru

            Would you mind helping me further? I loaded the bitmap, however when I check the values from the array into which the bitmap is loaded they show up as 0 (null).

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Really. And we are supposed to guess what code and where the error occurs?

            C 1 Reply Last reply
            0
            • C Calin Negru

              Would you mind helping me further? I loaded the bitmap, however when I check the values from the array into which the bitmap is loaded they show up as 0 (null).

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #6

              fearless_ wrote:

              Would you mind helping me further? I loaded the bitmap, however when I check the values from the array into which the bitmap is loaded they show up as 0 (null).

              1. How did you load the bitmap? 2. How did you "check the values from the array into which the bitmap is loaded"? 3. Did you try to open this bitmap with some graphic editor/viewer?

              C 1 Reply Last reply
              0
              • L Lost User

                Really. And we are supposed to guess what code and where the error occurs?

                C Offline
                C Offline
                Calin Negru
                wrote on last edited by
                #7

                here is my code

                StringCchPrintfA(pszFilename,1024,"hello.bmp");
                // Attempt to open the Dib file for reading.
                if( !cf.Open( pszFilename, CFile::modeRead ) )
                {
                StringCchPrintfA(message,1024,"error reading file");
                MessageBox(NULL, message, "Textures.exe", MB_OK);
                return( FALSE );
                }

                // Get the size of the file and store
                // in a local variable. Subtract the
                // size of the BITMAPFILEHEADER structure
                // since we won't keep that in memory.
                DWORD dwDibSize= cf.GetLength() - sizeof( BITMAPFILEHEADER );
                int bsize =0;
                //dwDibSize =
                bsize=	cf.GetLength() - sizeof( BITMAPFILEHEADER );
                StringCchPrintfA(message,1024,"bitmap size  %d",bsize);
                MessageBox(NULL, message, "Textures.exe", MB\_OK);
                
                // Attempt to allocate the Dib memory.
                unsigned char \*pDib;
                pDib = new unsigned char \[dwDibSize\];
                if( pDib == NULL )
                {
                		StringCchPrintfA(message,1024,"error allocating array ");
                			MessageBox(NULL, message, "Textures.exe", MB\_OK);
                			return( FALSE );
                }
                	
                
                BITMAPFILEHEADER BFH;
                
                // Read in the Dib header and data.
                try{
                
                	// Did we read in the entire BITMAPFILEHEADER?
                	if( cf.Read( &BFH, sizeof( BITMAPFILEHEADER ) )
                		!= sizeof( BITMAPFILEHEADER ) ||
                
                		// Is the type 'MB'?
                		BFH.bfType != 'MB' ||
                
                		// Did we read in the remaining data?
                		cf.Read( pDib, dwDibSize ) != dwDibSize )
                		{
                			StringCchPrintfA(message,1024,"error processing the file ");
                			MessageBox(NULL, message, "Textures.exe", MB\_OK);
                		// Delete the memory if we had any
                		// errors and return FALSE.
                		delete \[\] pDib;
                		return( FALSE );
                		}
                	}
                
                L 1 Reply Last reply
                0
                • C Calin Negru

                  here is my code

                  StringCchPrintfA(pszFilename,1024,"hello.bmp");
                  // Attempt to open the Dib file for reading.
                  if( !cf.Open( pszFilename, CFile::modeRead ) )
                  {
                  StringCchPrintfA(message,1024,"error reading file");
                  MessageBox(NULL, message, "Textures.exe", MB_OK);
                  return( FALSE );
                  }

                  // Get the size of the file and store
                  // in a local variable. Subtract the
                  // size of the BITMAPFILEHEADER structure
                  // since we won't keep that in memory.
                  DWORD dwDibSize= cf.GetLength() - sizeof( BITMAPFILEHEADER );
                  int bsize =0;
                  //dwDibSize =
                  bsize=	cf.GetLength() - sizeof( BITMAPFILEHEADER );
                  StringCchPrintfA(message,1024,"bitmap size  %d",bsize);
                  MessageBox(NULL, message, "Textures.exe", MB\_OK);
                  
                  // Attempt to allocate the Dib memory.
                  unsigned char \*pDib;
                  pDib = new unsigned char \[dwDibSize\];
                  if( pDib == NULL )
                  {
                  		StringCchPrintfA(message,1024,"error allocating array ");
                  			MessageBox(NULL, message, "Textures.exe", MB\_OK);
                  			return( FALSE );
                  }
                  	
                  
                  BITMAPFILEHEADER BFH;
                  
                  // Read in the Dib header and data.
                  try{
                  
                  	// Did we read in the entire BITMAPFILEHEADER?
                  	if( cf.Read( &BFH, sizeof( BITMAPFILEHEADER ) )
                  		!= sizeof( BITMAPFILEHEADER ) ||
                  
                  		// Is the type 'MB'?
                  		BFH.bfType != 'MB' ||
                  
                  		// Did we read in the remaining data?
                  		cf.Read( pDib, dwDibSize ) != dwDibSize )
                  		{
                  			StringCchPrintfA(message,1024,"error processing the file ");
                  			MessageBox(NULL, message, "Textures.exe", MB\_OK);
                  		// Delete the memory if we had any
                  		// errors and return FALSE.
                  		delete \[\] pDib;
                  		return( FALSE );
                  		}
                  	}
                  
                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  And what is the problem and where does it occur? And please use proper <pre> tags so your code is formatted properly. The code button at the top of the edit box provides proper formatting for most languages.

                  C 2 Replies Last reply
                  0
                  • L Lost User

                    And what is the problem and where does it occur? And please use proper <pre> tags so your code is formatted properly. The code button at the top of the edit box provides proper formatting for most languages.

                    C Offline
                    C Offline
                    Calin Negru
                    wrote on last edited by
                    #9

                    At the end of code posted above I try to do this:

                    StringCchPrintfA(message,1024,"dib at 34 %d",pDib[34]);
                    MessageBox(NULL, message, "Textures.exe", MB_OK);

                    I expect pDib[34] to be 255 since the bitmap is 10 white pixels ( in a row) 40 bytes in total.

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      And what is the problem and where does it occur? And please use proper <pre> tags so your code is formatted properly. The code button at the top of the edit box provides proper formatting for most languages.

                      C Offline
                      C Offline
                      Calin Negru
                      wrote on last edited by
                      #10

                      Richard I appreciate your willingness to help

                      1 Reply Last reply
                      0
                      • V Victor Nijegorodov

                        fearless_ wrote:

                        Would you mind helping me further? I loaded the bitmap, however when I check the values from the array into which the bitmap is loaded they show up as 0 (null).

                        1. How did you load the bitmap? 2. How did you "check the values from the array into which the bitmap is loaded"? 3. Did you try to open this bitmap with some graphic editor/viewer?

                        C Offline
                        C Offline
                        Calin Negru
                        wrote on last edited by
                        #11

                        Victor thanks for offering to help, take a look at my reply to Richard

                        1 Reply Last reply
                        0
                        • C Calin Negru

                          At the end of code posted above I try to do this:

                          StringCchPrintfA(message,1024,"dib at 34 %d",pDib[34]);
                          MessageBox(NULL, message, "Textures.exe", MB_OK);

                          I expect pDib[34] to be 255 since the bitmap is 10 white pixels ( in a row) 40 bytes in total.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          I think you are missing something in your code. You read the BITMAPFILEHEADER but then assume that the remaining data is the actual bitmap, whereas the BITMAPFILEHEADER is followed by another header as described at BITMAPFILEHEADER (wingdi.h) - Win32 apps | Microsoft Docs[^].

                          C 1 Reply Last reply
                          0
                          • L Lost User

                            I think you are missing something in your code. You read the BITMAPFILEHEADER but then assume that the remaining data is the actual bitmap, whereas the BITMAPFILEHEADER is followed by another header as described at BITMAPFILEHEADER (wingdi.h) - Win32 apps | Microsoft Docs[^].

                            C Offline
                            C Offline
                            Calin Negru
                            wrote on last edited by
                            #13

                            yeah I was a bit in a hurry, I was expecting to find pixels right after the first header.

                            L 1 Reply Last reply
                            0
                            • C Calin Negru

                              yeah I was a bit in a hurry, I was expecting to find pixels right after the first header.

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              It is all explained at Bitmap Header Types - Win32 apps | Microsoft Docs[^]. But take special care of the different header types that may exist.

                              C 1 Reply Last reply
                              0
                              • C Calin Negru

                                I fount a class to read and write bitmaps, it`s old source code that uses MFC. it uses CFile to read and write, when I try to compile I get a

                                Quote:

                                WINDOWS.H already included

                                message. Is there a way to work around the problem?

                                L Offline
                                L Offline
                                leon de boer
                                wrote on last edited by
                                #15

                                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

                                V 1 Reply Last reply
                                0
                                • L leon de boer

                                  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

                                  V Offline
                                  V Offline
                                  Victor Nijegorodov
                                  wrote on last edited by
                                  #16

                                  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.

                                  L 1 Reply Last reply
                                  0
                                  • V Victor Nijegorodov

                                    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.

                                    L Offline
                                    L Offline
                                    leon de boer
                                    wrote on last edited by
                                    #17

                                    Yep sorry needed more coffee last night, clearly :-)

                                    In vino veritas

                                    1 Reply Last reply
                                    0
                                    • L Lost User

                                      It is all explained at Bitmap Header Types - Win32 apps | Microsoft Docs[^]. But take special care of the different header types that may exist.

                                      C Offline
                                      C Offline
                                      Calin Negru
                                      wrote on last edited by
                                      #18

                                      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

                                      L 1 Reply Last reply
                                      0
                                      • C Calin Negru

                                        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

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #19

                                        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.

                                        C 1 Reply Last reply
                                        0
                                        • L Lost User

                                          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.

                                          C Offline
                                          C Offline
                                          Calin Negru
                                          wrote on last edited by
                                          #20

                                          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

                                          L 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups