@@ -991,6 +991,8 @@ grk_image* TIFFFormat<T>::decode(const std::string& filename, grk_cparameters* p
991991 uint16_t compress;
992992 float *luma = nullptr , *refBlackWhite = nullptr ;
993993 uint16_t *red_orig = nullptr , *green_orig = nullptr , *blue_orig = nullptr ;
994+ uint32_t num_pages = 0 ;
995+ uint32_t cur_dir;
994996
995997 tif_ = TIFFOpen (filename.c_str (), " r" );
996998 if (!tif_)
@@ -1002,6 +1004,9 @@ grk_image* TIFFFormat<T>::decode(const std::string& filename, grk_cparameters* p
10021004 if (TIFFIsTiled (tif_))
10031005 {
10041006 spdlog::error (" TIFFFormat<T>::decode: tiled TIFF images not supported" );
1007+ if (tif_)
1008+ TIFFClose (tif_);
1009+ tif_ = nullptr ;
10051010 return 0 ;
10061011 }
10071012
@@ -1033,6 +1038,29 @@ grk_image* TIFFFormat<T>::decode(const std::string& filename, grk_cparameters* p
10331038
10341039 // 1. sanity checks
10351040
1041+ cur_dir = TIFFCurrentDirectory (tif_);
1042+ do
1043+ {
1044+ uint32_t subfiletype = 0 ;
1045+ if (TIFFGetField (tif_, TIFFTAG_SUBFILETYPE, &subfiletype))
1046+ {
1047+ if (subfiletype == FILETYPE_PAGE)
1048+ num_pages++;
1049+ }
1050+ else
1051+ {
1052+ num_pages++;
1053+ }
1054+ } while (TIFFReadDirectory (tif_));
1055+ if (num_pages > 1 )
1056+ spdlog::warn (
1057+ " TIFFFormat<T>::decode: multi-page document detected. Only the first page will be encoded" );
1058+ if (!TIFFSetDirectory (tif_, cur_dir))
1059+ {
1060+ spdlog::error (" TIFFFormat<T>::decode: failed to reset to directory {}" , cur_dir);
1061+ goto cleanup;
1062+ }
1063+
10361064 // check for supported photometric interpretation
10371065 if (tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_MINISWHITE &&
10381066 tiPhoto != PHOTOMETRIC_RGB && tiPhoto != PHOTOMETRIC_ICCLAB && tiPhoto != PHOTOMETRIC_CIELAB &&
0 commit comments