summaryrefslogtreecommitdiff
path: root/support/dktools/dk4bift.c
blob: 15ca3317e99fb4d623238be22231e35280271ef1 (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
Copyright (C) 2017-2020, Dirk Krause
SPDX-License-Identifier: BSD-3-Clause
*/

/*
	WARNING: This file was generated by the dkct program (see
	http://dktools.sourceforge.net/ for details).
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4bift.ctr
*/

/**	@file dk4bift.c The dk4bift module.
*/


#line 8 "dk4bift.ctr"
#include "dk4conf.h"




#line 12 "dk4bift.ctr"



#if	DK4_HAVE_TIFF_H

#ifndef	DK4MEM_H_INCLUDED
#include "dk4mem.h"
#endif

#ifndef	DK4BIF_H_INCLUDED
#include "dk4bif.h"
#endif

#ifndef	BIF_H_INCLUDED
#include "bif.h"
#endif

#ifndef	DK4MAAL_H_INCLUDED
#include "dk4maal.h"
#endif

#if DK4_HAVE_ASSERT_H
#ifndef	ASSERT_H_INCLUDED
#include <assert.h>
#define	ASSERT_H_INCLUDED 1
#endif
#endif


void
dk4biftiff_nc_release_per_frame_data(
	void			*tsdata
)
{
	dk4_bif_tiff_per_frame_t	*pfd	= NULL;	/* Per-frame data */
	

#line 48 "dk4bift.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != tsdata);
#endif
	pfd = (dk4_bif_tiff_per_frame_t *)tsdata;
	if (NULL != pfd) {
		if (NULL != pfd->rows) {	

#line 54 "dk4bift.ctr"
			_TIFFfree(pfd->rows);
			pfd->rows = NULL;
		} 

#line 57 "dk4bift.ctr"
		dk4mem_free(pfd);
	}
	

#line 60 "dk4bift.ctr"
}



dk4_bif_t *
dk4biftiff_nc_open_type(
	dkChar	const				*fn,
	dk4_cs_conv_ctx_t	const	*pcsctx,
	dk4_er_t					*erp
)
{
	dk4_er_t					 er;			/* Error report for math op */
	dk4_bif_tiff_per_frame_t	*pfd	= NULL;	/* Per-frame data */
	dk4_bif_t					*back	= NULL;
	TIFF						*tiff	= NULL;	/* TIFF file */
	double						 xrv;			/* Temporary resolution valuej */
	float						 rv;			/* Temporary resolution value */
	uint32						 w;				/* Frame width */
	uint32						 h;				/* Frame height */
	int							 ok		= 0;	/* Flag: Success */
	int							 cc		= 0;	/* Flag: Can continue */
	uint16						 ru;			/* Resolution unit */
	uint16						 orie;			/* Orientation */
	tsize_t						 nby;			/* Byte number to allocate */
	

#line 85 "dk4bift.ctr"
	back = dk4biftool_nc_new_for_one_frame(
		fn, DK4_BIF_TYPE_TIFF, 0, pcsctx, erp
	);
	if (NULL == back) { goto finished; }
	

#line 90 "dk4bift.ctr"

	pfd = dk4mem_new(dk4_bif_tiff_per_frame_t,1,NULL);
	if (NULL == pfd) { goto finished; }
	pfd->rows = NULL;
	

#line 95 "dk4bift.ctr"

	back->cf->tsdata = pfd;

#if	(DK4_CHAR_SIZE > 1) && (DK4_ON_WINDOWS)
	tiff = TIFFOpenW(fn, "rb");
#else
	tiff = TIFFOpen(fn, "rb");
#endif
	if (NULL == tiff) { goto finished; }
	

#line 105 "dk4bift.ctr"

	/*	Traverse all frames
	*/
	ok = 1;
	do {
		cc = 0;
		w  = 0UL;
		h  = 0UL;

		/*	Read image width and height
		*/
		if (1 == TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w)) {
			back->cf->dim_x = w;
		}
		else {			

#line 120 "dk4bift.ctr"
			w = 0UL;
		}
		if (1 == TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h)) {
			back->cf->dim_y = h;
		}
		else {			

#line 126 "dk4bift.ctr"
			h = 0UL;
		}

		/*	Read resolution
		*/
		rv = 0.0;
		if (1 == TIFFGetField(tiff, TIFFTAG_XRESOLUTION, &rv)) {
			back->cf->res_x = rv;	

#line 134 "dk4bift.ctr"
		}
		rv = 0.0;
		if (1 == TIFFGetField(tiff, TIFFTAG_YRESOLUTION, &rv)) {
			back->cf->res_y = rv;	

#line 138 "dk4bift.ctr"
		}
		ru = 0;
		if (1 == TIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &ru)) {
			if (RESUNIT_CENTIMETER == ru) {	

#line 142 "dk4bift.ctr"
				back->cf->res_x *= 2.54;
				back->cf->res_y *= 2.54;
			}
		}
		if ((0.0 >= back->cf->res_x) || (0.0 >= back->cf->res_y)) {
			back->cf->res_x = back->cf->res_y = -1.0;
			

#line 149 "dk4bift.ctr"
		}

		/*	Read orientation, swap x and y coordinates if necessary
		*/
		orie = ORIENTATION_TOPLEFT;
		if (1 == TIFFGetFieldDefaulted(tiff, TIFFTAG_ORIENTATION, &orie)) {
			switch (orie) {
				case 5: case 6: case 7: case 8: {	

#line 157 "dk4bift.ctr"
					back->cf->dim_x = h; back->cf->dim_y = w;
					xrv = back->cf->res_x;
					back->cf->res_x = back->cf->res_y;
					back->cf->res_y = xrv;
				} break;
			}
		}
		else {
			orie = ORIENTATION_TOPLEFT;
		}

		/*	Calculate byte number
		*/
		dk4error_init(&er);
		nby = dk4ma_long_mul(
			dk4ma_long_mul(w, h, &er), 4L, &er
		);

		/*	Read image data
		*/
		if ((DK4_E_NONE == er.ec) && (0L < w) && (0L < h)) {
			pfd->rows = (uint32 *)_TIFFmalloc(nby);
			if (NULL != pfd->rows) {
				if (1 == TIFFReadRGBAImageOriented(tiff,w,h,pfd->rows,orie,0)) {
					back->cf->bg[0] = 255;	

#line 182 "dk4bift.ctr"
					back->cf->bg[1] = 255;
					back->cf->bg[2] = 255;
					back->cf->bg[3] = 255;
					back->cf->bglbd[0] = 255;
					back->cf->bglbd[1] = 255;
					back->cf->bglbd[2] = 255;
					back->cf->bglbd[3] = 255;
					dk4pxres_setup(&(back->cf->resampler), 8, 8);
					back->cf->l_bdepth = back->cf->r_bdepth = 8;
					back->cf->n_comp = 4;
					back->cf->l_cs = back->cf->f_cs = DK4_CS_RGB_ALPHA;
					back->cf->hbg = 0;
				}
				else {						

#line 196 "dk4bift.ctr"
					dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
					cc = -1;
					ok = 0;
				}
			}
			else {		

#line 202 "dk4bift.ctr"
				dk4error_set_elsize_nelem(
					erp, DK4_E_MEMORY_ALLOCATION_FAILED,
					nby, 1
				);
				cc = -1;
				ok = 0;
			}
		}
		else {			

#line 211 "dk4bift.ctr"
			dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
			cc = -1;
			ok = 0;
		}

		/*	Attempt to read one further frame
		*/
		if (0 == cc) {
			if (1 == TIFFReadDirectory(tiff)) {	

#line 220 "dk4bift.ctr"
				if (0 != dk4biftool_nc_add_frame(back, erp)) {
					pfd = dk4mem_new(dk4_bif_tiff_per_frame_t,1,NULL);
					if (NULL != pfd) {			

#line 223 "dk4bift.ctr"
						pfd->rows = NULL;
						back->cf->tsdata = pfd;
						cc = 1;
					}
					else {						

#line 228 "dk4bift.ctr"
						dk4error_set_elsize_nelem(
							erp, DK4_E_MEMORY_ALLOCATION_FAILED,
							sizeof(dk4_bif_tiff_per_frame_t), 1
						);
						ok = 0;
						cc = -1;
					}
				}
				else {							

#line 237 "dk4bift.ctr"
					ok = 0;
					cc = -1;
				}
			}
		}

	} while (1 == cc);

	finished:
	if (NULL != tiff) {			

#line 247 "dk4bift.ctr"
		TIFFClose(tiff);
	}
	if (0 == ok) {
		if (NULL != back) {		

#line 251 "dk4bift.ctr"
			dk4bif_close(back);
			back = NULL;
		}
	}
	

#line 256 "dk4bift.ctr"
	return back;
}



int
dk4biftiff_nc_get_original_pixel(
	dk4_px_t				*dptr,
	dk4_bif_t		const	*bif,
	dk4_bif_dim_t			 rowno,
	dk4_bif_dim_t			 colno
)
{
	dk4_bif_tiff_per_frame_t	*pfd	= NULL;	/* Per-frame data */
	uint32						 abgr;			/* Pixel index in array */
	int							 back	= 0;
	

#line 273 "dk4bift.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != dptr);
	assert(NULL != bif);
#endif
	if (NULL == bif->cf->tsdata) {		

#line 278 "dk4bift.ctr"
		goto finished;
	}
	pfd = (dk4_bif_tiff_per_frame_t *)(bif->cf->tsdata);
	abgr = (pfd->rows)[(rowno * bif->cf->dim_x) + colno];
	dptr[0] = TIFFGetR(abgr);
	dptr[1] = TIFFGetG(abgr);
	dptr[2] = TIFFGetB(abgr);
	dptr[3] = TIFFGetA(abgr);
	back = 1;

	finished:
	

#line 290 "dk4bift.ctr"
	return back;
}



#endif
/* if DK4_HAVE_TIFF_H */


/* vim: set ai sw=4 ts=4 : */