Click here to Skip to main content
15,886,873 members
Articles / Desktop Programming / MFC

ImageStone - A Powerful C++ Class Library for Image Manipulation

Rate me:
Please Sign up or sign in to vote.
4.81/5 (250 votes)
6 Dec 2011Zlib3 min read 1.1M   51.5K   405  
An article on a library for image manipulation
#define _CRT_SECURE_NO_DEPRECATE

#include <stdio.h>
#include "../../../ImageStone.h"

//================================================================================
/// Our format handler
class FCImageHandle_OurFormat : public FCImageHandleBase
{
    virtual bool LoadImageMemory (const BYTE* pStart, int nMemSize,
                                  PCL_Interface_Composite<FCObjImage>& rImageList,
                                  std::auto_ptr<FCImageProperty>& rImageProp)
    {
        std::string   strHead = "ImageStone" ;
        if (memcmp (pStart, strHead.c_str(), strHead.length()) != 0)
        {
            assert(false); return false;
        }

        FCObjImage   * pImg = new FCObjImage ;
        pImg->Serialize (false, const_cast<BYTE*>(pStart) + strHead.length()) ;
        rImageList.PCL_PushObject (pImg) ;
        return true ;
    }

    virtual bool SaveImageFile (const char* szFileName,
                                const std::deque<const FCObjImage*>& rImageList,
                                const FCImageProperty& rImageProp)
    {
        if (rImageList.empty() || !rImageList[0]->IsValidImage())
            return false ;
        FCObjImage   &img = *const_cast<FCObjImage*>(rImageList[0]) ;

        // create image file, if the file already exists, its contents are discarded.
        FILE   * pf = fopen (szFileName, "wb") ;
        if (!pf)
            return false ;

        PCL_array<BYTE>   pBuf (img.GetPitch()*img.Height() + 1024) ;
        int               nSize = img.Serialize (true, pBuf.get()) ;

        // image header - "ImageStone"
        std::string   strHead = "ImageStone" ;
        fwrite (strHead.c_str(), 1, strHead.length(), pf) ;
        fwrite (pBuf.get(), 1, nSize, pf) ;
        fclose (pf) ;
        return true ;
    }
};
//================================================================================
class FCImageHandleFactory_OurFormat : public FCImageHandleFactory_Mini
{
    virtual IMAGE_TYPE QueryImageFileType (const char* szFileName)
    {
        if (FCOXOHelper::GetFileExt(szFileName) == "foo")
            return IMG_CUSTOM ;
        return FCImageHandleFactory::QueryImageFileType (szFileName) ;
    }

    virtual FCImageHandleBase* CreateImageHandle (IMAGE_TYPE imgType)
    {
        if (imgType == IMG_CUSTOM)
        {
            return new FCImageHandle_OurFormat ;
        }
        return FCImageHandleFactory_Mini::CreateImageHandle (imgType) ;
    }
};
//================================================================================
int main (int argc, char* argv[])
{
    FCObjImage::SetImageHandleFactory (new FCImageHandleFactory_OurFormat) ;

    const char* szSaveFile = "save.foo" ; // image to save our format

    // make a gradient image
    FCObjImage   img ;
    img.Create (800, 600, 24) ;

    POINT     ptStart={0,0}, ptEnd={100,100} ;
    FCPixelGradientConicalSym   aCmd (ptStart, ptEnd, FCColor::crWhite(), PCL_RGBA(0,0,255), REPEAT_NONE) ;
    img.SinglePixelProcessProc (aCmd) ;

    // save image
    img.Save (szSaveFile) ;

    // test load image
    FCObjImage   imgLoad ;
    imgLoad.Load (szSaveFile) ;
    imgLoad.Save ("save.bmp") ;

    printf ("the effected image has been saved into %s !\n\n", szSaveFile) ;
    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
Team Leader PhoXo
China China
graduate from University of Science and Technology of China at 2002.

Now I work at www.phoxo.com.

Comments and Discussions