I am very interested in inventing the best way possible for rendering bitmaps on-screen using the base drawing functions (putpixel(x, y)
, lineto(x, y)
, moveto(x, y)
, setpen(r, g, b, transp, size)
)
You don't need to use graphic library to have them. You can call BIOS interrupt or initialize registers manually to set the video mode to 13h (for example) for 320x200 canvas field and 256-color mode and write from offset segment A000h
I know there are ways to make the rendering faster.
- If loop iterations are reduced
- If loop body computations are reduced
- If the function is smart enough to figure out where to use lineto or larger pen
C89, no std libs, implementation of putpixel/setpen functions (from graphics.h, sdl, cairo etc)
#define OFFSET_OF_ID (0x0)
#define OFFSET_OF_SIZE (0x2)
#define OFFSET_OF_PIXELS (0xA)
#define OFFSET_OF_NDIB (0xE)
#define OFFSET_OF_WIDTH (0x12)
#define OFFSET_OF_HEIGHT (0x16)
#define OFFSET_OF_BPP (0x1C)
#define OFFSET_OF_NRAW (0x22)
#define loop(x, y, z) int x; for(; y; z)
typedef const volatile unsigned long data, *pdata;
typedef unsigned char pixel [0x3], (*byte_buffer);
void bmp_render (byte_buffer image)
{
data image_width = *(pdata)&image[OFFSET_OF_WIDTH];
data image_height = *(pdata)&image[OFFSET_OF_HEIGHT];
data image_offset_pixels = *(pdata)&image[OFFSET_OF_PIXELS];
data image_raw_size = *(pdata)&image[OFFSET_OF_NRAW];
data image_row = image_width * sizeof(pixel);
unsigned h = 0, w = 0, pad = 0, padding = image_width % 4;
loop(i = image_offset_pixels, i < image_raw_size, i += sizeof(pixel))
{
if(i == image_offset_pixels + (image_row * ((h + 1)) + (pad * h)))
{
pad = padding;
i += pad;
h += 1;
w = !w;
}
setpen(image[i + 2], image[i + 1], image[i], 0, 1); // setpen(r, g, b, transp, size)
putpixel(w++, image_height - h); // putpixel(x, y) of the canvas
}
}
pad
andpadding
?pad
is initially 0, but it's not ever used before it's reset topadding
. \$\endgroup\$pad
to be 0 as the first line of the image has been encountered. This is at least what I was thinking when inventing this way of raster scan long ago. If it makes no difference.. people will mention it in the review anyway. \$\endgroup\$