]> git.saurik.com Git - wxWidgets.git/blob - src/tiff/tif_win32.c
cleanup of handlers
[wxWidgets.git] / src / tiff / tif_win32.c
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27 /*
28 * TIFF Library Win32-specific Routines. Adapted from tif_unix.c 4/5/95 by
29 * Scott Wagner (wagner@itek.com), Itek Graphix, Rochester, NY USA
30 */
31 #include <windows.h>
32 #include "tiffiop.h"
33
34 static tsize_t
35 _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
36 {
37 DWORD dwSizeRead;
38 if (!ReadFile(fd, buf, size, &dwSizeRead, NULL))
39 return(0);
40 return ((tsize_t) dwSizeRead);
41 }
42
43 static tsize_t
44 _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
45 {
46 DWORD dwSizeWritten;
47 if (!WriteFile(fd, buf, size, &dwSizeWritten, NULL))
48 return(0);
49 return ((tsize_t) dwSizeWritten);
50 }
51
52 static toff_t
53 _tiffSeekProc(thandle_t fd, toff_t off, int whence)
54 {
55 DWORD dwMoveMethod, dwMoveHigh;
56
57 /* we use this as a special code, so avoid accepting it */
58 if( off == 0xFFFFFFFF )
59 return 0xFFFFFFFF;
60
61 switch(whence)
62 {
63 case SEEK_SET:
64 dwMoveMethod = FILE_BEGIN;
65 break;
66 case SEEK_CUR:
67 dwMoveMethod = FILE_CURRENT;
68 break;
69 case SEEK_END:
70 dwMoveMethod = FILE_END;
71 break;
72 default:
73 dwMoveMethod = FILE_BEGIN;
74 break;
75 }
76 dwMoveHigh = 0;
77 return ((toff_t)SetFilePointer(fd, (LONG) off, (PLONG)&dwMoveHigh,
78 dwMoveMethod));
79 }
80
81 static int
82 _tiffCloseProc(thandle_t fd)
83 {
84 return (CloseHandle(fd) ? 0 : -1);
85 }
86
87 static toff_t
88 _tiffSizeProc(thandle_t fd)
89 {
90 return ((toff_t)GetFileSize(fd, NULL));
91 }
92
93 #ifdef __BORLANDC__
94 #pragma argsused
95 #endif
96 static int
97 _tiffDummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
98 {
99 return (0);
100 }
101
102 /*
103 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
104 *
105 * Windows uses both a handle and a pointer for file mapping,
106 * but according to the SDK documentation and Richter's book
107 * "Advanced Windows Programming" it is safe to free the handle
108 * after obtaining the file mapping pointer
109 *
110 * This removes a nasty OS dependency and cures a problem
111 * with Visual C++ 5.0
112 */
113 static int
114 _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
115 {
116 toff_t size;
117 HANDLE hMapFile;
118
119 if ((size = _tiffSizeProc(fd)) == 0xFFFFFFFF)
120 return (0);
121 hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, size, NULL);
122 if (hMapFile == NULL)
123 return (0);
124 *pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
125 CloseHandle(hMapFile);
126 if (*pbase == NULL)
127 return (0);
128 *psize = size;
129 return(1);
130 }
131
132 #ifdef __BORLANDC__
133 #pragma argsused
134 #endif
135 static void
136 _tiffDummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
137 {
138 }
139
140 static void
141 _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
142 {
143 UnmapViewOfFile(base);
144 }
145
146 /*
147 * Open a TIFF file descriptor for read/writing.
148 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
149 * string, which forces the file to be opened unmapped.
150 */
151 TIFF*
152 TIFFFdOpen(int ifd, const char* name, const char* mode)
153 {
154 TIFF* tif;
155 BOOL fSuppressMap = (mode[1] == 'u' || (mode[1]!=0 && mode[2] == 'u'));
156
157 tif = TIFFClientOpen(name, mode,
158 (thandle_t)ifd,
159 _tiffReadProc, _tiffWriteProc,
160 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
161 fSuppressMap ? _tiffDummyMapProc : _tiffMapProc,
162 fSuppressMap ? _tiffDummyUnmapProc : _tiffUnmapProc);
163 if (tif)
164 tif->tif_fd = ifd;
165 return (tif);
166 }
167
168 /*
169 * Open a TIFF file for read/writing.
170 */
171 TIFF*
172 TIFFOpen(const char* name, const char* mode)
173 {
174 static const char module[] = "TIFFOpen";
175 thandle_t fd;
176 int m;
177 DWORD dwMode;
178
179 m = _TIFFgetMode(mode, module);
180
181 switch(m)
182 {
183 case O_RDONLY:
184 dwMode = OPEN_EXISTING;
185 break;
186 case O_RDWR:
187 dwMode = OPEN_ALWAYS;
188 break;
189 case O_RDWR|O_CREAT:
190 dwMode = OPEN_ALWAYS;
191 break;
192 case O_RDWR|O_TRUNC:
193 dwMode = CREATE_ALWAYS;
194 break;
195 case O_RDWR|O_CREAT|O_TRUNC:
196 dwMode = CREATE_ALWAYS;
197 break;
198 default:
199 return ((TIFF*)0);
200 }
201 fd = (thandle_t)CreateFile(name, (m == O_RDONLY) ? GENERIC_READ :
202 (GENERIC_READ | GENERIC_WRITE), FILE_SHARE_READ, NULL, dwMode,
203 (m == O_RDONLY) ? FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL, NULL);
204 if (fd == INVALID_HANDLE_VALUE) {
205 TIFFError(module, "%s: Cannot open", name);
206 return ((TIFF *)0);
207 }
208 return (TIFFFdOpen((int)fd, name, mode));
209 }
210
211 tdata_t
212 _TIFFmalloc(tsize_t s)
213 {
214 return ((tdata_t)GlobalAlloc(GMEM_FIXED, s));
215 }
216
217 void
218 _TIFFfree(tdata_t p)
219 {
220 GlobalFree(p);
221 return;
222 }
223
224 tdata_t
225 _TIFFrealloc(tdata_t p, tsize_t s)
226 {
227 void* pvTmp;
228 tsize_t old=GlobalSize(p);
229 if (old>=s)
230 {
231 if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
232 CopyMemory(pvTmp, p, s);
233 GlobalFree(p);
234 }
235 }
236 else
237 {
238 if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
239 CopyMemory(pvTmp, p, old);
240 GlobalFree(p);
241 }
242 }
243 return ((tdata_t)pvTmp);
244 }
245
246 void
247 _TIFFmemset(void* p, int v, tsize_t c)
248 {
249 FillMemory(p, c, (BYTE)v);
250 }
251
252 void
253 _TIFFmemcpy(void* d, const tdata_t s, tsize_t c)
254 {
255 CopyMemory(d, s, c);
256 }
257
258 int
259 _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
260 {
261 register const BYTE *pb1 = (const BYTE *) p1;
262 register const BYTE *pb2 = (const BYTE *) p2;
263 register DWORD dwTmp = c;
264 register int iTmp;
265 for (iTmp = 0; dwTmp-- && !iTmp; iTmp = (int)*pb1++ - (int)*pb2++)
266 ;
267 return (iTmp);
268 }
269
270 static void
271 Win32WarningHandler(const char* module, const char* fmt, va_list ap)
272 {
273 #ifndef TIF_PLATFORM_CONSOLE
274 LPTSTR szTitle;
275 LPTSTR szTmp;
276 LPCTSTR szTitleText = "%s Warning";
277 LPCTSTR szDefaultModule = "TIFFLIB";
278 szTmp = (module == NULL) ? (LPTSTR)szDefaultModule : (LPTSTR)module;
279 if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, (lstrlen(szTmp) +
280 lstrlen(szTitleText) + lstrlen(fmt) + 128)*sizeof(TCHAR))) == NULL)
281 return;
282 wsprintf(szTitle, szTitleText, szTmp);
283 szTmp = szTitle + (lstrlen(szTitle)+2)*sizeof(TCHAR);
284 wvsprintf(szTmp, fmt, ap);
285 MessageBox(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
286 LocalFree(szTitle);
287 return;
288 #else
289 if (module != NULL)
290 fprintf(stderr, "%s: ", module);
291 fprintf(stderr, "Warning, ");
292 vfprintf(stderr, fmt, ap);
293 fprintf(stderr, ".\n");
294 #endif
295 }
296 TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
297
298 static void
299 Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
300 {
301 #ifndef TIF_PLATFORM_CONSOLE
302 LPTSTR szTitle;
303 LPTSTR szTmp;
304 LPCTSTR szTitleText = "%s Error";
305 LPCTSTR szDefaultModule = "TIFFLIB";
306 szTmp = (module == NULL) ? (LPTSTR)szDefaultModule : (LPTSTR)module;
307 if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, (lstrlen(szTmp) +
308 lstrlen(szTitleText) + lstrlen(fmt) + 128)*sizeof(TCHAR))) == NULL)
309 return;
310 wsprintf(szTitle, szTitleText, szTmp);
311 szTmp = szTitle + (lstrlen(szTitle)+2)*sizeof(TCHAR);
312 wvsprintf(szTmp, fmt, ap);
313 MessageBox(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
314 LocalFree(szTitle);
315 return;
316 #else
317 if (module != NULL)
318 fprintf(stderr, "%s: ", module);
319 vfprintf(stderr, fmt, ap);
320 fprintf(stderr, ".\n");
321 #endif
322 }
323 TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;