summaryrefslogtreecommitdiff
path: root/support/dktools/dk4bif.ctr
blob: 09e2d4bbc6cd27986660b7d09df19de7480c756b (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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
%%	options 
copyright owner	=	Dirk Krause
copyright year	=	2017-xxxx
SPDX-License-Identifier:	BSD-3-Clause

%%	header

/**	@file dk4bif.h	Bitmap image files.
*/

#ifndef	DK4CONF_H_INCLUDED
#if	DK4_BUILDING_DKTOOLS4
#include "dk4conf.h"
#else
#include <dktools-4/dk4conf.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


/**	Find bitmap image file type for file name.
	@param	fn	File name to check.
	@return	Non-negative dk4_bif_type_t value on success,
	-1 on error (NULL file name or file name suffix does not indicate
	a graphics type the function can handle).
*/

int
dk4bif_type_for_suffix(
	dkChar	const	*fn
);


/**	Open bitmap image file.
	@param	fn		File name containing the bitmap image.
	@param	hdronly	Flag: Read header only.
	@param	pcsctx	Color space conversion context, may be NULL.
	@param	erp		Error report, may be NULL.
	@return	Valid pointer to bitmap image file on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if fn is NULL,
	- DK4_E_NOT_FOUND<br>
	  if the file name does not contain a suffix or the suffix does
	  not indicate a bitmap image file type the function can handle,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  if a memory allocation failed,
	- DK4_E_SYNTAX<br>
	  if the file contents is incorrect (not matching the specified file
	  type),
	- DK4_E_READ_FAILED<br>
	  if the function failed to read from file fn.
*/

dk4_bif_t *
dk4bif_open(
	dkChar	const				*fn,
	int							 hdronly,
	dk4_cs_conv_ctx_t	const	*pcsctx,
	dk4_er_t					*erp
);


/**	Open bitmap image file.
	@param	fn		File name containing the bitmap image.
	@param	ty		Image file type (PNG, JPEG or TIFF).
	@param	honly	Flag: Read header information only.
	@param	pcsctx	Color space conversion context, may be NULL.
	@param	erp		Error report, may be NULL.
	@return	Valid pointer to bitmap image file on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if fn is NULL or an unknown bitmap type is requested,
	- DK4_E_READ_FAILED<br>
	  if the function failed to read from file fn,
	- DK4_E_NOT_SUPPORTED<br>
	  if the operation is not supported (i.e. compiled without a
	  required library).
*/

dk4_bif_t *
dk4bif_open_type(
	dkChar	const				*fn,
	int							 ty,
	int							 honly,
	dk4_cs_conv_ctx_t	const	*pcsctx,
	dk4_er_t					*erp
);


/**	Open bitmap image file.
	@param	fipo	File, opened for binary reading.
	@param	fn		File name containing the bitmap image.
	@param	ty		Image file type (PNG, JPEG or TIFF).
	@param	honly	Flag: Read image header only.
	@param	pcsctx	Color space conversion context, may be NULL.
	@param	erp		Error report, may be NULL.
	@return	Valid pointer to bitmap image file on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if fipo or fn is NULL,
	- DK4_E_NOT_SUPPORTED<br>
	  if the operation is not supported (i.e. compiled without a
	  required library).
*/

dk4_bif_t *
dk4bif_open_file(
	FILE						*fipo,
	dkChar	const				*fn,
	int							 ty,
	int							 honly,
	dk4_cs_conv_ctx_t	const	*pcsctx,
	dk4_er_t					*erp
);


/**	Close bitmap image file and release resources.
	@param	bif		Bitmap image file to close.
*/

void
dk4bif_close(
	dk4_bif_t		*bif
);


/**	Find number of frames in bitmap image file.
	@param	bif		Bitmap image file to query.
	@return	Positive value on success, 0 on error.
*/

size_t
dk4bif_get_number_of_frames(
	dk4_bif_t const		*bif
);


/**	Set current frame in a bitmap image file.
	@param	bif		Bitmap image file.
	@param	fno		Index of new current frame (0 for first frame).
	@return	1 on success, 0 on error (out of range).
*/

int
dk4bif_set_current_frame(
	dk4_bif_t		*bif,
	size_t			 fno
);


/**	Retrieve low level bit depth of current frame bitmap image file.
	@param	bif		Bitmap image file
	@return	Positive value on success, 0 on error.
*/

dk4_px_bit_depth_t
dk4bif_get_original_bit_depth(
	dk4_bif_t const		*bif
);


/**	Retrieve color space of current frame returned from low-level library.
	@param	bif		Bitmap image file.
	@return	Non-negative dk4_bif_cs_t value on success, -1 on error.
*/

int
dk4bif_get_color_space(
	dk4_bif_t const		*bif
);


/**	Retrieve color space of current frame contained in the file.
	@param	bif		Bitmap image file.
	@return	Non-negative dk4_bif_cs_t value on success, -1 on error.
*/

int
dk4bif_get_file_color_space(
	dk4_bif_t const		*bif
);


/**	Set required output bit depth for pixel data to retrieve for
	all frames.
	@param	bif		Bitmap image file.
	@param	bd		Required output bit depth.
	@return	1 on success, 0 on error.
*/

int
dk4bif_set_output_bit_depth(
	dk4_bif_t			*bif,
	dk4_px_bit_depth_t	 bd
);


/**	Set default background or enforced background for bitmap image file
	for all frames.
	@param	bif		Bitmap image file.
	@param	bg		Array containing background values.
	@param	szbg	Size of bg array (number of elements).
	@param	bd		Bit depth used in the bg array.
	@param	force	Flag: Enforce this background. Without this flag the
					background information from here is used only if no
					background information was found in the file.
	@return	1 on success, 0 on error.
*/

int
dk4bif_set_background(
	dk4_bif_t					*bif,
	dk4_px_t			const	*bg,
	size_t						 szbg,
	dk4_px_bit_depth_t			 bd,
	int							 force
);


/**	Get image type.
	@param	bif	Bitmap image file.
	@return	Non-negative image type on success, negative value on error.
*/

int
dk4bif_get_image_type(
	dk4_bif_t	const	*bif
);


/**	Retrieve data for one pixel in the required output bit depth
	and color space.
	@param	dptr	Address of destination array, must be large enough.
	@param	szdptr	Size of dptr array (number of elements).
	@param	bif		Bitmap image file.
	@param	rowno	Row number (0 for top row).
	@param	colno	Column number (0 for left column).
	@param	cs		Required color space.
	@return	1 on success, 0 on error.
*/

int
dk4bif_get_pixel(
	dk4_px_t				*dptr,
	size_t					 szdptr,
	dk4_bif_t		const	*bif,
	dk4_bif_dim_t			 rowno,
	dk4_bif_dim_t			 colno,
	int						 cs
);


/**	Get the current frames x resolution in dpi.
	@param	bif	Bitmap image file.
	@return	Positive number on success, negative value on error or if
	not found.
*/

double
dk4bif_get_xres(
	dk4_bif_t const		*bif
);


/**	Get the current frames y resolution in dpi.
	@param	bif	Bitmap image file.
	@return	Positive number on success, negative value on error or if
	not found.
*/

double
dk4bif_get_yres(
	dk4_bif_t const		*bif
);


/**	Set resolution explicitly.
	@param	bif		Bitmap image file.
	@param	xres	X resolution, must be positive.
	@param	yres	Y resolution, must be positive.
	@param	erp		Error report, may be NULL.
	@return	1 on success, 0 on error
*/

int
dk4bif_set_res(
	dk4_bif_t			*bif,
	double				 xres,
	double				 yres,
	dk4_er_t			*erp
);

/**	Get current frame width.
	@param	bif	Bitmap image file.
	@return	Positive number on success, negative value on error.
*/

dk4_bif_dim_t
dk4bif_get_width(
	dk4_bif_t const		 *bif
);


/**	Get current frame height.
	@param	bif	Bitmap image file.
	@return	Positive number on success, negative value on error.
*/

dk4_bif_dim_t
dk4bif_get_height(
	dk4_bif_t const 		*bif
);


/**	Get color space conversion context for bitmap image.
	@param	bif	Bitmap image file.
	@return	Valid pointer on success, NULL on error.
*/

const
dk4_cs_conv_ctx_t *
dk4bif_get_cs_conv_ctx(
	dk4_bif_t const		*bif
);


/**	Set color space conversion context for bitmap image.
	@param	bif		Bitmap image file.
	@param	pctx	Color space conversion context to use.
*/

void
dk4bif_set_cs_conv_ctx(
	dk4_bif_t					*bif,
	dk4_cs_conv_ctx_t	const	*pctx
);


/**	Close a bitmap image frame, release ressources.
	@param	frptr	Frame to close.
	@param	imgtype	Bitmap image file type.
*/

void
dk4bif_frame_close(
	dk4_bif_frame_t	*frptr,
	int				 imgtype
);

#ifdef	__cplusplus
}
#endif

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

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

#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	DK4PATHD_H_INCLUDED
#include "dk4pathd.h"
#endif

#ifndef	DK4STRD_H_INCLUDED
#include "dk4strd.h"
#endif

#ifndef	DK4FOPD_H_INCLUDED
#include "dk4fopd.h"
#endif

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


$!trace-include


/**	File name suffixes for bitmap image files.
*/
static const dkChar * const	dk4bif_suffixes[] = {
$!string-table	macro=dkT
.png
.jpg
.jpeg
.tif
.tiff
.pbm
.pgm
.ppm
.pnm
.pam
$!end
};


/**	Keywords used by the module.
*/
static const dkChar * const	dk4bif_kw[] = {
$!string-table	macro=dkT
#
#	File open mode: read binary
#
rb
$!end
};



void
dk4bif_frame_close(
	dk4_bif_frame_t	*frptr,
	int				 imgtype
)
{
	$? "+ dk4bif_frame_close frptr=%d", TR_IPTR(frptr)
#if	DK4_USE_ASSERT
	assert(NULL != frptr);
#endif
	if (NULL != frptr) {
		if (NULL != frptr->tsdata) {
			switch (imgtype) {
				case DK4_BIF_TYPE_PNG : {
#if	DK4_HAVE_PNG_H
					dk4bifpng_nc_release_per_frame_data(frptr->tsdata);
#else
					dk4mem_release(frptr->tsdata);
#endif
				} break;
				case DK4_BIF_TYPE_JPEG : {
#if	DK4_HAVE_JPEGLIB_H
					dk4bifjpeg_nc_release_per_frame_data(frptr->tsdata, frptr);
#else
					dk4mem_release(frptr->tsdata);
#endif
				} break;
				case DK4_BIF_TYPE_TIFF : {
#if	DK4_HAVE_TIFF_H
					dk4biftiff_nc_release_per_frame_data(frptr->tsdata);
#else
					dk4mem_release(frptr->tsdata);
#endif
				} break;
				case DK4_BIF_TYPE_NETPBM : {
					dk4bifnetpbm_nc_release_per_frame_data(frptr->tsdata);
				} break;
				default : {
					dk4mem_release(frptr->tsdata);
				} break;
			}
			frptr->tsdata = NULL;
		}
		dk4mem_free(frptr);
	}
	$? "- dk4bif_frame_close"
}



void
dk4bif_close(
	dk4_bif_t		*bif
)
{
	dk4_bif_frame_t		*frptr;		/* Traverse all frames */
	$? "+ dk4bif_close ptr=%d", TR_IPTR(bif)
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	if (NULL != bif) {
		if (NULL != bif->s_frames) {
			if (NULL != bif->i_frames) {
				dk4sto_it_reset(bif->i_frames);
				do {
					frptr = (dk4_bif_frame_t *)dk4sto_it_next(bif->i_frames);
					if (NULL != frptr) {
						dk4bif_frame_close(frptr, bif->imgtype);
					}
				} while(NULL != frptr);
				dk4sto_it_close(bif->i_frames);
				bif->i_frames = NULL;
			}
			dk4sto_close(bif->s_frames);
			bif->s_frames = NULL;
		}
		bif->cf = NULL;
		if (NULL != bif->tsdata) {
			switch (bif->imgtype) {
				case DK4_BIF_TYPE_PNG : {
#if	DK4_HAVE_PNG_H
					dk4bifpng_nc_release_per_file_data(bif->tsdata);
#else
					dk4mem_release(bif->tsdata);
#endif
				} break;
				case DK4_BIF_TYPE_JPEG : {
#if 0
/* No per-file data stored for JPEG file */
#if	DK4_HAVE_JPEGLIB_H
					dk4bifjpeg_nc_release_per_file_data(bif->tsdata);
#else
					dk4mem_release(bif->tsdata);
#endif
#endif
				} break;
				case DK4_BIF_TYPE_TIFF : {
#if 0
/* No per-file data stored for TIFF */
#if	DK4_HAVE_TIFF_H
					dk4biftiff_nc_release_per_file_data(bif->tsdata);
#else
					dk4mem_release(bif->tsdata);
#endif
#endif
				} break;
				case DK4_BIF_TYPE_NETPBM : {
					/*
						No per-file data stored for NetPBM.
					*/
				} break;
				default : {
					dk4mem_release(bif->tsdata);
				} break;
			}
		}
		dk4mem_release(bif->filename);
		bif->n_frames = 0;
		bif->imgtype = 0;
		dk4mem_free(bif);
	}
	$? "- dk4bif_close"
}



int
dk4bif_type_for_suffix(
	dkChar	const	*fn
)
{
	const dkChar	*sptr;			/* Pointer to dot of file name suffix */
	int				 back	= -1;
	$? "+ dk4bif_type_for_suffix \"%!ds\"", TR_STR(fn)
#if	DK4_USE_ASSERT
	assert(NULL != fn);
#endif
	if (NULL != fn) {
		sptr = dk4path_get_suffix(fn, NULL);
		$? ". sptr = \"%!ds\"", TR_STR(sptr)
		if (NULL != sptr) {
			switch (dk4str_array_index(dk4bif_suffixes, sptr, 0)) {
				case 0 : {
					back = DK4_BIF_TYPE_PNG;
				} break;
				case 1 : case 2 : {
					back = DK4_BIF_TYPE_JPEG;
				} break;
				case 3 : case 4 : {
					back = DK4_BIF_TYPE_TIFF;
				} break;
				case 5 : case 6 : case 7 : case 8 : case 9 : {
					back = DK4_BIF_TYPE_NETPBM;
				} break;
			}
		}
	}
	$? "- dk4bif_type_for_suffix %d", back
	return back;
}



dk4_bif_t *
dk4bif_open_file(
	FILE						*fipo,
	dkChar	const				*fn,
	int							 ty,
	int							 honly,
	dk4_cs_conv_ctx_t	const	*pcsctx,
	dk4_er_t					*erp
)
{
	dk4_bif_png_clobbered_t	 pngcp;	/* Clobber protected variables */
	dk4_bif_jpg_clobbered_t	 jpgcp;	/* Clobber protected variables */
	dk4_bif_t				*back	= NULL;
	$? "+ dk4bif_open_file ptr=%d \"%!ds\"", TR_IPTR(fipo), TR_STR(fn)
#if	DK4_USE_ASSERT
	assert(NULL != fipo);
#endif
	if (NULL == fipo) {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		goto finished;
	}
	switch (ty) {
		case DK4_BIF_TYPE_PNG : {
#if	DK4_HAVE_PNG_H
			pngcp.back = NULL;
			pngcp.pfrpng = NULL;
			pngcp.ok = 0;
			back = dk4bifpng_nc_open_file(fipo, fn, honly, pcsctx, &pngcp, erp);
#else
			dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
		} break;
		case DK4_BIF_TYPE_JPEG : {
#if	DK4_HAVE_JPEGLIB_H
			jpgcp.st = 0;
			jpgcp.ok = 0;
			jpgcp.back = NULL;
			back = dk4bifjpeg_nc_open_file(fipo,fn,honly,pcsctx,&jpgcp,erp);
#else
			dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
		} break;
		case DK4_BIF_TYPE_NETPBM : {
			$? ". before dk4bifnetpbm_nc_open_file honly=%d", honly
			back = dk4bifnetpbm_nc_open_file(fipo, fn, honly, pcsctx, erp);
			$? ". after dk4bifnetpbm_nc_open_file"
		} break;
		default : {
			dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		} break;
	}
	finished:
	$? "- dk4bif_open_file %d", TR_IPTR(back)
	return back;
}



dk4_bif_t *
dk4bif_open_type(
	dkChar	const				*fn,
	int							 ty,
	int							 honly,
	dk4_cs_conv_ctx_t	const	*pcsctx,
	dk4_er_t					*erp
)
{
	FILE		*inputfile;							/* Input file */
	dk4_bif_t	*back	= NULL;
	int			 tests	= DK4_FOPEN_SC_IS_REGULAR;	/* Tests for fopen */
	$? "+ dk4bif_open_type \"%!ds\"", TR_STR(fn)
#if	DK4_USE_ASSERT
	assert(NULL != fn);
#endif
	if (NULL == fn) {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		goto finished;
	}
	if ((DK4_BIF_TYPE_MIN > ty) || (DK4_BIF_TYPE_MAX < ty)) {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		goto finished;
	}
	switch (ty) {
		case DK4_BIF_TYPE_PNG :
		case DK4_BIF_TYPE_JPEG :
		case DK4_BIF_TYPE_NETPBM : {
			inputfile = dk4fopen(fn, dk4bif_kw[0], tests, erp);
			if (NULL != inputfile) {
				back = dk4bif_open_file(inputfile, fn, ty, honly, pcsctx, erp);
				fclose(inputfile);
			}
		} break;
		case DK4_BIF_TYPE_TIFF : {
#if	DK4_HAVE_TIFF_H
			back = dk4biftiff_nc_open_type(fn, pcsctx, erp);
#else
			dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
		} break;
	}
	finished:
	$? "- dk4bif_open_type %d", TR_IPTR(back)
	return back;
}



dk4_bif_t *
dk4bif_open(
	dkChar	const				*fn,
	int							 hdronly,
	dk4_cs_conv_ctx_t	const	*pcsctx,
	dk4_er_t					*erp
)
{
	dk4_bif_t	*back	= NULL;
	int			 tp;				/* Bitmap image file type */
	$? "+ dk4bif_open \"%!ds\"", TR_STR(fn)
#if	DK4_USE_ASSERT
	assert(NULL != fn);
#endif
	if (NULL != fn) {
		tp = dk4bif_type_for_suffix(fn);
		if ((DK4_BIF_TYPE_MIN <= tp) && (DK4_BIF_TYPE_MAX >= tp)) {
			back = dk4bif_open_type(fn, tp, hdronly, pcsctx, erp);
		}
		else {
			dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
		}
	}
	else {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}
	$? "- dk4bif_open %d", TR_IPTR(back)
	return back;
}



size_t
dk4bif_get_number_of_frames(
	dk4_bif_t const		*bif
)
{
	size_t	back	= 0;
	$? "+ dk4bif_get_number_of_frames %d", TR_IPTR(bif)
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	if (NULL != bif) {
		back = bif->n_frames;
	}
	$? "- dk4bif_get_number_of_frames %lu", (unsigned long)back
	return back;
}



int
dk4bif_set_current_frame(
	dk4_bif_t		*bif,
	size_t			 fno
)
{
	int		 back = 0;
	$? "+ dk4bif_set_current_frame ptr=%d frame=%lu", TR_IPTR(bif), (unsigned long)fno
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	/*
		Check arguments
	*/
	if (NULL == bif) {
		goto finished;
	}
	if ((NULL == bif->s_frames) || (NULL == bif->i_frames)) {
		goto finished;
	}
	/*	Retrieve frames from storage iterator until found
	*/
	dk4sto_it_reset(bif->i_frames);
	do {
		bif->cf = (dk4_bif_frame_t *)dk4sto_it_next(bif->i_frames);
		if (NULL == bif->cf) {
			goto finished;
		}
	} while(0 < fno--);
	if (NULL != bif->cf) { back = 1; }

	/*	Return
	*/
	finished:
	$? "- dk4bif_set_current_frame %d", back
	return back;
}



dk4_px_bit_depth_t
dk4bif_get_original_bit_depth(
	dk4_bif_t const		*bif
)
{
	dk4_px_bit_depth_t	back	= 0;
	$? "+ dk4_px_bit_depth_t ptr=%d", TR_IPTR(bif)
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	/*
		Check arguments
	*/
	if (NULL == bif) {
		goto finished;
	}
	if (NULL == bif->cf) {
		goto finished;
	}
	/*	Retrieve bit depth from current frame
	*/
	back = (bif->cf)->l_bdepth;

	/*	Return
	*/
	finished:
	$? "- dk4_px_bit_depth_t %u", (unsigned)back
	return back;
}



int
dk4bif_get_color_space(
	dk4_bif_t const		*bif
)
{
	int		 back	= -1;
	$? "+ dk4bif_get_color_space ptr=%d", TR_IPTR(bif)
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	/*
		Check arguments
	*/
	if (NULL == bif) {
		goto finished;
	}
	if (NULL == bif->cf) {
		goto finished;
	}
	/*	Retrieve library color space from current frame
	*/
	back = (bif->cf)->l_cs;

	/*	Return
	*/
	finished:
	$? "- dk4bif_get_color_space %d", back
	return back;
}



int
dk4bif_get_file_color_space(
	dk4_bif_t const		*bif
)
{
	int		 back	= -1;
	$? "+ dk4bif_get_file_color_space ptr=%d", TR_IPTR(bif)
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	/*
		Check arguments
	*/
	if (NULL == bif) {
		goto finished;
	}
	if (NULL == bif->cf) {
		goto finished;
	}
	/*	Retrieve file color space from current frame
	*/
	back = (bif->cf)->f_cs;

	/*	Return
	*/
	finished:
	$? "- dk4bif_get_file_color_space %d", back
	return back;
}



int
dk4bif_set_output_bit_depth(
	dk4_bif_t			*bif,
	dk4_px_bit_depth_t	 bd
)
{
	dk4_bif_frame_t		*fptr;			/* Traverse all frames in storage */
	int					 back	= 0;
	$? "+ dk4bif_set_output_bit_depth ptr=%d bd=%u", TR_IPTR(bif), (unsigned)bd
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	/*
		Check arguments
	*/
	if (NULL == bif) {
		goto finished;
	}
	if ((NULL == bif->s_frames) || (NULL == bif->i_frames)) {
		goto finished;
	}
	if (0 != bif->hdronly) {
		goto finished;
	}
	/*	Traverse all frames, set required bit depth and resampler in frame
	*/
	dk4sto_it_reset(bif->i_frames);
	do {
		fptr = (dk4_bif_frame_t *)dk4sto_it_next(bif->i_frames);
		if (NULL != fptr) {	$? ". set bit depth for frame"
			if (0 != fptr->fmres) {
				dk4pxres_setup_from_maxval(&(fptr->resampler), fptr->fmrmv, bd);
			}
			else {
				dk4pxres_setup(&(fptr->resampler), fptr->l_bdepth, bd);
			}
			fptr->r_bdepth = bd;
			$? ". bglbd[0]=%u", (unsigned)(fptr->bglbd[0])
			$? ". bglbd[1]=%u", (unsigned)(fptr->bglbd[1])
			$? ". bglbd[2]=%u", (unsigned)(fptr->bglbd[2])
			dk4mem_cpy(
				&(fptr->bg[0]), &(fptr->bglbd[0]), 4*sizeof(dk4_px_t), NULL
			);
			if ((fptr->l_bdepth != bd) || (0 != fptr->fmres)) {
				dk4pxres_multiple(&(fptr->bg[0]), 4, &(fptr->resampler));
			}
			$? ". bg[0]=%u", (unsigned)(fptr->bg[0])
			$? ". bg[1]=%u", (unsigned)(fptr->bg[1])
			$? ". bg[2]=%u", (unsigned)(fptr->bg[2])
		}
	} while (NULL != fptr);
	/*
		Indicate success
	*/
	back = 1;

	/*	Return
	*/
	finished:
	$? "- dk4bif_set_output_bit_depth %d", back
	return back;
}



int
dk4bif_set_background(
	dk4_bif_t					*bif,
	dk4_px_t			const	*bg,
	size_t						 szbg,
	dk4_px_bit_depth_t			 bd,
	int							 force
)
{
	dk4_px_resample_t	 bgres;		/* Resample bg data to required bit depth */
	dk4_bif_frame_t		*fptr;		/* Traverse all frames */
	size_t				 i;			/* Current bg component index */
	int					 back	= 0;
	$? "+ dk4bif_set_background ptr=%d bg=%d", TR_IPTR(bif), TR_IPTR(bg)
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	/*
		Check arguments
	*/
	if ((NULL == bif) || (NULL == bg)) {
		goto finished;
	}
	if ((NULL == bif->s_frames) || (NULL == bif->i_frames)) {
		goto finished;
	}
	if (0 != bif->hdronly) {
		goto finished;
	}
	/*	Correct number of background arguments
	*/
	if (szbg > 4) {
		szbg = 4;
	}
	/*	Traverse all frames, set background in the frames bit depth
	*/
	dk4sto_it_reset(bif->i_frames);
	do {
		fptr = (dk4_bif_frame_t *)dk4sto_it_next(bif->i_frames);
		if (NULL != fptr) {
			if ((0 != force) || (0 == (bif->cf)->hbg)) {
				if (0 != fptr->fmres) {
					dk4pxres_setup_to_maxval(&bgres, bd, fptr->fmrmv);
				}
				else {
					dk4pxres_setup(&bgres, bd, (fptr->l_bdepth));
				}
				for (i = 0; i < szbg; i++) {
					fptr->bg[i] = dk4pxres_get(&bgres, bg[i]);
					fptr->bglbd[i] = bg[i];
				}
			}
		}
	} while (NULL != fptr);
	/*
		Indicate success
	*/
	back = 1;

	/*	Return
	*/
	finished:
	$? "- dk4bif_set_background %d", back
	return back;
}



double
dk4bif_get_xres(
	dk4_bif_t const		*bif
)
{
	double		 back	= -1.0;
	$? "+ dk4bif_get_xres %d", TR_IPTR(bif)
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	if (NULL == bif) {
		goto finished;
	}
	if (NULL == bif->cf) {
		goto finished;
	}
	/*	Retrieve x resolution from current frame
	*/
	back = (bif->cf)->res_x;

	/*	Return
	*/
	finished:
	$? "- dk4bif_get_xres %g", back
	return back;
}



double
dk4bif_get_yres(
	dk4_bif_t const		*bif
)
{
	double		 back	= -1.0;
	$? "+ dk4bif_get_yres %d", TR_IPTR(bif)
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	/*
		Check arguments
	*/
	if (NULL == bif) {
		goto finished;
	}
	if (NULL == bif->cf) {
		goto finished;
	}
	/*	Retrieve y resolution from current frame
	*/
	back = (bif->cf)->res_y;

	/*	Return
	*/
	finished:
	$? "- dk4bif_get_yres %g", back
	return back;
}



int
dk4bif_set_res(
	dk4_bif_t			*bif,
	double				 xres,
	double				 yres,
	dk4_er_t			*erp
)
{
	int		 back	= 0;
	$? "+ dk4bif_set_res %lg %lg", xres, yres
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	if ((NULL != bif) && (0.0 < xres) && (0.0 < yres)) {
		if (NULL != bif->cf) {	$? ". settings applied"
			bif->cf->res_x = xres;
			bif->cf->res_y = yres;
			back = 1;
		}
		else {
			dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		}
	}
	else {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}
	$? "- dk4bif_set_res %d", back
	return back;
}



dk4_bif_dim_t
dk4bif_get_width(
	dk4_bif_t const		 *bif
)
{
	dk4_bif_dim_t		 back	= -1L;
	$? "+ dk4_bif_dim_t %d", TR_IPTR(bif)
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	/*
		Check arguments
	*/
	if (NULL == bif) {
		goto finished;
	}
	if (NULL == bif->cf) {
		goto finished;
	}
	/*	Retrieve width of current frame
	*/
	back = (bif->cf)->dim_x;

	/*	Return
	*/
	finished:
	$? "- dk4_bif_dim_t %ld", (long)back
	return back;
}



dk4_bif_dim_t
dk4bif_get_height(
	dk4_bif_t const 		*bif
)
{
	dk4_bif_dim_t		 back	= -1L;
	$? "+ dk4bif_get_height %d", TR_IPTR(bif)
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	/*
		Check arguments
	*/
	if (NULL == bif) {
		goto finished;
	}
	if (NULL == bif->cf) {
		goto finished;
	}
	/*	Retrieve width from current frame
	*/
	back = (bif->cf)->dim_y;

	/*	Return
	*/
	finished:
	$? "- dk4bif_get_height %ld", (long)back
	return back;
}



const
dk4_cs_conv_ctx_t *
dk4bif_get_cs_conv_ctx(
	dk4_bif_t const		*bif
)
{
	const dk4_cs_conv_ctx_t	*back	= NULL;
	$? "+ dk4bif_get_cs_conv_ctx %d", TR_IPTR(bif)
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	/*
		Check arguments
	*/
	if (NULL == bif) {
		goto finished;
	}
	/*	Retrieve address of color space conversion context
	*/
	back = &(bif->cs_ctx);

	/*	Return
	*/
	finished:
	$? "- dk4bif_get_cs_conv_ctx %d", TR_IPTR(back)
	return back;
}



void
dk4bif_set_cs_conv_ctx(
	dk4_bif_t					*bif,
	dk4_cs_conv_ctx_t	const	*pctx
)
{
	$? "+ dk4bif_set_cs_conv_ctx bif=%d pctx=%d", TR_IPTR(bif), TR_IPTR(pctx)
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	if ((NULL != bif) && (NULL != pctx)) {
		DK4_MEMCPY(&(bif->cs_ctx), pctx, sizeof(dk4_cs_conv_ctx_t));
	}
	$? "- dk4bif_set_cs_conv_ctx"
}



int
dk4bif_nc_get_original_pixel(
	dk4_px_t				*dptr,
	size_t					 szdptr,
	dk4_bif_t		const	*bif,
	dk4_bif_dim_t			 rowno,
	dk4_bif_dim_t			 colno
)
{
	int		 back	= 0;
	$? "+ dk4bif_nc_get_original_pixel"
#if	DK4_USE_ASSERT
	assert(NULL != bif);
	assert(NULL != dptr);
	assert(0 < szdptr);
#endif
	switch (bif->imgtype) {
		case DK4_BIF_TYPE_PNG : {
#if	DK4_HAVE_PNG_H
			back = dk4bifpng_nc_get_original_pixel(dptr, bif, rowno, colno);
#endif
		} break;
		case DK4_BIF_TYPE_JPEG : {
#if	DK4_HAVE_JPEGLIB_H
			back = dk4bifjpeg_nc_get_original_pixel(
				dptr, szdptr, bif, rowno, colno
			);
#endif
		} break;
		case DK4_BIF_TYPE_TIFF : {
#if	DK4_HAVE_TIFF_H
			back = dk4biftiff_nc_get_original_pixel(dptr, bif, rowno, colno);
#endif
		} break;
		case DK4_BIF_TYPE_NETPBM : {
			back = dk4bifnetpbm_nc_get_original_pixel(dptr, bif, rowno, colno);
		} break;
		/* ##### FURTHER IMAGE TYPES HERE */
	}
	$? "- dk4bif_nc_get_original_pixel %d", back
	return back;
}



#if	TRACE_DEBUG
static
void
show_pixel_data(
	dk4_px_t	*dptr,
	size_t		 szdptr,
	int			 cs
)
{
	size_t	i;		/* Index of current component to process */
	size_t	max;	/* Number of components in color space */

	max = dk4cs_color_space_size(cs);
	if (max > szdptr) { max = szdptr; }
	for (i = 0; i < max; i++) {
		$? ". component[%u] = %lu", (unsigned)i, (unsigned long)(dptr[i])
	}
}
#endif



int
dk4bif_nc_get_pixel(
	dk4_px_t				*dptr,
	size_t					 szdptr,
	dk4_bif_t		const	*bif,
	dk4_bif_dim_t			 rowno,
	dk4_bif_dim_t			 colno,
	int						 cs
)
{
	dk4_px_t		 tmp[4];		/* Temporary pixel data */
	size_t			 sztmp	= DK4_SIZEOF(tmp,dk4_px_t);
	int				 back	= 0;
	$? "+ dk4bif_nc_get_pixel szdptr=%u", (unsigned)szdptr
#if	DK4_USE_ASSERT
	assert(NULL != bif);
	assert(NULL != dptr);
	assert(0 < szdptr);
#endif
	if ((bif->cf)->l_cs == cs) {			$? ". same color space"
		/*
			Same color space
		*/
		back = dk4bif_nc_get_original_pixel(dptr, szdptr, bif, rowno, colno);
		$!trace-code	show_pixel_data(dptr, szdptr, (bif->cf)->l_cs);
		if (0 != back) {
			if (
				((bif->cf)->l_bdepth != (bif->cf)->r_bdepth)
				|| (0 != bif->cf->fmres)
			) {
				/*
					Bit depth correction required
				*/
				$? ". change bit depth"
				dk4pxres_multiple(
					dptr, dk4cs_color_space_size(cs), &((bif->cf)->resampler)
				);
			}
		}
#if TRACE_DEBUG
		else {							$? "! dk4bif_nc_get_original_pixel"
		}
#endif
	}
	else {									$? ". different color spaces"
		/*
			Color space conversion required
		*/
		back = dk4bif_nc_get_original_pixel(tmp, sztmp, bif, rowno, colno);
		$!trace-code	show_pixel_data(tmp, szdptr, (bif->cf)->l_cs);
		if (0 != back) {
			if (
				((bif->cf)->l_bdepth != (bif->cf)->r_bdepth)
				|| (0 != bif->cf->fmres)
			)
			{
				$? ". different bit depths"
				/*
					Bit depth correction required
				*/
				if ((bif->cf)->l_bdepth > (bif->cf)->r_bdepth) {
					$? ". color space conversion before bit depth change"
					/*
						Color space conversion first, bit depth later
					*/
					dk4cs_nc_px_convert(
						dptr, cs, tmp, (bif->cf)->l_cs, &((bif->cf)->bglbd[0]),
						(dk4_px_t)((bif->cf)->resampler.denom), &(bif->cs_ctx)
					);
					dk4pxres_multiple(
						dptr, dk4cs_color_space_size(cs),
						&((bif->cf)->resampler)
					);
				}
				else {
					$? ". bit depth change before color space conversion"
					/*
						Bit depth conversion first, color space later
					*/
					dk4pxres_multiple(
						tmp, dk4cs_color_space_size((bif->cf)->l_cs),
						&((bif->cf)->resampler)
					);
					dk4cs_nc_px_convert(
						dptr, cs, tmp, (bif->cf)->l_cs, &((bif->cf)->bg[0]),
						(dk4_px_t)((bif->cf)->resampler.nom), &(bif->cs_ctx)
					);
				}
			}
			else {							$? ". same bit depth"
				/* No bit depth correction, just change color space
				*/
				dk4cs_nc_px_convert(
					dptr, cs, tmp, (bif->cf)->l_cs, &((bif->cf)->bg[0]),
					(dk4_px_t)((bif->cf)->resampler.denom), &(bif->cs_ctx)
				);
			}
		}
#if TRACE_DEBUG
		else {								$? "! dk4bif_nc_get_original_pixel"
		}
#endif
	}
	$? "- dk4bif_nc_get_pixel %d", back
	return back;
}



int
dk4bif_get_pixel(
	dk4_px_t				*dptr,
	size_t					 szdptr,
	dk4_bif_t		const	*bif,
	dk4_bif_dim_t			 rowno,
	dk4_bif_dim_t			 colno,
	int						 cs
)
{
	int		 back	= 0;
	$? "+ dk4bif_get_pixel ptr=%d", TR_IPTR(dptr)
#if	DK4_USE_ASSERT
	assert(NULL != bif);
	assert(NULL != dptr);
	assert(0 < szdptr);
#endif
	/*
		Check arguments
	*/
	if ((NULL == dptr) || (NULL == bif)) {			$? "! dptr/bif"
		goto finished;
	}
	if (0 != bif->hdronly) {						$? "! hdronly"
		goto finished;
	}
	if (0 == dk4cs_check(cs)) {						$? "! cs"
		goto finished;
	}
	if (szdptr < dk4cs_color_space_size(cs)) {		$? "! szdptr"
		goto finished;
	}
	if (((dk4_bif_dim_t)0L > rowno) || ((bif->cf)->dim_y <= rowno)) { $? "! row"
		goto finished;
	}
	if (((dk4_bif_dim_t)0L > colno) || ((bif->cf)->dim_x <= colno)) { $? "! col"
		goto finished;
	}
	back = dk4bif_nc_get_pixel(dptr, szdptr, bif, rowno, colno, cs);
	$!trace-code	show_pixel_data(dptr, szdptr, cs);

	/*	Return, fill default value 0 if there was any problem
	*/
	finished:
	if (0 == back) {
		if (NULL != dptr) {				$? "! zero fill on error"
			while(0 < szdptr--) {
				*(dptr++) = 0;
			}
		}
	}
	$? "- dk4bif_get_pixel %d", back
	return back;
}



int
dk4bif_get_image_type(
	dk4_bif_t	const	*bif
)
{
	int		back	= -1;
#if	DK4_USE_ASSERT
	assert(NULL != bif);
#endif
	if (NULL != bif) {
		back = bif->imgtype;
	}
	return back;
}



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