Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / MFC

CxImage

Rate me:
Please Sign up or sign in to vote.
4.65/5 (949 votes)
15 Feb 2008Zlib13 min read 13M   300.6K   1.6K  
CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
// Console.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "ximage.h"

CString FindExtension(const CString& name)
{
	int len = name.GetLength();
	int i;
	for (i = len-1; i >= 0; i--){
		if (name[i] == '.'){
			return name.Mid(i+1);
		}
	}
	return CString("");
}

int FindFormat(const CString& ext)
{
	if (ext == "bmp")						return CXIMAGE_FORMAT_BMP;
	else if (ext == "gif")				    return CXIMAGE_FORMAT_GIF;
	else if (ext == "ico")				    return CXIMAGE_FORMAT_ICO;
	else if (ext == "tga")				    return CXIMAGE_FORMAT_TGA;
	else if (ext == "jpg")				    return CXIMAGE_FORMAT_JPG;
    else if (ext == "tif" || ext=="tiff")   return CXIMAGE_FORMAT_TIF;
    else if (ext == "png")                  return CXIMAGE_FORMAT_PNG;
    else if (ext == "wbmp")                 return CXIMAGE_FORMAT_WBMP;
	else if (ext == "wmf" || ext =="emf")   return CXIMAGE_FORMAT_WMF;
    else if (ext == "pcx")                  return CXIMAGE_FORMAT_PCX;
    //else if (ext == "j2k" || ext =="jp2") return CXIMAGE_FORMAT_J2K;
    //else if (ext == "jbg")                return CXIMAGE_FORMAT_JBG;
	else return CXIMAGE_FORMAT_UNKNOWN;
}

int main(int argc, char* argv[])
{

    if (argc<3) {
        fprintf(stderr, "CxImage 5.00 - Console demo\n");
        fprintf(stderr, "usage: %s input-file output-file\n", argv[0]);
        return 1;
    }

	CString filein(argv[1]);
	CString extin(FindExtension(filein));
	extin.MakeLower();
	int typein = FindFormat(extin);
	if (typein == CXIMAGE_FORMAT_UNKNOWN) {
        fprintf(stderr, "unknown extension for %s\n", argv[1]);
        return 1;
	}

	CString fileout(argv[2]);
	CString extout(FindExtension(fileout));
	extout.MakeLower();
	int typeout = FindFormat(extout);
	if (typeout == CXIMAGE_FORMAT_UNKNOWN) {
        fprintf(stderr, "unknown extension for %s\n", argv[2]);
        return 1;
	}

	CxImage image;

	if (!image.Load(argv[1],typein)){
        fprintf(stderr, "%s\n", image.GetLastError());
        fprintf(stderr, "error loading %s\n", argv[1]);
        return 1;
	}

	if (!image.Save(argv[2],typeout)){
        fprintf(stderr, "%s\n", image.GetLastError());
        fprintf(stderr, "error saving %s\n", argv[2]);
        return 1;
	}

	printf("Done!\n");
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The zlib/libpng License


Written By
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions