| 286 | |
| 287 | |
| 288 | ILboolean IwiReadImage(ILimage *BaseImage, IWIHEAD *Header, ILuint NumMips) |
| 289 | { |
| 290 | ILimage *Image; |
| 291 | ILuint SizeOfData; |
| 292 | ILubyte *CompData = NULL; |
| 293 | ILint i, j, k, m; |
| 294 | |
| 295 | for (i = NumMips; i >= 0; i--) { |
| 296 | Image = BaseImage; |
| 297 | // Go to the ith mipmap level. |
| 298 | // The mipmaps go from smallest to the largest. |
| 299 | for (j = 0; j < i; j++) |
| 300 | Image = Image->Mipmaps; |
| 301 | |
| 302 | switch (Header->Format) |
| 303 | { |
| 304 | case IWI_ARGB8: // These are all |
| 305 | case IWI_RGB8: // uncompressed data, |
| 306 | case IWI_A8: // so just read it. |
| 307 | if (iread(Image->Data, 1, Image->SizeOfData) != Image->SizeOfData) |
| 308 | return IL_FALSE; |
| 309 | break; |
| 310 | |
| 311 | case IWI_ARGB4: //@TODO: Find some test images for this. |
| 312 | // Data is in ARGB4 format - 4 bits per component. |
| 313 | SizeOfData = Image->Width * Image->Height * 2; |
| 314 | CompData = (ILubyte*)ialloc(SizeOfData); // Not really compressed - just in ARGB4 format. |
| 315 | if (CompData == NULL) |
| 316 | return IL_FALSE; |
| 317 | if (iread(CompData, 1, SizeOfData) != SizeOfData) { |
| 318 | ifree(CompData); |
| 319 | return IL_FALSE; |
| 320 | } |
| 321 | for (k = 0, m = 0; k < (ILint)Image->SizeOfData; k += 4, m += 2) { |
| 322 | // @TODO: Double the image data into the low and high nibbles for a better range of values. |
| 323 | Image->Data[k+0] = CompData[m] & 0xF0; |
| 324 | Image->Data[k+1] = (CompData[m] & 0x0F) << 4; |
| 325 | Image->Data[k+2] = CompData[m+1] & 0xF0; |
| 326 | Image->Data[k+3] = (CompData[m+1] & 0x0F) << 4; |
| 327 | } |
| 328 | break; |
| 329 | |
| 330 | case IWI_DXT1: |
| 331 | // DXT1 data has at least 8 bytes, even for one pixel. |
| 332 | SizeOfData = IL_MAX(Image->Width * Image->Height / 2, 8); |
| 333 | CompData = (ILubyte*)ialloc(SizeOfData); // Gives a 6:1 compression ratio (or 8:1 for DXT1 with alpha) |
| 334 | if (CompData == NULL) |
| 335 | return IL_FALSE; |
| 336 | if (iread(CompData, 1, SizeOfData) != SizeOfData) { |
| 337 | ifree(CompData); |
| 338 | return IL_FALSE; |
| 339 | } |
| 340 | |
| 341 | // Decompress the DXT1 data into Image (ith mipmap). |
| 342 | if (!DecompressDXT1(Image, CompData)) { |
| 343 | ifree(CompData); |
| 344 | return IL_FALSE; |
| 345 | } |
no test coverage detected