1 /* $Id: tif_win32.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
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
34 _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
37 if (!ReadFile(fd, buf, size, &dwSizeRead, NULL))
39 return ((tsize_t) dwSizeRead);
43 _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
46 if (!WriteFile(fd, buf, size, &dwSizeWritten, NULL))
48 return ((tsize_t) dwSizeWritten);
52 _tiffSeekProc(thandle_t fd, toff_t off, int whence)
54 DWORD dwMoveMethod, dwMoveHigh;
56 /* we use this as a special code, so avoid accepting it */
57 if( off == 0xFFFFFFFF )
63 dwMoveMethod = FILE_BEGIN;
66 dwMoveMethod = FILE_CURRENT;
69 dwMoveMethod = FILE_END;
72 dwMoveMethod = FILE_BEGIN;
76 return ((toff_t)SetFilePointer(fd, (LONG) off, (PLONG)&dwMoveHigh,
81 _tiffCloseProc(thandle_t fd)
83 return (CloseHandle(fd) ? 0 : -1);
87 _tiffSizeProc(thandle_t fd)
89 return ((toff_t)GetFileSize(fd, NULL));
96 _tiffDummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
102 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
104 * Windows uses both a handle and a pointer for file mapping,
105 * but according to the SDK documentation and Richter's book
106 * "Advanced Windows Programming" it is safe to free the handle
107 * after obtaining the file mapping pointer
109 * This removes a nasty OS dependency and cures a problem
110 * with Visual C++ 5.0
113 _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
118 if ((size = _tiffSizeProc(fd)) == 0xFFFFFFFF)
120 hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, size, NULL);
121 if (hMapFile == NULL)
123 *pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
124 CloseHandle(hMapFile);
135 _tiffDummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
140 _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
142 UnmapViewOfFile(base);
146 * Open a TIFF file descriptor for read/writing.
147 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
148 * string, which forces the file to be opened unmapped.
151 TIFFFdOpen(int ifd, const char* name, const char* mode)
154 BOOL fSuppressMap = (mode[1] == 'u' || (mode[1]!=0 && mode[2] == 'u'));
156 tif = TIFFClientOpen(name, mode, (thandle_t)ifd,
157 _tiffReadProc, _tiffWriteProc,
158 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
159 fSuppressMap ? _tiffDummyMapProc : _tiffMapProc,
160 fSuppressMap ? _tiffDummyUnmapProc : _tiffUnmapProc);
167 * Open a TIFF file for read/writing.
170 TIFFOpen(const char* name, const char* mode)
172 static const char module[] = "TIFFOpen";
178 m = _TIFFgetMode(mode, module);
183 dwMode = OPEN_EXISTING;
186 dwMode = OPEN_ALWAYS;
189 dwMode = OPEN_ALWAYS;
192 dwMode = CREATE_ALWAYS;
194 case O_RDWR|O_CREAT|O_TRUNC:
195 dwMode = CREATE_ALWAYS;
200 fd = (thandle_t)CreateFileA(name,
201 (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ | GENERIC_WRITE),
202 FILE_SHARE_READ, NULL, dwMode,
203 (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
205 if (fd == INVALID_HANDLE_VALUE) {
206 TIFFError(module, "%s: Cannot open", name);
210 tif = TIFFFdOpen((int)fd, name, mode);
217 * Open a TIFF file with a Unicode filename, for read/writing.
220 TIFFOpenW(const wchar_t* name, const char* mode)
222 static const char module[] = "TIFFOpenW";
230 m = _TIFFgetMode(mode, module);
233 case O_RDONLY: dwMode = OPEN_EXISTING; break;
234 case O_RDWR: dwMode = OPEN_ALWAYS; break;
235 case O_RDWR|O_CREAT: dwMode = OPEN_ALWAYS; break;
236 case O_RDWR|O_TRUNC: dwMode = CREATE_ALWAYS; break;
237 case O_RDWR|O_CREAT|O_TRUNC: dwMode = CREATE_ALWAYS; break;
238 default: return ((TIFF*)0);
241 fd = (thandle_t)CreateFileW(name,
242 (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ|GENERIC_WRITE),
243 FILE_SHARE_READ, NULL, dwMode,
244 (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
246 if (fd == INVALID_HANDLE_VALUE) {
247 TIFFError(module, "%S: Cannot open", name);
252 mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
254 mbname = _TIFFmalloc(mbsize);
257 "Can't allocate space for filename conversion buffer");
261 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
265 tif = TIFFFdOpen((int)fd,
266 (mbname != NULL) ? mbname : "<unknown>", mode);
274 _TIFFmalloc(tsize_t s)
276 return ((tdata_t)GlobalAlloc(GMEM_FIXED, s));
287 _TIFFrealloc(tdata_t p, tsize_t s)
293 return ((tdata_t)GlobalAlloc(GMEM_FIXED, s));
298 if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
299 CopyMemory(pvTmp, p, s);
303 if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
304 CopyMemory(pvTmp, p, old);
308 return ((tdata_t)pvTmp);
312 _TIFFmemset(void* p, int v, tsize_t c)
314 FillMemory(p, c, (BYTE)v);
318 _TIFFmemcpy(void* d, const tdata_t s, tsize_t c)
324 _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
326 register const BYTE *pb1 = (const BYTE *) p1;
327 register const BYTE *pb2 = (const BYTE *) p2;
328 register DWORD dwTmp = c;
330 for (iTmp = 0; dwTmp-- && !iTmp; iTmp = (int)*pb1++ - (int)*pb2++)
336 Win32WarningHandler(const char* module, const char* fmt, va_list ap)
338 #ifndef TIF_PLATFORM_CONSOLE
341 LPCTSTR szTitleText = "%s Warning";
342 LPCTSTR szDefaultModule = "LIBTIFF";
343 LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
344 if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, (strlen(szTmpModule) +
345 strlen(szTitleText) + strlen(fmt) + 128)*sizeof(char))) == NULL)
347 sprintf(szTitle, szTitleText, szTmpModule);
348 szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
349 vsprintf(szTmp, fmt, ap);
350 MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
355 fprintf(stderr, "%s: ", module);
356 fprintf(stderr, "Warning, ");
357 vfprintf(stderr, fmt, ap);
358 fprintf(stderr, ".\n");
361 TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
364 Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
366 #ifndef TIF_PLATFORM_CONSOLE
369 LPCTSTR szTitleText = "%s Error";
370 LPCTSTR szDefaultModule = "LIBTIFF";
371 LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
372 if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, (strlen(szTmpModule) +
373 strlen(szTitleText) + strlen(fmt) + 128)*sizeof(char))) == NULL)
375 sprintf(szTitle, szTitleText, szTmpModule);
376 szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
377 vsprintf(szTmp, fmt, ap);
378 MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
383 fprintf(stderr, "%s: ", module);
384 vfprintf(stderr, fmt, ap);
385 fprintf(stderr, ".\n");
388 TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
390 /* vim: set ts=8 sts=8 sw=8 noet: */