/trunk/source/Engine/External/FreeImage/include/Utilities.h

# · C++ Header · 266 lines · 153 code · 45 blank · 68 comment · 18 complexity · ef7cc167473c2646e882e7b8bfc44e58 MD5 · raw 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. #ifndef UTILITIES_H
  24. #define UTILITIES_H
  25. // ==========================================================
  26. // Standard includes used by the library
  27. // ==========================================================
  28. #include <math.h>
  29. #include <stdlib.h>
  30. #include <memory.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <stdarg.h>
  34. #include <ctype.h>
  35. #include <string>
  36. #include <list>
  37. #include <map>
  38. #include <set>
  39. #include <vector>
  40. #include <stack>
  41. #include <sstream>
  42. // ==========================================================
  43. // Bitmap palette and pixels alignment
  44. // ==========================================================
  45. #define FIBITMAP_ALIGNMENT 16 // We will use a 16 bytes alignment boundary
  46. // Memory allocation on a specified alignment boundary
  47. // defined in BitmapAccess.cpp
  48. void* FreeImage_Aligned_Malloc(size_t amount, size_t alignment);
  49. void FreeImage_Aligned_Free(void* mem);
  50. // ==========================================================
  51. // File I/O structs
  52. // ==========================================================
  53. // these structs are for file I/O and should not be confused with similar
  54. // structs in FreeImage.h which are for in-memory bitmap handling
  55. #ifdef WIN32
  56. #pragma pack(push, 1)
  57. #else
  58. #pragma pack(1)
  59. #endif // WIN32
  60. typedef struct tagFILE_RGBA {
  61. unsigned char r,g,b,a;
  62. } FILE_RGBA;
  63. typedef struct tagFILE_BGRA {
  64. unsigned char b,g,r,a;
  65. } FILE_BGRA;
  66. typedef struct tagFILE_RGB {
  67. unsigned char r,g,b;
  68. } FILE_RGB;
  69. typedef struct tagFILE_BGR {
  70. unsigned char b,g,r;
  71. } FILE_BGR;
  72. #ifdef WIN32
  73. #pragma pack(pop)
  74. #else
  75. #pragma pack()
  76. #endif // WIN32
  77. // ==========================================================
  78. // Utility functions
  79. // ==========================================================
  80. #ifndef WIN32
  81. inline char*
  82. i2a(unsigned i, char *a, unsigned r) {
  83. if (i/r > 0) a = i2a(i/r,a,r);
  84. *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
  85. return a+1;
  86. }
  87. /**
  88. Transforms integer i into an ascii string and stores the result in a;
  89. string is encoded in the base indicated by r.
  90. @param i Number to be converted
  91. @param a String result
  92. @param r Base of value; must be in the range 2 - 36
  93. @return Returns a
  94. */
  95. inline char *
  96. _itoa(int i, char *a, int r) {
  97. r = ((r < 2) || (r > 36)) ? 10 : r;
  98. if(i < 0) {
  99. *a = '-';
  100. *i2a(-i, a+1, r) = 0;
  101. }
  102. else *i2a(i, a, r) = 0;
  103. return a;
  104. }
  105. #endif // !WIN32
  106. inline unsigned char
  107. HINIBBLE (unsigned char byte) {
  108. return byte & 0xF0;
  109. }
  110. inline unsigned char
  111. LOWNIBBLE (unsigned char byte) {
  112. return byte & 0x0F;
  113. }
  114. inline int
  115. CalculateUsedBits(int bits) {
  116. int bit_count = 0;
  117. unsigned bit = 1;
  118. for (unsigned i = 0; i < 32; i++) {
  119. if ((bits & bit) == bit) {
  120. bit_count++;
  121. }
  122. bit <<= 1;
  123. }
  124. return bit_count;
  125. }
  126. inline int
  127. CalculateLine(int width, int bitdepth) {
  128. return ((width * bitdepth) + 7) / 8;
  129. }
  130. inline int
  131. CalculatePitch(int line) {
  132. return line + 3 & ~3;
  133. }
  134. inline int
  135. CalculateUsedPaletteEntries(int bit_count) {
  136. if ((bit_count >= 1) && (bit_count <= 8))
  137. return 1 << bit_count;
  138. return 0;
  139. }
  140. inline unsigned char *
  141. CalculateScanLine(unsigned char *bits, unsigned pitch, int scanline) {
  142. return (bits + (pitch * scanline));
  143. }
  144. inline void
  145. ReplaceExtension(char *result, const char *filename, const char *extension) {
  146. for (int i = strlen(filename) - 1; i > 0; --i) {
  147. if (filename[i] == '.') {
  148. memcpy(result, filename, i);
  149. result[i] = '.';
  150. memcpy(result + i + 1, extension, strlen(extension) + 1);
  151. return;
  152. }
  153. }
  154. memcpy(result, filename, strlen(filename));
  155. result[strlen(filename)] = '.';
  156. memcpy(result + strlen(filename) + 1, extension, strlen(extension) + 1);
  157. }
  158. // ==========================================================
  159. // Big Endian / Little Endian utility functions
  160. // ==========================================================
  161. inline void
  162. SwapShort(unsigned short *sp) {
  163. unsigned char *cp = (unsigned char *)sp, t = cp[0]; cp[0] = cp[1]; cp[1] = t;
  164. }
  165. inline void
  166. SwapLong(unsigned long *lp) {
  167. unsigned char *cp = (unsigned char *)lp, t = cp[0]; cp[0] = cp[3]; cp[3] = t;
  168. t = cp[1]; cp[1] = cp[2]; cp[2] = t;
  169. }
  170. // ==========================================================
  171. // Greyscale conversion
  172. // ==========================================================
  173. #define GREY(r, g, b) (BYTE)(((WORD)r * 77 + (WORD)g * 150 + (WORD)b * 29) >> 8) // .299R + .587G + .114B
  174. /*
  175. #define GREY(r, g, b) (BYTE)(((WORD)r * 169 + (WORD)g * 256 + (WORD)b * 87) >> 9) // .33R + 0.5G + .17B
  176. */
  177. // ==========================================================
  178. // Template utility functions
  179. // ==========================================================
  180. /// Max function
  181. template <class T> T MAX(T a, T b) {
  182. return (a > b) ? a: b;
  183. }
  184. /// Min function
  185. template <class T> T MIN(T a, T b) {
  186. return (a < b) ? a: b;
  187. }
  188. /// INPLACESWAP adopted from codeguru.com
  189. template <class T> void INPLACESWAP(T& a, T& b) {
  190. a ^= b; b ^= a; a ^= b;
  191. }
  192. /** This procedure computes minimum min and maximum max
  193. of n numbers using only (3n/2) - 2 comparisons.
  194. min = L[i1] and max = L[i2].
  195. ref: Aho A.V., Hopcroft J.E., Ullman J.D.,
  196. The design and analysis of computer algorithms,
  197. Addison-Wesley, Reading, 1974.
  198. */
  199. template <class T> void
  200. MAXMIN(const T* L, long n, T& max, T& min) {
  201. long i1, i2, i, j;
  202. T x1, x2;
  203. long k1, k2;
  204. i1 = 0; i2 = 0; min = L[0]; max = L[0]; j = 0;
  205. if((n % 2) != 0) j = 1;
  206. for(i = j; i < n; i+= 2) {
  207. k1 = i; k2 = i+1;
  208. x1 = L[k1]; x2 = L[k2];
  209. if(x1 > x2) {
  210. k1 = k2; k2 = i;
  211. x1 = x2; x2 = L[k2];
  212. }
  213. if(x1 < min) {
  214. min = x1; i1 = k1;
  215. }
  216. if(x2 > max) {
  217. max = x2; i2 = k2;
  218. }
  219. }
  220. }
  221. #endif // UTILITIES_H