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