1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
// C_Converter
//class to convert images between different pixel formats.
#include "common.h"
#include "glimage.h"
#include "glconverter.h"
struct S_MaskShiftVal
{
int r;
int g;
int b;
int a;
};
struct S_MaskShift
{
S_MaskShiftVal stShift;
S_MaskShiftVal stMask;
S_MaskShiftVal stNumPos;
};
S_MaskShift TBL_LOOKUP[4][4] = {
{ //1 byte
{ {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },
{ {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },
{ {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },
{ {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },
},{ //2 bytes
{ {11,5,0,0}, {(31<<11),(63<<5),(31<<0), 0}, {5,6,5,0} }, //_BGR565
{ {0,5,11,0}, {(31<<0),(63<<5),(31<<11), 0}, {5,6,5,0} }, //_RGB565
{ {10,5,0,15}, {(31<<10),(31<<5),(31<<0), (1<<15)}, {5,5,5,1} }, //_BGR555
{ {0,5,10,15}, {(31<<0),(31<<5),(31<<10), (1<<15)}, {5,5,5,1} }, //_RGB555
},{ //3 bytes
{ {16,8,0,0}, {(255<<16),(255<<8),(255<<0), 0}, {8,8,8,0} }, //_BGR888
{ {0,8,16,0}, {(255<<0),(255<<8),(255<<16), 0}, {8,8,8,0} }, //_RGB888
{ {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },
{ {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },
},{ //4 bytes
{ {16,8,0,24}, {(255<<16),(255<<8),(255<<0), (255<<24)}, {8,8,8,8} }, //_BGRA8888
{ {0,8,16,24}, {(255<<0),(255<<8),(255<<16), (255<<24)}, {8,8,8,8} }, //_RGBA8888
{ {16,8,0,24}, {(255<<16),(255<<8),(255<<0), 0}, {8,8,8,8} }, //_BGRx8888
{ {0,8,16,24}, {(255<<0),(255<<8),(255<<16), 0}, {8,8,8,8} }, //_RGBx8888
}
};
//constructor, sets m_iDstPixelFmt format
C_Converter::C_Converter(int iDstPixelFmt)
{
SetDstPixelFormat(iDstPixelFmt);
}
//set m_iDstPixelFmt format
void C_Converter::SetDstPixelFormat(int iDstPixelFmt)
{
m_iDstPixelFmt = iDstPixelFmt;
m_iDstBytes = iDstPixelFmt & 0x0f;
}
//convert an image to m_iDstPixelFmt format
void C_Converter::Convert(C_Image *src, C_Image **dst, int *o_iResult)
{
if(o_iResult) *o_iResult = 0;
if(!src || !dst) return;
bool bDeleteSrc = *dst == src;
src->GetInfo(&m_iWidth, &m_iHeight, &m_iSrcPixelFmt);
m_iSrcBytes = m_iSrcPixelFmt & 0x0f;
if(m_iSrcPixelFmt == m_iDstPixelFmt) {
*dst = src;
if(!bDeleteSrc) *dst = new C_Image(src); //copy src to dst
if(o_iResult) *o_iResult = 1;
return; //same format; but maybe not original
}
//remove if 16->8, true->8 become supported
if(m_iDstBytes == 1) return;
//no palette...
src->GetBufferMemory(&m_pSrcBuf.u8ptr, &m_pSrcPalBuf);
if((m_iSrcBytes == 1) && !m_pSrcPalBuf) return;
*dst = new C_Image(m_iWidth, m_iHeight, m_iDstPixelFmt);
(*dst)->GetBufferMemory(&m_pDstBuf.u8ptr, NULL);
src->GetLineSizeInfo(NULL, NULL, &m_iSrcAlign);
(*dst)->GetLineSizeInfo(NULL, NULL, &m_iDstAlign);
if(m_iDstBytes == 4) (*dst)->FillWithAbsoluteColor(NULL, 0);
//the above clears the unused byte in 32 bit format.
// makes sense when you use colorkey
if(m_iSrcBytes == 1) _8torgb();
else _rgbtorgb();
if(bDeleteSrc) delete src;
if(o_iResult) *o_iResult = 1;
}
//convert a color in _RGBA8888 format to m_iDstPixelFmt format
uint32_t C_Converter::GetAbsoluteColor(S_Color *pColor)
{
uint32_t iColor = 0;
if(m_iDstBytes == 1) return 0;
if(m_iDstPixelFmt == _RGB888) { //same format?
memcpy(&iColor, pColor, 3);
return iColor;
}
if(m_iDstPixelFmt == _RGBA8888) { //same format?
memcpy(&iColor, pColor, 4);
return iColor;
}
m_iWidth = m_iHeight = 1;
m_iSrcPixelFmt = _RGBA8888;
m_pSrcBuf.u8ptr = (uint8_t *)pColor;
m_iSrcBytes = m_iSrcPixelFmt & 0x0f;
m_pDstBuf.u32ptr = &iColor;
m_iSrcAlign = m_iDstAlign = 0;
_rgbtorgb();
return iColor;
}
//convert a color in m_iDstPixelFmt format to _RGBA8888 format
void C_Converter::GetColor(uint32_t iColor, S_Color *o_pColor)
{
m_iSrcPixelFmt = m_iDstPixelFmt; //hack to get srcformat from constructor
m_iSrcBytes = m_iSrcPixelFmt & 0x0f;
SetDstPixelFormat(_RGBA8888);
if(m_iSrcBytes == 1) return;
o_pColor->a = 0;
if(m_iSrcPixelFmt == _RGB888) { //same format?
memcpy(o_pColor, &iColor, 3);
return;
}
if(m_iSrcPixelFmt == _RGBA8888) { //same format?
memcpy(o_pColor, &iColor, 4);
return;
}
m_iWidth = m_iHeight = 1;
m_iSrcAlign = m_iDstAlign = 0;
m_pSrcBuf.u32ptr = &iColor;
m_pDstBuf.u8ptr = (uint8_t *)o_pColor;
_rgbtorgb();
}
//rgb24->bgr24,rgba32,bgra32,rgb16,bgr16,rgb15,bgr15
//rgba32->bgra32,rgb24,bgr24,rgb16,bgr16,rgb15,bgr15
//rgb16->rgb24,bgr24,rgba32,bgra32,bgr16,rgb15,bgr15
//rgb15->rgb24,bgr24,rgba32,bgra32,rgb16,bgr16,bgr15
//bgr24->rgb24,rgba32,bgra32,rgb16,bgr16,rgb15,bgr15
//bgra32->rgba32,rgb24,bgr24,rgb16,bgr16,rgb15,bgr15
//bgr16->rgb24,bgr24,rgba32,bgra32,rgb16,rgb15,bgr15
//bgr15->rgb24,bgr24,rgba32,bgra32,rgb16,bgr16,rgb15
void C_Converter::_rgbtorgb()
{
S_MaskShiftVal *pstSrcShift = &TBL_LOOKUP[m_iSrcBytes-1][m_iSrcPixelFmt >> 4].stShift;
S_MaskShiftVal *pstDstShift = &TBL_LOOKUP[m_iDstBytes-1][m_iDstPixelFmt >> 4].stShift;
S_MaskShiftVal *pstSrcMask = &TBL_LOOKUP[m_iSrcBytes-1][m_iSrcPixelFmt >> 4].stMask;
S_MaskShiftVal *pstDstMask = &TBL_LOOKUP[m_iDstBytes-1][m_iDstPixelFmt >> 4].stMask;
S_MaskShiftVal *pstSrcNumPos = &TBL_LOOKUP[m_iSrcBytes-1][m_iSrcPixelFmt >> 4].stNumPos;
S_MaskShiftVal *pstDstNumPos = &TBL_LOOKUP[m_iDstBytes-1][m_iDstPixelFmt >> 4].stNumPos;
int iShiftRightR = (pstSrcShift->r - pstDstShift->r) - (pstDstNumPos->r - pstSrcNumPos->r);
int iShiftRightG = (pstSrcShift->g - pstDstShift->g) - (pstDstNumPos->g - pstSrcNumPos->g);
int iShiftRightB = (pstSrcShift->b - pstDstShift->b) - (pstDstNumPos->b - pstSrcNumPos->b);
int iShiftRightA = (pstSrcShift->a - pstDstShift->a) - (pstDstNumPos->a - pstSrcNumPos->a);
uint32_t iSrcCol, iDstCol, iDstColR, iDstColG, iDstColB, iDstColA;
for(int y=0; y<m_iHeight; y++) {
for(int x=0; x<m_iWidth; x++) {
memcpy(&iSrcCol, m_pSrcBuf.u8ptr, m_iSrcBytes);
iDstColR = iShiftRightR>=0
? (iSrcCol >> iShiftRightR) & pstDstMask->r
: (iSrcCol << -iShiftRightR) & pstDstMask->r;
iDstColG = iShiftRightG>=0
? (iSrcCol >> iShiftRightG) & pstDstMask->g
: (iSrcCol << -iShiftRightG) & pstDstMask->g;
iDstColB = iShiftRightB>=0
? (iSrcCol >> iShiftRightB) & pstDstMask->b
: (iSrcCol << -iShiftRightB) & pstDstMask->b;
iDstCol = iDstColR | iDstColG | iDstColB;
if(pstSrcMask->a == 0) { //no alpha source
/*!*/if(iDstCol == 0) iDstColA = 0; //set transparancy
/*!*/else iDstColA = 0xffffffff & pstDstMask->a;
} else {
iDstColA = iShiftRightA>=0
? (iSrcCol >> iShiftRightA) & pstDstMask->a
: (iSrcCol << -iShiftRightA) & pstDstMask->a;
}
iDstCol |= iDstColA;
memcpy(m_pDstBuf.u32ptr, &iDstCol, m_iDstBytes);
//m_pDstBuf.u32ptr[0] = iDstCol;
m_pSrcBuf.u8ptr += (int)m_iSrcBytes;
m_pDstBuf.u8ptr += (int)m_iDstBytes;
}
m_pDstBuf.u8ptr += m_iDstAlign;
m_pSrcBuf.u8ptr += m_iSrcAlign;
}
}
//8pal->rgb24,bgr24,rgba32,bgra32,rgb16,bgr16,rgb15,bgr15
void C_Converter::_8torgb()
{
S_MaskShiftVal *pstDstShift = &TBL_LOOKUP[m_iDstBytes-1][m_iDstPixelFmt >> 4].stShift;
S_MaskShiftVal *pstDstMask = &TBL_LOOKUP[m_iDstBytes-1][m_iDstPixelFmt >> 4].stMask;
S_MaskShiftVal *pstDstNumPos = &TBL_LOOKUP[m_iDstBytes-1][m_iDstPixelFmt >> 4].stNumPos;
int iShiftRightR = -pstDstShift->r - (pstDstNumPos->r - 8);
int iShiftRightG = -pstDstShift->g - (pstDstNumPos->g - 8);
int iShiftRightB = -pstDstShift->b - (pstDstNumPos->b - 8);
int iShiftRightA = -pstDstShift->a - (pstDstNumPos->a - 8);
uint32_t iDstCol, iDstColR, iDstColG, iDstColB, iDstColA, iIdx;
for(int y=0; y<m_iHeight; y++) {
for(int x=0; x<m_iWidth; x++) {
iIdx = *m_pSrcBuf.u8ptr++;
iIdx <<= 2; //get to start in palette
iDstColR = iShiftRightR>=0
? (m_pSrcPalBuf[iIdx+0] >> iShiftRightR) & pstDstMask->r
: (m_pSrcPalBuf[iIdx+0] << -iShiftRightR) & pstDstMask->r;
iDstColG = iShiftRightG>=0
? (m_pSrcPalBuf[iIdx+1] >> iShiftRightG) & pstDstMask->g
: (m_pSrcPalBuf[iIdx+1] << -iShiftRightG) & pstDstMask->g;
iDstColB = iShiftRightB>=0
? (m_pSrcPalBuf[iIdx+2] >> iShiftRightB) & pstDstMask->b
: (m_pSrcPalBuf[iIdx+2] << -iShiftRightB) & pstDstMask->b;
iDstCol = iDstColR | iDstColG | iDstColB;
//if(iDstCol == 0) iDstColA = 255; //set transparancy
iDstColA = iShiftRightA>=0
? (m_pSrcPalBuf[iIdx+3] >> iShiftRightA) & pstDstMask->a
: (m_pSrcPalBuf[iIdx+3] << -iShiftRightA) & pstDstMask->a;
iDstCol |= iDstColA;
memcpy(m_pDstBuf.u32ptr, &iDstCol, m_iDstBytes);
//m_pDstBuf.u32ptr[0] = iDstCol;
m_pDstBuf.u8ptr += (int)m_iDstBytes;
}
m_pDstBuf.u8ptr += m_iDstAlign;
m_pSrcBuf.u8ptr += m_iSrcAlign;
}
}
|