]> git.saurik.com Git - wxWidgets.git/blob - src/tiff/tif_apple.c
wxChoice/wxComboBox background colour change (to get back to where we were);
[wxWidgets.git] / src / tiff / tif_apple.c
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 *
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.
14 *
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.
18 *
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
24 * OF THIS SOFTWARE.
25 */
26
27 /*
28 * TIFF Library Macintosh-specific routines.
29 *
30 * These routines use only Toolbox and high-level File Manager traps.
31 * They make no calls to the THINK C "unix" compatibility library. Also,
32 * malloc is not used directly but it is still referenced internally by
33 * the ANSI library in rare cases. Heap fragmentation by the malloc ring
34 * buffer is therefore minimized.
35 *
36 * O_RDONLY and O_RDWR are treated identically here. The tif_mode flag is
37 * checked in TIFFWriteCheck().
38 *
39 * Create below fills in a blank creator signature and sets the file type
40 * to 'TIFF'. It is much better for the application to do this by Create'ing
41 * the file first and TIFFOpen'ing it later.
42 */
43
44 #include "tiffiop.h"
45 #include <Errors.h>
46 #include <Files.h>
47 #include <Memory.h>
48
49 #if defined(__PPCC__) || defined(__SC__) || defined(__MRC__) || defined(applec)
50 #define CtoPstr c2pstr
51 #endif
52
53 static tsize_t
54 _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
55 {
56 return (FSRead((short) fd, (long*) &size, (char*) buf) == noErr ?
57 size : (tsize_t) -1);
58 }
59
60 static tsize_t
61 _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
62 {
63 return (FSWrite((short) fd, (long*) &size, (char*) buf) == noErr ?
64 size : (tsize_t) -1);
65 }
66
67 static toff_t
68 _tiffSeekProc(thandle_t fd, toff_t off, int whence)
69 {
70 long fpos, size;
71
72 if (GetEOF((short) fd, &size) != noErr)
73 return EOF;
74 (void) GetFPos((short) fd, &fpos);
75
76 switch (whence) {
77 case SEEK_CUR:
78 if (off + fpos > size)
79 SetEOF((short) fd, off + fpos);
80 if (SetFPos((short) fd, fsFromMark, off) != noErr)
81 return EOF;
82 break;
83 case SEEK_END:
84 if (off > 0)
85 SetEOF((short) fd, off + size);
86 if (SetFPos((short) fd, fsFromStart, off + size) != noErr)
87 return EOF;
88 break;
89 case SEEK_SET:
90 if (off > size)
91 SetEOF((short) fd, off);
92 if (SetFPos((short) fd, fsFromStart, off) != noErr)
93 return EOF;
94 break;
95 }
96
97 return (toff_t)(GetFPos((short) fd, &fpos) == noErr ? fpos : EOF);
98 }
99
100 static int
101 _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
102 {
103 return (0);
104 }
105
106 static void
107 _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
108 {
109 }
110
111 static int
112 _tiffCloseProc(thandle_t fd)
113 {
114 return (FSClose((short) fd));
115 }
116
117 static toff_t
118 _tiffSizeProc(thandle_t fd)
119 {
120 long size;
121
122 if (GetEOF((short) fd, &size) != noErr) {
123 TIFFError("_tiffSizeProc", "%s: Cannot get file size");
124 return (-1L);
125 }
126 return ((toff_t) size);
127 }
128
129 /*
130 * Open a TIFF file descriptor for read/writing.
131 */
132 TIFF*
133 TIFFFdOpen(int fd, const char* name, const char* mode)
134 {
135 TIFF* tif;
136
137 tif = TIFFClientOpen(name, mode, (thandle_t) fd,
138 _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc,
139 _tiffSizeProc, _tiffMapProc, _tiffUnmapProc);
140 if (tif)
141 tif->tif_fd = fd;
142 return (tif);
143 }
144
145 /*
146 * Open a TIFF file for read/writing.
147 */
148 TIFF*
149 TIFFOpen(const char* name, const char* mode)
150 {
151 static const char module[] = "TIFFOpen";
152 Str255 pname;
153 FInfo finfo;
154 short fref;
155 OSErr err;
156
157 strcpy((char*) pname, name);
158 CtoPstr((char*) pname);
159
160 switch (_TIFFgetMode(mode, module)) {
161 default:
162 return ((TIFF*) 0);
163 case O_RDWR | O_CREAT | O_TRUNC:
164 if (GetFInfo(pname, 0, &finfo) == noErr)
165 FSDelete(pname, 0);
166 /* fall through */
167 case O_RDWR | O_CREAT:
168 if ((err = GetFInfo(pname, 0, &finfo)) == fnfErr) {
169 if (Create(pname, 0, ' ', 'TIFF') != noErr)
170 goto badCreate;
171 if (FSOpen(pname, 0, &fref) != noErr)
172 goto badOpen;
173 } else if (err == noErr) {
174 if (FSOpen(pname, 0, &fref) != noErr)
175 goto badOpen;
176 } else
177 goto badOpen;
178 break;
179 case O_RDONLY:
180 case O_RDWR:
181 if (FSOpen(pname, 0, &fref) != noErr)
182 goto badOpen;
183 break;
184 }
185 return (TIFFFdOpen((int) fref, name, mode));
186 badCreate:
187 TIFFError(module, "%s: Cannot create", name);
188 return ((TIFF*) 0);
189 badOpen:
190 TIFFError(module, "%s: Cannot open", name);
191 return ((TIFF*) 0);
192 }
193
194 void
195 _TIFFmemset(tdata_t p, int v, tsize_t c)
196 {
197 memset(p, v, (size_t) c);
198 }
199
200 void
201 _TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
202 {
203 memcpy(d, s, (size_t) c);
204 }
205
206 int
207 _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
208 {
209 return (memcmp(p1, p2, (size_t) c));
210 }
211
212 tdata_t
213 _TIFFmalloc(tsize_t s)
214 {
215 return (NewPtr((size_t) s));
216 }
217
218 void
219 _TIFFfree(tdata_t p)
220 {
221 DisposePtr(p);
222 }
223
224 tdata_t
225 _TIFFrealloc(tdata_t p, tsize_t s)
226 {
227 Ptr n = p;
228
229 SetPtrSize(p, (size_t) s);
230 if (MemError() && (n = NewPtr((size_t) s)) != NULL) {
231 BlockMove(p, n, GetPtrSize(p));
232 DisposePtr(p);
233 }
234 return ((tdata_t) n);
235 }
236
237 static void
238 appleWarningHandler(const char* module, const char* fmt, va_list ap)
239 {
240 if (module != NULL)
241 fprintf(stderr, "%s: ", module);
242 fprintf(stderr, "Warning, ");
243 vfprintf(stderr, fmt, ap);
244 fprintf(stderr, ".\n");
245 }
246 TIFFErrorHandler _TIFFwarningHandler = appleWarningHandler;
247
248 static void
249 appleErrorHandler(const char* module, const char* fmt, va_list ap)
250 {
251 if (module != NULL)
252 fprintf(stderr, "%s: ", module);
253 vfprintf(stderr, fmt, ap);
254 fprintf(stderr, ".\n");
255 }
256 TIFFErrorHandler _TIFFerrorHandler = appleErrorHandler;