std::auto_ptr<image_list> STKFormat::read_multi(byte_source* src, ImageFactory* factory) { shift_source moved(src); stk_extend ext; tiff_warn_error twe; tif_holder t = read_client(&moved); std::auto_ptr<image_list> images(new image_list); const uint32 h = tiff_get<uint32>(t, TIFFTAG_IMAGELENGTH); const uint32 w = tiff_get<uint32>(t, TIFFTAG_IMAGEWIDTH); const uint16 nr_samples = tiff_get<uint16>(t, TIFFTAG_SAMPLESPERPIXEL, 1); const uint16 bits_per_sample = tiff_get<uint16>(t, TIFFTAG_BITSPERSAMPLE, 8); const int depth = nr_samples > 1 ? nr_samples : -1; const int strip_size = TIFFStripSize(t.tif); const int n_strips = TIFFNumberOfStrips(t.tif); int32_t n_planes; void* data; TIFFGetField(t.tif, UIC3Tag, &n_planes, &data); int raw_strip_size = 0; for (int st = 0; st != n_strips; ++st) { raw_strip_size += TIFFRawStripSize(t.tif, st); } for (int z = 0; z < n_planes; ++z) { // Monkey patch strip offsets. This is very hacky, but it seems to work! moved.shift(z * raw_strip_size); std::auto_ptr<Image> output(factory->create(bits_per_sample, h, w, depth)); uint8_t* start = output->rowp_as<uint8_t>(0); for (int st = 0; st != n_strips; ++st) { const int offset = TIFFReadEncodedStrip(t.tif, st, start, strip_size); if (offset == -1) { throw CannotReadError("imread.imread._tiff.stk: Error reading strip"); } start += offset; } images->push_back(output); } return images; }
/* obtain some values from the tiff and check if they are what was expected */ int tifgettags(Mytiff * mytiff){ int row,cval; int32_t ww=0,hh=0,bb=0; cval=TIFFGetField(mytiff->tiffp,TIFFTAG_IMAGEWIDTH,&ww); if(cval != 1 ) { fprintf(stderr,"ERROR: %s: error reading width\n",__func__); return(103); } cval=TIFFGetField(mytiff->tiffp,TIFFTAG_ROWSPERSTRIP,&hh); //printf("rowsperstrip: %i\n",hh); mytiff->stripnum=TIFFNumberOfStrips(mytiff->tiffp); mytiff->stripsize=TIFFRawStripSize(mytiff->tiffp,0); cval=TIFFGetField(mytiff->tiffp,TIFFTAG_STRIPOFFSETS,&(mytiff->offsets)); if(cval != 1 ) { fprintf(stderr,"ERROR: %s: error reading offsets\n",__func__); return(103); } cval=TIFFGetField(mytiff->tiffp,TIFFTAG_STRIPBYTECOUNTS,&(mytiff->bytecounts)); if(cval != 1 ) { fprintf(stderr,"ERROR: %s: error reading bytecounts\n",__func__); return(103); } cval=TIFFGetField(mytiff->tiffp,TIFFTAG_IMAGELENGTH,&hh); if(cval != 1 ) { fprintf(stderr,"ERROR: %s: error reading length\n",__func__); return(103); } cval=TIFFGetField(mytiff->tiffp,TIFFTAG_BITSPERSAMPLE,&bb); if(cval != 1 ) { fprintf(stderr,"ERROR: %s: error reading bitspersample\n",__func__); return(103); } /* check validity of this tiff for the job */ /* height and width should always match */ /* unless zero then it's unknown */ /* and the new value used */ if (mytiff->wd != 0) { if(ww != mytiff->wd){ fprintf(stderr,"ERROR: data mismatch in %s!\n found ww=%i hh=%i bb=%i \n",__func__,ww,hh,bb); fprintf(stderr,"expected wd=%i ht=%i bits=%i \n",mytiff->wd,mytiff->ht,mytiff->bits); return(102); } } if (mytiff->ht != 0) { if(hh != mytiff->ht){ fprintf(stderr,"ERROR: data mismatch in %s!\n found ww=%i hh=%i bb=%i \n",__func__,ww,hh,bb); fprintf(stderr,"expected wd=%i ht=%i bits=%i \n",mytiff->wd,mytiff->ht,mytiff->bits); return(102); } } if (mytiff->bits != 0) { if(bb != mytiff->bits){ fprintf(stderr,"ERROR: data mismatch in %s!\n found ww=%i hh=%i bb=%i \n",__func__,ww,hh,bb); fprintf(stderr,"expected wd=%i ht=%i bits=%i \n",mytiff->wd,mytiff->ht,mytiff->bits); return(102); } } mytiff->ht = hh; mytiff->wd = ww; mytiff->bits=(bb); mytiff->bytes=(bb/8); mytiff->ndata=mytiff->wd * mytiff->ht; mytiff->nbytes = mytiff->ndata * mytiff->bytes; /* obtain the raw file io descriptor number */ mytiff->tiffno=TIFFFileno(mytiff->tiffp); return(0); }