Debian Codegraph @

converter/other/tiff.c

raw
1 /*============================================================================
2 tiff.c
3 ==============================================================================
4
5 Stuff specific to the TIFF format -- essentially and extension to libtiff.
6
7 ============================================================================*/
8
9 #define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
10 #define _BSD_SOURCE /* Make sure strcaseeq() is in nstring.h */
11
12 #include <string.h>
13
14 #include <tiffio.h>
15
16 #include "pm_c_util.h"
17 #include "nstring.h"
18 #include "mallocvar.h"
19 #include "pm.h"
20 #include "pm_tiff.h"
21
22
23
24 static uint32_t
25 number(const char * const value,
26 tagvalmap const tagvallist[]) {
27
28 char * ep;
29 long num;
30
31 if (strlen(value) == 0)
32 pm_error("null string where numeric tag value or enumerated tag "
33 "value name expected");
34
35 num = strtol(value, &ep, 10);
36 if (*ep != '\0') {
37 /* It's not a numeric string, so it must be an enumerated value name */
38 unsigned int i;
39 for (i = 0; tagvallist[i].name; ++i) {
40 if (strcaseeq(value, tagvallist[i].name))
41 return tagvallist[i].value;
42 }
43 pm_error("'%s' is neither a number nor a valid value name", value);
44 return 0; /* avoid compiler warning */
45 } else
46 return num;
47 }
48
49
50
51 static double
52 dnumber(const char * const value) {
53
54 char * ep;
55 double num;
56
57 num = strtod(value, &ep);
58 if (ep == value || *ep != '\0')
59 pm_error("Bad floating point number %s", value);
60
61 return num;
62 }
63
64
65
66 #if 0
67 static void
68 putByte(TIFF * const tifP,
69 unsigned int const tagnum,
70 const char * const value,
71 tagvalmap const tagvallist[]) {
72
73 uint16 const num = number(value, tagvallist);
74
75 TIFFSetField(tifP, tagnum, num);
76 }
77 #endif
78
79
80
81 static void
82 putAscii(TIFF * const tifP,
83 unsigned int const tagnum,
84 const char * const value,
85 tagvalmap const tagvallist[]) {
86
87 TIFFSetField(tifP, tagnum, value);
88 }
89
90
91
92 static void
93 putShort(TIFF * const tifP,
94 unsigned const tagnum,
95 const char * const value,
96 tagvalmap const tagvallist[]) {
97
98 uint16_t const num = number(value, tagvallist);
99
100 TIFFSetField(tifP, tagnum, num);
101 }
102
103
104
105 static void
106 putLong(TIFF * const tifP,
107 unsigned int const tagnum,
108 const char * const value,
109 tagvalmap const tagvallist[]) {
110
111 uint32_t const num = number(value, tagvallist);
112
113 TIFFSetField(tifP, tagnum, num);
114 }
115
116
117
118 static void
119 putRational(TIFF * const tifP,
120 unsigned const tagnum,
121 const char * const value,
122 tagvalmap const tagvallist[]) {
123
124 float const num = dnumber(value);
125
126 TIFFSetField(tifP, tagnum, num);
127 }
128
129
130
131 static void
132 putCountBytes(TIFF * const tifP,
133 unsigned const tagnum,
134 const char * const value,
135 tagvalmap const tagvallist[]) {
136
137 uint32_t const len = strlen(value)/2;
138
139 char * data;
140 uint32_t i;
141 bool error;
142
143 MALLOCARRAY_NOFAIL(data, len);
144
145 for (i = 0, error = FALSE; !error && i < len; ++i) {
146 unsigned int val;
147 int nb;
148 int rc;
149 rc = sscanf(&value[i*2], "%2x%n", &val, &nb);
150 if (rc != 1 || nb != 2)
151 error = TRUE;
152 else
153 data[i] = val;
154 }
155 if (!error)
156 TIFFSetField(tifP, tagnum, len, (void *)data);
157
158 free(data);
159 }
160
161
162
163 #define TV(p,a) { #a, p##a, }
164
165 static tagvalmap const
166 tvm_compression[] = {
167 TV(COMPRESSION_,NONE),
168 TV(COMPRESSION_,CCITTRLE),
169 TV(COMPRESSION_,CCITTFAX3),
170 TV(COMPRESSION_,CCITTFAX4),
171 TV(COMPRESSION_,LZW),
172 TV(COMPRESSION_,OJPEG),
173 TV(COMPRESSION_,JPEG),
174 TV(COMPRESSION_,NEXT),
175 TV(COMPRESSION_,CCITTRLEW),
176 TV(COMPRESSION_,PACKBITS),
177 TV(COMPRESSION_,THUNDERSCAN),
178 TV(COMPRESSION_,IT8CTPAD),
179 TV(COMPRESSION_,IT8LW),
180 TV(COMPRESSION_,IT8MP),
181 TV(COMPRESSION_,IT8BL),
182 TV(COMPRESSION_,PIXARFILM),
183 TV(COMPRESSION_,PIXARLOG),
184 TV(COMPRESSION_,DEFLATE),
185 TV(COMPRESSION_,ADOBE_DEFLATE),
186 TV(COMPRESSION_,DCS),
187 TV(COMPRESSION_,JBIG),
188 TV(COMPRESSION_,SGILOG),
189 TV(COMPRESSION_,SGILOG24),
190 { NULL, 0, },
191 };
192
193 static tagvalmap const
194 tvm_faxmode[] = {
195 TV(FAXMODE_,CLASSIC),
196 TV(FAXMODE_,NORTC),
197 TV(FAXMODE_,NOEOL),
198 TV(FAXMODE_,BYTEALIGN),
199 TV(FAXMODE_,WORDALIGN),
200 TV(FAXMODE_,CLASSF),
201 { NULL, 0, },
202 };
203
204 static tagvalmap const
205 tvm_fillorder[] = {
206 TV(FILLORDER_,MSB2LSB),
207 TV(FILLORDER_,LSB2MSB),
208 { NULL, 0, },
209 };
210
211 static tagvalmap
212 const tvm_group3options[] = {
213 TV(GROUP3OPT_,2DENCODING),
214 TV(GROUP3OPT_,UNCOMPRESSED),
215 TV(GROUP3OPT_,FILLBITS),
216 { NULL, 0, },
217 };
218
219 static tagvalmap
220 const tvm_group4options[] = {
221 TV(GROUP4OPT_,UNCOMPRESSED),
222 { NULL, 0, },
223 };
224
225 static tagvalmap
226 const tvm_inkset[] = {
227 TV(INKSET_,CMYK),
228 { NULL, 0, },
229 };
230
231 static tagvalmap
232 const tvm_orientation[] = {
233 TV(ORIENTATION_,TOPLEFT),
234 TV(ORIENTATION_,TOPRIGHT),
235 TV(ORIENTATION_,BOTRIGHT),
236 TV(ORIENTATION_,BOTLEFT),
237 TV(ORIENTATION_,LEFTTOP),
238 TV(ORIENTATION_,RIGHTTOP),
239 TV(ORIENTATION_,RIGHTBOT),
240 TV(ORIENTATION_,LEFTBOT),
241 { NULL, 0, },
242 };
243
244 static tagvalmap const
245 tvm_photometric[] = {
246 TV(PHOTOMETRIC_,MINISWHITE),
247 TV(PHOTOMETRIC_,MINISBLACK),
248 TV(PHOTOMETRIC_,RGB),
249 TV(PHOTOMETRIC_,PALETTE),
250 TV(PHOTOMETRIC_,MASK),
251 TV(PHOTOMETRIC_,SEPARATED),
252 TV(PHOTOMETRIC_,YCBCR),
253 TV(PHOTOMETRIC_,CIELAB),
254 TV(PHOTOMETRIC_,LOGL),
255 TV(PHOTOMETRIC_,LOGLUV),
256 { NULL, 0, },
257 };
258
259 static tagvalmap const
260 tvm_planarconfig[] = {
261 TV(PLANARCONFIG_,CONTIG),
262 TV(PLANARCONFIG_,SEPARATE),
263 { NULL, 0, },
264 };
265
266 static tagvalmap const
267 tvm_resolutionunit[] = {
268 TV(RESUNIT_,NONE),
269 TV(RESUNIT_,INCH),
270 TV(RESUNIT_,CENTIMETER),
271 { NULL, 0, },
272 };
273
274 static tagvalmap const
275 tvm_sampleformat[] = {
276 TV(SAMPLEFORMAT_,UINT),
277 TV(SAMPLEFORMAT_,INT),
278 TV(SAMPLEFORMAT_,IEEEFP),
279 TV(SAMPLEFORMAT_,VOID),
280 { NULL, 0, },
281 };
282
283 static tagvalmap const
284 tvm_subfiletype[] = {
285 TV(FILETYPE_,REDUCEDIMAGE),
286 TV(FILETYPE_,PAGE),
287 TV(FILETYPE_,MASK),
288 { NULL, 0, },
289 };
290
291 static tagvalmap const
292 tvm_threshholding[] =
293 {
294 TV(THRESHHOLD_,BILEVEL),
295 TV(THRESHHOLD_,HALFTONE),
296 TV(THRESHHOLD_,ERRORDIFFUSE),
297 { NULL, 0, },
298 };
299 #undef TV
300
301 /*
302 * For full functionality, some tags have an implied count
303 * (eg. colormap, dotrange). See TIFFGetField(3).
304 * Also, some are arrays.
305 */
306 #define DECL(a,p,c) { #a, TIFFTAG_##a, p, c, }
307
308 static tagDefinition const
309 tagDefinitions[] = {
310 DECL(ARTIST, putAscii, NULL),
311 DECL(BADFAXLINES, putLong, NULL),
312 DECL(BITSPERSAMPLE, putShort, NULL),
313 DECL(CELLLENGTH, NULL, NULL),
314 DECL(CELLWIDTH, NULL, NULL),
315 DECL(CLEANFAXDATA, putShort, NULL),
316 DECL(COLORMAP, NULL, NULL),
317 DECL(COLORRESPONSEUNIT, NULL, NULL),
318 DECL(COMPRESSION, putShort, tvm_compression),
319 DECL(CONSECUTIVEBADFAXLINES, putLong, NULL),
320 DECL(COPYRIGHT, putAscii, NULL),
321 DECL(DATATYPE, putShort, NULL),
322 DECL(DATETIME, putAscii, NULL),
323 DECL(DCSBALANCEARRAY, NULL, NULL),
324 DECL(DCSCALIBRATIONFD, NULL, NULL),
325 DECL(DCSCLIPRECTANGLE, NULL, NULL),
326 DECL(DCSCORRECTMATRIX, NULL, NULL),
327 DECL(DCSGAMMA, NULL, NULL),
328 DECL(DCSHUESHIFTVALUES, NULL, NULL),
329 DECL(DCSIMAGERTYPE, NULL, NULL),
330 DECL(DCSINTERPMODE, NULL, NULL),
331 DECL(DCSTOESHOULDERPTS, NULL, NULL),
332 DECL(DOCUMENTNAME, putAscii, NULL),
333 DECL(DOTRANGE, NULL, NULL),
334 DECL(EXTRASAMPLES, NULL, NULL),
335 DECL(FAXFILLFUNC, NULL, NULL),
336 DECL(FAXMODE, putLong, tvm_faxmode),
337 DECL(FAXRECVPARAMS, NULL, NULL),
338 DECL(FAXRECVTIME, NULL, NULL),
339 DECL(FAXSUBADDRESS, NULL, NULL),
340 DECL(FEDEX_EDR, NULL, NULL),
341 DECL(FILLORDER, putShort, tvm_fillorder),
342 DECL(FRAMECOUNT, NULL, NULL),
343 DECL(FREEBYTECOUNTS, NULL, NULL),
344 DECL(FREEOFFSETS, NULL, NULL),
345 DECL(GRAYRESPONSECURVE, NULL, NULL),
346 DECL(GRAYRESPONSEUNIT, NULL, NULL),
347 DECL(GROUP3OPTIONS, putLong, tvm_group3options),
348 DECL(GROUP4OPTIONS, putLong, tvm_group4options),
349 DECL(HALFTONEHINTS, NULL, NULL),
350 DECL(HOSTCOMPUTER, putAscii, NULL),
351 DECL(ICCPROFILE, putCountBytes, NULL),
352 DECL(IMAGEDEPTH, putLong, NULL),
353 DECL(IMAGEDESCRIPTION, putAscii, NULL),
354 DECL(IMAGELENGTH, putLong, NULL),
355 DECL(IMAGEWIDTH, putLong, NULL),
356 DECL(INKNAMES, putAscii, NULL),
357 DECL(INKSET, putShort, tvm_inkset),
358 DECL(IT8BITSPEREXTENDEDRUNLENGTH, NULL, NULL),
359 DECL(IT8BITSPERRUNLENGTH, NULL, NULL),
360 DECL(IT8BKGCOLORINDICATOR, NULL, NULL),
361 DECL(IT8BKGCOLORVALUE, NULL, NULL),
362 DECL(IT8COLORCHARACTERIZATION, NULL, NULL),
363 DECL(IT8COLORSEQUENCE, NULL, NULL),
364 DECL(IT8COLORTABLE, NULL, NULL),
365 DECL(IT8HEADER, NULL, NULL),
366 DECL(IT8IMAGECOLORINDICATOR, NULL, NULL),
367 DECL(IT8IMAGECOLORVALUE, NULL, NULL),
368 DECL(IT8PIXELINTENSITYRANGE, NULL, NULL),
369 DECL(IT8RASTERPADDING, NULL, NULL),
370 DECL(IT8SITE, NULL, NULL),
371 DECL(IT8TRANSPARENCYINDICATOR, NULL, NULL),
372 DECL(JBIGOPTIONS, NULL, NULL),
373 DECL(JPEGACTABLES, NULL, NULL),
374 DECL(JPEGCOLORMODE, NULL, NULL),
375 DECL(JPEGDCTABLES, NULL, NULL),
376 DECL(JPEGIFBYTECOUNT, NULL, NULL),
377 DECL(JPEGIFOFFSET, NULL, NULL),
378 DECL(JPEGLOSSLESSPREDICTORS, NULL, NULL),
379 DECL(JPEGPOINTTRANSFORM, NULL, NULL),
380 DECL(JPEGPROC, NULL, NULL),
381 DECL(JPEGQTABLES, NULL, NULL),
382 DECL(JPEGQUALITY, NULL, NULL),
383 DECL(JPEGRESTARTINTERVAL, NULL, NULL),
384 DECL(JPEGTABLES, NULL, NULL),
385 DECL(JPEGTABLESMODE, NULL, NULL),
386 DECL(MAKE, putAscii, NULL),
387 DECL(MATTEING, putShort, NULL),
388 DECL(MAXSAMPLEVALUE, putShort, NULL),
389 DECL(MINSAMPLEVALUE, putShort, NULL),
390 DECL(MODEL, putAscii, NULL),
391 DECL(NUMBEROFINKS, NULL, NULL),
392 DECL(ORIENTATION, putShort, tvm_orientation),
393 DECL(OSUBFILETYPE, NULL, NULL),
394 DECL(PAGENAME, putAscii, NULL),
395 DECL(PAGENUMBER, NULL, NULL),
396 DECL(PHOTOMETRIC, putShort, tvm_photometric),
397 DECL(PHOTOSHOP, NULL, NULL),
398 DECL(PIXAR_FOVCOT, NULL, NULL),
399 DECL(PIXAR_IMAGEFULLLENGTH, NULL, NULL),
400 DECL(PIXAR_IMAGEFULLWIDTH, NULL, NULL),
401 DECL(PIXAR_MATRIX_WORLDTOCAMERA, NULL, NULL),
402 DECL(PIXAR_MATRIX_WORLDTOSCREEN, NULL, NULL),
403 DECL(PIXAR_TEXTUREFORMAT, NULL, NULL),
404 DECL(PIXAR_WRAPMODES, NULL, NULL),
405 DECL(PIXARLOGDATAFMT, NULL, NULL),
406 DECL(PIXARLOGQUALITY, NULL, NULL),
407 DECL(PLANARCONFIG, putShort, tvm_planarconfig),
408 DECL(PREDICTOR, putShort, NULL),
409 DECL(PRIMARYCHROMATICITIES, NULL, NULL),
410 DECL(REFERENCEBLACKWHITE, NULL, NULL),
411 DECL(REFPTS, NULL, NULL),
412 DECL(REGIONAFFINE, NULL, NULL),
413 DECL(REGIONTACKPOINT, NULL, NULL),
414 DECL(REGIONWARPCORNERS, NULL, NULL),
415 DECL(RESOLUTIONUNIT, putShort, tvm_resolutionunit),
416 DECL(RICHTIFFIPTC, NULL, NULL),
417 DECL(ROWSPERSTRIP, putLong, NULL),
418 DECL(SAMPLEFORMAT, putShort, tvm_sampleformat),
419 DECL(SAMPLESPERPIXEL, putShort, NULL),
420 DECL(SGILOGDATAFMT, NULL, NULL),
421 DECL(SMAXSAMPLEVALUE, NULL, NULL),
422 DECL(SMINSAMPLEVALUE, NULL, NULL),
423 DECL(SOFTWARE, putAscii, NULL),
424 DECL(STONITS, NULL, NULL),
425 DECL(STRIPBYTECOUNTS, NULL, NULL),
426 DECL(STRIPOFFSETS, NULL, NULL),
427 DECL(SUBFILETYPE, putLong, tvm_subfiletype),
428 DECL(SUBIFD, NULL, NULL),
429 DECL(TARGETPRINTER, putAscii, NULL),
430 DECL(THRESHHOLDING, putShort, tvm_threshholding),
431 DECL(TILEBYTECOUNTS, NULL, NULL),
432 DECL(TILEDEPTH, putLong, NULL),
433 DECL(TILELENGTH, putLong, NULL),
434 DECL(TILEOFFSETS, NULL, NULL),
435 DECL(TILEWIDTH, putLong, NULL),
436 DECL(TRANSFERFUNCTION, NULL, NULL),
437 DECL(WHITEPOINT, NULL, NULL),
438 DECL(WRITERSERIALNUMBER, NULL, NULL),
439 DECL(XPOSITION, putRational, NULL),
440 DECL(XRESOLUTION, putRational, NULL),
441 DECL(YCBCRCOEFFICIENTS, NULL, NULL),
442 DECL(YCBCRPOSITIONING, NULL, NULL),
443 DECL(YCBCRSUBSAMPLING, NULL, NULL),
444 DECL(YPOSITION, putRational, NULL),
445 DECL(YRESOLUTION, putRational, NULL),
446 DECL(ZIPQUALITY, NULL, NULL),
447 };
448 #undef DECL
449
450
451
452 const tagDefinition *
453 tagDefFind(const char * const name) {
454
455 unsigned int i;
456
457 for (i = 0;
458 i < ARRAY_SIZE(tagDefinitions) && tagDefinitions[i].name;
459 ++i) {
460 if (strcaseeq(tagDefinitions[i].name, name))
461 return &tagDefinitions[i];
462 }
463
464 return NULL;
465 }
466
467
468