CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Graphics Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Graphics Programming Discussion graphics programming using C++. Valid topics include OpenGL, DirectX, GDI/GDI+, Aero, and more.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 5th, 2009, 12:33 AM
    bitshifter420's Avatar
    bitshifter420 bitshifter420 is online now
    Member
     
    Join Date: Jun 2007
    Location: MA-USA
    Posts: 224
    bitshifter420 has a spectacular aura about (100+)bitshifter420 has a spectacular aura about (100+)
    32 bit DIB from 24 bit bitmap

    Hello, I have a Win32 GDI question...
    I need a way to make a 32 bit DIB from a 24 bit bitmap.
    I can load the bitmap with LoadImage but how to make it a 32 bit DIB?
    Any help would be greatly appreciated...
    Reply With Quote
      #2    
    Old November 5th, 2009, 03:41 AM
    olivthill2 olivthill2 is offline
    Member
     
    Join Date: Apr 2009
    Posts: 200
    olivthill2 has a spectacular aura about (150+)olivthill2 has a spectacular aura about (150+)
    Re: 32 bit DIB from 24 bit bitmap

    When your image is loaded, then use GetDIBits() with the argument containing the BITMAPINFO structure indicating you want a 32-bit bitmap, i.e. with bi.bmiHeader.biBitCount = 32:
    Code:
       BITMAPINFO bi;
    ...
       bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
       bi.bmiHeader.biWidth = picture_width;
       bi.bmiHeader.biHeight = - picture_height;
       bi.bmiHeader.biPlanes = 1;
       bi.bmiHeader.biBitCount = 32;
       bi.bmiHeader.biCompression = BI_RGB;
       bi.bmiHeader.biSizeImage = bi.bmiHeader.biWidth * 4 * bi.bmiHeader.biHeight * -1 * sizeof(unsigned char);
       bi.bmiHeader.biClrUsed = 0;
       bi.bmiHeader.biClrImportant = 0;
    ...
       if (!GetDIBits(hdc_mem, hbm, 0, picture_height, DIBits, &bi, DIB_RGB_COLORS)) {
    ...
    Google will probably give you more complete examples.
    Reply With Quote
      #3    
    Old November 5th, 2009, 03:27 PM
    bitshifter420's Avatar
    bitshifter420 bitshifter420 is online now
    Member
     
    Join Date: Jun 2007
    Location: MA-USA
    Posts: 224
    bitshifter420 has a spectacular aura about (100+)bitshifter420 has a spectacular aura about (100+)
    Re: 32 bit DIB from 24 bit bitmap

    Ok, i have come up with a simple solution.
    This is how to create an offscreen buffer (DIB32)
    and load a 24bit texture and make it DIB32 compatible.
    It seems i dont need to set the bmiHeader.biSizeImage value? Just zero it?
    Code:
       // First we need a device context that is compatible
       // with the desktop settings. (1024x768x32 on my PC)
    
       HDC BufferDC = CreateCompatibleDC(NULL);
    
       // Now we describle a format for the offscreen buffer.
    
       BITMAPINFO BufferInfo;
       ZeroMemory(&BufferInfo,sizeof(BITMAPINFO));
       BufferInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
       BufferInfo.bmiHeader.biWidth = GetSystemMetrics(SM_CXSCREEN);
       BufferInfo.bmiHeader.biHeight = GetSystemMetrics(SM_CYSCREEN);
       BufferInfo.bmiHeader.biPlanes = 1;
       BufferInfo.bmiHeader.biBitCount = 32;
       BufferInfo.bmiHeader.biCompression = BI_RGB;
    
       // Create a device independant bitmap for the offscreen buffer.
    
       HBITMAP BufferBitmap = CreateDIBSection(
          BufferDC,
          &BufferInfo,
          DIB_RGB_COLORS,
          (void**)&g_Buffer.first, // Store pointer to first pixel in this value.
          NULL,0); // No file mapping crap.
    
       // Describe the texture format.
       // Same as buffer format but with hardwired size.
    
       BITMAPINFO TextureInfo = BufferInfo;
       TextureInfo.bmiHeader.biWidth = 100;
       TextureInfo.bmiHeader.biHeight = 100;
    
       // Create device independant bitmap for texture.
    
       HBITMAP TextureBitmap = CreateDIBSection(
          BufferDC,
          &TextureInfo,
          DIB_RGB_COLORS,
          (void**)&g_Texture.first, // Store pointer to first pixel in this value.
          NULL,0); // No file mapping crap.
    
       // Load the 24bit image from file.
    
       HBITMAP hImage = (HBITMAP)LoadImage(
          NULL, // No HINSTANCE since its not a resource.
          "Image.bmp",
          IMAGE_BITMAP,
          TextureInfo.bmiHeader.biWidth,
          TextureInfo.bmiHeader.biHeight,
          LR_LOADFROMFILE);
    
       // Transfer the bitmap into the texture DIB.
    
       GetDIBits(
          BufferDC,
          hImage,
          0,                              // First scan line.
          TextureInfo.bmiHeader.biHeight, // Number of scan lines.
          g_Texture.first,                // Pointer to first pixel.
          &TextureInfo,                   // Address of BITMAPINFO with desired specs.
          DIB_RGB_COLORS);
    
       // Were done with the inage so...
    
       DeleteObject(hImage);
    
       // Select the buffer for rendering to.
    
       HBITMAP OldBitmap = (HBITMAP)SelectObject(BufferDC,BufferBitmap);
    
       // Dont forget to clean up later :)
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Graphics Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:51 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009