- Main Page
- Data Structures
- Files

- File List

# E:/DevIL/src-IL/src/il_convert.c

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Copyright (C) 2000-2008 by Denton Woods
    00005 // Last modified: 12/06/2006
    00006 //
    00007 // Filename: src-IL/src/il_convert.c
    00008 //
    00009 // Description: Converts between several image formats
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014 #include "il_internal.h"
    00015 #include "il_manip.h"
    00016 #include <limits.h>
    00017
    00018
    00019 ILimage *iConvertPalette(ILimage *Image, ILenum DestFormat)
    00020 {
    00021         static const ILfloat LumFactor[3] = { 0.212671f, 0.715160f, 0.072169f };  // http://www.inforamp.net/~poynton/ and libpng's libpng.txt - Used for conversion to luminance.
    00022         ILimage         *NewImage = NULL, *CurImage = NULL;
    00023         ILuint          i, j, k, c, Size, LumBpp = 1;
    00024         ILfloat         Resultf;
    00025         ILubyte         *Temp = NULL;
    00026         ILboolean       Converted;
    00027         ILboolean       HasAlpha;
    00028
    00029         NewImage = (ILimage*)icalloc(1, sizeof(ILimage));  // Much better to have it all set to 0.
    00030         if (NewImage == NULL) {
    00031                 return IL_FALSE;
    00032         }
    00033
    00034         ilCopyImageAttr(NewImage, Image);
    00035
    00036         if (!Image->Pal.Palette || !Image->Pal.PalSize || Image->Pal.PalType == IL_PAL_NONE || Image->Bpp != 1) {
    00037                 ilCloseImage(NewImage);
    00038                 ilSetError(IL_ILLEGAL_OPERATION);
    00039                 return NULL;
    00040         }
    00041
    00042         if (DestFormat == IL_LUMINANCE || DestFormat == IL_LUMINANCE_ALPHA) {
    00043                 if (NewImage->Pal.Palette)
    00044                         ifree(NewImage->Pal.Palette);
    00045                 if (DestFormat == IL_LUMINANCE_ALPHA)
    00046                         LumBpp = 2;
    00047
    00048                 switch (iCurImage->Pal.PalType)
    00049                 {
    00050                         case IL_PAL_RGB24:
    00051                         case IL_PAL_RGB32:
    00052                         case IL_PAL_RGBA32:
    00053                                 Temp = (ILubyte*)ialloc(LumBpp * Image->Pal.PalSize / ilGetBppPal(Image->Pal.PalType));
    00054                                 if (Temp == NULL)
    00055                                         goto alloc_error;
    00056
    00057                                 Size = ilGetBppPal(Image->Pal.PalType);
    00058                                 for (i = 0, k = 0; i < Image->Pal.PalSize; i += Size, k += LumBpp) {
    00059                                         Resultf = 0.0f;
    00060                                         for (c = 0; c < Size; c++) {
    00061                                                 Resultf += Image->Pal.Palette[i + c] * LumFactor[c];
    00062                                         }
    00063                                         Temp[k] = (ILubyte)Resultf;
    00064                                         if (LumBpp == 2) {
    00065                                                 if (iCurImage->Pal.PalType == IL_PAL_RGBA32)
    00066                                                         Temp[k+1] = Image->Pal.Palette[i + 3];
    00067                                                 else
    00068                                                         Temp[k+1] = 0xff;
    00069                                         }
    00070                                 }
    00071
    00072                                 break;
    00073
    00074                         case IL_PAL_BGR24:
    00075                         case IL_PAL_BGR32:
    00076                         case IL_PAL_BGRA32:
    00077                                 Temp = (ILubyte*)ialloc(LumBpp * Image->Pal.PalSize / ilGetBppPal(Image->Pal.PalType));
    00078                                 if (Temp == NULL)
    00079                                         goto alloc_error;
    00080
    00081                                 Size = ilGetBppPal(Image->Pal.PalType);
    00082                                 for (i = 0, k = 0; i < Image->Pal.PalSize; i += Size, k += LumBpp) {
    00083                                         Resultf = 0.0f;  j = 2;
    00084                                         for (c = 0; c < Size; c++, j--) {
    00085                                                 Resultf += Image->Pal.Palette[i + c] * LumFactor[j];
    00086                                         }
    00087                                         Temp[k] = (ILubyte)Resultf;
    00088                                         if (LumBpp == 2) {
    00089                                                 if (iCurImage->Pal.PalType == IL_PAL_RGBA32)
    00090                                                         Temp[k+1] = Image->Pal.Palette[i + 3];
    00091                                                 else
    00092                                                         Temp[k+1] = 0xff;
    00093                                         }
    00094                                 }
    00095
    00096                                 break;
    00097                 }
    00098
    00099                 NewImage->Pal.Palette = NULL;
    00100                 NewImage->Pal.PalSize = 0;
    00101                 NewImage->Pal.PalType = IL_PAL_NONE;
    00102                 NewImage->Format = DestFormat;
    00103                 NewImage->Bpp = LumBpp;
    00104                 NewImage->Bps = NewImage->Width * LumBpp;
    00105                 NewImage->SizeOfData = NewImage->SizeOfPlane = NewImage->Bps * NewImage->Height;
    00106                 NewImage->Data = (ILubyte*)ialloc(NewImage->SizeOfData);
    00107                 if (NewImage->Data == NULL)
    00108                         goto alloc_error;
    00109
    00110                 if (LumBpp == 2) {
    00111                         for (i = 0; i < Image->SizeOfData; i++) {
    00112                                 NewImage->Data[i*2] = Temp[Image->Data[i] * 2];
    00113                                 NewImage->Data[i*2+1] = Temp[Image->Data[i] * 2 + 1];
    00114                         }
    00115                 }
    00116                 else {
    00117                         for (i = 0; i < Image->SizeOfData; i++) {
    00118                                 NewImage->Data[i] = Temp[Image->Data[i]];
    00119                         }
    00120                 }
    00121
    00122                 ifree(Temp);
    00123
    00124                 return NewImage;
    00125         }
    00126         else if (DestFormat == IL_ALPHA) {
    00127                 if (NewImage->Pal.Palette)
    00128                         ifree(NewImage->Pal.Palette);
    00129
    00130                 switch (iCurImage->Pal.PalType)
    00131                 {
    00132                         // Opaque, so all the values are 0xFF.
    00133                         case IL_PAL_RGB24:
    00134                         case IL_PAL_RGB32:
    00135                         case IL_PAL_BGR24:
    00136                         case IL_PAL_BGR32:
    00137                                 HasAlpha = IL_FALSE;
    00138                                 break;
    00139
    00140                         case IL_PAL_BGRA32:
    00141                         case IL_PAL_RGBA32:
    00142                                 HasAlpha = IL_TRUE;
    00143                                 Temp = (ILubyte*)ialloc(1 * Image->Pal.PalSize / ilGetBppPal(Image->Pal.PalType));
    00144                                 if (Temp == NULL)
    00145                                         goto alloc_error;
    00146
    00147                                 Size = ilGetBppPal(Image->Pal.PalType);
    00148                                 for (i = 0, k = 0; i < Image->Pal.PalSize; i += Size, k += 1) {
    00149                                         Temp[k] = Image->Pal.Palette[i + 3];
    00150                                 }
    00151
    00152                                 break;
    00153                 }
    00154
    00155                 NewImage->Pal.Palette = NULL;
    00156                 NewImage->Pal.PalSize = 0;
    00157                 NewImage->Pal.PalType = IL_PAL_NONE;
    00158                 NewImage->Format = DestFormat;
    00159                 NewImage->Bpp = LumBpp;
    00160                 NewImage->Bps = NewImage->Width * 1;  // Alpha is only one byte.
    00161                 NewImage->SizeOfData = NewImage->SizeOfPlane = NewImage->Bps * NewImage->Height;
    00162                 NewImage->Data = (ILubyte*)ialloc(NewImage->SizeOfData);
    00163                 if (NewImage->Data == NULL)
    00164                         goto alloc_error;
    00165
    00166                 if (HasAlpha) {
    00167                         for (i = 0; i < Image->SizeOfData; i++) {
    00168                                 NewImage->Data[i*2] = Temp[Image->Data[i] * 2];
    00169                                 NewImage->Data[i*2+1] = Temp[Image->Data[i] * 2 + 1];
    00170                         }
    00171                 }
    00172                 else {  // No alpha, opaque.
    00173                         for (i = 0; i < Image->SizeOfData; i++) {
    00174                                 NewImage->Data[i] = 0xFF;
    00175                         }
    00176                 }
    00177
    00178                 ifree(Temp);
    00179
    00180                 return NewImage;
    00181         }
    00182
    00183         //NewImage->Format = ilGetPalBaseType(iCurImage->Pal.PalType);
    00184         NewImage->Format = DestFormat;
    00185
    00186         if (ilGetBppFormat(NewImage->Format) == 0) {
    00187                 ilCloseImage(NewImage);
    00188                 ilSetError(IL_ILLEGAL_OPERATION);
    00189                 return NULL;
    00190         }
    00191
    00192         CurImage = iCurImage;
    00193         ilSetCurImage(NewImage);
    00194
    00195         switch (DestFormat)
    00196         {
    00197                 case IL_RGB:
    00198                         Converted = ilConvertPal(IL_PAL_RGB24);
    00199                         break;
    00200
    00201                 case IL_BGR:
    00202                         Converted = ilConvertPal(IL_PAL_BGR24);
    00203                         break;
    00204
    00205                 case IL_RGBA:
    00206                         Converted = ilConvertPal(IL_PAL_RGB32);
    00207                         break;
    00208
    00209                 case IL_BGRA:
    00210                         Converted = ilConvertPal(IL_PAL_BGR32);
    00211                         break;
    00212
    00213                 case IL_COLOUR_INDEX:
    00214                         // Just copy the original image over.
    00215                         NewImage->Data = (ILubyte*)ialloc(CurImage->SizeOfData);
    00216                         if (NewImage->Data == NULL)
    00217                                 goto alloc_error;
    00218                         NewImage->Pal.Palette = (ILubyte*)ialloc(iCurImage->Pal.PalSize);
    00219                         if (NewImage->Pal.Palette == NULL)
    00220                                 goto alloc_error;
    00221                         memcpy(NewImage->Data, CurImage->Data, CurImage->SizeOfData);
    00222                         memcpy(NewImage->Pal.Palette, Image->Pal.Palette, Image->Pal.PalSize);
    00223                         NewImage->Pal.PalSize = Image->Pal.PalSize;
    00224                         NewImage->Pal.PalType = Image->Pal.PalType;
    00225                         ilSetCurImage(CurImage);
    00226                         return NewImage;
    00227
    00228                 default:
    00229                         ilCloseImage(NewImage);
    00230                         ilSetError(IL_INVALID_CONVERSION);
    00231                         return NULL;
    00232         }
    00233
    00234         // Resize to new bpp
    00235         ilResizeImage(NewImage, NewImage->Width, NewImage->Height, NewImage->Depth, ilGetBppFormat(DestFormat), /*ilGetBpcType(DestType)*/1);
    00236
    00237         // ilConvertPal already sets the error message - no need to confuse the user.
    00238         if (!Converted) {
    00239                 ilSetCurImage(CurImage);
    00240                 ilCloseImage(NewImage);
    00241                 return NULL;
    00242         }
    00243
    00244         Size = ilGetBppPal(NewImage->Pal.PalType);
    00245         for (i = 0; i < Image->SizeOfData; i++) {
    00246                 for (c = 0; c < NewImage->Bpp; c++) {
    00247                         NewImage->Data[i * NewImage->Bpp + c] = NewImage->Pal.Palette[Image->Data[i] * Size + c];
    00248                 }
    00249         }
    00250
    00251         ifree(NewImage->Pal.Palette);
    00252
    00253         NewImage->Pal.Palette = NULL;
    00254         NewImage->Pal.PalSize = 0;
    00255         NewImage->Pal.PalType = IL_PAL_NONE;
    00256         ilSetCurImage(CurImage);
    00257
    00258         return NewImage;
    00259
    00260 alloc_error:
    00261         ifree(Temp);
    00262         if (NewImage)
    00263                 ilCloseImage(NewImage);
    00264         if (CurImage != iCurImage)
    00265                 ilSetCurImage(CurImage);
    00266         return NULL;
    00267 }
    00268
    00269
    00270 // In il_quantizer.c
    00271 ILimage *iQuantizeImage(ILimage *Image, ILuint NumCols);
    00272 // In il_neuquant.c
    00273 ILimage *iNeuQuant(ILimage *Image, ILuint NumCols);
    00274
    00275 // Converts an image from one format to another
    00276 ILAPI ILimage* ILAPIENTRY iConvertImage(ILimage *Image, ILenum DestFormat, ILenum DestType)
    00277 {
    00278         ILimage *NewImage, *CurImage;
    00279         ILuint  i;
    00280         ILubyte *NewData;
    00281
    00282         CurImage = Image;
    00283         if (Image == NULL) {
    00284                 ilSetError(IL_ILLEGAL_OPERATION);
    00285                 return IL_FALSE;
    00286         }
    00287
    00288         // We don't support 16-bit color indices (or higher).
    00289         if (DestFormat == IL_COLOUR_INDEX && DestType >= IL_SHORT) {
    00290                 ilSetError(IL_INVALID_CONVERSION);
    00291                 return NULL;
    00292         }
    00293
    00294         if (Image->Format == IL_COLOUR_INDEX) {
    00295                 NewImage = iConvertPalette(Image, DestFormat);
    00296
    00297                 //added test 2003-09-01
    00298                 if (NewImage == NULL)
    00299                         return NULL;
    00300
    00301                 if (DestType == NewImage->Type)
    00302                         return NewImage;
    00303
    00304                 NewData = (ILubyte*)ilConvertBuffer(NewImage->SizeOfData, NewImage->Format, DestFormat, NewImage->Type, DestType, NewImage->Data);
    00305                 if (NewData == NULL) {
    00306                         ifree(NewImage);  // ilCloseImage not needed.
    00307                         return NULL;
    00308                 }
    00309                 ifree(NewImage->Data);
    00310                 NewImage->Data = NewData;
    00311
    00312                 ilCopyImageAttr(NewImage, Image);
    00313                 NewImage->Format = DestFormat;
    00314                 NewImage->Type = DestType;
    00315                 NewImage->Bpc = ilGetBpcType(DestType);
    00316                 NewImage->Bpp = ilGetBppFormat(DestFormat);
    00317                 NewImage->Bps = NewImage->Bpp * NewImage->Bpc * NewImage->Width;
    00318                 NewImage->SizeOfPlane = NewImage->Bps * NewImage->Height;
    00319                 NewImage->SizeOfData = NewImage->SizeOfPlane * NewImage->Depth;
    00320         }
    00321         else if (DestFormat == IL_COLOUR_INDEX && Image->Format != IL_LUMINANCE) {
    00322                 if (iGetInt(IL_QUANTIZATION_MODE) == IL_NEU_QUANT)
    00323                         return iNeuQuant(Image, iGetInt(IL_MAX_QUANT_INDICES));
    00324                 else // Assume IL_WU_QUANT otherwise.
    00325                         return iQuantizeImage(Image, iGetInt(IL_MAX_QUANT_INDICES));
    00326         }
    00327         else {
    00328                 NewImage = (ILimage*)icalloc(1, sizeof(ILimage));  // Much better to have it all set to 0.
    00329                 if (NewImage == NULL) {
    00330                         return NULL;
    00331                 }
    00332
    00333                 if (ilGetBppFormat(DestFormat) == 0) {
    00334                         ilSetError(IL_INVALID_PARAM);
    00335                         ifree(NewImage);
    00336                         return NULL;
    00337                 }
    00338
    00339                 ilCopyImageAttr(NewImage, Image);
    00340                 NewImage->Format = DestFormat;
    00341                 NewImage->Type = DestType;
    00342                 NewImage->Bpc = ilGetBpcType(DestType);
    00343                 NewImage->Bpp = ilGetBppFormat(DestFormat);
    00344                 NewImage->Bps = NewImage->Bpp * NewImage->Bpc * NewImage->Width;
    00345                 NewImage->SizeOfPlane = NewImage->Bps * NewImage->Height;
    00346                 NewImage->SizeOfData = NewImage->SizeOfPlane * NewImage->Depth;
    00347
    00348                 if (DestFormat == IL_COLOUR_INDEX && Image->Format == IL_LUMINANCE) {
    00349                         NewImage->Pal.PalSize = 768;
    00350                         NewImage->Pal.PalType = IL_PAL_RGB24;
    00351                         NewImage->Pal.Palette = (ILubyte*)ialloc(768);
    00352                         for (i = 0; i < 256; i++) {
    00353                                 NewImage->Pal.Palette[i * 3] = i;
    00354                                 NewImage->Pal.Palette[i * 3 + 1] = i;
    00355                                 NewImage->Pal.Palette[i * 3 + 2] = i;
    00356                         }
    00357                         NewImage->Data = (ILubyte*)ialloc(Image->SizeOfData);
    00358                         if (NewImage->Data == NULL) {
    00359                                 ilCloseImage(NewImage);
    00360                                 return NULL;
    00361                         }
    00362                         memcpy(NewImage->Data, Image->Data, Image->SizeOfData);
    00363                 }
    00364                 else {
    00365                         NewImage->Data = (ILubyte*)ilConvertBuffer(Image->SizeOfData, Image->Format, DestFormat, Image->Type, DestType, Image->Data);
    00366                         if (NewImage->Data == NULL) {
    00367                                 ifree(NewImage);  // ilCloseImage not needed.
    00368                                 return NULL;
    00369                         }
    00370                 }
    00371         }
    00372
    00373         return NewImage;
    00374 }
    00375
    00376
    00378 ILboolean ILAPIENTRY ilConvertImage(ILenum DestFormat, ILenum DestType)
    00379 {
    00380         ILimage *Image, *pCurImage;
    00381
    00382         if (iCurImage == NULL) {
    00383                 ilSetError(IL_ILLEGAL_OPERATION);
    00384                 return IL_FALSE;
    00385         }
    00386
    00387         if (DestFormat == iCurImage->Format && DestType == iCurImage->Type)
    00388                 return IL_TRUE;  // No conversion needed.
    00389
    00390         if (DestType == iCurImage->Type) {
    00391                 if (iFastConvert(DestFormat)) {
    00392                         iCurImage->Format = DestFormat;
    00393                         return IL_TRUE;
    00394                 }
    00395         }
    00396
    00397         if (ilIsEnabled(IL_USE_KEY_COLOUR)) {
    00398                 ilAddAlphaKey(iCurImage);
    00399         }
    00400
    00401         pCurImage = iCurImage;
    00402         while (pCurImage != NULL)
    00403         {
    00404                 Image = iConvertImage(pCurImage, DestFormat, DestType);
    00405                 if (Image == NULL)
    00406                         return IL_FALSE;
    00407
    00408                 //ilCopyImageAttr(pCurImage, Image);  // Destroys subimages.
    00409
    00410                 // We don't copy the colour profile here, since it stays the same.
    00411                 //      Same with the DXTC data.
    00412                 pCurImage->Format = DestFormat;
    00413                 pCurImage->Type = DestType;
    00414                 pCurImage->Bpc = ilGetBpcType(DestType);
    00415                 pCurImage->Bpp = ilGetBppFormat(DestFormat);
    00416                 pCurImage->Bps = pCurImage->Width * pCurImage->Bpc * pCurImage->Bpp;
    00417                 pCurImage->SizeOfPlane = pCurImage->Bps * pCurImage->Height;
    00418                 pCurImage->SizeOfData = pCurImage->Depth * pCurImage->SizeOfPlane;
    00419                 if (pCurImage->Pal.Palette && pCurImage->Pal.PalSize && pCurImage->Pal.PalType != IL_PAL_NONE)
    00420                         ifree(pCurImage->Pal.Palette);
    00421                 pCurImage->Pal.Palette = Image->Pal.Palette;
    00422                 pCurImage->Pal.PalSize = Image->Pal.PalSize;
    00423                 pCurImage->Pal.PalType = Image->Pal.PalType;
    00424                 Image->Pal.Palette = NULL;
    00425                 ifree(pCurImage->Data);
    00426                 pCurImage->Data = Image->Data;
    00427                 Image->Data = NULL;
    00428                 ilCloseImage(Image);
    00429
    00430                 pCurImage = pCurImage->Next;
    00431         }
    00432
    00433         return IL_TRUE;
    00434 }
    00435
    00436
    00437 // Swaps the colour order of the current image (rgb(a)->bgr(a) or vice-versa).
    00438 //      Must be either an 8, 24 or 32-bit (coloured) image (or palette).
    00439 ILboolean ilSwapColours()
    00440 {
    00441         ILuint          i = 0, Size = iCurImage->Bpp * iCurImage->Width * iCurImage->Height;
    00442         ILbyte          PalBpp = ilGetBppPal(iCurImage->Pal.PalType);
    00443         ILushort        *ShortPtr;
    00444         ILuint          *IntPtr, Temp;
    00445         ILdouble        *DoublePtr, DoubleTemp;
    00446
    00447         if ((iCurImage->Bpp != 1 && iCurImage->Bpp != 3 && iCurImage->Bpp != 4)) {
    00448                 ilSetError(IL_INVALID_VALUE);
    00449                 return IL_FALSE;
    00450         }
    00451
    00452         // Just check before we change the format.
    00453         if (iCurImage->Format == IL_COLOUR_INDEX) {
    00454                 if (PalBpp == 0 || iCurImage->Format != IL_COLOUR_INDEX) {
    00455                         ilSetError(IL_ILLEGAL_OPERATION);
    00456                         return IL_FALSE;
    00457                 }
    00458         }
    00459
    00460         switch (iCurImage->Format)
    00461         {
    00462                 case IL_RGB:
    00463                         iCurImage->Format = IL_BGR;
    00464                         break;
    00465                 case IL_RGBA:
    00466                         iCurImage->Format = IL_BGRA;
    00467                         break;
    00468                 case IL_BGR:
    00469                         iCurImage->Format = IL_RGB;
    00470                         break;
    00471                 case IL_BGRA:
    00472                         iCurImage->Format = IL_RGBA;
    00473                         break;
    00474                 case IL_ALPHA:
    00475                 case IL_LUMINANCE:
    00476                 case IL_LUMINANCE_ALPHA:
    00477                         return IL_TRUE;  // No need to do anything to luminance or alpha images.
    00478                 case IL_COLOUR_INDEX:
    00479                         switch (iCurImage->Pal.PalType)
    00480                         {
    00481                                 case IL_PAL_RGB24:
    00482                                         iCurImage->Pal.PalType = IL_PAL_BGR24;
    00483                                         break;
    00484                                 case IL_PAL_RGB32:
    00485                                         iCurImage->Pal.PalType = IL_PAL_BGR32;
    00486                                         break;
    00487                                 case IL_PAL_RGBA32:
    00488                                         iCurImage->Pal.PalType = IL_PAL_BGRA32;
    00489                                         break;
    00490                                 case IL_PAL_BGR24:
    00491                                         iCurImage->Pal.PalType = IL_PAL_RGB24;
    00492                                         break;
    00493                                 case IL_PAL_BGR32:
    00494                                         iCurImage->Pal.PalType = IL_PAL_RGB32;
    00495                                         break;
    00496                                 case IL_PAL_BGRA32:
    00497                                         iCurImage->Pal.PalType = IL_PAL_RGBA32;
    00498                                         break;
    00499                                 default:
    00500                                         ilSetError(IL_ILLEGAL_OPERATION);
    00501                                         return IL_FALSE;
    00502                         }
    00503                         break;
    00504                 default:
    00505                         ilSetError(IL_ILLEGAL_OPERATION);
    00506                         return IL_FALSE;
    00507         }
    00508
    00509         if (iCurImage->Format == IL_COLOUR_INDEX) {
    00510                 for (; i < iCurImage->Pal.PalSize; i += PalBpp) {
    00511                                 Temp = iCurImage->Pal.Palette[i];
    00512                                 iCurImage->Pal.Palette[i] = iCurImage->Pal.Palette[i+2];
    00513                                 iCurImage->Pal.Palette[i+2] = Temp;
    00514                 }
    00515         }
    00516         else {
    00517                 ShortPtr = (ILushort*)iCurImage->Data;
    00518                 IntPtr = (ILuint*)iCurImage->Data;
    00519                 DoublePtr = (ILdouble*)iCurImage->Data;
    00520                 switch (iCurImage->Bpc)
    00521                 {
    00522                         case 1:
    00523                                 for (; i < Size; i += iCurImage->Bpp) {
    00524                                         Temp = iCurImage->Data[i];
    00525                                         iCurImage->Data[i] = iCurImage->Data[i+2];
    00526                                         iCurImage->Data[i+2] = Temp;
    00527                                 }
    00528                                 break;
    00529                         case 2:
    00530                                 for (; i < Size; i += iCurImage->Bpp) {
    00531                                         Temp = ShortPtr[i];
    00532                                         ShortPtr[i] = ShortPtr[i+2];
    00533                                         ShortPtr[i+2] = Temp;
    00534                                 }
    00535                                 break;
    00536                         case 4:  // Works fine with ILint, ILuint and ILfloat.
    00537                                 for (; i < Size; i += iCurImage->Bpp) {
    00538                                         Temp = IntPtr[i];
    00539                                         IntPtr[i] = IntPtr[i+2];
    00540                                         IntPtr[i+2] = Temp;
    00541                                 }
    00542                                 break;
    00543                         case 8:
    00544                                 for (; i < Size; i += iCurImage->Bpp) {
    00545                                         DoubleTemp = DoublePtr[i];
    00546                                         DoublePtr[i] = DoublePtr[i+2];
    00547                                         DoublePtr[i+2] = DoubleTemp;
    00548                                 }
    00549                                 break;
    00550                 }
    00551         }
    00552
    00553         return IL_TRUE;
    00554 }
    00555
    00556
    00557 // Adds an opaque alpha channel to a 24-bit image
    00558 ILboolean ilAddAlpha()
    00559 {
    00560         ILubyte         *NewData, NewBpp;
    00561         ILuint          i = 0, j = 0, Size;
    00562
    00563         if (iCurImage == NULL) {
    00564                 ilSetError(IL_ILLEGAL_OPERATION);
    00565                 return IL_FALSE;
    00566         }
    00567
    00568         if (iCurImage->Bpp != 3) {
    00569                 ilSetError(IL_INVALID_VALUE);
    00570                 return IL_FALSE;
    00571         }
    00572
    00573         Size = iCurImage->Bps * iCurImage->Height / iCurImage->Bpc;
    00574         NewBpp = (ILubyte)(iCurImage->Bpp + 1);
    00575
    00576         NewData = (ILubyte*)ialloc(NewBpp * iCurImage->Bpc * iCurImage->Width * iCurImage->Height);
    00577         if (NewData == NULL) {
    00578                 return IL_FALSE;
    00579         }
    00580
    00581         switch (iCurImage->Type)
    00582         {
    00583                 case IL_BYTE:
    00584                 case IL_UNSIGNED_BYTE:
    00585                         for (; i < Size; i += iCurImage->Bpp, j += NewBpp) {
    00586                                 NewData[j]   = iCurImage->Data[i];
    00587                                 NewData[j+1] = iCurImage->Data[i+1];
    00588                                 NewData[j+2] = iCurImage->Data[i+2];
    00589                                 NewData[j+3] = UCHAR_MAX;  // Max opaqueness
    00590                         }
    00591                         break;
    00592
    00593                 case IL_SHORT:
    00594                 case IL_UNSIGNED_SHORT:
    00595                         for (; i < Size; i += iCurImage->Bpp, j += NewBpp) {
    00596                                 ((ILushort*)NewData)[j]   = ((ILushort*)iCurImage->Data)[i];
    00597                                 ((ILushort*)NewData)[j+1] = ((ILushort*)iCurImage->Data)[i+1];
    00598                                 ((ILushort*)NewData)[j+2] = ((ILushort*)iCurImage->Data)[i+2];
    00599                                 ((ILushort*)NewData)[j+3] = USHRT_MAX;
    00600                         }
    00601                         break;
    00602
    00603                 case IL_INT:
    00604                 case IL_UNSIGNED_INT:
    00605                         for (; i < Size; i += iCurImage->Bpp, j += NewBpp) {
    00606                                 ((ILuint*)NewData)[j]   = ((ILuint*)iCurImage->Data)[i];
    00607                                 ((ILuint*)NewData)[j+1] = ((ILuint*)iCurImage->Data)[i+1];
    00608                                 ((ILuint*)NewData)[j+2] = ((ILuint*)iCurImage->Data)[i+2];
    00609                                 ((ILuint*)NewData)[j+3] = UINT_MAX;
    00610                         }
    00611                         break;
    00612
    00613                 case IL_FLOAT:
    00614                         for (; i < Size; i += iCurImage->Bpp, j += NewBpp) {
    00615                                 ((ILfloat*)NewData)[j]   = ((ILfloat*)iCurImage->Data)[i];
    00616                                 ((ILfloat*)NewData)[j+1] = ((ILfloat*)iCurImage->Data)[i+1];
    00617                                 ((ILfloat*)NewData)[j+2] = ((ILfloat*)iCurImage->Data)[i+2];
    00618                                 ((ILfloat*)NewData)[j+3] = 1.0f;
    00619                         }
    00620                         break;
    00621
    00622                 case IL_DOUBLE:
    00623                         for (; i < Size; i += iCurImage->Bpp, j += NewBpp) {
    00624                                 ((ILdouble*)NewData)[j]   = ((ILdouble*)iCurImage->Data)[i];
    00625                                 ((ILdouble*)NewData)[j+1] = ((ILdouble*)iCurImage->Data)[i+1];
    00626                                 ((ILdouble*)NewData)[j+2] = ((ILdouble*)iCurImage->Data)[i+2];
    00627                                 ((ILdouble*)NewData)[j+3] = 1.0;
    00628                         }
    00629                         break;
    00630
    00631                 default:
    00632                         ifree(NewData);
    00633                         ilSetError(IL_INTERNAL_ERROR);
    00634                         return IL_FALSE;
    00635         }
    00636
    00637
    00638         iCurImage->Bpp = NewBpp;
    00639         iCurImage->Bps = iCurImage->Width * iCurImage->Bpc * NewBpp;
    00640         iCurImage->SizeOfPlane = iCurImage->Bps * iCurImage->Height;
    00641         iCurImage->SizeOfData = iCurImage->SizeOfPlane * iCurImage->Depth;
    00642         ifree(iCurImage->Data);
    00643         iCurImage->Data = NewData;
    00644
    00645         switch (iCurImage->Format)
    00646         {
    00647                 case IL_RGB:
    00648                         iCurImage->Format = IL_RGBA;
    00649                         break;
    00650                 case IL_BGR:
    00651                         iCurImage->Format = IL_BGRA;
    00652                         break;
    00653         }
    00654
    00655         return IL_TRUE;
    00656 }
    00657
    00658
    00659 //ILfloat KeyRed = 0, KeyGreen = 0, KeyBlue = 0, KeyAlpha = 0;
    00660
    00661 void ILAPIENTRY ilKeyColour(ILclampf Red, ILclampf Green, ILclampf Blue, ILclampf Alpha)
    00662 {
    00663         ILfloat KeyRed = 0, KeyGreen = 0, KeyBlue = 0, KeyAlpha = 0;
    00664         KeyRed          = Red;
    00665         KeyGreen        = Green;
    00666         KeyBlue         = Blue;
    00667         KeyAlpha        = Alpha;
    00668         return;
    00669 }
    00670
    00671
    00672 // Adds an alpha channel to an 8 or 24-bit image,
    00673 //      making the image transparent where Key is equal to the pixel.
    00674 ILboolean ilAddAlphaKey(ILimage *Image)
    00675 {
    00676         ILfloat KeyRed = 0, KeyGreen = 0, KeyBlue = 0, KeyAlpha = 0;
    00677         ILubyte         *NewData, NewBpp;
    00678         ILfloat         KeyColour[3];
    00679         ILuint          i = 0, j = 0, c, Size;
    00680         ILboolean       Same;
    00681
    00682         if (Image == NULL) {
    00683                 ilSetError(IL_ILLEGAL_OPERATION);
    00684                 return IL_FALSE;
    00685         }
    00686
    00687         if (Image->Format != IL_COLOUR_INDEX) {
    00688                 if (Image->Bpp != 3) {
    00689                         ilSetError(IL_INVALID_VALUE);
    00690                         return IL_FALSE;
    00691                 }
    00692
    00693                 if (Image->Format == IL_BGR || Image->Format == IL_BGRA) {
    00694                         KeyColour[0] = KeyBlue;
    00695                         KeyColour[1] = KeyGreen;
    00696                         KeyColour[2] = KeyRed;
    00697                 }
    00698                 else {
    00699                         KeyColour[0] = KeyRed;
    00700                         KeyColour[1] = KeyGreen;
    00701                         KeyColour[2] = KeyBlue;
    00702                 }
    00703
    00704                 Size = Image->Bps * Image->Height / Image->Bpc;
    00705
    00706                 NewBpp = (ILubyte)(Image->Bpp + 1);
    00707
    00708                 NewData = (ILubyte*)ialloc(NewBpp * Image->Bpc * Image->Width * Image->Height);
    00709                 if (NewData == NULL) {
    00710                         return IL_FALSE;
    00711                 }
    00712
    00713                 switch (Image->Type)
    00714                 {
    00715                         case IL_BYTE:
    00716                         case IL_UNSIGNED_BYTE:
    00717                                 for (; i < Size; i += Image->Bpp, j += NewBpp) {
    00718                                         NewData[j]   = Image->Data[i];
    00719                                         NewData[j+1] = Image->Data[i+1];
    00720                                         NewData[j+2] = Image->Data[i+2];
    00721                                         Same = IL_TRUE;
    00722                                         for (c = 0; c < Image->Bpp; c++) {
    00723                                                 if (Image->Data[i+c] != KeyColour[c] * UCHAR_MAX)
    00724                                                         Same = IL_FALSE;
    00725                                         }
    00726
    00727                                         if (Same)
    00728                                                 NewData[j+3] = 0;  // Transparent - matches key colour
    00729                                         else
    00730                                                 NewData[j+3] = UCHAR_MAX;
    00731                                 }
    00732                                 break;
    00733
    00734                         case IL_SHORT:
    00735                         case IL_UNSIGNED_SHORT:
    00736                                 for (; i < Size; i += Image->Bpp, j += NewBpp) {
    00737                                         ((ILushort*)NewData)[j]   = ((ILushort*)Image->Data)[i];
    00738                                         ((ILushort*)NewData)[j+1] = ((ILushort*)Image->Data)[i+1];
    00739                                         ((ILushort*)NewData)[j+2] = ((ILushort*)Image->Data)[i+2];
    00740                                         Same = IL_TRUE;
    00741                                         for (c = 0; c < Image->Bpp; c++) {
    00742                                                 if (((ILushort*)Image->Data)[i+c] != KeyColour[c] * USHRT_MAX)
    00743                                                         Same = IL_FALSE;
    00744                                         }
    00745
    00746                                         if (Same)
    00747                                                 ((ILushort*)NewData)[j+3] = 0;
    00748                                         else
    00749                                                 ((ILushort*)NewData)[j+3] = USHRT_MAX;
    00750                                 }
    00751                                 break;
    00752
    00753                         case IL_INT:
    00754                         case IL_UNSIGNED_INT:
    00755                                 for (; i < Size; i += Image->Bpp, j += NewBpp) {
    00756                                         ((ILuint*)NewData)[j]   = ((ILuint*)Image->Data)[i];
    00757                                         ((ILuint*)NewData)[j+1] = ((ILuint*)Image->Data)[i+1];
    00758                                         ((ILuint*)NewData)[j+2] = ((ILuint*)Image->Data)[i+2];
    00759                                         Same = IL_TRUE;
    00760                                         for (c = 0; c < Image->Bpp; c++) {
    00761                                                 if (((ILuint*)Image->Data)[i+c] != KeyColour[c] * UINT_MAX)
    00762                                                         Same = IL_FALSE;
    00763                                         }
    00764
    00765                                         if (Same)
    00766                                                 ((ILuint*)NewData)[j+3] = 0;
    00767                                         else
    00768                                                 ((ILuint*)NewData)[j+3] = UINT_MAX;
    00769                                 }
    00770                                 break;
    00771
    00772                         case IL_FLOAT:
    00773                                 for (; i < Size; i += Image->Bpp, j += NewBpp) {
    00774                                         ((ILfloat*)NewData)[j]   = ((ILfloat*)Image->Data)[i];
    00775                                         ((ILfloat*)NewData)[j+1] = ((ILfloat*)Image->Data)[i+1];
    00776                                         ((ILfloat*)NewData)[j+2] = ((ILfloat*)Image->Data)[i+2];
    00777                                         Same = IL_TRUE;
    00778                                         for (c = 0; c < Image->Bpp; c++) {
    00779                                                 if (((ILfloat*)Image->Data)[i+c] != KeyColour[c])
    00780                                                         Same = IL_FALSE;
    00781                                         }
    00782
    00783                                         if (Same)
    00784                                                 ((ILfloat*)NewData)[j+3] = 0.0f;
    00785                                         else
    00786                                                 ((ILfloat*)NewData)[j+3] = 1.0f;
    00787                                 }
    00788                                 break;
    00789
    00790                         case IL_DOUBLE:
    00791                                 for (; i < Size; i += Image->Bpp, j += NewBpp) {
    00792                                         ((ILdouble*)NewData)[j]   = ((ILdouble*)Image->Data)[i];
    00793                                         ((ILdouble*)NewData)[j+1] = ((ILdouble*)Image->Data)[i+1];
    00794                                         ((ILdouble*)NewData)[j+2] = ((ILdouble*)Image->Data)[i+2];
    00795                                         Same = IL_TRUE;
    00796                                         for (c = 0; c < Image->Bpp; c++) {
    00797                                                 if (((ILdouble*)Image->Data)[i+c] != KeyColour[c])
    00798                                                         Same = IL_FALSE;
    00799                                         }
    00800
    00801                                         if (Same)
    00802                                                 ((ILdouble*)NewData)[j+3] = 0.0;
    00803                                         else
    00804                                                 ((ILdouble*)NewData)[j+3] = 1.0;
    00805                                 }
    00806                                 break;
    00807
    00808                         default:
    00809                                 ifree(NewData);
    00810                                 ilSetError(IL_INTERNAL_ERROR);
    00811                                 return IL_FALSE;
    00812                 }
    00813
    00814
    00815                 Image->Bpp = NewBpp;
    00816                 Image->Bps = Image->Width * Image->Bpc * NewBpp;
    00817                 Image->SizeOfPlane = Image->Bps * Image->Height;
    00818                 Image->SizeOfData = Image->SizeOfPlane * Image->Depth;
    00819                 ifree(Image->Data);
    00820                 Image->Data = NewData;
    00821
    00822                 switch (Image->Format)
    00823                 {
    00824                         case IL_RGB:
    00825                                 Image->Format = IL_RGBA;
    00826                                 break;
    00827                         case IL_BGR:
    00828                                 Image->Format = IL_BGRA;
    00829                                 break;
    00830                 }
    00831         }
    00832         else {  // IL_COLOUR_INDEX
    00833                 if (Image->Bpp != 1) {
    00834                         ilSetError(IL_INTERNAL_ERROR);
    00835                         return IL_FALSE;
    00836                 }
    00837
    00838                 Size = ilGetInteger(IL_PALETTE_NUM_COLS);
    00839                 if (Size == 0) {
    00840                         ilSetError(IL_INTERNAL_ERROR);
    00841                         return IL_FALSE;
    00842                 }
    00843
    00844                 if ((ILuint)(KeyAlpha * UCHAR_MAX) > Size) {
    00845                         ilSetError(IL_INVALID_VALUE);
    00846                         return IL_FALSE;
    00847                 }
    00848
    00849                 switch (Image->Pal.PalType)
    00850                 {
    00851                         case IL_PAL_RGB24:
    00852                         case IL_PAL_RGB32:
    00853                         case IL_PAL_RGBA32:
    00854                                 if (!ilConvertPal(IL_PAL_RGBA32))
    00855                                         return IL_FALSE;
    00856                                 break;
    00857                         case IL_PAL_BGR24:
    00858                         case IL_PAL_BGR32:
    00859                         case IL_PAL_BGRA32:
    00860                                 if (!ilConvertPal(IL_PAL_BGRA32))
    00861                                         return IL_FALSE;
    00862                                 break;
    00863                         default:
    00864                                 ilSetError(IL_INTERNAL_ERROR);
    00865                                 return IL_FALSE;
    00866                 }
    00867
    00868                 // Set the colour index to be transparent.
    00869                 Image->Pal.Palette[(ILuint)(KeyAlpha * UCHAR_MAX) * 4 + 3] = 0;
    00870
    00871                 // @TODO: Check if this is the required behaviour.
    00872
    00873                 if (Image->Pal.PalType == IL_PAL_RGBA32)
    00874                         ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
    00875                 else
    00876                         ilConvertImage(IL_BGRA, IL_UNSIGNED_BYTE);
    00877         }
    00878
    00879         return IL_TRUE;
    00880 }
    00881
    00882
    00883 // Removes alpha from a 32-bit image
    00884 //      Should we maybe add an option that changes the image based on the alpha?
    00885 ILboolean ilRemoveAlpha()
    00886 {
    00887         ILubyte *NewData, NewBpp;
    00888         ILuint i = 0, j = 0, Size;
    00889
    00890         if (iCurImage == NULL) {
    00891                 ilSetError(IL_INVALID_PARAM);
    00892                 return IL_FALSE;
    00893         }
    00894
    00895         if (iCurImage->Bpp != 4) {
    00896                 ilSetError(IL_INVALID_VALUE);
    00897                 return IL_FALSE;
    00898         }
    00899
    00900         Size = iCurImage->Bps * iCurImage->Height;
    00901         NewBpp = (ILubyte)(iCurImage->Bpp - 1);
    00902
    00903         NewData = (ILubyte*)ialloc(NewBpp * iCurImage->Bpc * iCurImage->Width * iCurImage->Height);
    00904         if (NewData == NULL) {
    00905                 return IL_FALSE;
    00906         }
    00907
    00908         switch (iCurImage->Type)
    00909         {
    00910                 case IL_BYTE:
    00911                 case IL_UNSIGNED_BYTE:
    00912                         for (; i < Size; i += iCurImage->Bpp, j += NewBpp) {
    00913                                 NewData[j]   = iCurImage->Data[i];
    00914                                 NewData[j+1] = iCurImage->Data[i+1];
    00915                                 NewData[j+2] = iCurImage->Data[i+2];
    00916                         }
    00917                         break;
    00918
    00919                 case IL_SHORT:
    00920                 case IL_UNSIGNED_SHORT:
    00921                         for (; i < Size; i += iCurImage->Bpp, j += NewBpp) {
    00922                                 ((ILushort*)NewData)[j]   = ((ILushort*)iCurImage->Data)[i];
    00923                                 ((ILushort*)NewData)[j+1] = ((ILushort*)iCurImage->Data)[i+1];
    00924                                 ((ILushort*)NewData)[j+2] = ((ILushort*)iCurImage->Data)[i+2];
    00925                         }
    00926                         break;
    00927
    00928                 case IL_INT:
    00929                 case IL_UNSIGNED_INT:
    00930                         for (; i < Size; i += iCurImage->Bpp, j += NewBpp) {
    00931                                 ((ILuint*)NewData)[j]   = ((ILuint*)iCurImage->Data)[i];
    00932                                 ((ILuint*)NewData)[j+1] = ((ILuint*)iCurImage->Data)[i+1];
    00933                                 ((ILuint*)NewData)[j+2] = ((ILuint*)iCurImage->Data)[i+2];
    00934                         }
    00935                         break;
    00936
    00937                 case IL_FLOAT:
    00938                         for (; i < Size; i += iCurImage->Bpp, j += NewBpp) {
    00939                                 ((ILfloat*)NewData)[j]   = ((ILfloat*)iCurImage->Data)[i];
    00940                                 ((ILfloat*)NewData)[j+1] = ((ILfloat*)iCurImage->Data)[i+1];
    00941                                 ((ILfloat*)NewData)[j+2] = ((ILfloat*)iCurImage->Data)[i+2];
    00942                         }
    00943                         break;
    00944
    00945                 case IL_DOUBLE:
    00946                         for (; i < Size; i += iCurImage->Bpp, j += NewBpp) {
    00947                                 ((ILdouble*)NewData)[j]   = ((ILdouble*)iCurImage->Data)[i];
    00948                                 ((ILdouble*)NewData)[j+1] = ((ILdouble*)iCurImage->Data)[i+1];
    00949                                 ((ILdouble*)NewData)[j+2] = ((ILdouble*)iCurImage->Data)[i+2];
    00950                         }
    00951                         break;
    00952
    00953                 default:
    00954                         ifree(NewData);
    00955                         ilSetError(IL_INTERNAL_ERROR);
    00956                         return IL_FALSE;
    00957         }
    00958
    00959
    00960         iCurImage->Bpp = NewBpp;
    00961         iCurImage->Bps = iCurImage->Width * iCurImage->Bpc * NewBpp;
    00962         iCurImage->SizeOfPlane = iCurImage->Bps * iCurImage->Height;
    00963         iCurImage->SizeOfData = iCurImage->SizeOfPlane * iCurImage->Depth;
    00964         ifree(iCurImage->Data);
    00965         iCurImage->Data = NewData;
    00966
    00967         switch (iCurImage->Format)
    00968         {
    00969                 case IL_RGBA:
    00970                         iCurImage->Format = IL_RGB;
    00971                         break;
    00972                 case IL_BGRA:
    00973                         iCurImage->Format = IL_BGR;
    00974                         break;
    00975         }
    00976
    00977         return IL_TRUE;
    00978 }
    00979
    00980
    00981 ILboolean ilFixCur()
    00982 {
    00983         if (ilIsEnabled(IL_ORIGIN_SET)) {
    00984                 if ((ILenum)ilGetInteger(IL_ORIGIN_MODE) != iCurImage->Origin) {
    00985                         if (!ilFlipImage()) {
    00986                                 return IL_FALSE;
    00987                         }
    00988                 }
    00989         }
    00990
    00991         if (ilIsEnabled(IL_TYPE_SET)) {
    00992                 if ((ILenum)ilGetInteger(IL_TYPE_MODE) != iCurImage->Type) {
    00993                         if (!ilConvertImage(iCurImage->Format, ilGetInteger(IL_TYPE_MODE))) {
    00994                                 return IL_FALSE;
    00995                         }
    00996                 }
    00997         }
    00998         if (ilIsEnabled(IL_FORMAT_SET)) {
    00999                 if ((ILenum)ilGetInteger(IL_FORMAT_MODE) != iCurImage->Format) {
    01000                         if (!ilConvertImage(ilGetInteger(IL_FORMAT_MODE), iCurImage->Type)) {
    01001                                 return IL_FALSE;
    01002                         }
    01003                 }
    01004         }
    01005
    01006         if (iCurImage->Format == IL_COLOUR_INDEX) {
    01007                 if (ilGetBoolean(IL_CONV_PAL) == IL_TRUE) {
    01008                         if (!ilConvertImage(IL_BGR, IL_UNSIGNED_BYTE)) {
    01009                                 return IL_FALSE;
    01010                         }
    01011                 }
    01012         }
    01013 /*      Swap Colors on Big Endian !!!!!
    01014 #ifdef __BIG_ENDIAN__
    01015         // Swap endian
    01016         EndianSwapData(iCurImage);
    01017 #endif
    01018 */
    01019         return IL_TRUE;
    01020 }
    01021
    01022 /*
    01023 ILboolean ilFixImage()
    01024 {
    01025         ILuint NumImages, i;
    01026
    01027         NumImages = ilGetInteger(IL_NUM_IMAGES);
    01028         for (i = 0; i < NumImages; i++) {
    01029                 ilBindImage(ilGetCurName());  // Set to parent image first.
    01030                 if (!ilActiveImage(i+1))
    01031                         return IL_FALSE;
    01032                 if (!ilFixCur())
    01033                         return IL_FALSE;
    01034         }
    01035
    01036         NumImages = ilGetInteger(IL_NUM_MIPMAPS);
    01037         for (i = 0; i < NumImages; i++) {
    01038                 ilBindImage(ilGetCurName());  // Set to parent image first.
    01039                 if (!ilActiveMipmap(i+1))
    01040                         return IL_FALSE;
    01041                 if (!ilFixCur())
    01042                         return IL_FALSE;
    01043         }
    01044
    01045         NumImages = ilGetInteger(IL_NUM_LAYERS);
    01046         for (i = 0; i < NumImages; i++) {
    01047                 ilBindImage(ilGetCurName());  // Set to parent image first.
    01048                 if (!ilActiveLayer(i+1))
    01049                         return IL_FALSE;
    01050                 if (!ilFixCur())
    01051                         return IL_FALSE;
    01052         }
    01053
    01054         ilBindImage(ilGetCurName());
    01055         ilFixCur();
    01056
    01057         return IL_TRUE;
    01058 }
    01059 */
    01060
    01061 /*
    01062 This function was replaced 20050304, because the previous version
    01063 didn't fix the mipmaps of the subimages etc. This version is not
    01064 completely correct either, because the subimages of the subimages
    01065 etc. are not fixed, but at the moment no images of this type can
    01066 be loaded anyway. Thanks to Chris Lux for pointing this out.
    01067 */
    01068
    01069 ILboolean ilFixImage()
    01070 {
    01071         ILuint NumFaces,  f;
    01072         ILuint NumImages, i;
    01073         ILuint NumMipmaps,j;
    01074         ILuint NumLayers, k;
    01075
    01076         NumImages = ilGetInteger(IL_NUM_IMAGES);
    01077         for (i = 0; i <= NumImages; i++) {
    01078                 ilBindImage(ilGetCurName());  // Set to parent image first.
    01079                 if (!ilActiveImage(i))
    01080                         return IL_FALSE;
    01081
    01082                 NumFaces = ilGetInteger(IL_NUM_FACES);
    01083                 for (f = 0; f <= NumFaces; f++) {
    01084                         ilBindImage(ilGetCurName());  // Set to parent image first.
    01085                         if (!ilActiveImage(i))
    01086                                 return IL_FALSE;
    01087                         if (!ilActiveFace(f))
    01088                                 return IL_FALSE;
    01089
    01090                         NumLayers = ilGetInteger(IL_NUM_LAYERS);
    01091                         for (k = 0; k <= NumLayers; k++) {
    01092                                 ilBindImage(ilGetCurName());  // Set to parent image first.
    01093                                 if (!ilActiveImage(i))
    01094                                         return IL_FALSE;
    01095                                 if (!ilActiveFace(f))
    01096                                         return IL_FALSE;
    01097                                 if (!ilActiveLayer(k))
    01098                                         return IL_FALSE;
    01099
    01100                                 NumMipmaps = ilGetInteger(IL_NUM_MIPMAPS);
    01101                                 for (j = 0; j <= NumMipmaps; j++) {
    01102                                         ilBindImage(ilGetCurName());    // Set to parent image first.
    01103                                         if (!ilActiveImage(i))
    01104                                                 return IL_FALSE;
    01105                                         if (!ilActiveFace(f))
    01106                                                 return IL_FALSE;
    01107                                         if (!ilActiveLayer(k))
    01108                                                 return IL_FALSE;
    01109                                         if (!ilActiveMipmap(j))
    01110                                                 return IL_FALSE;
    01111                                         if (!ilFixCur())
    01112                                                 return IL_FALSE;
    01113                                 }
    01114                         }
    01115                 }
    01116         }
    01117         ilBindImage(ilGetCurName());
    01118
    01119         return IL_TRUE;
    01120 }