| 249 | |
| 250 | |
| 251 | ILboolean iReadColMapTga(TARGAHEAD *Header) |
| 252 | { |
| 253 | char ID[255]; |
| 254 | ILuint i; |
| 255 | ILushort Pixel; |
| 256 | |
| 257 | if (iread(ID, 1, Header->IDLen) != Header->IDLen) |
| 258 | return IL_FALSE; |
| 259 | |
| 260 | if (!ilTexImage(Header->Width, Header->Height, 1, (ILubyte)(Header->Bpp >> 3), 0, IL_UNSIGNED_BYTE, NULL)) { |
| 261 | return IL_FALSE; |
| 262 | } |
| 263 | if (iCurImage->Pal.Palette && iCurImage->Pal.PalSize) |
| 264 | ifree(iCurImage->Pal.Palette); |
| 265 | |
| 266 | iCurImage->Format = IL_COLOUR_INDEX; |
| 267 | iCurImage->Pal.PalSize = Header->ColMapLen * (Header->ColMapEntSize >> 3); |
| 268 | |
| 269 | switch (Header->ColMapEntSize) |
| 270 | { |
| 271 | case 16: |
| 272 | iCurImage->Pal.PalType = IL_PAL_BGRA32; |
| 273 | iCurImage->Pal.PalSize = Header->ColMapLen * 4; |
| 274 | break; |
| 275 | case 24: |
| 276 | iCurImage->Pal.PalType = IL_PAL_BGR24; |
| 277 | break; |
| 278 | case 32: |
| 279 | iCurImage->Pal.PalType = IL_PAL_BGRA32; |
| 280 | break; |
| 281 | default: |
| 282 | // Should *never* reach here |
| 283 | ilSetError(IL_ILLEGAL_FILE_VALUE); |
| 284 | return IL_FALSE; |
| 285 | } |
| 286 | |
| 287 | iCurImage->Pal.Palette = (ILubyte*)ialloc(iCurImage->Pal.PalSize); |
| 288 | if (iCurImage->Pal.Palette == NULL) { |
| 289 | return IL_FALSE; |
| 290 | } |
| 291 | |
| 292 | // Do we need to do something with FirstEntry? Like maybe: |
| 293 | // iread(Image->Pal + Targa->FirstEntry, 1, Image->Pal.PalSize); ?? |
| 294 | if (Header->ColMapEntSize != 16) |
| 295 | { |
| 296 | if (iread(iCurImage->Pal.Palette, 1, iCurImage->Pal.PalSize) != iCurImage->Pal.PalSize) |
| 297 | return IL_FALSE; |
| 298 | } |
| 299 | else { |
| 300 | // 16 bit palette, so we have to break it up. |
| 301 | for (i = 0; i < iCurImage->Pal.PalSize; i += 4) |
| 302 | { |
| 303 | Pixel = GetBigUShort(); |
| 304 | if (ieof()) |
| 305 | return IL_FALSE; |
| 306 | iCurImage->Pal.Palette[3] = (Pixel & 0x8000) >> 12; |
| 307 | iCurImage->Pal.Palette[0] = (Pixel & 0xFC00) >> 7; |
| 308 | iCurImage->Pal.Palette[1] = (Pixel & 0x03E0) >> 2; |
no test coverage detected