/
OPImageCodecTIFF.m
379 lines (316 loc) · 8.17 KB
/
OPImageCodecTIFF.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/** <title>OPImageCodecTIFF</title>
<abstract>C Interface to graphics drawing library</abstract>
Copyright <copy>(C) 2010 Free Software Foundation, Inc.</copy>
Author: Eric Wasylishen <ewasylishen@gmail.com>
Date: June, 2010
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <tiff.h>
#include <tiffio.h>
#import <Foundation/NSData.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSException.h>
#import <Foundation/NSString.h>
#import "CGImageSource-private.h"
#import "CGImageDestination-private.h"
#import "CGDataProvider-private.h"
#import "CGDataConsumer-private.h"
extern void DumpPixel(const void *data, NSString *msg);
@interface OPTIFFHandle : NSObject
{
NSData *data;
off_t pos;
BOOL mutable;
CGDataConsumerRef consumer;
}
- (id)initWithDataProvider: (CGDataProviderRef)provider;
- (id)initWithDataConsumer: (CGDataConsumerRef)consumer;
- (size_t) read: (unsigned char *)buf count: (size_t)count;
- (size_t) write: (const unsigned char *)buf count: (size_t)count;
- (off_t) seek: (off_t)off mode: (int)model;
@end
@implementation OPTIFFHandle
- (id)initWithDataProvider: (CGDataProviderRef)provider
{
self = [super init];
data = CGDataProviderCopyData(provider);
mutable = NO;
return self;
}
- (id)initWithDataConsumer: (CGDataConsumerRef)dc
{
self = [super init];
data = [[NSMutableData alloc] initWithLength: 0];
ASSIGN(consumer, dc);
mutable = YES;
return self;
}
- (void)dealloc
{
// FIXME: this is ugly
if (mutable)
{
OPDataConsumerPutBytes(consumer, [data bytes], [data length]);
}
[data release];
[consumer release];
[super dealloc];
}
- (size_t) read: (unsigned char *)buf count: (size_t)count
{
[data getBytes: buf range: NSMakeRange(pos, count)];
pos += count;
return count;
}
- (size_t) write: (const unsigned char *)buf count: (size_t)count
{
[(NSMutableData*)data replaceBytesInRange: NSMakeRange(pos, MAX(0, (off_t)[data length] - pos))
withBytes: buf
length: count];
pos += count;
return count;
}
- (off_t) seek: (off_t)off mode: (int)mode
{
switch(mode)
{
case SEEK_SET:
pos = off;
break;
case SEEK_CUR:
pos += off;
break;
case SEEK_END:
pos = ([data length] - 1) + off;
break;
default:
break;
}
if (pos < 0)
{
pos = 0;
}
else if (pos > [data length] && !mutable)
{
pos = [data length];
}
return pos;
}
- (size_t)size
{
return [data length];
}
- (void)map: (tdata_t*)bytes size: (toff_t*)size
{
if (mutable)
{
*bytes = [(NSMutableData*)data mutableBytes];
}
else
{
*bytes = (tdata_t*)[data bytes];
}
*size = [data length];
}
static tsize_t OPTIFFReadProc(thandle_t handle, tdata_t buf, tsize_t count)
{
return [(OPTIFFHandle*)handle read: buf count: count];
}
static tsize_t OPTIFFWriteProc(thandle_t handle, tdata_t buf, tsize_t count)
{
return [(OPTIFFHandle*)handle write: buf count: count];
}
static toff_t OPTIFFSeekProc(thandle_t handle, toff_t offset, int mode)
{
return [(OPTIFFHandle*)handle seek: offset mode: mode];
}
static int OPTIFFCloseProc(thandle_t handle)
{
[(OPTIFFHandle*)handle release];
return 0;
}
static toff_t OPTIFFSizeProc(thandle_t handle)
{
return [(OPTIFFHandle*)handle size];
}
static int OPTIFFMapProc(thandle_t handle, tdata_t* data, toff_t* size)
{
[(OPTIFFHandle*)handle map: (tdata_t*)data size: (toff_t*)size];
return 1;
}
static void OPTIFFUnmapProc(thandle_t handle, tdata_t data, toff_t size)
{
}
@end
@interface CGImageSourceTIFF : CGImageSource
{
TIFF *tiff;
}
@end
@implementation CGImageSourceTIFF
+ (void)load
{
[CGImageSource registerSourceClass: self];
}
+ (NSArray *)typeIdentifiers
{
return [NSArray arrayWithObject: @"public.tiff"];
}
- (id)initWithProvider: (CGDataProviderRef)provider;
{
self = [super init];
// Will be released by libtiff via OPTIFFCloseProc
OPTIFFHandle *handle = [[OPTIFFHandle alloc] initWithDataProvider: provider];
tiff = TIFFClientOpen("OPTIFFWithDataProvider", "r", handle,
OPTIFFReadProc, OPTIFFWriteProc, OPTIFFSeekProc,
OPTIFFCloseProc, OPTIFFSizeProc, OPTIFFMapProc,
OPTIFFUnmapProc);
if (tiff == NULL)
{
[self release];
return nil;
}
return self;
}
- (void)dealloc
{
if (tiff != NULL)
{
TIFFClose(tiff);
}
[super dealloc];
}
- (NSDictionary*)propertiesWithOptions: (NSDictionary*)opts
{
return [NSDictionary dictionary];
}
- (NSDictionary*)propertiesWithOptions: (NSDictionary*)opts atIndex: (size_t)index
{
return [NSDictionary dictionary];
}
- (size_t)count
{
size_t dirs = 0;
if (1 == TIFFSetDirectory(tiff, 0))
{
do
{
dirs++;
}
while (1 == TIFFReadDirectory(tiff));
}
return dirs;
}
- (CGImageRef)createImageAtIndex: (size_t)index options: (NSDictionary*)opts
{
CGImageRef img = NULL;
TIFFSetDirectory(tiff, index);
uint32_t width;
uint32_t height;
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height);
NSMutableData *imgData = [[NSMutableData alloc] initWithLength: height * width * 4];
if (1 == TIFFReadRGBAImageOriented(tiff, width, height, [imgData mutableBytes],
ORIENTATION_TOPLEFT, 0))
{
CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData(imgData);
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
img = CGImageCreate(width, height, 8, 32, 4 * width, cs,
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast, imgDataProvider,
NULL, true, kCGRenderingIntentDefault);
DumpPixel([imgData bytes], @"read from TIFF: (expecting R G B A)");
CGColorSpaceRelease(cs);
CGDataProviderRelease(imgDataProvider);
}
[imgData release];
return img;
}
- (CGImageRef)createThumbnailAtIndex: (size_t)index options: (NSDictionary*)opts
{
return nil;
}
- (CGImageSourceStatus)status
{
return kCGImageStatusComplete;
}
- (CGImageSourceStatus)statusAtIndex: (size_t)index
{
return kCGImageStatusComplete;
}
- (NSString*)type
{
return @"public.tiff";
}
- (void)updateDataProvider: (CGDataProviderRef)provider finalUpdate: (bool)finalUpdate
{
;
}
@end
@interface CGImageDestinationTIFF : CGImageDestination
{
TIFF *tiff;
CFDictionaryRef props;
CGImageRef img;
}
@end
@implementation CGImageDestinationTIFF
+ (void)load
{
[CGImageDestination registerDestinationClass: self];
}
+ (NSArray *)typeIdentifiers
{
return [NSArray arrayWithObject: @"public.tiff"];
}
- (id) initWithDataConsumer: (CGDataConsumerRef)consumer
type: (CFStringRef)type
count: (size_t)count
options: (CFDictionaryRef)opts
{
self = [super init];
if ([type isEqualToString: @"public.tiff"] || count != 1)
{
[self release];
return nil;
}
// Will be released by libtiff via OPTIFFCloseProc
OPTIFFHandle *handle = [[OPTIFFHandle alloc] initWithDataConsumer: consumer];
tiff = TIFFClientOpen("OPTIFFWithDataConsumer", "w", handle,
OPTIFFReadProc, OPTIFFWriteProc, OPTIFFSeekProc,
OPTIFFCloseProc, OPTIFFSizeProc, OPTIFFMapProc,
OPTIFFUnmapProc);
return self;
}
- (void)dealloc
{
// FIXME: close in -finalize
TIFFClose(tiff);
[props release];
CGImageRelease(img);
[super dealloc];
}
- (void) setProperties: (CFDictionaryRef)properties
{
ASSIGN(props, properties);
}
- (void) addImage: (CGImageRef)image properties: (CFDictionaryRef)properties
{
ASSIGN(img, image);
ASSIGN(props, properties);
}
- (bool) finalize
{
return false;
}
@end