summaryrefslogtreecommitdiff
path: root/DevIL/src-IL/src/il_utility.cpp
blob: e2e6ca7a6146c891e01bf9d08155c44179ef930d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//-----------------------------------------------------------------------------
//
// ImageLib Sources
// Copyright (C) 2000-2009 by Denton Woods
// Last modified: 02/04/2009
//
// Filename: src-IL/src/il_utility.c
//
// Description: Utility functions
//
//-----------------------------------------------------------------------------


#include "il_internal.h"


// Returns the bpp of any Format
ILAPI ILubyte ILAPIENTRY ilGetBppFormat(ILenum Format)
{
	switch (Format)
	{
		case IL_COLOUR_INDEX:
		case IL_LUMINANCE:
		case IL_ALPHA:
			return 1;
		case IL_LUMINANCE_ALPHA:
			return 2;
		case IL_RGB:
		case IL_BGR:
			return 3;
		case IL_RGBA:
		case IL_BGRA:
			return 4;
	}
	return 0;
}


// Returns the format of any bpp
ILAPI ILenum ILAPIENTRY ilGetFormatBpp(ILubyte Bpp)
{
	switch (Bpp)
	{
		case 1:
			return IL_LUMINANCE;
		case 2:
			return IL_LUMINANCE_ALPHA;
		case 3:
			return IL_RGB;
		case 4:
			return IL_RGBA;
	}
	return 0;
}


// Returns the bpc of any Type
ILAPI ILubyte ILAPIENTRY ilGetBpcType(ILenum Type)
{
	switch (Type)
	{
		case IL_BYTE:
		case IL_UNSIGNED_BYTE:
			return 1;
		case IL_SHORT:
		case IL_UNSIGNED_SHORT:
		case IL_HALF:
			return 2;
		case IL_INT:
		case IL_UNSIGNED_INT:
		case IL_FLOAT:
			return 4;
		case IL_DOUBLE:
			return 8;
	}
	return 0;
}


// Returns the type matching a bpc
ILAPI ILenum ILAPIENTRY ilGetTypeBpc(ILubyte Bpc)
{
	switch (Bpc)
	{
		case 1:
			return IL_UNSIGNED_BYTE;
		case 2:
			return IL_UNSIGNED_SHORT;
		case 4:
			return IL_UNSIGNED_INT;
		case 8:
			return IL_DOUBLE;
	}
	return 0;
}


// Returns the bpp of any palette type (PalType)
ILAPI ILubyte ILAPIENTRY ilGetBppPal(ILenum PalType)
{
	switch (PalType)
	{
		case IL_PAL_RGB24:
		case IL_PAL_BGR24:
			return 3;
		case IL_PAL_RGB32:
		case IL_PAL_RGBA32:
		case IL_PAL_BGR32:
		case IL_PAL_BGRA32:
			return 4;
	}
	return 0;
}

// Returns the base format of a palette type (PalType)
ILAPI ILenum ILAPIENTRY ilGetPalBaseType(ILenum PalType)
{
	switch (PalType)
	{
		case IL_PAL_RGB24:
			return IL_RGB;
		case IL_PAL_RGB32:
			return IL_RGBA;  // Not sure
		case IL_PAL_RGBA32:
			return IL_RGBA;
		case IL_PAL_BGR24:
			return IL_BGR;
		case IL_PAL_BGR32:
			return IL_BGRA;  // Not sure
		case IL_PAL_BGRA32:
			return IL_BGRA;
	}

	return 0;
}


// Returns the next power of 2 if Num isn't 2^n or returns Num if Num is 2^n
ILAPI ILuint ILAPIENTRY ilNextPower2(ILuint n)
{	
	ILuint power = 1;
	while( power < n ) {
		power <<= 1;
	}
	return power;
}

ILAPI void ILAPIENTRY iMemSwap(ILubyte *s1, ILubyte *s2, const ILuint size)
{
	const ILuint block_size = 4096;
	const ILuint blocks = size/block_size;
	ILuint i;

	ILubyte *block = (ILubyte*)ialloc(block_size);
	if(block == NULL) return;
	for( i = 0; i < blocks; i++ ) {
		memcpy(block,s1,block_size);
		memcpy(s1,s2,block_size);
		memcpy(s2,block,block_size);
		s2 += block_size;
		s1 += block_size;
	}
	i = size - i*block_size;
	if( i > 0 ) {
		memcpy(block,s1,i);
		memcpy(s1,s2,i);
		memcpy(s2,block,i);
	}
	ifree(block);
	return;
}