The CDib Class As Used By
|
This page shows the C++ code for a CDib class that can be used to create, manipulate and display bitmaps.
I've had enough! I want to go home
Take me back to the screen saver code
Show me how to draw the Plasma
The Plasma screen saver uses methods of my CDib class to prepare and display screen images. The methods allow creation and manipulation of Windows bitmaps, as well as creation and use of palettes and DIB sections.
The methods are :
makePicture | Creates a Windows bitmap of a specified size |
loadFromResource | Loads a Windows bitmap that has been incorporated as a bitmap resource. |
makeDibSection | Creates a Windows DIB section, which is a Windows bitmap with fast access to video memory and access to the bitmap pixels. |
makePalette | Creates a palette |
identityPalette | Converts a palette to be an identity palette, that is a palette
that is identical to the system palette. |
draw | Displays all or part of a bitmap or DIB section |
setBgColour | Specifies a background colour for a bitmap |
bgFill | Fills all or part of a bitmap with the background colour |
solidMerge | Copies all or part of a bitmap to another bitmap |
transpMerge | Copies all or part of a bitmap to another bitmap, but allowing a transparent colour to be selected. Pixels that have the transparent colour are not copied. |
maskMerge | Copies all or part of a bitmap to another bitmap, but
only copying a specified pattern of pixels. The pattern is specified as a mask. |
getBpp | Returns the colour depth of a bitmap, that is, how many bits per pixel. |
getNumOfColours | Returns the number of different colours that a bitmap actually
uses in its image. This only applies to 256-colour bitmaps. |
trimColours | Alters a bitmap's colour table so that unused colour entries are zeroed out (effectively making them black). |
addColours | Adds new colours from a bitmap's colour table to another bitmap's colour table. |
synchronize | Adjusts a bitmap's pixels and colour table so that its colour
table is identical with another bitmap. |
getCornerPixel | Returns the colour of the pixel in a specified corner of a bitmap. |
palettize | Changes a bitmap's pixels so that they relate directly to a
palette rather than the bitmap's colour table. Also makes the bitmap's colour table a 1-to-1 table, so that it just contains values 0 to 255 in sequence. Windows recognizes this as a signal that the pixels refer directly to palettes, and knows it can omit the step of matching the bitmap's colour table with the palette when displaying the image. |
You can download the source code for the CDib class here.
(There's too much to paste it into this page)
I've used 256-colour bitmaps for the Plasma screen saver. The advantages are that you know it will work with any screen colour mode (256-colour, high-colour, true-colour), and there are fewer bytes to be copied around (each pixel is only one byte long, instead of two or three). The disadvantages are that you have to manage the palettes, and that you are limited to 256 colours.
Some time in the future, I'm hoping to expand the CDib class to provide DirectX access.
The next page shows the code that creates and draws the Plasma.