1 /* 2 Copyright 2019-2020, Dirk Krause. All rights reserved. 3 SPDX-License-Identifier: BSD-3-Clause 4 */ 5 #ifndef BIF_H_INCLUDED 6 /** Protection against multiple inclusion. */ 7 #define BIF_H_INCLUDED 1 8 9 /** @file bif.h Library-internal function prototypes. 10 11 This header contains functions used cross-module within the library 12 but not part of the API and thus not intended for use by a 13 library consumer. 14 15 Consequently this file is not installed to /usr/include or some similar 16 directory. 17 */ 18 19 #ifndef DK4CONF_H_INCLUDED 20 #if DK4_BUILDING_DKTOOLS4 21 #include "dk4conf.h" 22 #else 23 #include <dktools-4/dk4conf.h> 24 #endif 25 #endif 26 27 #if DK4_HAVE_PNG_H 28 #ifndef PNG_H_INCLUDED 29 #include <png.h> 30 #define PNG_H_INCLUDED 1 31 #endif 32 #endif 33 34 #if DK4_HAVE_JPEGLIB_H 35 #ifndef JPEGLIB_H_INCLUDED 36 #include <jpeglib.h> 37 #define JPEGLIB_H_INCLUDED 1 38 #endif 39 #endif 40 41 #if DK4_HAVE_TIFF_H 42 #ifndef TIFF_H_INCLUDED 43 #include <tiff.h> 44 #define TIFF_H_INCLUDED 1 45 #endif 46 #ifndef TIFFIO_H_INCLUDED 47 #include <tiffio.h> 48 #define TIFFIO_H_INCLUDED 1 49 #endif 50 #endif 51 52 53 54 /** NetPBM magic numbers (file types). 55 */ 56 enum { 57 /** Plain PBM. 58 */ 59 DK4_NETPBM_P1 = 0, 60 61 /** Plain PGM. 62 */ 63 DK4_NETPBM_P2 , 64 65 /** Plain PPM (RGB). 66 */ 67 DK4_NETPBM_P3 , 68 69 /** New PBM. 70 */ 71 DK4_NETPBM_P4 , 72 73 /** New PGM. 74 */ 75 DK4_NETPBM_P5 , 76 77 /** New PPM (RGB). 78 */ 79 DK4_NETPBM_P6 , 80 81 /** PAM (any map). 82 */ 83 DK4_NETPBM_P7 84 }; 85 86 87 88 /** NetPBM P7 tuple types. 89 */ 90 enum { 91 /** Black and white. 92 */ 93 DK4_NETPBM_TT_BLACKANDWHITE = 0 , 94 95 /** Grayscale. 96 */ 97 DK4_NETPBM_TT_GRAYSCALE , 98 99 /** RGB. 100 */ 101 DK4_NETPBM_TT_RGB , 102 103 /** Black and white with alpha channel. 104 */ 105 DK4_NETPBM_TT_BLACKANDWHITE_ALPHA , 106 107 /** Grayscale with alpha channel. 108 */ 109 DK4_NETPBM_TT_GRAYSCALE_ALPHA , 110 111 /** RGB with alpha channel. 112 */ 113 DK4_NETPBM_TT_RGB_ALPHA 114 }; 115 116 117 118 /** Data to store per frame for PNG. 119 */ 120 typedef struct { 121 #if DK4_HAVE_SETJMP_H 122 jmp_buf jump_buf; /**< Buffer for long jump. */ 123 #endif 124 png_bytep *rows; /**< Data for image rows. */ 125 double dblxdpi; /**< X resolution. */ 126 double dblydpi; /**< Y resolution. */ 127 png_size_t rowbytes; /**< Bytes per row. */ 128 png_uint_32 w; /**< Image width. */ 129 png_uint_32 h; /**< Image height. */ 130 png_uint_32 xdpi; /**< X resolution. */ 131 png_uint_32 ydpi; /**< Y resolution. */ 132 int ct; /**< Color type. */ 133 int im; /**< Interlace method. */ 134 int cm; /**< Compression method. */ 135 int fm; /**< Filter method. */ 136 int ch; /**< Number of channels. */ 137 int ip; /**< Number of interlace passes. */ 138 int bd; /**< Bit depth (bits per component). */ 139 int had_error; /**< Error flag: 0=none, 1=warning, 2=error. */ 140 } dk4_bif_png_per_frame_t; 141 142 143 144 /** Data to store per frame for JPEG. 145 */ 146 typedef struct { 147 JSAMPLE **rows; /**< Image pixel data. */ 148 size_t num_comp; /**< Number of components per pixel. */ 149 short is_adobe; /**< Flag: Adobe marker found. */ 150 } dk4_bif_jpeg_per_frame_t; 151 152 153 154 /** Data to store per frame for TIFF. 155 */ 156 typedef struct { 157 uint32 *rows; /**< Data rows. */ 158 } dk4_bif_tiff_per_frame_t; 159 160 161 162 /** Data to store per frame for NetPBM. 163 */ 164 typedef struct { 165 unsigned char **rows; /**< Data rows. */ 166 dk4_bif_dim_t w; /**< Image width. */ 167 dk4_bif_dim_t h; /**< Image height. */ 168 size_t ch; /**< Number of channels. */ 169 size_t vpr; /**< Number of values per row. */ 170 size_t bpr; /**< Number of bytes per row. */ 171 int magnum; /**< Magic number 1-7 (P1-P7). */ 172 int tupt; /**< Tuple type for P7. */ 173 dk4_px_t maxval; /**< Maximum value. */ 174 dk4_px_bit_depth_t bpc; /**< Bits per component. */ 175 } dk4_bif_netpbm_per_frame_t; 176 177 178 179 /** Structure containing values proably clobbered by longjmp for PNG. 180 */ 181 typedef struct { 182 dk4_bif_t *back; 183 dk4_bif_png_per_frame_t *pfrpng; 184 int ok; 185 } dk4_bif_png_clobbered_t; 186 187 188 /** Structure containing values probably clobbered by longjmp for JPEG. 189 */ 190 typedef struct { 191 dk4_bif_t *back; /**< Function result. */ 192 int st; /**< Reader state. */ 193 int ok; /**< Flag to indicate success. */ 194 } dk4_bif_jpg_clobbered_t; 195 196 197 #ifdef __cplusplus 198 extern "C" { 199 #endif 200 201 /* Module dk4bif 202 ------------- 203 */ 204 205 206 /** Retrieve original pixel data for one pixel in original color space 207 and bit depth, without checking arguments for validity before. 208 This function is intended for library-internal use only, application 209 programs should not use it directly. 210 @param dptr Address of destination array, must be large enough. 211 @param szdptr Size of dptr array (number of elements). 212 @param bif Bitmap image file. 213 @param rowno Row number (0 for top row). 214 @param colno Column number (0 for left column). 215 @return 1 on success, 0 on error. 216 */ 217 218 int 219 dk4bif_nc_get_original_pixel( 220 dk4_px_t *dptr, 221 size_t szdptr, 222 dk4_bif_t const *bif, 223 dk4_bif_dim_t rowno, 224 dk4_bif_dim_t colno 225 ); 226 227 228 /** Retrieve data for one pixel in the required output bit depth 229 and color space, without checking arguments for validity before. 230 This function is intended for library-internal use only, application 231 programs should not use it directly. 232 @param dptr Address of destination array, must be large enough. 233 @param szdptr Size of dptr array (number of elements). 234 @param bif Bitmap image file. 235 @param rowno Row number (0 for top row). 236 @param colno Column number (0 for left column). 237 @param cs Required color space. 238 @return 1 on success, 0 on error. 239 */ 240 241 int 242 dk4bif_nc_get_pixel( 243 dk4_px_t *dptr, 244 size_t szdptr, 245 dk4_bif_t const *bif, 246 dk4_bif_dim_t rowno, 247 dk4_bif_dim_t colno, 248 int cs 249 ); 250 251 252 /* Module dk4cs 253 ------------ 254 */ 255 256 /** Convert pixel data from one color space into another, without 257 checking arguments for validity before. 258 This function is intended for library-internal use only, application 259 programs should not use it directly. 260 @param dptr Address of destination array. 261 @param dcs Destination color space. 262 @param dsz Size of destination array (number of elements). 263 @param sptr Address of source array. 264 @param scs Source color space. 265 @param ssz Source array size (number of elements). 266 @param bptr Address of background array, may be NULL. 267 @param bsz Size of background array (number of elements), may be 0. 268 @param max Maximum value for a component. 269 @param pctx Conversion context, may be NULL. 270 */ 271 272 void 273 dk4cs_nc_px_convert( 274 dk4_px_t *dptr, 275 int dcs, 276 dk4_px_t const *sptr, 277 int scs, 278 dk4_px_t const *bptr, 279 dk4_px_t max, 280 dk4_cs_conv_ctx_t const *pctx 281 ); 282 283 284 /** Convert pixel data from one color space into another without 285 checking arguments before. 286 This function is intended for library-internal use only, application 287 programs should not use it directly. 288 @param dptr Address of destination array, values in interval [0;1]. 289 @param dcs Destination color space. 290 @param dsz Size of destination array (number of elements). 291 @param sptr Address of source array, values in interval [0;1]. 292 @param scs Source color space. 293 @param ssz Source array size (number of elements). 294 @param bptr Address of background array, values in [0;1], may be NULL. 295 @param bsz Size of background array (number of elements), may be 0. 296 @param pctx Conversion context, may be NULL. 297 */ 298 299 void 300 dk4cs_nc_color_convert( 301 double *dptr, 302 int dcs, 303 double const *sptr, 304 int scs, 305 double const *bptr, 306 dk4_cs_conv_ctx_t const *pctx 307 ); 308 309 310 /* Module dk4bifp 311 -------------- 312 */ 313 314 #if DK4_HAVE_PNG_H 315 316 /** Release per-frame data for PNG. 317 @param tsdata Data to release. 318 */ 319 320 void 321 dk4bifpng_nc_release_per_frame_data( 322 void *tsdata 323 ); 324 325 326 /** Release per-file data for PNG. 327 @param tsdata Data to release. 328 */ 329 330 void 331 dk4bifpng_nc_release_per_file_data( 332 void *tsdata 333 ); 334 335 336 /** Open PNG file. 337 @param fipo File, opened for binary reading. 338 @param fn File name containing the bitmap image. 339 @param honly Flag: Read image header only, no image data. 340 @param pcsctx Color space conversion context, may be NULL. 341 @param erp Error report, may be NULL. 342 @return Valid pointer to bitmap image file on success, NULL on error. 343 */ 344 345 dk4_bif_t * 346 dk4bifpng_nc_open_file( 347 FILE *fipo, 348 dkChar const *fn, 349 int honly, 350 dk4_cs_conv_ctx_t const *pcsctx, 351 dk4_bif_png_clobbered_t *pngcp, 352 dk4_er_t *erp 353 ); 354 355 356 /** Retrieve original data for one pixel, without checking arguments 357 for validity before. 358 This function is intended for library-internal use only, application 359 programs should not use it directly. 360 @param dptr Address of destination array, must be large enough. 361 @param szdptr Size of dptr array (number of elements). 362 @param bif Bitmap image file. 363 @param rowno Row number (0 for top row). 364 @param colno Column number (0 for left column). 365 @return 1 on success, 0 on error. 366 */ 367 368 int 369 dk4bifpng_nc_get_original_pixel( 370 dk4_px_t *dptr, 371 dk4_bif_t const *bif, 372 dk4_bif_dim_t rowno, 373 dk4_bif_dim_t colno 374 ); 375 376 377 #endif 378 /* if DK4_HAVE_PNG_H */ 379 380 381 /* Module dk4bifj 382 -------------- 383 */ 384 385 #if DK4_HAVE_JPEGLIB_H 386 387 /** Release per-frame data for JPEG. 388 @param tsdata Data to release. 389 @param frptr Frame the data belongs to. 390 */ 391 392 void 393 dk4bifjpeg_nc_release_per_frame_data( 394 void *tsdata, 395 dk4_bif_frame_t *frptr 396 ); 397 398 399 #if 0 400 /* No per-file data stored for JPEG */ 401 402 /** Release per-file data for JPEG. 403 @param tsdata Data to release. 404 */ 405 406 void 407 dk4bifjpeg_nc_release_per_file_data( 408 void *tsdata 409 ); 410 411 #endif 412 413 /** Open JPEG file. 414 @param fipo File, opened for binary reading. 415 @param fn File name containing the bitmap image. 416 @param honly Flag: Read image header only, no image data. 417 @param pcsctx Color space conversion context, may be NULL. 418 @param jpgcp Structure containing variables which would 419 be clobbered by longjmp if they were local. 420 @param erp Error report, may be NULL. 421 @return Valid pointer to bitmap image file on success, NULL on error. 422 */ 423 424 dk4_bif_t * 425 dk4bifjpeg_nc_open_file( 426 FILE *fipo, 427 dkChar const *fn, 428 int honly, 429 dk4_cs_conv_ctx_t const *pcsctx, 430 dk4_bif_jpg_clobbered_t *jpgcp, 431 dk4_er_t *erp 432 ); 433 434 435 /** Retrieve original data for one pixel, without checking arguments 436 for validity before. 437 This function is intended for library-internal use only, application 438 programs should not use it directly. 439 @param dptr Address of destination array, must be large enough. 440 @param szdptr Size of dptr array (number of elements). 441 @param bif Bitmap image file. 442 @param rowno Row number (0 for top row). 443 @param colno Column number (0 for left column). 444 @return 1 on success, 0 on error. 445 */ 446 447 int 448 dk4bifjpeg_nc_get_original_pixel( 449 dk4_px_t *dptr, 450 size_t szdptr, 451 dk4_bif_t const *bif, 452 dk4_bif_dim_t rowno, 453 dk4_bif_dim_t colno 454 ); 455 456 #endif 457 /* if DK4_HAVE_JPEGLIB_H */ 458 459 460 /* Module dk4bift 461 -------------- 462 */ 463 464 #if DK4_HAVE_TIFF_H 465 466 /** Release per-frame data for TIFF. 467 @param tsdata Data to release. 468 */ 469 470 void 471 dk4biftiff_nc_release_per_frame_data( 472 void *tsdata 473 ); 474 475 476 #if 0 477 /* No per-file data stored for TIFF */ 478 479 /** Release per-file data for TIFF. 480 @param tsdata Data to release. 481 */ 482 483 void 484 dk4biftiff_nc_release_per_file_data( 485 void *tsdata 486 ); 487 488 #endif 489 490 491 /** Open TIFF file. 492 @param fn File name containing the bitmap image. 493 @param honly Flag: Read image header only, no image data. 494 @param pcsctx Color space conversion context, may be NULL. 495 @param erp Error report, may be NULL. 496 @return Valid pointer to bitmap image file on success, NULL on error. 497 */ 498 499 dk4_bif_t * 500 dk4biftiff_nc_open_type( 501 dkChar const *fn, 502 dk4_cs_conv_ctx_t const *pcsctx, 503 dk4_er_t *erp 504 ); 505 506 507 /** Retrieve original data for one pixel, without checking arguments 508 for validity before. 509 This function is intended for library-internal use only, application 510 programs should not use it directly. 511 @param dptr Address of destination array, must be large enough. 512 @param szdptr Size of dptr array (number of elements). 513 @param bif Bitmap image file. 514 @param rowno Row number (0 for top row). 515 @param colno Column number (0 for left column). 516 @return 1 on success, 0 on error. 517 */ 518 519 int 520 dk4biftiff_nc_get_original_pixel( 521 dk4_px_t *dptr, 522 dk4_bif_t const *bif, 523 dk4_bif_dim_t rowno, 524 dk4_bif_dim_t colno 525 ); 526 527 #endif 528 /* if DK4_HAVE_TIFF_H */ 529 530 531 532 /* Module di4bifn 533 -------------- 534 */ 535 536 /** Open NetPBM file. 537 @param fn File name containing the bitmap image. 538 @param honly Flag: Read image header only, no image data. 539 @param pcsctx Color space conversion context, may be NULL. 540 @param erp Error report, may be NULL. 541 @return Valid pointer to bitmap image file on success, NULL on error. 542 */ 543 544 dk4_bif_t * 545 dk4bifnetpbm_nc_open_file( 546 FILE *fipo, 547 dkChar const *fn, 548 int honly, 549 dk4_cs_conv_ctx_t const *pcsctx, 550 dk4_er_t *erp 551 ); 552 553 554 /* Module dk4bifto 555 --------------- 556 */ 557 558 559 /** Initialize a newly allocated image file structure. 560 @param bptr Bitmap image file structure to initialize. 561 @param fn File name, may be NULL. 562 @param ty Bitmap image type. 563 @param honly Flag: Read header only. 564 @param pcsctx Color space conversion context, may be NULL. 565 @param erp Error report, may be NULL. 566 @return 1 on success, 0 on error. 567 568 Error codes: 569 - DK4_E_INVALID_ARGUMENTS<br> 570 if elsize or nelem is 0, 571 - DK4_E_MATH_OVERFLOW<br> 572 on numeric overflow when calculating the 573 product of elsize and nelem, 574 - DK4_E_MEMORY_ALLOCATION_FAILED<br> 575 with mem.elsize and mem.nelem 576 set if there is not enough memory available. 577 */ 578 579 int 580 dk4biftool_nc_bif_init( 581 dk4_bif_t *bptr, 582 dkChar const *fn, 583 int ty, 584 int honly, 585 dk4_cs_conv_ctx_t const *pcsctx, 586 dk4_er_t *erp 587 ); 588 589 590 /** Initialize newly allocated frame structure. 591 @param fptr Frame to initialize. 592 */ 593 594 void 595 dk4biftool_nc_frame_init( 596 dk4_bif_frame_t *fptr 597 ); 598 599 600 /** Add new frame to bitmap image file structure. 601 On success, make the new frame the current frame. 602 @param bptr Bitmap image file. 603 @param erp Error report, may be NULL. 604 @return 1 on success, 0 on error. 605 606 Error codes: 607 - DK4_E_INVALID_ARGUMENTS<br> 608 if elsize or nelem is 0, 609 - DK4_E_MATH_OVERFLOW<br> 610 on numeric overflow when calculating the 611 product of elsize and nelem, or if there are too many frames in the 612 image, 613 - DK4_E_MEMORY_ALLOCATION_FAILED<br> 614 with mem.elsize and mem.nelem 615 set if there is not enough memory available. 616 */ 617 618 int 619 dk4biftool_nc_add_frame( 620 dk4_bif_t *bptr, 621 dk4_er_t *erp 622 ); 623 624 625 /** Create new image file structure for a file containing one frame. 626 @param fn File name. 627 @param ty Image file type. 628 @param honly Flag: Read header only. 629 @param pcsctx Color space conversion context, may be NULL. 630 @param erp Error report, may be NULL. 631 @return Valid pointer to new structure on success, NULL on error. 632 633 Error codes: 634 - DK4_E_INVALID_ARGUMENTS<br> 635 if elsize or nelem is 0, 636 - DK4_E_MATH_OVERFLOW<br> 637 on numeric overflow when calculating the 638 product of elsize and nelem, or if there are too many frames in the 639 image, 640 - DK4_E_MEMORY_ALLOCATION_FAILED<br> 641 with mem.elsize and mem.nelem 642 set if there is not enough memory available. 643 */ 644 645 dk4_bif_t * 646 dk4biftool_nc_new_for_one_frame( 647 dkChar const *fn, 648 int ty, 649 int honly, 650 dk4_cs_conv_ctx_t const *pcsctx, 651 dk4_er_t *erp 652 ); 653 654 655 /* 656 Module dk4bifbp 657 --------------- 658 */ 659 660 661 /** Retrieve value from a packed bits array. 662 @param buffer Array address. 663 @param elin Index of element to retrieve. 664 @param bd Bit depth of each element: 1, 2, 4, 8 or 16. 665 @param nbytes Number of bytes in the buffer. 666 @return Element value, 0 as fallback for illegal arguments. 667 */ 668 669 dk4_px_t 670 dk4bifpackedbits_get_element( 671 unsigned char const *buffer, 672 size_t elin, 673 size_t bd, 674 size_t nbytes 675 ); 676 677 678 /** Release per-frame data for TIFF. 679 @param tsdata Data to release. 680 */ 681 682 void 683 dk4bifnetpbm_nc_release_per_frame_data( 684 void *tsdata 685 ); 686 687 688 /** Retrieve original pixel data for one pixel in original color space 689 and bit depth, without checking arguments for validity before. 690 This function is intended for library-internal use only, application 691 programs should not use it directly. 692 @param dptr Address of destination array, must be large enough. 693 @param szdptr Size of dptr array (number of elements). 694 @param bif Bitmap image file. 695 @param rowno Row number (0 for top row). 696 @param colno Column number (0 for left column). 697 @return 1 on success, 0 on error. 698 */ 699 700 int 701 dk4bifnetpbm_nc_get_original_pixel( 702 dk4_px_t *dptr, 703 dk4_bif_t const *bif, 704 dk4_bif_dim_t rowno, 705 dk4_bif_dim_t colno 706 ); 707 708 #ifdef __cplusplus 709 } 710 #endif 711 712 /* vim: set ai sw=4 ts=4 : */ 713 #endif 714 715