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
35 _tiffReadProc(thandle_t fd
, tdata_t buf
, tsize_t size
)
38 if (!ReadFile(fd
, buf
, size
, &dwSizeRead
, NULL
))
40 return ((tsize_t
) dwSizeRead
);
44 _tiffWriteProc(thandle_t fd
, tdata_t buf
, tsize_t size
)
47 if (!WriteFile(fd
, buf
, size
, &dwSizeWritten
, NULL
))
49 return ((tsize_t
) dwSizeWritten
);
53 _tiffSeekProc(thandle_t fd
, toff_t off
, int whence
)
55 DWORD dwMoveMethod
, dwMoveHigh
;
57 /* we use this as a special code, so avoid accepting it */
58 if( off
== 0xFFFFFFFF )
64 dwMoveMethod
= FILE_BEGIN
;
67 dwMoveMethod
= FILE_CURRENT
;
70 dwMoveMethod
= FILE_END
;
73 dwMoveMethod
= FILE_BEGIN
;
77 return ((toff_t
)SetFilePointer(fd
, (LONG
) off
, (PLONG
)&dwMoveHigh
,
82 _tiffCloseProc(thandle_t fd
)
84 return (CloseHandle(fd
) ? 0 : -1);
88 _tiffSizeProc(thandle_t fd
)
90 return ((toff_t
)GetFileSize(fd
, NULL
));
97 _tiffDummyMapProc(thandle_t fd
, tdata_t
* pbase
, toff_t
* psize
)
103 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
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
110 * This removes a nasty OS dependency and cures a problem
111 * with Visual C++ 5.0
114 _tiffMapProc(thandle_t fd
, tdata_t
* pbase
, toff_t
* psize
)
119 if ((size
= _tiffSizeProc(fd
)) == 0xFFFFFFFF)
121 hMapFile
= CreateFileMapping(fd
, NULL
, PAGE_READONLY
, 0, size
, NULL
);
122 if (hMapFile
== NULL
)
124 *pbase
= MapViewOfFile(hMapFile
, FILE_MAP_READ
, 0, 0, 0);
125 CloseHandle(hMapFile
);
136 _tiffDummyUnmapProc(thandle_t fd
, tdata_t base
, toff_t size
)
141 _tiffUnmapProc(thandle_t fd
, tdata_t base
, toff_t size
)
143 UnmapViewOfFile(base
);
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.
152 TIFFFdOpen(int ifd
, const char* name
, const char* mode
)
155 BOOL fSuppressMap
= (mode
[1] == 'u' || (mode
[1]!=0 && mode
[2] == 'u'));
157 tif
= TIFFClientOpen(name
, mode
,
159 _tiffReadProc
, _tiffWriteProc
,
160 _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
,
161 fSuppressMap
? _tiffDummyMapProc
: _tiffMapProc
,
162 fSuppressMap
? _tiffDummyUnmapProc
: _tiffUnmapProc
);
169 * Open a TIFF file for read/writing.
172 TIFFOpen(const char* name
, const char* mode
)
174 static const char module[] = "TIFFOpen";
179 m
= _TIFFgetMode(mode
, module);
184 dwMode
= OPEN_EXISTING
;
187 dwMode
= OPEN_ALWAYS
;
190 dwMode
= OPEN_ALWAYS
;
193 dwMode
= CREATE_ALWAYS
;
195 case O_RDWR
|O_CREAT
|O_TRUNC
:
196 dwMode
= CREATE_ALWAYS
;
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
);
208 return (TIFFFdOpen((int)fd
, name
, mode
));
212 _TIFFmalloc(tsize_t s
)
214 return ((tdata_t
)GlobalAlloc(GMEM_FIXED
, s
));
225 _TIFFrealloc(tdata_t p
, tsize_t s
)
228 tsize_t old
=GlobalSize(p
);
231 if ((pvTmp
= GlobalAlloc(GMEM_FIXED
, s
)) != NULL
) {
232 CopyMemory(pvTmp
, p
, s
);
238 if ((pvTmp
= GlobalAlloc(GMEM_FIXED
, s
)) != NULL
) {
239 CopyMemory(pvTmp
, p
, old
);
243 return ((tdata_t
)pvTmp
);
247 _TIFFmemset(void* p
, int v
, tsize_t c
)
249 FillMemory(p
, c
, (BYTE
)v
);
253 _TIFFmemcpy(void* d
, const tdata_t s
, tsize_t c
)
259 _TIFFmemcmp(const tdata_t p1
, const tdata_t p2
, tsize_t c
)
261 register const BYTE
*pb1
= (const BYTE
*) p1
;
262 register const BYTE
*pb2
= (const BYTE
*) p2
;
263 register DWORD dwTmp
= c
;
265 for (iTmp
= 0; dwTmp
-- && !iTmp
; iTmp
= (int)*pb1
++ - (int)*pb2
++)
271 Win32WarningHandler(const char* module, const char* fmt
, va_list ap
)
273 #ifndef TIF_PLATFORM_CONSOLE
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
)
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
);
290 fprintf(stderr
, "%s: ", module);
291 fprintf(stderr
, "Warning, ");
292 vfprintf(stderr
, fmt
, ap
);
293 fprintf(stderr
, ".\n");
296 TIFFErrorHandler _TIFFwarningHandler
= Win32WarningHandler
;
299 Win32ErrorHandler(const char* module, const char* fmt
, va_list ap
)
301 #ifndef TIF_PLATFORM_CONSOLE
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
)
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
);
318 fprintf(stderr
, "%s: ", module);
319 vfprintf(stderr
, fmt
, ap
);
320 fprintf(stderr
, ".\n");
323 TIFFErrorHandler _TIFFerrorHandler
= Win32ErrorHandler
;