PNG WIN32 PROGRAMMING GUIDE
Well it took me some time to figure out how to integrate pnglib properly into my application, I only wanted to spend 2 days doing this and all I wanted was to load and save simple PNG files. I was faced with lack of information, too much useless information and bad code examples. But how can you complain if it's free. I though what I learned might prove usefull to other people so I wrote this quick guide. Anyway here is a how-to for reading and writing PNG that should help you get on your way as quickly as possible. If your not familiar with using open source libraries this is a good start.
What I did?
Implement PNG reading/writing image data in a Win32 app using Visual Studio 6 (not a .net fan). I raster the image data using opengl. My motivation for using PNG was that it was a widely accepted format, had transparency, and most of all had 16bit color depth which grants much needed precision when editing hightmaps. TIFF was another possibility but I wanted to use something more common. You can either link staticly or use PNG as a dll. In both cases you must ensure that your run-time libraries are the same, between your app and pnglib, both release and debug. You need to have matching run-time libraries because pnglib forces you to pass a file pointer across a dll, and a mismatched run-time library will cause it to crash, it's a difficult problem to catch if you've never dealt much with using external libraries and dlls.
What you need to get
PNGLIB source code from lippng.com latest version where it says Source code: (don't get the binaries/dll)
ZLIB source code from zlib.net
extract both these zips to your common projects folder and name the directories
/ZLIB
/PNG
in PNG\projects\visualc6 you will find README.txt for more information
Compile libpng.lib as a static or dynamic library
Open the vc6 workspace in PNG\projects\visualc6, you should find zlib project file automatically if you've placed it in the right location. Now you need to select the correct project to build. Build->Set Active Configuration and select libpng - Win32 LIB Release/Debug for a static library or libpng - Win32 DLL Release/Debug for a dynamic library.
Now build, corresponding ZLib will build also.
Integrate resources into your project
Now you need move your newly compiled libpng.lib or libpng13.lib(dll) to your lib folder. Move png.h, png.config.h (from PNG) and zlib.h, zconf.h (from ZLIB) to your common include folder.
In my setup I make /lib and /include folders in my project folder for common external resources and in the projects setting of my my app I add ../lib/libpng.lib (release static lib) to my libraries in Link and ../include in C/C++ preprocessor under project settings.
Make sure you link with the proper lib for both release and debug
libpng.lib - release staticly linked
libpngd.lib - debug staticly linked
libpng13.dll - release dll
libpng13d.dll - debug dll
You must ensure the run-time libraries between your libpng.lib and your app are the same, the default for libpng.lib is multi-threaded DLL. If they do not match you will get linking problems or crashing. Go to Project->Settings->C/C++ , code generation Use run-time library and change it to multi-threaded DLL or debug multi-threaded DLL (if debug of course).
include png.h in your source code, and make sure everything compiles correctly
If you are using dll's copy your zlib1.dll or zlib1d.dll(debug) and libpng13.dll or libpng13d.dl(debug) to your root project folder where your executable will run.
Now you begin using PNG lib to read and write PNG files
Reading PNG Files
I pretty much just learned from PNG:The Definitive Guide chapter reading PNG. The code is cryptic, fragmented into multiple parts and I found it difficult to understand.
This is my version (excludes PNG background color and Gamma correction) you will have to provide your own code to raster the image
Writing PNG Files
Same guide, but writing PNG
This is my version
That should be all you need to start reading and writing pngs quickly.
Copyright © 2005 Ernest Szoka All rights reserved.