Lately, my father needed some image scaling code for an application (yep, he is a software engineer). You would suppose that there are many libraries for scaling images, as there are a whole bunch of algorithms like bilinear, bicubic etc. Lots of googling, however, leads to another impression: There are quite a few libraries, but with enormous dependencies themselves, one even requiring the whole GTK+ library!
The project my father is working on is being written in Borland C++ Builder (yuck!). The Borland C++ toolkit itself doesn’t support anything other than scaling with the nearest neighbor algorithm, which looks very ugly (pixely). Tired of googling, I wrote a small image scaling code which doesn’t depend on anything else (than a standard C++ compiler). I’ve made my own considerations on how to scale down an image, but the nearest thing to it is the bilinear interpolation algorithm (I really don’t like the mathematical approach on Wikipedia by the way. I’m a more a programmer than a mathematician!)
The downscaling is done, self-explainingly, by the
The code is an optimized version including macros, which unfortunately
is not very readable. I suggest you just use it ;-) You could also bug
me to make a nicer version.
readpng() and the PNG library are only needed to provide some sample image input, which you should replace by some other form of getting pixels. Update: A version with BMP input is now available in the ZIP archive.
At the moment, the code only supports downscaling. It seems that a few % upscaling is no problem though.
Update: Seems like you need the following stuff starting from libpng 1.4:
#define png_infopp_NULL (png_infopp)NULL #define png_voidp_NULL (png_voidp)NULL
Update 2: Some compressed PNGs were not extracted with the proper pixel data - this bug is now fixed.