PGF Console
6.21.2
Toggle main menu visibility
Loading...
Searching...
No Matches
Utilities.h
Go to the documentation of this file.
1
// ==========================================================
2
// Utility functions
3
//
4
// Design and implementation by
5
// - Floris van den Berg (flvdberg@wxs.nl)
6
// - Herv� Drolon <drolon@infonie.fr>
7
// - Ryan Rubley (ryan@lostreality.org)
8
//
9
// This file is part of FreeImage 3
10
//
11
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
12
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
13
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
14
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
15
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
16
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
17
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
18
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
19
// THIS DISCLAIMER.
20
//
21
// Use at your own risk!
22
// ==========================================================
23
24
#ifndef UTILITIES_H
25
#define UTILITIES_H
26
27
// ==========================================================
28
// Standard includes used by the library
29
// ==========================================================
30
31
#include <math.h>
32
#include <stdlib.h>
33
#include <memory.h>
34
#include <stdio.h>
35
#include <string.h>
36
#include <stdarg.h>
37
#include <ctype.h>
38
#include <assert.h>
39
40
#include <string>
41
#include <list>
42
#include <map>
43
#include <set>
44
#include <vector>
45
#include <stack>
46
#include <sstream>
47
#include <algorithm>
48
49
#if defined(__linux__) || defined(__APPLE__)
50
#define nullptr NULL
51
#endif
52
53
// ==========================================================
54
// Bitmap palette and pixels alignment
55
// ==========================================================
56
57
#define FIBITMAP_ALIGNMENT 16
// We will use a 16 bytes alignment boundary
58
59
// Memory allocation on a specified alignment boundary
60
// defined in BitmapAccess.cpp
61
62
void
*
FreeImage_Aligned_Malloc
(
size_t
amount,
size_t
alignment);
63
void
FreeImage_Aligned_Free
(
void
* mem);
64
65
// ==========================================================
66
// File I/O structs
67
// ==========================================================
68
69
// these structs are for file I/O and should not be confused with similar
70
// structs in FreeImage.h which are for in-memory bitmap handling
71
72
#ifdef _WIN32
73
#pragma pack(push, 1)
74
#else
75
#pragma pack(1)
76
#endif
// _WIN32
77
78
typedef
struct
tagFILE_RGBA
{
79
unsigned
char
r
,
g
,
b
,
a
;
80
}
FILE_RGBA
;
81
82
typedef
struct
tagFILE_BGRA
{
83
unsigned
char
b
,
g
,
r
,
a
;
84
}
FILE_BGRA
;
85
86
typedef
struct
tagFILE_RGB
{
87
unsigned
char
r
,
g
,
b
;
88
}
FILE_RGB
;
89
90
typedef
struct
tagFILE_BGR
{
91
unsigned
char
b
,
g
,
r
;
92
}
FILE_BGR
;
93
94
#ifdef _WIN32
95
#pragma pack(pop)
96
#else
97
#pragma pack()
98
#endif
// _WIN32
99
100
// ==========================================================
101
// Utility functions
102
// ==========================================================
103
104
#ifndef _WIN32
105
inline
char
*
106
i2a
(
unsigned
i,
char
*a,
unsigned
r) {
107
if
(i/r > 0) a =
i2a
(i/r,a,r);
108
*a =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
[i%r];
109
return
a+1;
110
}
111
120
inline
char
*
121
_itoa
(
int
i,
char
*a,
int
r) {
122
r = ((r < 2) || (r > 36)) ? 10 : r;
123
if
(i < 0) {
124
*a =
'-'
;
125
*
i2a
(-i, a+1, r) = 0;
126
}
127
else
*
i2a
(i, a, r) = 0;
128
return
a;
129
}
130
131
#endif
// !_WIN32
132
133
inline
unsigned
char
134
HINIBBLE
(
unsigned
char
byte
) {
135
return
byte
& 0xF0;
136
}
137
138
inline
unsigned
char
139
LOWNIBBLE
(
unsigned
char
byte
) {
140
return
byte
& 0x0F;
141
}
142
143
inline
int
144
CalculateUsedBits
(
int
bits) {
145
int
bit_count = 0;
146
unsigned
bit = 1;
147
148
for
(
unsigned
i = 0; i < 32; i++) {
149
if
((bits & bit) == bit) {
150
bit_count++;
151
}
152
153
bit <<= 1;
154
}
155
156
return
bit_count;
157
}
158
159
inline
int
160
CalculateLine
(
int
width,
int
bitdepth) {
161
return
((width * bitdepth) + 7) / 8;
162
}
163
164
inline
int
165
CalculatePitch
(
int
line) {
166
return
line + 3 & ~3;
167
}
168
169
inline
int
170
CalculateUsedPaletteEntries
(
int
bit_count) {
171
if
((bit_count >= 1) && (bit_count <= 8))
172
return
1 << bit_count;
173
174
return
0;
175
}
176
177
inline
unsigned
char
*
178
CalculateScanLine
(
unsigned
char
*bits,
unsigned
pitch,
int
scanline) {
179
return
(bits + (pitch * scanline));
180
}
181
182
inline
void
183
ReplaceExtension
(
char
*result,
const
char
*filename,
const
char
*extension) {
184
for
(
size_t
i = strlen(filename) - 1; i > 0; --i) {
185
if
(filename[i] ==
'.'
) {
186
memcpy(result, filename, i);
187
result[i] =
'.'
;
188
memcpy(result + i + 1, extension, strlen(extension) + 1);
189
return
;
190
}
191
}
192
193
memcpy(result, filename, strlen(filename));
194
result[strlen(filename)] =
'.'
;
195
memcpy(result + strlen(filename) + 1, extension, strlen(extension) + 1);
196
}
197
198
// ==========================================================
199
// Big Endian / Little Endian utility functions
200
// ==========================================================
201
202
inline
void
203
SwapShort
(WORD *sp) {
204
BYTE *cp = (BYTE *)sp, t = cp[0]; cp[0] = cp[1]; cp[1] = t;
205
}
206
207
inline
void
208
SwapLong
(DWORD *lp) {
209
BYTE *cp = (BYTE *)lp, t = cp[0]; cp[0] = cp[3]; cp[3] = t;
210
t = cp[1]; cp[1] = cp[2]; cp[2] = t;
211
}
212
213
// ==========================================================
214
// Greyscale conversion
215
// ==========================================================
216
217
#define GREY(r, g, b) (BYTE)(((WORD)r * 77 + (WORD)g * 150 + (WORD)b * 29) >> 8)
// .299R + .587G + .114B
218
/*
219
#define GREY(r, g, b) (BYTE)(((WORD)r * 169 + (WORD)g * 256 + (WORD)b * 87) >> 9) // .33R + 0.5G + .17B
220
*/
221
222
// ==========================================================
223
// Template utility functions
224
// ==========================================================
225
227
template
<
class
T> T
MAX
(T a, T b) {
228
return
(a > b) ? a: b;
229
}
230
232
template
<
class
T> T
MIN
(T a, T b) {
233
return
(a < b) ? a: b;
234
}
235
237
template
<
class
T>
void
INPLACESWAP
(T& a, T& b) {
238
a ^= b; b ^= a; a ^= b;
239
}
240
248
template
<
class
T>
void
249
MAXMIN
(
const
T* L,
long
n, T& max, T& min) {
250
long
i1, i2, i, j;
251
T x1, x2;
252
long
k1, k2;
253
254
i1 = 0; i2 = 0; min = L[0]; max = L[0]; j = 0;
255
if
((n % 2) != 0) j = 1;
256
for
(i = j; i < n; i+= 2) {
257
k1 = i; k2 = i+1;
258
x1 = L[k1]; x2 = L[k2];
259
if
(x1 > x2) {
260
k1 = k2; k2 = i;
261
x1 = x2; x2 = L[k2];
262
}
263
if
(x1 < min) {
264
min = x1; i1 = k1;
265
}
266
if
(x2 > max) {
267
max = x2; i2 = k2;
268
}
269
}
270
}
271
272
// ==========================================================
273
// Generic error messages
274
// ==========================================================
275
276
static
const
char
*
FI_MSG_ERROR_MEMORY
=
"Not enough memory"
;
277
278
279
#endif
// UTILITIES_H
280
MAXMIN
void MAXMIN(const T *L, long n, T &max, T &min)
Definition
Utilities.h:249
SwapLong
void SwapLong(DWORD *lp)
Definition
Utilities.h:208
_itoa
char * _itoa(int i, char *a, int r)
Definition
Utilities.h:121
FI_MSG_ERROR_MEMORY
static const char * FI_MSG_ERROR_MEMORY
Definition
Utilities.h:276
CalculatePitch
int CalculatePitch(int line)
Definition
Utilities.h:165
FILE_BGRA
struct tagFILE_BGRA FILE_BGRA
FILE_BGR
struct tagFILE_BGR FILE_BGR
CalculateUsedBits
int CalculateUsedBits(int bits)
Definition
Utilities.h:144
HINIBBLE
unsigned char HINIBBLE(unsigned char byte)
Definition
Utilities.h:134
i2a
char * i2a(unsigned i, char *a, unsigned r)
Definition
Utilities.h:106
FreeImage_Aligned_Malloc
void * FreeImage_Aligned_Malloc(size_t amount, size_t alignment)
FreeImage_Aligned_Free
void FreeImage_Aligned_Free(void *mem)
MIN
T MIN(T a, T b)
Min function.
Definition
Utilities.h:232
SwapShort
void SwapShort(WORD *sp)
Definition
Utilities.h:203
FILE_RGBA
struct tagFILE_RGBA FILE_RGBA
CalculateLine
int CalculateLine(int width, int bitdepth)
Definition
Utilities.h:160
CalculateUsedPaletteEntries
int CalculateUsedPaletteEntries(int bit_count)
Definition
Utilities.h:170
CalculateScanLine
unsigned char * CalculateScanLine(unsigned char *bits, unsigned pitch, int scanline)
Definition
Utilities.h:178
FILE_RGB
struct tagFILE_RGB FILE_RGB
INPLACESWAP
void INPLACESWAP(T &a, T &b)
INPLACESWAP adopted from codeguru.com.
Definition
Utilities.h:237
MAX
T MAX(T a, T b)
Max function.
Definition
Utilities.h:227
LOWNIBBLE
unsigned char LOWNIBBLE(unsigned char byte)
Definition
Utilities.h:139
ReplaceExtension
void ReplaceExtension(char *result, const char *filename, const char *extension)
Definition
Utilities.h:183
tagFILE_BGRA
Definition
Utilities.h:82
tagFILE_BGRA::g
unsigned char g
Definition
Utilities.h:83
tagFILE_BGRA::r
unsigned char r
Definition
Utilities.h:83
tagFILE_BGRA::a
unsigned char a
Definition
Utilities.h:83
tagFILE_BGRA::b
unsigned char b
Definition
Utilities.h:83
tagFILE_BGR
Definition
Utilities.h:90
tagFILE_BGR::r
unsigned char r
Definition
Utilities.h:91
tagFILE_BGR::b
unsigned char b
Definition
Utilities.h:91
tagFILE_BGR::g
unsigned char g
Definition
Utilities.h:91
tagFILE_RGBA
Definition
Utilities.h:78
tagFILE_RGBA::g
unsigned char g
Definition
Utilities.h:79
tagFILE_RGBA::r
unsigned char r
Definition
Utilities.h:79
tagFILE_RGBA::a
unsigned char a
Definition
Utilities.h:79
tagFILE_RGBA::b
unsigned char b
Definition
Utilities.h:79
tagFILE_RGB
Definition
Utilities.h:86
tagFILE_RGB::b
unsigned char b
Definition
Utilities.h:87
tagFILE_RGB::r
unsigned char r
Definition
Utilities.h:87
tagFILE_RGB::g
unsigned char g
Definition
Utilities.h:87
src
Utilities.h
Generated by
1.17.0