Click here to Skip to main content
11,798,866 members (61,553 online)
Click here to Skip to main content

Adobe Photoshop file loader template class

, 29 Jan 2002 87.4K 637 16
Rate this:
Please Sign up or sign in to vote.
A template class to making loading Adobe Photoshop files easier


Here is a quick way to load color, alpha and selection channel data from an flattened* Adobe Photoshop image file.  

* If Photoshop's save file preference is set to maximize compatibility with older versions the file will contain a pre-composited version of the image will be saved along with the layers. ).

Example Use

typedef BPT::TPSDFileLoader<ExampleSurface> loader_type;

typedef loader_type::channel_collection_type::iterator channel_iterator;

loader_type psdLoader;

loader_type::channel_collection_type * pChannels = psdLoader.Load( pszFilename );

if ( pChannels ){

    int channelCount = pChannels->size();

    std::cout << "There are " << channelCount << "channels.\n" << std::endl;

    int channel = 1;

    for ( channel_iterator it = pChannels->begin(); it != pChannels->end(); it++ ) {

        ExampleSurface * pSurface = *it;

        if ( 1 == channel ) {
            std::cout << "Red ";
        } else if ( 2 == channel ) {
            std:cout << "Green ";
        } else if ( 3 == channel ) {
            std:cout << "Blue ";
        } else {
            std:cout << "Alpha ";

        std::cout << "Channel " << channel << "of " << channelCount;
        std::cout << " Width " << pSurface->Width();
        std::cout << " Height " << pSurface->Height();
        std::cout << endl;



    psdLoader.Unload( pChannels );


The TPSDFileLoader template parameters

    ,class INPUT_POLICY = TFileIO<>
    ,class SIZE_COLLECTION = std::vector<int>
class TPSDFileLoader { 

The default template parameters are reasonable defaults for use in a Win32 environment. However if you want to bring the code to a slightly different platform or you have specific storage needs you can override defaults with your types. If you are fine with the defaults, the CHANNEL_SURFACE is the only type you *must* specify. 


The loader references only four methods ( Create(), Width(), Height() and Iterator() ) and one exposed type ( pixel_iterator ) from the CHANNEL_SURFACE type.  The interface is relatively small it should be easy to write an adaptor class for most bitmap representations.  Here is an example surface that conforms to the loader's CHANNEL_SURFACE use.

struct ExampleSurface {

    typedef BYTE pixel_type;
    typedef pixel_type * pixel_iterator;

    SIZE m_Size;
    pixel_iterator m_Storage;

    int Width() { return; }
    int Height() { return; }

    pixel_iterator Iterator( const int x, const int y ) {
        if ( !m_Storage ) return 0;
        return m_Storage + (y * + x;

    void Destroy() {
        if ( m_Storage ) {
            delete [] m_Storage;
            m_Storage = 0;
        } = = 0;

    bool Create( const int width, const int height ) {


        m_Storage = new pixel_type [ width * height ]; = width; = height;

        return (0 != m_Storage);

    ~ExampleSurface() { Destroy(); }

    ExampleSurface() : m_Storage(0) { = = 0; }



CHANNEL_COLLECTION is the collection type that will be used to return the channel data. 



This policy class is used to provide low-level input capabilities; the default class is simply a wrapper around the fopen/fclose and related basic read methods. It has methods for reading 16 bit and 32 bit values stored in endian specific fashion.  In addition to providing the necessary read interface it also hides memory allocation/deallocation details when reading chunks of data into memory. 

struct ExampleInputPolicy {
    bool Open( const char * filename, const char * access );
    void Close();
    void Seek( const int pos, const int mode );
    int Read_Byte();
    int Read_m16();
    int Read_m32();
    BYTE * LoadData( const int nBytes );
    void UnloadData( BYTE * ptr );


Internally the size of each channel is temporarily cached to make the loader implementation simpler. 

class SIZECOLLECTION = std::vector<int>


if you want to eliminate the dependency on the STL you can do so by specifying a different collection type for both the SIZE_COLLECTION and CHANNEL_COLLECTION.


If you plan on using the default implementation you will need to include the list, vector headers. 

#include <list>
#include <vector>

#include "psd_load_src.h"

Public Methods

Unload() destroys the returned channel collection.

void Unload( CHANNEL_COLLECTION * pLoadedData );

LoadFromAbstraction() is the low-level public method, it is used to read from the IO policy object passed.

template< class INPUT > CHANNEL_COLLECTION *
LoadFromAbstraction( INPUT & input, const int nLoadChannelCountMax = 0 );

Load() -- accepts a null terminated string and uses the INPUT_POLICY and LoadFromAbstraction() to load the channel data.

Load( const char * filename, const int nLoadChannelCountMax = 0 );


It's common to need access to the individual channel data in an Adobe Photoshop file, so this template saves a lot of time and effort.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Brad P. Taylor
Web Developer
United States United States
Programming since 1984, mostly in c/c++ and assembly language when necessary.

You may also be interested in...

Comments and Discussions

GeneralImageMagick Pin
Uwe Keim22-Jan-02 21:29
memberUwe Keim22-Jan-02 21:29 
GeneralImgSource Pin
Chris Losinger23-Jan-02 4:52
memberChris Losinger23-Jan-02 4:52 
GeneralRe: ImgSource Pin
Oz Solomonovich23-Jan-02 6:54
memberOz Solomonovich23-Jan-02 6:54 
GeneralRe: ImgSource Pin
Chris Losinger23-Jan-02 7:50
memberChris Losinger23-Jan-02 7:50 
GeneralRe: ImgSource Pin
Brad P. Taylor23-Jan-02 8:14
memberBrad P. Taylor23-Jan-02 8:14 
GeneralRe: ImgSource Pin
Chris Losinger23-Jan-02 9:40
memberChris Losinger23-Jan-02 9:40 
GeneralRe: ImgSource Pin
Brad P. Taylor23-Jan-02 12:20
memberBrad P. Taylor23-Jan-02 12:20 
GeneralRe: ImgSource Pin
Chris Losinger23-Jan-02 9:48
memberChris Losinger23-Jan-02 9:48 
GeneralRe: ImgSource Pin
Brad P. Taylor23-Jan-02 12:26
memberBrad P. Taylor23-Jan-02 12:26 
GeneralRe: ImgSource Pin
Neal Josephson30-Jan-02 21:18
memberNeal Josephson30-Jan-02 21:18 
GeneralRe: ImgSource Pin
Swinefeaster30-Jun-02 0:11
memberSwinefeaster30-Jun-02 0:11 
GeneralRe: ImgSource Pin
Anonymous16-Jul-02 0:36
sussAnonymous16-Jul-02 0:36 
GeneralRe: ImgSource Pin
Chris Losinger16-Jul-02 3:56
memberChris Losinger16-Jul-02 3:56 
GeneralRe: ImgSource Pin
Christian Graus16-Jul-02 11:05
memberChristian Graus16-Jul-02 11:05 
GeneralRe: ImgSource [XPost] Pin
Jonas Beckeman28-Jul-05 9:39
memberJonas Beckeman28-Jul-05 9:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 30 Jan 2002
Article Copyright 2002 by Brad P. Taylor
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid