]> git.saurik.com Git - wxWidgets.git/blob - src/tiff/tif_win32.c
rtti api mods added
[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;
56 switch(whence)
57 {
58 case 0:
59 dwMoveMethod = FILE_BEGIN;
60 break;
61 case 1:
62 dwMoveMethod = FILE_CURRENT;
63 break;
64 case 2:
65 dwMoveMethod = FILE_END;
66 break;
67 default:
68 dwMoveMethod = FILE_BEGIN;
69 break;
70 }
71 return ((toff_t)SetFilePointer(fd, off, NULL, dwMoveMethod));
72 }
73
74 static int
75 _tiffCloseProc(thandle_t fd)
76 {
77 return (CloseHandle(fd) ? 0 : -1);
78 }
79
80 static toff_t
81 _tiffSizeProc(thandle_t fd)
82 {
83 return ((toff_t)GetFileSize(fd, NULL));
84 }
85
86 static int
87 _tiffDummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
88 {
89 return (0);
90 }
91
92 /*
93 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
94 *
95 * Windows uses both a handle and a pointer for file mapping,
96 * but according to the SDK documentation and Richter's book
97 * "Advanced Windows Programming" it is safe to free the handle
98 * after obtaining the file mapping pointer
99 *
100 * This removes a nasty OS dependency and cures a problem
101 * with Visual C++ 5.0
102 */
103 static int
104 _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
105 {
106 toff_t size;
107 HANDLE hMapFile;
108
109 if ((size = _tiffSizeProc(fd)) == (toff_t)-1)
110 return (0);
111 hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, size, NULL);
112 if (hMapFile == NULL)
113 return (0);
114 *pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
115 CloseHandle(hMapFile);
116 if (*pbase == NULL)
117 return (0);
118 *psize = size;
119 return(1);
120 }
121
122 static void
123 _tiffDummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
124 {
125 }
126
127 static void
128 _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
129 {
130 UnmapViewOfFile(base);
131 }
132
133 /*
134 * Open a TIFF file descriptor for read/writing.
135 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
136 * string, which forces the file to be opened unmapped.
137 */
138 TIFF*
139 TIFFFdOpen(int ifd, const char* name, const char* mode)
140 {
141 TIFF* tif;
142 BOOL fSuppressMap = (mode[1] == 'u' || mode[2] == 'u');
143
144 tif = TIFFClientOpen(name, mode,
145 (thandle_t)ifd,
146 _tiffReadProc, _tiffWriteProc,
147 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
148 fSuppressMap ? _tiffDummyMapProc : _tiffMapProc,
149 fSuppressMap ? _tiffDummyUnmapProc : _tiffUnmapProc);
150 if (tif)
151 tif->tif_fd = ifd;
152 return (tif);
153 }
154
155 /*
156 * Open a TIFF file for read/writing.
157 */
158 TIFF*
159 TIFFOpen(const char* name, const char* mode)
160 {
161 static const char module[] = "TIFFOpen";
162 thandle_t fd;
163 int m;
164 DWORD dwMode;
165
166 m = _TIFFgetMode(mode, module);
167
168 switch(m)
169 {
170 case O_RDONLY:
171 dwMode = OPEN_EXISTING;
172 break;
173 case O_RDWR:
174 dwMode = OPEN_ALWAYS;
175 break;
176 case O_RDWR|O_CREAT:
177 dwMode = OPEN_ALWAYS;
178 break;
179 case O_RDWR|O_TRUNC:
180 dwMode = CREATE_ALWAYS;
181 break;
182 case O_RDWR|O_CREAT|O_TRUNC:
183 dwMode = CREATE_ALWAYS;
184 break;
185 default:
186 return ((TIFF*)0);
187 }
188 fd = (thandle_t)CreateFile(name, (m == O_RDONLY) ? GENERIC_READ :
189 (GENERIC_READ | GENERIC_WRITE), FILE_SHARE_READ, NULL, dwMode,
190 (m == O_RDONLY) ? FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL, NULL);
191 if (fd == INVALID_HANDLE_VALUE) {
192 TIFFError(module, "%s: Cannot open", name);
193 return ((TIFF *)0);
194 }
195 return (TIFFFdOpen((int)fd, name, mode));
196 }
197
198 tdata_t
199 _TIFFmalloc(tsize_t s)
200 {
201 return ((tdata_t)GlobalAlloc(GMEM_FIXED, s));
202 }
203
204 void
205 _TIFFfree(tdata_t p)
206 {
207 GlobalFree(p);
208 return;
209 }
210
211 tdata_t
212 _TIFFrealloc(tdata_t p, tsize_t s)
213 {
214 void* pvTmp;
215 if ((pvTmp = GlobalReAlloc(p, s, 0)) == NULL) {
216 if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
217 CopyMemory(pvTmp, p, GlobalSize(p));
218 GlobalFree(p);
219 }
220 }
221 return ((tdata_t)pvTmp);
222 }
223
224 void
225 _TIFFmemset(void* p, int v, tsize_t c)
226 {
227 FillMemory(p, c, (BYTE)v);
228 }
229
230 void
231 _TIFFmemcpy(void* d, const tdata_t s, tsize_t c)
232 {
233 CopyMemory(d, s, c);
234 }
235
236 int
237 _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
238 {
239 register const BYTE *pb1 = p1;
240 register const BYTE *pb2 = p2;
241 register DWORD dwTmp = c;
242 register int iTmp;
243 for (iTmp = 0; dwTmp-- && !iTmp; iTmp = (int)*pb1++ - (int)*pb2++)
244 ;
245 return (iTmp);
246 }
247
248 static void
249 Win32WarningHandler(const char* module, const char* fmt, va_list ap)
250 {
251 #ifndef TIF_PLATFORM_CONSOLE
252 LPTSTR szTitle;
253 LPTSTR szTmp;
254 LPCTSTR szTitleText = "%s Warning";
255 LPCTSTR szDefaultModule = "TIFFLIB";
256 szTmp = (module == NULL) ? (LPTSTR)szDefaultModule : (LPTSTR)module;
257 if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, (lstrlen(szTmp) +
258 lstrlen(szTitleText) + lstrlen(fmt) + 128)*sizeof(TCHAR))) == NULL)
259 return;
260 wsprintf(szTitle, szTitleText, szTmp);
261 szTmp = szTitle + (lstrlen(szTitle)+2)*sizeof(TCHAR);
262 wvsprintf(szTmp, fmt, ap);
263 MessageBox(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
264 LocalFree(szTitle);
265 return;
266 #else
267 if (module != NULL)
268 fprintf(stderr, "%s: ", module);
269 fprintf(stderr, "Warning, ");
270 vfprintf(stderr, fmt, ap);
271 fprintf(stderr, ".\n");
272 #endif
273 }
274 TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
275
276 static void
277 Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
278 {
279 #ifndef TIF_PLATFORM_CONSOLE
280 LPTSTR szTitle;
281 LPTSTR szTmp;
282 LPCTSTR szTitleText = "%s Error";
283 LPCTSTR szDefaultModule = "TIFFLIB";
284 szTmp = (module == NULL) ? (LPTSTR)szDefaultModule : (LPTSTR)module;
285 if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, (lstrlen(szTmp) +
286 lstrlen(szTitleText) + lstrlen(fmt) + 128)*sizeof(TCHAR))) == NULL)
287 return;
288 wsprintf(szTitle, szTitleText, szTmp);
289 szTmp = szTitle + (lstrlen(szTitle)+2)*sizeof(TCHAR);
290 wvsprintf(szTmp, fmt, ap);
291 MessageBox(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
292 LocalFree(szTitle);
293 return;
294 #else
295 if (module != NULL)
296 fprintf(stderr, "%s: ", module);
297 vfprintf(stderr, fmt, ap);
298 fprintf(stderr, ".\n");
299 #endif
300 }
301 TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;