/* 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 #endif #endif #if DK4_HAVE_PNG_H #ifndef PNG_H_INCLUDED #include #define PNG_H_INCLUDED 1 #endif #endif #if DK4_HAVE_JPEGLIB_H #ifndef JPEGLIB_H_INCLUDED #include #define JPEGLIB_H_INCLUDED 1 #endif #endif #if DK4_HAVE_TIFF_H #ifndef TIFF_H_INCLUDED #include #define TIFF_H_INCLUDED 1 #endif #ifndef TIFFIO_H_INCLUDED #include #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
if elsize or nelem is 0, - DK4_E_MATH_OVERFLOW
on numeric overflow when calculating the product of elsize and nelem, - DK4_E_MEMORY_ALLOCATION_FAILED
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
if elsize or nelem is 0, - DK4_E_MATH_OVERFLOW
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
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
if elsize or nelem is 0, - DK4_E_MATH_OVERFLOW
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
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