1 //----------------------------------------------------------------------------- 2 // 3 // ImageLib Sources 4 // Last modified: 12/06/2006 5 // 6 // Filename: src-IL/src/il_endian.c 7 // 8 // Description: Takes care of endian issues 9 // 10 //----------------------------------------------------------------------------- 11 12 13 #define IL_ENDIAN_C 14 15 #include "il_endian.h" 16 EndianSwapData(void * _Image)17void EndianSwapData(void *_Image) 18 { 19 ILuint i; 20 ILubyte *temp, *s, *d; 21 ILushort *ShortS, *ShortD; 22 ILuint *IntS, *IntD; 23 ILfloat *FltS, *FltD; 24 ILdouble *DblS, *DblD; 25 26 ILimage *Image = (ILimage*)_Image; 27 28 switch (Image->Type) { 29 case IL_BYTE: 30 case IL_UNSIGNED_BYTE: 31 switch (Image->Bpp) { 32 case 3: 33 temp = (ILubyte*)ialloc(Image->SizeOfData); 34 if (temp == NULL) 35 return; 36 s = Image->Data; 37 d = temp; 38 39 for( i = Image->Width * Image->Height; i > 0; i-- ) { 40 *d++ = *(s+2); 41 *d++ = *(s+1); 42 *d++ = *s; 43 s += 3; 44 } 45 46 ifree(Image->Data); 47 Image->Data = temp; 48 break; 49 50 case 4: 51 temp = (ILubyte*)ialloc(Image->SizeOfData); 52 if (temp == NULL) 53 return; 54 s = Image->Data; 55 d = temp; 56 57 for (i = Image->Width * Image->Height; i > 0; i--) { 58 *d++ = *(s+3); 59 *d++ = *(s+2); 60 *d++ = *(s+1); 61 *d++ = *s; 62 s += 4; 63 } 64 65 ifree(Image->Data); 66 Image->Data = temp; 67 break; 68 } 69 break; 70 71 case IL_SHORT: 72 case IL_UNSIGNED_SHORT: 73 switch (Image->Bpp) { 74 case 3: 75 temp = (ILubyte*)ialloc(Image->SizeOfData); 76 if (temp == NULL) 77 return; 78 ShortS = (ILushort*)Image->Data; 79 ShortD = (ILushort*)temp; 80 81 for (i = Image->Width * Image->Height; i > 0; i--) { 82 *ShortD = *ShortS++; iSwapUShort(ShortD++); 83 *ShortD = *ShortS++; iSwapUShort(ShortD++); 84 *ShortD = *ShortS++; iSwapUShort(ShortD++); 85 } 86 87 ifree(Image->Data); 88 Image->Data = temp; 89 break; 90 91 case 4: 92 temp = (ILubyte*)ialloc(Image->SizeOfData); 93 if (temp == NULL) 94 return; 95 ShortS = (ILushort*)Image->Data; 96 ShortD = (ILushort*)temp; 97 98 for (i = Image->Width * Image->Height; i > 0; i--) { 99 *ShortD = *ShortS++; iSwapUShort(ShortD++); 100 *ShortD = *ShortS++; iSwapUShort(ShortD++); 101 *ShortD = *ShortS++; iSwapUShort(ShortD++); 102 *ShortD = *ShortS++; iSwapUShort(ShortD++); 103 } 104 105 ifree(Image->Data); 106 Image->Data = temp; 107 break; 108 } 109 break; 110 111 case IL_INT: 112 case IL_UNSIGNED_INT: 113 switch (Image->Bpp) 114 { 115 case 3: 116 temp = (ILubyte*)ialloc(Image->SizeOfData); 117 if (temp == NULL) 118 return; 119 IntS = (ILuint*)Image->Data; 120 IntD = (ILuint*)temp; 121 122 for (i = Image->Width * Image->Height; i > 0; i--) { 123 *IntD = *IntS++; iSwapUInt(IntD++); 124 *IntD = *IntS++; iSwapUInt(IntD++); 125 *IntD = *IntS++; iSwapUInt(IntD++); 126 } 127 128 ifree(Image->Data); 129 Image->Data = temp; 130 break; 131 132 case 4: 133 temp = (ILubyte*)ialloc(Image->SizeOfData); 134 if (temp == NULL) 135 return; 136 IntS = (ILuint*)Image->Data; 137 IntD = (ILuint*)temp; 138 139 for (i = Image->Width * Image->Height; i > 0; i--) { 140 *IntD = *IntS++; iSwapUInt(IntD++); 141 *IntD = *IntS++; iSwapUInt(IntD++); 142 *IntD = *IntS++; iSwapUInt(IntD++); 143 *IntD = *IntS++; iSwapUInt(IntD++); 144 } 145 146 ifree(Image->Data); 147 Image->Data = temp; 148 break; 149 } 150 break; 151 152 case IL_FLOAT: 153 switch (Image->Bpp) 154 { 155 case 3: 156 temp = (ILubyte*)ialloc(Image->SizeOfData); 157 if (temp == NULL) 158 return; 159 FltS = (ILfloat*)Image->Data; 160 FltD = (ILfloat*)temp; 161 162 for (i = Image->Width * Image->Height; i > 0; i--) { 163 *FltD = *FltS++; iSwapFloat(FltD++); 164 *FltD = *FltS++; iSwapFloat(FltD++); 165 *FltD = *FltS++; iSwapFloat(FltD++); 166 } 167 168 ifree(Image->Data); 169 Image->Data = temp; 170 break; 171 172 case 4: 173 temp = (ILubyte*)ialloc(Image->SizeOfData); 174 if (temp == NULL) 175 return; 176 FltS = (ILfloat*)Image->Data; 177 FltD = (ILfloat*)temp; 178 179 for (i = Image->Width * Image->Height; i > 0; i--) { 180 *FltD = *FltS++; iSwapFloat(FltD++); 181 *FltD = *FltS++; iSwapFloat(FltD++); 182 *FltD = *FltS++; iSwapFloat(FltD++); 183 *FltD = *FltS++; iSwapFloat(FltD++); 184 } 185 186 ifree(Image->Data); 187 Image->Data = temp; 188 break; 189 } 190 break; 191 192 case IL_DOUBLE: 193 switch (Image->Bpp) 194 { 195 case 3: 196 temp = (ILubyte*)ialloc(Image->SizeOfData); 197 if (temp == NULL) 198 return; 199 DblS = (ILdouble*)Image->Data; 200 DblD = (ILdouble*)temp; 201 202 for (i = Image->Width * Image->Height; i > 0; i--) { 203 *DblD = *DblS++; iSwapDouble(DblD++); 204 *DblD = *DblS++; iSwapDouble(DblD++); 205 *DblD = *DblS++; iSwapDouble(DblD++); 206 } 207 208 ifree(Image->Data); 209 Image->Data = temp; 210 break; 211 212 case 4: 213 temp = (ILubyte*)ialloc(Image->SizeOfData); 214 if (temp == NULL) 215 return; 216 DblS = (ILdouble*)Image->Data; 217 DblD = (ILdouble*)temp; 218 219 for (i = Image->Width * Image->Height; i > 0; i--) { 220 *DblD = *DblS++; iSwapDouble(DblD++); 221 *DblD = *DblS++; iSwapDouble(DblD++); 222 *DblD = *DblS++; iSwapDouble(DblD++); 223 *DblD = *DblS++; iSwapDouble(DblD++); 224 } 225 226 ifree(Image->Data); 227 Image->Data = temp; 228 break; 229 } 230 break; 231 } 232 233 if( iCurImage->Format == IL_COLOUR_INDEX ) { 234 switch (iCurImage->Pal.PalType) { 235 case IL_PAL_RGB24: 236 case IL_PAL_BGR24: 237 temp = (ILubyte*)ialloc(Image->Pal.PalSize); 238 if (temp == NULL) 239 return; 240 s = Image->Pal.Palette; 241 d = temp; 242 243 for (i = Image->Pal.PalSize / 3; i > 0; i--) { 244 *d++ = *(s+2); 245 *d++ = *(s+1); 246 *d++ = *s; 247 s += 3; 248 } 249 250 ifree(Image->Pal.Palette); 251 Image->Pal.Palette = temp; 252 break; 253 254 case IL_PAL_RGBA32: 255 case IL_PAL_RGB32: 256 case IL_PAL_BGRA32: 257 case IL_PAL_BGR32: 258 temp = (ILubyte*)ialloc(Image->Pal.PalSize); 259 if (temp == NULL) 260 return; 261 s = Image->Pal.Palette; 262 d = temp; 263 264 for (i = Image->Pal.PalSize / 4; i > 0; i--) { 265 *d++ = *(s+3); 266 *d++ = *(s+2); 267 *d++ = *(s+1); 268 *d++ = *s; 269 s += 4; 270 } 271 272 ifree(Image->Pal.Palette); 273 Image->Pal.Palette = temp; 274 break; 275 } 276 } 277 return; 278 } 279