Skip to content

Bitmap Manipulation

Ant edited this page Dec 27, 2013 · 1 revision

The mutable bitmap classes include only the most basic functions for drawing – pixel plotting and blitting of rows of pixels. These are clearly insufficient for creating complex interfaces or drawing interesting designs to bitmaps. As a way of keeping the bitmaps lightweight, all of the advanced drawing functionality is stored in a separate class: Graphics.

Graphics

The Graphics class provides the ability to perform complex drawing operations to an instance of any class that inherits from MutableBitmapBase. Its drawing functions include:

  • getPixel(), to get the colour of a pixel at a given set of co-ordinates;
  • drawPixel(), to set the colour of a pixel at a given set of co-ordinates;
  • drawRect(), to draw the outline of a rectangle;
  • drawFilledRect(), to draw a filled rectangle;
  • drawHorizLine(), to draw a horizontal line;
  • drawVertLine(), to draw a vertical line;
  • drawLine(), to draw a line in any direction;
  • drawCircle(), to draw the outline of a circle;
  • drawFilledCircle(), to draw a filled circle;
  • drawEllipse(), to draw the outline of an ellipse;
  • drawFilledEllipse(), to draw a filled ellipse;
  • drawText(), to print a string;
  • drawBitmap(), to draw a second bitmap (or portion of it) to the current bitmap;
  • drawBitmapGreyScale(), to draw a second bitmap in greyscale to the current bitmap;
  • drawXORPixel(), to XOR a pixel at a given set of co-ordinates;
  • copy(), to copy one rectangular area of the bitmap to another;
  • dim(), to halve the brightness of a rectangular area of the bitmap;
  • greyScale(), to convert a rectangular area of the bitmap to greyscale.

All of these functions are limited to the size of the bitmap being drawn to by default, though it is also possible for developers to define their own clipping region for all of the drawing functions.

The standard Bitmap class includes a method for creating a new Graphics object that can draw to the bitmap. Here is an example:

// Create bitmap
Bitmap* bitmap = new Bitmap(100, 100);

// Get graphics object
Graphics* gfx = bitmap->newGraphics();

// Draw a circle to the bitmap
gfx->drawCircle(50, 50, 10, woopsiRGB(0, 10, 0);

// Delete the graphics object
delete gfx;

// Delete the bitmap
delete bitmap;

The circle could be made any size, within the constraints of the integer datatypes used to represent its radius, and it would not be drawn past the boundaries of the bitmap.

Note that it is essential that the Graphics object is deleted once it is no longer needed in order to avoid a memory leak.

Clone this wiki locally