PGF         Source code for loading PGF images using libPGF in Ogre

About PGF

PGF is a relatively new image codec like JPG but which compresses better in most cases.
For more information and downloading libPGF (which includes a command line tool for compressing) see http://libpgf.org.

Loading PGF in Ogre

OgrePGFCodec.h:

/*
 -----------------------------------------------------------------------------
 Copyright (c) 2006 by Urs Hanselmann
 
 Permission is hereby granted, free of charge, to any person obtaining a copy 
 of this software and associated documentation files (the "Software"), to deal 
 in the Software without restriction, including without limitation the rights 
 to use, copy, modify, merge,  publish, distribute, sublicense, and/or sell 
 copies of the Software, and to permit persons to whom the Software is 
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included 
 in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 -----------------------------------------------------------------------------
 */
 #ifndef _PGFImageCodec_H__
 #define _PGFImageCodec_H__
 
 #include "OgreImageCodec.h"
 
 namespace Ogre {
 
 /**
  * PGF codec for Ogre. See http://libpgf.org
  */
  class PGFCodec : public Ogre::ImageCodec
 {
 private:
    
    /** Common decoding routine **/
    static unsigned char *decodePGF (unsigned char *data, size_t len, int &w, int &h);
 
 public:
    PGFCodec();
    virtual ~PGFCodec();
    
    virtual String getType() const;
    
    DataStreamPtr code(Ogre::MemoryDataStreamPtr& input, Codec::CodecDataPtr& pData) const;
    void codeToFile(MemoryDataStreamPtr& input, const String& outFileName, Codec::CodecDataPtr& pData) const;
    Codec::DecodeResult decode(DataStreamPtr& input) const;
       
    static PGFCodec *ptr;
    /** Static method to startup and register the PGF codec **/
    static void startup(void);
    /** Static method to shutdown and unregister the PGF codec **/
    static void shutdown(void);
 };
 
 }
 
 #endif

OgrePGFCodec.cpp:

/*
 -----------------------------------------------------------------------------
 Copyright (c) 2006 by Urs Hanselmann
 
 Permission is hereby granted, free of charge, to any person obtaining a copy 
 of this software and associated documentation files (the "Software"), to deal 
 in the Software without restriction, including without limitation the rights 
 to use, copy, modify, merge,  publish, distribute, sublicense, and/or sell 
 copies of the Software, and to permit persons to whom the Software is 
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included 
 in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 -----------------------------------------------------------------------------
 */
 #include "OgreStableHeaders.h"
 
 #include "OgreRoot.h"
 #include "OgreRenderSystem.h"
 #include "OgrePGFCodec.h"
 #include "OgreLogManager.h"
 #include "OgreException.h"
 
 #include "libpgf/PGFimage.h"
 
 namespace Ogre
 {
 
 PGFCodec *PGFCodec::ptr = NULL;
 
 UINT8 *PGFCodec::decodePGF (UINT8 *data, size_t len, int &w, int &h)
 {
    // init PGF object
    CPGFImage pgf;
    // init PGF memory stream
    CPGFMemoryStream s(&data, len);
    
    // open PGF image
    try
    {
       pgf.Open(&s);
       
       pgf.Read(0);
    }
    catch (IOException& e)
    {
       return NULL;
    }
    
    // get properties
    w = pgf.Width();
    h = pgf.Height();
    
    // decode image
    UINT8 *imgdata = new UINT8[pgf.Width()*pgf.Height()*3];
    pgf.GetBitmap(pgf.Width()*3, (UINT8 *)imgdata, 24);
    
    return imgdata;
 }
 
 void PGFCodec::startup ()
 {
    LogManager::getSingleton().logMessage(LML_NORMAL, "Registering PGF codec");
    
    // startup and register PGF codec
    ptr = new PGFCodec ();
    Codec::registerCodec(ptr);
 }
 
 void PGFCodec::shutdown ()
 {
    // shutdown and unregister PGF codec
    Codec::unRegisterCodec (ptr);
    delete ptr;
 }
 
 PGFCodec::PGFCodec()
 {
 }
 
 String PGFCodec::getType() const
 {
    return "pgf";
 }
 
 DataStreamPtr PGFCodec::code(MemoryDataStreamPtr& input, Codec::CodecDataPtr& pData) const
 {
    OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "Encoding not yet supported", "PRFImageCodec::code");
 }
 
 void PGFCodec::codeToFile(MemoryDataStreamPtr& input, const String& outFileName, Codec::CodecDataPtr& pData) const
 {
    OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "Encoding not yet supported", "PRFImageCodec::codeToFile");
 }
 
 Codec::DecodeResult PGFCodec::decode(DataStreamPtr& input) const
 {
    MemoryDataStream memStream(input, true);
    
    // decode image from PGF mem stream
    int w, h;
    UINT8 *data = decodePGF ((UINT8*)memStream.getPtr(), memStream.size(), w, h);
    ImageData* imgData = new ImageData();
    MemoryDataStreamPtr output(new MemoryDataStream(data, imgData->size, true));
    
    // set image properties
    imgData->depth = 1;
    imgData->width = w;
    imgData->height = h;
    imgData->num_mipmaps = 0;
    imgData->flags = 0;
    imgData->format = PF_R8G8B8;
    unsigned dstPitch = imgData->width * PixelUtil::getNumElemBytes(imgData->format);
    imgData->size = dstPitch * imgData->height;
    
    // return decoded image
    DecodeResult ret;
    ret.first = output;
    ret.second = CodecDataPtr(imgData);
    return ret;
 }
 
 PGFCodec::~PGFCodec()
 {
 }
 
 }

Usage:

Just add this 2 files to your project and link against libPGF.
Call PGFCodec::startup() at startup and PGFCodec::shutdown() at shutdown.