HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Exotic bitmap renderer for core drawing functions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
coreexoticdrawingforbitmapfunctionsrenderer

Problem

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++,

Solution

If loop iterations are reduced

Loop iterations can be only lowered in occasions in which you use lineto or/and larger than 1 pixel pen size.


If loop body computations are reduced

The padding computation can be omitted from each iteration. Videlicet the modulo % operator slows down tremendously. Nevertheless with your current approach of raster scan I believe you are combining minimum resources during the loop.


If the function is smart enough to figure out where to use lineto or
larger pen

Relates to the first idea for speculative optimization. Idea, that can be achieved along the second idea for optimization. And that is to give RLE-compressed (figuratively) image instead of raw image, which compression determines where to use line, where to use larger pen size or a combination of these two.

I've been through similar cases where optimizing is precious and with that consideration and in this particular circumstance I am only capable of confirming these "theories" as well as share your opinion about how useful that could be. Quite frequently people's first game projects are based on drawing from scratch.. and they have a hard time rendering images with the desired quality.

Context

StackExchange Code Review Q#86634, answer score: 4

Revisions (0)

No revisions yet.