NO

Author Topic: How to render 32 Bit images?  (Read 9171 times)

Fred_Pyo

  • Guest
How to render 32 Bit images?
« on: April 05, 2007, 06:22:47 AM »
Hello,

I am aware that when rendering a bitmap I can make one color of the bitmap fully transparent. And this has been generally enough for my GUI needs. This time, I need (actually, WANT) to render transparent bitmaps, say PNG files with an alpha map.

I read through the SDK, and I found a couple of articles about PNG, but they don't contain any example source code on loading PNG files, which I guess is not as direct as loading a BMP file (I already tried using the LoadImage (...) function... nothing happens... ).

I also found out about the AlphaBlend function, which is similar to BitBlt but has alpha support, although if I am not mistaken, you can only specify ONE alpha value for the whole image.

So, the question is: Can someone give me any example code or any tips on how to work with PNG files (or whatever other 32 bit images)?

Thank you very much for the help!

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: How to render 32 Bit images?
« Reply #1 on: April 05, 2007, 09:37:30 AM »
Whichever format of images you use you must transform them to a bitmap before you can render them. And this is what the image format libraries do.
If you get the PNG library (free open source) you will see that there are functions to read the image from the file, or a resource where it is stored in the same file format, and functions to convert to a suitable bitmap for rendering. This is because the screen resource is a bitmap, and to show something you have to convert to a bitmap.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: How to render 32 Bit images?
« Reply #2 on: April 05, 2007, 04:18:25 PM »
Untested, but there is also the BI_PNG compression type (BITMAPINFOHEADER?, BITMAPV4HEADER, BITMAPV5HEADER). Perhaps possible to load the entire PNG file into a private buffer, and then use something like StretchDIBits(). My Platform SDK Help contains a partial example for a JPEG file, but the PNG format should work too. But as I said, I have never tried it...
/Pelle

Fred_Pyo

  • Guest
Re: How to render 32 Bit images?
« Reply #3 on: April 05, 2007, 07:27:59 PM »
I will look further on the SDK and see how to implement that BITMAPINFOHEADER stuff, and if that fails, I'll simply get that PNG library to solve my problem.

Thanks!

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: How to render 32 Bit images?
« Reply #4 on: April 06, 2007, 12:24:03 AM »
John Findlay has some GDI+ examples at his site including saving PNG.
Take a look at the Pelles C examples at http://www.johnfindlay.plus.com/
---
Stefan

Proud member of the UltraDefrag Development Team

zeph

  • Guest
Re: How to render 32 Bit images?
« Reply #5 on: April 21, 2007, 10:55:47 AM »
this is not coded by me, i found this somewhere. and it working with PNGlib - a free PNG decoder library (beta) http://www.madwizard.org/view.php?page=downloads

Code: [Select]
#ifndef WS_EX_LAYERED
#define WS_EX_LAYERED 0x00080000
#endif


#ifndef LWA_ALPHA
#define LWA_ALPHA 2
#endif

int MakeDialogTransparent (HWND _handle,int _transvalue)
{
    typedef BOOL (WINAPI* lpfn)(HWND,COLORREF,int,DWORD);
    lpfn _SetLayeredWindowAttributes;
    long style;

    _SetLayeredWindowAttributes=(lpfn)GetProcAddress((GetModuleHandle("user32.dll")),"SetLayeredWindowAttributes");
    if (_SetLayeredWindowAttributes!=0)
    {
        style=WS_EX_LAYERED|(GetWindowLong(_handle,GWL_EXSTYLE));
        if (SetWindowLong(_handle,GWL_EXSTYLE,style)!=0)
        {
            if (_SetLayeredWindowAttributes(_handle,0,_transvalue,LWA_ALPHA)!=0)
            {
                return (1);
            }
        }
    }
    return (0);
}

Code: [Select]
case WM_PAINT:
MakeDialogTransparent(hWnd,220); // 220 is transparency value


benjaminmaggi

  • Guest
Re: How to render 32 Bit images?
« Reply #6 on: April 09, 2008, 05:15:52 AM »
I've faced the same problem may times.
You might use the OleLoadPicture function (this is actually a COM interface form a component from IE, it can load many file types like JPG and PNG), examples can be found on pellesc web page & www.johnfindlay.plus.com.
The problem with this COM is that the target machine has to have IE > 4.0, and I don't really think I supports 8bit transparency.
Another way is to use CGI+ (IMHO pure rubbish, stands in the middle of GDI and the W.VISTA (cant remember the name...) it can be installed on win98 and comes with winXp, it can display PNG and JPG, supports transparency, but you have to install the libs and .H's lots of weird function names, not really easy to work with.
Third option, my personal choice, FreeImage library, comes with sources support LOTS of formats and transparency, really easy to use.

I would not rely on BITMAPINFOHEADER compression, it just doesn't make much sense and BTW BITMAPV5HEADER is not supported on all windows versions.
The AlphaBlend function supports 8bit transparency, cute but it's just to problematic, you have to go through every color byte, do the stupid multiplication, shift the transparency bit to MSB and then work your way, at least 2 nested for's imagine that for big images, not good.

The png lib is a bitch to compile, you have to download the zlib first, and then work your way to get it working, have a look at freeimage lib first.