/src/FreeImage/Source/FreeImage/FreeImage.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 226 lines · 132 code · 62 blank · 32 comment · 20 complexity · 178ddacd044a507228f1c8f44c1b0c5e MD5 · raw file

  1. // ==========================================================
  2. // FreeImage implementation
  3. //
  4. // Design and implementation by
  5. // - Floris van den Berg (flvdberg@wxs.nl)
  6. // - Hervé Drolon (drolon@infonie.fr)
  7. // - Karl-Heinz Bussian (khbussian@moss.de)
  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. #ifdef _WIN32
  24. #include <windows.h>
  25. #endif
  26. #include "FreeImage.h"
  27. #include "Utilities.h"
  28. //----------------------------------------------------------------------
  29. static const char *s_copyright = "This program uses FreeImage, a free, open source image library supporting all common bitmap formats. See http://freeimage.sourceforge.net for details";
  30. //----------------------------------------------------------------------
  31. #if defined(_WIN32) && !defined(__MINGW32__)
  32. #ifndef FREEIMAGE_LIB
  33. BOOL APIENTRY
  34. DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
  35. switch (ul_reason_for_call) {
  36. case DLL_PROCESS_ATTACH :
  37. FreeImage_Initialise(FALSE);
  38. break;
  39. case DLL_PROCESS_DETACH :
  40. FreeImage_DeInitialise();
  41. break;
  42. case DLL_THREAD_ATTACH :
  43. case DLL_THREAD_DETACH :
  44. break;
  45. }
  46. return TRUE;
  47. }
  48. #endif // FREEIMAGE_LIB
  49. #else // !_WIN32
  50. #ifndef FREEIMAGE_LIB
  51. void FreeImage_SO_Initialise() __attribute__((constructor));
  52. void FreeImage_SO_DeInitialise() __attribute__((destructor));
  53. void FreeImage_SO_Initialise() {
  54. FreeImage_Initialise(FALSE);
  55. }
  56. void FreeImage_SO_DeInitialise() {
  57. FreeImage_DeInitialise();
  58. }
  59. #endif // FREEIMAGE_LIB
  60. #endif // _WIN32
  61. //----------------------------------------------------------------------
  62. const char * DLL_CALLCONV
  63. FreeImage_GetVersion() {
  64. static char s_version[16];
  65. sprintf(s_version, "%d.%d.%d", FREEIMAGE_MAJOR_VERSION, FREEIMAGE_MINOR_VERSION, FREEIMAGE_RELEASE_SERIAL);
  66. return s_version;
  67. }
  68. const char * DLL_CALLCONV
  69. FreeImage_GetCopyrightMessage() {
  70. return s_copyright;
  71. }
  72. //----------------------------------------------------------------------
  73. BOOL DLL_CALLCONV
  74. FreeImage_IsLittleEndian() {
  75. union {
  76. DWORD i;
  77. BYTE c[4];
  78. } u;
  79. u.i = 1;
  80. return (u.c[0] != 0);
  81. }
  82. //----------------------------------------------------------------------
  83. static FreeImage_OutputMessageFunction freeimage_outputmessage_proc = NULL;
  84. static FreeImage_OutputMessageFunctionStdCall freeimage_outputmessagestdcall_proc = NULL;
  85. void DLL_CALLCONV
  86. FreeImage_SetOutputMessage(FreeImage_OutputMessageFunction omf) {
  87. freeimage_outputmessage_proc = omf;
  88. }
  89. void DLL_CALLCONV
  90. FreeImage_SetOutputMessageStdCall(FreeImage_OutputMessageFunctionStdCall omf) {
  91. freeimage_outputmessagestdcall_proc = omf;
  92. }
  93. void DLL_CALLCONV
  94. FreeImage_OutputMessageProc(int fif, const char *fmt, ...) {
  95. const int MSG_SIZE = 512; // 512 bytes should be more than enough for a short message
  96. if ((fmt != NULL) && ((freeimage_outputmessage_proc != NULL) || (freeimage_outputmessagestdcall_proc != NULL))) {
  97. char message[MSG_SIZE];
  98. memset(message, 0, MSG_SIZE);
  99. // initialize the optional parameter list
  100. va_list arg;
  101. va_start(arg, fmt);
  102. // check the length of the format string
  103. int str_length = (int)( (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt) );
  104. // parse the format string and put the result in 'message'
  105. for (int i = 0, j = 0; i < str_length; ++i) {
  106. if (fmt[i] == '%') {
  107. if (i + 1 < str_length) {
  108. switch(tolower(fmt[i + 1])) {
  109. case '%' :
  110. message[j++] = '%';
  111. break;
  112. case 'o' : // octal numbers
  113. {
  114. char tmp[16];
  115. _itoa(va_arg(arg, int), tmp, 8);
  116. strcat(message, tmp);
  117. j += (int)strlen(tmp);
  118. ++i;
  119. break;
  120. }
  121. case 'i' : // decimal numbers
  122. case 'd' :
  123. {
  124. char tmp[16];
  125. _itoa(va_arg(arg, int), tmp, 10);
  126. strcat(message, tmp);
  127. j += (int)strlen(tmp);
  128. ++i;
  129. break;
  130. }
  131. case 'x' : // hexadecimal numbers
  132. {
  133. char tmp[16];
  134. _itoa(va_arg(arg, int), tmp, 16);
  135. strcat(message, tmp);
  136. j += (int)strlen(tmp);
  137. ++i;
  138. break;
  139. }
  140. case 's' : // strings
  141. {
  142. char *tmp = va_arg(arg, char*);
  143. strcat(message, tmp);
  144. j += (int)strlen(tmp);
  145. ++i;
  146. break;
  147. }
  148. };
  149. } else {
  150. message[j++] = fmt[i];
  151. }
  152. } else {
  153. message[j++] = fmt[i];
  154. };
  155. }
  156. // deinitialize the optional parameter list
  157. va_end(arg);
  158. // output the message to the user program
  159. if (freeimage_outputmessage_proc != NULL)
  160. freeimage_outputmessage_proc((FREE_IMAGE_FORMAT)fif, message);
  161. if (freeimage_outputmessagestdcall_proc != NULL)
  162. freeimage_outputmessagestdcall_proc((FREE_IMAGE_FORMAT)fif, message);
  163. }
  164. }