1 | /*
|
---|
2 | * TIFF file IO, using CxFile.
|
---|
3 | */
|
---|
4 |
|
---|
5 | #ifdef WIN32
|
---|
6 | #include <windows.h>
|
---|
7 | #endif
|
---|
8 | #include <stdio.h>
|
---|
9 |
|
---|
10 | #include "ximage.h"
|
---|
11 |
|
---|
12 | #if CXIMAGE_SUPPORT_TIF
|
---|
13 |
|
---|
14 | #include "tiff/tiffiop.h"
|
---|
15 |
|
---|
16 | #include "xfile.h"
|
---|
17 |
|
---|
18 | static tsize_t
|
---|
19 | _tiffReadProcEx(thandle_t fd, tdata_t buf, tsize_t size)
|
---|
20 | {
|
---|
21 | return (tsize_t)((CxFile*)fd)->Read(buf, 1, size);
|
---|
22 | }
|
---|
23 |
|
---|
24 | static tsize_t
|
---|
25 | _tiffWriteProcEx(thandle_t fd, tdata_t buf, tsize_t size)
|
---|
26 | {
|
---|
27 | return (tsize_t)((CxFile*)fd)->Write(buf, 1, size);
|
---|
28 | }
|
---|
29 |
|
---|
30 | static toff_t
|
---|
31 | _tiffSeekProcEx(thandle_t fd, toff_t off, int whence)
|
---|
32 | {
|
---|
33 | if ( off == 0xFFFFFFFF )
|
---|
34 | return 0xFFFFFFFF;
|
---|
35 | if (!((CxFile*)fd)->Seek(off, whence))
|
---|
36 | return 0xFFFFFFFF;
|
---|
37 | if (whence == SEEK_SET)
|
---|
38 | return off;
|
---|
39 |
|
---|
40 | return (toff_t)((CxFile*)fd)->Tell();
|
---|
41 | }
|
---|
42 |
|
---|
43 | // Return nonzero if error
|
---|
44 | static int
|
---|
45 | _tiffCloseProcEx(thandle_t /*fd*/)
|
---|
46 | {
|
---|
47 | // return !((CxFile*)fd)->Close(); // "//" needed for memory files <DP>
|
---|
48 | return 0;
|
---|
49 | }
|
---|
50 |
|
---|
51 | #include <sys/stat.h>
|
---|
52 |
|
---|
53 | static toff_t
|
---|
54 | _tiffSizeProcEx(thandle_t fd)
|
---|
55 | {
|
---|
56 | return ((CxFile*)fd)->Size();
|
---|
57 | }
|
---|
58 |
|
---|
59 | static int
|
---|
60 | _tiffMapProcEx(thandle_t /*fd*/, tdata_t* /*pbase*/, toff_t* /*psize*/)
|
---|
61 | {
|
---|
62 | return (0);
|
---|
63 | }
|
---|
64 |
|
---|
65 | static void
|
---|
66 | _tiffUnmapProcEx(thandle_t /*fd*/, tdata_t /*base*/, toff_t /*size*/)
|
---|
67 | {
|
---|
68 | }
|
---|
69 |
|
---|
70 | // Open a TIFF file descriptor for read/writing.
|
---|
71 | /*
|
---|
72 | TIFF*
|
---|
73 | TIFFOpen(const char* name, const char* mode)
|
---|
74 | {
|
---|
75 | static const char module[] = "TIFFOpen";
|
---|
76 | FILE* stream = fopen(name, mode);
|
---|
77 | if (stream == NULL)
|
---|
78 | {
|
---|
79 | TIFFError(module, "%s: Cannot open", name);
|
---|
80 | return NULL;
|
---|
81 | }
|
---|
82 | return (TIFFFdOpen((int)stream, name, mode));
|
---|
83 | }
|
---|
84 | */
|
---|
85 |
|
---|
86 | TIFF*
|
---|
87 | _TIFFFdOpen(void* fd, const char* name, const char* mode)
|
---|
88 | {
|
---|
89 | TIFF* tif;
|
---|
90 |
|
---|
91 | tif = TIFFClientOpen(name, mode,
|
---|
92 | (thandle_t) fd,
|
---|
93 | _tiffReadProcEx, _tiffWriteProcEx, _tiffSeekProcEx, _tiffCloseProcEx,
|
---|
94 | _tiffSizeProcEx, _tiffMapProcEx, _tiffUnmapProcEx);
|
---|
95 | if (tif)
|
---|
96 | // BT: add cast so it is now has identical behavior to version 5.9.9 and it will compile again
|
---|
97 | tif->tif_fd = (int)fd;
|
---|
98 | return (tif);
|
---|
99 | }
|
---|
100 |
|
---|
101 | extern "C" TIFF* _TIFFOpenEx(CxFile* stream, const char* mode)
|
---|
102 | {
|
---|
103 | return (_TIFFFdOpen(stream, "TIFF IMAGE", mode));
|
---|
104 | }
|
---|
105 |
|
---|
106 | #ifdef __GNUC__
|
---|
107 | extern char* malloc();
|
---|
108 | extern char* realloc();
|
---|
109 | #else
|
---|
110 | #include <malloc.h>
|
---|
111 | #endif
|
---|
112 |
|
---|
113 | tdata_t
|
---|
114 | _TIFFmalloc(tsize_t s)
|
---|
115 | {
|
---|
116 | return (malloc((size_t) s));
|
---|
117 | }
|
---|
118 |
|
---|
119 | void
|
---|
120 | _TIFFfree(tdata_t p)
|
---|
121 | {
|
---|
122 | free(p);
|
---|
123 | }
|
---|
124 |
|
---|
125 | tdata_t
|
---|
126 | _TIFFrealloc(tdata_t p, tsize_t s)
|
---|
127 | {
|
---|
128 | return (realloc(p, (size_t) s));
|
---|
129 | }
|
---|
130 |
|
---|
131 | void
|
---|
132 | _TIFFmemset(tdata_t p, int v, tsize_t c)
|
---|
133 | {
|
---|
134 | memset(p, v, (size_t) c);
|
---|
135 | }
|
---|
136 |
|
---|
137 | void
|
---|
138 | _TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
|
---|
139 | {
|
---|
140 | memcpy(d, s, (size_t) c);
|
---|
141 | }
|
---|
142 |
|
---|
143 | int
|
---|
144 | _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
|
---|
145 | {
|
---|
146 | return (memcmp(p1, p2, (size_t) c));
|
---|
147 | }
|
---|
148 |
|
---|
149 | #ifndef UNICODE
|
---|
150 | #define DbgPrint wvsprintf
|
---|
151 | #define DbgPrint2 wsprintf
|
---|
152 | #define DbgMsgBox MessageBox
|
---|
153 | #else
|
---|
154 | #define DbgPrint wvsprintfA
|
---|
155 | #define DbgPrint2 wsprintfA
|
---|
156 | #define DbgMsgBox MessageBoxA
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | static void
|
---|
160 | Win32WarningHandler(const char* module, const char* fmt, va_list ap)
|
---|
161 | {
|
---|
162 | #ifdef _DEBUG
|
---|
163 | #if (!defined(_CONSOLE) && !defined(_WIN32_WCE) && defined(WIN32))
|
---|
164 | LPSTR szTitle;
|
---|
165 | LPSTR szTmp;
|
---|
166 | LPCSTR szTitleText = "%s Warning";
|
---|
167 | LPCSTR szDefaultModule = "TIFFLIB";
|
---|
168 | szTmp = (module == NULL) ? (LPSTR)szDefaultModule : (LPSTR)module;
|
---|
169 | if ((szTitle = (LPSTR)LocalAlloc(LMEM_FIXED, (strlen(szTmp) +
|
---|
170 | strlen(szTitleText) + strlen(fmt) + 128))) == NULL)
|
---|
171 | return;
|
---|
172 | DbgPrint2(szTitle, szTitleText, szTmp);
|
---|
173 | szTmp = szTitle + (strlen(szTitle)+2);
|
---|
174 | DbgPrint(szTmp, fmt, ap);
|
---|
175 | DbgMsgBox(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
|
---|
176 | LocalFree(szTitle);
|
---|
177 | return;
|
---|
178 | #else
|
---|
179 | if (module != NULL)
|
---|
180 | fprintf(stderr, "%s: ", module);
|
---|
181 | fprintf(stderr, "Warning, ");
|
---|
182 | vfprintf(stderr, fmt, ap);
|
---|
183 | fprintf(stderr, ".\n");
|
---|
184 | #endif
|
---|
185 | #endif
|
---|
186 | }
|
---|
187 | TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
|
---|
188 |
|
---|
189 | static void
|
---|
190 | Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
|
---|
191 | {
|
---|
192 | #ifdef _DEBUG
|
---|
193 | #if (!defined(_CONSOLE) && !defined(_WIN32_WCE) && defined(WIN32))
|
---|
194 | LPSTR szTitle;
|
---|
195 | LPSTR szTmp;
|
---|
196 | LPCSTR szTitleText = "%s Error";
|
---|
197 | LPCSTR szDefaultModule = "TIFFLIB";
|
---|
198 | szTmp = (module == NULL) ? (LPSTR)szDefaultModule : (LPSTR)module;
|
---|
199 | if ((szTitle = (LPSTR)LocalAlloc(LMEM_FIXED, (strlen(szTmp) +
|
---|
200 | strlen(szTitleText) + strlen(fmt) + 128))) == NULL)
|
---|
201 | return;
|
---|
202 | DbgPrint2(szTitle, szTitleText, szTmp);
|
---|
203 | szTmp = szTitle + (strlen(szTitle)+2);
|
---|
204 | DbgPrint(szTmp, fmt, ap);
|
---|
205 | DbgMsgBox(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
|
---|
206 | LocalFree(szTitle);
|
---|
207 | return;
|
---|
208 | #else
|
---|
209 | if (module != NULL)
|
---|
210 | fprintf(stderr, "%s: ", module);
|
---|
211 | vfprintf(stderr, fmt, ap);
|
---|
212 | fprintf(stderr, ".\n");
|
---|
213 | #endif
|
---|
214 | #endif
|
---|
215 | }
|
---|
216 | TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
|
---|
217 |
|
---|
218 | #endif
|
---|
219 |
|
---|