1 //-----------------------------------------------------------------------------
2 //
3 // ImageLib Sources
4 // Copyright (C) 2000-2009 by Denton Woods
5 // Last modified: 02/04/2009
6 //
7 // Filename: src-IL/src/il_utility.c
8 //
9 // Description: Utility functions
10 //
11 //-----------------------------------------------------------------------------
12
13
14 #include "il_internal.h"
15
16
17 // Returns the bpp of any Format
ilGetBppFormat(ILenum Format)18 ILAPI ILubyte ILAPIENTRY ilGetBppFormat(ILenum Format)
19 {
20 switch (Format)
21 {
22 case IL_COLOUR_INDEX:
23 case IL_LUMINANCE:
24 case IL_ALPHA:
25 return 1;
26 case IL_LUMINANCE_ALPHA:
27 return 2;
28 case IL_RGB:
29 case IL_BGR:
30 return 3;
31 case IL_RGBA:
32 case IL_BGRA:
33 return 4;
34 }
35 return 0;
36 }
37
38
39 // Returns the format of any bpp
ilGetFormatBpp(ILubyte Bpp)40 ILAPI ILenum ILAPIENTRY ilGetFormatBpp(ILubyte Bpp)
41 {
42 switch (Bpp)
43 {
44 case 1:
45 return IL_LUMINANCE;
46 case 2:
47 return IL_LUMINANCE_ALPHA;
48 case 3:
49 return IL_RGB;
50 case 4:
51 return IL_RGBA;
52 }
53 return 0;
54 }
55
56
57 // Returns the bpc of any Type
ilGetBpcType(ILenum Type)58 ILAPI ILubyte ILAPIENTRY ilGetBpcType(ILenum Type)
59 {
60 switch (Type)
61 {
62 case IL_BYTE:
63 case IL_UNSIGNED_BYTE:
64 return 1;
65 case IL_SHORT:
66 case IL_UNSIGNED_SHORT:
67 case IL_HALF:
68 return 2;
69 case IL_INT:
70 case IL_UNSIGNED_INT:
71 case IL_FLOAT:
72 return 4;
73 case IL_DOUBLE:
74 return 8;
75 }
76 return 0;
77 }
78
79
80 // Returns the type matching a bpc
ilGetTypeBpc(ILubyte Bpc)81 ILAPI ILenum ILAPIENTRY ilGetTypeBpc(ILubyte Bpc)
82 {
83 switch (Bpc)
84 {
85 case 1:
86 return IL_UNSIGNED_BYTE;
87 case 2:
88 return IL_UNSIGNED_SHORT;
89 case 4:
90 return IL_UNSIGNED_INT;
91 case 8:
92 return IL_DOUBLE;
93 }
94 return 0;
95 }
96
97
98 // Returns the bpp of any palette type (PalType)
ilGetBppPal(ILenum PalType)99 ILAPI ILubyte ILAPIENTRY ilGetBppPal(ILenum PalType)
100 {
101 switch (PalType)
102 {
103 case IL_PAL_RGB24:
104 case IL_PAL_BGR24:
105 return 3;
106 case IL_PAL_RGB32:
107 case IL_PAL_RGBA32:
108 case IL_PAL_BGR32:
109 case IL_PAL_BGRA32:
110 return 4;
111 }
112 return 0;
113 }
114
115 // Returns the base format of a palette type (PalType)
ilGetPalBaseType(ILenum PalType)116 ILAPI ILenum ILAPIENTRY ilGetPalBaseType(ILenum PalType)
117 {
118 switch (PalType)
119 {
120 case IL_PAL_RGB24:
121 return IL_RGB;
122 case IL_PAL_RGB32:
123 return IL_RGBA; // Not sure
124 case IL_PAL_RGBA32:
125 return IL_RGBA;
126 case IL_PAL_BGR24:
127 return IL_BGR;
128 case IL_PAL_BGR32:
129 return IL_BGRA; // Not sure
130 case IL_PAL_BGRA32:
131 return IL_BGRA;
132 }
133
134 return 0;
135 }
136
137
138 // Returns the next power of 2 if Num isn't 2^n or returns Num if Num is 2^n
ilNextPower2(ILuint n)139 ILAPI ILuint ILAPIENTRY ilNextPower2(ILuint n)
140 {
141 ILuint power = 1;
142 while( power < n ) {
143 power <<= 1;
144 }
145 return power;
146 }
147
iMemSwap(ILubyte * s1,ILubyte * s2,const ILuint size)148 ILAPI void ILAPIENTRY iMemSwap(ILubyte *s1, ILubyte *s2, const ILuint size)
149 {
150 const ILuint block_size = 4096;
151 const ILuint blocks = size/block_size;
152 ILuint i;
153
154 ILubyte *block = (ILubyte*)ialloc(block_size);
155 if(block == NULL) return;
156 for( i = 0; i < blocks; i++ ) {
157 memcpy(block,s1,block_size);
158 memcpy(s1,s2,block_size);
159 memcpy(s2,block,block_size);
160 s2 += block_size;
161 s1 += block_size;
162 }
163 i = size - i*block_size;
164 if( i > 0 ) {
165 memcpy(block,s1,i);
166 memcpy(s1,s2,i);
167 memcpy(s2,block,i);
168 }
169 ifree(block);
170 return;
171 }
172