Jonathan. Frech’s WebBlog

BMP Im­ple­men­ta­tion in C — Graph­ic Primitives (#193)

Jonathan Frech,

Continuing de­vel­op­ment of my C bitmap li­brary, I added basic graph­ic primitives to augment the li­brary’s func­tion­al­i­ty beyond simply read­ing and writ­ing bitmaps and manually manipulating individual pixels. Source code can be seen below and also downloaded — bmp.c.
The underlying im­ple­men­ta­tion of the bitmap file format can be seen in my prior post BMP Im­ple­men­ta­tion in C. Graph­ic primitives in­clude drawing lines, rectangles, circles, ellipses; rotating, flipping, cropping, resizing and blitting images. A full list of defined graph­ic primitives can be seen below, to­geth­er with a short func­tion­al­i­ty de­scrip­tion.

Test image re­gard­ing drawing primitives.
/* === DRAWING PRIMITIVES === */
void hline      (image *img, int x0, int x1, int y , int c               ); // draw horizontal line
void vline      (image *img, int x , int y0, int y1, int c               ); // draw vertical line
void line       (image *img, int x0, int y0, int x1, int y1, int c       ); // draw line
void fillrect   (image *img, int x0, int y0, int x1, int y1, int c       ); // draw filled rectangle
void rect       (image *img, int x0, int y0, int x1, int y1, int c       ); // draw rectangle
void fillcircle (image *img, int x , int y , int r , int c               ); // draw filled circle
void circle     (image *img, int x , int y , int r , int t , int c       ); // draw circle (with certain thickness)
void fillellipse(image *img, int x , int y , int rx, int ry, int c       ); // draw filled ellipse
void ellipse    (image *img, int x , int y , int rx, int ry, int t, int c); // draw ellipse (with certain thickness)

/* === TRANSFORMATION PRIMITIVES === */
image *resize   (image *img, int w , int h                                ); // resize an image
image *hflip    (image *img                                               ); // flip horizontally
image *vflip    (image *img                                               ); // flip vertically
image *rrotate  (image *img                                               ); // rotate clockwise
image *lrotate  (image *img                                               ); // rotate counter-clockwise
image *hrotate  (image *img                                               ); // rotate half a revolution
image *crop     (image *img, int x0, int y0, int x1, int y1               ); // crop an image
void   blit     (image *img, image*, int x , int y                        ); // blit an image onto another one
Test image re­gard­ing transformation primitives.

Future plans for this li­brary in­clude per­for­mance op­ti­mi­za­tions re­gard­ing the ellipse drawing primitives; circle drawing is already optimized as it uses the shape’s symmetry to save computational cost.Further primitives that may be added in­clude a flood filling func­tion­al­i­ty as well as the abil­i­ty to draw irregular polygons.

Source code: bmp.c