39 cout <<
"DEBUG readTIFF: Reading image: " << p->
file_name() << endl;
42 if ( ( fimg = TIFFOpen(p->
file_name().c_str(),
"r") ) == NULL )
return -1;
47 flags = TIFFPRINT_STRIPS & TIFFPRINT_CURVES & TIFFPRINT_COLORMAP;
48 TIFFPrintDirectory(fimg, stdout, flags);
52 long i, n, iz, iy, j, jz, jy, k;
53 uint32 x(1), y(1), z(1), px, py, pz;
54 uint16 compression(COMPRESSION_NONE);
55 uint16 samplesperpixel(1), bitspersample(0), lutsize(0);
56 uint16 sampleformat(SAMPLEFORMAT_UINT), photometric(0);
57 uint16 planarconfig(PLANARCONFIG_CONTIG);
58 uint16 orientation(ORIENTATION_BOTLEFT);
59 float xresolution(1), yresolution(1);
65 int notdone(1), nimg(0);
67 if ( img_select > -1 ) {
68 while ( TIFFReadDirectory(fimg) ) nimg++;
69 if ( img_select > nimg ) img_select = nimg;
71 TIFFSetDirectory(fimg, img_select);
72 TIFFGetField(fimg, TIFFTAG_IMAGEWIDTH, &x);
73 TIFFGetField(fimg, TIFFTAG_IMAGELENGTH, &y);
74 TIFFGetField(fimg, TIFFTAG_IMAGEDEPTH, &z);
78 TIFFGetField(fimg, TIFFTAG_IMAGEWIDTH, &x);
79 TIFFGetField(fimg, TIFFTAG_IMAGELENGTH, &y);
80 TIFFGetField(fimg, TIFFTAG_IMAGEDEPTH, &z);
86 cerr <<
"Warning: TIFF image " << nimg <<
" is of a different size: " << x <<
"," << y <<
"," << z << endl;
87 cerr <<
" ---> Only " << nimg <<
" images read!" << endl;
91 }
while ( notdone && TIFFReadDirectory(fimg) );
93 TIFFSetDirectory(fimg, 0);
97 char* timestring = NULL, mon[4];
98 TIFFGetField(fimg, TIFFTAG_DATETIME, ×tring);
101 cout <<
"TIFF timestring: " << timestring << endl;
102 if ( isdigit(timestring[0]) ) {
103 sscanf(timestring,
"%4d:%2d:%2d %2d:%2d:%2d",
104 &t->tm_year, &t->tm_mon, &t->tm_mday,
105 &t->tm_hour, &t->tm_min, &t->tm_sec);
108 sscanf(timestring,
"%3s %3s %2d %2d:%2d:%2d %4d",
109 mon, mon, &t->tm_mday, &t->tm_hour, &t->tm_min, &t->tm_sec, &t->tm_year);
110 for ( i=0; strncmp(mon,
month[i], 3) != 0; i++ ) ;
111 if ( i < 12 ) t->tm_mon = i;
117 TIFFGetField(fimg, TIFFTAG_COMPRESSION, &compression);
119 TIFFGetField(fimg, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
130 if ( TIFFIsTiled(fimg) ) {
131 TIFFGetField(fimg, TIFFTAG_TILEWIDTH, &x);
132 TIFFGetField(fimg, TIFFTAG_TILELENGTH, &y);
133 TIFFGetField(fimg, TIFFTAG_TILEDEPTH, &z);
137 TIFFGetField(fimg, TIFFTAG_XRESOLUTION, &xresolution);
138 TIFFGetField(fimg, TIFFTAG_YRESOLUTION, &yresolution);
140 cout <<
"DEBUG readTIFF: xresolution=" << xresolution <<
" yresolution=" << yresolution << endl;
141 p->
sampling(xresolution, yresolution, 1);
143 p->
sampling(xresolution, yresolution, xresolution);
145 TIFFGetField(fimg, TIFFTAG_BITSPERSAMPLE, &bitspersample);
146 TIFFGetField(fimg, TIFFTAG_PHOTOMETRIC, &photometric);
147 TIFFGetField(fimg, TIFFTAG_SAMPLEFORMAT, &sampleformat);
148 TIFFGetField(fimg, TIFFTAG_ORIENTATION, &orientation);
151 cout <<
"DEBUG readTIFF: bitspersample=" << bitspersample <<
" tiff datatype=" << sampleformat << endl;
153 switch ( photometric ) {
154 case PHOTOMETRIC_MINISWHITE:
155 case PHOTOMETRIC_MINISBLACK:
157 case PHOTOMETRIC_RGB:
158 case PHOTOMETRIC_YCBCR:
162 case PHOTOMETRIC_SEPARATED:
165 case PHOTOMETRIC_PALETTE:
168 for ( i=0; i<bitspersample; i++ ) lutsize *= 2;
170 cout <<
"DEBUG readTIFF: lutsize=" << lutsize << endl;
176 switch ( bitspersample ) {
182 if ( sampleformat == SAMPLEFORMAT_INT )
187 if ( sampleformat == SAMPLEFORMAT_INT )
192 if ( sampleformat == SAMPLEFORMAT_INT )
194 else if ( sampleformat == SAMPLEFORMAT_IEEEFP )
199 if ( sampleformat == SAMPLEFORMAT_INT )
201 else if ( sampleformat == SAMPLEFORMAT_IEEEFP )
211 uint16 min(0), max(0);
212 double dmin(0), dmax(0);
214 TIFFGetField(fimg, TIFFTAG_MINSAMPLEVALUE, &min);
215 TIFFGetField(fimg, TIFFTAG_MAXSAMPLEVALUE, &max);
219 TIFFGetField(fimg, TIFFTAG_SMINSAMPLEVALUE, &dmin);
220 TIFFGetField(fimg, TIFFTAG_SMAXSAMPLEVALUE, &dmax);
226 TIFFGetField(fimg, TIFFTAG_IMAGEDESCRIPTION, &label);
227 if ( label ) p->
label(label);
230 cout <<
"DEBUG readTIFF: Minimum and maximum: " << p->
minimum() <<
" " << p->
maximum() << endl;
234 long elementsize = p->
channels()*datatypesize;
235 long linesize = p->
sizeX()*elementsize;
236 unsigned char* data = NULL;
241 TIFFGetField(fimg, TIFFTAG_PLANARCONFIG, &planarconfig);
242 if ( samplesperpixel > 1 && planarconfig == PLANARCONFIG_SEPARATE ) {
243 cerr <<
"Error: Separate planes not supported!" << endl;
248 cout <<
"DEBUG readTIFF: readdata=" << readdata <<
" datasize=" <<
249 p->
data_size() <<
" linesize=" << linesize << endl;
262 unsigned short* rgbdata = NULL;
267 cout <<
"DEBUG readTIFF: lutsize=" << lutsize << endl;
268 rgbdata =
new unsigned short[3*datasize];
274 TIFFGetField(fimg, TIFFTAG_COLORMAP, &red, &green, &blue);
276 cout <<
"DEBUG readTIFF: lut:" << endl;
277 for ( i=0; i<lutsize; i++ )
278 cout << i <<
tab << red[i] <<
tab << green[i] <<
tab << blue[i] << endl;
281 TIFFGetField(fimg, TIFFTAG_PLANARCONFIG, &planarconfig);
282 if ( samplesperpixel > 1 && planarconfig == PLANARCONFIG_SEPARATE ) {
283 cerr <<
"Error: Separate planes not supported!" << endl;
286 if ( TIFFIsTiled(fimg) ) {
287 TIFFGetField(fimg, TIFFTAG_TILEWIDTH, &x);
288 TIFFGetField(fimg, TIFFTAG_TILELENGTH, &y);
289 TIFFGetField(fimg, TIFFTAG_TILEDEPTH, &z);
291 tilesize = TIFFTileSize(fimg);
294 cout <<
"DEBUG readTIFF: tile size: " << x <<
" x " << y
295 <<
" x " << z <<
" = " << tilesize << endl;
297 page =
new char[tilesize];
301 TIFFReadTile(fimg, (
char *) page, px, py, pz, 0);
302 for ( z=0; z<p->
page_size()[2] && z<p->sizeZ()-pz; z++ ) {
306 iy = (iz + y + py)*p->
sizeX();
311 memcpy(data+iy, page+jy, linesize);
313 for ( x=0; x<p->
page_size()[0] && x<p->sizeX()-px; x++ ) {
314 i = (iy + x + px)*elementsize;
315 j = (jy + x)*elementsize;
316 memcpy(data+i, page+j, elementsize);
327 linesize = TIFFScanlineSize(fimg);
329 cout <<
"DEBUG readTIFF: scanline size=" << linesize << endl;
332 for ( z=0; z<p->
sizeZ(); z++ ) {
333 for ( y=0; y<p->
sizeY(); y++, i+=linesize ) {
335 TIFFReadScanline(fimg, data+i, y, 0);
337 if ( bitspersample == 8 ) {
338 for ( x=0, j=i, k=3*j; x<p->
sizeX(); x++, j++ ) {
340 rgbdata[k++] = red[idx];
341 rgbdata[k++] = green[idx];
342 rgbdata[k++] = blue[idx];
345 unsigned short* usdata = (
unsigned short *) data;
346 for ( x=0, j=i/2, k=3*j; x<p->
sizeX(); x++, j++ ) {
348 rgbdata[k++] = red[idx];
349 rgbdata[k++] = green[idx];
350 rgbdata[k++] = blue[idx];
360 cout <<
"DEBUG readTIFF: image " << n <<
" read" << endl;
363 }
while ( n < p->images() && TIFFReadDirectory(fimg) );
368 cout <<
"DEBUG readTIFF: images read: " << p->
images() << endl << endl;
382 cout <<
"DEBUG readTIFF: Tiff photometric order: " << photometric << endl;
383 if ( photometric == PHOTOMETRIC_MINISWHITE ) p->
invert();
384 switch ( orientation ) {
385 case ORIENTATION_TOPLEFT: order =
"x-yz";
break;
386 case ORIENTATION_TOPRIGHT: order =
"-x-yz";
break;
387 case ORIENTATION_BOTRIGHT: order =
"-xyz";
break;
388 case ORIENTATION_LEFTTOP: order =
"-yxz";
break;
389 case ORIENTATION_RIGHTTOP: order =
"-y-xz";
break;
390 case ORIENTATION_RIGHTBOT: order =
"y-xz";
break;
391 case ORIENTATION_LEFTBOT: order =
"yxz";
break;
392 default: order =
"xyz";
395 cout <<
"DEBUG readTIFF: Tiff image order: " << order << endl;
396 if ( orientation != ORIENTATION_BOTLEFT ) p->
reslice(order);
416 if ( ( fimg = TIFFOpen(p->
file_name().c_str(),
"w") ) == NULL )
return -1;
419 int istiled = flags & 1;
422 uint16 compression(flags & COMPRESSION_LZW);
424 uint16 min = (uint16) p->
minimum();
425 uint16 max = (uint16) p->
maximum();
426 uint16 resolution_unit(1);
427 float xresolution = p->
sampling(0)[0];
428 float yresolution = p->
sampling(0)[1];
429 uint32 rowsperstrip = (uint32) -1;
435 long pagesize, tilerowsize(0);
444 p->
page_size((
int) (unitsize*floor(p->
sizeX()*1.0/unitsize + 0.99999)),
445 (
int) (unitsize*floor(p->
sizeY()*1.0/unitsize + 0.99999)), 1);
447 long tilex = (int) (unitsize*floor(p->
sizeX()*1.0/unitsize + 0.99999));
448 long tiley = (int) (unitsize*floor(p->
sizeY()*1.0/unitsize + 0.99999));
453 snprintf(timestring, 20,
"%4d:%02d:%02d %02d:%02d:%02d",
454 t->tm_year+1900, t->tm_mon+1, t->tm_mday,
455 t->tm_hour, t->tm_min, t->tm_sec);
457 for ( n=0; n<p->
images(); n++ ) {
458 TIFFSetDirectory(fimg, n);
459 TIFFSetField(fimg, TIFFTAG_IMAGEWIDTH, p->
sizeX());
460 TIFFSetField(fimg, TIFFTAG_IMAGELENGTH, p->
sizeY());
461 TIFFSetField(fimg, TIFFTAG_IMAGEDEPTH, p->
sizeZ());
462 TIFFSetField(fimg, TIFFTAG_SAMPLESPERPIXEL, p->
channels());
464 TIFFSetField(fimg, TIFFTAG_TILEWIDTH, tilex);
465 TIFFSetField(fimg, TIFFTAG_TILELENGTH, tiley);
466 TIFFSetField(fimg, TIFFTAG_TILEDEPTH, tilez);
468 TIFFSetField(fimg, TIFFTAG_ROWSPERSTRIP,
469 TIFFDefaultStripSize(fimg, rowsperstrip));
471 TIFFSetField(fimg, TIFFTAG_COMPRESSION, compression);
474 cout <<
"DEBUG writeTIFF: Image " << n <<
": size " << p->
sizeX() <<
" "
476 cout <<
"DEBUG writeTIFF: page dimensions: " <<
480 TIFFSetField(fimg, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
481 TIFFSetField(fimg, TIFFTAG_XRESOLUTION, xresolution);
482 TIFFSetField(fimg, TIFFTAG_YRESOLUTION, yresolution);
484 TIFFSetField(fimg, TIFFTAG_IMAGEDESCRIPTION, p->
label().c_str());
485 TIFFSetField(fimg, TIFFTAG_DATETIME, timestring);
488 TIFFSetField(fimg, TIFFTAG_MINSAMPLEVALUE, min);
489 TIFFSetField(fimg, TIFFTAG_MAXSAMPLEVALUE, max);
491 TIFFSetField(fimg, TIFFTAG_SMINSAMPLEVALUE, p->
minimum());
492 TIFFSetField(fimg, TIFFTAG_SMAXSAMPLEVALUE, p->
maximum());
496 cout <<
"DEBUG writeTIFF: min & max: " << min <<
" " << max << endl;
500 TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
501 TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 1);
504 TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
505 TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 8);
508 TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
509 TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 8);
512 TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
513 TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 16);
516 TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
517 TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 16);
520 TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
521 TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 32);
524 TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
525 TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 32);
528 TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
529 TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 64);
532 TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
533 TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 64);
536 TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
537 TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 32);
540 TIFFSetField(fimg, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
541 TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 64);
544 TIFFSetField(fimg, TIFFTAG_BITSPERSAMPLE, 8);
547 int extraTypes[1] = {EXTRASAMPLE_ASSOCALPHA};
550 TIFFSetField(fimg, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
554 TIFFSetField(fimg, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
557 TIFFSetField(fimg, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
558 TIFFSetField(fimg, TIFFTAG_EXTRASAMPLES, 1, extraTypes);
561 TIFFSetField(fimg, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
562 TIFFSetField(fimg, TIFFTAG_INKSET, INKSET_CMYK);
565 TIFFSetField(fimg, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
568 TIFFSetField(fimg, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
569 TIFFSetField(fimg, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
572 cout <<
"DEBUG writeTIFF: configuration set to planar" << endl;
573 cout <<
"DEBUG writeTIFF: Typesize=" << datatypesize <<
" Datasize=" << datasize << endl;
575 cout <<
"DEBUG writeTIFF: Tilesize=" << TIFFTileSize(fimg) << endl;
577 cout <<
"DEBUG writeTIFF: Scanlinesize=" << TIFFScanlineSize(fimg) << endl;
578 cout <<
"DEBUG writeTIFF: Pages: ";
582 pagesize = TIFFTileSize(fimg);
583 page =
new char[pagesize];
584 tilerowsize = TIFFTileRowSize(fimg);
585 for ( z=0; z<p->
sizeZ(); z++ ) {
586 memset(page, 0, pagesize);
587 for ( y=0; y<p->
sizeY(); y++ ) {
589 j = ((n*p->
sizeZ() + z)*p->
sizeY() + y)*linesize;
593 TIFFWriteTile(fimg, page, 0, 0, z, 0);
598 cout <<
"DEBUG writeTIFF: linesize=" << linesize << endl;
599 for ( z=0; z<p->
sizeZ(); z++ ) {
600 for ( y=0; y<p->
sizeY(); y++ ) {
601 j = ((n*p->
sizeZ() + z)*p->
sizeY() + y)*linesize;
610 TIFFWriteDirectory(fimg);
614 cout <<
"DEBUG writeTIFF: data written" << endl;
619 cout <<
"DEBUG writeTIFF: Memory freed" << endl;
628 cerr <<
"Error: TIFF files are not supported!" << endl;
635 cerr <<
"Error: TIFF files are not supported!" << endl;