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

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 ICONs, MNG, TGA and PCX. Finally I added some specific functions to obtain an image from global HANDLEs (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.

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:

... 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

Compatibility

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

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