MCPcopy Create free account
hub / github.com/DentonW/DevIL / ILAPIENTRY ilApplyPal

Function ILAPIENTRY ilApplyPal

DevIL/src-IL/src/il_pal.cpp:914–1111  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

912
913
914ILboolean ILAPIENTRY ilApplyPal(ILconst_string FileName)
915{
916 ILimage Image, *CurImage = iCurImage;
917 ILubyte *NewData;
918 ILuint *PalInfo, NumColours, NumPix, MaxDist, DistEntry=0, i, j;
919 ILint Dist1, Dist2, Dist3;
920 ILboolean Same;
921 ILenum Origin;
922// COL_CUBE *Cubes;
923
924 if( iCurImage == NULL || (iCurImage->Format != IL_BYTE || iCurImage->Format != IL_UNSIGNED_BYTE) ) {
925 ilSetError(IL_ILLEGAL_OPERATION);
926 return IL_FALSE;
927 }
928
929 NewData = (ILubyte*)ialloc(iCurImage->Width * iCurImage->Height * iCurImage->Depth);
930 if (NewData == NULL) {
931 return IL_FALSE;
932 }
933
934 iCurImage = &Image;
935 imemclear(&Image, sizeof(ILimage));
936 // IL_PAL_RGB24, because we don't want to make parts transparent that shouldn't be.
937 if (!ilLoadPal(FileName) || !ilConvertPal(IL_PAL_RGB24)) {
938 ifree(NewData);
939 iCurImage = CurImage;
940 return IL_FALSE;
941 }
942
943 NumColours = Image.Pal.PalSize / 3; // RGB24 is 3 bytes per entry.
944 PalInfo = (ILuint*)ialloc(NumColours * sizeof(ILuint));
945 if (PalInfo == NULL) {
946 ifree(NewData);
947 iCurImage = CurImage;
948 return IL_FALSE;
949 }
950
951 NumPix = CurImage->SizeOfData / ilGetBppFormat(CurImage->Format);
952 switch (CurImage->Format)
953 {
954 case IL_COLOUR_INDEX:
955 iCurImage = CurImage;
956 if (!ilConvertPal(IL_PAL_RGB24)) {
957 ifree(NewData);
958 ifree(PalInfo);
959 return IL_FALSE;
960 }
961
962 NumPix = iCurImage->Pal.PalSize / ilGetBppPal(iCurImage->Pal.PalType);
963 for (i = 0; i < NumPix; i++) {
964 for (j = 0; j < Image.Pal.PalSize; j += 3) {
965 // No need to perform a sqrt.
966 Dist1 = (ILint)iCurImage->Pal.Palette[i] - (ILint)Image.Pal.Palette[j];
967 Dist2 = (ILint)iCurImage->Pal.Palette[i+1] - (ILint)Image.Pal.Palette[j+1];
968 Dist3 = (ILint)iCurImage->Pal.Palette[i+2] - (ILint)Image.Pal.Palette[j+2];
969 PalInfo[j / 3] = Dist1 * Dist1 + Dist2 * Dist2 + Dist3 * Dist3;
970 }
971 MaxDist = UINT_MAX;

Callers

nothing calls this directly

Calls 3

ilSetErrorFunction · 0.85
ilGetBppFormatFunction · 0.85
ilGetBppPalFunction · 0.85

Tested by

no test coverage detected