| 1 | /* |
| 2 | Copyright (C) 2003 Rice1964 |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU General Public License |
| 6 | as published by the Free Software Foundation; either version 2 |
| 7 | of the License, or (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | |
| 18 | */ |
| 19 | |
| 20 | #ifndef __CONVERTIMAGE_H__ |
| 21 | #define __CONVERTIMAGE_H__ |
| 22 | |
| 23 | #include "typedefs.h" |
| 24 | #include "Texture.h" |
| 25 | #include "TextureManager.h" |
| 26 | |
| 27 | |
| 28 | static const uint8 OneToEight[2] = |
| 29 | { |
| 30 | 0x00, // 0 -> 00 00 00 00 |
| 31 | 0xff // 1 -> 11 11 11 11 |
| 32 | }; |
| 33 | |
| 34 | static const uint8 OneToFour[2] = |
| 35 | { |
| 36 | 0x00, // 0 -> 00 00 |
| 37 | 0x0f // 1 -> 11 11 |
| 38 | }; |
| 39 | |
| 40 | static const uint8 TwoToEight[4] = |
| 41 | { |
| 42 | 0x00, // 00 -> 00 00 00 00 |
| 43 | 0x55, // 01 -> 01 01 01 01 |
| 44 | 0xaa, // 10 -> 10 10 10 10 |
| 45 | 0xff // 11 -> 11 11 11 11 |
| 46 | }; |
| 47 | |
| 48 | static const uint8 TwoToFour[4] = |
| 49 | { |
| 50 | 0x0, // 00 -> 00 00 |
| 51 | 0x5, // 01 -> 01 01 |
| 52 | 0xa, // 10 -> 10 10 |
| 53 | 0xf // 11 -> 11 11 |
| 54 | }; |
| 55 | |
| 56 | static const uint8 ThreeToEight[8] = |
| 57 | { |
| 58 | 0x00, // 000 -> 00 00 00 00 |
| 59 | 0x24, // 001 -> 00 10 01 00 |
| 60 | 0x49, // 010 -> 01 00 10 01 |
| 61 | 0x6d, // 011 -> 01 10 11 01 |
| 62 | 0x92, // 100 -> 10 01 00 10 |
| 63 | 0xb6, // 101 -> 10 11 01 10 |
| 64 | 0xdb, // 110 -> 11 01 10 11 |
| 65 | 0xff // 111 -> 11 11 11 11 |
| 66 | }; |
| 67 | |
| 68 | static const uint8 ThreeToFour[8] = |
| 69 | { |
| 70 | 0x0, // 000 -> 00 00 00 00 |
| 71 | 0x2, // 001 -> 00 10 01 00 |
| 72 | 0x4, // 010 -> 01 00 10 01 |
| 73 | 0x6, // 011 -> 01 10 11 01 |
| 74 | 0x9, // 100 -> 10 01 00 10 |
| 75 | 0xb, // 101 -> 10 11 01 10 |
| 76 | 0xd, // 110 -> 11 01 10 11 |
| 77 | 0xf // 111 -> 11 11 11 11 |
| 78 | }; |
| 79 | |
| 80 | static const uint8 FourToEight[16] = |
| 81 | { |
| 82 | 0x00, 0x11, 0x22, 0x33, |
| 83 | 0x44, 0x55, 0x66, 0x77, |
| 84 | 0x88, 0x99, 0xaa, 0xbb, |
| 85 | 0xcc, 0xdd, 0xee, 0xff |
| 86 | }; |
| 87 | |
| 88 | static const uint16 FourToSixteen[16] = |
| 89 | { |
| 90 | 0x0000, 0x1111, 0x2222, 0x3333, |
| 91 | 0x4444, 0x5555, 0x6666, 0x7777, |
| 92 | 0x8888, 0x9999, 0xaaaa, 0xbbbb, |
| 93 | 0xcccc, 0xdddd, 0xeeee, 0xffff |
| 94 | }; |
| 95 | |
| 96 | static const uint8 FiveToEight[32] = |
| 97 | { |
| 98 | 0x00, // 00000 -> 00000000 |
| 99 | 0x08, // 00001 -> 00001000 |
| 100 | 0x10, // 00010 -> 00010000 |
| 101 | 0x18, // 00011 -> 00011000 |
| 102 | 0x21, // 00100 -> 00100001 |
| 103 | 0x29, // 00101 -> 00101001 |
| 104 | 0x31, // 00110 -> 00110001 |
| 105 | 0x39, // 00111 -> 00111001 |
| 106 | 0x42, // 01000 -> 01000010 |
| 107 | 0x4a, // 01001 -> 01001010 |
| 108 | 0x52, // 01010 -> 01010010 |
| 109 | 0x5a, // 01011 -> 01011010 |
| 110 | 0x63, // 01100 -> 01100011 |
| 111 | 0x6b, // 01101 -> 01101011 |
| 112 | 0x73, // 01110 -> 01110011 |
| 113 | 0x7b, // 01111 -> 01111011 |
| 114 | |
| 115 | 0x84, // 10000 -> 10000100 |
| 116 | 0x8c, // 10001 -> 10001100 |
| 117 | 0x94, // 10010 -> 10010100 |
| 118 | 0x9c, // 10011 -> 10011100 |
| 119 | 0xa5, // 10100 -> 10100101 |
| 120 | 0xad, // 10101 -> 10101101 |
| 121 | 0xb5, // 10110 -> 10110101 |
| 122 | 0xbd, // 10111 -> 10111101 |
| 123 | 0xc6, // 11000 -> 11000110 |
| 124 | 0xce, // 11001 -> 11001110 |
| 125 | 0xd6, // 11010 -> 11010110 |
| 126 | 0xde, // 11011 -> 11011110 |
| 127 | 0xe7, // 11100 -> 11100111 |
| 128 | 0xef, // 11101 -> 11101111 |
| 129 | 0xf7, // 11110 -> 11110111 |
| 130 | 0xff // 11111 -> 11111111 |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | #define RGBA5551_RedMask (0xF800) |
| 135 | #define RGBA5551_GreenMask (0x07C0) |
| 136 | #define RGBA5551_BlueMask (0x003E) |
| 137 | #define RGBA5551_AlphaMask (0x0001) |
| 138 | |
| 139 | |
| 140 | #define RGBA5551_RedShift 11 |
| 141 | #define RGBA5551_GreenShift 6 |
| 142 | #define RGBA5551_BlueShift 1 |
| 143 | #define RGBA5551_AlphaShift 0 |
| 144 | |
| 145 | #define RGBA565_RedMask (0xF800) |
| 146 | #define RGBA565_GreenMask (0x07E0) |
| 147 | #define RGBA565_BlueMask (0x001F) |
| 148 | |
| 149 | #define RGBA565_RedShift 11 |
| 150 | #define RGBA565_GreenShift 5 |
| 151 | #define RGBA565_BlueShift 0 |
| 152 | |
| 153 | inline uint16 ConvertRGBTo555(uint8 red, uint8 grn, uint8 blu) |
| 154 | { |
| 155 | return (uint16)(((uint16)(red >> 3) << RGBA5551_RedShift) | |
| 156 | ((uint16)(grn >> 3) << RGBA5551_GreenShift) | |
| 157 | ((uint16)(blu >> 3) << RGBA5551_BlueShift) | |
| 158 | ((uint16)(1) << RGBA5551_AlphaShift)); |
| 159 | } |
| 160 | |
| 161 | inline uint16 ConvertRGBTo565(uint8 red, uint8 grn, uint8 blu) |
| 162 | { |
| 163 | return (uint16)(((uint16)(red >> 3) << RGBA565_RedShift) | |
| 164 | ((uint16)(grn >> 2) << RGBA565_GreenShift) | |
| 165 | ((uint16)(blu >> 3) << RGBA565_BlueShift)); |
| 166 | } |
| 167 | inline uint16 Convert555To565(uint16 w555) |
| 168 | { |
| 169 | // Probably a faster method by fudging the low bits.. |
| 170 | |
| 171 | uint8 red = FiveToEight[(w555&RGBA5551_RedMask) >> RGBA5551_RedShift]; |
| 172 | uint8 grn = FiveToEight[(w555&RGBA5551_GreenMask)>> RGBA5551_GreenShift]; |
| 173 | uint8 blu = FiveToEight[(w555&RGBA5551_BlueMask) >> RGBA5551_BlueShift]; |
| 174 | |
| 175 | return ConvertRGBTo565(red, grn, blu); |
| 176 | } |
| 177 | |
| 178 | #define R4G4B4A4_MAKE(r,g,b,a) ((uint16)(((a) << 12) | ((r)<< 8) | ((g)<<4) | (b))) |
| 179 | |
| 180 | |
| 181 | inline uint32 Convert555ToRGBA(uint16 w555) |
| 182 | { |
| 183 | uint32 dwRed = FiveToEight[(w555&RGBA5551_RedMask) >> RGBA5551_RedShift]; |
| 184 | uint32 dwGreen = FiveToEight[(w555&RGBA5551_GreenMask)>> RGBA5551_GreenShift]; |
| 185 | uint32 dwBlue = FiveToEight[(w555&RGBA5551_BlueMask) >> RGBA5551_BlueShift]; |
| 186 | uint32 dwAlpha = (w555&RGBA5551_AlphaMask) ? 0xFF : 0x00; |
| 187 | return COLOR_RGBA(dwRed, dwGreen, dwBlue, dwAlpha); |
| 188 | |
| 189 | } |
| 190 | inline uint16 Convert555ToR4G4B4A4(uint16 w555) |
| 191 | { |
| 192 | uint8 dwRed = ((w555&RGBA5551_RedMask) >> RGBA5551_RedShift)>>1; |
| 193 | uint8 dwGreen = ((w555&RGBA5551_GreenMask)>> RGBA5551_GreenShift)>>1; |
| 194 | uint8 dwBlue = ((w555&RGBA5551_BlueMask) >> RGBA5551_BlueShift)>>1; |
| 195 | uint8 dwAlpha = (w555&RGBA5551_AlphaMask) ? 0xF : 0x0; |
| 196 | |
| 197 | return R4G4B4A4_MAKE(dwRed, dwGreen, dwBlue, dwAlpha); |
| 198 | } |
| 199 | |
| 200 | inline uint32 ConvertIA4ToRGBA(uint8 IA4) |
| 201 | { |
| 202 | uint32 I = ThreeToEight[(IA4 & 0x0F) >> 1]; |
| 203 | uint32 A = OneToEight[(IA4 & 0x01)]; |
| 204 | return COLOR_RGBA(I, I, I, A); |
| 205 | } |
| 206 | |
| 207 | inline uint16 ConvertIA4ToR4G4B4A4(uint8 IA4) |
| 208 | { |
| 209 | uint32 I = ThreeToFour[(IA4 & 0x0F) >> 1]; |
| 210 | uint32 A = OneToFour[(IA4 & 0x01)]; |
| 211 | return R4G4B4A4_MAKE(I, I, I, A); |
| 212 | } |
| 213 | inline uint32 ConvertI4ToRGBA(uint8 I4) |
| 214 | { |
| 215 | uint32 I = FourToEight[I4 & 0x0F]; |
| 216 | return COLOR_RGBA(I, I, I, I); |
| 217 | } |
| 218 | |
| 219 | inline uint16 ConvertI4ToR4G4B4A4(uint8 I4) |
| 220 | { |
| 221 | return FourToSixteen[I4 & 0x0F]; |
| 222 | } |
| 223 | |
| 224 | inline uint32 ConvertIA16ToRGBA(uint16 wIA) |
| 225 | { |
| 226 | uint32 dwIntensity = (wIA >> 8) & 0xFF; |
| 227 | uint32 dwAlpha = (wIA ) & 0xFF; |
| 228 | return COLOR_RGBA(dwIntensity, dwIntensity, dwIntensity, dwAlpha); |
| 229 | } |
| 230 | |
| 231 | inline uint16 ConvertIA16ToR4G4B4A4(uint16 wIA) |
| 232 | { |
| 233 | uint16 dwIntensity = (wIA >> 12) & 0x0F; |
| 234 | uint16 dwAlpha = (wIA >> 4) & 0x0F; |
| 235 | |
| 236 | return R4G4B4A4_MAKE(dwIntensity, dwIntensity, dwIntensity, dwAlpha); |
| 237 | } |
| 238 | |
| 239 | extern int g_convk0,g_convk1,g_convk2,g_convk3,g_convk4,g_convk5; |
| 240 | extern float g_convc0,g_convc1,g_convc2,g_convc3,g_convc4,g_convc5; |
| 241 | |
| 242 | uint32 ConvertYUV16ToR8G8B8(int Y, int U, int V); |
| 243 | uint16 ConvertYUV16ToR4G4B4(int Y, int U, int V); |
| 244 | |
| 245 | |
| 246 | typedef void ( * ConvertFunction )( CTexture * p_texture, const TxtrInfo & ti ); |
| 247 | |
| 248 | void ConvertRGBA16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 249 | void ConvertRGBA32(CTexture *pTexture, const TxtrInfo &tinfo); |
| 250 | |
| 251 | |
| 252 | void ConvertIA4(CTexture *pTexture, const TxtrInfo &tinfo); |
| 253 | void ConvertIA8(CTexture *pTexture, const TxtrInfo &tinfo); |
| 254 | void ConvertIA16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 255 | |
| 256 | void ConvertI4(CTexture *pTexture, const TxtrInfo &tinfo); |
| 257 | void ConvertI8(CTexture *pTexture, const TxtrInfo &tinfo); |
| 258 | |
| 259 | void ConvertCI4( CTexture *pTexture, const TxtrInfo & ti ); |
| 260 | void ConvertCI8( CTexture *pTexture, const TxtrInfo & ti ); |
| 261 | |
| 262 | void ConvertCI4_RGBA16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 263 | void ConvertCI4_IA16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 264 | void ConvertCI8_RGBA16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 265 | void ConvertCI8_IA16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 266 | |
| 267 | void ConvertYUV(CTexture *pTexture, const TxtrInfo &tinfo); |
| 268 | |
| 269 | // 16 a4r4g4b4 |
| 270 | void ConvertRGBA16_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 271 | void ConvertRGBA32_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 272 | |
| 273 | |
| 274 | void ConvertIA4_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 275 | void ConvertIA8_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 276 | void ConvertIA16_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 277 | |
| 278 | void ConvertI4_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 279 | void ConvertI8_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 280 | |
| 281 | void ConvertCI4_16( CTexture *pTexture, const TxtrInfo & ti ); |
| 282 | void ConvertCI8_16( CTexture *pTexture, const TxtrInfo & ti ); |
| 283 | |
| 284 | void ConvertCI4_RGBA16_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 285 | void ConvertCI4_IA16_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 286 | void ConvertCI8_RGBA16_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 287 | void ConvertCI8_IA16_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 288 | |
| 289 | void ConvertYUV_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 290 | |
| 291 | void Convert4b(CTexture *pTexture, const TxtrInfo &tinfo); |
| 292 | void Convert8b(CTexture *pTexture, const TxtrInfo &tinfo); |
| 293 | void Convert16b(CTexture *pTexture, const TxtrInfo &tinfo); |
| 294 | |
| 295 | void Convert4b_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 296 | void Convert8b_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 297 | void Convert16b_16(CTexture *pTexture, const TxtrInfo &tinfo); |
| 298 | |
| 299 | #endif |
| 300 | |