18 #pragma warning( disable : 4611 )
24 #include <itksys/SystemTools.hxx>
43 int Open(
const char *filename );
74 if ( stat(filename, &fs) )
79 this->
m_Image = TIFFOpen(filename,
"r");
136 if ( !TIFFGetField(this->
m_Image, TIFFTAG_IMAGEWIDTH, &this->
m_Width) ||
155 if ( !TIFFGetField(this->
m_Image,
161 char** description =
new char*[255];
162 if (TIFFGetField(this->
m_Image,TIFFTAG_IMAGEDESCRIPTION,description))
165 std::string desc = description[0];
166 int pos = desc.find(
"images=");
167 int pos2 = desc.find(
"\n");
168 if( (pos != -1) && (pos2 != -1))
184 itkGenericExceptionMacro(
185 <<
"Cannot read tile width and tile length from file" );
201 int32 subfiletype = 6;
202 if(TIFFGetField(this->
m_Image, TIFFTAG_SUBFILETYPE, &subfiletype))
209 TIFFReadDirectory(this->
m_Image);
213 TIFFSetDirectory(this->
m_Image,0);
216 TIFFGetFieldDefaulted(this->
m_Image, TIFFTAG_ORIENTATION,
218 TIFFGetFieldDefaulted(this->
m_Image, TIFFTAG_SAMPLESPERPIXEL,
221 TIFFGetFieldDefaulted(this->
m_Image, TIFFTAG_BITSPERSAMPLE,
268 std::string filename = file;
271 itkDebugMacro(<<
"No filename specified.");
276 TIFFErrorHandler save = TIFFSetErrorHandler(0);
280 TIFFSetErrorHandler(save);
284 TIFFSetErrorHandler(save);
292 unsigned int height )
297 tdata_t buf = _TIFFmalloc(isize);
303 unsigned char* image;
306 for ( row = 0; row < (int)height; row ++ )
310 itkExceptionMacro( <<
"Problem reading the row: " << row );
316 image =
reinterpret_cast<unsigned char*
>(out) + row * width * inc;
320 image =
reinterpret_cast<unsigned char*
>(out) + width * inc * (height - (row + 1));
323 for (cc = 0; cc < isize;
327 static_cast<unsigned char *>(buf) +
338 for (s = 0; s < nsamples; s++)
340 for ( row = 0; row < (int)height; row ++ )
344 itkExceptionMacro( <<
"Problem reading the row: " << row );
352 image =
reinterpret_cast<unsigned char*
>(out) + row * width * inc;
356 image =
reinterpret_cast<unsigned char*
>(out) + width * inc * (height - (row + 1));
361 for (cc = 0; cc < isize;
364 (*image) = *(
static_cast<unsigned char *
>(buf) + cc);
375 unsigned short* image;
378 for ( row = 0; row < (int)height; row ++ )
382 itkExceptionMacro( <<
"Problem reading the row: " << row );
388 image =
reinterpret_cast<unsigned short*
>(out) + row * width * inc;
392 image =
reinterpret_cast<unsigned short*
>(out) + width * inc * (height - (row + 1));
395 for (cc = 0; cc < isize;
399 static_cast<unsigned short *>(buf) +
409 for (s = 0; s < nsamples; s++)
411 for ( row = 0; row < (int)height; row ++ )
415 itkExceptionMacro( <<
"Problem reading the row: " << row );
421 image =
reinterpret_cast<unsigned short*
>(out) + row * width * inc;
425 image =
reinterpret_cast<unsigned short*
>(out) + width * inc * (height - (row + 1));
429 for (cc = 0; cc < isize;
432 (*image) = *(
static_cast<unsigned short *
>(buf) + cc);
446 unsigned int height )
451 tdata_t buf = _TIFFmalloc(isize);
455 itkExceptionMacro( <<
"This reader can only do PLANARCONFIG_CONTIG" );
478 unsigned char* image;
481 for ( row = 0; row < (int)height; row ++ )
485 itkExceptionMacro( <<
"Problem reading the row: " << row );
491 image =
reinterpret_cast<unsigned char*
>(out) + row * width * inc;
495 image =
reinterpret_cast<unsigned char*
>(out) + width * inc * (height - (row + 1));
498 for (cc = 0; cc < isize;
502 static_cast<unsigned char *>(buf) +
513 for (s = 0; s < nsamples; s++)
515 for ( row = 0; row < (int)height; row ++ )
519 itkExceptionMacro( <<
"Problem reading the row: " << row );
526 image =
reinterpret_cast<unsigned char*
>(out) + row * width * inc;
530 image =
reinterpret_cast<unsigned char*
>(out) + width * inc * (height - (row + 1));
533 for (cc = 0; cc < isize;
537 static_cast<unsigned char *>(buf) +
550 for ( row = 0; row < (int)height; row ++ )
554 itkExceptionMacro( <<
"Problem reading the row: " << row );
560 image =
reinterpret_cast<char*
>(out) + row * width * inc;
564 image =
reinterpret_cast<char*
>(out) + width * inc * (height - (row + 1));
567 for (cc = 0; cc < isize;
571 static_cast<char *>(buf) +
582 for (s = 0; s < nsamples; s++)
584 for ( row = 0; row < (int)height; row ++ )
588 itkExceptionMacro( <<
"Problem reading the row: " << row );
595 image =
reinterpret_cast<char*
>(out) + row * width * inc;
599 image =
reinterpret_cast<char*
>(out) + width * inc * (height - (row + 1));
602 for (cc = 0; cc < isize;
606 static_cast<char *>(buf) +
617 unsigned short* image;
620 for ( row = 0; row < (int)height; row ++ )
624 itkExceptionMacro( <<
"Problem reading the row: " << row );
630 image =
reinterpret_cast<unsigned short*
>(out) + row * width * inc;
634 image =
reinterpret_cast<unsigned short*
>(out) + width * inc * (height - (row + 1));
637 for (cc = 0; cc < isize;
641 static_cast<unsigned short *>(buf) +
651 for (s = 0; s < nsamples; s++)
653 for ( row = 0; row < (int)height; row ++ )
657 itkExceptionMacro( <<
"Problem reading the row: " << row );
663 image =
reinterpret_cast<unsigned short*
>(out) + row * width * inc;
667 image =
reinterpret_cast<unsigned short*
>(out) + width * inc * (height - (row + 1));
669 for (cc = 0; cc < isize;
673 static_cast<unsigned short *>(buf) +
688 for ( row = 0; row < (int)height; row ++ )
692 itkExceptionMacro( <<
"Problem reading the row: " << row );
698 image =
reinterpret_cast<short*
>(out) + row * width * inc;
702 image =
reinterpret_cast<short*
>(out) + width * inc * (height - (row + 1));
705 for (cc = 0; cc < isize;
709 static_cast<short *>(buf) +
719 for (s = 0; s < nsamples; s++)
721 for ( row = 0; row < (int)height; row ++ )
725 itkExceptionMacro( <<
"Problem reading the row: " << row );
731 image =
reinterpret_cast<short*
>(out) + row * width * inc;
735 image =
reinterpret_cast<short*
>(out) + width * inc * (height - (row + 1));
737 for (cc = 0; cc < isize;
741 static_cast<short *>(buf) +
755 unsigned char *image = (
unsigned char*)out;
756 unsigned char *source = (
unsigned char*)in;
759 unsigned short red, green, blue, alpha;
764 PHOTOMETRIC_MINISBLACK )
768 unsigned short *image_us = (
unsigned short*)out;
769 unsigned short *source_us = (
unsigned short*)in;
770 *image_us = *source_us;
774 short *image_us = (
short*)out;
775 short *source_us = (
short*)in;
776 *image_us = *source_us;
780 char *image_us = (
char*)out;
781 char *source_us = (
char*)in;
782 *image_us = *source_us;
791 *image = ~( *source );
796 this->
GetColor(*source, &red, &green, &blue);
797 *image =
static_cast<unsigned char>(red >> 8);
803 unsigned short *image_us = (
unsigned short*)out;
804 unsigned short *source_us = (
unsigned short*)in;
807 green = *(source_us+1);
808 blue = *(source_us+2);
810 *(image_us+1) = green;
811 *(image_us+2) = blue;
814 alpha = *(source_us+3);
815 *(image_us+3) = 65535-alpha;
829 *(image+3) = 255-alpha;
837 unsigned short *image_us = (
unsigned short*)out;
838 unsigned short *source_us = (
unsigned short*)in;
839 this->
GetColor(*source_us, &red, &green, &blue);
840 *(image_us) = red << 8;
841 *(image_us+1) = green << 8;
842 *(image_us+2) = blue << 8;
846 short *image_us = (
short*)out;
847 short *source_us = (
short*)in;
848 this->
GetColor(*source_us, &red, &green, &blue);
849 *(image_us) = red << 8;
850 *(image_us+1) = green << 8;
851 *(image_us+2) = blue << 8;
855 this->
GetColor(*source, &red, &green, &blue);
856 *(image) = static_cast<char>(red >> 8);
857 *(image+1) = static_cast<char>(green >> 8);
858 *(image+2) = static_cast<char>(blue >> 8);
862 this->
GetColor(*source, &red, &green, &blue);
863 *(image) = static_cast<unsigned char>(red >> 8);
864 *(image+1) = static_cast<unsigned char>(green >> 8);
865 *(image+2) = static_cast<unsigned char>(blue >> 8);
878 unsigned short *green,
unsigned short *blue )
885 itkExceptionMacro( <<
"Color index has to be greater than 0" );
893 itkExceptionMacro( <<
"Color index has to be less than number of colors ("
903 unsigned short photometric;
909 itkExceptionMacro( <<
"You can only access colors for palette images" );
914 unsigned short *red_orig, *green_orig, *blue_orig;
918 case 1:
case 2:
case 4:
922 itkExceptionMacro( <<
"Sorry, can not handle image with "
928 &red_orig, &green_orig, &blue_orig))
930 itkExceptionMacro( <<
"Missing required \"Colormap\" tag" );
937 itkExceptionMacro( <<
"Color index has to be less than number of colors ("
945 *red = *(red_orig + index);
946 *green = *(green_orig + index);
947 *blue = *(blue_orig + index);
963 case PHOTOMETRIC_RGB:
964 case PHOTOMETRIC_YCBCR:
967 case PHOTOMETRIC_MINISWHITE:
968 case PHOTOMETRIC_MINISBLACK:
971 case PHOTOMETRIC_PALETTE:
972 for( cc=0; cc<256; cc++ )
974 unsigned short red, green, blue;
975 this->
GetColor( cc, &red, &green, &blue );
976 if ( red != green || red != blue )
992 unsigned char* volume =
reinterpret_cast<unsigned char*
>(buffer);
998 unsigned char *tempImage;
1003 itkExceptionMacro( <<
"Cannot read tile : "<< row <<
"," << col <<
" from file" );
1004 if ( tempImage != buffer )
1006 delete [] tempImage;
1012 unsigned int xx, yy;
1019 *volume = *(tempImage++);
1038 int32 subfiletype = 6;
1041 if(subfiletype != 0)
1059 unsigned short* volume =
reinterpret_cast<unsigned short*
>(buffer);
1065 short* volume =
reinterpret_cast<short*
>(buffer);
1071 char* volume =
reinterpret_cast<char*
>(buffer);
1077 unsigned char* volume =
reinterpret_cast<unsigned char*
>(buffer);
1086 tempImage =
new uint32[ width * height ];
1092 itkExceptionMacro( <<
"Cannot read TIFF image or as a TIFF RGBA image" );
1093 if ( tempImage != buffer )
1095 delete [] tempImage;
1104 unsigned short *fimage = (
unsigned short *)buffer;
1105 fimage += width*height*4*page;
1106 for ( yy = 0; yy < height; yy ++ )
1108 ssimage = tempImage + (height - yy - 1) * width;
1109 for ( xx = 0; xx < width; xx++ )
1111 unsigned short red =
static_cast<unsigned short>(TIFFGetR(*ssimage));
1112 unsigned short green =
static_cast<unsigned short>(TIFFGetG(*ssimage));
1113 unsigned short blue =
static_cast<unsigned short>(TIFFGetB(*ssimage));
1114 unsigned short alpha =
static_cast<unsigned short>(TIFFGetA(*ssimage));
1117 *(fimage+1) = green;
1119 *(fimage+3) = alpha;
1127 short *fimage = (
short *)buffer;
1128 fimage += width*height*4*page;
1129 for ( yy = 0; yy < height; yy ++ )
1131 ssimage = tempImage + (height - yy - 1) * width;
1132 for ( xx = 0; xx < width; xx++ )
1134 short red =
static_cast<short>(TIFFGetR(*ssimage));
1135 short green =
static_cast<short>(TIFFGetG(*ssimage));
1136 short blue =
static_cast<short>(TIFFGetB(*ssimage));
1137 short alpha =
static_cast<short>(TIFFGetA(*ssimage));
1140 *(fimage+1) = green;
1142 *(fimage+3) = alpha;
1150 char *fimage = (
char *)buffer;
1151 fimage += width*height*4*page;
1152 for ( yy = 0; yy < height; yy ++ )
1154 ssimage = tempImage + (height - yy - 1) * width;
1155 for ( xx = 0; xx < width; xx++ )
1157 char red =
static_cast<char>(TIFFGetR(*ssimage));
1158 char green =
static_cast<char>(TIFFGetG(*ssimage));
1159 char blue =
static_cast<char>(TIFFGetB(*ssimage));
1160 char alpha =
static_cast<char>(TIFFGetA(*ssimage));
1163 *(fimage+1) = green;
1165 *(fimage+3) = alpha;
1173 unsigned char *fimage = (
unsigned char *)buffer;
1174 fimage += width*height*4*page/2;
1175 for ( yy = 0; yy < height; yy ++ )
1177 ssimage = tempImage + (height - yy - 1) * width;
1178 for ( xx = 0; xx < width; xx++ )
1180 unsigned char red =
static_cast<unsigned char>(TIFFGetR(*ssimage));
1181 unsigned char green =
static_cast<unsigned char>(TIFFGetG(*ssimage));
1182 unsigned char blue =
static_cast<unsigned char>(TIFFGetB(*ssimage));
1183 unsigned char alpha =
static_cast<unsigned char>(TIFFGetA(*ssimage));
1186 *(fimage+1) = green;
1188 *(fimage+3) = alpha;
1194 if ( tempImage != 0 && tempImage != buffer )
1196 delete [] tempImage;
1201 unsigned int format = this->
GetFormat();
1210 unsigned short* volume =
reinterpret_cast<unsigned short*
>(buffer);
1216 short* volume =
reinterpret_cast<short*
>(buffer);
1222 char* volume =
reinterpret_cast<char*
>(buffer);
1228 unsigned char* volume =
reinterpret_cast<unsigned char*
>(buffer);
1239 unsigned short* volume =
reinterpret_cast<unsigned short*
>(buffer);
1245 short* volume =
reinterpret_cast<short*
>(buffer);
1251 char* volume =
reinterpret_cast<char*
>(buffer);
1257 unsigned char* volume =
reinterpret_cast<unsigned char*
>(buffer);
1275 itkExceptionMacro( <<
"This reader cannot read old JPEG compression" );
1301 tempImage =
new uint32[ width * height ];
1307 itkExceptionMacro( <<
"Cannot read TIFF image or as a TIFF RGBA image" );
1308 if ( tempImage != buffer )
1310 delete [] tempImage;
1318 unsigned char *fimage = (
unsigned char *)buffer;
1320 for ( yy = 0; yy < height; yy ++ )
1322 ssimage = tempImage + (height - yy - 1) * width;
1323 for ( xx = 0; xx < width; xx++ )
1325 unsigned char red =
static_cast<unsigned char>(TIFFGetR(*ssimage));
1326 unsigned char green =
static_cast<unsigned char>(TIFFGetG(*ssimage));
1327 unsigned char blue =
static_cast<unsigned char>(TIFFGetB(*ssimage));
1328 unsigned char alpha =
static_cast<unsigned char>(TIFFGetA(*ssimage));
1331 *(fimage+1) = green;
1333 *(fimage+3) = alpha;
1340 if ( tempImage != 0 && tempImage != buffer )
1342 delete [] tempImage;
1348 unsigned int format = this->
GetFormat();
1423 itkExceptionMacro(<<
"Cannot open the file!");
1548 std::string filename = name;
1555 std::string::size_type TIFFPos = filename.rfind(
".TIFF");
1556 if ( (TIFFPos != std::string::npos)
1557 && (TIFFPos == filename.length() - 5) )
1562 TIFFPos = filename.rfind(
".tiff");
1563 if ( (TIFFPos != std::string::npos)
1564 && (TIFFPos == filename.length() - 5) )
1569 TIFFPos = filename.rfind(
".tif");
1570 if ( (TIFFPos != std::string::npos)
1571 && (TIFFPos == filename.length() - 4) )
1577 TIFFPos = filename.rfind(
".TIF");
1578 if ( (TIFFPos != std::string::npos)
1579 && (TIFFPos == filename.length() - 4) )
1601 itkExceptionMacro(<<
"TIFF Writer can only write 2-d or 3-d images");
1609 static tsize_t
TIFFRead(thandle_t, tdata_t, tsize_t) {
return 0; }
1612 static tsize_t
TIFFWrite(thandle_t fd, tdata_t buf, tsize_t size)
1614 std::ostream *out =
reinterpret_cast<std::ostream *
>(fd);
1615 out->write(static_cast<char *>(buf), size);
1616 return out->fail() ?
static_cast<tsize_t
>(0) : size;
1619 static toff_t
TIFFSeek(thandle_t fd, toff_t off,
int whence)
1624 std::ostream *out =
reinterpret_cast<std::ostream *
>(fd);
1629 out->seekp(0, std::ios::end);
1630 need =
static_cast<int>(off) - static_cast<int>(out->tellp());
1633 char buf[1] = {
'\0'};
1634 for (
int ii = 0; ii < need; ii++) out->write(buf,1);
1637 out->seekp(off, std::ios::beg);
1640 out->seekp(off, std::ios::end);
1643 out->seekp(off, std::ios::cur);
1646 return static_cast<toff_t
>(out->tellp());
1648 return static_cast<toff_t
>(out->tellp());
1653 std::ostream *out =
reinterpret_cast<std::ostream *
>(fd);
1654 out->seekp(0, std::ios::end);
1655 return static_cast<toff_t
>(out->tellp());
1664 char *outPtr = (
char *) buffer;
1666 unsigned int width, height, page, pages = 1;
1675 double resolution = -1;
1676 uint32 rowsperstrip = (uint32) -1;
1695 <<
"TIFF supports unsigned/signed char and unsigned/signed short");
1700 TIFF *tif = TIFFOpen(
m_FileName.c_str(),
"w");
1703 itkExceptionMacro(
"Error while trying to open file for writing: "
1707 << itksys::SystemTools::GetLastSystemError());
1713 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
1721 TIFFCreateDirectory( tif );
1723 for (page = 0; page < pages; page++)
1725 TIFFSetDirectory( tif, page );
1726 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, w);
1727 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, h);
1728 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1729 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, scomponents);
1730 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
1731 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1735 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
1737 TIFFSetField(tif, TIFFTAG_SOFTWARE,
"InsightToolkit");
1739 if ( scomponents > 3 )
1743 uint16 extra_samples = scomponents-3;
1744 uint16 *sample_info =
new uint16[scomponents-3];
1745 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
1747 for ( cc = 1; cc < scomponents-3; cc ++ )
1749 sample_info[cc] = EXTRASAMPLE_UNSPECIFIED;
1751 TIFFSetField(tif,TIFFTAG_EXTRASAMPLES,extra_samples,
1753 delete [] sample_info;
1766 default: compression = COMPRESSION_NONE;
1771 compression = COMPRESSION_NONE;
1774 TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
1776 uint16 photometric = (scomponents ==1) ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB;
1778 if ( compression == COMPRESSION_JPEG )
1780 TIFFSetField(tif, TIFFTAG_JPEGQUALITY, 75);
1781 TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
1782 photometric = PHOTOMETRIC_YCBCR;
1784 else if ( compression == COMPRESSION_LZW )
1787 TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor);
1788 itkDebugMacro( <<
"LZW compression is patented outside US so it is disabled" );
1790 else if ( compression == COMPRESSION_DEFLATE )
1793 TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor);
1796 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
1799 TIFFTAG_ROWSPERSTRIP,
1800 TIFFDefaultStripSize(tif, rowsperstrip));
1803 TIFFSetField(tif, TIFFTAG_XRESOLUTION, resolution);
1804 TIFFSetField(tif, TIFFTAG_YRESOLUTION, resolution);
1805 TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
1811 TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
1813 TIFFSetField(tif, TIFFTAG_PAGENUMBER, page, pages);
1820 rowLength =
sizeof(
unsigned char);
1823 rowLength =
sizeof(
unsigned short);
1826 rowLength =
sizeof(char);
1829 rowLength =
sizeof(short);
1833 <<
"TIFF supports unsigned/signed char and unsigned/signed short");
1840 for (
unsigned int idx2 = 0; idx2 < height; idx2++)
1842 if ( TIFFWriteScanline(tif, const_cast<char*>(outPtr), row, 0) < 0)
1844 itkExceptionMacro( <<
"TIFFImageIO: error out of disk space" );
1847 outPtr += rowLength;
1853 TIFFWriteDirectory(tif);
1864 itkExceptionMacro( <<
"Need to call CanReadFile before" );
1882 itkExceptionMacro( <<
"Need to call CanReadFile before" );
1886 void *raw_data =
NULL;
1890 itkExceptionMacro( <<
"fld is NULL" );
1895 if( fld->field_passcount )
1899 itkExceptionMacro( <<
"Tag cannot be found" );
1904 if( fld->field_type != TIFF_BYTE )
1906 itkExceptionMacro( <<
"Tag is not of type TIFF_BYTE" );