summaryrefslogtreecommitdiff
path: root/support/dktools/dk4bifa.ctr
blob: 244bbbbf635f702598275c22d8fa07de3c753fc7 (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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2017-xxxx
SPDX-License-Identifier:	BSD-3-Clause


%%	header

/**	@file	dk4bifa.h	Bitmap image file analysis.
*/

#ifndef	DK4CONF_H_INCLUDED
#if	DK4_BUILDING_DKTOOLS4
#include "dk4conf.h"
#else
#include <dktools-4/dk4conf.h>
#endif
#endif

#ifndef	DK4ERROR_H_INCLUDED
#if	DK4_BUILDING_DKTOOLS4
#include "dk4error.h"
#else
#include <dktools-4/dk4error.h>
#endif
#endif

#ifndef	DK4BIFTY_H_INCLUDED
#if	DK4_BUILDING_DKTOOLS4
#include "dk4bifty.h"
#else
#include <dktools-4/dk4bifty.h>
#endif
#endif

#ifdef	__cplusplus
extern "C" {
#endif

/**	Analyze bitmap image file, apply the following tests:
	- If the image is RGB or CMYK, if R=G=B or C=M=Y for each pixel,
	  writing gray output is sufficient.
	- If the image contains an alpha channel and alpha data indicates
	  full opacity for all pixels, we can ignore the alpha channel.
	- If we can reduce the number of bits per component (bits per sample)
	  for all samples of all pixels without quality loss, do so.
	
	@param	pcs	Address of color space variable (in/out).
	@param	pbd	Address of bit depth (in/out).
	@param	bif	Bitmap image file.
	@param	anb	Flag: Analyze bit depth.
	@param	anc	Flag: Analyze color usage.
	@param	ana	Flag: Analyze alpha channel usage.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if:
	  - bif is NULL,
	  - pcs is NULL while color or alpha analysis is requested, or
	  - pbd is NULL while bit depth analysis is requested.
	- DK4_E_SYNTAX<br>
	  if an error occured while retrieving a pixel.
*/

int
dk4bif_analyze(
	int							*pcs,
	dk4_px_bit_depth_t			*pbd,
	dk4_bif_t			const	*bif,
	int							 anb,
	int							 anc,
	int							 ana,
	dk4_er_t					*erp
);

#ifdef	__cplusplus
}
#endif

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

%%	module

#ifndef	DK4CONF_H_INCLUDED
#include "dk4conf.h"
#endif

#ifndef	DK4BIFTY_H_INCLUDED
#include "dk4bifty.h"
#endif

#ifndef	DK4BIFA_H_INCLUDED
#include "dk4bifa.h"
#endif

#ifndef	DK4PXBIT_H_INCLUDED
#include "dk4pxbit.h"
#endif

#ifndef	DK4PXRES_H_INCLUDED
#include "dk4pxres.h"
#endif

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

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


$!trace-include



/**	Test for various resamplers whether the value can be resampled
	without quality loss.
	@param	pxr			Array of pixel resamplers to 1, 2, 4, 8 and 12 bit.
	@param	pxo			Array of can-use flags for the pixel resamplers.
	@param	changed		Address of change flag.
	@param	value		Pixel sample value to test.
	@param	ori_bd		Original bit depth.
*/
static
void
dk4bif_ana_bd(
	dk4_px_resample_t	*pxr,
	int					*pxo,
	int					*changed,
	dk4_px_t			 value,
	dk4_px_bit_depth_t	 ori_bd
)
{
#if	DK4_USE_ASSERT
	assert(NULL != pxr);
	assert(NULL != pxo);
	assert(NULL != changed);
#endif
	if (1 < ori_bd) {
		if (0 != pxo[0]) {
			if (0 == dk4pxres_check(&(pxr[0]), value)) {
				pxo[0] = 0;
				*changed = 1;
			}
		}
		if (2 < ori_bd) {
			if (0 != pxo[1]) {
				if (0 == dk4pxres_check(&(pxr[1]), value)) {
					pxo[1] = 0;
					*changed = 1;
				}
			}
			if (4 < ori_bd) {
				if (0 != pxo[2]) {
					if (0 == dk4pxres_check(&(pxr[2]), value)) {
						pxo[2] = 0;
						*changed = 1;
					}
				}
				if (8 < ori_bd) {
					if (0 != pxo[3]) {
						if (0 == dk4pxres_check(&(pxr[3]), value)) {
							pxo[3] = 0;
							*changed = 1;
						}
					}
					if (12 < ori_bd) {
						if (0 != pxo[4]) {
							if (0 == dk4pxres_check(&(pxr[4]), value)) {
								pxo[4] = 0;
								*changed = 1;
							}
						}
					}
				}
			}
		}
	}
}



int
dk4bif_analyze(
	int							*pcs,
	dk4_px_bit_depth_t			*pbd,
	dk4_bif_t			const	*bif,
	int							 anb,
	int							 anc,
	int							 ana,
	dk4_er_t					*erp
)
{
	dk4_px_resample_t	pxr[8];				/* Pixel resampling structures */
	dk4_px_t			pix[8];				/* Pixel component values */
	int					pxo[8];				/* Pixel resampling ok */
	size_t				i;					/* Traverse resampling structures */
	dk4_bif_dim_t		x;					/* Current column number */
	dk4_bif_dim_t		y;					/* Current row number */
	dk4_bif_dim_t		w;					/* Image width */
	dk4_bif_dim_t		h;					/* Image height */
	dk4_px_t			max			=	1;	/* Maximum value for bit depth */
	int					ori_cs		=	0;	/* Original color space */
	int					back		=	0;	/* Function result */
	int					f_col		=	0;	/* Flag: Found colored pixel */
	int					f_alp		=	0;	/* Flag: Found pixel with alpha */
	int					cc			=	1;	/* Flag: Can continue */
	int					changed		=	0;	/* Flag: Change in loop pass */
	dk4_px_bit_depth_t	ori_bd		=	8;	/* Original bit depth */

	$? "+ dk4bif_analyze"
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	/*
		Check arguments
	*/
	if (NULL == bif) {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		goto finished;
	}
	if ((NULL == pcs) && ((0 != anc) || (0 != ana))) {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		goto finished;
	}
	if ((NULL == pbd) && (0 != anb)) {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		goto finished;
	}

	/*	All arguments ok
	*/
	back = 1;

	/*	Correct analysis for color space
		(i.e. no need to check for colors if color space is gray)
	*/
	ori_cs = dk4bif_get_color_space(bif);
	ori_bd = dk4bif_get_original_bit_depth(bif);
	$? ". ori cs = %d", ori_cs
	$? ". ori bd = %u", (unsigned)ori_bd
	max = dk4pxbit_get_max(ori_bd);
	switch (ori_cs) {
		case DK4_CS_GRAY : {
			anc = 0;
			ana = 0;
		} break;
		case DK4_CS_GRAY_ALPHA : {
			anc = 0;
		} break;
		case DK4_CS_RGB : {
			ana = 0;
		} break;
		case DK4_CS_RGB_ALPHA : {
			ana = 0;
		} break;
		case DK4_CS_CMYK : {
			ana = 0;
		} break;
	}

	/*	If no analysis to execute, skip processing
	*/
	if ((0 == anb) && (0 == anc) && (0 == ana)) {
		if (NULL != pcs) { *pcs = ori_cs; }
		if (NULL != pbd) { *pbd = ori_bd; }
		goto finished;
	}

	/*	Mark all bit depths as available
	*/
	for (i = 0; i < 5; i++) {
		dk4pxres_init(&(pxr[i]));
		pxo[i] = 1;
	}

	/*	Set up pixel resampling structures to test bit depth conversions
	*/
	(void)dk4pxres_setup(&(pxr[0]), ori_bd,  1);
	(void)dk4pxres_setup(&(pxr[1]), ori_bd,  2);
	(void)dk4pxres_setup(&(pxr[2]), ori_bd,  4);
	(void)dk4pxres_setup(&(pxr[3]), ori_bd,  8);
	(void)dk4pxres_setup(&(pxr[4]), ori_bd, 12);
	if (NULL != bif->cf) {
		if (0 != bif->cf->fmres) {
			(void)dk4pxres_setup_from_maxval(&(pxr[0]), bif->cf->fmrmv,  1);
			(void)dk4pxres_setup_from_maxval(&(pxr[1]), bif->cf->fmrmv,  2);
			(void)dk4pxres_setup_from_maxval(&(pxr[2]), bif->cf->fmrmv,  4);
			(void)dk4pxres_setup_from_maxval(&(pxr[3]), bif->cf->fmrmv,  8);
			(void)dk4pxres_setup_from_maxval(&(pxr[4]), bif->cf->fmrmv, 12);
		}
	}

	/*	Obtain image dimensions
	*/
	w = dk4bif_get_width(bif);
	h = dk4bif_get_height(bif);

	/*	Traverse image
	*/
	for (y = 0; ((0 != cc) && (y < h)); y++) {
		for (x = 0; ((0 != cc) && (x < w)); x++) {
			changed = 0;
			if (0 != dk4bif_get_pixel(pix, 8, bif, y, x, ori_cs)) {
				/* Implementation, do the checks */
				switch (ori_cs) {
					case DK4_CS_GRAY : {
						if (0 != anb) {
							dk4bif_ana_bd(pxr, pxo, &changed, pix[0], ori_bd);
						}
					} break;
					case DK4_CS_GRAY_ALPHA : {
						if (0 != anb) {
							dk4bif_ana_bd(pxr, pxo, &changed, pix[0], ori_bd);
							dk4bif_ana_bd(pxr, pxo, &changed, pix[1], ori_bd);
						}
						if (0 != ana) {
							if (0 == f_alp) {
								if (max > pix[1]) {
									f_alp = 1;
									changed = 1;
								}
							}
						}
					} break;
					case DK4_CS_RGB : {
						if (0 != anb) {
							dk4bif_ana_bd(pxr, pxo, &changed, pix[0], ori_bd);
							dk4bif_ana_bd(pxr, pxo, &changed, pix[1], ori_bd);
							dk4bif_ana_bd(pxr, pxo, &changed, pix[2], ori_bd);
						}
						if (0 != anc) {
							if (0 == f_col) {
								if (pix[0] != pix[1]) {
									f_col = 1; changed = 1;
								}
								if (pix[0] != pix[2]) {
									f_col = 1; changed = 1;
								}
							}
						}
					} break;
					case DK4_CS_RGB_ALPHA : {
						if (0 != anb) {
							dk4bif_ana_bd(pxr, pxo, &changed, pix[0], ori_bd);
							dk4bif_ana_bd(pxr, pxo, &changed, pix[1], ori_bd);
							dk4bif_ana_bd(pxr, pxo, &changed, pix[2], ori_bd);
							dk4bif_ana_bd(pxr, pxo, &changed, pix[3], ori_bd);
						}
						if (0 != anc) {
							if (0 == f_col) {
								if (pix[0] != pix[1]) {
									f_col = 1; changed = 1;
								}
								if (pix[0] != pix[2]) {
									f_col = 1; changed = 1;
								}
							}
						}
						if (0 != ana) {
							if (0 == f_alp) {
								if (max > pix[3]) {
									f_alp = 1;
									changed = 1;
								}
							}
						}
					} break;
					case DK4_CS_CMYK : {
						if (0 != anb) {
							dk4bif_ana_bd(pxr, pxo, &changed, pix[0], ori_bd);
							dk4bif_ana_bd(pxr, pxo, &changed, pix[1], ori_bd);
							dk4bif_ana_bd(pxr, pxo, &changed, pix[2], ori_bd);
							dk4bif_ana_bd(pxr, pxo, &changed, pix[3], ori_bd);
						}
						if (0 != anc) {
							if (0 == f_col) {
								if (0 < pix[0]) { f_col = 1; changed = 1; }
								if (0 < pix[1]) { f_col = 1; changed = 1; }
								if (0 < pix[2]) { f_col = 1; changed = 1; }
							}
						}
					} break;
				}
			}
			else {
				cc = 0;
				back = 0;
				dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
			}
			/* Check whether we must continue */
			if (0 != changed) {
				cc = 0;
				if ((0 != ana) && (0 == f_alp)) {
					cc = 1;
				}
				else {
					if ((0 != anc) && (0 == f_col)) {
						cc = 1;
					}
					else {
						if (12 < ori_bd) {
							if (0 != pxo[4]) { cc = 1; }
							if (0 != pxo[3]) { cc = 1; }
							if (0 != pxo[2]) { cc = 1; }
							if (0 != pxo[1]) { cc = 1; }
							if (0 != pxo[0]) { cc = 1; }
						}
						else {
							if (8 < ori_bd) {
								if (0 != pxo[3]) { cc = 1; }
								if (0 != pxo[2]) { cc = 1; }
								if (0 != pxo[1]) { cc = 1; }
								if (0 != pxo[0]) { cc = 1; }
							}
							else {
								if (4 < ori_bd) {
									if (0 != pxo[2]) { cc = 1; }
									if (0 != pxo[1]) { cc = 1; }
									if (0 != pxo[0]) { cc = 1; }
								}
								else {
									if (2 < ori_bd) {
										if (0 != pxo[1]) { cc = 1; }
										if (0 != pxo[0]) { cc = 1; }
									}
									else {
										if (1 < ori_bd) {
											if (0 != pxo[0]) { cc = 1; }
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	/*	Transfer results to result variables
	*/
	if (0 != back) {
		if (NULL != pcs) {
			/* Set result color space */
			switch (ori_cs) {
				case DK4_CS_GRAY : {
					*pcs = DK4_CS_GRAY;
				} break;
				case DK4_CS_GRAY_ALPHA : {
					if ((0 != f_alp) || (0 == ana)) {
						*pcs = DK4_CS_GRAY_ALPHA;
					}
					else {
						*pcs = DK4_CS_GRAY;
					}
				} break;
				case DK4_CS_RGB : {
					if (0 != f_col) {
						*pcs = DK4_CS_RGB;
					}
					else {
						*pcs = DK4_CS_GRAY;
					}
				} break;
				case DK4_CS_RGB_ALPHA : {
					if ((0 != f_alp) || (0 == ana)) {
						if (0 != f_col) {
							*pcs = DK4_CS_RGB_ALPHA;
						}
						else {
							*pcs = DK4_CS_GRAY_ALPHA;
						}
					}
					else {
						if (0 != f_col) {
							*pcs = DK4_CS_RGB;
						}
						else {
							*pcs = DK4_CS_GRAY;
						}
					}
				} break;
				case DK4_CS_CMYK : {
					if (0 != f_col) {
						*pcs = DK4_CS_CMYK;
					}
					else {
						*pcs = DK4_CS_GRAY;
					}
				} break;
			}
		}
		if (NULL != pbd) {
			/* Set result bit depth */
			if (0 != pxo[0]) {
				*pbd = 1;
			}
			else {
				if (0 != pxo[1]) {
					*pbd = 2;
				}
				else {
					if (0 != pxo[2]) {
						*pbd = 4;
					}
					else {
						if (0 != pxo[3]) {
							*pbd = 8;
						}
						else {
							if (0 != pxo[4]) {
								*pbd = 12;
							}
							else {
								if (1 < ori_bd) {
									if (2 < ori_bd) {
										if (4 < ori_bd) {
											if (8 < ori_bd) {
												*pbd = 12;
											}
											else {
												*pbd = 8;
											}
										}
										else {
											*pbd = 4;
										}
									}
									else {
										*pbd = 2;
									}
								}
								else {
									*pbd = 1;
								}
							}
						}
					}
				}
			}
		}
	}

	finished:
#if TRACE_DEBUG
	if (NULL != pcs) {
		$? ". new cs = %d", *pcs
	}
	if (NULL != pbd) {
		$? ". new bd = %u", (unsigned)(*pbd)
	}
#endif
	$? "- dk4bif_analyze"
	return back;
}



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