summaryrefslogtreecommitdiff
path: root/support/dktools/bif.h
blob: 3f809a2e8dada3c54d2d184b74f2910f110ca8da (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
/*
Copyright 2019-2020, Dirk Krause. All rights reserved.
SPDX-License-Identifier:	BSD-3-Clause
*/
#ifndef	BIF_H_INCLUDED
/**	Protection against multiple inclusion. */
#define	BIF_H_INCLUDED 1

/**	@file	bif.h	Library-internal function prototypes.

This header contains functions used cross-module within the library
but not part of the API and thus not intended for use by a
library consumer.

Consequently this file is not installed to /usr/include or some similar
directory.
*/

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

#if	DK4_HAVE_PNG_H
#ifndef	PNG_H_INCLUDED
#include <png.h>
#define	PNG_H_INCLUDED 1
#endif
#endif

#if	DK4_HAVE_JPEGLIB_H
#ifndef	JPEGLIB_H_INCLUDED
#include <jpeglib.h>
#define	JPEGLIB_H_INCLUDED 1
#endif
#endif

#if	DK4_HAVE_TIFF_H
#ifndef	TIFF_H_INCLUDED
#include <tiff.h>
#define	TIFF_H_INCLUDED 1
#endif
#ifndef TIFFIO_H_INCLUDED
#include <tiffio.h>
#define	TIFFIO_H_INCLUDED 1
#endif
#endif



/**	NetPBM magic numbers (file types).
*/
enum {
					/**	Plain PBM.
					*/
	DK4_NETPBM_P1	= 0,

					/**	Plain PGM.
					*/
	DK4_NETPBM_P2 ,

					/**	Plain PPM (RGB).
					*/
	DK4_NETPBM_P3 ,

					/**	New PBM.
					*/
	DK4_NETPBM_P4 ,

					/**	New PGM.
					*/
	DK4_NETPBM_P5 ,

					/**	New PPM (RGB).
					*/
	DK4_NETPBM_P6 ,

					/**	PAM (any map).
					*/
	DK4_NETPBM_P7
};



/**	NetPBM P7 tuple types.
*/
enum {
										/**	Black and white.
										*/
	DK4_NETPBM_TT_BLACKANDWHITE			= 0 ,

										/**	Grayscale.
										*/
	DK4_NETPBM_TT_GRAYSCALE ,

										/**	RGB.
										*/
	DK4_NETPBM_TT_RGB ,

										/**	Black and white with alpha channel.
										*/
	DK4_NETPBM_TT_BLACKANDWHITE_ALPHA ,

										/**	Grayscale with alpha channel.
										*/
	DK4_NETPBM_TT_GRAYSCALE_ALPHA ,

										/**	RGB with alpha channel.
										*/
	DK4_NETPBM_TT_RGB_ALPHA
};



/**	Data to store per frame for PNG.
*/
typedef struct {
#if	DK4_HAVE_SETJMP_H
	jmp_buf		 jump_buf;		/**< Buffer for long jump. */
#endif
	png_bytep	*rows;			/**< Data for image rows. */
	double		 dblxdpi;		/**< X resolution. */
	double		 dblydpi;		/**< Y resolution. */
	png_size_t	 rowbytes;		/**< Bytes per row. */
	png_uint_32	 w;				/**< Image width. */
	png_uint_32	 h;				/**< Image height. */
	png_uint_32	 xdpi;			/**< X resolution. */
	png_uint_32	 ydpi;			/**< Y resolution. */
	int			 ct;			/**< Color type. */
	int			 im;			/**< Interlace method. */
	int			 cm;			/**< Compression method. */
	int			 fm;			/**< Filter method. */
	int			 ch;			/**< Number of channels. */
	int			 ip;			/**< Number of interlace passes. */
	int			 bd;			/**< Bit depth (bits per component). */
	int			 had_error;		/**< Error flag: 0=none, 1=warning, 2=error. */
} dk4_bif_png_per_frame_t;



/**	Data to store per frame for JPEG.
*/
typedef struct {
	JSAMPLE		**rows;		/**< Image pixel data. */
	size_t		  num_comp;	/**< Number of components per pixel. */
	short		  is_adobe;	/**< Flag: Adobe marker found. */
} dk4_bif_jpeg_per_frame_t;



/**	Data to store per frame for TIFF.
*/
typedef struct {
	uint32		*rows;		/**< Data rows. */
} dk4_bif_tiff_per_frame_t;



/**	Data to store per frame for NetPBM.
*/
typedef struct {
	unsigned char		**rows;		/**< Data rows. */
	dk4_bif_dim_t		  w;		/**< Image width. */
	dk4_bif_dim_t		  h;		/**< Image height. */
	size_t				  ch;		/**< Number of channels. */
	size_t				  vpr;		/**< Number of values per row. */
	size_t				  bpr;		/**< Number of bytes per row. */
	int					  magnum;	/**< Magic number 1-7 (P1-P7). */
	int					  tupt;		/**< Tuple type for P7. */
	dk4_px_t			  maxval;	/**< Maximum value. */
	dk4_px_bit_depth_t	  bpc;		/**< Bits per component. */
} dk4_bif_netpbm_per_frame_t;



/**	Structure containing values proably clobbered by longjmp for PNG.
*/
typedef struct {
	dk4_bif_t				*back;
	dk4_bif_png_per_frame_t	*pfrpng;
	int						 ok;
} dk4_bif_png_clobbered_t;


/**	Structure containing values probably clobbered by longjmp for JPEG.
*/
typedef struct {
	dk4_bif_t				*back;		/**< Function result. */
	int						 st;		/**< Reader state. */
	int						 ok;		/**< Flag to indicate success. */
} dk4_bif_jpg_clobbered_t;


#ifdef	__cplusplus
extern "C" {
#endif

/*	Module dk4bif
	-------------
*/


/**	Retrieve original pixel data for one pixel in original color space
	and bit depth, without checking arguments for validity before.
	This function is intended for library-internal use only, application
	programs should not use it directly.
	@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).
	@return	1 on success, 0 on error.
*/

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
);


/**	Retrieve data for one pixel in the required output bit depth
	and color space, without checking arguments for validity before.
	This function is intended for library-internal use only, application
	programs should not use it directly.
	@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_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
);


/*	Module dk4cs
	------------
*/

/**	Convert pixel data from one color space into another, without
	checking arguments for validity before.
	This function is intended for library-internal use only, application
	programs should not use it directly.
	@param	dptr	Address of destination array.
	@param	dcs		Destination color space.
	@param	dsz		Size of destination array (number of elements).
	@param	sptr	Address of source array.
	@param	scs		Source color space.
	@param	ssz		Source array size (number of elements).
	@param	bptr	Address of background array, may be NULL.
	@param	bsz		Size of background array (number of elements), may be 0.
	@param	max		Maximum value for a component.
	@param	pctx	Conversion context, may be NULL.
*/

void
dk4cs_nc_px_convert(
	dk4_px_t				*dptr,
	int			 			 dcs,
	dk4_px_t const			*sptr,
	int						 scs,
	dk4_px_t const			*bptr,
	dk4_px_t				 max,
	dk4_cs_conv_ctx_t const	*pctx
);


/**	Convert pixel data from one color space into another without
	checking arguments before.
	This function is intended for library-internal use only, application
	programs should not use it directly.
	@param	dptr	Address of destination array, values in interval [0;1].
	@param	dcs		Destination color space.
	@param	dsz		Size of destination array (number of elements).
	@param	sptr	Address of source array, values in interval [0;1].
	@param	scs		Source color space.
	@param	ssz		Source array size (number of elements).
	@param	bptr	Address of background array, values in [0;1], may be NULL.
	@param	bsz		Size of background array (number of elements), may be 0.
	@param	pctx	Conversion context, may be NULL.
*/

void
dk4cs_nc_color_convert(
	double					*dptr,
	int			 			 dcs,
	double const			*sptr,
	int						 scs,
	double const			*bptr,
	dk4_cs_conv_ctx_t const	*pctx
);


/*	Module dk4bifp
	--------------
*/

#if	DK4_HAVE_PNG_H

/**	Release per-frame data for PNG.
	@param	tsdata	Data to release.
*/

void
dk4bifpng_nc_release_per_frame_data(
	void			*tsdata
);


/**	Release per-file data for PNG.
	@param	tsdata	Data to release.
*/

void
dk4bifpng_nc_release_per_file_data(
	void	*tsdata
);


/**	Open PNG file.
	@param	fipo	File, opened for binary reading.
	@param	fn		File name containing the bitmap image.
	@param	honly	Flag: Read image header only, no image data.
	@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.
*/

dk4_bif_t *
dk4bifpng_nc_open_file(
	FILE						*fipo,
	dkChar	const				*fn,
	int							 honly,
	dk4_cs_conv_ctx_t	const	*pcsctx,
	dk4_bif_png_clobbered_t		*pngcp,
	dk4_er_t					*erp
);


/**	Retrieve original data for one pixel, without checking arguments
	for validity before.
	This function is intended for library-internal use only, application
	programs should not use it directly.
	@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).
	@return	1 on success, 0 on error.
*/

int
dk4bifpng_nc_get_original_pixel(
	dk4_px_t				*dptr,
	dk4_bif_t		const	*bif,
	dk4_bif_dim_t			 rowno,
	dk4_bif_dim_t			 colno
);


#endif
/* if DK4_HAVE_PNG_H */


/*	Module dk4bifj
	--------------
*/

#if	DK4_HAVE_JPEGLIB_H

/**	Release per-frame data for JPEG.
	@param	tsdata	Data to release.
	@param	frptr	Frame the data belongs to.
*/

void
dk4bifjpeg_nc_release_per_frame_data(
	void			*tsdata,
	dk4_bif_frame_t	*frptr
);


#if 0
/* No per-file data stored for JPEG */

/**	Release per-file data for JPEG.
	@param	tsdata	Data to release.
*/

void
dk4bifjpeg_nc_release_per_file_data(
	void	*tsdata
);

#endif

/**	Open JPEG file.
	@param	fipo	File, opened for binary reading.
	@param	fn		File name containing the bitmap image.
	@param	honly	Flag: Read image header only, no image data.
	@param	pcsctx	Color space conversion context, may be NULL.
	@param	jpgcp	Structure containing variables which would
					be clobbered by longjmp if they were local.
	@param	erp		Error report, may be NULL.
	@return	Valid pointer to bitmap image file on success, NULL on error.
*/

dk4_bif_t *
dk4bifjpeg_nc_open_file(
	FILE						*fipo,
	dkChar	const				*fn,
	int							 honly,
	dk4_cs_conv_ctx_t	const	*pcsctx,
	dk4_bif_jpg_clobbered_t		*jpgcp,
	dk4_er_t					*erp
);


/**	Retrieve original data for one pixel, without checking arguments
	for validity before.
	This function is intended for library-internal use only, application
	programs should not use it directly.
	@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).
	@return	1 on success, 0 on error.
*/

int
dk4bifjpeg_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
);

#endif
/* if DK4_HAVE_JPEGLIB_H */


/*	Module dk4bift
	--------------
*/

#if	DK4_HAVE_TIFF_H

/**	Release per-frame data for TIFF.
	@param	tsdata	Data to release.
*/

void
dk4biftiff_nc_release_per_frame_data(
	void			*tsdata
);


#if 0
/* No per-file data stored for TIFF */

/**	Release per-file data for TIFF.
	@param	tsdata	Data to release.
*/

void
dk4biftiff_nc_release_per_file_data(
	void	*tsdata
);

#endif


/**	Open TIFF file.
	@param	fn		File name containing the bitmap image.
	@param	honly	Flag: Read image header only, no image data.
	@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.
*/

dk4_bif_t *
dk4biftiff_nc_open_type(
	dkChar	const				*fn,
	dk4_cs_conv_ctx_t	const	*pcsctx,
	dk4_er_t					*erp
);


/**	Retrieve original data for one pixel, without checking arguments
	for validity before.
	This function is intended for library-internal use only, application
	programs should not use it directly.
	@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).
	@return	1 on success, 0 on error.
*/

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
);

#endif
/* if DK4_HAVE_TIFF_H */



/*	Module di4bifn
	--------------
*/

/**	Open NetPBM file.
	@param	fn		File name containing the bitmap image.
	@param	honly	Flag: Read image header only, no image data.
	@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.
*/

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


/*	Module dk4bifto
	---------------
*/


/**	Initialize a newly allocated image file structure.
	@param	bptr	Bitmap image file structure to initialize.
	@param	fn		File name, may be NULL.
	@param	ty		Bitmap image type.
	@param	honly	Flag: Read header only.
	@param	pcsctx	Color space conversion context, may be NULL.
	@param	erp		Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if elsize or nelem is 0,
	- DK4_E_MATH_OVERFLOW<br>
	  on numeric overflow when calculating the
	  product of elsize and nelem,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  with mem.elsize and mem.nelem
	  set if there is not enough memory available.
*/

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


/**	Initialize newly allocated frame structure.
	@param	fptr	Frame to initialize.
*/

void
dk4biftool_nc_frame_init(
	dk4_bif_frame_t	*fptr
);


/**	Add new frame to bitmap image file structure.
	On success, make the new frame the current frame.
	@param	bptr	Bitmap image file.
	@param	erp		Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if elsize or nelem is 0,
	- DK4_E_MATH_OVERFLOW<br>
	  on numeric overflow when calculating the
	  product of elsize and nelem, or if there are too many frames in the
	  image,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  with mem.elsize and mem.nelem
	  set if there is not enough memory available.
*/

int
dk4biftool_nc_add_frame(
	dk4_bif_t		*bptr,
	dk4_er_t		*erp
);


/**	Create new image file structure for a file containing one frame.
	@param	fn		File name.
	@param	ty		Image file type.
	@param	honly	Flag: Read header only.
	@param	pcsctx	Color space conversion context, may be NULL.
	@param	erp		Error report, may be NULL.
	@return	Valid pointer to new structure on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if elsize or nelem is 0,
	- DK4_E_MATH_OVERFLOW<br>
	  on numeric overflow when calculating the
	  product of elsize and nelem, or if there are too many frames in the
	  image,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  with mem.elsize and mem.nelem
	  set if there is not enough memory available.
*/

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


/*
	Module dk4bifbp
	---------------
*/


/**	Retrieve value from a packed bits array.
	@param	buffer	Array address.
	@param	elin	Index of element to retrieve.
	@param	bd		Bit depth of each element: 1, 2, 4, 8 or 16.
	@param	nbytes	Number of bytes in the buffer.
	@return	Element value, 0 as fallback for illegal arguments.
*/

dk4_px_t
dk4bifpackedbits_get_element(
	unsigned char	const	*buffer,
	size_t					 elin,
	size_t					 bd,
	size_t					 nbytes
);


/**	Release per-frame data for TIFF.
	@param	tsdata	Data to release.
*/

void
dk4bifnetpbm_nc_release_per_frame_data(
	void			*tsdata
);


/**	Retrieve original pixel data for one pixel in original color space
	and bit depth, without checking arguments for validity before.
	This function is intended for library-internal use only, application
	programs should not use it directly.
	@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).
	@return	1 on success, 0 on error.
*/

int
dk4bifnetpbm_nc_get_original_pixel(
	dk4_px_t				*dptr,
	dk4_bif_t		const	*bif,
	dk4_bif_dim_t			 rowno,
	dk4_bif_dim_t			 colno
);

#ifdef	__cplusplus
}
#endif

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