121 {
122 try
123 {
125 if(!tif)
126 {
127
128 logger->error("Failed to open file {}", fileName);
129 return {};
130 }
131
132 auto spp = TIFFGetFieldCheckedOr<uint16_t>(tif,
TT(TIFFTAG_SAMPLESPERPIXEL), 1);
133 auto bps = TIFFGetFieldCheckedOr<uint16_t>(tif,
TT(TIFFTAG_BITSPERSAMPLE), (spp == 1) ? 1 : 8);
134 auto width = TIFFGetFieldChecked<uint32_t>(tif,
TT(TIFFTAG_IMAGEWIDTH));
135 auto height = TIFFGetFieldChecked<uint32_t>(tif,
TT(TIFFTAG_IMAGELENGTH));
136 auto photometric = TIFFGetFieldChecked<uint16_t>(tif,
TT(TIFFTAG_PHOTOMETRIC));
137
139
140 if(photometric != PHOTOMETRIC_PALETTE && colormapHelper)
141 {
142 logger->warn("Tiff contains a colormap, but photometric isn't palette");
143 colormapHelper.reset();
144 }
145
146 auto numberOfGreyscaleColors = (bps == 2 || bps == 4) ? (1 << bps) : 2;
147 switch(photometric)
148 {
149 case PHOTOMETRIC_MINISBLACK:
151 break;
152
153 case PHOTOMETRIC_MINISWHITE:
155 break;
156
157 case PHOTOMETRIC_PALETTE:
158 if(!colormapHelper)
159 {
160 logger->warn(
"Photometric is palette, but tiff doesn't contain a colormap");
162 }
163 break;
164
165 case PHOTOMETRIC_RGB:
166 case PHOTOMETRIC_SEPARATED:
167 break;
168
169 default:
170 logger->error(
"Unrecognized value {} for photometric", photometric);
171 return {};
172 }
173
175 if(aspectRatio)
176 {
177 logger->debug(
"This bitmap has size {}*{}, aspect ratio {:.2}*{:.2}", width, height, aspectRatio->x, aspectRatio->y);
178 }
179 else
180 {
181 logger->debug(
"This bitmap has size {}*{}", width, height);
182 }
183
184 BitmapMetaData bmd{{}, bps, spp, Scroom::Utils::make_rect<int>(0, 0, width, height), aspectRatio, colormapHelper};
185
186 if(bps != 1 && bps != 2 && bps != 4 && bps != 8)
187 {
188 logger->error(
"{} bits per sample not supported (yet)", bps);
189 return {};
190 }
191
192 if(spp == 4)
193 {
195 }
196 else if(spp == 3)
197 {
199
200 if(bps != 8)
201 {
202 logger->error(
"A RGB bitmap with {} samples per pixel isn't supported (yet)", bps);
203 return {};
204 }
205 }
206 else if(spp == 1)
207 {
208 bmd.type = (photometric == PHOTOMETRIC_PALETTE) ? Colormapped :
Greyscale;
209 }
210 else
211 {
212 logger->error(
"{} samples per pixel not supported (yet)", spp);
213 return {};
214 }
215
216 return std::make_tuple(bmd, tif);
217 }
218 catch(const std::exception& ex)
219 {
220 logger->error(
"{}", ex.what());
221 return {};
222 }
223 }
std::shared_ptr< ColormapHelperBase > Ptr
Definition colormappable.hh:158
static Ptr create(int numberOfColors)
Definition colormap-helpers.cc:68
static Ptr createInverted(int numberOfColors)
Definition colormap-helpers.cc:73
std::shared_ptr< TIFF > TIFFPtr
Definition tiffsource.hh:29
const std::string Greyscale
Definition layerspecforbitmap.cc:53
const std::string CMYK
Definition layerspecforbitmap.cc:52
const std::string RGB
Definition layerspecforbitmap.cc:51
Scroom::Logger logger
Definition callbacks.cc:54
void TIFFCloseUnlessNull(TIFF *tif)
Definition tiffsource.cc:50
#define TT(x)
Definition tiffsource.cc:59