Newer
Older
Import / research / signals-slots / src / gui / format.h
#ifndef PIXEL_FORMAT_H
#define PIXEL_FORMAT_H


/*
    Format description
 
        0xAAAABBBB
            AAAA is order of components
            BBBB is size of each component
            (only allows up to 4 components)

        For order of components the following values are used:
            0 = Nothing
            1 = Red
            2 = Green
            3 = Blue
            4 = Alpha
            5 = PremultipliedAlpha
            6 = Mask
*/
enum PixelFormat
{
    Format_Invalid = 0,

    // 32bit with standard alpha
    Format_RGBA_8888 = 0x12348888,
    Format_ARGB_8888 = 0x41238888, // Alpha first
    Format_ABGR_8888 = 0x43218888, // Reversed byte order
    Format_BGRA_8888 = 0x32148888,

    // 32bit with premultiplied alpha
    Format_RGBP_8888 = 0x12358888,
    Format_PRGB_8888 = 0x51238888,
    Format_PBGR_8888 = 0x53218888,
    Format_BGRP_8888 = 0x32158888,

    // 32bit with no alpha
    Format_RGB0_8888 = 0x12308888,
    Format_0RGB_8888 = 0x01238888,
    Format_0BGR_8888 = 0x03218888,
    Format_BGR0_8888 = 0x32108888,

    // 24bit
    Format_0RGB_0888 = 0x01230888,
    Format_0BGR_0888 = 0x03210888,

    // 18bit
    Format_ARGB_6666 = 0x41236666,
    Format_0RGB_6666 = 0x01236666,
    Format_MRGB_1666 = 0x61236666,

    // 16bit
    Format_0RGB_0565 = 0x01230565,
    Format_0BGR_0565 = 0x03210565,
    Format_ARGB_1555 = 0x61231555,
    Format_ABGR_1555 = 0x63211555,

    // 15bit
    Format_0RGB_1555 = 0x01231555,
    Format_0BGR_1555 = 0x03211555,
};


/*
    Macros

        FORMAT_BITS_PER_PIXEL(format)       Returns the number of bits per pixel of the format

        FORMAT_RED_BITS(format)             Returns the number of red bits per pixel of the format
        FORMAT_GREEN_BITS(format)           Returns the number of green bits per pixel of the format
        FORMAT_BLUE_BITS(format)            Returns the number of blue bits per pixel of the format
        FORMAT_ALPHA_BITS(format)           Returns the number of alpha bits per pixel of the format

        FORMAT_RED_SHIFT(format)            Returns the offset of the red bits in a pixel for the format
        FORMAT_GREEN_SHIFT(format)          Returns the offset of the green bits in a pixel for the format
        FORMAT_BLUE_SHIFT(format)           Returns the offset of the blue bits in a pixel for the format
        FORMAT_ALPHA_SHIFT(format)          Returns the offset of the alpha bits in a pixel for the format
*/
#define FORMAT_BITS_PER_PIXEL(fmt)      \
( (fmt&0x000F) + ((fmt&0x00F0)>>4) + ((fmt&0x0F00)>>8) + ((fmt&0xF000)>>12) )

#define FORMAT_RED_INDEX(fmt)   \
    (((fmt&0xF0000000)==0x10000000)?1: \
     (((fmt&0x0F000000)==0x01000000)?2: \
      (((fmt&0x00F00000)==0x00100000)?3: \
       (((fmt&0x000F0000)==0x00010000)?4:0))))
#define FORMAT_GREEN_INDEX(fmt) \
    (((fmt&0xF0000000)==0x20000000)?1: \
     (((fmt&0x0F000000)==0x02000000)?2: \
      (((fmt&0x00F00000)==0x00200000)?3: \
       (((fmt&0x000F0000)==0x00020000)?4:0))))
#define FORMAT_BLUE_INDEX(fmt) \
    (((fmt&0xF0000000)==0x30000000)?1: \
     (((fmt&0x0F000000)==0x03000000)?2: \
      (((fmt&0x00F00000)==0x00300000)?3: \
       (((fmt&0x000F0000)==0x00030000)?4:0))))
#define FORMAT_ALPHA_INDEX(fmt) \
    (((fmt&0xF0000000)==0x40000000)?1: \
     (((fmt&0x0F000000)==0x04000000)?2: \
      (((fmt&0x00F00000)==0x00400000)?3: \
       (((fmt&0x000F0000)==0x00040000)?4:0))))

const static int fmt1[] = { 0, 0xF000, 0x0F00, 0x00F0, 0x000F };
const static int sht1[] = { 32, 12, 8, 4, 0 };
const static int fmt2[] = { 0, 0x0FFF, 0x00FF, 0x000F, 0x0000 };

#define FORMAT_RED_BITS(fmt) \
    ( (fmt & fmt1[FORMAT_RED_INDEX(fmt)]) >> sht1[FORMAT_RED_INDEX(fmt)] )
#define FORMAT_GREEN_BITS(fmt) \
    ( (fmt & fmt1[FORMAT_GREEN_INDEX(fmt)]) >> sht1[FORMAT_GREEN_INDEX(fmt)] )
#define FORMAT_BLUE_BITS(fmt) \
    ( (fmt & fmt1[FORMAT_BLUE_INDEX(fmt)]) >> sht1[FORMAT_BLUE_INDEX(fmt)] )
#define FORMAT_ALPHA_BITS(fmt) \
    ( (fmt & fmt1[FORMAT_ALPHA_INDEX(fmt)]) >> sht1[FORMAT_ALPHA_INDEX(fmt)] )

#define FORMAT_RED_SHIFT(fmt)   \
    ( ((fmt&(0x000F&fmt2[FORMAT_RED_INDEX(fmt)]))>>0) + \
      ((fmt&(0x00F0&fmt2[FORMAT_RED_INDEX(fmt)]))>>4) + \
      ((fmt&(0x0F00&fmt2[FORMAT_RED_INDEX(fmt)]))>>8) + \
      ((fmt&(0xF000&fmt2[FORMAT_RED_INDEX(fmt)]))>>12) )
#define FORMAT_GREEN_SHIFT(fmt) \
    ( ((fmt&(0x000F&fmt2[FORMAT_GREEN_INDEX(fmt)]))>>0) + \
      ((fmt&(0x00F0&fmt2[FORMAT_GREEN_INDEX(fmt)]))>>4) + \
      ((fmt&(0x0F00&fmt2[FORMAT_GREEN_INDEX(fmt)]))>>8) + \
      ((fmt&(0xF000&fmt2[FORMAT_GREEN_INDEX(fmt)]))>>12) )
#define FORMAT_BLUE_SHIFT(fmt)  \
    ( ((fmt&(0x000F&fmt2[FORMAT_BLUE_INDEX(fmt)]))>>0) + \
      ((fmt&(0x00F0&fmt2[FORMAT_BLUE_INDEX(fmt)]))>>4) + \
      ((fmt&(0x0F00&fmt2[FORMAT_BLUE_INDEX(fmt)]))>>8) + \
      ((fmt&(0xF000&fmt2[FORMAT_BLUE_INDEX(fmt)]))>>12) )
#define FORMAT_ALPHA_SHIFT(fmt) \
    ( ((fmt&(0x000F&fmt2[FORMAT_ALPHA_INDEX(fmt)]))>>0) + \
      ((fmt&(0x00F0&fmt2[FORMAT_ALPHA_INDEX(fmt)]))>>4) + \
      ((fmt&(0x0F00&fmt2[FORMAT_ALPHA_INDEX(fmt)]))>>8) + \
      ((fmt&(0xF000&fmt2[FORMAT_ALPHA_INDEX(fmt)]))>>12) )


#endif // PIXEL_FORMAT_H