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 scale() function. 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.
Download: scaledown.zip
Last modified: June 18, 2010David Madl, April 20, 2010:
Hi Johann,
thanks for the feedback! The artifacts are very interesting. My code is only used for scaling a GUI (for which probably better methods exist, but what the heck), thus artifacts of this kind are no problem.
The fields e-mail and website are optional. Your e-mail-address will not be displayed, it's only for me to be able to reply.
© 2009 by David Madl. Impressum | Home (English) | Home (Deutsch) | Processing time: 0.654 s
johann, April 20, 2010:
For correct downscaling you need some kind of low pass filtering done before interpolation. Please look at: http://www.xs4all.nl/%7Ebvdwolf/main/foto/down_sample/down_sample.htm