It's very common to need to draw some shapes, texts or to apply some effects to images.
But GDI+ has a limitation when working with images created in indexed pixel formats, such as 1bppindexed (0x00030101), 4bppindexed (0x00030402) and 8bppindexed (0x00030803). these are the pixel formats used by gif (graphics interchange format) files too. if you try to draw on these kind of images you may receive an error message like this :

A very simple solution is to load the gif using gpimage, and retrieve some properties, like width and height. then create a new and empty bitmap object with the same size from the original gif, but using a non indexed pixel format. _gdiplus.vcx uses pixelformat_32bpppargb as default.

The next step is to "draw" the original gif image on the new created bitmap in the just created non indexed pixel format image using the procedure drawimage. now you can draw freely on this new image.

Finally, you can save the new image as gif or any other type.
An important detail that you must know is that when you tell gdi+ to save as gif, it will automatically convert the image again to 8bppindexed pixel format, that is the default for gdi+.

Here's a sample code that does that, drawing 2 ellipses and a rectangle on a gif.

#define gdiplus_pixelformat_1bppindexed 0x00030101
#define gdiplus_pixelformat_4bppindexed 0x00030402
#define gdiplus_pixelformat_8bppindexed 0x00030803
#define gdiplus_pixelformat_16bppgrayscale 0x00101004
#define gdiplus_pixelformat_16bpprgb555 0x00021005
#define gdiplus_pixelformat_16bpprgb565 0x00021006
#define gdiplus_pixelformat_16bppargb1555 0x00061007
#define gdiplus_pixelformat_24bpprgb 0x00021808
#define gdiplus_pixelformat_32bpprgb 0x00022009
#define gdiplus_pixelformat_32bppargb 0x0026200a
#define gdiplus_pixelformat_32bpppargb 0x000e200b
#define gdiplus_pixelformat_48bpprgb 0x0010300c

local lcsource, lcdest
lcsource = getpict("gif")
if empty(lcsource)

lcdest = addbs(justpath(lcsource))+ "_" +;


*** load image and check if indexed :
loimage as gpimage of ffc/_gdiplus.vcx
loimage = newobject('gpimage',home() + 'ffc/_gdiplus.vcx')
wimg = loimage.imagewidth
himg = loimage.imageheight
lcpixformat = getpixformatname(loimage.pixelformat)
if not "indexed" $ upper(lcpixformat)
   messagebox("draw directly on the image, cause it's not" + ;
      "of an indexed pixel format !",64, "use common technique")

*** create new bitmap with same dimensions :
lobitmap as gpbitmap of ffc/_gdiplus.vcx
lobitmap = newobject("gpbitmap", home() + "ffc/_gdiplus.vcx")
local lograph as gpgraphics of home() + ffc/_gdiplus.vcx
lograph = newobject('gpgraphics', home() + "ffc/_gdiplus.vcx")
lobitmap.create(wimg, himg, gdiplus_pixelformat_16bpprgb555 )

*** paste original image to the new created :
lograph.drawimagescaled(loimage, 0, 0, wimg, himg)

*** now we can draw anything we want to the image :
local loblue, lored, logreen as gpcolor of ffc/_gdiplus.vcx
local lopen as gppen of home() + ffc/_gdiplus.vcx

loblue = newobject("gpcolor", home() + "ffc/_gdiplus.vcx","",;
40,40,240 )
lored = newobject("gpcolor", home() + "ffc/_gdiplus.vcx","",;
240,40,40 )
logreen = newobject("gpcolor", home() + "ffc/_gdiplus.vcx","",;
40,230,40 )


lopen = newobject("gppen", home() + "ffc/_gdiplus.vcx")
lopen.create(loblue, 12)
lograph.drawellipse( lopen, 0, 0, wimg, himg)

lopen.pencolor = lored
lograph.drawellipse( lopen, 0+12, 0+12 , wimg-24 , himg-24)

lopen.pencolor = logreen
lograph.drawrectangle( lopen, 0+30, 0+30 , wimg-60 , himg-60)


*** save image in gif and jpg :


procedure getpixformatname(npix)
do case
npix = 0x00030101
   return "1bppindexed"
case npix = 0x00030402
   return "4bppindexed"
case npix = 0x00030803
   return "8bppindexed"
case npix = 0x00101004
   return "16bppgrayscale"
case npix = 0x00021005
   return "16bpprgb555"
case npix = 0x00021006
   return "16bpprgb565"
case npix = 0x00061007
   return "16bppargb1555"
case npix = 0x00021808
   return "24bpprgb"
case npix = 0x00022009
   return "32bpprgb"
case npix = 0x0026200a
   return "32bppargb"
case npix = 0x000e200b
   return "32bpppargb"
case npix = 0x0010300c
   return "48bpprgb"
case npix = 0x001c400e
   return "64bpppargb"
   return "unidentified"


This procedure will cause a big inconvenient, because in this automatic conversion, gdi+ writes the file by using a halftone palette to which the image object's bits have been color reduced. gdi+ does a color conversion from 32 bits-per-pixel (32 bpp) when it writes the image to the file.
According to szaak priester, "when gdi+ saves a bitmap in gif format, it performs a very crude form of color quantization. it always uses the same color palette, mostly filled with the 216 'web-safe colors.' in the early years of the internet, these colors were the only ones to be displayed consistently by most browsers; hence the name. ... on top of that, the 216 web-safe colors were chosen purely on the basis of their technical merits (they uniformly divide the rgb color space), and not because of their visual qualities. as a consequence, the web-safe palette (also called the 'halftone palette') contains many almost indiscernible purples and a lot of muddy greenish and brownish colors, whereas some more useable parts of the spectrum are seriously underpopulated."
Below you can see the result obtained using the technique discussed here.


1000.107.1304._flower 1000.107.1305._flower

gif - original image

gif - 8bppindexed image

jpeg - 32bppargb


Pay attention to the differences of quality between the three images.
This technique can be used also to resize any image, including gifs. for resizing purposes, all you need to change in the previous code are the instructions that deal with the image size, lobitmap.create(newwidth, newheight, pixelformat) and when pasting the original image to the new created bitmap, lograph.drawimageportionat(looriginalimage, 0, 0, newwidth, newheight).
You just need to know the gifs limitations, and decide if you will save it as gif or as another image format.


Here are some differences between gifs and jpegs, according to msdn :


Should you store images in gif format or jpeg format? although this question doesn't relate directly to palette management, it confuses a lot of people, and it is somewhat relevant to our subject matter.

There are two major differences between gif and jpeg images:

gif images are compressed in a way that preserves all data, while jpeg images are compressed in a way that loses some data.
gif images are limited to a maximum of 256 colors, while jpeg images aren't limited in the number of colors they use.


But is there a way to convert an image to gif format without using a better distributed color pallete instead of the the halftone pallete that deteriorates the pictures ? of course, but that's for another post...

1 comment:

  1. Versión en Español de este artículo en / Spanish version at http://www.portalfox.com/article.php?sid=2244