Preface

Years after the previous version, I can tell that the article has survived its author, and even if my opinion on the limits of this library has not changed, an update was really necessary.

Thank you very much to all the people that gave their contribution to this new release, with hundreds of small and big enhancements, my role was mainly to put all the pieces together.

Introduction & License

CxImage is a C++ class that can load, save, display, and transform images in a very simple and fast way.

The class CxImage is free; as for the TIFF, JPEG, PNG and ZLIB libraries : "If you use this source code in a product, acknowledgement is not required but would be appreciated."

CxImage is open source and licensed under the zlib license . In a nutshell, this means that you can use the code however you wish, as long as you don't claim it as your own.

What's New in Version 6.00

The complete list of bugfixes and enhancements is reported in the documentation, (see \doc\cximage_history.htm, or this link). Here I will highlight some of the new features.

An application written with CxImage version 5.99 should work also with the new version; the interface of few methods is different, normally because of new parameters, but the default behaviour is the same.

The applications linked with the old DLL will not work with the new one; but if necessary you can edit the declarations, or add new overloads, and revert to the old interface for the DLL. An issue to take care is ENUM_CXIMAGE_FORMATS: in the old version the CXIMAGE_FORMAT_... can change the value, depending on the supported formats enabled by the corresponding CXIMAGE_SUPPORT_... switch. In the new version, CXIMAGE_FORMAT_... are assigned to unique values. Static methods like GetNumTypes, GetTypeIdFromName, GetTypeIdFromIndex, GetTypeIndexFromId will help the application to manage the new policy.

File Formats & Linked C Libraries

CxImage works with the latest version of these libraries: Zlib (1.2.3), Jasper ( 1.900.1), LibMNG (1.0.10), LibPNG (1.2.24). LibTIFF 3.8.2 can be linked with CxImage, but the version included in the CxImage distribution (3.5.7, patched) can read images with OJPEG compression, or with non standard bit per samples. The choice is up to you.

The j2k library (now openjpeg), and the associated class CxImageJ2K, have been removed from the project. JPEG2000 images are supported through Jasper and CxImageJAS.

CxImage 6.00 includes a new class (CxImageRAW) and a new library (LibDCR) to read RAW images from digital cameras; common file extension are: RAW, CRW, NEF, CR2, DNG, ORF, ARW, ERF, 3FR, DCR, X3F, MEF, RAF, MRW, PEF, SR2. LibDCR is based on Dave Coffin's dcraw.c; and offers the same features of the original dcraw application (see the "dcr.dsw" project included in the \raw directory). The restricted features under the GPL Version 2 are disabled; please read the license terms in "libdcr.h" before enabling the restricted code.

CxImageRAW implements the basic functions to decode the image, the only available option controlled by SetCodecOption, is about the interpolation quality (DECODE_QUALITY_LIN=0, DECODE_QUALITY_VNG=1, DECODE_QUALITY_PPG=2, DECODE_QUALITY_AHD=3).

CxImagePNG has been improved to read and write all of the PNG_COLOR_TYPE_... combinations. PNGs with a "pixel depth" more that 8 bits per channel will be converted down to 8 bits, this is the major limit in CxImage.

CxImageGIF: better support for reading animated GIF, now can decode all frames in a single pass, if enabled with SetRetreiveAllFrames. The CxImage demo implements this option, and shows how to play an animated GIF (when the program asks "File with N images. Read all?", select "Cancel").

CxImageJPG: new options for the JPEG format subsampling (ENCODE_SUBSAMPLE_422, ENCODE_SUBSAMPLE_444), default is 4:1:1 (high), can be set to 4:2:2 (medium) or 4:4:4 (none).

The next table shows the different amount of artifacts in the compressed image, using to different subsampling. 4:4:4 subsampling is useful in images with sharp edges, to reduce the typical blurring effect of the JPG compression.

original image

JPG image and artefacts,quality 75, subsampling 4:1:1

JPG image and artefacts,quality 75, subsampling 4:4:4

Portability

The class and the projects have been tested with different compilers, from Microsoft VC++6 to VC++2008, with Borland C++ Builder 3 and 6, and partially with wxDev-C++ and MinGW.

UNICODE and non-UNICODE configurations are provided for all the libraries (thanks to Eric Jesover).

A light version (cximage600_lite) without the C libraries and with a small demo is provided for the first time users, or as a basic template, without the clutter of the full demo project.

The console (\demo2) and the CxImageCrtDll projects can be built with VC++Express2005 and the Microsoft Platform SDK. If you get linker errors like "unresolved external...", check if all the C libraries have been compiled (set the correct project dependencies), or add the gdi32.lib and user32.lib modules to the Linker\Input\Additional Dependencies property.

CxImage works also with Pocket PC 2003; a working version and demo (cximage600_ce) are provided for the VC++2005 compiler (thanks to Vincent Richomme). For the old embedded VC compilers, the main limit was the support for exception handling. To overcome this problem, the try, throw and catch statements have been replaced with 3 macros (defined in ximadef.h), and with the definition of CXIMAGE_SUPPORT_EXCEPTION_HANDLING; in this way it is possible to build the library without exception handling support. Maybe the solution is not so elegant, but the impact on the source code is minimal when exception handling is disabled, while there are no changes in case of exception handling enabled.

The compatibility between little-endian and big-endian platforms, for the built in formats (bmp, ico, tga, pcx, gif, ska) is handled by ntohs and ntohl.

Demo

Almost all the new features can be tested in the main CxImage demo application. The demo is just a test bench, even if it offers some nice features, it is not intended to be a serious application.

CQuantizer : the class is used with the DecreaseBpp menu. In the previous version there was a rounding error, clearly visible when in some case the white colour (255,255,255) was converted to (254,254,254). In the new release this error has been fixed.

Copy/Paste : now pastes also metafile pictures (for example, from Office applications). The demo internally uses a custom clipboard format, to test the Dump/Undump methods.Copy works on the active selection, use \CxImage\Remove Selection to copy the full image.

FloodFill : (\View\Tools\Flood Fill) with a floating dialog, you can test the FloodFill colour, tolerance, opacity, and selection. The case with opacity = 0 and selection enabled acts as a "magic wand".

Graph data extraction : (\Filters\graph data extraction) extracts the numeric data from graphs (acquired from scanners or downloaded from the internet). The converted values are pasted into the clipboard, and can be saved to text or Excel files.Data Extraction dialog and results

RedEyeRemove : (\Filters\Non Linear\Remove Red Eye) removes the red-eye effect that frequently occurs in pictures. You must select the region around the pupil, where the function will filter the red channel. The selection can also be rectangular, or including part of the iris: the filter works on a circular region centred on the selection.

Custom linear filters : (\Filters\Linear\Custom) a small graphic interface to test new kernels for the Filter function (thanks to Priyank Bolia).

Histogram : (\Colors\Histogram\...) many menus are available to test HistogramStretch with different approaches (0 = luminance, 1 = linked channels , 2 = independent channels); the threshold parameter increases the robustness of the algorithm in case of noisy images. "Half Saturation" and "Full Saturation" test the combination of ConvertColorSpace, Histogram, and Saturate, to stretch the histogram in the YUV colorspace.

original image

+ HistogramStretch(2,0.005f)

+ "Half saturation"

Thresholding : ( \Colors\Threshold... and \Colors\Adaptive Threshold). OptimalThreshold is a new method to find the optimal threshold for image binarization, the available algorithms are:1 = Otsu;2 = Kittler & Illingworth;3 = maximum entropy;4 = potential difference;0 = average all methods (default, and used in the demo);

The option "preserve colors less than the threshold" will test Threshold2, useful to filter colored images with a noisy background, the result will be a colored image with a uniform background. AdaptiveThreshold is an application of OptimalThreshold to build a variable threshold mask. AdaptiveThreshold is useful in case of images with a non uniform lightness, where a single threshold can't be optimal for the whole image; but in general it will give bad results if the parameters are not well tuned.

original image

OptimalThreshold + Threshold

AdaptiveThreshold

Add shadow : (\Filters\Add Shadow...) this menu is a small example on how the simple CxImage methods (selections, GaussianBlur, Mix) can be combined to obtain a common effect.

purple shadow on white background

black shadow on red background

Text smoothing : DrawStringEx implements a new option, through CXTEXTINFO::smooth, that can be tested with the text tool (\View\Tools\Text), checking the "antialias" option.

A similar effect can be obtained in post processing with TextBlur (\Filters\Non Linear\Text Blur): a non linear filter that works only on diagonal or round edges, without affecting vertical or horizontal lines. In the next table there is a comparison between different smoothing methods:

DrawStringEx without smoothing

DrawStringEx with smoothing

post processing with TextBlur

post processing with a linear 3x3 "soften" Filter

CxImage Structure

In the vertical hierarchy of the library, CxImage stays on the top of the other modules, it's not a clean OOP approach, but the result was good since the first release and now it's too late to change again. Anyway you can always use the derived classes to perform the format specific operations, like for CxImageTIF to save multipage TIFFs.

The glue to connect all the modules and the C libraries is CxFile, a virtual class that provides the standard methods to access the data from a file on the disk or in memory.

A Cximage object is basically a bitmap, with the addition of some member variables to store useful information:

A CxImage object is also a set of layers. The buffers in each layer are allocated only when necessary.

CxImage::pDib is the background image. CxImage::pAlpha is the transparency layer. CxImage::pSelection is the selection layer, used to create regions of interest for image processing. Over these 3 specific planes, you can add other generic layers, stored in CxImage::ppLayers. The generic layers are full CxImage objects, so you can build complex structures of nested layers. CxImage::ppFrames is reserved for animated images (GIF)

CxImage Class Members & Operations

CxImage is documented using Doxygen , however for historical reasons, many uncommon features are still undocumented. The class members reference, together with release history, and license, can be found here

Supported Formats and Options

The whole library is quite big, in the main header file ximcfg.h you'll find the switches to enable or disable a specific graphic format or feature. Each JPG, PNG and TIFF library adds about 100KB to the final application, while the CxImage impact is about 50KB. So you should support and link only the formats that your application really needs.

Using CxImage in your Projects

The CxImgLib.dsw workspace shows the libraries required to build an application (demo.exe) including almost all the features and the formats available in CxImage. You must compile all the libraries before you can link the final application.In the same workspace you'll find the projects to build different libraries and applications:

CxImage : cximage.lib - static library

CxImageCrtDll : cximagecrt.dll - DLL not using mfc

CxImageMfcDll : cximage.dll - DLL using mfc

Demo : demo.exe - program linked with cximage.lib and the C libraries

DemoDll : demodll.exe - program linked with cximagecrt.dll

libdcr,jasper,jbig,jpeg,png,tiff,zlib : static C libraries

Building the projects will need some minutes to complete. When everything is done, select the demo project and launch the application.

Note: don't mix debug and release modules; each configuration must use its respective library modules.

Adding your Custom Functions in CxImage

Writing a new function for image processing is not so hard with CxImage. Here I'm going to describe CxImage::Jitter — it's very simple but it shows many aspects to take care of when you work inside CxImage. The first thing, of course, is the declaration : bool Jitter(long radius=2); in the CXIMAGE_SUPPORT_DSP section of ximage.h, you can declare the function everywhere in the public scope of the class. And now the definition:

bool CxImage::Jitter(long radius)
{
// check if the image is valid, this should be always the first line in
// the function
if (!pDib) returnfalse;
// local variables
long nx,ny;
// temporary image to store the partial results of the algorithm
CxImage tmp(*this,pSelection!=0,true,true);
// limit the effects of the functions only in the smallest rectangle that
// holds the selected region (defined with the Selection...() functions ),
// this will speed up the loops.
long xmin,xmax,ymin,ymax;
if (pSelection){
xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
} else {
xmin = ymin = 0;
xmax = head.biWidth; ymax=head.biHeight;
}
// main loop : scan the image in vertical direction
for(long y=ymin; y <ymax; y++){
// monitor the progress of the loops
info.nProgress = (long)(100*y/head.biHeight);
// let the application a way to exit quickly
if (info.nEscape) break;
// main loop : scan the image in horizontal direction
for(long x=xmin; x<xmax; x++){
// if the feature is enabled, process only the pixels inside the
// selected region
#if CXIMAGE_SUPPORT_SELECTION
if (SelectionIsInside(x,y))
#endif//CXIMAGE_SUPPORT_SELECTION
{
// main algorithm
nx=x+(long)((rand()/(float)RAND_MAX - 0.5)*(radius*2));
ny=y+(long)((rand()/(float)RAND_MAX - 0.5)*(radius*2));
if (!IsInside(nx,ny)) {
nx=x;
ny=y;
}
// save the result in the temporary image.
// if you can, use PixelColor only for 24 bpp images,
// and PixelIndex for 8, 4 and 1 bpp images : it's faster
if (head.biClrUsed==0){
tmp.SetPixelColor(x,y,GetPixelColor(nx,ny));
} else {
tmp.SetPixelIndex(x,y,GetPixelIndex(nx,ny));
}
// if the feature is enabled, process also the pixels
// in the alpha layer
#if CXIMAGE_SUPPORT_ALPHA
tmp.AlphaSet(x,y,AlphaGet(nx,ny));
#endif//CXIMAGE_SUPPORT_ALPHA
}
}
}
// save the result and exit
Transfer(tmp);
returntrue;
}

History and Credits

Starting form my CxDib class, that implements memory DIBs only, I tried to add some members to read images from files. Looking for a solution, I found a nice MFC class named CImage on the net, release 1.4 (1998). CImage supports BMP, GIF, PNG and JPG, but suffers many little bugs and uses a complex class structure, so I decided to strip it to the base and merge CxDib with the CImage philosophy, to obtain the new CxImage class. Also I updated the libraries for JPG, PNG and ZLIB.

With CxImage it is very easy to add new image types, so I added the TIFF library (rev. 6) and a minimal support for ICONs, MNG, TGA and PCX. Finally I added some specific functions to obtain an image from global HANDLEs (windows clipboard) and objects (windows resources). This is the story until the early release, the following is written in the documentation.

Thanks to Troels Knakkergaard for his precious work in the earlier versions of CxImage, Rajiv Ramachandran for CTwain code; to Abe for multi page tiffs code; to Chris Shearer Cooper for memory file suggestions and code; to Brent Corkum for BCMenu code.

More specific credits and disclaimers are in every header file of each library.

I want to save a picture as a tiff image,but I only know that I can use the sentence :CXIMAGE_FORMAT_TIF.Who tell me the details about the mean of CXIMAGE_FORMAT_TIF and others method to save an image as tiff.Thank you.

Great article. A question. Given a jpeg, can you somehow extract an image quality index for the picture? I guess you would have to provide a godd one and a bad one in that you define the limits. I think the only way this can be done is by looking at the image and determine the degree of jaggedness.

Hi All,
I want to use the CXImage library with a C# windows application on visual studio 2010, the application is framework 3.5.
I need to read an image using the cximage library and display it on a picturebox control.

I am capturing live images coming from a camera in a CxImage object (cxVideo)
This work well.
Now a want to add an overlay (some lines, circles, or text) over the live image.
For that I have added a layer to the video CxImage with another CxImage where I add the overlay information.

Is there a way to preview a TIFF file just looking for certain data without loading the entire image? My issue is that I need to look through thousands of large images, collecting only the dpi, width and height and the program is really slow in loading each file, making the process unbearably slow.

Is there a way to read only the TIFF tag info into the object without reading the image data?

The first time I used CxImage,it came up with some problems.I used it to handle wmf,but it said corrupted wmf.I was confused.So I scaned your codes.I found the resolution is limited.I crossed out the upper limitation.Then my file was showed correctly.However,other problems came when I intended to handle it.I found my wmf image was transmitted to a bitmap before it was showed.So that when I changed its size,it showed me a lot of squares like mosaic.I hope you could improve it.Thanks for your work.

Now I get a Byte array(resultBuf) including Tiff header and the following encoded raw data.
Would you show me how to get the position and the size of raw data ?
Or are there other ways to get encoded raw data ?

I'm having a really frustrating time trying to resolve this issue. When I create a brand new project (or use one of my previous projects created with VC9) everything will work fine. When I try to retrofit an old program (initially created with VC6 and has since been updated to VC9), I get a _CrtIsValidHeapPointer assertion from inside _TIFFfree. When I run it in Release mode, everything works fine (I'm assuming since heap checking is disabled in release mode).

Any ideas? It's making it really difficult to debug some other unrelated issues...