MainExamplesDocumentationRegistration  


 Info
What is ShotGraph
Why use ShotGraph
Download
How to register
FAQ


 Making images
Common principles
Palettes
OLE support
ShotGraph on 64-bit Windows
Creating animations
Trick: anti-aliased fonts
 

Palettes

The image contained in the ShotGraph's imagespace is always a true color image. However, there are some cases when you need to get the image with limited number of colors (GIF or WBMP image). GIF file can have up to 256 colors, WBMP file is a B/W image. Therefore, in such cases an image needs to be converted from true color form to paletted one.

The easiest way to get paletted image is to define several indexed colors using the SetColor method and then use only those colors for building image. In this case you are limited to use only predefined shapes (line, ellipse and others) and text for creating your image context.

For example, you created image with 8 indexed colors and use the simple lines, rectangles and non-antialiased text to draw the image content, and then call the GifImage method to create a gif file. In such case ShotGraph makes this conversion automatically when you call the GifImage function. In the process of conversion, all color indexes earlier defined by SetColor will be mapped into result gif image palette entries.

However, in many situations the number of individual colors in an image is very high and real colors of the image are unknown or don't match the indexed colors defined by you. Such situation can occur in the following cases:

You have used the ReadImage function to read the image file into ShotGraph imagespace. The image can be a true-color or palettized with palette different from one of current image
You used drawing (text, for example) with anti-aliased effect. The antialiasing requires additional colors to draw the half-tone gradations.
You use image resizing (the Stretch or Resize method)
You use semi-transparency, grayscale and some other features.


In this situation use either BuildPalette or ApplyPalette method to convert your true color image to paletted one.

BuildPalette
This method recalculates palette entries from image data and alter image in accordance with them. As a rule, the method is called once just before GifImage (or similar method creating a paletted image). For example, if you want to convert Jpeg image (true color) to Gif format, then after reading jpeg image into imagespace you need to call the BuildPalette once just before GifImage method.

ApplyPalette
This method alters image data in imagespace in accordance with specified colors. The colors should be predefined by SetColor calls. These palette entries are not altered. Using this feature you can convert your image to BW and, in common case, to any bi-, three-, four- etc. color image with predefined colors. As a rule, the method is called once just before GifImage (or similar method creating a paletted image).

Palette size

Palette size is the number of indexed colors in the image. The maximum size of palette is 256. The minimum size of palette is 2. In the consequence of specific limitations of GIF file format, the palette size must be a power of 2. So possible values are 2, 4, 8, 16, 32, 64, 128, 256.

Palette index

Palette index is zero based. For example, if you creates the image with 64 colors, the palette index mush be between and including 0 and 63. To define the palette entry with specific color use the SetColor function.

Examples

The following example shows the effect of different methods (BuildPalette, ApplyPalette). In case of BuildPalette we use the palette size 16 (to make the changes more visible).

Original image
 
Degrade number of colors to 16

CreateImage xsize,ysize,16
ReadImage file_path,pal,0,0
' Now the imagespace contains the original image in true color
BuildPalette 0
GifImage...
' New 16-color image is ready
 
Creating bi-color image without dithering

CreateImage xsize,ysize,16
ReadImage file_path,pal,0,0
' Now the imagespace contains the original image in true color
SetColor 0,0,0,0
SetColor 1,255,255,255
ApplyPalette False
GifImage... (or WbmpImage)
' New bi-color is image ready
 
Creating bi-color image with dithering

CreateImage xsize,ysize,16
ReadImage file_path,pal,0,0
' Now the imagespace contains the original image in true color
SetColor 0,0,0,0
SetColor 1,255,255,255
ApplyPalette True
GifImage... (or WbmpImage)
' New bi-color is image ready

See too

CreateImage, SetColor, BuildPalette, ApplyPalette functions in the Methods & properties.

© 1998-2001 Mikhail Tchikalov
Email to contact: mtchikalov@usa.com