source: liacs/MIR2010/SourceCode/cximage/tif_xfile.cpp@ 327

Last change on this file since 327 was 95, checked in by Rick van der Zwet, 15 years ago

Bad boy, improper move of directory

File size: 4.6 KB
Line 
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
18static 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
24static 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
30static 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
44static 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
53static toff_t
54_tiffSizeProcEx(thandle_t fd)
55{
56 return ((CxFile*)fd)->Size();
57}
58
59static int
60_tiffMapProcEx(thandle_t /*fd*/, tdata_t* /*pbase*/, toff_t* /*psize*/)
61{
62 return (0);
63}
64
65static 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/*
72TIFF*
73TIFFOpen(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
86TIFF*
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
101extern "C" TIFF* _TIFFOpenEx(CxFile* stream, const char* mode)
102{
103 return (_TIFFFdOpen(stream, "TIFF IMAGE", mode));
104}
105
106#ifdef __GNUC__
107extern char* malloc();
108extern char* realloc();
109#else
110#include <malloc.h>
111#endif
112
113tdata_t
114_TIFFmalloc(tsize_t s)
115{
116 return (malloc((size_t) s));
117}
118
119void
120_TIFFfree(tdata_t p)
121{
122 free(p);
123}
124
125tdata_t
126_TIFFrealloc(tdata_t p, tsize_t s)
127{
128 return (realloc(p, (size_t) s));
129}
130
131void
132_TIFFmemset(tdata_t p, int v, tsize_t c)
133{
134 memset(p, v, (size_t) c);
135}
136
137void
138_TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
139{
140 memcpy(d, s, (size_t) c);
141}
142
143int
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
159static void
160Win32WarningHandler(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}
187TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
188
189static void
190Win32ErrorHandler(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}
216TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
217
218#endif
219
Note: See TracBrowser for help on using the repository browser.