about summary refs log tree commit diff
path: root/converter/other/tiff.c
blob: a498571b0cf5c81575c29784bbec9a36cf9fa764 (plain) (blame)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*============================================================================
                                 tiff.c
==============================================================================

  Stuff specific to the TIFF format -- essentially and extension to libtiff.

============================================================================*/

#include <string.h>

#ifdef VMS
#ifdef SYSV
#undef SYSV
#endif
#include <tiffioP.h>
#endif
#include <tiffio.h>

#include "pm_c_util.h"
#include "nstring.h"
#include "mallocvar.h"
#include "pm.h"
#include "pm_tiff.h"



static uint32
number(const char * const value,
       tagvalmap    const tagvallist[]) {
    
    char * ep;
    long num;

    if (strlen(value) == 0)
        pm_error("null string where numeric tag value or enumerated tag "
                 "value name expected");
    
    num = strtol(value, &ep, 10);
    if (*ep != '\0') {
        /* It's not a numeric string, so it must be an enumerated value name */
        unsigned int i;
        for (i = 0; tagvallist[i].name; ++i) {
            if (STRCASEEQ(value, tagvallist[i].name))
                return tagvallist[i].value;
        }
        pm_error("'%s' is neither a number nor a valid value name", value);
        return 0; /* avoid compiler warning */
    } else
        return num;
}



static double
dnumber(const char * const value) {

    char * ep;
    double num;
    
    num = strtod(value, &ep);
    if (ep == value || *ep != '\0')
        pm_error("Bad floating point number %s", value);

    return num;
}



#if 0
static void
putByte(TIFF *          const tifP,
        unsigned int    const tagnum,
        const char *    const value,
        tagvalmap const tagvallist[]) {

    uint16 const num = number(value, tagvallist);

    TIFFSetField(tifP, tagnum, num);
}
#endif



static void
putAscii(TIFF *       const tifP,
         unsigned int const tagnum,
         const char * const value,
         tagvalmap    const tagvallist[]) {

    TIFFSetField(tifP, tagnum, value);
}



static void
putShort(TIFF *       const tifP,
         unsigned     const tagnum,
         const char * const value,
         tagvalmap    const tagvallist[]) {

    uint16 const num = number(value, tagvallist);

    TIFFSetField(tifP, tagnum, num);
}



static void
putLong(TIFF *       const tifP,
        unsigned int const tagnum,
        const char * const value,
        tagvalmap    const tagvallist[]) {

    uint32 const num = number(value, tagvallist);

    TIFFSetField(tifP, tagnum, num);
}



static void
putRational(TIFF *       const tifP,
            unsigned     const tagnum,
            const char * const value,
            tagvalmap    const tagvallist[]) {

    float const num = dnumber(value);

    TIFFSetField(tifP, tagnum, num);
}



static void
putCountBytes(TIFF *       const tifP,
              unsigned     const tagnum,
              const char * const value,
              tagvalmap    const tagvallist[]) {

    uint32 const len = strlen(value)/2;

    char * data;
    uint32 i;
    bool   error;

    MALLOCARRAY_NOFAIL(data, len);

    for (i = 0, error = FALSE; !error && i < len; ++i) {
        unsigned int val;
        int    nb;
        int    rc;
        rc = sscanf(&value[i*2], "%2x%n", &val, &nb);
        if (rc != 1 || nb != 2)
            error = TRUE;
        else
            data[i] = val;
    }
    if (!error)
        TIFFSetField(tifP, tagnum, len, (void *)data);

    free(data);
}



#define TV(p,a) { #a, p##a, }

static tagvalmap const 
tvm_compression[] = {
    TV(COMPRESSION_,NONE),
    TV(COMPRESSION_,CCITTRLE),
    TV(COMPRESSION_,CCITTFAX3),
    TV(COMPRESSION_,CCITTFAX4),
    TV(COMPRESSION_,LZW),
    TV(COMPRESSION_,OJPEG),
    TV(COMPRESSION_,JPEG),
    TV(COMPRESSION_,NEXT),
    TV(COMPRESSION_,CCITTRLEW),
    TV(COMPRESSION_,PACKBITS),
    TV(COMPRESSION_,THUNDERSCAN),
    TV(COMPRESSION_,IT8CTPAD),
    TV(COMPRESSION_,IT8LW),
    TV(COMPRESSION_,IT8MP),
    TV(COMPRESSION_,IT8BL),
    TV(COMPRESSION_,PIXARFILM),
    TV(COMPRESSION_,PIXARLOG),
    TV(COMPRESSION_,DEFLATE),
    TV(COMPRESSION_,ADOBE_DEFLATE),
    TV(COMPRESSION_,DCS),
    TV(COMPRESSION_,JBIG),
    TV(COMPRESSION_,SGILOG),
    TV(COMPRESSION_,SGILOG24),
    { NULL, 0, },
};

static tagvalmap const
tvm_faxmode[] = {
    TV(FAXMODE_,CLASSIC),
    TV(FAXMODE_,NORTC),
    TV(FAXMODE_,NOEOL),
    TV(FAXMODE_,BYTEALIGN),
    TV(FAXMODE_,WORDALIGN),
    TV(FAXMODE_,CLASSF),
    { NULL, 0, },
};

static tagvalmap const
tvm_fillorder[] = {
    TV(FILLORDER_,MSB2LSB),
    TV(FILLORDER_,LSB2MSB),
    { NULL, 0, },
};

static tagvalmap
const tvm_group3options[] = {
    TV(GROUP3OPT_,2DENCODING),
    TV(GROUP3OPT_,UNCOMPRESSED),
    TV(GROUP3OPT_,FILLBITS),
    { NULL, 0, },
};

static tagvalmap
const tvm_group4options[] = {
    TV(GROUP4OPT_,UNCOMPRESSED),
    { NULL, 0, },
};

static tagvalmap
const tvm_inkset[] = {
    TV(INKSET_,CMYK),
    { NULL, 0, },
};

static tagvalmap
const tvm_orientation[] = {
    TV(ORIENTATION_,TOPLEFT),
    TV(ORIENTATION_,TOPRIGHT),
    TV(ORIENTATION_,BOTRIGHT),
    TV(ORIENTATION_,BOTLEFT),
    TV(ORIENTATION_,LEFTTOP),
    TV(ORIENTATION_,RIGHTTOP),
    TV(ORIENTATION_,RIGHTBOT),
    TV(ORIENTATION_,LEFTBOT),
    { NULL, 0, },
};

static tagvalmap const
tvm_photometric[] = {
    TV(PHOTOMETRIC_,MINISWHITE),
    TV(PHOTOMETRIC_,MINISBLACK),
    TV(PHOTOMETRIC_,RGB),
    TV(PHOTOMETRIC_,PALETTE),
    TV(PHOTOMETRIC_,MASK),
    TV(PHOTOMETRIC_,SEPARATED),
    TV(PHOTOMETRIC_,YCBCR),
    TV(PHOTOMETRIC_,CIELAB),
    TV(PHOTOMETRIC_,LOGL),
    TV(PHOTOMETRIC_,LOGLUV),
    { NULL, 0, },
};

static tagvalmap const
tvm_planarconfig[] = {
    TV(PLANARCONFIG_,CONTIG),
    TV(PLANARCONFIG_,SEPARATE),
    { NULL, 0, },
};

static tagvalmap const
tvm_resolutionunit[] = {
    TV(RESUNIT_,NONE),
    TV(RESUNIT_,INCH),
    TV(RESUNIT_,CENTIMETER),
    { NULL, 0, },
};

static tagvalmap const
tvm_sampleformat[] = {
    TV(SAMPLEFORMAT_,UINT),
    TV(SAMPLEFORMAT_,INT),
    TV(SAMPLEFORMAT_,IEEEFP),
    TV(SAMPLEFORMAT_,VOID),
    { NULL, 0, },
};

static tagvalmap const
tvm_subfiletype[] = {
    TV(FILETYPE_,REDUCEDIMAGE),
    TV(FILETYPE_,PAGE),
    TV(FILETYPE_,MASK),
    { NULL, 0, },
};

static tagvalmap const
tvm_threshholding[] =
{
    TV(THRESHHOLD_,BILEVEL),
    TV(THRESHHOLD_,HALFTONE),
    TV(THRESHHOLD_,ERRORDIFFUSE),
    { NULL, 0, },
};
#undef TV

/*
 * For full functionality, some tags have an implied count
 * (eg. colormap, dotrange). See TIFFGetField(3).
 * Also, some are arrays.
 */
#define DECL(a,p,c) { #a, TIFFTAG_##a, p, c, }

static tagDefinition const
tagDefinitions[] = {
    DECL(ARTIST,                  putAscii,    NULL),
    DECL(BADFAXLINES,             putLong,     NULL),
    DECL(BITSPERSAMPLE,           putShort,    NULL),
    DECL(CELLLENGTH,              NULL,        NULL),
    DECL(CELLWIDTH,               NULL,        NULL),
    DECL(CLEANFAXDATA,            putShort,    NULL),
    DECL(COLORMAP,                NULL,        NULL),
    DECL(COLORRESPONSEUNIT,       NULL,        NULL),
    DECL(COMPRESSION,             putShort,    tvm_compression),
    DECL(CONSECUTIVEBADFAXLINES,  putLong,     NULL),
    DECL(COPYRIGHT,               putAscii,    NULL),
    DECL(DATATYPE,                putShort,    NULL),
    DECL(DATETIME,                putAscii,    NULL),
    DECL(DCSBALANCEARRAY,         NULL,        NULL),
    DECL(DCSCALIBRATIONFD,        NULL,        NULL),
    DECL(DCSCLIPRECTANGLE,        NULL,        NULL),
    DECL(DCSCORRECTMATRIX,        NULL,        NULL),
    DECL(DCSGAMMA,                NULL,        NULL),
    DECL(DCSHUESHIFTVALUES,       NULL,        NULL),
    DECL(DCSIMAGERTYPE,           NULL,        NULL),
    DECL(DCSINTERPMODE,           NULL,        NULL),
    DECL(DCSTOESHOULDERPTS,       NULL,        NULL),
    DECL(DOCUMENTNAME,            putAscii,    NULL),
    DECL(DOTRANGE,                NULL,        NULL),
    DECL(EXTRASAMPLES,            NULL,        NULL),
    DECL(FAXFILLFUNC,             NULL,        NULL),
    DECL(FAXMODE,                 putLong,     tvm_faxmode),
    DECL(FAXRECVPARAMS,           NULL,        NULL),
    DECL(FAXRECVTIME,             NULL,        NULL),
    DECL(FAXSUBADDRESS,           NULL,        NULL),
    DECL(FEDEX_EDR,               NULL,        NULL),
    DECL(FILLORDER,               putShort,    tvm_fillorder),
    DECL(FRAMECOUNT,              NULL,        NULL),
    DECL(FREEBYTECOUNTS,          NULL,        NULL),
    DECL(FREEOFFSETS,             NULL,        NULL),
    DECL(GRAYRESPONSECURVE,       NULL,        NULL),
    DECL(GRAYRESPONSEUNIT,        NULL,        NULL),
    DECL(GROUP3OPTIONS,           putLong,     tvm_group3options),
    DECL(GROUP4OPTIONS,           putLong,     tvm_group4options),
    DECL(HALFTONEHINTS,           NULL,        NULL),
    DECL(HOSTCOMPUTER,            putAscii,    NULL),
    DECL(ICCPROFILE,              putCountBytes, NULL),
    DECL(IMAGEDEPTH,              putLong,     NULL),
    DECL(IMAGEDESCRIPTION,        putAscii,    NULL),
    DECL(IMAGELENGTH,             putLong,     NULL),
    DECL(IMAGEWIDTH,              putLong,     NULL),
    DECL(INKNAMES,                putAscii,    NULL),
    DECL(INKSET,                  putShort,    tvm_inkset),
    DECL(IT8BITSPEREXTENDEDRUNLENGTH, NULL,    NULL),
    DECL(IT8BITSPERRUNLENGTH,     NULL,        NULL),
    DECL(IT8BKGCOLORINDICATOR,    NULL,        NULL),
    DECL(IT8BKGCOLORVALUE,        NULL,        NULL),
    DECL(IT8COLORCHARACTERIZATION, NULL,       NULL),
    DECL(IT8COLORSEQUENCE,        NULL,        NULL),
    DECL(IT8COLORTABLE,           NULL,        NULL),
    DECL(IT8HEADER,               NULL,        NULL),
    DECL(IT8IMAGECOLORINDICATOR,  NULL,        NULL),
    DECL(IT8IMAGECOLORVALUE,      NULL,        NULL),
    DECL(IT8PIXELINTENSITYRANGE,  NULL,        NULL),
    DECL(IT8RASTERPADDING,        NULL,        NULL),
    DECL(IT8SITE,                 NULL,        NULL),
    DECL(IT8TRANSPARENCYINDICATOR, NULL,       NULL),
    DECL(JBIGOPTIONS,             NULL,        NULL),
    DECL(JPEGACTABLES,            NULL,        NULL),
    DECL(JPEGCOLORMODE,           NULL,        NULL),
    DECL(JPEGDCTABLES,            NULL,        NULL),
    DECL(JPEGIFBYTECOUNT,         NULL,        NULL),
    DECL(JPEGIFOFFSET,            NULL,        NULL),
    DECL(JPEGLOSSLESSPREDICTORS,  NULL,        NULL),
    DECL(JPEGPOINTTRANSFORM,      NULL,        NULL),
    DECL(JPEGPROC,                NULL,        NULL),
    DECL(JPEGQTABLES,             NULL,        NULL),
    DECL(JPEGQUALITY,             NULL,        NULL),
    DECL(JPEGRESTARTINTERVAL,     NULL,        NULL),
    DECL(JPEGTABLES,              NULL,        NULL),
    DECL(JPEGTABLESMODE,          NULL,        NULL),
    DECL(MAKE,                    putAscii,    NULL),
    DECL(MATTEING,                putShort,    NULL),
    DECL(MAXSAMPLEVALUE,          putShort,    NULL),
    DECL(MINSAMPLEVALUE,          putShort,    NULL),
    DECL(MODEL,                   putAscii,    NULL),
    DECL(NUMBEROFINKS,            NULL,        NULL),
    DECL(ORIENTATION,             putShort,    tvm_orientation),
    DECL(OSUBFILETYPE,            NULL,        NULL),
    DECL(PAGENAME,                putAscii,    NULL),
    DECL(PAGENUMBER,              NULL,        NULL),
    DECL(PHOTOMETRIC,             putShort,    tvm_photometric),
    DECL(PHOTOSHOP,               NULL,        NULL),
    DECL(PIXAR_FOVCOT,            NULL,        NULL),
    DECL(PIXAR_IMAGEFULLLENGTH,   NULL,        NULL),
    DECL(PIXAR_IMAGEFULLWIDTH,    NULL,        NULL),
    DECL(PIXAR_MATRIX_WORLDTOCAMERA, NULL,     NULL),
    DECL(PIXAR_MATRIX_WORLDTOSCREEN, NULL,     NULL),
    DECL(PIXAR_TEXTUREFORMAT,     NULL,        NULL),
    DECL(PIXAR_WRAPMODES,         NULL,        NULL),
    DECL(PIXARLOGDATAFMT,         NULL,        NULL),
    DECL(PIXARLOGQUALITY,         NULL,        NULL),
    DECL(PLANARCONFIG,            putShort,    tvm_planarconfig),
    DECL(PREDICTOR,               putShort,    NULL),
    DECL(PRIMARYCHROMATICITIES,   NULL,        NULL),
    DECL(REFERENCEBLACKWHITE,     NULL,        NULL),
    DECL(REFPTS,                  NULL,        NULL),
    DECL(REGIONAFFINE,            NULL,        NULL),
    DECL(REGIONTACKPOINT,         NULL,        NULL),
    DECL(REGIONWARPCORNERS,       NULL,        NULL),
    DECL(RESOLUTIONUNIT,          putShort,    tvm_resolutionunit),
    DECL(RICHTIFFIPTC,            NULL,        NULL),
    DECL(ROWSPERSTRIP,            putLong,     NULL),
    DECL(SAMPLEFORMAT,            putShort,    tvm_sampleformat),
    DECL(SAMPLESPERPIXEL,         putShort,    NULL),
    DECL(SGILOGDATAFMT,           NULL,        NULL),
    DECL(SMAXSAMPLEVALUE,         NULL,        NULL),
    DECL(SMINSAMPLEVALUE,         NULL,        NULL),
    DECL(SOFTWARE,                putAscii,    NULL),
    DECL(STONITS,                 NULL,        NULL),
    DECL(STRIPBYTECOUNTS,         NULL,        NULL),
    DECL(STRIPOFFSETS,            NULL,        NULL),
    DECL(SUBFILETYPE,             putLong,     tvm_subfiletype),
    DECL(SUBIFD,                  NULL,        NULL),
    DECL(TARGETPRINTER,           putAscii,    NULL),
    DECL(THRESHHOLDING,           putShort,    tvm_threshholding),
    DECL(TILEBYTECOUNTS,          NULL,        NULL),
    DECL(TILEDEPTH,               putLong,     NULL),
    DECL(TILELENGTH,              putLong,     NULL),
    DECL(TILEOFFSETS,             NULL,        NULL),
    DECL(TILEWIDTH,               putLong,     NULL),
    DECL(TRANSFERFUNCTION,        NULL,        NULL),
    DECL(WHITEPOINT,              NULL,        NULL),
    DECL(WRITERSERIALNUMBER,      NULL,        NULL),
    DECL(XPOSITION,               putRational, NULL),
    DECL(XRESOLUTION,             putRational, NULL),
    DECL(YCBCRCOEFFICIENTS,       NULL,        NULL),
    DECL(YCBCRPOSITIONING,        NULL,        NULL),
    DECL(YCBCRSUBSAMPLING,        NULL,        NULL),
    DECL(YPOSITION,               putRational, NULL),
    DECL(YRESOLUTION,             putRational, NULL),
    DECL(ZIPQUALITY,              NULL,        NULL),
};
#undef DECL



const tagDefinition *
tagDefFind(const char * const name) {

    unsigned int i;

    for (i = 0;
         i < ARRAY_SIZE(tagDefinitions) && tagDefinitions[i].name;
         ++i) {
        if (STRCASEEQ(tagDefinitions[i].name, name))
            return &tagDefinitions[i];
    }

    return NULL;
}