- Main Page
- Data Structures
- Files

- File List

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

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Last modified: 12/06/2006
    00005 //
    00006 // Filename: src-IL/src/il_endian.c
    00007 //
    00008 // Description: Takes care of endian issues
    00009 //
    00010 //-----------------------------------------------------------------------------
    00011
    00012
    00013 #define IL_ENDIAN_C
    00014
    00015 #include "il_endian.h"
    00016
    00017 void EndianSwapData(void *_Image)
    00018 {
    00019         ILuint          i;
    00020         ILubyte         *temp, *s, *d;
    00021         ILushort        *ShortS, *ShortD;
    00022         ILuint          *IntS, *IntD;
    00023         ILfloat         *FltS, *FltD;
    00024         ILdouble        *DblS, *DblD;
    00025
    00026         ILimage *Image = (ILimage*)_Image;
    00027
    00028         switch (Image->Type) {
    00029                 case IL_BYTE:
    00030                 case IL_UNSIGNED_BYTE:
    00031                         switch (Image->Bpp) {
    00032                                 case 3:
    00033                                         temp = (ILubyte*)ialloc(Image->SizeOfData);
    00034                                         if (temp == NULL)
    00035                                                 return;
    00036                                         s = Image->Data;
    00037                                         d = temp;
    00038
    00039                                         for( i = Image->Width * Image->Height; i > 0; i-- ) {
    00040                                                 *d++ = *(s+2);
    00041                                                 *d++ = *(s+1);
    00042                                                 *d++ = *s;
    00043                                                 s += 3;
    00044                                         }
    00045
    00046                                         ifree(Image->Data);
    00047                                         Image->Data = temp;
    00048                                         break;
    00049
    00050                                 case 4:
    00051                                         temp = (ILubyte*)ialloc(Image->SizeOfData);
    00052                                         if (temp == NULL)
    00053                                                 return;
    00054                                         s = Image->Data;
    00055                                         d = temp;
    00056
    00057                                         for (i = Image->Width * Image->Height; i > 0; i--) {
    00058                                                 *d++ = *(s+3);
    00059                                                 *d++ = *(s+2);
    00060                                                 *d++ = *(s+1);
    00061                                                 *d++ = *s;
    00062                                                 s += 4;
    00063                                         }
    00064
    00065                                         ifree(Image->Data);
    00066                                         Image->Data = temp;
    00067                                         break;
    00068                         }
    00069                         break;
    00070
    00071                 case IL_SHORT:
    00072                 case IL_UNSIGNED_SHORT:
    00073                         switch (Image->Bpp) {
    00074                                 case 3:
    00075                                         temp = (ILubyte*)ialloc(Image->SizeOfData);
    00076                                         if (temp == NULL)
    00077                                                 return;
    00078                                         ShortS = (ILushort*)Image->Data;
    00079                                         ShortD = (ILushort*)temp;
    00080
    00081                                         for (i = Image->Width * Image->Height; i > 0; i--) {
    00082                                                 *ShortD = *ShortS++; iSwapUShort(ShortD++);
    00083                                                 *ShortD = *ShortS++; iSwapUShort(ShortD++);
    00084                                                 *ShortD = *ShortS++; iSwapUShort(ShortD++);
    00085                                         }
    00086
    00087                                         ifree(Image->Data);
    00088                                         Image->Data = temp;
    00089                                         break;
    00090
    00091                                 case 4:
    00092                                         temp = (ILubyte*)ialloc(Image->SizeOfData);
    00093                                         if (temp == NULL)
    00094                                                 return;
    00095                                         ShortS = (ILushort*)Image->Data;
    00096                                         ShortD = (ILushort*)temp;
    00097
    00098                                         for (i = Image->Width * Image->Height; i > 0; i--) {
    00099                                                 *ShortD = *ShortS++; iSwapUShort(ShortD++);
    00100                                                 *ShortD = *ShortS++; iSwapUShort(ShortD++);
    00101                                                 *ShortD = *ShortS++; iSwapUShort(ShortD++);
    00102                                                 *ShortD = *ShortS++; iSwapUShort(ShortD++);
    00103                                         }
    00104
    00105                                         ifree(Image->Data);
    00106                                         Image->Data = temp;
    00107                                         break;
    00108                         }
    00109                         break;
    00110
    00111                 case IL_INT:
    00112                 case IL_UNSIGNED_INT:
    00113                         switch (Image->Bpp)
    00114                         {
    00115                                 case 3:
    00116                                         temp = (ILubyte*)ialloc(Image->SizeOfData);
    00117                                         if (temp == NULL)
    00118                                                 return;
    00119                                         IntS = (ILuint*)Image->Data;
    00120                                         IntD = (ILuint*)temp;
    00121
    00122                                         for (i = Image->Width * Image->Height; i > 0; i--) {
    00123                                                 *IntD = *IntS++; iSwapUInt(IntD++);
    00124                                                 *IntD = *IntS++; iSwapUInt(IntD++);
    00125                                                 *IntD = *IntS++; iSwapUInt(IntD++);
    00126                                         }
    00127
    00128                                         ifree(Image->Data);
    00129                                         Image->Data = temp;
    00130                                         break;
    00131
    00132                                 case 4:
    00133                                         temp = (ILubyte*)ialloc(Image->SizeOfData);
    00134                                         if (temp == NULL)
    00135                                                 return;
    00136                                         IntS = (ILuint*)Image->Data;
    00137                                         IntD = (ILuint*)temp;
    00138
    00139                                         for (i = Image->Width * Image->Height; i > 0; i--) {
    00140                                                 *IntD = *IntS++; iSwapUInt(IntD++);
    00141                                                 *IntD = *IntS++; iSwapUInt(IntD++);
    00142                                                 *IntD = *IntS++; iSwapUInt(IntD++);
    00143                                                 *IntD = *IntS++; iSwapUInt(IntD++);
    00144                                         }
    00145
    00146                                         ifree(Image->Data);
    00147                                         Image->Data = temp;
    00148                                         break;
    00149                         }
    00150                         break;
    00151
    00152                 case IL_FLOAT:
    00153                         switch (Image->Bpp)
    00154                         {
    00155                                 case 3:
    00156                                         temp = (ILubyte*)ialloc(Image->SizeOfData);
    00157                                         if (temp == NULL)
    00158                                                 return;
    00159                                         FltS = (ILfloat*)Image->Data;
    00160                                         FltD = (ILfloat*)temp;
    00161
    00162                                         for (i = Image->Width * Image->Height; i > 0; i--) {
    00163                                                 *FltD = *FltS++; iSwapFloat(FltD++);
    00164                                                 *FltD = *FltS++; iSwapFloat(FltD++);
    00165                                                 *FltD = *FltS++; iSwapFloat(FltD++);
    00166                                         }
    00167
    00168                                         ifree(Image->Data);
    00169                                         Image->Data = temp;
    00170                                         break;
    00171
    00172                                 case 4:
    00173                                         temp = (ILubyte*)ialloc(Image->SizeOfData);
    00174                                         if (temp == NULL)
    00175                                                 return;
    00176                                         FltS = (ILfloat*)Image->Data;
    00177                                         FltD = (ILfloat*)temp;
    00178
    00179                                         for (i = Image->Width * Image->Height; i > 0; i--) {
    00180                                                 *FltD = *FltS++; iSwapFloat(FltD++);
    00181                                                 *FltD = *FltS++; iSwapFloat(FltD++);
    00182                                                 *FltD = *FltS++; iSwapFloat(FltD++);
    00183                                                 *FltD = *FltS++; iSwapFloat(FltD++);
    00184                                         }
    00185
    00186                                         ifree(Image->Data);
    00187                                         Image->Data = temp;
    00188                                         break;
    00189                         }
    00190                         break;
    00191
    00192                 case IL_DOUBLE:
    00193                         switch (Image->Bpp)
    00194                         {
    00195                                 case 3:
    00196                                         temp = (ILubyte*)ialloc(Image->SizeOfData);
    00197                                         if (temp == NULL)
    00198                                                 return;
    00199                                         DblS = (ILdouble*)Image->Data;
    00200                                         DblD = (ILdouble*)temp;
    00201
    00202                                         for (i = Image->Width * Image->Height; i > 0; i--) {
    00203                                                 *DblD = *DblS++; iSwapDouble(DblD++);
    00204                                                 *DblD = *DblS++; iSwapDouble(DblD++);
    00205                                                 *DblD = *DblS++; iSwapDouble(DblD++);
    00206                                         }
    00207
    00208                                         ifree(Image->Data);
    00209                                         Image->Data = temp;
    00210                                         break;
    00211
    00212                                 case 4:
    00213                                         temp = (ILubyte*)ialloc(Image->SizeOfData);
    00214                                         if (temp == NULL)
    00215                                                 return;
    00216                                         DblS = (ILdouble*)Image->Data;
    00217                                         DblD = (ILdouble*)temp;
    00218
    00219                                         for (i = Image->Width * Image->Height; i > 0; i--) {
    00220                                                 *DblD = *DblS++; iSwapDouble(DblD++);
    00221                                                 *DblD = *DblS++; iSwapDouble(DblD++);
    00222                                                 *DblD = *DblS++; iSwapDouble(DblD++);
    00223                                                 *DblD = *DblS++; iSwapDouble(DblD++);
    00224                                         }
    00225
    00226                                         ifree(Image->Data);
    00227                                         Image->Data = temp;
    00228                                         break;
    00229                         }
    00230                         break;
    00231         }
    00232
    00233         if( iCurImage->Format == IL_COLOUR_INDEX ) {
    00234                 switch (iCurImage->Pal.PalType) {
    00235                         case IL_PAL_RGB24:
    00236                         case IL_PAL_BGR24:
    00237                                 temp = (ILubyte*)ialloc(Image->Pal.PalSize);
    00238                                 if (temp == NULL)
    00239                                         return;
    00240                                 s = Image->Pal.Palette;
    00241                                 d = temp;
    00242
    00243                                 for (i = Image->Pal.PalSize / 3; i > 0; i--) {
    00244                                         *d++ = *(s+2);
    00245                                         *d++ = *(s+1);
    00246                                         *d++ = *s;
    00247                                         s += 3;
    00248                                 }
    00249
    00250                                 ifree(Image->Pal.Palette);
    00251                                 Image->Pal.Palette = temp;
    00252                                 break;
    00253
    00254                         case IL_PAL_RGBA32:
    00255                         case IL_PAL_RGB32:
    00256                         case IL_PAL_BGRA32:
    00257                         case IL_PAL_BGR32:
    00258                                 temp = (ILubyte*)ialloc(Image->Pal.PalSize);
    00259                                 if (temp == NULL)
    00260                                         return;
    00261                                 s = Image->Pal.Palette;
    00262                                 d = temp;
    00263
    00264                                 for (i = Image->Pal.PalSize / 4; i > 0; i--) {
    00265                                         *d++ = *(s+3);
    00266                                         *d++ = *(s+2);
    00267                                         *d++ = *(s+1);
    00268                                         *d++ = *s;
    00269                                         s += 4;
    00270                                 }
    00271
    00272                                 ifree(Image->Pal.Palette);
    00273                                 Image->Pal.Palette = temp;
    00274                                 break;
    00275                 }
    00276         }
    00277         return;
    00278 }