File: xtif_dir.c

package info (click to toggle)
kdc2tiff 0.35-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 556 kB
  • sloc: cpp: 3,107; ansic: 832; sh: 330; makefile: 23
file content (491 lines) | stat: -rw-r--r-- 14,931 bytes parent folder | download | duplicates (8)
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
 * xtif_dir.c
 *
 * Extended TIFF Directory Tag Support.
 *
 *  You may use this file as a template to add your own
 *  extended tags to the library. Only the parts of the code
 *  marked with "XXX" require modification. Three example tags
 *  are shown; you should replace them with your own.
 *
 *  Author: Niles D. Ritter
 */
 
#include <config.h>
#include <stdio.h>

#include "xtiffiop.h"

/*  Tiff info structure.
 *
 *     Entry format:
 *        { TAGNUMBER, ReadCount, WriteCount, DataType, FIELDNUM, 
 *          OkToChange, PassDirCountOnSet, AsciiName }
 *
 *     For ReadCount, WriteCount, -1 = unknown; used for mult-valued
 *         tags and ASCII.
 */


static const TIFFFieldInfo xtiffFieldInfo[] = {
  { TIFFTAG_COPYRIGHT,    -1, -1, TIFF_ASCII,     FIELD_33432, TRUE, FALSE, "copyright" },

  { TIFFTAG_KDC_33423,    -1, -1, TIFF_ASCII,     FIELD_33423, TRUE, FALSE, "kdc_33423" },
  { TIFFTAG_KDC_33424,     1, -1, TIFF_LONG,      FIELD_33424, TRUE, FALSE, "kdc_33424" },
  { TIFFTAG_KDC_33434,     1, -1, TIFF_RATIONAL,  FIELD_33434, TRUE, FALSE, "kdc_33434" },
  { TIFFTAG_KDC_33437,     1, -1, TIFF_RATIONAL,  FIELD_33437, TRUE, FALSE, "kdc_33437" },
  { TIFFTAG_KDC_34850,     1, -1, TIFF_SHORT,     FIELD_34850, TRUE, FALSE, "kdc_34850" },
  { TIFFTAG_KDC_34859,     1, -1, TIFF_SHORT,     FIELD_34859, TRUE, FALSE, "kdc_34859" },
  { TIFFTAG_KDC_DATETIME, -1, -1, TIFF_ASCII,     FIELD_36867, TRUE, FALSE, "kdc_datetime" },
  { TIFFTAG_KDC_37382,    -1, -1, TIFF_RATIONAL,  FIELD_37382, TRUE, FALSE, "kdc_37382" },
  { TIFFTAG_KDC_37385,     1, -1, TIFF_SHORT,     FIELD_37385, TRUE, FALSE, "kdc_37385" },
  { TIFFTAG_KDC_37386,     1, -1, TIFF_RATIONAL,  FIELD_37386, TRUE, FALSE, "kdc_37386" },
  { TIFFTAG_KDC_37393,     1, -1, TIFF_SHORT,     FIELD_37393, TRUE, FALSE, "kdc_37393" },
  { TIFFTAG_KDC_37398,     4, -1, TIFF_BYTE,      FIELD_37398, TRUE, FALSE, "kdc_37398" },
  { TIFFTAG_KDC_37399,     1, -1, TIFF_SHORT,     FIELD_37399, TRUE, FALSE, "kdc_37399" },
  { TIFFTAG_KDC_33421,     2, -1, TIFF_SHORT,     FIELD_33421, TRUE, FALSE, "kdc_33421" },
  { TIFFTAG_KDC_33422,     4, -1, TIFF_BYTE,      FIELD_33422, TRUE, FALSE, "kdc_33422" },
  { TIFFTAG_KDC_37122,    -1, -1, TIFF_RATIONAL,  FIELD_37122, TRUE, FALSE, "kdc_37122" },
  { TIFFTAG_KDC_37400,    12, -1, TIFF_SRATIONAL, FIELD_37400, TRUE, FALSE, "kdc_37400" }
};
#define	N(a)	(sizeof (a) / sizeof (a[0]))


static void
_XTIFFPrintDirectory(TIFF* tif, FILE* fd, long flags) {

  xtiff *xt = XTIFFDIR(tif);
  XTIFFDirectory *xd = &xt->xtif_dir;

  /* call the inherited method */
  if (PARENT(xt,printdir))
    (PARENT(xt,printdir))(tif,fd,flags);
  
  /* XXX Add field printing here. Replace the three example
   *    tags implemented below with your own.
   */

  if (TIFFFieldSet(tif,FIELD_33432)) {
    _TIFFprintAsciiTag(fd,"Copyright",xd->xd_copyright);
  }

  fprintf(fd,"--KDC Specific Tags--\n");

  if (TIFFFieldSet(tif,FIELD_33423)) {
    _TIFFprintAsciiTag(fd,"Battery Status",xd->xd_kdc_33423);
  }
  if (TIFFFieldSet(tif,FIELD_36867)) {
    _TIFFprintAsciiTag(fd,"Date & Time",xd->xd_kdc_36867);
  }

  if (TIFFFieldSet(tif,FIELD_33424)) {
    fprintf(fd,"  33424: %ld\n", xd->xd_kdc_33424);
  }
  if (TIFFFieldSet(tif,FIELD_33434)) {
    fprintf(fd,"  33434: %ld/%ld\n", xd->xd_kdc_33434[0],xd->xd_kdc_33434[1]);
  }
  if (TIFFFieldSet(tif,FIELD_33437)) {
    fprintf(fd,"  33437: %ld/%ld\n", xd->xd_kdc_33437[0],xd->xd_kdc_33437[1]);
  }
  if (TIFFFieldSet(tif,FIELD_34850)) {
    fprintf(fd,"  34850: %d\n", xd->xd_kdc_34850);
  }
  if (TIFFFieldSet(tif,FIELD_34859)) {
    fprintf(fd,"  34859: %d\n", xd->xd_kdc_34859);
  }
  if (TIFFFieldSet(tif,FIELD_37382)) {
    //    fprintf(fd,"  37382: %ld/%ld\n", xd->xd_kdc_37382[0],xd->xd_kdc_37382[1]);
  }
  if (TIFFFieldSet(tif,FIELD_37385)) {
    fprintf(fd,"  37385: %d\n", xd->xd_kdc_37385);
  }
  if (TIFFFieldSet(tif,FIELD_37386)) {
    //    fprintf(fd,"  37386: %ld/%ld\n", xd->xd_kdc_37386[0],xd->xd_kdc_37386[1]);
  }
  if (TIFFFieldSet(tif,FIELD_37393)) {
    fprintf(fd,"  37393: %d\n", xd->xd_kdc_37393);
  }
  if (TIFFFieldSet(tif,FIELD_37398)) {
    unsigned char* u = (unsigned char*)xd->xd_kdc_37398;
    fprintf(fd,"  37398: %d %d %d %d\n", u[0],u[1],u[2],u[3]);
  }
  if (TIFFFieldSet(tif,FIELD_37399)) {
    fprintf(fd,"  37399: %d\n", xd->xd_kdc_37399);
  }
  if (TIFFFieldSet(tif,FIELD_33421)) {
    fprintf(fd,"  33421: %d %d\n", xd->xd_kdc_33421[0],xd->xd_kdc_33421[1]);
  }
  if (TIFFFieldSet(tif,FIELD_33422)) {
    unsigned char* u = (unsigned char*)xd->xd_kdc_33422;
    fprintf(fd,"  33422: %d %d %d %d\n", u[0],u[1],u[2],u[3]);
  }
  if (TIFFFieldSet(tif,FIELD_37122)) {
    //    fprintf(fd,"  37122: %d\n", xd->xd_kdc_37122);
  }
  if (TIFFFieldSet(tif,FIELD_37400)) {
    fprintf(fd,"  37400: %ld/%ld\n", xd->xd_kdc_37400[0],xd->xd_kdc_37400[1]);
  }

}

static int
_XTIFFVSetField(TIFF* tif, ttag_t tag, va_list ap) {

  xtiff *xt = XTIFFDIR(tif);
  XTIFFDirectory* xd = &xt->xtif_dir;
  int status = 1;
  /*uint32 v32=0;
    int v=0;*/
  /*va_list ap1 = ap;*/
  
  //fprintf(stderr,"_XTIFFVSetField() entered %d\n",tag);

  /* va_start is called by the calling routine */
  
  switch (tag) {
    /* 
     * XXX put your extended tags here; replace the implemented
     *     example tags with your own. 
     */
  case TIFFTAG_KDC_33423:  /*(0x828f) ASCII (2) 6<Full\0\0>*/
    _TIFFsetString(&xd->xd_kdc_33423, va_arg(ap, char*));
    break;
  case TIFFTAG_KDC_33424:  /*(0x8290) LONG (4) 1<1002>*/
    xd->xd_kdc_33424 = va_arg(ap, uint32);
    break;
  case TIFFTAG_KDC_33434:  /*(0x829a) RATIONAL (5) 1<0.01069>*/
//    _TIFFsetLongArray(&xd->xd_kdc_33434, va_arg(ap, uint32*),2);    
    break;
  case TIFFTAG_KDC_33437:  /*(0x829d) RATIONAL (5) 1<2.5>*/
//    _TIFFsetLongArray(&xd->xd_kdc_33437, va_arg(ap, uint32*),2);
    break;
  case TIFFTAG_KDC_34850:  /*(0x8822) SHORT (3) 1<2>*/
    xd->xd_kdc_34850 = va_arg(ap, uint16);
    break;
  case TIFFTAG_KDC_34859:  /*(0x882b) SHORT (3) 1<0>*/
    xd->xd_kdc_34859 = va_arg(ap, uint16);
    break;
  case TIFFTAG_KDC_DATETIME:  /*(0x9003) ASCII (2) 20<1994:03:25 00:00:25\0>*/
    _TIFFsetString(&xd->xd_kdc_36867, va_arg(ap, char*));
    break;
  case TIFFTAG_KDC_37382:  /*(0x9206) RATIONAL (5) 1<0.7>*/
//      _TIFFsetLongArray(&xd->xd_kdc_37382, va_arg(ap, uint32*),2);
    break;
  case TIFFTAG_KDC_37385:  /*(0x9209) SHORT (3) 1<31>*/
    xd->xd_kdc_37385 = va_arg(ap, uint16);
    break;
  case TIFFTAG_KDC_37386:  /*(0x920a) RATIONAL (5) 1<100>*/
//    _TIFFsetLongArray(&xd->xd_kdc_37386, va_arg(ap, uint32*),2);
    break;
  case TIFFTAG_KDC_37393:  /*(0x9211) SHORT (3) 1<1>*/
    xd->xd_kdc_37393 = va_arg(ap, uint16);
    break;
  case TIFFTAG_KDC_37398:  /*(0x9216) BYTE (1) 4<00 00 00 0x1>*/
    _TIFFsetByteArray(&xd->xd_kdc_37398, va_arg(ap, void*),4);
    break;
  case TIFFTAG_KDC_37399:  /*(0x9217) SHORT (3) 1<2>*/
    xd->xd_kdc_37399 = va_arg(ap, uint16);
    break;
  case TIFFTAG_KDC_33421:  /*(0x828d) SHORT (3) 2<2 2>*/
    _TIFFsetShortArray(&xd->xd_kdc_33421, va_arg(ap, uint16*),2);
    break;
  case TIFFTAG_KDC_33422:  /*(0x828e) BYTE (1) 4<0x1 0x2 00 0x1>*/
    _TIFFsetByteArray(&xd->xd_kdc_33422, va_arg(ap, void*),4);
    break;
  case TIFFTAG_KDC_37122:  /*(0x9102) RATIONAL (5) 1<0.75>*/
//    _TIFFsetLongArray(&xd->xd_kdc_37122, va_arg(ap, uint32*),2);
    break;
  case TIFFTAG_KDC_37400:  /*(0x9218) SRATIONAL (10) 12<0 -2.02926 1.04202*/
//    _TIFFsetLongArray(&xd->xd_kdc_37400, va_arg(ap, uint32*),24);
    break;
  case TIFFTAG_COPYRIGHT:
    _TIFFsetString(&xd->xd_copyright, va_arg(ap, char*));
    break;

  default:
    /* call the inherited method */
    return (PARENT(xt,vsetfield))(tif,tag,ap);
    break;
  }
  if (status) {
    /* we have to override the print method here,
     * after the compression tags have gotten to it.
     * This makes sense because the only time we would
     * need the extended print method is if an extended
     * tag is set by the reader.
     */
    if (!(xt->xtif_flags & XTIFFP_PRINT)) {
      PARENT(xt,printdir) =  TIFFMEMBER(tif,printdir);
      TIFFMEMBER(tif,printdir) = _XTIFFPrintDirectory;
      xt->xtif_flags |= XTIFFP_PRINT;
    }
    TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit);
    tif->tif_flags |= TIFF_DIRTYDIRECT;
  }
  va_end(ap);
  return (status);

  /*
 badvalue:
  TIFFError(tif->tif_name, "%d: Bad value for \"%s\"", v,
	    _TIFFFieldWithTag(tif, tag)->field_name);
  va_end(ap);
  return (0);
 badvalue32:
  TIFFError(tif->tif_name, "%ld: Bad value for \"%s\"", v32,
	    _TIFFFieldWithTag(tif, tag)->field_name);
  va_end(ap);
  return (0);
  */
}


static int
_XTIFFVGetField(TIFF* tif, ttag_t tag, va_list ap) {

  xtiff *xt = XTIFFDIR(tif);
  XTIFFDirectory* xd = &xt->xtif_dir;

  switch (tag) {
    /* 
     * XXX put your extended tags here; replace the implemented
     *     example tags with your own.
     */
  case TIFFTAG_KDC_33423:  /*(0x828f) ASCII (2) 6<Full\0\0>*/
    *va_arg(ap, char**) = xd->xd_kdc_33423;
    break;
  case TIFFTAG_KDC_33424:  /*(0x8290) LONG (4) 1<1002>*/
    *va_arg(ap, uint32*) = xd->xd_kdc_33424;
    break;
  case TIFFTAG_KDC_33434:  /*(0x829a) RATIONAL (5) 1<0.01069>*/
    *va_arg(ap, uint32**) = xd->xd_kdc_33434;
    break;
  case TIFFTAG_KDC_33437:  /*(0x829d) RATIONAL (5) 1<2.5>*/
    *va_arg(ap, uint32**) = xd->xd_kdc_33437;
    break;
  case TIFFTAG_KDC_34850:  /*(0x8822) SHORT (3) 1<2>*/
    *va_arg(ap, uint16*) = xd->xd_kdc_34850;
    break;
  case TIFFTAG_KDC_34859:  /*(0x882b) SHORT (3) 1<0>*/
    *va_arg(ap, uint16*) = xd->xd_kdc_34859;
    break;
  case TIFFTAG_KDC_DATETIME:  /*(0x9003) ASCII (2) 20<1994:03:25 00:00:25\0>*/
    *va_arg(ap, char**) = xd->xd_kdc_36867;
    break;
  case TIFFTAG_KDC_37382:  /*(0x9206) RATIONAL (5) 1<0.7>*/
    *va_arg(ap, uint32**) = xd->xd_kdc_37382;
    break;
  case TIFFTAG_KDC_37385:  /*(0x9209) SHORT (3) 1<31>*/
    *va_arg(ap, uint16*) = xd->xd_kdc_37385;
    break;
  case TIFFTAG_KDC_37386:  /*(0x920a) RATIONAL (5) 1<100>*/
    *va_arg(ap, uint32**) = xd->xd_kdc_37386;
    break;
  case TIFFTAG_KDC_37393:  /*(0x9211) SHORT (3) 1<1>*/
    *va_arg(ap, uint16*) = xd->xd_kdc_37393;
    break;
  case TIFFTAG_KDC_37398:  /*(0x9216) BYTE (1) 4<00 00 00 0x1>*/
    *va_arg(ap, void**) = xd->xd_kdc_37398;
    break;
  case TIFFTAG_KDC_37399:  /*(0x9217) SHORT (3) 1<2>*/
    *va_arg(ap, uint16*) = xd->xd_kdc_37399;
    break;
  case TIFFTAG_KDC_33421:  /*(0x828d) SHORT (3) 2<2 2>*/
    *va_arg(ap, uint16**) = xd->xd_kdc_33421; 
    break;
  case TIFFTAG_KDC_33422:  /*(0x828e) BYTE (1) 4<0x1 0x2 00 0x1>*/
    *va_arg(ap, void**) = xd->xd_kdc_33422;
    break;
  case TIFFTAG_KDC_37122:  /*(0x9102) RATIONAL (5) 1<0.75>*/
    *va_arg(ap, uint32**) = xd->xd_kdc_37122; 
    break;
  case TIFFTAG_KDC_37400:  /*(0x9218) SRATIONAL (10) 12<0 -2.02926 1.04202*/
    *va_arg(ap, uint32**) = xd->xd_kdc_37400; 
    break;
  case TIFFTAG_COPYRIGHT:
    *va_arg(ap, char**) = xd->xd_copyright;
    break;

  default:
    /* return inherited method */
    return (PARENT(xt,vgetfield))(tif,tag,ap);
    break;
  }
  return (1);
}

#define	CleanupField(member) {		\
    if (xd->member) {			\
	_TIFFfree(xd->member);		\
	xd->member = 0;			\
    }					\
}
/*
 * Release storage associated with a directory.
 */
static void
_XTIFFFreeDirectory(xtiff* xt) {

  XTIFFDirectory* xd = &xt->xtif_dir;
  
  /* 
   *  XXX - Purge all Your allocated memory except
   *  for the xtiff directory itself. This includes
   *  all fields that require a _TIFFsetXXX call in
   *  _XTIFFVSetField().
   */
  
  CleanupField(xd_kdc_33423);
  CleanupField(xd_kdc_33434);  /* RATIONAL */
  CleanupField(xd_kdc_33437);  /* RATIONAL */
  CleanupField(xd_kdc_36867);
  CleanupField(xd_kdc_37382);  /* RATIONAL */
  CleanupField(xd_kdc_37386);  /* RATIONAL */
  CleanupField(xd_kdc_37398);
  CleanupField(xd_kdc_33421);
  CleanupField(xd_kdc_33422);
  CleanupField(xd_kdc_37122);  /* RATIONAL */
  CleanupField(xd_kdc_37400);  /* 24 x SRATIONAL */
  CleanupField(xd_copyright);
  
}
#undef CleanupField

static void _XTIFFLocalDefaultDirectory(TIFF *tif) {

  xtiff *xt = XTIFFDIR(tif);
  /*XTIFFDirectory* xd = &xt->xtif_dir;*/
  
  /* Install the extended Tag field info */
  _TIFFMergeFieldInfo(tif, xtiffFieldInfo, N(xtiffFieldInfo));
  
  /*
   *  free up any dynamically allocated arrays
   *  before the new directory is read in.
   */
  
  _XTIFFFreeDirectory(xt);	
  _TIFFmemset(xt,0,sizeof(xtiff));
  
  /* Override the tag access methods */
  
  PARENT(xt,vsetfield) =  TIFFMEMBER(tif,vsetfield);
  TIFFMEMBER(tif,vsetfield) = _XTIFFVSetField;
  PARENT(xt,vgetfield) =  TIFFMEMBER(tif,vgetfield);
  TIFFMEMBER(tif,vgetfield) = _XTIFFVGetField;
  
  /* 
   * XXX Set up any default values here.
   */
  
}



/**********************************************************************
 *    Nothing below this line should need to be changed.
 **********************************************************************/

static TIFFExtendProc _ParentExtender;

/*
 *  This is the callback procedure, and is
 *  called by the DefaultDirectory method
 *  every time a new TIFF directory is opened.
 */

static void
_XTIFFDefaultDirectory(TIFF *tif)
{

	xtiff *xt;

	/* Allocate Directory Structure if first time, and install it */
	if (!(tif->tif_flags & XTIFF_INITIALIZED))
	{
		xt = _TIFFmalloc(sizeof(xtiff));
		if (!xt)
		{
			/* handle memory allocation failure here ! */
			return;
		}
		_TIFFmemset(xt,0,sizeof(xtiff));
		/*
		 * Install into TIFF structure.
		 */
		TIFFMEMBER(tif,clientdir) = (tidata_t)xt;
		tif->tif_flags |= XTIFF_INITIALIZED; /* dont do this again! */
	}
	
	/* set up our own defaults */
	_XTIFFLocalDefaultDirectory(tif);

	/* Since an XTIFF client module may have overridden
	 * the default directory method, we call it now to
	 * allow it to set up the rest of its own methods.
         */

	if (_ParentExtender) 
		(*_ParentExtender)(tif);

}

/*
 *  XTIFF Initializer -- sets up the callback
 *   procedure for the TIFF module.
 */

static
void _XTIFFInitialize(void)
{

	static int first_time=1;

	if (! first_time) return; /* Been there. Done that. */
	first_time = 0;
	
	/* Grab the inherited method and install */
	_ParentExtender = TIFFSetTagExtender(_XTIFFDefaultDirectory);
}


/*
 * Public File I/O Routines.
 */
TIFF*
XTIFFOpen(const char* name, const char* mode)
{

	/* Set up the callback */
	_XTIFFInitialize();	
	
	/* Open the file; the callback will set everything up
	 */
	return TIFFOpen(name, mode);
}

TIFF*
XTIFFFdOpen(int fd, const char* name, const char* mode)
{
	/* Set up the callback */
	_XTIFFInitialize();	

	/* Open the file; the callback will set everything up
	 */
	return TIFFFdOpen(fd, name, mode);
}


void
XTIFFClose(TIFF *tif)
{

	xtiff *xt = XTIFFDIR(tif);

	/* call inherited function first */
	TIFFClose(tif);
	
	/* Free up extended allocated memory */
	_XTIFFFreeDirectory(xt);
	_TIFFfree(xt);
}