Monday 1 May 2023

High quality VGA graphics!

So, this post is a bit of a flex of how code can work first try, especially when you least expect it! The other day, I had the idea of adding a routine to my graphics library that draws graphics using its own set of colours, instead of the VGA's default palette. I already have a function that draws full-screen graphics, but it uses the default palette, so the colour range is hugely limited. Here's a test picture, converted using the default palette:


As you can see, it looks pretty awful. It's using such a reduced range of colours, because there's a lot of red shades in this picture, but it only has so many to choose from. As a result, it looks very blotchy. Dithering would take up too much space in this case, since I'm dealing with .com files, and they can't be above 64kb!

I spent time writing a simple conversion program in Python, based on the one I'd written already, but this time using a shoddy quantization routine that only uses 254 colours instead of 256, because integer division. The layout of the resulting file is ridiculously simple: the first 768 bytes consist of the palette entries, and after that, you have the picture, encoded in RLE. For a brief rundown, it works in pairs of bytes (or words), with the first byte being the colour to use, and the second byte deciding how many times to draw that colour. This saves a lot of space, especially if your image has lots of solid colours. Once I wrote that, I added a routine to my graphics library that gets the palette and draws the image.

Things got exciting, because I hadn't tested if my theory worked, so I assumed that my conversion program did the right thing and my drawing routine worked. Imagine my surprise when I assembled the program for the first time, and saw this:


Isn't it beautiful? Even though it only uses 254 colours, it still looks amazing, and the picture is only 46kb after conversion! I can't get over how good it looks, especially without dithering. This feature probably isn't so useful for pictures with solid colours, but for photos like the above, it works amazingly well. I can't wait to try it out with other pictures!

So there you have it, the miracles of code working the first time! The source code to the conversion program is available here, and the graphics library here.

No comments:

Post a Comment

Amiga module player for DOS - the first draft!

After one week, I've finally pulled off what I previously thought impossible - an entire module player, running in real mode DOS! I'...