Skip to content

Commit e24a53a

Browse files
author
Grok Compression
committed
TIFFFormat: warn if attempt to encode multi-page image
1 parent 7255da0 commit e24a53a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/lib/codec/formats/TIFFFormat.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)