This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

| Pixel Format Class Submitted by

Pixel Formats are used to define the type of pixels supported by a display mode or surface. Often programmers need to be able to convert between different pixel format types when converting from 24bit, to 32bit, or 32 bit to 16bit pixels. The CPixelFormat class provided here allows programmers to create their own pixel formats and also provides methods that convert pixels and pixel streams from one pixel format to another.
If you have any questions, concerns, or comments, please feel free to contact me.
Thank you, Tobin Schwaiger-Hastanan (aka Hasty)
tobin@13hex.com

Currently browsing [pixelformat.zip] (3,260 bytes) - [pixelformat.h] - (5,551 bytes)

    /* ************************************************************************** */
    // File       : pixelformat.h
    // Programmer : Tobin Schwaiger-Hastanan
    // Copyright (c) 1997-1999 Tobin Schwaiger-Hastanan
    // Notes      :
    /* ************************************************************************** */

    #ifndef _PIXELFORMAT_DAWT_H_INCLUDED_
    #define _PIXELFORMAT_DAWT_H_INCLUDED_

    /*
    required type definitions -

    typedef unsigned char uchar;
    typedef unsigned long uint32;
    */

    /* ************************************************************************** */
    // CColor class
    /* ************************************************************************** */
    class CColor
    {
    public:
    	union
    	{
    		float c[4];
    		struct
    		{
    	        float red;
    	        float green;
    	        float blue;
    	        float alpha;
    		};
    	};
    public:
    	CColor( float Red = 0.0f, float Green = 0.0f, float Blue = 0.0f, float Alpha = 0.0f )
    	{
    		red   = Red;
    		green = Green;
    		blue  = Blue;
    		alpha = Alpha;
    	}

    	float GetRed()   const { return red;   }
    	float GetGreen() const { return green; }
    	float GetBlue()  const { return blue;  }
    	float GetAlpha() const { return alpha; }

    	void SetRed( float Red )     { red   = Red;   }
    	void SetGreen( float Green ) { green = Green; }
    	void SetBlue( float Blue )   { blue  = Blue;  }
    	void SetAlpha( float Alpha ) { alpha = Alpha; }
    };

    /* ************************************************************************** */
    // CPixelFormat class
    /* ************************************************************************** */
    class CPixelFormat
    {
    public:
    	CPixelFormat( int BPP = 32, 			 uint32 RedMask  = 0xFF0000,
    				  uint32 GreenMask = 0xFF00, uint32 BlueMask = 0xFF,
    				  uint32 AlphaMask = 0x0 );

    	int    GetBPP()	      const { return m_BPP; }
    	uint32 GetRedMask()   const { return m_RedMask;   }
    	uint32 GetGreenMask() const { return m_GreenMask; }
    	uint32 GetBlueMask()  const { return m_BlueMask;  }
    	uint32 GetAlphaMask() const { return m_AlphaMask; }

    	uint32 BuildPixel( float Red, float Green, float Blue,
    					   float Alpha = 0.0f ) const;

    	uint32 BuildPixel( uint32 Red, uint32 Green, uint32 Blue,
    					   uint32 Alpha = 0.0f ) const;

    	uint32 BuildPixel( const CColor& Color ) const;

    	uint32 GetRedBits( uint32 Pixel )   const;
    	uint32 GetGreenBits( uint32 Pixel ) const;
    	uint32 GetBlueBits( uint32 Pixel )  const;
    	uint32 GetAlphaBits( uint32 Pixel ) const;

    	uint32 ConvertStream( uchar* pDest, uint32 DestLength, const CPixelFormat& pfSource, uchar* pSrc, uint32 SrcLength ) const;

    	friend inline int operator==( const CPixelFormat& pf1, const CPixelFormat& pf2 );
        friend inline int operator!=( const CPixelFormat& pf1, const CPixelFormat& pf2 );

    private:
    	int    m_BPP;           // Bits per pixel

        // bit mask required for each element (RGBA combos)
    	uint32 m_RedMask;       // Red bit mask
    	uint32 m_GreenMask;     // Green bit mask
    	uint32 m_BlueMask;      // Blue bit mask
    	uint32 m_AlphaMask;     // Alpha bitmask

        // number of bit shifts required for each element
    	int m_RedShift;         // Red shift bits
    	int m_GreenShift;       // Green shift bits
    	int m_BlueShift;        // Blue shift bits
    	int m_AlphaShift;       // Alpha shift bits

        // number of bits required for each element.
    	int m_RedBits;          // Red bits
    	int m_GreenBits;        // Green bits
    	int m_BlueBits;         // Blue bits
    	int m_AlphaBits;        // Alpha bits

    	int GetShiftBits( uint32 Mask )
    	{
    		for( int shift = 0; ( ~Mask & 1 ) && shift < 32; Mask >>= 1 )
    			shift ++;
    		return shift;
    	}

    	int GetBits( uint32 Mask )
    	{
    		Mask >>= GetShiftBits( Mask );
    		for( int bits = 0; ( Mask & 1 ) && bits < 32; Mask >>= 1 )
    			bits ++;
    		return bits;
    	}
    };

    // Standard pixel formats used
    const CPixelFormat RGB_555   = CPixelFormat( 16, 0x7C00,   0x3E0,  0x1F );
    const CPixelFormat RGB_565   = CPixelFormat( 16, 0xF800,   0x7E0,  0x1F );
    const CPixelFormat RGB_24    = CPixelFormat( 24, 0xFF0000, 0xFF00, 0xFF );
    const CPixelFormat RGB_32    = CPixelFormat( 32, 0xFF0000, 0xFF00, 0xFF );
    const CPixelFormat RGBA_1555 = CPixelFormat( 16, 0x7C00,   0x3E0,  0x1F, 0x8000 );
    const CPixelFormat RGBA_24   = CPixelFormat( 24, 0xFF0000, 0xFF00, 0xFF, 0xFF000000 );
    const CPixelFormat RGBA_32   = CPixelFormat( 32, 0xFF0000, 0xFF00, 0xFF, 0xFF000000 );
    const CPixelFormat BGR_555   = CPixelFormat( 16, 0x1F,     0x3E0,  0x7C00 );
    const CPixelFormat BGR_565   = CPixelFormat( 16, 0x1F,     0x7E0,  0xF800 );
    const CPixelFormat BGR_24    = CPixelFormat( 24, 0xFF,     0xFF00, 0xFF0000 );
    const CPixelFormat BGR_32    = CPixelFormat( 24, 0xFF,     0xFF00, 0xFF0000 );

    /* ************************************************************************** */
    // CPixelFormat::operator==( ... )
    // - Pixel format compare operator
    /* ************************************************************************** */
    inline int operator==( const CPixelFormat& pf1, const CPixelFormat& pf2 )
    {
    	return	pf1.m_BPP		== pf2.m_BPP	   &&
    			pf1.m_RedMask   == pf2.m_RedMask   &&
    			pf1.m_GreenMask == pf2.m_GreenMask &&
    			pf1.m_BlueMask  == pf2.m_BlueMask  &&
    			pf1.m_AlphaMask == pf2.m_AlphaMask;
    }

    inline int operator!=( const CPixelFormat& pf1, const CPixelFormat& pf2 )
    {
    	return	pf1.m_BPP		!= pf2.m_BPP	   ||
    			pf1.m_RedMask   != pf2.m_RedMask   ||
    			pf1.m_GreenMask != pf2.m_GreenMask ||
    			pf1.m_BlueMask  != pf2.m_BlueMask  ||
    			pf1.m_AlphaMask != pf2.m_AlphaMask;
    }

    #endif

Currently browsing [pixelformat.zip] (3,260 bytes) - [pixelformat.cpp] - (7,148 bytes)

    /* ************************************************************************** */
    // File       : pixelformat.cpp
    // Programmer : Tobin Schwaiger-Hastanan
    // Copyright (c) 1997-1999 Tobin Schwaiger-Hastanan
    // Notes      :
    /* ************************************************************************** */

    #include "pixelformat.h"

    /*
    required type definitions -

    typedef unsigned char uchar;
    typedef unsigned long uint32;
    */

    /* ************************************************************************** */
    // CPixelFormat::CPixelFormat( ... )
    // - CPixelFormat constructor
    // - Initializes member variables
    /* ************************************************************************** */
    CPixelFormat::CPixelFormat( int BPP,		  uint32 RedMask,
    							uint32 GreenMask, uint32 BlueMask,
    							uint32 AlphaMask )
    {
    	m_BPP = BPP;

    	m_RedMask	= RedMask;
    	m_GreenMask = GreenMask;
    	m_BlueMask  = BlueMask;
    	m_AlphaMask = AlphaMask;

    	m_RedShift   = GetShiftBits( m_RedMask );
    	m_GreenShift = GetShiftBits( m_GreenMask );
    	m_BlueShift  = GetShiftBits( m_BlueMask );
    	m_AlphaShift = GetShiftBits( m_AlphaMask );

    	m_RedBits   = GetBits( m_RedMask );
    	m_GreenBits = GetBits( m_GreenMask );
    	m_BlueBits  = GetBits( m_BlueMask );
    	m_AlphaBits = GetBits( m_AlphaMask );
    }

    /* ************************************************************************** */
    // CPixelFormat::BuildPixel( ... )
    // - Builds a pixel from the floating point parameters provide to the pixel
    //   format specified by the member variables.
    // - Floating point color values range from 0.0 to 1.0
    /* ************************************************************************** */
    uint32 CPixelFormat::BuildPixel( float Red, float Green, float Blue,
    				   float Alpha ) const
    {
    	uint32 pixel = 0;

    	pixel |= uint32( m_RedMask   * Red   ) & m_RedMask;
    	pixel |= uint32( m_GreenMask * Green ) & m_GreenMask;
    	pixel |= uint32( m_BlueMask  * Blue  ) & m_BlueMask;
    	pixel |= uint32( m_AlphaMask * Alpha ) & m_AlphaMask;

    	return pixel;
    }

    /* ************************************************************************** */
    // CPixelFormat::BuildPixel( ... )
    // - Builds a pixel from the uint32 parameters provide to the pixel
    //   format specified by the member variables.
    // - Integer color values range from 0 to 255
    /* ************************************************************************** */
    uint32 CPixelFormat::BuildPixel( uint32 Red, uint32 Green, uint32 Blue,
    				   uint32 Alpha ) const
    {
    	uint32 pixel = 0;

    	Red   >>= 8 - m_RedBits;
    	Green >>= 8 - m_GreenBits;
    	Blue  >>= 8 - m_BlueBits;
    	Alpha >>= 8 - m_AlphaBits;

    	pixel |= ( Red   << m_RedShift   ) & m_RedMask;
    	pixel |= ( Green << m_GreenShift ) & m_GreenMask;
    	pixel |= ( Blue  << m_BlueShift  ) & m_BlueMask;
    	pixel |= ( Alpha << m_AlphaShift ) & m_AlphaMask;

    	return pixel;
    }

    /* ************************************************************************** */
    // CPixelFormat::BuildPixel( ... )
    // - Builds a pixel from the floating point parameters provide to the pixel
    //   format specified by the member variables.
    /* ************************************************************************** */
    uint32 CPixelFormat::BuildPixel( const CColor& Color ) const
    {
    	uint32 pixel = 0;

    	pixel |= uint32( Color.GetRed()   * m_RedMask   ) & m_RedMask;
    	pixel |= uint32( Color.GetGreen() * m_GreenMask ) & m_GreenMask;
    	pixel |= uint32( Color.GetBlue()  * m_BlueMask  ) & m_BlueMask;
    	pixel |= uint32( Color.GetAlpha() * m_AlphaMask ) & m_AlphaMask;

    	return pixel;
    }

    /* ************************************************************************** */
    // CPixelFormat::GetRedBits( ... )
    // - Extract and returns the red bits from a pixel matching the pixel
    //   format specified by the member variables
    // - Return value ranges from 0 to 255
    /* ************************************************************************** */
    uint32 CPixelFormat::GetRedBits( uint32 Pixel ) const
    {
    	Pixel   = ( Pixel & m_RedMask ) >> m_RedShift;
    	Pixel <<= ( 8 - m_RedBits );
    	return Pixel;
    }

    /* ************************************************************************** */
    // CPixelFormat::GetGreenBits( ... )
    // - Extract and returns the green bits from a pixel matching the pixel
    //   format specified by the member variables
    // - Return value ranges from 0 to 255
    /* ************************************************************************** */
    uint32 CPixelFormat::GetGreenBits( uint32 Pixel ) const
    {
    	Pixel   = ( Pixel & m_GreenMask ) >> m_GreenShift;
    	Pixel <<= ( 8 - m_GreenBits );
    	return Pixel;
    }

    /* ************************************************************************** */
    // CPixelFormat::GetBlueBits( ... )
    // - Extract and returns the blue bits from a pixel matching the pixel
    //   format specified by the member variables
    // - Return value ranges from 0 to 255
    /* ************************************************************************** */
    uint32 CPixelFormat::GetBlueBits( uint32 Pixel ) const
    {
    	Pixel   = ( Pixel & m_BlueMask ) >> m_BlueShift;
    	Pixel <<= ( 8 - m_BlueBits );
    	return Pixel;
    }

    /* ************************************************************************** */
    // CPixelFormat::GetAlphaBits( ... )
    // - Extract and returns the alpha bits from a pixel matching the pixel
    //   format specified by the member variables
    // - Return value ranges from 0 to 255
    /* ************************************************************************** */
    uint32 CPixelFormat::GetAlphaBits( uint32 Pixel ) const
    {
    	Pixel   = ( Pixel & m_AlphaMask ) >> m_AlphaShift;
    	Pixel <<= ( 8 - m_AlphaBits );
    	return Pixel;
    }

    /* ************************************************************************** */
    // CPixelFormat::ConvertStream( ... )
    // - Converts a pixel stream of a specific pixel format to the pixel format
    //   specified by the member variables.
    // - Returns the number of bytes used in the destination buffer during
    //   conversion
    /* ************************************************************************** */
    uint32 CPixelFormat::ConvertStream( uchar* pDest, uint32 DestLength, const CPixelFormat& pfSource, uchar* pSrc, uint32 SrcLength ) const
    {
    	uchar* pDestEnd, *pSrcEnd;
    	int    SrcBytesPP, DestBytesPP;
    	int    SrcBPP, DestBPP;
    	uint32 SrcRed, SrcGreen, SrcBlue, SrcAlpha;

    	SrcBPP  = pfSource.GetBPP();
    	DestBPP = GetBPP();

    	DestBytesPP = DestBPP >> 3;
    	SrcBytesPP  = SrcBPP  >> 3;

    	pDestEnd = pDest + DestLength;
    	pSrcEnd  = pSrc  + SrcLength;

    	while( pDest < pDestEnd && pSrc < pSrcEnd )
    	{
    		uint32 SrcPixel = 0, DestPixel;

    		for( int i = 0; i < SrcBytesPP; i ++)
    			SrcPixel |= (uchar) (*pSrc++) << ( i * 8 );

    		SrcRed    = pfSource.GetRedBits( SrcPixel );
    		SrcGreen  = pfSource.GetGreenBits( SrcPixel );
    		SrcBlue   = pfSource.GetBlueBits( SrcPixel );
    		SrcAlpha  = pfSource.GetAlphaBits( SrcPixel );
    		DestPixel = BuildPixel( SrcRed, SrcGreen, SrcBlue, SrcAlpha );

    		for( i = 0; i < DestBytesPP; i ++ )
    			*pDest++ = (uchar) ( DestPixel >> (i * 8) );
    	}

    	return pDestEnd - pDest;
    }

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.