/src/FreeImage/Source/FreeImage/FreeImage.cpp
C++ | 226 lines | 132 code | 62 blank | 32 comment | 19 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 24 25#ifdef _WIN32 26#include <windows.h> 27#endif 28 29#include "FreeImage.h" 30#include "Utilities.h" 31 32//---------------------------------------------------------------------- 33 34static 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"; 35 36//---------------------------------------------------------------------- 37 38#if defined(_WIN32) && !defined(__MINGW32__) 39#ifndef FREEIMAGE_LIB 40 41BOOL APIENTRY 42DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { 43 switch (ul_reason_for_call) { 44 case DLL_PROCESS_ATTACH : 45 FreeImage_Initialise(FALSE); 46 break; 47 48 case DLL_PROCESS_DETACH : 49 FreeImage_DeInitialise(); 50 break; 51 52 case DLL_THREAD_ATTACH : 53 case DLL_THREAD_DETACH : 54 break; 55 } 56 57 return TRUE; 58} 59 60#endif // FREEIMAGE_LIB 61 62#else // !_WIN32 63#ifndef FREEIMAGE_LIB 64 65void FreeImage_SO_Initialise() __attribute__((constructor)); 66void FreeImage_SO_DeInitialise() __attribute__((destructor)); 67 68void FreeImage_SO_Initialise() { 69 FreeImage_Initialise(FALSE); 70} 71 72void FreeImage_SO_DeInitialise() { 73 FreeImage_DeInitialise(); 74} 75#endif // FREEIMAGE_LIB 76 77#endif // _WIN32 78 79//---------------------------------------------------------------------- 80 81const char * DLL_CALLCONV 82FreeImage_GetVersion() { 83 static char s_version[16]; 84 sprintf(s_version, "%d.%d.%d", FREEIMAGE_MAJOR_VERSION, FREEIMAGE_MINOR_VERSION, FREEIMAGE_RELEASE_SERIAL); 85 return s_version; 86} 87 88const char * DLL_CALLCONV 89FreeImage_GetCopyrightMessage() { 90 return s_copyright; 91} 92 93//---------------------------------------------------------------------- 94 95BOOL DLL_CALLCONV 96FreeImage_IsLittleEndian() { 97 union { 98 DWORD i; 99 BYTE c[4]; 100 } u; 101 u.i = 1; 102 return (u.c[0] != 0); 103} 104 105//---------------------------------------------------------------------- 106 107static FreeImage_OutputMessageFunction freeimage_outputmessage_proc = NULL; 108static FreeImage_OutputMessageFunctionStdCall freeimage_outputmessagestdcall_proc = NULL; 109 110void DLL_CALLCONV 111FreeImage_SetOutputMessage(FreeImage_OutputMessageFunction omf) { 112 freeimage_outputmessage_proc = omf; 113} 114 115void DLL_CALLCONV 116FreeImage_SetOutputMessageStdCall(FreeImage_OutputMessageFunctionStdCall omf) { 117 freeimage_outputmessagestdcall_proc = omf; 118} 119 120void DLL_CALLCONV 121FreeImage_OutputMessageProc(int fif, const char *fmt, ...) { 122 const int MSG_SIZE = 512; // 512 bytes should be more than enough for a short message 123 124 if ((fmt != NULL) && ((freeimage_outputmessage_proc != NULL) || (freeimage_outputmessagestdcall_proc != NULL))) { 125 char message[MSG_SIZE]; 126 memset(message, 0, MSG_SIZE); 127 128 // initialize the optional parameter list 129 130 va_list arg; 131 va_start(arg, fmt); 132 133 // check the length of the format string 134 135 int str_length = (int)( (strlen(fmt) > MSG_SIZE) ? MSG_SIZE : strlen(fmt) ); 136 137 // parse the format string and put the result in 'message' 138 139 for (int i = 0, j = 0; i < str_length; ++i) { 140 if (fmt[i] == '%') { 141 if (i + 1 < str_length) { 142 switch(tolower(fmt[i + 1])) { 143 case '%' : 144 message[j++] = '%'; 145 break; 146 147 case 'o' : // octal numbers 148 { 149 char tmp[16]; 150 151 _itoa(va_arg(arg, int), tmp, 8); 152 153 strcat(message, tmp); 154 155 j += (int)strlen(tmp); 156 157 ++i; 158 159 break; 160 } 161 162 case 'i' : // decimal numbers 163 case 'd' : 164 { 165 char tmp[16]; 166 167 _itoa(va_arg(arg, int), tmp, 10); 168 169 strcat(message, tmp); 170 171 j += (int)strlen(tmp); 172 173 ++i; 174 175 break; 176 } 177 178 case 'x' : // hexadecimal numbers 179 { 180 char tmp[16]; 181 182 _itoa(va_arg(arg, int), tmp, 16); 183 184 strcat(message, tmp); 185 186 j += (int)strlen(tmp); 187 188 ++i; 189 190 break; 191 } 192 193 case 's' : // strings 194 { 195 char *tmp = va_arg(arg, char*); 196 197 strcat(message, tmp); 198 199 j += (int)strlen(tmp); 200 201 ++i; 202 203 break; 204 } 205 }; 206 } else { 207 message[j++] = fmt[i]; 208 } 209 } else { 210 message[j++] = fmt[i]; 211 }; 212 } 213 214 // deinitialize the optional parameter list 215 216 va_end(arg); 217 218 // output the message to the user program 219 220 if (freeimage_outputmessage_proc != NULL) 221 freeimage_outputmessage_proc((FREE_IMAGE_FORMAT)fif, message); 222 223 if (freeimage_outputmessagestdcall_proc != NULL) 224 freeimage_outputmessagestdcall_proc((FREE_IMAGE_FORMAT)fif, message); 225 } 226}