Click here to Skip to main content
6,630,901 members and growing! (20,849 online)
Email Password   helpLost your password?
Multimedia » General Graphics » General     Intermediate License: The zlib/libpng License

ImageStone

By crazybit

An article on a library for image manipulation.
VC6Win2K, WinXP, MFC, Dev
Posted:24 Mar 2006
Updated:12 Mar 2007
Views:215,717
Bookmarked:211 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
202 votes for this article.
Popularity: 10.86 Rating: 4.71 out of 5
6 votes, 3.0%
1
1 vote, 0.5%
2
2 votes, 1.0%
3
4 votes, 2.0%
4
189 votes, 93.6%
5

Sample Image

Introduction

ImageStone is a powerful C++ class library for image manipulation. It is written in pure C++ and is easily to portable. Its features include load/save (supports BMP, GIF, JPG, PNG, TIF, ICO, TGA, PCX, PSD...), display, histogram, undo/redo, and image transformation with over 100 predefined effects.

License

ImageStone is free. You can use the code however you want (free or commercial), as long as you don't claim it as your own. (If you use it in your product, I hope I could be notified.)

Using ImageStone

It's extremely easy, all you need to do is add #include "ImageStone.h" at the beginning of your source code. If you are using ImageStone in a MFC project, just add this include line at the end of the StdAfx.h file.

The most basic and the most important class is FCObjImage, let's learn how to use it.

... load image from memory under any OS

// Load image into memory

char   * p = 0 ;
int    n = 0 ;
FCOXOHelper::LoadFileToBuffer ("test.jpg", p, n) ;

FCObjImage   img ;
img.Load (p, n, IMG_JPG) ;

delete[] p ;

// this line demonstrate how to determine image's format by file's ext name

IMAGE_TYPE  t = 
    FCObjImage::GetImageHandleFactory()->QueryImageFileType("test.jpg");

... load image from DIB stream under any OS

// Load image into memory

char   * p = 0 ;
int    n = 0 ;
FCOXOHelper::LoadFileToBuffer ("test.bmp", p, n) ;
p += sizeof(BITMAPFILEHEADER) ;

// now p point to a DIB stream

FCObjImage   img ;
img.LoadDIBStream (p, n) ;

delete[] p ;

... load image from resource under Windows

// Load image from local exe file

FCObjImage   img ;
FCWin32::LoadImageRes (img, MAKEINTRESOURCE(nID), TEXT("JPG"), IMG_JPG) ;

// Load image from DLL's resource

HMODULE   hDll = LoadLibrary (TEXT("ResDll.dll")) ;
FCWin32::LoadImageRes (img, MAKEINTRESOURCE(nID), TEXT("JPG"), IMG_JPG, hDll) ;

// Load image from standard BITMAP resource

FCWin32::LoadImageBitmapRes (img, MAKEINTRESOURCE(nID)) ;

... load/save image via FreeImage library

// change to FreeImage library load/save image

// more detail refer to example 005

FCObjImage::SetImageHandleFactory (new FCImageHandleFactory_FreeImage) ;
img.Load ("test.jpg") ;

// change to GDI+ load/save image

FCObjImage::SetImageHandleFactory (new FCImageHandleFactory_Gdiplus) ;
img.Load ("test.jpg") ;

... combine ImageHandleFactory

// use FreeImage to load/save PSD/PCX image

class CMyImageFactory : public FCImageHandleFactory
{
protected:
    virtual FCImageHandleBase* CreateImageHandle (IMAGE_TYPE imgType)
    {
        switch (imgType)
        {
            case IMG_BMP : return new FCImageHandle_Bmp ;
            case IMG_TGA : return new FCImageHandle_Tga ;
            case IMG_JPG : return new FCImageHandle_Gdiplus ;
            case IMG_GIF : return new FCImageHandle_Gdiplus ;
            case IMG_TIF : return new FCImageHandle_Gdiplus ;
            case IMG_PNG : return new FCImageHandle_Gdiplus ;
            case IMG_PCX : return new FCImageHandle_FreeImage ;
            case IMG_PSD : return new FCImageHandle_FreeImage ;
        }
        return 0 ;
    }
    // protected avoid user delete object.

    virtual ~CMyImageFactory() {}
};

// use our custom factory to read/write image

FCObjImage::SetImageHandleFactory (new CMyImageFactory) ;
FCObjImage   img ;
img.Load ("test.jpg") ;

... load multi-frame GIF

FCObjMultiFrame   img ;
img.Load ("test.gif") ;
img.GetFrame(0)->Save ("001.jpg") ;
img.GetFrame(1)->Save ("001.jpg") ;
...

... Load jpeg's EXIF information

FCObjImage        img ;
FCImageProperty   prop ;
img.Load ("test.jpg", ∝) ;

// get camera ISO speed

std::string   m = prop.QueryPropertyValue (IMAGE_TAG_EXIF_ISOSpeed) ;
// get camera equip model

std::string   n = prop.QueryPropertyValue (IMAGE_TAG_EquipModel) ;

... draw image object under Windows

FCObjImage   img ;
// capture current screen

RECT         rc = {0, 0, GetSystemMetrics(SM_CXSCREEN), 
                         GetSystemMetrics(SM_CYSCREEN)} ;
FCWin32::CaptureScreen (img, rc) ;

// Draw image (no stretch) where top-left at (0,0) of hdc

FCWin32::DrawImage (img, hdc, 0, 0) ;

// Stretch image on region of hdc

RECT     rcOnDC = {100, 100, 200, 200} ;
FCWin32::DrawImage (img, hdc, rcOnDC) ;

// Stretch image on central of hdc's region and keeping image's aspect

FCWin32::DrawImageAspect (img, hdc, rcOnDC) ;

// Stretch regionof image on region of hdc

RECT     rcImg = {20, 20, 50, 50} ;
FCWin32::DrawImage (img, hdc, rcOnDC, rcImg) ;

... copy/paste image to/from clipboard

FCObjImage   img ;
img.Load ("test.jpg") ;

// copy image to clipboard

FCWin32::CopyToClipboard (img) ;

// get image in clipboard

FCWin32::GetClipboardImage (img) ;

... convert between GDI HBITMAP and FCObjImage

// create HBITMAP from FCObjImage object

FCObjImage   img ;
img.Load ("test.jpg") ;
HBITMAP   h = FCWin32::CreateDDBHandle (img) ;

// create FCObjImage object from HBITMAP

FCWin32::CreateImageFromDDB (h, img) ;

... convert between GDI+ Bitmap and FCObjImage

// create GDI+ Bitmap from FCObjImage object

FCObjImage   img ;
img.Load ("test.jpg") ;
Gdiplus::Bitmap   * pBmp = FCWin32::GDIPlus_CreateBitmap(img) ;
delete pBmp ;

// create FCObjImage object from GDI+ Bitmap

FCWin32::GDIPlus_LoadBitmap (*pBmp, img) ;

... process image

FCObjImage   img ;
img.Load ("test.jpg") ;

// resize (smooth) image

img.Stretch (nWidth, nHeight) ;
img.Stretch_Smooth (nWidth, nHeight) ;

// Use SinglePixelProcessProc interface to process

// image, there are over 100 pre-implemented effect,

// please refer to class derived from FCInterface_PixelProcess

FCPixelRotate   aCmd (37) ;
img.SinglePixelProcessProc (aCmd) ;

FCPixelBrightness   aCmd (150) ; // 150%

img.SinglePixelProcessProc (aCmd) ;

FCPixelMosaic   aCmd(5) ;
img.SinglePixelProcessProc (aCmd) ;

FCPixelOilPaint   aCmd (3) ;
img.SinglePixelProcessProc (aCmd) ;

... custom image processing

// our processor : change pixel's RGB value 

class CMyPixelProcessor : public FCSinglePixelProcessBase
{
public:
    CMyPixelProcessor (int nR, int nG, int nB) : m_R(nR), m_G(nG), m_B(nB) {}
private:
    virtual void ProcessPixel (FCObjImage* pImg, int x, int y, BYTE* pPixel)
    {
        PCL_B(pPixel) = FClamp0255 (PCL_B(pPixel) + m_B) ;
        PCL_G(pPixel) = FClamp0255 (PCL_G(pPixel) + m_G) ;
        PCL_R(pPixel) = FClamp0255 (PCL_R(pPixel) + m_R) ;
    }
    int   m_R, m_G, m_B ;
};

// this class has the same function to upper

// class, but implement other class

class CMyImageProcessor : public FCPixelWholeImageBase
{
public:
    CMyPixelProcessor (int nR, int nG, int nB) : m_R(nR), m_G(nG), m_B(nB) {}
private:
    virtual void ProcessWholeImage (FCObjImage* pImg, 
                                    FCObjProgress* pProgress)
    {
        for (int y=0 ; y < pImg->Height() ; y++)
        {
            for (int x=0 ; x < pImg->Width() ; x++)
            {
                BYTE   * p = pImg->GetBits(x,y) ;
                PCL_B(p) = FClamp0255 (PCL_B(p) + m_B) ;
                PCL_G(p) = FClamp0255 (PCL_G(p) + m_G) ;
                PCL_R(p) = FClamp0255 (PCL_R(p) + m_R) ;
            }
            if (pProgress)
                pProgress->SetProgress (100 * y / pImg->Height()) ;
        }
    }
    int   m_R, m_G, m_B ;
};

// use our custom processor

FCObjImage   img ;
img.Load ("test.jpg") ;

CMyPixelProcessor   aCmd (20, 20, 20) ;
img.SinglePixelProcessProc (aCmd) ;

CMyImageProcessor   aCmd (20, 20, 20) ;
img.SinglePixelProcessProc (aCmd) ;

... add text on image

FCObjImage   img ;
img.Load ("c:\\test.jpg") ;

// now we create text layer

FCObjTextLayer   imgT ;
PACK_TextLayer   tp ;
tp.m_bAddShadow = false ;
tp.m_bAntiAliased = true ;
tp.m_bBold = true ;
tp.m_bItalic = true ;
tp.m_crFont = PCL_RGBA(0,0,255) ;
tp.m_nFontSize = 128 ;
tp.m_strFace = "Arial" ;
tp.m_strText = "Hello" ;
FCWin32::CreateTextLayer_GDIPlus (imgT, tp) ;

// now we have create text image, additional

// we can add some affect on it, such as gradient color

POINT                 pt1={0,0}, pt2={0,50} ;
FCPixelGradientLine   aCmd (pt1, pt2, PCL_RGBA(0,0,255), 
                            FCColor::crWhite()) ;
imgT.SinglePixelProcessProc (aCmd) ;

// blend text layer on image

RECT   rc = {0, 0, imgT.Width(), imgT.Height()} ;
img.AlphaBlend (imgT, rc, rc, 100) ;

History

  • 2007 - 03 - 11, V4.0
    + Add FCPixelFillGradientFrame.
    + Add FCPixelLensFlare / FCPixelTileReflection / FCPixelSoftGlow effect.
    * Modify example.
    * Improve FCObjImage::AlphaBlend.
    * Modify FCPixelBlinds.
    * Modify brightness/contrast/hue/saturation/emboss.
    * Rewrite gauss blur processor.
    - Remove FCPixelDeinterlace.
    - Remove FCPixelAddRandomNoise.
    - Remove FCPixelFill3DSolidFrame.
    - Remove FCImageHandleFactory_IJL15 and FCImageHandle_IPicture.

  • 2006 - 10 - 25, V3.0
    * Improve FCImageHandle_Gdiplus class to load multi-frame gif/tiff image and load jpeg's EXIF information.
    * Improve FCImageHandle_FreeImage class to save gif with transparency color.
    * Change FCPixelHueSaturation's hue arithmetic.
    * Change FCPixelColorTone's arithmetic, more look like old photo.
    * Change inner FCImageHandleBase interface, it's never mind for user.
    * Substitute std::fstream by ANSI C file function, because of a bug in VC2005.
    + Add FCImageProperty to store image's property, function FCObjImage::Load and FCObjImage::Save support it.
    + Add example 010 : Load jpeg's EXIF information via GDI+.
    - Remove FCObjImage::GetNextFrameDelay and FCObjImage::SetNextFrameDelay, you can get them from FCImageProperty.

  • 2006 - 09 - 07, V2.0
    + More mature.

  • 2006 - 03 - 11, V1.0
    + Initial version.

Author and Contact

Contact information of the author, Fu Li:

License

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

About the Author

crazybit


Member
graduate from University of Science and Technology of China at 2002.

Now I work at kingsoft.

E-Mail: crazybitwps@hotmail.com
Occupation: Team Leader
Location: China China

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 127 (Total in Forum: 127) (Refresh)FirstPrevNext
GeneralImage >= 32MB PinmemberSala812:06 15 Oct '09  
Generalerror C2146 in compiling example 08 in VS2005 Pinmemberdrhani3330:45 13 Aug '09  
GeneralRe: error C2146 in compiling example 08 in VS2005 Pinmemberdrhani33322:48 13 Aug '09  
GeneralDoes include/add such feature? Pinmemberwise.bear22:36 2 Aug '09  
GeneralFeatures? Pinmemberandwan08:16 7 Jun '09  
GeneralRe: Features? Pinmembercrazybit18:57 21 Jun '09  
GeneralRe: Features? Pinmemberandwan05:11 1 Jul '09  
GeneralconvertToTrueColor [modified] Pinmemberomega06:54 24 Jan '09  
QuestionCopy image byte during process Pinmemberelroulianito0:49 24 Nov '08  
QuestionMingw-GCC not compatible PinmemberAli Imran Khan Shirani21:56 20 Nov '08  
GeneralLossless rotation PinmemberMember 304416414:13 16 Oct '08  
QuestionHow to specify text layer position? PinmemberMember 40222095:58 14 Mar '08  
QuestionHow set FreeImage handler to support more file types for linux Pinmemberthabang6:46 10 Mar '08  
GeneralRe: How set FreeImage handler to support more file types for linux Pinmembercrazybit19:27 10 Mar '08  
Questionerror C2065: 'META_SETBKCOLOR' : undeclared identifier GdiplusEnums.h 558 Pinmemberannemeeaye1:31 9 Mar '08  
GeneralRe: error C2065: 'META_SETBKCOLOR' : undeclared identifier GdiplusEnums.h 558 Pinmembercrazybit2:28 10 Mar '08  
GeneralError when file not exist PinmemberTran Huu Chuong5:52 4 Mar '08  
GeneralRe: Error when file not exist PinmemberTran Huu Chuong16:12 5 Mar '08  
GeneralRe: Error when file not exist Pinmembercrazybit2:34 10 Mar '08  
GeneralRe: Error when file not exist [modified] PinmemberTran Huu Chuong5:42 16 Mar '08  
GeneralRe: Error when file not exist Pinmembercrazybit16:53 16 Mar '08  
GeneralRetain EXIF [modified] PinmemberDave2107512:06 19 Jan '08  
Questionerrors while trying to compile examples in 6.0 ? PinmemberboTiCS10:17 4 Nov '07  
GeneralRe: errors while trying to compile examples in 6.0 ? PinmemberJ A4:50 17 Jul '09  
GeneralRe: errors while trying to compile examples in 6.0 ? PinmemberJ A17:27 18 Jul '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Mar 2007
Editor: Sean Ewington
Copyright 2006 by crazybit
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project