PGF Console 6.21.2
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
62void* FreeImage_Aligned_Malloc(size_t amount, size_t alignment);
63void 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
78typedef struct tagFILE_RGBA {
79 unsigned char r,g,b,a;
81
82typedef struct tagFILE_BGRA {
83 unsigned char b,g,r,a;
85
86typedef struct tagFILE_RGB {
87 unsigned char r,g,b;
89
90typedef struct tagFILE_BGR {
91 unsigned char b,g,r;
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
105inline char*
106i2a(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
120inline 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
133inline unsigned char
134HINIBBLE (unsigned char byte) {
135 return byte & 0xF0;
136}
137
138inline unsigned char
139LOWNIBBLE (unsigned char byte) {
140 return byte & 0x0F;
141}
142
143inline int
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
159inline int
160CalculateLine(int width, int bitdepth) {
161 return ((width * bitdepth) + 7) / 8;
162}
163
164inline int
165CalculatePitch(int line) {
166 return line + 3 & ~3;
167}
168
169inline int
171 if ((bit_count >= 1) && (bit_count <= 8))
172 return 1 << bit_count;
173
174 return 0;
175}
176
177inline unsigned char *
178CalculateScanLine(unsigned char *bits, unsigned pitch, int scanline) {
179 return (bits + (pitch * scanline));
180}
181
182inline void
183ReplaceExtension(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
202inline void
203SwapShort(WORD *sp) {
204 BYTE *cp = (BYTE *)sp, t = cp[0]; cp[0] = cp[1]; cp[1] = t;
205}
206
207inline void
208SwapLong(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
227template <class T> T MAX(T a, T b) {
228 return (a > b) ? a: b;
229}
230
232template <class T> T MIN(T a, T b) {
233 return (a < b) ? a: b;
234}
235
237template <class T> void INPLACESWAP(T& a, T& b) {
238 a ^= b; b ^= a; a ^= b;
239}
240
248template <class T> void
249MAXMIN(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
276static const char *FI_MSG_ERROR_MEMORY = "Not enough memory";
277
278
279#endif // UTILITIES_H
280
void MAXMIN(const T *L, long n, T &max, T &min)
Definition Utilities.h:249
void SwapLong(DWORD *lp)
Definition Utilities.h:208
char * _itoa(int i, char *a, int r)
Definition Utilities.h:121
static const char * FI_MSG_ERROR_MEMORY
Definition Utilities.h:276
int CalculatePitch(int line)
Definition Utilities.h:165
struct tagFILE_BGRA FILE_BGRA
struct tagFILE_BGR FILE_BGR
int CalculateUsedBits(int bits)
Definition Utilities.h:144
unsigned char HINIBBLE(unsigned char byte)
Definition Utilities.h:134
char * i2a(unsigned i, char *a, unsigned r)
Definition Utilities.h:106
void * FreeImage_Aligned_Malloc(size_t amount, size_t alignment)
void FreeImage_Aligned_Free(void *mem)
T MIN(T a, T b)
Min function.
Definition Utilities.h:232
void SwapShort(WORD *sp)
Definition Utilities.h:203
struct tagFILE_RGBA FILE_RGBA
int CalculateLine(int width, int bitdepth)
Definition Utilities.h:160
int CalculateUsedPaletteEntries(int bit_count)
Definition Utilities.h:170
unsigned char * CalculateScanLine(unsigned char *bits, unsigned pitch, int scanline)
Definition Utilities.h:178
struct tagFILE_RGB FILE_RGB
void INPLACESWAP(T &a, T &b)
INPLACESWAP adopted from codeguru.com.
Definition Utilities.h:237
T MAX(T a, T b)
Max function.
Definition Utilities.h:227
unsigned char LOWNIBBLE(unsigned char byte)
Definition Utilities.h:139
void ReplaceExtension(char *result, const char *filename, const char *extension)
Definition Utilities.h:183
unsigned char g
Definition Utilities.h:83
unsigned char r
Definition Utilities.h:83
unsigned char a
Definition Utilities.h:83
unsigned char b
Definition Utilities.h:83
unsigned char r
Definition Utilities.h:91
unsigned char b
Definition Utilities.h:91
unsigned char g
Definition Utilities.h:91
unsigned char g
Definition Utilities.h:79
unsigned char r
Definition Utilities.h:79
unsigned char a
Definition Utilities.h:79
unsigned char b
Definition Utilities.h:79
unsigned char b
Definition Utilities.h:87
unsigned char r
Definition Utilities.h:87
unsigned char g
Definition Utilities.h:87