/* Copyright (C) 2017-2020, Dirk Krause SPDX-License-Identifier: BSD-3-Clause */ /* WARNING: This file was generated by the dkct program (see http://dktools.sourceforge.net/ for details). Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: dk4bift.ctr */ /** @file dk4bift.c The dk4bift module. */ #line 8 "dk4bift.ctr" #include "dk4conf.h" #line 12 "dk4bift.ctr" #if DK4_HAVE_TIFF_H #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 DK4MAAL_H_INCLUDED #include "dk4maal.h" #endif #if DK4_HAVE_ASSERT_H #ifndef ASSERT_H_INCLUDED #include #define ASSERT_H_INCLUDED 1 #endif #endif void dk4biftiff_nc_release_per_frame_data( void *tsdata ) { dk4_bif_tiff_per_frame_t *pfd = NULL; /* Per-frame data */ #line 48 "dk4bift.ctr" #if DK4_USE_ASSERT assert(NULL != tsdata); #endif pfd = (dk4_bif_tiff_per_frame_t *)tsdata; if (NULL != pfd) { if (NULL != pfd->rows) { #line 54 "dk4bift.ctr" _TIFFfree(pfd->rows); pfd->rows = NULL; } #line 57 "dk4bift.ctr" dk4mem_free(pfd); } #line 60 "dk4bift.ctr" } dk4_bif_t * dk4biftiff_nc_open_type( dkChar const *fn, dk4_cs_conv_ctx_t const *pcsctx, dk4_er_t *erp ) { dk4_er_t er; /* Error report for math op */ dk4_bif_tiff_per_frame_t *pfd = NULL; /* Per-frame data */ dk4_bif_t *back = NULL; TIFF *tiff = NULL; /* TIFF file */ double xrv; /* Temporary resolution valuej */ float rv; /* Temporary resolution value */ uint32 w; /* Frame width */ uint32 h; /* Frame height */ int ok = 0; /* Flag: Success */ int cc = 0; /* Flag: Can continue */ uint16 ru; /* Resolution unit */ uint16 orie; /* Orientation */ tsize_t nby; /* Byte number to allocate */ #line 85 "dk4bift.ctr" back = dk4biftool_nc_new_for_one_frame( fn, DK4_BIF_TYPE_TIFF, 0, pcsctx, erp ); if (NULL == back) { goto finished; } #line 90 "dk4bift.ctr" pfd = dk4mem_new(dk4_bif_tiff_per_frame_t,1,NULL); if (NULL == pfd) { goto finished; } pfd->rows = NULL; #line 95 "dk4bift.ctr" back->cf->tsdata = pfd; #if (DK4_CHAR_SIZE > 1) && (DK4_ON_WINDOWS) tiff = TIFFOpenW(fn, "rb"); #else tiff = TIFFOpen(fn, "rb"); #endif if (NULL == tiff) { goto finished; } #line 105 "dk4bift.ctr" /* Traverse all frames */ ok = 1; do { cc = 0; w = 0UL; h = 0UL; /* Read image width and height */ if (1 == TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w)) { back->cf->dim_x = w; } else { #line 120 "dk4bift.ctr" w = 0UL; } if (1 == TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h)) { back->cf->dim_y = h; } else { #line 126 "dk4bift.ctr" h = 0UL; } /* Read resolution */ rv = 0.0; if (1 == TIFFGetField(tiff, TIFFTAG_XRESOLUTION, &rv)) { back->cf->res_x = rv; #line 134 "dk4bift.ctr" } rv = 0.0; if (1 == TIFFGetField(tiff, TIFFTAG_YRESOLUTION, &rv)) { back->cf->res_y = rv; #line 138 "dk4bift.ctr" } ru = 0; if (1 == TIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &ru)) { if (RESUNIT_CENTIMETER == ru) { #line 142 "dk4bift.ctr" back->cf->res_x *= 2.54; back->cf->res_y *= 2.54; } } if ((0.0 >= back->cf->res_x) || (0.0 >= back->cf->res_y)) { back->cf->res_x = back->cf->res_y = -1.0; #line 149 "dk4bift.ctr" } /* Read orientation, swap x and y coordinates if necessary */ orie = ORIENTATION_TOPLEFT; if (1 == TIFFGetFieldDefaulted(tiff, TIFFTAG_ORIENTATION, &orie)) { switch (orie) { case 5: case 6: case 7: case 8: { #line 157 "dk4bift.ctr" back->cf->dim_x = h; back->cf->dim_y = w; xrv = back->cf->res_x; back->cf->res_x = back->cf->res_y; back->cf->res_y = xrv; } break; } } else { orie = ORIENTATION_TOPLEFT; } /* Calculate byte number */ dk4error_init(&er); nby = dk4ma_long_mul( dk4ma_long_mul(w, h, &er), 4L, &er ); /* Read image data */ if ((DK4_E_NONE == er.ec) && (0L < w) && (0L < h)) { pfd->rows = (uint32 *)_TIFFmalloc(nby); if (NULL != pfd->rows) { if (1 == TIFFReadRGBAImageOriented(tiff,w,h,pfd->rows,orie,0)) { back->cf->bg[0] = 255; #line 182 "dk4bift.ctr" back->cf->bg[1] = 255; back->cf->bg[2] = 255; back->cf->bg[3] = 255; back->cf->bglbd[0] = 255; back->cf->bglbd[1] = 255; back->cf->bglbd[2] = 255; back->cf->bglbd[3] = 255; dk4pxres_setup(&(back->cf->resampler), 8, 8); back->cf->l_bdepth = back->cf->r_bdepth = 8; back->cf->n_comp = 4; back->cf->l_cs = back->cf->f_cs = DK4_CS_RGB_ALPHA; back->cf->hbg = 0; } else { #line 196 "dk4bift.ctr" dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); cc = -1; ok = 0; } } else { #line 202 "dk4bift.ctr" dk4error_set_elsize_nelem( erp, DK4_E_MEMORY_ALLOCATION_FAILED, nby, 1 ); cc = -1; ok = 0; } } else { #line 211 "dk4bift.ctr" dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); cc = -1; ok = 0; } /* Attempt to read one further frame */ if (0 == cc) { if (1 == TIFFReadDirectory(tiff)) { #line 220 "dk4bift.ctr" if (0 != dk4biftool_nc_add_frame(back, erp)) { pfd = dk4mem_new(dk4_bif_tiff_per_frame_t,1,NULL); if (NULL != pfd) { #line 223 "dk4bift.ctr" pfd->rows = NULL; back->cf->tsdata = pfd; cc = 1; } else { #line 228 "dk4bift.ctr" dk4error_set_elsize_nelem( erp, DK4_E_MEMORY_ALLOCATION_FAILED, sizeof(dk4_bif_tiff_per_frame_t), 1 ); ok = 0; cc = -1; } } else { #line 237 "dk4bift.ctr" ok = 0; cc = -1; } } } } while (1 == cc); finished: if (NULL != tiff) { #line 247 "dk4bift.ctr" TIFFClose(tiff); } if (0 == ok) { if (NULL != back) { #line 251 "dk4bift.ctr" dk4bif_close(back); back = NULL; } } #line 256 "dk4bift.ctr" return back; } 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 ) { dk4_bif_tiff_per_frame_t *pfd = NULL; /* Per-frame data */ uint32 abgr; /* Pixel index in array */ int back = 0; #line 273 "dk4bift.ctr" #if DK4_USE_ASSERT assert(NULL != dptr); assert(NULL != bif); #endif if (NULL == bif->cf->tsdata) { #line 278 "dk4bift.ctr" goto finished; } pfd = (dk4_bif_tiff_per_frame_t *)(bif->cf->tsdata); abgr = (pfd->rows)[(rowno * bif->cf->dim_x) + colno]; dptr[0] = TIFFGetR(abgr); dptr[1] = TIFFGetG(abgr); dptr[2] = TIFFGetB(abgr); dptr[3] = TIFFGetA(abgr); back = 1; finished: #line 290 "dk4bift.ctr" return back; } #endif /* if DK4_HAVE_TIFF_H */ /* vim: set ai sw=4 ts=4 : */