- Download source files - 1.28 Mb
- Download demo 330 Kb

## Introduction

`CxImage` is a C++ class to manage virtually any kind of images. It can load, save, display, transform images in a very simple and fast way.

## Why another image library?

Around there are many good libraries (OpenIL, FreeImage, PaintLib ...), these are powerful, complete, and constantly updated. However if you ever try to use them, you may find some difficulties; because these libraries are mainly platform independent, written in C, sometimes with a basic C++ wrapper and with tons of compiler switch declarations. Now with the new GDI+ classes on the scene, CxImage isn't so useful, but at least here you have the source code.

## Is it a MFC library?

No. Altogether it's a windows library, because of some particular constructors and the painting functions, but the backbone is platform independent.

## What's new in version 5.00

- See file 'history.htm' for the complete list.
- new formats: WBMP, WMF - EMF, JBIG, JPEG2000 (beta).
- new full codec for PCX, with support for RGBA images.
- new methods: IsTransparent , ShiftRGB, Median, Gamma, Noise, Combine, FFT, Repair, AlphaSplit
- added r/w support for 8bpp TIFFs with alpha layer.
- added support for TrueColor, RLE and uncompressed GIFs.
- added CXIMAGEJPG_SUPPORT_EXIF for JPGs.
- added ximadefs.h to adjust automatically the switches.
- the codecs for ICO, PNG, TGA and TIF work also with CXIMAGE_SUPPORT_ALPHA = 0
- new CxFile CxMemFile CxIOFile implementations
- ...

## To GIF or not to GIF?

Easy: PNG and MNG! The library contains LZW encoders/decoders. Due to Unisys patent enforcement, you should be aware of the limitations. The TIFF library is also affected by this. With `CxImage` it's a simple operation to remove a specific format, you are free to make your choice.
With the release 5.00, CxImage uses RLE compression as default for GIF images, it is less efficient but it's free. CxImage can also read and write truecolor images with the GIF format, it is a useless option, because the result is larger than a BMP, just try it if you are curious, IE5 loads correctly this format.

## CxImage structure

CxImage Class Members & Operations [^]

## Hey, this library is huge!

I'm sorry, but each JPG, PNG and TIFF library adds about 100KB to the final application. `CxImage` impact is less than 50KB. So you should support and link only the formats that your application really needs. These are the weights of the switches:

Option | Size [Kbyte]
CxImage core (all switches off) | 20
CXIMAGE_SUPPORT_ALPHA + CXIMAGE_SUPPORT_SELECTION + CXIMAGE_SUPPORT_TRANSFORMATION | 20
CXIMAGE_SUPPORT_DSP | 16
CXIMAGE_SUPPORT_WINDOWS | 8
Built in codecs (BMP, GIF, ICO, TGA, PCX, WBMP, WMF) | 24
JPEG | 88
PNG | 104
TIFF | 124

## Will CxImage support more formats?

Maybe. `CxImage` has been designed to be easily extended, but `CxImage` first should be stable enough to guarantee a good base class for the other formats. In any case, the actual formats cover the 99% of the normal demand.

## 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` is very easy to add new image types, so I added the TIFF library (rev. 6) and a minimal support for `ICON`s, MNG, TGA and PCX. Finally I added some specific functions to obtain an image from global `HANDLE`s (windows clipboard) and objects (windows resources).
With the release 5, CxImage has now a good support for memory files, new methods and file formats, and it is more portable: it works also with WinCE and Linux.

- CImage © 1995-1998, Alejandro Aguilar Sierra.
- IJG JPEG library ©1994-1998, Thomas G. Lane.
- LibPNG version 1.2.4 © 1998-2001 Glenn Randers-Pehrson
- LibTIFF version 3.5.7 © 1988-1997 Sam Leffler, © 1991-1997 Silicon Graphics, Inc.
- LibMNG version 1.0.2 © 2000,2001 Gerard Juyn.
- Gif-RLE © Hutchison Avenue Software Corporation, 1998
- LibJ2K© David Janssens, 2001 - 2002
- LibJBG © Markus Kuhn, 2002
- FreeImage 2.4.0 : Design and implementation by Floris van den Berg.
- 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.

## How to ...

## ... compile the library

You must compile all the libraries before you can link the demo application. The whole operation is quite simple: open the CxImgLib.dsw workspace, select the menu "Build/Batch Build..." and click the "Build" button.

This will need some minutes to complete (the intermediate files occupy 60MB!). When everything is done, select the demo project and launch the application.
The demo application shows how to:

- Open, save, display and print images.
- Display Bitmap, JPEG, PNG, TIFF or GIF resources.
- Exchange images with the clipboard.
- Capture windows screenshots or acquire image from TWAIN compatible devices.
- Get pixels information and transform the images.

## ... load an image resource

    	//Load the resource IDR_PNG1 from the PNG resource type
    	CxImage* newImage = new CxImage();
    	newImage->LoadResource(FindResource(NULL,MAKEINTRESOURCE(IDR_PNG1),"PNG"),CXIMAGE_FORMAT_PNG);

or

    	//Load the resource IDR_JPG1 from DLL
     	CxImage* newImage = new CxImage();
    	HINSTANCE hdll=LoadLibrary("imagelib.dll");
    	if (hdll){
    		HRSRC hres=FindResource(hdll,MAKEINTRESOURCE(IDR_JPG1),"JPG");
    		newImage->LoadResource(hres,CXIMAGE_FORMAT_JPG,hdll);
    		FreeLibrary(hdll);
    	}

or

    	//Load a bitmap resource;
    	HBITMAP bitmap = ::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP1)));
    	CxImage *newImage = new CxImage();
    	newImage->CreateFromHBITMAP(bitmap);

## ... decode an image from memory

    	CxImage image((BYTE*)buffer,size,image_type);

or

    	CxMemFile memfile((BYTE*)buffer,size);
    	CxImage image(&memfile,image_type);

or

    	CxMemFile memfile((BYTE*)buffer,size);
    	CxImage* image = new CxImage();
    	image->Decode(&memfile,type);

## ... encode an image in memory

    	long size=0;
    	BYTE* buffer=0;
    	image.Encode(buffer,size,image_type);
    	...
    	free(buffer);

or

    	CxMemFile memfile;
    	memfile.Open();
    	image.Encode(&memfile,image_type);
    	BYTE* buffer = memfile.GetBuffer();
    	long size = memfile.Size();
    	...
    	free(buffer);

## ... create a multipage TIFF

    	CxImage *pimage[3];
    	pimage[0]=&image1;
    	pimage[1]=&image2;
    	pimage[2]=&image3;

    	FILE* hFile;
    	hFile = fopen("multipage.tif","w+b");

    	CxImageTIF multiimage;
    	multiimage.Encode(hFile,pimage,3);

    	fclose(hFile);

or

    	FILE* hFile;
    	hFile = fopen("c:\\multi.tif","w+b");

    	CxImageTIF image;
    	image.Load("c:\\1.tif",CXIMAGE_FORMAT_TIF);
    	image.Encode(hFile,true);
    	image.Load("c:\\2.bmp",CXIMAGE_FORMAT_BMP);
    	image.Encode(hFile,true);
    	image.Load("c:\\3.png",CXIMAGE_FORMAT_PNG);
    	image.Encode(hFile);

    	fclose(hFile);

## ... copy/paste an image

    	//copy
    	HANDLE hDIB = image->CopyToHandle();
    	if (::OpenClipboard(AfxGetApp()->m_pMainWnd->GetSafeHwnd())) {
    		if(::EmptyClipboard()) {
    			if (::SetClipboardData(CF_DIB,hDIB) == NULL ) {
    				AfxMessageBox( "Unable to set Clipboard data" );
    	}	}	}
    	CloseClipboard();

    	//paste
    	HANDLE hBitmap=NULL;
    	CxImage *newima = new CxImage();
    	if (OpenClipboard()) hBitmap=GetClipboardData(CF_DIB);
    	if (hBitmap) newima->CreateFromHANDLE(hBitmap);
    	CloseClipboard();

## ... display a file in a picture box

    	CxImage image("myfile.png", CXIMAGE_FORMAT_PNG);
    	HBITMAP m_bitmap = image.MakeBitmap(m_picture.GetDC()->m_hDC);
    	m_picture.SetBitmap(m_bitmap);

## Remarks

- The TIFF decoder for JPEG images generated by Imaging for Windows (aka OJPEG support) will not be implemented. The reasons can be found in the LibTIFF site.

## Compatibility

Win95,WinNT, Win98, WinME, W2K, WinXP, WinCE, Linux = Yes

For any questions, e-mail to: ing.davide.pizzolato@libero.it