Scaling an Image with PNGwriter


PNGwriter helps you scale an image in several different ways, according to your needs. The algorithm used is a simple bilinear interpolation, which gives good results.

To scale the image, you can use any of the following three functions:

  • scale_k(double k): Scale an image proportionally using a singe scale factor.
  • scale_kxky(double kx, double ky): Scale an image non-proportionately, using horizontal and vertical scale factors.
  • scale_wh(double w, double h): Scale an image non-proportionately, specifying the desired final width and height.


Here are some scaled images. The original is the burro.png image included in the PNGwriter distribution. The images have been converted to JPG format to keep the bandwidth down.

An image scaled proportionally with k = 0.25
Scaled image.
An image scaled proportionally with k = 0.5
Scaled image.
An image scaled non-proportionally with kx = 2.0, ky = 0.5
Scaled image.


Here we can compare two images. One of them is the original small image. The one other is 5 times larger. Note that though the scaled image is smooth, one can distinguish the original pixels to some extent. In case you're wondering what that thing in the picture is, it's me, in a sleeping bag, showing only my lips. What I was trying to imitate, I won't say! :-) (San Pedro de Atacama, Chile, circa January 99).

Original image.
Original image.
Scaled image (k = 5.0)
Scaled image.


This is an example program:

#include <pngwriter.h>


int main(int argc, char * argv[])
{
   pngwriter image1(1,1,0,"scaled_k.png");
   pngwriter image2(1,1,0,"scaled_kxky.png");
   pngwriter image3(1,1,0,"scaled_wh.png");
   
   image1.readfromfile(argv[1]);
   image2.readfromfile(argv[1]);
   image3.readfromfile(argv[1]);

   std::cout << "Read file is:" << image1.getheight() << " " << image1.getwidth() << "\n";   
   std::cout << "Done read\n";   
   
   image1.scale_k(0.7);
   std::cout << "New file is:" << image1.getheight() << " " << image1.getwidth() << "\n";   
   std::cout << image1.read(100,100,1) << std::endl;
   image1.close();
   
   std::cout << "Done1\n";   
   
   image2.scale_kxky(0.7, 0.3);
   image2.close();
   
   std::cout << "Done2\n";   
   
   image3.scale_wh(400,100);
   image3.close();
   
   std::cout << "Done3\n";   
   
   
   
   
   
   return 0;   
}



syntax highlighted by Code2HTML, v. 0.9.1

Valid CSS!


© 2002, 2003, 2004, 2005, 2006, 2007 Paul Blackburn