Jump to content

RGB or RGBA struct from GIF image with GDIPlus


 Share

Recommended Posts

To create a RGB/RGBA struct from a BMP, JPG, PNG or TIF image with GDIPlus you can do something like this:

$hBitmapObj = _GDIPlus_ImageLoadFromFile( $imgFile )

$aPixelFormat = _GDIPlus_ImageGetPixelFormat( $hBitmapObj )
$iPixelFormat = $aPixelFormat[0]
; Inspect the pixel format
; Probably 24 Bpp RGB, 32 Bpp RGB or 32 Bpp ARGB

$bytes = $bytes_per_pixel ; 3 or 4
$width = _GDIPlus_ImageGetWidth( $hBitmapObj )
$height = _GDIPlus_ImageGetHeight( $hBitmapObj )
$size = $width * $height * $bytes

; Lock bitmap for reading
$tBitmapData = _GDIPlus_BitmapLockBits( $hBitmapObj, 0, 0, $width, $height, $GDIP_ILMREAD, $iPixelFormat )

; Create RGB/RGBA struct
$pScan0 = DllStructGetData( $tBitmapData, "Scan0" )
$image = DllStructCreate( "byte[" & $size & "]", $pScan0 )
$pImage = DllStructGetPtr( $image )

How do you create a RGB/RGBA struct from a GIF image?

The problem is that the pixel format for a GIF image is 8 Bpp Indexed. And with this format you can't just use _GDIPlus_BitmapLockBits() to get a RGB/RGBA struct. You have to convert the 8 Bpp Indexed format to e.g. 24 Bpp RGB.

But how do you do that?

Link to comment
Share on other sites

Create an empty bitmap using GdipCreateBitmapFromScan0 (see GDIP.au3) in 32 bit format ($GDIP_PXF32RGB) and copy the gif image to the bitmap.

#include <GDIPlus.au3>

_GDIPlus_Startup()
$hBitmapObj = _GDIPlus_ImageLoadFromFile("c:\Program Files (x86)\AutoIt3\Examples\GUI\logo4.gif")
PrintImageFormat($hBitmapObj)
$hBitmapObj_new = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", _GDIPlus_ImageGetWidth($hBitmapObj) , "int", _GDIPlus_ImageGetHeight($hBitmapObj) , "int", 0, "int", $GDIP_PXF24RGB, "ptr", 0, "int*", 0)
$hBitmapObj_new = $hBitmapObj_new[6]
$hCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmapObj_new)
_GDIPlus_GraphicsDrawImage($hCtxt, $hBitmapObj, 0, 0)
_GDIPlus_GraphicsDispose($hCtxt)
PrintImageFormat($hBitmapObj_new)
_GDIPlus_BitmapDispose($hBitmapObj)
_GDIPlus_BitmapDispose($hBitmapObj_new)
_GDIPlus_Shutdown()

Exit

Func PrintImageFormat($hBitmapObj)
    Local $aPixelFormat = _GDIPlus_ImageGetPixelFormat($hBitmapObj)
    Local $iPixelFormat = $aPixelFormat[0]
    Switch $iPixelFormat
        Case $GDIP_PXF01INDEXED
            ConsoleWrite("$GDIP_PXF01INDEXED" & @LF)
        Case $GDIP_PXF04INDEXED
            ConsoleWrite("$GDIP_PXF04INDEXED" & @LF)
        Case $GDIP_PXF04INDEXED
            ConsoleWrite("$GDIP_PXF04INDEXED" & @LF)
        Case $GDIP_PXF08INDEXED
            ConsoleWrite("$GDIP_PXF08INDEXED" & @LF)
        Case $GDIP_PXF16RGB555
            ConsoleWrite("$GDIP_PXF16RGB555" & @LF)
        Case $GDIP_PXF16RGB565
            ConsoleWrite("$GDIP_PXF16RGB565" & @LF)
        Case $GDIP_PXF16ARGB1555
            ConsoleWrite("$GDIP_PXF16ARGB1555" & @LF)
        Case $GDIP_PXF24RGB
            ConsoleWrite("$GDIP_PXF24RGB" & @LF)
        Case $GDIP_PXF32RGB
            ConsoleWrite("$GDIP_PXF32RGB" & @LF)
        Case $GDIP_PXF32ARGB
            ConsoleWrite("$GDIP_PXF32ARGB" & @LF)
        Case $GDIP_PXF32PARGB
            ConsoleWrite("$GDIP_PXF32PARGB" & @LF)
    EndSwitch
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...