Find Simplicity and Portability in PNGwriter Graphics Library

C++ applications of many stripes need to produce graphics files portably, efficiently, and without a lot of hassle. These graphics are generated as a result of computation and user input, what I used to call plots in the bad old days. The first thing a C++ developer might reach for to implement this function probably is the old Windows SDK's rudimentary bitmap functions. Unfortunately, the Windows SDK bitmap functions are so rudimentary that the developer will quickly find himself or herself spending more time reading the help files than actually coding.

Enter PNGwriter, a very easy-to-use open source graphics library that uses PNG as its output format. For ease of use and portability, it's tough to beat PNGwriter. The interface has been designed to be as simple and intuitive as possible. It supports the following:

  • Plotting and reading in the RGB (red, green, blue), HSV (hue, saturation, value/brightness), and CMYK (cyan, magenta, yellow, black) colorspaces
  • Basic shapes
  • Scaling
  • Bilinear interpolation
  • Full TrueType antialiased and rotated text support
  • Bezier curves
  • Opening existing PNG images
  • Much more

PNGwriter runs under Linux, Unix, Mac OS X, and Windows. It requires libpng and optionally FreeType2 for the text support. If you have a Debian Linux distro, you've already got PNGwriter on board. Otherwise, you need to download the source from the PNGwriter Sourceforge site. What's more, if you can generate a series of related images, it's easy to convert a stream of PNGs to a movie.

What Exactly Is a PNG, You Ask?

PNG, short for Portable Network Graphics, arose as an international standard in 1999 when the ever-increasing number of graphics formats started to become a problem.

Today, you'll find PNG support in all Web browsers, office products, and photo editors. PNG was designed to be patent-free from the get-go, and yet to replace all existing lossless compression file types, such as GIF and TIFF to name just two.

PNG has a lot of flexibility and many features designed right into the specs, including the following:

  • Multiple CRCs so that file integrity can be checked without viewing
  • Signature that can detect the most common types of file corruption
  • Up to 50 percent better compression than GIF
  • Two-dimensional interlacing scheme
  • 1-, 2-, 4-, and 8-bit palette support (like GIF)
  • 1-, 2-, 4-, 8-, and 16-bit grayscale support
  • 24-bit and 48-bit true color support
  • Alpha-channel transparency in 8- and 16-bit modes
  • Gamma correction for cross-platform brightness control
  • Color correction for cross-platform precision color
  • Both compressed and uncompressed text chunks for copyright and other info

Setting Up PNGwriter

PNGwriter may be easy to use but setting it up for the Visual C++ environment involves a number of steps. In fact, you must follow a step-by-step procedure for it rather than use a makefile. PNG uses Zlib for lossless compression, so you'll need to download Zlib. It uses pnglib as its base technology, so you'll need to download pnglib too. Last, you should download FreeType to be able to mix text rendered in TrueType in with your graphics. For the sake of brevity, I disabled FreeType in my project because it had no Visual C++ makefiles. In the unlikely event that you use Cygwin or MinGW compilers, you can skip these steps.

In the end, I created a small batchfile:

set MYINCLUDES=/I D:\pngwriter-0.5.3\src /I D:\libpng-1.2.5
               /I D:\zlib /I D:\freetype-2.2.1\include


cl example.c /TP /Z7 /EHsc %MYINCLUDES% /link libpng13.lib zlib.lib

Note that there is no "pngwriter.lib". It is a standalone sourcefile that you can compile separately or actually just #include into your sourcefile. For ease of debugging, I built it as a separate module. Note the /DNO_FREETYPE I used to disable FreeType.

Hello PNGwriter

It never hurts to start out with the simplest possible example code. In this case, I use the sinewave plot program to show how a PNG can be created programmatically:

 1 #include "pngwriter.h"
 3 int main()
 4 {
 5    int i;
 6    pngwriter png(300,300,0,"test.png");
 7    for(i = 1; i < 300;i++)
 8    {
 9       png.plot(i,150+100*sin((double)i*9/300.0), 0.0, 0.0, 1.0);
10    }
11    png.close();
12    return 0;
13 }

What is going on here? I initialize a pngwriter class bitmap object in line 6 to be 300x300 pixels, a background color of black (zero), and an output filename named "test.png". Then, I loop across the X axis, left to right, computing a sinewave Y value and plot it on line 9. There are two overloaded plot methods; one uses 16-bit ints for RGB and the other has doubles for color inputs from 0.0 to 1.0. In this case, I plot using the doubles (0.0, 0.0, 1.0), which means RED=0, GREEN=0, BLUE=100 percent. So, if you've been paying attention, you should expect a blue sinewave on a black background (see Figure 1).

Figure 1. Blue Sinewave on a Black Background

From this demo, it's just a matter of doing more math to get things like Lorenz Attractor fractals.

The Webmaster's Dilemma

One of the more boring aspects of being a modern webmaster is getting all the graphics to be the right size for a given purpose. I end up spinning out three to four sizes of every image I'm going to need so I have a good-looking rendition at 100 pix tall, 150 pix tall, and 200 pix tall. This quickly becomes a dull chore in programs like Paint Shop Pro, Photohhop, and so forth. How about a nice C program to resize your image for you and generate all the permutations your pages desire:

 1 #include "pngwriter.h"
 3 int main(int argc, char * argv[])
 4 {
 5    for (int img_height=100; img_height<300; img_height+=50) {
 6       char filename[255];
 7       sprintf(filename,"scaled_%d", img_height);
 8       pngwriter image1(1,1,0,filename);
 9       image1.readfromfile(argv[1]);
10       std::cout << "Read file is:" << image1.getheight()
                   << " " << image1.get width() << "\n";
11       double factor = (double)img_height / image1.getheight();
12       image1.scale_k(factor);
13       std::cout << "New file is:" << image1.getheight()
                   << " " << image1.getwidth() << "\n";
14       image1.close();
15    }
16    return 0;
17 }

In line 9, I read a filename passed in on the command line into the pngwriter object called image1. I then compute a proper scale factor based on the ratio of the desired height to the actual height. In line 12, I scale proportionally to get the new desired height. In line 14, I write out a generated PNG file, which might be scaled_100.png, scaled_150.png, and so on.

Book Recommendation

PNG: The Definitive Guide by Greg Roelofs is exactly what the title says it is. Because Roelofs is the maintainer and prime mover of the libpng distribution and home page, he has a pretty good grasp of the subject matter. In the "Design of PNG" section, he includes detailed information on compression and filtering, gamma correction and precision color, and PNG options and extensions. In the "Programming PNG" section, he steps through three sample programs that implement PNG with the libpng C library. It is chock full of examples, sample code, and practical hands-on advice.

Just the Tip of the Iceberg

I have really only scratched the surface of the more interesting PNGwriter features. PNGwriter can give you HSV and CMYK translations of image data, plot TrueType text, handle Bezier curves, and many other functions limited only by your imagination. If PNGwriter lacks anything, it would be a complete build system for Visual C++ or else a site dedicated to providing binaries for all the versions of Visual C++ out there in common use.

About the Author

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries; just drop an e-mail to

About the Author

Victor Volkman

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries, just drop an e-mail to


  • pttfmz

    Posted by Mandyzpr on 03/29/2013 06:46pm

    Seeing the whole body exudes extraordinary allure Suigetsu and immediately hugged Xiao Feng left arm, chest of giant pill with her pair of hand grip his arm tightly to his flirtatious coquetry cried. Oh ... this, ah, Suigetsu Hold on for just a second, let ray ban aviators Think first what better ability to help you exchange. And Xiao Feng so so mature and charming Suigetsu holding him, he pretended to look consider. But in fact he has been considered a day and a night, has long been thought to how to do, now also fitted in such a way for what is a male will know. Xiao Feng hearts at this time, however, lamented: really goblin it! The idea seems to want to use her as a secret weapon to be wasted! Jiao prettily beauty, ray ban aviator sunglasses, how can you bear to let her red in front of your own!ray ban wayfarer sunglasses, Hi .ray ban caravan,.oakley sunglasses outlet,. plans to keep up with changes ah!

  • eaqjtc

    Posted by Mandyxko on 03/28/2013 09:29am

    Eyebrows a challenge, said: fair niece, Lushun Shipyard things the court that at least Weng master should not know?cheap ghd, Lvshun Shipyard latest shipbuilding progress Younger know, two cruisers in the dock are working day and night to work from Jiangnan dock first built the hull.ghd australia, So that you can save three or four months time, plus arrived in Port Arthur Shipbuilding construction, estimated the longest But seven months to the time can be completely finished equipment Northern Navy .ghd hair straightener,.ghd,.ghd straightener, In addition to the two under construction cruiser outside Jiangnan dock being built two hull if Nanyang side according to the situation confirmed that you really ordering cruiser can be one of them to be transferred to Nanyang Navy as flagship ...... Tan Yankai said. That fair niece intend to pin down how long German? Germany may Jiaozhou Bay events yet to react.

  • ghd australia dzqwto

    Posted by Suttonjlt on 03/08/2013 04:39pm

    ghd nz dvuwinnx ghd nz sale cggcefjd ghd oshnfcxj

  • ghd australia cuurwb

    Posted by Mandyflt on 02/17/2013 12:02am

    cheap toms shoes znoevass cheap toms nenmsvuo toms on sale pbebaspq toms outlet hqbjfxrx toms sale lboxgrnb toms shoes on sale avqgolrx toms shoes sale gmihsosc toms shoes ngjdhiay

  • ghd australia biaoiz

    Posted by Suttonvca on 02/05/2013 08:11am

    0pZgp ugg pRhm ¥È¥ê©`¥Ð©`¥ÁÐÂ×÷ iNny nike shox 6jYcq toms sale 5mUob hollister outlet 1mShe bottes ugg 6pQex longchamp 2hUon cheap louis vuitton 9uHkk michael kors handbags 0iMhj christian louboutin 1tGmy 49ers jerseys 1dSxf 6tKme GHD Australia 0oUmk ghd france 8bNoy ugg sale

  • ugg boots vdwpll

    Posted by Suttonmpk on 01/29/2013 04:29pm

    2bXhk cheap nike shoes lKjj Michael Kors outlet mQdi ugg boots 2rJws monster beats 1iLig New York Knicks Lin NBA T-Shirt Blue Wholesale Sell 1pNcw uggs sko 3uWkk burberry outlet 6mLhz longchamp 3dUnt cheap nike air max 6gZpw ugg boots sale 5mDcv monster beats 4mAlf ugg 4fQma ghd 1zMgv 4hNlh

  • ghd australia wtrxwq

    Posted by Mandygxg on 01/29/2013 12:07pm

    8zFkl chaussures ugg qZsa ¥È¥ê©`¥Ð©`¥Á Ñ¥ kUem nike sko norge 7iAof cheap toms shoes 2iPag hollister outlet 2jDlc chaussures ugg 7nIbs portefeuille longchamp 0wUkn louis vuitton purses 5kNwt michael kors handbags 3uSyg christian louboutin 7yAmy ugg uk 5gEiy nfl jerseys 2cEsc 4pQsl lisseur ghd pas cher 5bKzq cheap ugg boots

  • ugg boots eplnzz

    Posted by Suttonrcz on 01/13/2013 12:20am

    2tWtu cheap ugg boots fVut Michael Kors outlet vAjf ugg boots 0lVgy Burberry outlet 6xXnb Cheap nfl jerseys 5xHam coach,coach outlet,coach outlet online,coach factory outlet 1hEvn 1nVqw 6bChf 2bRwp 3qGtt 9hWsk 0mRmc 3kYpd 3jAuc

  • qhgxmwfd ukfussmh pmrokvem lqhtfs

    Posted by PambInabe on 11/17/2012 02:34am

    jbtzgu pqraft louboutin homme ndrdrtmj louboutin pas cher hnwkaxd cwtxlok ovdba Find Simplicity and Portability in PNGwriter Graphics Library thpnnbg longchamp pas cher mkmeymeb longchamp pliage kfjrtyhw jordan pas cher jcbytsun

  • cheap beats by dre fake

    Posted by boowiffRodial on 11/16/2012 08:02am

    nujdy zufrx ugg ムートンブーツ ugg ugg キッズ deikz xisgyp Find Simplicity and Portability in PNGwriter Graphics Library muviyxh ダウン レディース モンクレールコート モンクレール maya maticgm ottag ugg outlet deckers ugg boots ugg boots zip up sefsozgp louis vuitton handbags buy online louis vuitton bags louis vuitton outlet watches fgwcrtsv

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date