]> Creatis software - clitk.git/blob - utilities/CxImage/tif_xfile.cpp
cosmetic
[clitk.git] / utilities / CxImage / tif_xfile.cpp
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                 tif->tif_fd = fd;
97         return (tif);
98 }
99
100 extern "C" TIFF* _TIFFOpenEx(CxFile* stream, const char* mode)
101 {
102         return (_TIFFFdOpen(stream, "TIFF IMAGE", mode));
103 }
104
105 #ifdef __GNUC__
106 extern  char* malloc();
107 extern  char* realloc();
108 #else
109 #include <malloc.h>
110 #endif
111
112 tdata_t
113 _TIFFmalloc(tsize_t s)
114 {
115         return (malloc((size_t) s));
116 }
117
118 void
119 _TIFFfree(tdata_t p)
120 {
121         free(p);
122 }
123
124 tdata_t
125 _TIFFrealloc(tdata_t p, tsize_t s)
126 {
127         return (realloc(p, (size_t) s));
128 }
129
130 void
131 _TIFFmemset(tdata_t p, int v, tsize_t c)
132 {
133         memset(p, v, (size_t) c);
134 }
135
136 void
137 _TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
138 {
139         memcpy(d, s, (size_t) c);
140 }
141
142 int
143 _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
144 {
145         return (memcmp(p1, p2, (size_t) c));
146 }
147
148 #ifndef UNICODE
149 #define DbgPrint wvsprintf
150 #define DbgPrint2 wsprintf
151 #define DbgMsgBox MessageBox
152 #else
153 #define DbgPrint wvsprintfA
154 #define DbgPrint2 wsprintfA
155 #define DbgMsgBox MessageBoxA
156 #endif
157
158 static void
159 Win32WarningHandler(const char* module, const char* fmt, va_list ap)
160 {
161 #ifdef _DEBUG
162 #if (!defined(_CONSOLE) && !defined(_WIN32_WCE) && defined(WIN32))
163         LPSTR szTitle;
164         LPSTR szTmp;
165         LPCSTR szTitleText = "%s Warning";
166         LPCSTR szDefaultModule = "TIFFLIB";
167         szTmp = (module == NULL) ? (LPSTR)szDefaultModule : (LPSTR)module;
168         if ((szTitle = (LPSTR)LocalAlloc(LMEM_FIXED, (strlen(szTmp) +
169                         strlen(szTitleText) + strlen(fmt) + 128))) == NULL)
170                 return;
171         DbgPrint2(szTitle, szTitleText, szTmp);
172         szTmp = szTitle + (strlen(szTitle)+2);
173         DbgPrint(szTmp, fmt, ap);
174         DbgMsgBox(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
175         LocalFree(szTitle);
176         return;
177 #else
178         if (module != NULL)
179                 fprintf(stderr, "%s: ", module);
180         fprintf(stderr, "Warning, ");
181         vfprintf(stderr, fmt, ap);
182         fprintf(stderr, ".\n");
183 #endif
184 #endif
185 }
186 TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
187
188 static void
189 Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
190 {
191 #ifdef _DEBUG
192 #if (!defined(_CONSOLE) && !defined(_WIN32_WCE) && defined(WIN32))
193         LPSTR szTitle;
194         LPSTR szTmp;
195         LPCSTR szTitleText = "%s Error";
196         LPCSTR szDefaultModule = "TIFFLIB";
197         szTmp = (module == NULL) ? (LPSTR)szDefaultModule : (LPSTR)module;
198         if ((szTitle = (LPSTR)LocalAlloc(LMEM_FIXED, (strlen(szTmp) +
199                         strlen(szTitleText) + strlen(fmt) + 128))) == NULL)
200                 return;
201         DbgPrint2(szTitle, szTitleText, szTmp);
202         szTmp = szTitle + (strlen(szTitle)+2);
203         DbgPrint(szTmp, fmt, ap);
204         DbgMsgBox(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
205         LocalFree(szTitle);
206         return;
207 #else
208         if (module != NULL)
209                 fprintf(stderr, "%s: ", module);
210         vfprintf(stderr, fmt, ap);
211         fprintf(stderr, ".\n");
212 #endif
213 #endif
214 }
215 TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
216
217 #endif
218