- Main Page
- Data Structures
- Files

- File List

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

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Copyright (C) 2000-2009 by Denton Woods
    00005 // Last modified: 12/15/2009
    00006 //
    00007 // Filename: src-IL/src/il_utility.c
    00008 //
    00009 // Description: Utility functions
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014 #include "il_internal.h"
    00015
    00016
    00017 // Returns the bpp of any Format
    00018 ILAPI ILubyte ILAPIENTRY ilGetBppFormat(ILenum Format)
    00019 {
    00020         switch (Format) {
    00021                 case IL_COLOUR_INDEX:
    00022                 case IL_LUMINANCE:
    00023                 case IL_ALPHA:
    00024                         return 1;
    00025                 case IL_LUMINANCE_ALPHA:
    00026                         return 2;
    00027                 case IL_RGB:
    00028                 case IL_BGR:
    00029                         return 3;
    00030                 case IL_RGBA:
    00031                 case IL_BGRA:
    00032                         return 4;
    00033         }
    00034         return 0;
    00035 }
    00036
    00037
    00038 // Returns the format of any bpp
    00039 ILAPI ILenum ILAPIENTRY ilGetFormatBpp(ILubyte Bpp)
    00040 {
    00041         switch (Bpp)
    00042         {
    00043                 case 1:
    00044                         return IL_LUMINANCE;
    00045                 case 2:
    00046                         return IL_LUMINANCE_ALPHA;
    00047                 case 3:
    00048                         return IL_RGB;
    00049                 case 4:
    00050                         return IL_RGBA;
    00051         }
    00052         return 0;
    00053 }
    00054
    00055
    00056 // Returns the bpc of any Type
    00057 ILAPI ILubyte ILAPIENTRY ilGetBpcType(ILenum Type)
    00058 {
    00059         switch (Type)
    00060         {
    00061                 case IL_BYTE:
    00062                 case IL_UNSIGNED_BYTE:
    00063                         return 1;
    00064                 case IL_SHORT:
    00065                 case IL_UNSIGNED_SHORT:
    00066                 case IL_HALF:
    00067                         return 2;
    00068                 case IL_INT:
    00069                 case IL_UNSIGNED_INT:
    00070                 case IL_FLOAT:
    00071                         return 4;
    00072                 case IL_DOUBLE:
    00073                         return 8;
    00074         }
    00075         return 0;
    00076 }
    00077
    00078
    00079 // Returns the type matching a bpc
    00080 ILAPI ILenum ILAPIENTRY ilGetTypeBpc(ILubyte Bpc)
    00081 {
    00082         switch (Bpc)
    00083         {
    00084                 case 1:
    00085                         return IL_UNSIGNED_BYTE;
    00086                 case 2:
    00087                         return IL_UNSIGNED_SHORT;
    00088                 case 4:
    00089                         return IL_UNSIGNED_INT;
    00090                 case 8:
    00091                         return IL_DOUBLE;
    00092         }
    00093         return 0;
    00094 }
    00095
    00096
    00097 // Returns the bpp of any palette type (PalType)
    00098 ILAPI ILubyte ILAPIENTRY ilGetBppPal(ILenum PalType)
    00099 {
    00100         switch (PalType)
    00101         {
    00102                 case IL_PAL_RGB24:
    00103                 case IL_PAL_BGR24:
    00104                         return 3;
    00105                 case IL_PAL_RGB32:
    00106                 case IL_PAL_RGBA32:
    00107                 case IL_PAL_BGR32:
    00108                 case IL_PAL_BGRA32:
    00109                         return 4;
    00110         }
    00111         return 0;
    00112 }
    00113
    00114 // Returns the base format of a palette type (PalType)
    00115 ILAPI ILenum ILAPIENTRY ilGetPalBaseType(ILenum PalType)
    00116 {
    00117         switch (PalType)
    00118         {
    00119                 case IL_PAL_RGB24:
    00120                         return IL_RGB;
    00121                 case IL_PAL_RGB32:
    00122                         return IL_RGBA;  // Not sure
    00123                 case IL_PAL_RGBA32:
    00124                         return IL_RGBA;
    00125                 case IL_PAL_BGR24:
    00126                         return IL_BGR;
    00127                 case IL_PAL_BGR32:
    00128                         return IL_BGRA;  // Not sure
    00129                 case IL_PAL_BGRA32:
    00130                         return IL_BGRA;
    00131         }
    00132
    00133         return 0;
    00134 }
    00135
    00136
    00137 // Returns the next power of 2 if Num isn't 2^n or returns Num if Num is 2^n
    00138 ILAPI ILuint ILAPIENTRY ilNextPower2(ILuint n)
    00139 {
    00140         ILuint power = 1;
    00141         while( power < n ) {
    00142                 power <<= 1;
    00143         }
    00144         return power;
    00145 }
    00146
    00147 ILAPI void ILAPIENTRY iMemSwap(ILubyte *s1, ILubyte *s2, const ILuint size)
    00148 {
    00149         const ILuint block_size = 4096;
    00150         const ILuint blocks = size/block_size;
    00151         ILuint i;
    00152
    00153         ILubyte *block = (ILubyte*)ialloc(block_size);
    00154         if(block == NULL) return;
    00155         for( i = 0; i < blocks; i++ ) {
    00156                 memcpy(block,s1,block_size);
    00157                 memcpy(s1,s2,block_size);
    00158                 memcpy(s2,block,block_size);
    00159                 s2 += block_size;
    00160                 s1 += block_size;
    00161         }
    00162         i = size - i*block_size;
    00163         if( i > 0 ) {
    00164                 memcpy(block,s1,i);
    00165                 memcpy(s1,s2,i);
    00166                 memcpy(s2,block,i);
    00167         }
    00168         ifree(block);
    00169         return;
    00170 }