Changeset 221:b89e669daefd


Ignore:
Timestamp:
10/10/2012 11:41:02 AM (8 years ago)
Author:
Dmitry Fedorov <fedorov@…>
Branch:
default
Phase:
public
Message:

new tinytiff lib, full support for bigtiff and ome-bigtiff

Files:
2 added
2 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • libsrc/libbioimg/formats/tiff/dim_cz_lsm_format_io.cpp

    r173 r221  
    2323#include "xstring.h"
    2424
    25 void read_text_tag(TIFF *tif, TDimTiffIFD *ifd, DIM_UINT tag, MemIOBuf *outIOBuf);
    26 
     25void read_text_tag(bim::TinyTiff::IFD *ifd, DIM_UINT tag, MemIOBuf *outIOBuf);
    2726
    2827//----------------------------------------------------------------------------
     
    216215  if (tiffParams == NULL) return false;
    217216  if (tiffParams->dimTiff->tif_flags&TIFF_BIGTIFF) return false;   
    218   if (isTagPresentInFirstIFD( &tiffParams->ifds, TIFFTAG_CZ_LSMINFO ) == TRUE) return TRUE;
    219   return FALSE;
     217  return tiffParams->ifds.tagPresentInFirstIFD(TIFFTAG_CZ_LSMINFO);
    220218}
    221219
    222220void doSwabLSMINFO(CZ_LSMINFO *b) {
    223  
    224221  TIFFSwabArrayOfLong( (uint32*) &b->u32MagicNumber, 10 );
    225222  TIFFSwabArrayOfDouble( (double*) &b->f64VoxelSizeX, 3 );
     
    241238  if (tiffParams == NULL) return 1;
    242239  if (tiffParams->dimTiff == NULL) return 1;
    243   if (tiffParams->ifds.count <= 0) return 1;
     240  if (!tiffParams->ifds.isValid()) return 1;
     241  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     242  if (!ifd) return 1;
    244243
    245244  TDimImageInfo *info = &tiffParams->info;
     
    267266
    268267  DIM_UCHAR *buf = NULL;
    269   uint32 size, type;
    270   if (!isTagPresentInFirstIFD( &tiffParams->ifds, TIFFTAG_CZ_LSMINFO )) return 1;
    271   readTiffTag (tiffParams->dimTiff, &tiffParams->ifds.ifds[0], TIFFTAG_CZ_LSMINFO, size, type, &buf);
     268  uint64 size;
     269  uint16 type;
     270  if (!ifd->tagPresent(TIFFTAG_CZ_LSMINFO )) return 1;
     271  ifd->readTag(TIFFTAG_CZ_LSMINFO, size, type, &buf);
    272272  if ( (size <= 0) || (buf == NULL) ) return 1;
    273   if (bigendian) doSwabLSMINFO((CZ_LSMINFO *) buf);
     273  if (bim::TinyTiff::bigendian) doSwabLSMINFO((CZ_LSMINFO *) buf);
    274274
    275275  lsm->lsm_info = * (CZ_LSMINFO *) buf;
    276   freeTiffTagBuf( &buf );
     276  _TIFFfree(buf);
    277277
    278278  //---------------------------------------------------------------
     
    281281  memset( &lsm->lsm_colors, 0, sizeof(CZ_ChannelColors) );
    282282  if (lsm->lsm_info.u32OffsetChannelColors > 0) {
    283     readTiffBufNoAlloc( tiffParams->dimTiff, lsm->lsm_info.u32OffsetChannelColors, sizeof(CZ_ChannelColors), TIFF_LONG, (unsigned char *) &lsm->lsm_colors );
     283    ifd->readBufNoAlloc( lsm->lsm_info.u32OffsetChannelColors, sizeof(CZ_ChannelColors), TIFF_LONG, (unsigned char *) &lsm->lsm_colors );
    284284
    285285    // here we have to assign channel names reading them from the file, but never seen this used...
     
    297297  memset( &lsm->lsm_TimeStamps, 0, sizeof(CZ_TimeStamps) );
    298298  if (lsm->lsm_info.u32OffsetTimeStamps > 0) {
    299     readTiffBufNoAlloc( tiffParams->dimTiff, lsm->lsm_info.u32OffsetTimeStamps, sizeof(CZ_TimeStamps), TIFF_LONG, (unsigned char *) &lsm->lsm_TimeStamps );
     299    ifd->readBufNoAlloc( lsm->lsm_info.u32OffsetTimeStamps, sizeof(CZ_TimeStamps), TIFF_LONG, (unsigned char *) &lsm->lsm_TimeStamps );
    300300
    301301    // here we have to assign channel names reading them from the file, but never seen this used...
     
    372372  if (tif->tif_readproc((thandle_t) tif->tif_fd, &si, size) < (int) size) return 1;
    373373
    374   if (bigendian) {
     374  if (bim::TinyTiff::bigendian) {
    375375    TIFFSwabArrayOfLong( (uint32*) &si.u32Entry, 1 );
    376376    TIFFSwabArrayOfLong( (uint32*) &si.u32Type, 1 );
     
    392392  if (tiffParams == NULL) return;
    393393  if (tiffParams->dimTiff == NULL) return;
    394   if (tiffParams->ifds.count <= 0) return;
     394  if (!tiffParams->ifds.isValid()) return;
    395395
    396396  TDimImageInfo *info = &tiffParams->info;
  • libsrc/libbioimg/formats/tiff/dim_fluoview_format_io.cpp

    r191 r221  
    3333void change_0_to_n (char *str, long size);
    3434
    35 void read_text_tag(TIFF *tif, TDimTiffIFD *ifd, DIM_UINT tag, MemIOBuf *outIOBuf);
     35void read_text_tag(bim::TinyTiff::IFD *ifd, DIM_UINT tag, MemIOBuf *outIOBuf);
    3636
    3737//----------------------------------------------------------------------------
     
    5858  if (!tiffParams) return false;
    5959  if (tiffParams->dimTiff->tif_flags&TIFF_BIGTIFF) return false;   
    60   return (isTagPresentInFirstIFD(&tiffParams->ifds, BIM_MMHEADER) &&
    61           isTagPresentInFirstIFD(&tiffParams->ifds, BIM_MMSTAMP) &&
    62           isTagPresentInFirstIFD(&tiffParams->ifds, BIM_MMUSERBLOCK) );
     60  return (tiffParams->ifds.tagPresentInFirstIFD(BIM_MMHEADER) &&
     61          tiffParams->ifds.tagPresentInFirstIFD(BIM_MMSTAMP) &&
     62          tiffParams->ifds.tagPresentInFirstIFD(BIM_MMUSERBLOCK) );
    6363}
    6464
     
    6666  if (!tiffParams) return false;
    6767  if (tiffParams->dimTiff->tif_flags&TIFF_BIGTIFF) return false;   
    68   return (isTagPresentInFirstIFD(&tiffParams->ifds, BIM_MMHEADER) &&
    69           isTagPresentInFirstIFD(&tiffParams->ifds, BIM_MMSTAMP) &&
    70           isTagPresentInFirstIFD(&tiffParams->ifds, BIM_ANDORBLOCK) );
     68  return (tiffParams->ifds.tagPresentInFirstIFD(BIM_MMHEADER) &&
     69          tiffParams->ifds.tagPresentInFirstIFD(BIM_MMSTAMP) &&
     70          tiffParams->ifds.tagPresentInFirstIFD(BIM_ANDORBLOCK) );
    7171}
    7272
     
    130130  if (tiffParams == NULL) return 1;
    131131  if (tiffParams->dimTiff == NULL) return 1;
    132   if (tiffParams->ifds.count <= 0) return 1;
     132  if (!tiffParams->ifds.isValid()) return 1;
    133133
    134134  TDimImageInfo *info = &tiffParams->info;
    135135  DFluoviewInfo *fvi = &tiffParams->fluoviewInfo;
     136  MM_HEAD *fvInfo;
     137
     138  if (!tiffParams->ifds.tagPresentInFirstIFD(BIM_MMHEADER)) return 1;
     139  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     140  if (!ifd) return 1;
     141
     142
    136143  DIM_UCHAR *buf = NULL;
    137   uint32 size, type;
    138   MM_HEAD *fvInfo;
    139 
    140   if (!isTagPresentInFirstIFD( &tiffParams->ifds, BIM_MMHEADER )) return 1;
    141 
    142   readTiffTag (tiffParams->dimTiff, &tiffParams->ifds.ifds[0], BIM_MMHEADER, size, type, &buf);
     144  uint64 size;
     145  uint16 type;
     146  ifd->readTag(BIM_MMHEADER, size, type, &buf);
    143147  if (!buf || size<sizeof(MM_HEAD)) return 1;
    144   if (bigendian) doSwabMMHEAD((MM_HEAD *) buf);
     148  if (bim::TinyTiff::bigendian) doSwabMMHEAD((MM_HEAD *) buf);
    145149
    146150  fvi->head = * (MM_HEAD *) buf;
    147   freeTiffTagBuf( &buf );
     151  _TIFFfree(buf);
    148152  fvInfo = &fvi->head;
    149153
     
    191195  tiffParams->info.depth = bitspersample;
    192196
    193   TDimTiffIFD *ifd = &tiffParams->ifds.ifds[0];
    194   TIFF *tif = tiffParams->dimTiff;
     197  //TDimTiffIFD *ifd = &tiffParams->ifds.ifds[0];
     198  //TIFF *tif = tiffParams->dimTiff;
    195199
    196200  //-------------------------------------------------------------------- 
     
    201205    fvi->sample_names[i] = xstring::xprintf( "Channel %d", i+1 );
    202206
    203   if (isTagPresentInIFD(ifd, 270)) {
    204     uint32 buf_size, buf_type;
    205     DIM_UCHAR *buf = NULL;
    206     readTiffTag (tif, ifd, 270, buf_size, buf_type, &buf);
    207     change_0_to_n ((char *) buf, buf_size);
     207  if (ifd->tagPresent(270)) {
     208    buf = NULL;
     209    ifd->readTag (270, size, type, &buf);
     210    change_0_to_n ((char *) buf, size);
    208211
    209212    // Extract channel names
     
    260263
    261264
    262     freeTiffTagBuf( (DIM_UCHAR **) &buf );
     265    _TIFFfree(buf);
    263266  }
    264267
     
    291294// in fluoview luts are saved as INI text in tag: 270 - Image Description
    292295void fluoviewInitPalette( DTiffParams *tiffParams, TDimImageInfo *info ) {
    293   uint32 buf_size;
    294   uint32 buf_type; 
    295   DIM_UCHAR *buf = NULL;
    296   TIFF *tif = tiffParams->dimTiff;
    297   TDimTiffIFD *ifd = &tiffParams->ifds.ifds[0];
    298 
    299   if (!tif) return;
    300   if (!info) return;
     296  if (!info && !tiffParams) return; 
    301297  if (tiffParams->subType!=tstFluoview) return;
    302298  if (tiffParams->fluoviewInfo.ch > 1) return;
    303   if (!isTagPresentInIFD(ifd, 270)) return;
    304 
    305   readTiffTag (tif, ifd, 270, buf_size, buf_type, &buf);
     299
     300  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     301  if (!ifd) return;
     302  if (!ifd->tagPresent(270)) return;
     303
     304
     305  uint64 buf_size;
     306  uint16 buf_type; 
     307  DIM_UCHAR *buf = NULL;
     308  ifd->readTag (270, buf_size, buf_type, &buf);
    306309  change_0_to_n ((char *) buf, buf_size);
    307310
     
    319322  info->lut.count = 256;
    320323
    321   freeTiffTagBuf( (DIM_UCHAR **) &buf );
     324  _TIFFfree(buf);
    322325}
    323326
     
    396399  // Additional info from Tag 270
    397400  //----------------------------------------------------------------------------
    398   TDimTiffIFD *ifd = &tiffParams->ifds.ifds[0];
    399   TIFF *tif = tiffParams->dimTiff;
    400   xstring tag_270 = read_tag_as_string(tif, ifd, 270);
     401  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     402  if (!ifd) return;
     403  xstring tag_270 = ifd->readTagString(270);
    401404
    402405  //----------------------------------------------------------------------------
     
    456459  // Additional info from Tag 270
    457460  //----------------------------------------------------------------------------
    458   TDimTiffIFD *ifd = &tiffParams->ifds.ifds[0];
    459   TIFF *tif = tiffParams->dimTiff;
    460   xstring tag_270 = read_tag_as_string(tif, ifd, 270);
     461  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     462  if (!ifd) return;
     463  xstring tag_270 = ifd->readTagString(270);
    461464
    462465  //----------------------------------------------------------------------------
  • libsrc/libbioimg/formats/tiff/dim_ometiff_format_io.cpp

    r173 r221  
    3030bool omeTiffIsValid(DTiffParams *par) {
    3131  if (!par) return false;
    32   if (par->dimTiff->tif_flags&TIFF_BIGTIFF) return false;       
    33   TDimTiffIFD *ifd = &par->ifds.ifds[0];
    34   TIFF *tif = par->dimTiff;
    35   if (!tif) return false;
     32  bim::TinyTiff::IFD *ifd = par->ifds.firstIfd();
    3633  if (!ifd) return false;
    37 
    38   if (!isTagPresentInFirstIFD( &par->ifds, TIFFTAG_IMAGEDESCRIPTION )) return false;
    39   xstring tag_270 = read_tag_as_string(tif, ifd, TIFFTAG_IMAGEDESCRIPTION );
     34  if (!ifd->tagPresent(TIFFTAG_IMAGEDESCRIPTION )) return false;
     35  xstring tag_270 = ifd->readTagString(TIFFTAG_IMAGEDESCRIPTION );
    4036  if ( tag_270.contains("<OME") && tag_270.contains("<Image") && tag_270.contains("<Pixels") ) return true;
    41 
    4237  return false;
    4338}
     
    6863  if (!par) return 1;
    6964  if (!par->dimTiff) return 1;
    70   if (par->ifds.count <= 0) return 1;
    71 
    72   TDimTiffIFD *ifd = &par->ifds.ifds[0];
     65  bim::TinyTiff::IFD *ifd = par->ifds.firstIfd();
     66  if (!ifd) return false;
     67
    7368  TIFF *tif = par->dimTiff;
    74   if (!tif) return false;
    75   if (!ifd) return false;
    76 
    7769  TDimImageInfo *info = &par->info;
    7870  DOMETiffInfo *ome = &par->omeTiffInfo;
    7971
    8072  // Read OME-XML from image description tag
    81   if (!isTagPresentInFirstIFD( &par->ifds, TIFFTAG_IMAGEDESCRIPTION )) return false;
    82   xstring tag_270 = read_tag_as_string(tif, ifd, TIFFTAG_IMAGEDESCRIPTION );
     73  if (!ifd->tagPresent(TIFFTAG_IMAGEDESCRIPTION )) return false;
     74  xstring tag_270 = ifd->readTagString(TIFFTAG_IMAGEDESCRIPTION );
    8375  if (tag_270.size()<=0) return false;
    8476
     
    269261  if (!par->dimTiff) return 1;
    270262  if (par->subType != tstOmeTiff) return 1; 
    271   if (par->ifds.count <= 0) return 1;
    272 
    273   TDimTiffIFD *ifd = &par->ifds.ifds[0];
     263  bim::TinyTiff::IFD *ifd = par->ifds.firstIfd();
     264  if (!ifd) return 1;
    274265  TIFF *tif = par->dimTiff;
    275266  if (!tif) return false;
    276   if (!ifd) return false;
    277267
    278268  TDimImageInfo *info = &par->info;
     
    388378  //----------------------------------------------------------------------------
    389379
    390   TDimTiffIFD *ifd = &par->ifds.ifds[0];
     380  bim::TinyTiff::IFD *ifd = par->ifds.firstIfd();
     381  if (!ifd) return 1; 
    391382  TIFF *tif = par->dimTiff;
    392   xstring tag_270 = read_tag_as_string(tif, ifd, TIFFTAG_IMAGEDESCRIPTION);
     383
     384  xstring tag_270 = ifd->readTagString(TIFFTAG_IMAGEDESCRIPTION);
    393385  if (tag_270.size()<=0) return 0;
    394386  hash->append_tag( bim::RAW_TAGS_PREFIX+"ome-tiff", tag_270 );
  • libsrc/libbioimg/formats/tiff/dim_psia_format_io.cpp

    r173 r221  
    3232bool psiaIsTiffValid(DTiffParams *tiffParams) {
    3333  if (tiffParams == NULL) return FALSE;
    34   if (tiffParams->dimTiff->tif_flags&TIFF_BIGTIFF) return false;   
    35   if (isTagPresentInFirstIFD( &tiffParams->ifds, 50434 ) == TRUE) return TRUE;
    36   if (isTagPresentInFirstIFD( &tiffParams->ifds, 50435 ) == TRUE) return TRUE;
    37   return FALSE;
     34  if (tiffParams->dimTiff->tif_flags&TIFF_BIGTIFF) return false; 
     35  if (tiffParams->ifds.tagPresentInFirstIFD(50434)) return true;
     36  if (tiffParams->ifds.tagPresentInFirstIFD(50435)) return true;
     37  return false;
    3838}
    3939
     
    4949  if (tiffParams == NULL) return 1;
    5050  if (tiffParams->dimTiff == NULL) return 1;
    51   if (tiffParams->ifds.count <= 0) return 1;
    52 
    53   DIM_UCHAR *buf = NULL;
    54   uint32 size, type;
     51  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     52  if (!ifd) return 1;
    5553  psiaInfoHeader *psiaInfo = &tiffParams->psiaInfo;
    5654
    57   if (!isTagPresentInFirstIFD( &tiffParams->ifds, 50435 )) return 1;
    58 
    59   readTiffTag (tiffParams->dimTiff, &tiffParams->ifds.ifds[0], 50435, size, type, &buf);
    60   if ( (size <= 0) || (buf == NULL) ) return 1;
     55  if (!ifd->tagPresent(50435)) return 1;
     56
     57  std::vector<uint8> bufv;
     58  ifd->readTag(50435, &bufv);
     59  if (bufv.size()<=0) return 1;
     60  uint8 *buf = &bufv[0];
    6161
    6262  psiaInfo->dfLPFStrength = * (DIM_FLOAT64 *) (buf + DIM_PSIA_OFFSET_LPFSSTRENGTH);
     
    8888
    8989  // if running the MSB machine (motorola, power pc) then swap
    90   if (bigendian) {
     90  if (bim::TinyTiff::bigendian) {
    9191    TIFFSwabDouble ( &psiaInfo->dfLPFStrength ); 
    9292    TIFFSwabLong   ( (uint32 *) &psiaInfo->bAutoFlatten );
     
    121121  wstr2charcpy (psiaInfo->szUnitW,         (char *) (buf + DIM_PSIA_OFFSET_UNIT), 8);
    122122
    123   freeTiffTagBuf( &buf );
    124 
    125123  return 0;
    126124}
     
    151149  if (img        == 0) return 1;
    152150  if (tiffParams->dimTiff == 0) return 1;
     151  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     152  if (!ifd) return 1;
    153153
    154154  DIM_UINT sample = 0;
    155   DIM_UCHAR *buf = NULL;
    156   uint32 size, type;
    157155  register unsigned int y;
    158156  DIM_UCHAR *p, *p2;
     
    168166  // read actual image
    169167  //--------------------------------------------------------------------
    170   size = getImgSizeInBytes( img );
    171   readTiffTag (tiffParams->dimTiff, &tiffParams->ifds.ifds[0], 50434, size, type, &buf);
    172   if ( (size <= 0) || (buf == NULL) ) return 1;
     168  std::vector<uint8> bufv;
     169  ifd->readTag(50434, &bufv);
     170  if (bufv.size()<getImgSizeInBytes(img)) return 1;
     171  uint8 *buf = &bufv[0]; 
    173172
    174173  DIM_UINT32 line_size = img->i.width*2;
     
    184183  }
    185184
    186   if (bigendian) //if (swabflag)
    187     TIFFSwabArrayOfShort( (DIM_UINT16 *) img->bits[0], size/2 );
    188   freeTiffTagBuf( &buf );
     185  if (bim::TinyTiff::bigendian)
     186    TIFFSwabArrayOfShort( (DIM_UINT16 *) img->bits[0], getImgSizeInBytes(img)/2 );
    189187
    190188  // psia data is stored inverted
     
    253251  hash->append_tag( bim::CUSTOM_TAGS_PREFIX+"Z Offset", meta->dfZOffset );
    254252  hash->append_tag( bim::CUSTOM_TAGS_PREFIX+"NCompression", (const int) meta->ncompression );
    255 
    256 
    257   TIFF *tif = tiffParams->dimTiff;
    258   TDimTiffIFD *ifd = &tiffParams->ifds.ifds[0];
    259   xstring psia_comments = read_tag_as_string(tif, ifd, 50436);
     253 
     254  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     255  if (!ifd) return 1;
     256  xstring psia_comments = ifd->readTagString(50436);
    260257  if (psia_comments.size()>0)
    261258    hash->append_tag( bim::CUSTOM_TAGS_PREFIX+"Comments", psia_comments );
  • libsrc/libbioimg/formats/tiff/dim_stk_format_io.cpp

    r173 r221  
    398398
    399399bool stkIsTiffValid(DTiffParams *tiffParams) {
    400   if (tiffParams == NULL) return FALSE;
    401   if (tiffParams->dimTiff->tif_flags&TIFF_BIGTIFF) return FALSE;
     400  if (tiffParams == NULL) return false;
     401  if (tiffParams->dimTiff->tif_flags&TIFF_BIGTIFF) return false;
    402402
    403403  // if tag 33629 exists then the file is valid STAK file
    404   if (isTagPresentInFirstIFD( &tiffParams->ifds, 33629 ) == TRUE) return TRUE;
    405   if (isTagPresentInFirstIFD( &tiffParams->ifds, 33630 ) == TRUE) return TRUE;
    406   if (isTagPresentInFirstIFD( &tiffParams->ifds, 33631 ) == TRUE) return TRUE;
    407 
    408   return FALSE;
     404  if (tiffParams->ifds.tagPresentInFirstIFD(33629)) return true;
     405  if (tiffParams->ifds.tagPresentInFirstIFD(33630)) return true;
     406  if (tiffParams->ifds.tagPresentInFirstIFD(33631)) return true;
     407
     408  return false;
    409409}
    410410
    411411template<typename Tb, typename To>
    412 void copy_clean_tag_buffer( Tb **buf, unsigned int size_in_bytes, To **out ) {
     412void copy_clean_tag_buffer( Tb **buf, uint64 size_in_bytes, To **out ) {
    413413  if (!*buf) return;
    414   unsigned int n = size_in_bytes / sizeof(Tb);
    415   unsigned int out_size_in_bytes = n * sizeof(To);
     414  uint64 n = size_in_bytes / sizeof(Tb);
     415  uint64 out_size_in_bytes = n * sizeof(To);
    416416  *out = (To *) _TIFFmalloc( out_size_in_bytes );
    417417
     
    445445  if (tiffParams->subType != tstStk) return;
    446446
    447   TDimTiffIFD *ifd = &tiffParams->ifds.ifds[0];
     447  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
    448448  int N = tiffParams->stkInfo.metaData.N;
    449   uint32 tag = 0;
    450449
    451450  // -----------------------------
    452451  // read Strip Offsets
    453452  // -----------------------------
    454   tag = 273;
    455   if (isTagPresentInFirstIFD( &tiffParams->ifds, tag ) == TRUE) {
    456     uint32 *buf=NULL, size=0, type=0;
    457     readTiffTag ( tif, ifd, tag, size, type, (DIM_UCHAR **) &buf );
     453  uint16 tag = 273;
     454  if (ifd->tagPresent(tag)) {
     455    uint32 *buf=NULL; uint64 size=0; uint16 type=0;
     456    ifd->readTag(tag, size, type, (DIM_UCHAR **) &buf );
    458457    copy_clean_tag_buffer<uint32, D_TIFF_OFFS_TYPE>( &buf, size, &stkInfo->strip_offsets );
    459458
     
    464463  // -----------------------------
    465464  tag = 279;
    466   if (isTagPresentInFirstIFD( &tiffParams->ifds, tag ) == TRUE) {
    467     uint32 *buf=NULL, size=0, type=0;
    468     readTiffTag ( tif, ifd, tag, size, type, (DIM_UCHAR **) &buf );
     465  if (ifd->tagPresent(tag)) {
     466    uint32 *buf=NULL; uint64 size=0; uint16 type=0;
     467    ifd->readTag(tag, size, type, (DIM_UCHAR **) &buf );
    469468    copy_clean_tag_buffer<uint32, D_TIFF_BCNT_TYPE>( &buf, size, &stkInfo->strip_bytecounts );
    470469  } // strip_bytecounts
     
    490489
    491490  DStkInfo *stkInfo = &tiffParams->stkInfo;
    492   TDimTiffIFD *ifd = &tiffParams->ifds.ifds[0];
     491  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     492  if (!ifd) return;
    493493
    494494  // read UIC2
    495   int tag = 33629;
    496   if (isTagPresentInFirstIFD( &tiffParams->ifds, tag ) != TRUE) return;
    497   else
    498   {
    499     DIM_INT32 i;
    500     DIM_INT32 N = stkInfo->metaData.N;
    501     DIM_UINT32 *buf = NULL;
    502     readTiffCustomTag (tiffParams->dimTiff, ifd, tag, 6*N*sizeof(DIM_INT32), DIM_TAG_LONG, (DIM_UCHAR **) &buf);
    503     if (buf == NULL) return;
    504 
    505     for (i=0; i<N; i++) {
    506       stkInfo->metaData.zDistance[i].num    = buf[i*6+0];
    507       stkInfo->metaData.zDistance[i].den    = buf[i*6+1];
    508       stkInfo->metaData.creationDate[i]     = buf[i*6+2];
    509       stkInfo->metaData.creationTime[i]     = buf[i*6+3];
    510       stkInfo->metaData.modificationDate[i] = buf[i*6+4];
    511       stkInfo->metaData.modificationTime[i] = buf[i*6+5];
    512     }
    513 
    514     freeTiffTagBuf( (DIM_UCHAR **) &buf );
    515   }
     495  uint16 tag = 33629;
     496  if (!ifd->tagPresent(tag)) return;
     497
     498  DIM_INT32 i;
     499  DIM_INT32 N = stkInfo->metaData.N;
     500  DIM_UINT32 *buf = NULL;
     501  ifd->readTagCustom (tag, 6*N*sizeof(DIM_INT32), DIM_TAG_LONG, (DIM_UCHAR **) &buf);
     502  if (buf == NULL) return;
     503
     504  for (i=0; i<N; i++) {
     505    stkInfo->metaData.zDistance[i].num    = buf[i*6+0];
     506    stkInfo->metaData.zDistance[i].den    = buf[i*6+1];
     507    stkInfo->metaData.creationDate[i]     = buf[i*6+2];
     508    stkInfo->metaData.creationTime[i]     = buf[i*6+3];
     509    stkInfo->metaData.modificationDate[i] = buf[i*6+4];
     510    stkInfo->metaData.modificationTime[i] = buf[i*6+5];
     511  }
     512
     513  _TIFFfree(buf);
    516514}
    517515
     
    528526
    529527  DStkInfo *stkInfo = &tiffParams->stkInfo;
    530   TDimTiffIFD *ifd = &tiffParams->ifds.ifds[0];
     528  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     529  if (!ifd) return;
    531530
    532531  // read UIC3
    533   int tag = 33630;
    534   if (isTagPresentInFirstIFD( &tiffParams->ifds, tag ) != TRUE) return;
    535   else
     532  uint16 tag = 33630;
     533  if (!ifd->tagPresent(tag)) return;
     534  DIM_INT32 i=0;
     535  DIM_INT32 N = tiffParams->stkInfo.metaData.N;
     536  DIM_UINT32 *buf = NULL;
     537 
     538  ifd->readTagCustom (tag, 2*N*sizeof(DIM_UINT32), DIM_TAG_LONG, (DIM_UCHAR **) &buf);
     539  if (buf == NULL) return;
     540
     541  for (i=0; i<N; i++)
    536542  {
    537     DIM_INT32 i=0;
    538     DIM_INT32 N = tiffParams->stkInfo.metaData.N;
    539     DIM_UINT32 *buf = NULL;
    540  
    541     readTiffCustomTag (tiffParams->dimTiff, ifd, tag, 2*N*sizeof(DIM_UINT32), DIM_TAG_LONG, (DIM_UCHAR **) &buf);
    542     if (buf == NULL) return;
    543 
    544     for (i=0; i<N; i++)
    545     {
    546       stkInfo->metaData.wavelength[i].num = buf[i*2];
    547       stkInfo->metaData.wavelength[i].den = buf[i*2+1];
    548     }
    549 
    550     freeTiffTagBuf( (DIM_UCHAR **) &buf );
    551   }
     543    stkInfo->metaData.wavelength[i].num = buf[i*2];
     544    stkInfo->metaData.wavelength[i].den = buf[i*2+1];
     545  }
     546
     547  _TIFFfree(buf);
    552548}
    553549
     
    573569
    574570  DStkInfo *stkInfo = &tiffParams->stkInfo;
    575   TDimTiffIFD *ifd = &tiffParams->ifds.ifds[0];
     571  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     572  if (!ifd) return;
    576573
    577574  // read UIC4
    578   int tag = TIFFTAG_STK_UIC4;
    579   if (isTagPresentInFirstIFD( &tiffParams->ifds, tag ) == TRUE) {
    580 
    581     DIM_INT32 N = stkInfo->metaData.N;
    582     short tag_id;
    583     std::vector<unsigned char> buffer;
    584     int32 data_offset = getTiffTagOffset(tiffParams->dimTiff, ifd, tag);
    585 
    586     while (data_offset != 0) {
    587       // first read key for a tuple
    588       if ( readTiffBufNoAlloc (tiffParams->dimTiff, data_offset, 2, TIFF_SHORT, (uchar *) &tag_id) != 0) break;
    589       //safeguard, currently i only know of keys <= 45, stop otherwise
    590       if (tag_id == 0 || tag_id > DIM_STK_CameraChipOffset) break;
    591       data_offset += 2;
    592 
    593       if (tag_id == DIM_STK_StagePosition) {
    594         int buf_size = N*4*sizeof(uint32);
    595         if (buffer.size() < buf_size) buffer.resize(buf_size);
    596         DIM_INT32 *buf = (DIM_INT32 *) &buffer[0];
    597         if ( readTiffBufNoAlloc (tiffParams->dimTiff, data_offset, buf_size, TIFF_LONG, (unsigned char *) buf ) != 0) break;
    598         data_offset += buf_size;
    599         for (int i=0; i<N; ++i) {
    600           stkInfo->metaData.StagePositionX[i].num = buf[i*4+0];
    601           stkInfo->metaData.StagePositionX[i].den = buf[i*4+1];
    602           stkInfo->metaData.StagePositionY[i].num = buf[i*4+2];
    603           stkInfo->metaData.StagePositionY[i].den = buf[i*4+3];
    604         }
    605         continue;
    606       } // tag DIM_STK_StagePosition
    607 
    608       if (tag_id == DIM_STK_CameraChipOffset) {
    609         int buf_size = N*4*sizeof(uint32);
    610         if (buffer.size() < buf_size) buffer.resize(buf_size);
    611         DIM_INT32 *buf = (DIM_INT32 *) &buffer[0];
    612         if ( readTiffBufNoAlloc (tiffParams->dimTiff, data_offset, buf_size, TIFF_LONG, (unsigned char *) buf ) != 0) break;
    613         data_offset += buf_size;
    614         for (int i=0; i<N; ++i) {
    615           stkInfo->metaData.CameraChipOffsetX[i].num = buf[i*4+0];
    616           stkInfo->metaData.CameraChipOffsetX[i].den = buf[i*4+1];
    617           stkInfo->metaData.CameraChipOffsetY[i].num = buf[i*4+2];
    618           stkInfo->metaData.CameraChipOffsetY[i].den = buf[i*4+3];
    619         }
    620         continue;
    621       } // tag DIM_STK_CameraChipOffset
    622     } // while
    623   }
     575  uint16 tag = TIFFTAG_STK_UIC4;
     576  if (!ifd->tagPresent(tag)) return;
     577
     578  DIM_INT32 N = stkInfo->metaData.N;
     579  short tag_id;
     580  std::vector<unsigned char> buffer;
     581  int64 data_offset = ifd->tagOffset(tag);
     582
     583  while (data_offset != 0) {
     584    // first read key for a tuple
     585    if (ifd->readBufNoAlloc(data_offset, 2, TIFF_SHORT, (uint8 *) &tag_id) != 0) break;
     586    //safeguard, currently i only know of keys <= 45, stop otherwise
     587    if (tag_id == 0 || tag_id > DIM_STK_CameraChipOffset) break;
     588    data_offset += 2;
     589
     590    if (tag_id == DIM_STK_StagePosition) {
     591      int buf_size = N*4*sizeof(uint32);
     592      if (buffer.size() < buf_size) buffer.resize(buf_size);
     593      DIM_INT32 *buf = (DIM_INT32 *) &buffer[0];
     594      if (ifd->readBufNoAlloc(data_offset, buf_size, TIFF_LONG, (unsigned char *) buf) != 0) break;
     595      data_offset += buf_size;
     596      for (int i=0; i<N; ++i) {
     597        stkInfo->metaData.StagePositionX[i].num = buf[i*4+0];
     598        stkInfo->metaData.StagePositionX[i].den = buf[i*4+1];
     599        stkInfo->metaData.StagePositionY[i].num = buf[i*4+2];
     600        stkInfo->metaData.StagePositionY[i].den = buf[i*4+3];
     601      }
     602      continue;
     603    } // tag DIM_STK_StagePosition
     604
     605    if (tag_id == DIM_STK_CameraChipOffset) {
     606      int buf_size = N*4*sizeof(uint32);
     607      if (buffer.size() < buf_size) buffer.resize(buf_size);
     608      DIM_INT32 *buf = (DIM_INT32 *) &buffer[0];
     609      if (ifd->readBufNoAlloc(data_offset, buf_size, TIFF_LONG, (unsigned char *) buf ) != 0) break;
     610      data_offset += buf_size;
     611      for (int i=0; i<N; ++i) {
     612        stkInfo->metaData.CameraChipOffsetX[i].num = buf[i*4+0];
     613        stkInfo->metaData.CameraChipOffsetX[i].den = buf[i*4+1];
     614        stkInfo->metaData.CameraChipOffsetY[i].num = buf[i*4+2];
     615        stkInfo->metaData.CameraChipOffsetY[i].den = buf[i*4+3];
     616      }
     617      continue;
     618    } // tag DIM_STK_CameraChipOffset
     619  } // while
    624620}
    625621
     
    640636
    641637void stkReadStringEntry(DIM_INT32 offset, DTiffParams *tiffParams, char **string) {
    642   //DStkInfo *stkInfo = &tiffParams->stkInfo;
    643   TIFF *tif = tiffParams->dimTiff;
    644638  if (offset <= 0) return;
     639  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     640  if (!ifd) return;
    645641
    646642  DIM_INT32 size;
    647   readTiffBufNoAlloc (tif, offset, sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) &size);
     643  ifd->readBufNoAlloc( offset, sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) &size);
    648644  *string = (char *) _TIFFmalloc( size+1 );
    649645  (*string)[size] = '\0';
    650   readTiffBufNoAlloc (tif, offset+sizeof( DIM_INT32 ), size, DIM_TAG_LONG, (DIM_UCHAR *) (*string));
     646  ifd->readBufNoAlloc( offset+sizeof( DIM_INT32 ), size, DIM_TAG_LONG, (DIM_UCHAR *) (*string));
    651647}
    652648
    653649void stkParseIDEntry(DIM_INT32 *pair, DIM_INT32 offset, DTiffParams *tiffParams) {
    654650  DStkInfo *stkInfo = &tiffParams->stkInfo;
    655   //TDimTiffIFD *ifd = &tiffParams->ifds.ifds[0];
    656   TIFF *tif = tiffParams->dimTiff;
     651  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     652  if (!ifd) return;
    657653
    658654  if (pair[0] == DIM_STK_AutoScale)
     
    669665
    670666  if (pair[0] == DIM_STK_XCalibration)
    671     readTiffBufNoAlloc (tif, pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.XCalibration);
     667    ifd->readBufNoAlloc( pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.XCalibration);
    672668
    673669  if (pair[0] == DIM_STK_YCalibration)
    674     readTiffBufNoAlloc (tif, pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.YCalibration);
     670    ifd->readBufNoAlloc( pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.YCalibration);
    675671
    676672  if (pair[0] == DIM_STK_CalibrationUnits)
     
    702698
    703699  if (pair[0] == DIM_STK_CreateTime)
    704     readTiffBufNoAlloc (tif, pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.CreateTime);
     700    ifd->readBufNoAlloc( pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.CreateTime);
    705701
    706702  if (pair[0] == DIM_STK_LastSavedTime)
    707     readTiffBufNoAlloc (tif, pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.LastSavedTime);
     703    ifd->readBufNoAlloc( pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.LastSavedTime);
    708704
    709705  if (pair[0] == DIM_STK_currentBuffer)
     
    717713
    718714  if (pair[0] == DIM_STK_grayX)
    719     readTiffBufNoAlloc (tif, pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.grayX);
     715    ifd->readBufNoAlloc( pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.grayX);
    720716
    721717  if (pair[0] == DIM_STK_grayY)
    722     readTiffBufNoAlloc (tif, pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.grayY);
     718    ifd->readBufNoAlloc( pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.grayY);
    723719
    724720  if (pair[0] == DIM_STK_grayMin)
    725     readTiffBufNoAlloc (tif, pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.grayMin);
     721    ifd->readBufNoAlloc( pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.grayMin);
    726722
    727723  if (pair[0] == DIM_STK_grayMax)
    728     readTiffBufNoAlloc (tif, pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.grayMax);
     724    ifd->readBufNoAlloc( pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.grayMax);
    729725
    730726  if (pair[0] == DIM_STK_grayUnitName)
     
    735731
    736732  if (pair[0] == DIM_STK_AutoScaleLoInfo)
    737     readTiffBufNoAlloc (tif, pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.AutoScaleLoInfo);
     733    ifd->readBufNoAlloc( pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.AutoScaleLoInfo);
    738734
    739735  if (pair[0] == DIM_STK_AutoScaleHiInfo)
    740     readTiffBufNoAlloc (tif, pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.AutoScaleHiInfo);
     736    ifd->readBufNoAlloc( pair[1], 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.AutoScaleHiInfo);
    741737
    742738  if (pair[0] == DIM_STK_Gamma)
    743     readTiffBufNoAlloc (tif, pair[1], sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) &stkInfo->metaData.Gamma);
     739    ifd->readBufNoAlloc( pair[1], sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) &stkInfo->metaData.Gamma);
    744740
    745741  if (pair[0] == DIM_STK_GammaRed)
    746     readTiffBufNoAlloc (tif, pair[1], sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) &stkInfo->metaData.GammaRed);
     742    ifd->readBufNoAlloc( pair[1], sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) &stkInfo->metaData.GammaRed);
    747743
    748744  if (pair[0] == DIM_STK_GammaGreen)
    749     readTiffBufNoAlloc (tif, pair[1], sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) &stkInfo->metaData.GammaGreen);
     745    ifd->readBufNoAlloc( pair[1], sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) &stkInfo->metaData.GammaGreen);
    750746
    751747  if (pair[0] == DIM_STK_GammaBlue)
    752     readTiffBufNoAlloc (tif, pair[1], sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) &stkInfo->metaData.GammaBlue);
     748    ifd->readBufNoAlloc( pair[1], sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) &stkInfo->metaData.GammaBlue);
    753749
    754750  DIM_INT32 N = stkInfo->metaData.N;
     
    762758    if (buffer.size() < buf_size) buffer.resize(buf_size);
    763759    DIM_INT32 *buf = (DIM_INT32 *) &buffer[0];
    764     if ( readTiffBufNoAlloc (tif, pair[1], buf_size, DIM_TAG_LONG, (unsigned char *) buf ) == 0) {
     760    if ( ifd->readBufNoAlloc( pair[1], buf_size, DIM_TAG_LONG, (unsigned char *) buf ) == 0) {
    765761      for (int i=0; i<N; ++i) {
    766762        stkInfo->metaData.StagePositionX[i].num = buf[i*4+0];
     
    776772    if (buffer.size() < buf_size) buffer.resize(buf_size);
    777773    DIM_INT32 *buf = (DIM_INT32 *) &buffer[0];
    778     if ( readTiffBufNoAlloc (tif, pair[1], buf_size, DIM_TAG_LONG, (unsigned char *) buf ) == 0) {
     774    if ( ifd->readBufNoAlloc( pair[1], buf_size, DIM_TAG_LONG, (unsigned char *) buf ) == 0) {
    779775      for (int i=0; i<N; ++i) {
    780776        stkInfo->metaData.CameraChipOffsetX[i].num = buf[i*4+0];
     
    788784 
    789785  if (pair[0] == DIM_STK_AbsoluteZ)
    790     readTiffBufNoAlloc (tif, pair[1], N*2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.AbsoluteZ);
     786    ifd->readBufNoAlloc( pair[1], N*2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.AbsoluteZ);
    791787
    792788  if (pair[0] == DIM_STK_AbsoluteZValid)
    793     readTiffBufNoAlloc (tif, pair[1], N*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.AbsoluteZValid);
     789    ifd->readBufNoAlloc( pair[1], N*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) stkInfo->metaData.AbsoluteZValid);
    794790}
    795791
     
    800796  if (tiffParams->subType != tstStk) return;
    801797
    802   TDimTiffIFD *ifd = &tiffParams->ifds.ifds[0];
    803   TIFF *tif = tiffParams->dimTiff;
    804 
    805   int tag = 33628;
    806   if (isTagPresentInFirstIFD( &tiffParams->ifds, tag ) != TRUE) return;
     798  bim::TinyTiff::IFD *ifd = tiffParams->ifds.firstIfd();
     799  if (!ifd) return;
     800
     801  uint16 tag = 33628;
     802  if (!ifd->tagPresent(tag)) return;
    807803 
    808   DIM_INT32 i, offset;
    809804  DIM_INT32 N = tiffParams->stkInfo.metaData.N;
    810805  DIM_INT32 pair[2];
    811806
    812   offset = getTiffTagOffset(tif, ifd, tag);
     807  uint64 offset = ifd->tagOffset(tag);
    813808  if (offset == -1) return;
    814   DIM_INT32 num_ids = getTiffTagCount(tif, ifd, tag);
    815   DIM_INT32 id_offset = offset;
     809  uint64 num_ids = ifd->tagCount(tag);
     810  uint64 id_offset = offset;
    816811
    817812  // now read and parce ID table
    818   for (i=0; i<num_ids; i++) {
    819     readTiffBufNoAlloc( tif, id_offset, 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) pair);
     813  for (uint64 i=0; i<num_ids; i++) {
     814   ifd->readBufNoAlloc( id_offset, 2*sizeof( DIM_INT32 ), DIM_TAG_LONG, (DIM_UCHAR *) pair);
    820815    stkParseIDEntry(pair, id_offset, tiffParams);
    821816    id_offset += 2*sizeof( DIM_INT32 );
     
    844839  if (tiffParams == NULL) return 1;
    845840  if (tiffParams->dimTiff == NULL) return 1;
    846   if (tiffParams->ifds.count <= 0) return 1;
     841  if (!tiffParams->ifds.isValid()) return 1;
    847842
    848843  tiffParams->stkInfo.allocMetaInfo( stkGetNumPlanes( tiffParams->dimTiff ) );
     
    938933      dimProgress( fmtHndl, 0, 10, "Reading STK" );
    939934     
    940       int read_size = tif->tif_readproc((thandle_t) tif->tif_fd, img->bits[sample], size);
     935      D_TIFF_SIZE_TYPE read_size = tif->tif_readproc((thandle_t) tif->tif_fd, img->bits[sample], size);
    941936      if (read_size != size) return 1;
    942937      // now swap bytes if needed
     
    963958        if ( dimTestAbort( fmtHndl ) == 1) break; 
    964959
    965         int read_size = TIFFReadEncodedStrip (tif, strip_offset, (D_TIFF_DATA_TYPE) buf, (D_TIFF_SIZE_TYPE) strip_size);
     960        D_TIFF_SIZE_TYPE read_size = TIFFReadEncodedStrip (tif, strip_offset, (D_TIFF_DATA_TYPE) buf, (D_TIFF_SIZE_TYPE) strip_size);
    966961        if (read_size == -1) return 1;
    967962        _TIFFmemcpy( ((unsigned char *) img->bits[sample])+strip_offset*strip_size, buf, read_size );
     
    11131108  if (!hash) return 1;
    11141109  DTiffParams *tiffpar = (DTiffParams *) fmtHndl->internalParams;
    1115   TDimTiffIFD *ifd = &tiffpar->ifds.ifds[0];
    11161110  TDimStkMetaData *stk = &tiffpar->stkInfo.metaData;
    11171111  TDimImageInfo *info = &tiffpar->info;
     1112  bim::TinyTiff::IFD *ifd = tiffpar->ifds.firstIfd();
     1113  if (!ifd) return 1;
    11181114
    11191115  // is this STK?
    1120   if (!isTagPresentInIFD(ifd, 33628)) return 0;
     1116  if (!ifd->tagPresent(33628)) return 0;
    11211117
    11221118  xstring name, val;
     
    12551251  //------------------------------------------------------------
    12561252  TIFF *tif = tiffpar->dimTiff;
    1257   xstring tag_305 = read_tag_as_string(tif, ifd, TIFFTAG_SOFTWARE );
     1253  xstring tag_305 = ifd->readTagString(TIFFTAG_SOFTWARE);
     1254
    12581255  hash->append_tag( bim::CUSTOM_TAGS_PREFIX+"Software", tag_305 );
    12591256  //xstring tag_306 = read_tag_as_string(tif, ifd, TIFFTAG_DATETIME );
    12601257
    1261   xstring tag_270 = read_tag_as_string(tif, ifd, TIFFTAG_IMAGEDESCRIPTION );
     1258  xstring tag_270 = ifd->readTagString(TIFFTAG_IMAGEDESCRIPTION);
    12621259  hash->parse_ini( tag_270, ":", bim::CUSTOM_TAGS_PREFIX );
    12631260
  • libsrc/libbioimg/formats/tiff/dim_tiff_format.cpp

    r190 r221  
    3131void* DimFree(void *p);
    3232
    33 std::string read_tag_as_string(TIFF *tif, TDimTiffIFD *ifd, DIM_UINT tag);
    34 
    3533#include "dim_tiff_format_io.cpp"
    3634
     
    4341  this->dimTiff = NULL;
    4442  this->subType = tstGeneric;
    45   this->ifds = initTDimTiffIFDs();
    4643}
    4744
     
    428425
    429426  if (fmtHndl->io_mode != DIM_IO_WRITE) {
    430     if (tiffpar != NULL) clearTiffIFDs( &tiffpar->ifds );
    431427    clearMetaTags( &fmtHndl->metaData );
    432428  }
     
    486482
    487483    if (tiffpar->dimTiff != NULL) {
    488       tiffpar->ifds = readAllTiffIFDs( tiffpar->dimTiff );
     484      tiffpar->ifds.read(tiffpar->dimTiff);
    489485      getImageInfo(tiffpar);
    490486      fmtHndl->subFormat = tiffpar->subType;
  • libsrc/libbioimg/formats/tiff/dim_tiff_format.h

    r190 r221  
    6262#endif
    6363
    64 #include "dim_tiny_tiff.h"
     64#include "bim_tiny_tiff.h"
    6565#include "dim_psia_format.h"
    6666#include "dim_stk_format.h"
     
    9999  TIFF  *dimTiff;
    100100  DIM_TiffSubType subType;
    101   TDimTiffIFDs ifds;
     101  bim::TinyTiff::Tiff ifds;
    102102
    103103  DStkInfo stkInfo;
  • libsrc/libbioimg/formats/tiff/dim_tiff_format_io.cpp

    r190 r221  
    2323
    2424#include "memio.h"
    25 #include "dim_tiny_tiff.h"
     25#include "bim_tiny_tiff.h"
    2626#include "dim_tiff_format.h"
    2727
     
    4040
    4141// must include these guys here if not no access to internal TIFF structs
    42 #include "dim_tiny_tiff.cpp"
    4342#include "dim_stk_format_io.cpp"
    4443#include "dim_psia_format_io.cpp"
     
    187186//****************************************************************************
    188187
    189 DIM_UINT read_one_tag (TDimFormatHandle *fmtHndl, DTiffParams *tifParams, int tag)
     188DIM_UINT read_one_tag (TDimFormatHandle *fmtHndl, DTiffParams *tifParams, uint16 tag)
    190189{
    191190  if (!areValidParams(fmtHndl, tifParams)) return 1;
    192   if (tifParams->ifds.count == 0) return 1;
    193 
    194   if (fmtHndl->pageNumber >= tifParams->ifds.count) fmtHndl->pageNumber = 0;
     191  bim::TinyTiff::IFD *ifd = tifParams->ifds.getIfd(fmtHndl->pageNumber);
     192  if (!ifd) return 1;
     193
     194  if (fmtHndl->pageNumber >= tifParams->ifds.count()) fmtHndl->pageNumber = 0;
    195195  TIFF *tif = tifParams->dimTiff;
    196   TDimTiffIFDs *ifds = &tifParams->ifds;
    197 
    198   DIM_UCHAR *buf = NULL;
    199   uint32 buf_size;
    200   uint32 buf_type;
    201 
    202   if ( (tifParams->subType == tstStk) && (tag == 33629) ) // stk 33629 got custom size 6*N
    203   {
     196
     197  DIM_UCHAR *buf=NULL; uint16 buf_type; uint64 buf_size;
     198
     199  if ( (tifParams->subType == tstStk) && (tag == 33629) ) {// stk 33629 got custom size 6*N
    204200    buf_type = DIM_TAG_LONG;
    205     DIM_INT32 count = getTiffTagCount(tif, &ifds->ifds[fmtHndl->pageNumber], tag);
    206     buf_size = ( count * 6 ) * tag_size_bytes[buf_type];
    207     readTiffCustomTag (tif, &ifds->ifds[fmtHndl->pageNumber], tag, buf_size, buf_type, (DIM_UCHAR **) &buf);
    208   }
    209   else
    210     readTiffTag (tif, &ifds->ifds[fmtHndl->pageNumber], tag, buf_size, buf_type, (DIM_UCHAR **) &buf);
    211 
    212 
    213 
    214   if ( (buf_size == 0) || (buf == NULL) ) return 1;
     201    uint64 count = ifd->tagCount(tag);
     202    buf_size = ( count * 6 ) * bim::TinyTiff::tag_size_bytes[buf_type];
     203    ifd->readTagCustom(tag, buf_size, buf_type, (DIM_UCHAR **) &buf);
     204  }
     205  else
     206    ifd->readTag(tag, buf_size, buf_type, (DIM_UCHAR **) &buf);
     207
     208
     209
     210  if (buf_size==0 || !buf) return 1;
    215211  else
    216212  {
     
    221217    item.tagId     = tag;
    222218    item.tagType   = buf_type;
    223     item.tagLength = buf_size / tag_size_bytes[buf_type];
     219    item.tagLength = buf_size / bim::TinyTiff::tag_size_bytes[buf_type];
    224220    item.tagData   = buf;
    225221
     
    233229{
    234230  if (!areValidParams(fmtHndl, tifParams)) return 1;
    235 
    236231  if (group == DIM_META_BIORAD) return 1;
    237  
    238   if (fmtHndl->pageNumber >= tifParams->ifds.count) fmtHndl->pageNumber = 0;
     232  bim::TinyTiff::IFD *ifd = tifParams->ifds.getIfd(fmtHndl->pageNumber);
     233  if (!ifd) return 1;
    239234
    240235  // first read custom formatted tags
     
    245240    return read_one_tag ( fmtHndl, tifParams, tag );
    246241
    247   if ( (group == -1) || (group == DIM_META_TIFF_TAG) )
    248   {
    249     DIM_UINT i;
    250     TDimTiffIFD *ifd = &tifParams->ifds.ifds[fmtHndl->pageNumber];
    251 
    252     for (i=0; i<ifd->count; i++ )
    253     {
    254       if (type == -1)
    255       {
    256         if ( (ifd->entries[i].tag > 532) &&
    257              (ifd->entries[i].tag != 50434)
    258 
    259            ) read_one_tag ( fmtHndl, tifParams, ifd->entries[i].tag );   
     242  if ( (group == -1) || (group == DIM_META_TIFF_TAG) ) {
     243    for (uint64 i=0; i<ifd->size(); i++ ) {
     244      bim::TinyTiff::Entry *entry = ifd->getEntry(i);
     245      if (type == -1) {
     246        if (entry->tag>532 && entry->tag!=50434)
     247            read_one_tag ( fmtHndl, tifParams, entry->tag );   
    260248   
    261         switch( ifd->entries[i].tag )
     249        switch( entry->tag )
    262250        {
    263251          case 269: //DocumentName
     
    270258          case 315: //Artist
    271259          case 316: //HostComputer
    272             read_one_tag ( fmtHndl, tifParams, ifd->entries[i].tag );
     260            read_one_tag ( fmtHndl, tifParams, entry->tag );
    273261            break;
    274262        } // switch
     
    276264      else
    277265      {
    278         if (ifd->entries[i].type == type)
    279           read_one_tag ( fmtHndl, tifParams, ifd->entries[i].tag );
     266        if (entry->type == type)
     267          read_one_tag ( fmtHndl, tifParams, entry->tag );
    280268
    281269      } // type != -1
     
    303291}
    304292
    305 void read_text_tag(TIFF *tif, TDimTiffIFD *ifd, DIM_UINT tag, MemIOBuf *outIOBuf, const char *text)
    306 {
    307   uint32 buf_size;
    308   uint32 buf_type; 
    309   DIM_UCHAR *buf = NULL;
    310  
    311   if (isTagPresentInIFD ( ifd, tag ) == TRUE ) {
     293void read_text_tag(bim::TinyTiff::IFD *ifd, DIM_UINT tag, MemIOBuf *outIOBuf, const char *text) {
     294    if (!ifd->tagPresent(tag)) return;
     295
     296    uint64 buf_size;
     297    uint16 buf_type; 
     298    DIM_UCHAR *buf = NULL;
    312299    write_title_text(text, outIOBuf);
    313     readTiffTag (tif, ifd, tag, buf_size, buf_type, &buf);
     300    ifd->readTag (tag, buf_size, buf_type, &buf);
    314301    change_0_to_n ((char *) buf, buf_size);
    315302    MemIO_WriteProc( (thandle_t) outIOBuf, buf, buf_size );
    316     freeTiffTagBuf( (DIM_UCHAR **) &buf );
    317   }
    318 }
    319 
    320 void read_text_tag(TIFF *tif, TDimTiffIFD *ifd, DIM_UINT tag, MemIOBuf *outIOBuf)
    321 {
    322   uint32 buf_size;
    323   uint32 buf_type; 
    324   DIM_UCHAR *buf = NULL;
     303    _TIFFfree( buf );
     304}
     305
     306void read_text_tag(bim::TinyTiff::IFD *ifd, DIM_UINT tag, MemIOBuf *outIOBuf) {
     307    if (!ifd->tagPresent(tag)) return;
     308    uint64 buf_size;
     309    uint16 buf_type; 
     310    DIM_UCHAR *buf = NULL;
    325311 
    326   if (isTagPresentInIFD ( ifd, tag ) == TRUE ) {
    327     readTiffTag (tif, ifd, tag, buf_size, buf_type, &buf);
     312    ifd->readTag (tag, buf_size, buf_type, &buf);
    328313    change_0_to_n ((char *) buf, buf_size);
    329314    MemIO_WriteProc( (thandle_t) outIOBuf, buf, buf_size );
    330     freeTiffTagBuf( (DIM_UCHAR **) &buf );
    331   }
     315    _TIFFfree( buf );
    332316}
    333317
    334318char* read_text_tiff_metadata ( TDimFormatHandle *fmtHndl, DTiffParams *tifParams ) {
    335   return NULL;
     319    return NULL;
    336320}
    337321
     
    345329  if (!hash) return 1;
    346330
    347   DTiffParams *tifParams = (DTiffParams *) fmtHndl->internalParams;
    348   TIFF *tif = tifParams->dimTiff;
    349   TDimTiffIFD *ifd = &tifParams->ifds.ifds[0];
     331  DTiffParams *par = (DTiffParams *) fmtHndl->internalParams;
     332  bim::TinyTiff::IFD *ifd = par->ifds.firstIfd();
     333  if (!ifd) return 1;
    350334
    351335  std::map< int, std::string > hash_tiff_tags;
     
    362346  std::map< int, std::string >::const_iterator it = hash_tiff_tags.begin();
    363347  while (it != hash_tiff_tags.end()) {
    364     xstring tag_str = read_tag_as_string(tif, ifd, it->first);
     348    xstring tag_str = ifd->readTagString(it->first);
    365349    if (tag_str.size()>0) hash->append_tag( xstring("custom/") + it->second, tag_str );
    366350    it++;
    367351  }
    368352  return 0;
    369 }
    370 
    371 std::string read_tag_as_string(TIFF *tif, TDimTiffIFD *ifd, DIM_UINT tag) {
    372   uint32 buf_size, buf_type; 
    373   DIM_UCHAR *buf = NULL;
    374   std::string s;
    375 
    376   if (isTagPresentInIFD ( ifd, tag ) == TRUE ) {
    377     readTiffTag (tif, ifd, tag, buf_size, buf_type, &buf);
    378     s.resize( buf_size );
    379     memcpy( &s[0], buf, buf_size );
    380     freeTiffTagBuf( (DIM_UCHAR **) &buf );
    381   }
    382   return s;
    383353}
    384354
     
    388358  if (!hash) return 1;
    389359
    390   DTiffParams *tifParams = (DTiffParams *) fmtHndl->internalParams;
    391   TIFF *tif = tifParams->dimTiff;
    392   TDimTiffIFD *ifd = &tifParams->ifds.ifds[0];
     360  DTiffParams *par = (DTiffParams *) fmtHndl->internalParams;
     361  bim::TinyTiff::IFD *ifd = par->ifds.firstIfd();
     362  if (!ifd) return 1;
    393363
    394364  /*
     
    408378  // check if it's QImage tiff file
    409379  // should exist private tags 50288 and 50296
    410   if (!isTagPresentInIFD(ifd, 50288)) return 0;
    411   if (!isTagPresentInIFD(ifd, 50296)) return 0;
     380  if (!ifd->tagPresent(50288)) return 0;
     381  if (!ifd->tagPresent(50296)) return 0;
    412382
    413383  // tag 305 should be "QCapture Pro"
    414   xstring tag_software = read_tag_as_string(tif, ifd, 305);
     384  xstring tag_software = ifd->readTagString(305);
    415385  if ( tag_software != "QCapture Pro" ) return 0;
    416386 
     
    419389
    420390
    421   xstring tag_description = read_tag_as_string(tif, ifd, TIFFTAG_IMAGEDESCRIPTION);
     391  xstring tag_description = ifd->readTagString(TIFFTAG_IMAGEDESCRIPTION);
    422392  if (tag_description.size()>0)
    423393    hash->parse_ini( tag_description, ":", bim::CUSTOM_TAGS_PREFIX );
    424394
    425395  // read tag 306 - Date/Time
    426   xstring tag_datetime = read_tag_as_string(tif, ifd, 306);
     396  xstring tag_datetime = ifd->readTagString(306);
    427397  if (tag_datetime.size()>0) {
    428398    int y=0, m=0, d=0, h=0, mi=0, s=0, ms=0;
  • src/imgcnv.pro

    r216 r221  
    277277SOURCES += $$DN_FMTS/dim_format_manager.cpp \
    278278           $$DN_FMTS/meta_format_manager.cpp\
     279           $$DN_FMTS/tiff/bim_tiny_tiff.cpp \           
    279280           $$DN_FMTS/tiff/dim_tiff_format.cpp \
    280281           $$DN_FMTS/tiff/dim_xtiff.c \
Note: See TracChangeset for help on using the changeset viewer.