summaryrefslogtreecommitdiff
path: root/support/dktools/dk4bifap.c
blob: 7eab24b9a83933fc3e1fee94ef099398b4e55b03 (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
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
492
493
/*
Copyright (C) 2018-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: dk4bifap.ctr
*/

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


#line 130 "dk4bifap.ctr"

#include "dk4conf.h"

#if	DK4_HAVE_MATH_H
#ifndef	MATH_H_INCLUDED
#if	DK4_ON_WINDOWS
#ifndef	_USE_MATH_DEFINES
#define	_USE_MATH_DEFINES 1
#endif
#endif
#include <math.h>
#define	MATH_H_INCLUDED 1
#endif
#endif

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

#ifndef	DK4BIFAP_H_INCLUDED
#include "dk4bifap.h"
#endif

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

#ifndef	DK4MAODD_H_INCLUDED
#include "dk4maodd.h"
#endif

#ifndef	DK4MAODDBL_H_INCLUDED
#include "dk4maoddbl.h"
#endif

#ifndef	DK4MATH_H_INCLUDED
#include "dk4math.h"
#endif

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



#line 177 "dk4bifap.ctr"



/**	File name dummy to use when processing standard input.
*/
static const dkChar		dk4bifap_def_fn[] = { dkT("<stdin>") };



/**	File name of string table for localizted texts.
*/
static const dkChar	dk4bifap_str[] = { dkT("dk4bif.str") };



/**	Default texts for diagnostic messages if localized texts are not found.
*/
static const dkChar * const	dk4bifap_kw[] = {
/* 0 */
dkT("File suffix does not inidicate a known image type!"),

/* 1 */
dkT("Failed to open image!"),

/* 2 */
dkT("Syntax error, file probably damaged!"),

/* 3 */
dkT("Failed to open and/or read file!"),

/* 4 */
dkT("File type not supported!"),

/* 5 */
dkT("Invalid arguments provided to function call, probably bug!"),

/* 6 */
dkT("Unusual maxval "),

/* 7 */
dkT("!\n\tMay result in color distorsion!"),

/* 8 */
dkT("Aspect ratio changed!\n\tChanging resolution from "),

/* 9 */
dkT("/"),

/* 10 */
dkT(" to "),

/* 11 */
dkT("/"),

/* 12 */
dkT(" dpi.\n\tThis will probably result in an image distorsion."),

NULL


#line 237 "dk4bifap.ctr"
};



/**	Report error using one text fragment.
	@param	app	Application structure.
	@param	si	Index of text fragment in dk4bifap_kw or
	list of localized texts.
*/
static
void
dk4bifap_error_1(
	dk4_app_t	*app,
	size_t		 si
)
{
	const dkChar * const	*msg	=	dk4bifap_kw;	/* Localized texs */
	size_t					 sz_msg;					/* Size of msg array */

	sz_msg = dk4app_string_table_size(dk4bifap_kw);
	msg = dk4app_string_table(app, dk4bifap_str, dk4bifap_kw);
	dk4app_log_1(app, msg, sz_msg, DK4_LL_ERROR, si);
}



/**	Report errors from attempt to open a bitmap image file.
	@param	fn	File name to read.
	@param	erp	Error report containing error information.
	@param	app	Application structure for diagnostics, may be NULL.
*/
static
void
dk4bif_error_open_app(
	dkChar		const	*fn,
	dk4_er_t			*erp,
	dk4_app_t			*app
)
{
	dkChar		const	*oldsourcefile;	/* Previous source file name to save */
	dk4_um_t			 oldsourceline;	/* Previous source line no to save */
	oldsourcefile = dk4app_get_log_source_file(app);
	oldsourceline = dk4app_get_log_source_line(app);
	if (NULL == fn) { fn = dk4bifap_def_fn; }
	dk4app_set_log_source_file(app, fn);
	dk4app_set_log_source_line(app, 0UL);
	dk4bifap_error_1(app, 1);
	switch (erp->ec) {
		case DK4_E_MEMORY_ALLOCATION_FAILED : {
			dk4app_log_base1(app, DK4_LL_ERROR, 90);
		} break;
		case DK4_E_SYNTAX : {
			dk4bifap_error_1(app, 2);
		} break;
		case DK4_E_MATH_OVERFLOW : {
			dk4app_log_base1(app, DK4_LL_ERROR, 82);
		} break;
		case DK4_E_NOT_FOUND : {
			dk4bifap_error_1(app, 0);
		} break;
		case DK4_E_READ_FAILED : {
			dk4bifap_error_1(app, 3);
		} break;
		case DK4_E_NOT_SUPPORTED : {
			dk4bifap_error_1(app, 4);
		} break;
		case DK4_E_INVALID_ARGUMENTS : {
			dk4bifap_error_1(app, 5);
		} break;
	}
	dk4app_set_log_source_file(app, oldsourcefile);
	dk4app_set_log_source_line(app, oldsourceline);
}



/**	Check whether the bitmap image file is NetPBM type and has an
	unusual maxval, report unusual maxval.
	@param	bif	Bitmap image file to check.
	@param	app	Application structure for diagnostics, may be NULL.
*/
static
void
dk4bifap_check_netpbm_maxval(
	dk4_bif_t	*bif,
	dk4_app_t	*app
)
{
	dkChar								 buf[16*sizeof(dk4_um_t)]; /* Buffer */
	dkChar const * const				*msg	= dk4bifap_kw;	/* Localized */
	dkChar						const	*oldsourcefile;	/* Previous src file */
	dk4_bif_netpbm_per_frame_t			*npfd;			/* Per-frame data */
	dk4_bif_frame_t						*frptr;			/* Current frame */
	dkChar						const	*fn = NULL;		/* File name */
	dk4_um_t							 oldsourceline;	/* Previous src line */
	size_t								 sz_msg;	/* Size of msg array */
	int									 res;		/* Operation result */
	dk4_px_t							 bdspec;	/* Maximum value for bpc */
	dk4_px_t							 bdmaxv;	/* Maxval */
	

#line 337 "dk4bifap.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != bif);
	assert(NULL != app);
#endif
	if ((NULL != bif) && (NULL != app)) {
		switch (bif->imgtype) {
			case DK4_BIF_TYPE_NETPBM : {
				dk4sto_it_reset(bif->i_frames);
				do {
					frptr = (dk4_bif_frame_t *)dk4sto_it_next(bif->i_frames);
					if (NULL != frptr) {
						npfd = (dk4_bif_netpbm_per_frame_t *)(frptr->tsdata);
						bdspec = dk4pxbit_get_max(npfd->bpc);
						bdmaxv = npfd->maxval;
						if (bdspec != bdmaxv) {
							fn = bif->filename;
							res = dk4ma_write_decimal_unsigned(
								buf, DK4_SIZEOF(buf,dkChar), bdmaxv, 0, NULL
							);
							if (0 != res) {
								oldsourcefile = dk4app_get_log_source_file(app);
								oldsourceline = dk4app_get_log_source_line(app);
								dk4app_set_log_source_file(app, fn);
								dk4app_set_log_source_line(app, (dk4_um_t)0UL);
								sz_msg = dk4app_string_table_size(dk4bifap_kw);
								msg = dk4app_string_table(
									app, dk4bifap_str, dk4bifap_kw
								);
								dk4app_log_3(
									app, msg, sz_msg, DK4_LL_WARNING, 6, 7, buf
								);
								dk4app_set_log_source_file(app, oldsourcefile);
								dk4app_set_log_source_line(app, oldsourceline);
							}
						}
					}
				} while (NULL != frptr);
				dk4sto_it_reset(bif->i_frames);
			} break;
		}
	}
	

#line 379 "dk4bifap.ctr"
}



dk4_bif_t *
dk4bif_open_app(
	dkChar				const	*fn,
	int							 honly,
	dk4_cs_conv_ctx_t	const	*pcsctx,
	dk4_app_t					*app
)
{
	dk4_er_t		 er;				/* Error report */
	dk4_bif_t		*back	= NULL;		/* Function result */
	

#line 394 "dk4bifap.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != fn);
#endif
	dk4error_init(&er);
	back = dk4bif_open(fn, honly, pcsctx, &er);
	if ((NULL == back) && (NULL != app)) {
		

#line 401 "dk4bifap.ctr"
		dk4bif_error_open_app(fn, &er, app);
	}
	if (NULL != back) {
		

#line 405 "dk4bifap.ctr"
		dk4bifap_check_netpbm_maxval(back, app);
	}
	

#line 408 "dk4bifap.ctr"
	return back;
}



dk4_bif_t *
dk4bif_open_file_app(
	FILE						*fipo,
	dkChar				const	*fn,
	int							 ty,
	int							 honly,
	dk4_cs_conv_ctx_t	const	*pcsctx,
	dk4_app_t					*app
)
{
	dk4_er_t		 er;
	dk4_bif_t		*back	= NULL;
	

#line 426 "dk4bifap.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != fipo);
#endif
	dk4error_init(&er);
	back = dk4bif_open_file(fipo, fn, ty, honly, pcsctx, &er);
	if ((NULL == back) && (NULL != app)) {
		

#line 433 "dk4bifap.ctr"
		dk4bif_error_open_app(fn, &er, app);
	}
	if (NULL != back) {
		

#line 437 "dk4bifap.ctr"
		dk4bifap_check_netpbm_maxval(back, app);
	}
	

#line 440 "dk4bifap.ctr"
	return back;
}



int
dk4bif_type_for_suffix_app(
	dkChar		const	*fn,
	dk4_app_t			*app
)
{
	const dkChar	*oldsourcefile	=	NULL;	/* Previous source file name */
	dk4_um_t		 oldsourceline	=	0UL;	/* Previous source line no */
	int				 back	= -1;
	

#line 455 "dk4bifap.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != fn);
#endif
	if (NULL != fn) {
		back = dk4bif_type_for_suffix(fn);
		if ((0 > back) && (NULL != app)) {
			oldsourcefile = dk4app_get_log_source_file(app);
			oldsourceline = dk4app_get_log_source_line(app);
			dk4app_set_log_source_file(app, fn);
			dk4app_set_log_source_line(app, 0UL);
			/* ##### ERROR: Not a known bitmap image file type */
			dk4app_set_log_source_file(app, oldsourcefile);
			dk4app_set_log_source_line(app, oldsourceline);
		}
	}
	

#line 471 "dk4bifap.ctr"
	return back;
}



void
dk4bif_warn_aspect_ratio_change(
	dk4_bif_t		*bif,
	double			 xres,
	double			 yres,
	dk4_app_t		*app
)
{
	dkChar	 b1[64];
	dkChar	 b2[64];
	dkChar	 b3[64];
	dkChar	 b4[64];
	const dkChar	*p[16];
	const dkChar	* const	*msg	=	dk4bifap_kw;
	

#line 491 "dk4bifap.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	dk4ma_write_double(b1, 64, bif->cf->res_x, 0, 1, NULL);
	dk4ma_write_double(b2, 64, bif->cf->res_y, 0, 1, NULL);
	dk4ma_write_double(b3, 64, xres, 0, 1, NULL);
	dk4ma_write_double(b4, 64, yres, 0, 1, NULL);
	/* WARNING: Resolution setting changes aspect ratio */
	msg = dk4app_string_table(app, dk4bifap_str, dk4bifap_kw);
	p[0] = msg[8];
	p[1] = b1;
	p[2] = msg[9];
	p[3] = b2;
	p[4] = msg[10];
	p[5] = b3;
	p[6] = msg[11];
	p[7] = b4;
	p[8] = msg[12];
	dk4app_log_msg(app, DK4_LL_WARNING, p, 9);
	

#line 511 "dk4bifap.ctr"
}



int
dk4bif_set_res_app(
	dk4_bif_t			*bif,
	double				 xres,
	double				 yres,
	dk4_app_t			*app
)
{
	double	 qo;			/* Quotient of old resolutions */
	double	 qn;			/* Quotient of new resolutions */
	int		 back	= 0;
	

#line 527 "dk4bifap.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != bif);
	assert(0.0 < xres);
	assert(0.0 < yres);
#endif
	if ((NULL != bif) && (0.0 < xres) && (0.0 < yres)) {	

#line 533 "dk4bifap.ctr"
		if (NULL != bif->cf) {								

#line 534 "dk4bifap.ctr"
			if ((0.0 < bif->cf->res_x) && (0.0 < bif->cf->res_y)) {
				

#line 536 "dk4bifap.ctr"
				qo = bif->cf->res_y / bif->cf->res_x;
				qn = yres / xres;
				if ((0 != dk4ma_is_finite(qo)) && (0 != dk4ma_is_finite(qn))) {
					if (DK4_BIF_EPSILON_ASPECT <= fabs(qn - qo)) {	

#line 540 "dk4bifap.ctr"
						dk4bif_warn_aspect_ratio_change(
							bif, xres, yres, app
						);
					}
				}
			}
			bif->cf->res_x = xres;
			bif->cf->res_y = yres;
			back = 1;
		}
	}
	else {
		/* ERROR: Invalid arguments */
	}
	

#line 555 "dk4bifap.ctr"
	return back;
}



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