1 /*
2 Copyright (C) 2017-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5 
6 /*
7 	WARNING: This file was generated by the dkct program (see
8 	http://dktools.sourceforge.net/ for details).
9 	Changes you make here will be lost if dkct is run again!
10 	You should modify the original source and run dkct on it.
11 	Original source: dk4bif.ctr
12 */
13 
14 /**	@file dk4bif.c The dk4bif module.
15 */
16 
17 
18 #ifndef	DK4CONF_H_INCLUDED
19 #include "dk4conf.h"
20 #endif
21 
22 #ifndef	DK4MEM_H_INCLUDED
23 #include <libdk4base/dk4mem.h>
24 #endif
25 
26 #ifndef	DK4BIF_H_INCLUDED
27 #include <libdk4bif/dk4bif.h>
28 #endif
29 
30 #ifndef	BIF_H_INCLUDED
31 #include <libdk4bif/bif.h>
32 #endif
33 
34 #ifndef	DK4PATHD_H_INCLUDED
35 #include <libdk4c/dk4pathd.h>
36 #endif
37 
38 #ifndef	DK4STRD_H_INCLUDED
39 #include <libdk4base/dk4strd.h>
40 #endif
41 
42 #ifndef	DK4FOPD_H_INCLUDED
43 #include <libdk4c/dk4fopd.h>
44 #endif
45 
46 #if DK4_HAVE_ASSERT_H
47 #ifndef	ASSERT_H_INCLUDED
48 #include <assert.h>
49 #define	ASSERT_H_INCLUDED 1
50 #endif
51 #endif
52 
53 
54 
55 
56 
57 /**	File name suffixes for bitmap image files.
58 */
59 static const dkChar * const	dk4bif_suffixes[] = {
60 /* 0 */
61 dkT(".png"),
62 
63 /* 1 */
64 dkT(".jpg"),
65 
66 /* 2 */
67 dkT(".jpeg"),
68 
69 /* 3 */
70 dkT(".tif"),
71 
72 /* 4 */
73 dkT(".tiff"),
74 
75 /* 5 */
76 dkT(".pbm"),
77 
78 /* 6 */
79 dkT(".pgm"),
80 
81 /* 7 */
82 dkT(".ppm"),
83 
84 /* 8 */
85 dkT(".pnm"),
86 
87 /* 9 */
88 dkT(".pam"),
89 
90 NULL
91 
92 };
93 
94 
95 /**	Keywords used by the module.
96 */
97 static const dkChar * const	dk4bif_kw[] = {
98 /* 0 */
99 dkT("rb"),
100 
101 NULL
102 
103 };
104 
105 
106 
107 void
dk4bif_frame_close(dk4_bif_frame_t * frptr,int imgtype)108 dk4bif_frame_close(
109 	dk4_bif_frame_t	*frptr,
110 	int				 imgtype
111 )
112 {
113 
114 #if	DK4_USE_ASSERT
115 	assert(NULL != frptr);
116 #endif
117 	if (NULL != frptr) {
118 		if (NULL != frptr->tsdata) {
119 			switch (imgtype) {
120 				case DK4_BIF_TYPE_PNG : {
121 #if	DK4_HAVE_PNG_H
122 					dk4bifpng_nc_release_per_frame_data(frptr->tsdata);
123 #else
124 					dk4mem_release(frptr->tsdata);
125 #endif
126 				} break;
127 				case DK4_BIF_TYPE_JPEG : {
128 #if	DK4_HAVE_JPEGLIB_H
129 					dk4bifjpeg_nc_release_per_frame_data(frptr->tsdata, frptr);
130 #else
131 					dk4mem_release(frptr->tsdata);
132 #endif
133 				} break;
134 				case DK4_BIF_TYPE_TIFF : {
135 #if	DK4_HAVE_TIFF_H
136 					dk4biftiff_nc_release_per_frame_data(frptr->tsdata);
137 #else
138 					dk4mem_release(frptr->tsdata);
139 #endif
140 				} break;
141 				case DK4_BIF_TYPE_NETPBM : {
142 					dk4bifnetpbm_nc_release_per_frame_data(frptr->tsdata);
143 				} break;
144 				default : {
145 					dk4mem_release(frptr->tsdata);
146 				} break;
147 			}
148 			frptr->tsdata = NULL;
149 		}
150 		dk4mem_free(frptr);
151 	}
152 
153 }
154 
155 
156 
157 void
dk4bif_close(dk4_bif_t * bif)158 dk4bif_close(
159 	dk4_bif_t		*bif
160 )
161 {
162 	dk4_bif_frame_t		*frptr;		/* Traverse all frames */
163 
164 #if	DK4_USE_ASSERT
165 	assert(NULL != bif);
166 #endif
167 	if (NULL != bif) {
168 		if (NULL != bif->s_frames) {
169 			if (NULL != bif->i_frames) {
170 				dk4sto_it_reset(bif->i_frames);
171 				do {
172 					frptr = (dk4_bif_frame_t *)dk4sto_it_next(bif->i_frames);
173 					if (NULL != frptr) {
174 						dk4bif_frame_close(frptr, bif->imgtype);
175 					}
176 				} while(NULL != frptr);
177 				dk4sto_it_close(bif->i_frames);
178 				bif->i_frames = NULL;
179 			}
180 			dk4sto_close(bif->s_frames);
181 			bif->s_frames = NULL;
182 		}
183 		bif->cf = NULL;
184 		if (NULL != bif->tsdata) {
185 			switch (bif->imgtype) {
186 				case DK4_BIF_TYPE_PNG : {
187 #if	DK4_HAVE_PNG_H
188 					dk4bifpng_nc_release_per_file_data(bif->tsdata);
189 #else
190 					dk4mem_release(bif->tsdata);
191 #endif
192 				} break;
193 				case DK4_BIF_TYPE_JPEG : {
194 #if 0
195 /* No per-file data stored for JPEG file */
196 #if	DK4_HAVE_JPEGLIB_H
197 					dk4bifjpeg_nc_release_per_file_data(bif->tsdata);
198 #else
199 					dk4mem_release(bif->tsdata);
200 #endif
201 #endif
202 				} break;
203 				case DK4_BIF_TYPE_TIFF : {
204 #if 0
205 /* No per-file data stored for TIFF */
206 #if	DK4_HAVE_TIFF_H
207 					dk4biftiff_nc_release_per_file_data(bif->tsdata);
208 #else
209 					dk4mem_release(bif->tsdata);
210 #endif
211 #endif
212 				} break;
213 				case DK4_BIF_TYPE_NETPBM : {
214 					/*
215 						No per-file data stored for NetPBM.
216 					*/
217 				} break;
218 				default : {
219 					dk4mem_release(bif->tsdata);
220 				} break;
221 			}
222 		}
223 		dk4mem_release(bif->filename);
224 		bif->n_frames = 0;
225 		bif->imgtype = 0;
226 		dk4mem_free(bif);
227 	}
228 
229 }
230 
231 
232 
233 int
dk4bif_type_for_suffix(dkChar const * fn)234 dk4bif_type_for_suffix(
235 	dkChar	const	*fn
236 )
237 {
238 	const dkChar	*sptr;			/* Pointer to dot of file name suffix */
239 	int				 back	= -1;
240 
241 #if	DK4_USE_ASSERT
242 	assert(NULL != fn);
243 #endif
244 	if (NULL != fn) {
245 		sptr = dk4path_get_suffix(fn, NULL);
246 
247 		if (NULL != sptr) {
248 			switch (dk4str_array_index(dk4bif_suffixes, sptr, 0)) {
249 				case 0 : {
250 					back = DK4_BIF_TYPE_PNG;
251 				} break;
252 				case 1 : case 2 : {
253 					back = DK4_BIF_TYPE_JPEG;
254 				} break;
255 				case 3 : case 4 : {
256 					back = DK4_BIF_TYPE_TIFF;
257 				} break;
258 				case 5 : case 6 : case 7 : case 8 : case 9 : {
259 					back = DK4_BIF_TYPE_NETPBM;
260 				} break;
261 			}
262 		}
263 	}
264 
265 	return back;
266 }
267 
268 
269 
270 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)271 dk4bif_open_file(
272 	FILE						*fipo,
273 	dkChar	const				*fn,
274 	int							 ty,
275 	int							 honly,
276 	dk4_cs_conv_ctx_t	const	*pcsctx,
277 	dk4_er_t					*erp
278 )
279 {
280 	dk4_bif_png_clobbered_t	 pngcp;	/* Clobber protected variables */
281 	dk4_bif_jpg_clobbered_t	 jpgcp;	/* Clobber protected variables */
282 	dk4_bif_t				*back	= NULL;
283 
284 #if	DK4_USE_ASSERT
285 	assert(NULL != fipo);
286 #endif
287 	if (NULL == fipo) {
288 		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
289 		goto finished;
290 	}
291 	switch (ty) {
292 		case DK4_BIF_TYPE_PNG : {
293 #if	DK4_HAVE_PNG_H
294 			pngcp.back = NULL;
295 			pngcp.pfrpng = NULL;
296 			pngcp.ok = 0;
297 			back = dk4bifpng_nc_open_file(fipo, fn, honly, pcsctx, &pngcp, erp);
298 #else
299 			dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
300 #endif
301 		} break;
302 		case DK4_BIF_TYPE_JPEG : {
303 #if	DK4_HAVE_JPEGLIB_H
304 			jpgcp.st = 0;
305 			jpgcp.ok = 0;
306 			jpgcp.back = NULL;
307 			back = dk4bifjpeg_nc_open_file(fipo,fn,honly,pcsctx,&jpgcp,erp);
308 #else
309 			dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
310 #endif
311 		} break;
312 		case DK4_BIF_TYPE_NETPBM : {
313 
314 			back = dk4bifnetpbm_nc_open_file(fipo, fn, honly, pcsctx, erp);
315 
316 		} break;
317 		default : {
318 			dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
319 		} break;
320 	}
321 	finished:
322 
323 	return back;
324 }
325 
326 
327 
328 dk4_bif_t *
dk4bif_open_type(dkChar const * fn,int ty,int honly,dk4_cs_conv_ctx_t const * pcsctx,dk4_er_t * erp)329 dk4bif_open_type(
330 	dkChar	const				*fn,
331 	int							 ty,
332 	int							 honly,
333 	dk4_cs_conv_ctx_t	const	*pcsctx,
334 	dk4_er_t					*erp
335 )
336 {
337 	FILE		*inputfile;							/* Input file */
338 	dk4_bif_t	*back	= NULL;
339 	int			 tests	= DK4_FOPEN_SC_IS_REGULAR;	/* Tests for fopen */
340 
341 #if	DK4_USE_ASSERT
342 	assert(NULL != fn);
343 #endif
344 	if (NULL == fn) {
345 		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
346 		goto finished;
347 	}
348 	if ((DK4_BIF_TYPE_MIN > ty) || (DK4_BIF_TYPE_MAX < ty)) {
349 		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
350 		goto finished;
351 	}
352 	switch (ty) {
353 		case DK4_BIF_TYPE_PNG :
354 		case DK4_BIF_TYPE_JPEG :
355 		case DK4_BIF_TYPE_NETPBM : {
356 			inputfile = dk4fopen(fn, dk4bif_kw[0], tests, erp);
357 			if (NULL != inputfile) {
358 				back = dk4bif_open_file(inputfile, fn, ty, honly, pcsctx, erp);
359 				fclose(inputfile);
360 			}
361 		} break;
362 		case DK4_BIF_TYPE_TIFF : {
363 #if	DK4_HAVE_TIFF_H
364 			back = dk4biftiff_nc_open_type(fn, pcsctx, erp);
365 #else
366 			dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
367 #endif
368 		} break;
369 	}
370 	finished:
371 
372 	return back;
373 }
374 
375 
376 
377 dk4_bif_t *
dk4bif_open(dkChar const * fn,int hdronly,dk4_cs_conv_ctx_t const * pcsctx,dk4_er_t * erp)378 dk4bif_open(
379 	dkChar	const				*fn,
380 	int							 hdronly,
381 	dk4_cs_conv_ctx_t	const	*pcsctx,
382 	dk4_er_t					*erp
383 )
384 {
385 	dk4_bif_t	*back	= NULL;
386 	int			 tp;				/* Bitmap image file type */
387 
388 #if	DK4_USE_ASSERT
389 	assert(NULL != fn);
390 #endif
391 	if (NULL != fn) {
392 		tp = dk4bif_type_for_suffix(fn);
393 		if ((DK4_BIF_TYPE_MIN <= tp) && (DK4_BIF_TYPE_MAX >= tp)) {
394 			back = dk4bif_open_type(fn, tp, hdronly, pcsctx, erp);
395 		}
396 		else {
397 			dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
398 		}
399 	}
400 	else {
401 		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
402 	}
403 
404 	return back;
405 }
406 
407 
408 
409 size_t
dk4bif_get_number_of_frames(dk4_bif_t const * bif)410 dk4bif_get_number_of_frames(
411 	dk4_bif_t const		*bif
412 )
413 {
414 	size_t	back	= 0;
415 
416 #if	DK4_USE_ASSERT
417 	assert(NULL != bif);
418 #endif
419 	if (NULL != bif) {
420 		back = bif->n_frames;
421 	}
422 
423 	return back;
424 }
425 
426 
427 
428 int
dk4bif_set_current_frame(dk4_bif_t * bif,size_t fno)429 dk4bif_set_current_frame(
430 	dk4_bif_t		*bif,
431 	size_t			 fno
432 )
433 {
434 	int		 back = 0;
435 
436 #if	DK4_USE_ASSERT
437 	assert(NULL != bif);
438 #endif
439 	/*
440 		Check arguments
441 	*/
442 	if (NULL == bif) {
443 		goto finished;
444 	}
445 	if ((NULL == bif->s_frames) || (NULL == bif->i_frames)) {
446 		goto finished;
447 	}
448 	/*	Retrieve frames from storage iterator until found
449 	*/
450 	dk4sto_it_reset(bif->i_frames);
451 	do {
452 		bif->cf = (dk4_bif_frame_t *)dk4sto_it_next(bif->i_frames);
453 		if (NULL == bif->cf) {
454 			goto finished;
455 		}
456 	} while(0 < fno--);
457 	if (NULL != bif->cf) { back = 1; }
458 
459 	/*	Return
460 	*/
461 	finished:
462 
463 	return back;
464 }
465 
466 
467 
468 dk4_px_bit_depth_t
dk4bif_get_original_bit_depth(dk4_bif_t const * bif)469 dk4bif_get_original_bit_depth(
470 	dk4_bif_t const		*bif
471 )
472 {
473 	dk4_px_bit_depth_t	back	= 0;
474 
475 #if	DK4_USE_ASSERT
476 	assert(NULL != bif);
477 #endif
478 	/*
479 		Check arguments
480 	*/
481 	if (NULL == bif) {
482 		goto finished;
483 	}
484 	if (NULL == bif->cf) {
485 		goto finished;
486 	}
487 	/*	Retrieve bit depth from current frame
488 	*/
489 	back = (bif->cf)->l_bdepth;
490 
491 	/*	Return
492 	*/
493 	finished:
494 
495 	return back;
496 }
497 
498 
499 
500 int
dk4bif_get_color_space(dk4_bif_t const * bif)501 dk4bif_get_color_space(
502 	dk4_bif_t const		*bif
503 )
504 {
505 	int		 back	= -1;
506 
507 #if	DK4_USE_ASSERT
508 	assert(NULL != bif);
509 #endif
510 	/*
511 		Check arguments
512 	*/
513 	if (NULL == bif) {
514 		goto finished;
515 	}
516 	if (NULL == bif->cf) {
517 		goto finished;
518 	}
519 	/*	Retrieve library color space from current frame
520 	*/
521 	back = (bif->cf)->l_cs;
522 
523 	/*	Return
524 	*/
525 	finished:
526 
527 	return back;
528 }
529 
530 
531 
532 int
dk4bif_get_file_color_space(dk4_bif_t const * bif)533 dk4bif_get_file_color_space(
534 	dk4_bif_t const		*bif
535 )
536 {
537 	int		 back	= -1;
538 
539 #if	DK4_USE_ASSERT
540 	assert(NULL != bif);
541 #endif
542 	/*
543 		Check arguments
544 	*/
545 	if (NULL == bif) {
546 		goto finished;
547 	}
548 	if (NULL == bif->cf) {
549 		goto finished;
550 	}
551 	/*	Retrieve file color space from current frame
552 	*/
553 	back = (bif->cf)->f_cs;
554 
555 	/*	Return
556 	*/
557 	finished:
558 
559 	return back;
560 }
561 
562 
563 
564 int
dk4bif_set_output_bit_depth(dk4_bif_t * bif,dk4_px_bit_depth_t bd)565 dk4bif_set_output_bit_depth(
566 	dk4_bif_t			*bif,
567 	dk4_px_bit_depth_t	 bd
568 )
569 {
570 	dk4_bif_frame_t		*fptr;			/* Traverse all frames in storage */
571 	int					 back	= 0;
572 
573 #if	DK4_USE_ASSERT
574 	assert(NULL != bif);
575 #endif
576 	/*
577 		Check arguments
578 	*/
579 	if (NULL == bif) {
580 		goto finished;
581 	}
582 	if ((NULL == bif->s_frames) || (NULL == bif->i_frames)) {
583 		goto finished;
584 	}
585 	if (0 != bif->hdronly) {
586 		goto finished;
587 	}
588 	/*	Traverse all frames, set required bit depth and resampler in frame
589 	*/
590 	dk4sto_it_reset(bif->i_frames);
591 	do {
592 		fptr = (dk4_bif_frame_t *)dk4sto_it_next(bif->i_frames);
593 		if (NULL != fptr) {
594 			if (0 != fptr->fmres) {
595 				dk4pxres_setup_from_maxval(&(fptr->resampler), fptr->fmrmv, bd);
596 			}
597 			else {
598 				dk4pxres_setup(&(fptr->resampler), fptr->l_bdepth, bd);
599 			}
600 			fptr->r_bdepth = bd;
601 
602 
603 
604 			dk4mem_cpy(
605 				&(fptr->bg[0]), &(fptr->bglbd[0]), 4*sizeof(dk4_px_t), NULL
606 			);
607 			if ((fptr->l_bdepth != bd) || (0 != fptr->fmres)) {
608 				dk4pxres_multiple(&(fptr->bg[0]), 4, &(fptr->resampler));
609 			}
610 
611 
612 
613 		}
614 	} while (NULL != fptr);
615 	/*
616 		Indicate success
617 	*/
618 	back = 1;
619 
620 	/*	Return
621 	*/
622 	finished:
623 
624 	return back;
625 }
626 
627 
628 
629 int
dk4bif_set_background(dk4_bif_t * bif,dk4_px_t const * bg,size_t szbg,dk4_px_bit_depth_t bd,int force)630 dk4bif_set_background(
631 	dk4_bif_t					*bif,
632 	dk4_px_t			const	*bg,
633 	size_t						 szbg,
634 	dk4_px_bit_depth_t			 bd,
635 	int							 force
636 )
637 {
638 	dk4_px_resample_t	 bgres;		/* Resample bg data to required bit depth */
639 	dk4_bif_frame_t		*fptr;		/* Traverse all frames */
640 	size_t				 i;			/* Current bg component index */
641 	int					 back	= 0;
642 
643 #if	DK4_USE_ASSERT
644 	assert(NULL != bif);
645 #endif
646 	/*
647 		Check arguments
648 	*/
649 	if ((NULL == bif) || (NULL == bg)) {
650 		goto finished;
651 	}
652 	if ((NULL == bif->s_frames) || (NULL == bif->i_frames)) {
653 		goto finished;
654 	}
655 	if (0 != bif->hdronly) {
656 		goto finished;
657 	}
658 	/*	Correct number of background arguments
659 	*/
660 	if (szbg > 4) {
661 		szbg = 4;
662 	}
663 	/*	Traverse all frames, set background in the frames bit depth
664 	*/
665 	dk4sto_it_reset(bif->i_frames);
666 	do {
667 		fptr = (dk4_bif_frame_t *)dk4sto_it_next(bif->i_frames);
668 		if (NULL != fptr) {
669 			if ((0 != force) || (0 == (bif->cf)->hbg)) {
670 				if (0 != fptr->fmres) {
671 					dk4pxres_setup_to_maxval(&bgres, bd, fptr->fmrmv);
672 				}
673 				else {
674 					dk4pxres_setup(&bgres, bd, (fptr->l_bdepth));
675 				}
676 				for (i = 0; i < szbg; i++) {
677 					fptr->bg[i] = dk4pxres_get(&bgres, bg[i]);
678 					fptr->bglbd[i] = bg[i];
679 				}
680 			}
681 		}
682 	} while (NULL != fptr);
683 	/*
684 		Indicate success
685 	*/
686 	back = 1;
687 
688 	/*	Return
689 	*/
690 	finished:
691 
692 	return back;
693 }
694 
695 
696 
697 double
dk4bif_get_xres(dk4_bif_t const * bif)698 dk4bif_get_xres(
699 	dk4_bif_t const		*bif
700 )
701 {
702 	double		 back	= -1.0;
703 
704 #if	DK4_USE_ASSERT
705 	assert(NULL != bif);
706 #endif
707 	if (NULL == bif) {
708 		goto finished;
709 	}
710 	if (NULL == bif->cf) {
711 		goto finished;
712 	}
713 	/*	Retrieve x resolution from current frame
714 	*/
715 	back = (bif->cf)->res_x;
716 
717 	/*	Return
718 	*/
719 	finished:
720 
721 	return back;
722 }
723 
724 
725 
726 double
dk4bif_get_yres(dk4_bif_t const * bif)727 dk4bif_get_yres(
728 	dk4_bif_t const		*bif
729 )
730 {
731 	double		 back	= -1.0;
732 
733 #if	DK4_USE_ASSERT
734 	assert(NULL != bif);
735 #endif
736 	/*
737 		Check arguments
738 	*/
739 	if (NULL == bif) {
740 		goto finished;
741 	}
742 	if (NULL == bif->cf) {
743 		goto finished;
744 	}
745 	/*	Retrieve y resolution from current frame
746 	*/
747 	back = (bif->cf)->res_y;
748 
749 	/*	Return
750 	*/
751 	finished:
752 
753 	return back;
754 }
755 
756 
757 
758 int
dk4bif_set_res(dk4_bif_t * bif,double xres,double yres,dk4_er_t * erp)759 dk4bif_set_res(
760 	dk4_bif_t			*bif,
761 	double				 xres,
762 	double				 yres,
763 	dk4_er_t			*erp
764 )
765 {
766 	int		 back	= 0;
767 
768 #if	DK4_USE_ASSERT
769 	assert(NULL != bif);
770 #endif
771 	if ((NULL != bif) && (0.0 < xres) && (0.0 < yres)) {
772 		if (NULL != bif->cf) {
773 			bif->cf->res_x = xres;
774 			bif->cf->res_y = yres;
775 			back = 1;
776 		}
777 		else {
778 			dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
779 		}
780 	}
781 	else {
782 		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
783 	}
784 
785 	return back;
786 }
787 
788 
789 
790 dk4_bif_dim_t
dk4bif_get_width(dk4_bif_t const * bif)791 dk4bif_get_width(
792 	dk4_bif_t const		 *bif
793 )
794 {
795 	dk4_bif_dim_t		 back	= -1L;
796 
797 #if	DK4_USE_ASSERT
798 	assert(NULL != bif);
799 #endif
800 	/*
801 		Check arguments
802 	*/
803 	if (NULL == bif) {
804 		goto finished;
805 	}
806 	if (NULL == bif->cf) {
807 		goto finished;
808 	}
809 	/*	Retrieve width of current frame
810 	*/
811 	back = (bif->cf)->dim_x;
812 
813 	/*	Return
814 	*/
815 	finished:
816 
817 	return back;
818 }
819 
820 
821 
822 dk4_bif_dim_t
dk4bif_get_height(dk4_bif_t const * bif)823 dk4bif_get_height(
824 	dk4_bif_t const 		*bif
825 )
826 {
827 	dk4_bif_dim_t		 back	= -1L;
828 
829 #if	DK4_USE_ASSERT
830 	assert(NULL != bif);
831 #endif
832 	/*
833 		Check arguments
834 	*/
835 	if (NULL == bif) {
836 		goto finished;
837 	}
838 	if (NULL == bif->cf) {
839 		goto finished;
840 	}
841 	/*	Retrieve width from current frame
842 	*/
843 	back = (bif->cf)->dim_y;
844 
845 	/*	Return
846 	*/
847 	finished:
848 
849 	return back;
850 }
851 
852 
853 
854 const
855 dk4_cs_conv_ctx_t *
dk4bif_get_cs_conv_ctx(dk4_bif_t const * bif)856 dk4bif_get_cs_conv_ctx(
857 	dk4_bif_t const		*bif
858 )
859 {
860 	const dk4_cs_conv_ctx_t	*back	= NULL;
861 
862 #if	DK4_USE_ASSERT
863 	assert(NULL != bif);
864 #endif
865 	/*
866 		Check arguments
867 	*/
868 	if (NULL == bif) {
869 		goto finished;
870 	}
871 	/*	Retrieve address of color space conversion context
872 	*/
873 	back = &(bif->cs_ctx);
874 
875 	/*	Return
876 	*/
877 	finished:
878 
879 	return back;
880 }
881 
882 
883 
884 void
dk4bif_set_cs_conv_ctx(dk4_bif_t * bif,dk4_cs_conv_ctx_t const * pctx)885 dk4bif_set_cs_conv_ctx(
886 	dk4_bif_t					*bif,
887 	dk4_cs_conv_ctx_t	const	*pctx
888 )
889 {
890 
891 #if	DK4_USE_ASSERT
892 	assert(NULL != bif);
893 #endif
894 	if ((NULL != bif) && (NULL != pctx)) {
895 		DK4_MEMCPY(&(bif->cs_ctx), pctx, sizeof(dk4_cs_conv_ctx_t));
896 	}
897 
898 }
899 
900 
901 
902 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)903 dk4bif_nc_get_original_pixel(
904 	dk4_px_t				*dptr,
905 	size_t					 szdptr,
906 	dk4_bif_t		const	*bif,
907 	dk4_bif_dim_t			 rowno,
908 	dk4_bif_dim_t			 colno
909 )
910 {
911 	int		 back	= 0;
912 
913 #if	DK4_USE_ASSERT
914 	assert(NULL != bif);
915 	assert(NULL != dptr);
916 	assert(0 < szdptr);
917 #endif
918 	switch (bif->imgtype) {
919 		case DK4_BIF_TYPE_PNG : {
920 #if	DK4_HAVE_PNG_H
921 			back = dk4bifpng_nc_get_original_pixel(dptr, bif, rowno, colno);
922 #endif
923 		} break;
924 		case DK4_BIF_TYPE_JPEG : {
925 #if	DK4_HAVE_JPEGLIB_H
926 			back = dk4bifjpeg_nc_get_original_pixel(
927 				dptr, szdptr, bif, rowno, colno
928 			);
929 #endif
930 		} break;
931 		case DK4_BIF_TYPE_TIFF : {
932 #if	DK4_HAVE_TIFF_H
933 			back = dk4biftiff_nc_get_original_pixel(dptr, bif, rowno, colno);
934 #endif
935 		} break;
936 		case DK4_BIF_TYPE_NETPBM : {
937 			back = dk4bifnetpbm_nc_get_original_pixel(dptr, bif, rowno, colno);
938 		} break;
939 		/* ##### FURTHER IMAGE TYPES HERE */
940 	}
941 
942 	return back;
943 }
944 
945 
946 
947 #if	TRACE_DEBUG
948 static
949 void
show_pixel_data(dk4_px_t * dptr,size_t szdptr,int cs)950 show_pixel_data(
951 	dk4_px_t	*dptr,
952 	size_t		 szdptr,
953 	int			 cs
954 )
955 {
956 	size_t	i;		/* Index of current component to process */
957 	size_t	max;	/* Number of components in color space */
958 
959 	max = dk4cs_color_space_size(cs);
960 	if (max > szdptr) { max = szdptr; }
961 	for (i = 0; i < max; i++) {
962 
963 	}
964 }
965 #endif
966 
967 
968 
969 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)970 dk4bif_nc_get_pixel(
971 	dk4_px_t				*dptr,
972 	size_t					 szdptr,
973 	dk4_bif_t		const	*bif,
974 	dk4_bif_dim_t			 rowno,
975 	dk4_bif_dim_t			 colno,
976 	int						 cs
977 )
978 {
979 	dk4_px_t		 tmp[4];		/* Temporary pixel data */
980 	size_t			 sztmp	= DK4_SIZEOF(tmp,dk4_px_t);
981 	int				 back	= 0;
982 
983 #if	DK4_USE_ASSERT
984 	assert(NULL != bif);
985 	assert(NULL != dptr);
986 	assert(0 < szdptr);
987 #endif
988 	if ((bif->cf)->l_cs == cs) {
989 		/*
990 			Same color space
991 		*/
992 		back = dk4bif_nc_get_original_pixel(dptr, szdptr, bif, rowno, colno);
993 
994 		if (0 != back) {
995 			if (
996 				((bif->cf)->l_bdepth != (bif->cf)->r_bdepth)
997 				|| (0 != bif->cf->fmres)
998 			) {
999 				/*
1000 					Bit depth correction required
1001 				*/
1002 
1003 				dk4pxres_multiple(
1004 					dptr, dk4cs_color_space_size(cs), &((bif->cf)->resampler)
1005 				);
1006 			}
1007 		}
1008 #if TRACE_DEBUG
1009 		else {
1010 		}
1011 #endif
1012 	}
1013 	else {
1014 		/*
1015 			Color space conversion required
1016 		*/
1017 		back = dk4bif_nc_get_original_pixel(tmp, sztmp, bif, rowno, colno);
1018 
1019 		if (0 != back) {
1020 			if (
1021 				((bif->cf)->l_bdepth != (bif->cf)->r_bdepth)
1022 				|| (0 != bif->cf->fmres)
1023 			)
1024 			{
1025 
1026 				/*
1027 					Bit depth correction required
1028 				*/
1029 				if ((bif->cf)->l_bdepth > (bif->cf)->r_bdepth) {
1030 
1031 					/*
1032 						Color space conversion first, bit depth later
1033 					*/
1034 					dk4cs_nc_px_convert(
1035 						dptr, cs, tmp, (bif->cf)->l_cs, &((bif->cf)->bglbd[0]),
1036 						(dk4_px_t)((bif->cf)->resampler.denom), &(bif->cs_ctx)
1037 					);
1038 					dk4pxres_multiple(
1039 						dptr, dk4cs_color_space_size(cs),
1040 						&((bif->cf)->resampler)
1041 					);
1042 				}
1043 				else {
1044 
1045 					/*
1046 						Bit depth conversion first, color space later
1047 					*/
1048 					dk4pxres_multiple(
1049 						tmp, dk4cs_color_space_size((bif->cf)->l_cs),
1050 						&((bif->cf)->resampler)
1051 					);
1052 					dk4cs_nc_px_convert(
1053 						dptr, cs, tmp, (bif->cf)->l_cs, &((bif->cf)->bg[0]),
1054 						(dk4_px_t)((bif->cf)->resampler.nom), &(bif->cs_ctx)
1055 					);
1056 				}
1057 			}
1058 			else {
1059 				/* No bit depth correction, just change color space
1060 				*/
1061 				dk4cs_nc_px_convert(
1062 					dptr, cs, tmp, (bif->cf)->l_cs, &((bif->cf)->bg[0]),
1063 					(dk4_px_t)((bif->cf)->resampler.denom), &(bif->cs_ctx)
1064 				);
1065 			}
1066 		}
1067 #if TRACE_DEBUG
1068 		else {
1069 		}
1070 #endif
1071 	}
1072 
1073 	return back;
1074 }
1075 
1076 
1077 
1078 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)1079 dk4bif_get_pixel(
1080 	dk4_px_t				*dptr,
1081 	size_t					 szdptr,
1082 	dk4_bif_t		const	*bif,
1083 	dk4_bif_dim_t			 rowno,
1084 	dk4_bif_dim_t			 colno,
1085 	int						 cs
1086 )
1087 {
1088 	int		 back	= 0;
1089 
1090 #if	DK4_USE_ASSERT
1091 	assert(NULL != bif);
1092 	assert(NULL != dptr);
1093 	assert(0 < szdptr);
1094 #endif
1095 	/*
1096 		Check arguments
1097 	*/
1098 	if ((NULL == dptr) || (NULL == bif)) {
1099 		goto finished;
1100 	}
1101 	if (0 != bif->hdronly) {
1102 		goto finished;
1103 	}
1104 	if (0 == dk4cs_check(cs)) {
1105 		goto finished;
1106 	}
1107 	if (szdptr < dk4cs_color_space_size(cs)) {
1108 		goto finished;
1109 	}
1110 	if (((dk4_bif_dim_t)0L > rowno) || ((bif->cf)->dim_y <= rowno)) {
1111 		goto finished;
1112 	}
1113 	if (((dk4_bif_dim_t)0L > colno) || ((bif->cf)->dim_x <= colno)) {
1114 		goto finished;
1115 	}
1116 	back = dk4bif_nc_get_pixel(dptr, szdptr, bif, rowno, colno, cs);
1117 
1118 
1119 	/*	Return, fill default value 0 if there was any problem
1120 	*/
1121 	finished:
1122 	if (0 == back) {
1123 		if (NULL != dptr) {
1124 			while(0 < szdptr--) {
1125 				*(dptr++) = 0;
1126 			}
1127 		}
1128 	}
1129 
1130 	return back;
1131 }
1132 
1133 
1134 
1135 int
dk4bif_get_image_type(dk4_bif_t const * bif)1136 dk4bif_get_image_type(
1137 	dk4_bif_t	const	*bif
1138 )
1139 {
1140 	int		back	= -1;
1141 #if	DK4_USE_ASSERT
1142 	assert(NULL != bif);
1143 #endif
1144 	if (NULL != bif) {
1145 		back = bif->imgtype;
1146 	}
1147 	return back;
1148 }
1149 
1150 
1151 
1152 /* vim: set ai sw=4 ts=4 : */
1153 
1154