summaryrefslogtreecommitdiff
path: root/tiff/contrib/tags/xtif_dir.c
blob: e67a6abf78909846e50afa8c4b4f302da9e22115 (plain)
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
/*
 * 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 "xtiffiop.h"
#include <stdio.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[] = {
  
  /* XXX Replace these example tags with your own extended tags */
    { TIFFTAG_EXAMPLE_MULTI,	-1,-1, TIFF_DOUBLE,	FIELD_EXAMPLE_MULTI,
      TRUE,	TRUE,	"MyMultivaluedTag" },
    { TIFFTAG_EXAMPLE_SINGLE,	 1, 1, TIFF_LONG,	FIELD_EXAMPLE_SINGLE,
      TRUE,	FALSE,	"MySingleLongTag" },
    { TIFFTAG_EXAMPLE_ASCII,	-1,-1, TIFF_ASCII,	FIELD_EXAMPLE_ASCII,
      TRUE,	FALSE,	"MyAsciiTag" },
};
#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;
	int i,num;

	/* 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.
         */

	fprintf(fd,"--My Example Tags--\n");

	/* Our first example tag may have a lot of values, so we
         * will only print them out if the TIFFPRINT_MYMULTIDOUBLES
         * flag is passed into the print method.
         */
	if (TIFFFieldSet(tif,FIELD_EXAMPLE_MULTI))
	{
		fprintf(fd, "  My Multi-Valued Doubles:");
		if (flags & TIFFPRINT_MYMULTIDOUBLES) 
		{
			double *value = xd->xd_example_multi;
	
			num = xd->xd_num_multi;
			fprintf(fd,"(");
			for (i=0;i<num;i++) fprintf(fd, " %lg", *value++);
			fprintf(fd,")\n");
		} else
			fprintf(fd, "(present)\n");
	}

	if (TIFFFieldSet(tif,FIELD_EXAMPLE_SINGLE))
	{
		fprintf(fd, "  My Single Long Tag:  %lu\n", xd->xd_example_single);
	}

	if (TIFFFieldSet(tif,FIELD_EXAMPLE_ASCII))
	{
		_TIFFprintAsciiTag(fd,"My ASCII Tag",
			 xd->xd_example_ascii);
	}
}

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 i=0, v=0;
	va_list ap1 = ap;

	/* 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_EXAMPLE_MULTI:
		/* multi-valued tags need to store the count as well */
		xd->xd_num_multi = (uint16) va_arg(ap, int);
		_TIFFsetDoubleArray(&xd->xd_example_multi, va_arg(ap, double*),
			(long) xd->xd_num_multi);
		break;
	case TIFFTAG_EXAMPLE_SINGLE:
		xd->xd_example_single = va_arg(ap, uint32);
		break;
	case TIFFTAG_EXAMPLE_ASCII:
		_TIFFsetString(&xd->xd_example_ascii, 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:
	TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "%d: Bad value for \"%s\"", v,
	    _TIFFFieldWithTag(tif, tag)->field_name);
	va_end(ap);
	return (0);
badvalue32:
	TIFFErrorExt(tif->tif_clientdata, 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_EXAMPLE_MULTI:
		*va_arg(ap, uint16*) = xd->xd_num_multi;
		*va_arg(ap, double**) = xd->xd_example_multi;
		break;
	case TIFFTAG_EXAMPLE_ASCII:
		*va_arg(ap, char**) = xd->xd_example_ascii;
		break;
	case TIFFTAG_EXAMPLE_SINGLE:
		*va_arg(ap, uint32*) = xd->xd_example_single;
		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_example_multi);
	CleanupField(xd_example_ascii);
	
}
#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.
	 */
	
	xd->xd_example_single = 234;
}



/**********************************************************************
 *    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 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);
}
/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 8
 * fill-column: 78
 * End:
 */