Scroom 0.14-48-ga0fee447
Loading...
Searching...
No Matches
Scroom::Tiff Namespace Reference

Classes

class  Source
 

Typedefs

using TIFFPtr = std::shared_ptr< TIFF >
 

Functions

Colormap::Ptr getColorMap (const TIFFPtr &tif, uint16_t bps)
 
boost::optional< Scroom::Utils::Point< double > > getAspectRatio (const TIFFPtr &tif)
 
ColormapHelperBase::Ptr getColormapHelper (const TIFFPtr &tif, uint16_t bps)
 
boost::optional< std::tuple< Scroom::TiledBitmap::BitmapMetaData, TIFFPtr > > open (const Scroom::Logger &logger, const std::string &fileName)
 

Typedef Documentation

◆ TIFFPtr

using Scroom::Tiff::TIFFPtr = typedef std::shared_ptr<TIFF>

Function Documentation

◆ getAspectRatio()

boost::optional< Scroom::Utils::Point< double > > Scroom::Tiff::getAspectRatio ( const TIFFPtr tif)
90 {
91 float resolutionX{};
92 float resolutionY{};
93 uint16_t resolutionUnit{};
94
95 if(
96 TIFFGetField(tif.get(), TIFFTAG_XRESOLUTION, &resolutionX) && TIFFGetField(tif.get(), TIFFTAG_YRESOLUTION, &resolutionY)
97 && TIFFGetField(tif.get(), TIFFTAG_RESOLUTIONUNIT, &resolutionUnit)
98 )
99 {
100 if(resolutionUnit != RESUNIT_NONE)
101 {
102 // Fix aspect ratio only
103 float const base = std::max(resolutionX, resolutionY);
104 resolutionX = base / resolutionX;
105 resolutionY = base / resolutionY;
106 }
107 return Scroom::Utils::make_point<double>(resolutionX, resolutionY);
108 }
109
110 return {};
111 }

Referenced by open().

Here is the caller graph for this function:

◆ getColorMap()

Colormap::Ptr Scroom::Tiff::getColorMap ( const TIFFPtr tif,
uint16_t  bps 
)
66 {
67 uint16_t* r{};
68 uint16_t* g{};
69 uint16_t* b{};
71
72 const int result = TIFFGetField(tif.get(), TIFFTAG_COLORMAP, &r, &g, &b);
73 if(result == 1)
74 {
76 colormap->name = "Original";
77 const size_t count = 1UL << bps;
78 colormap->colors.resize(count);
79
80 for(size_t i = 0; i < count; i++)
81 {
82 colormap->colors[i] = Color(1.0 * r[i] / 0xFFFF, 1.0 * g[i] / 0xFFFF, 1.0 * b[i] / 0xFFFF);
83 }
84 }
85
86 return colormap;
87 }
Blob::Ptr const b
Definition blob-tests.cc:118
Definition color.hh:37
static Colormap::Ptr create()
Definition colormappable.hh:46
std::shared_ptr< Colormap > Ptr
Definition colormappable.hh:29
Colormap::Ptr const colormap
Definition colormaphelpers_test.cc:55
const size_t count
Definition pageprovider-tests.cc:21
SampleIterator< const uint8_t > result
Definition sampleiterator-tests.cc:94
Scroom::Utils::Rectangle< double > const r
Definition transformpresentation_test.cc:65

Referenced by getColormapHelper().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getColormapHelper()

ColormapHelperBase::Ptr Scroom::Tiff::getColormapHelper ( const TIFFPtr tif,
uint16_t  bps 
)
114 {
115 auto colorMap = getColorMap(tif, bps);
116 return colorMap ? ColormapHelper::create(colorMap) : nullptr;
117 }
static Ptr create(int numberOfColors)
Definition colormap-helpers.cc:51
Colormap::Ptr getColorMap(const TIFFPtr &tif, uint16_t bps)
Definition tiffsource.cc:65

Referenced by open().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ open()

boost::optional< std::tuple< Scroom::TiledBitmap::BitmapMetaData, TIFFPtr > > Scroom::Tiff::open ( const Scroom::Logger logger,
const std::string &  fileName 
)
121 {
122 try
123 {
124 TIFFPtr const tif(TIFFOpen(fileName.c_str(), "r"), &TIFFCloseUnlessNull);
125 if(!tif)
126 {
127 // Todo: report error
128 logger->error("Failed to open file {}", fileName);
129 return {};
130 }
131
132 auto spp = TIFFGetFieldCheckedOr<uint16_t>(tif, TT(TIFFTAG_SAMPLESPERPIXEL), 1); // Default value, according to tiff spec
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
138 ColormapHelperBase::Ptr colormapHelper = getColormapHelper(tif, bps);
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; // Yuk
147 switch(photometric)
148 {
149 case PHOTOMETRIC_MINISBLACK:
150 colormapHelper = MonochromeColormapHelper::create(numberOfGreyscaleColors);
151 break;
152
153 case PHOTOMETRIC_MINISWHITE:
154 colormapHelper = MonochromeColormapHelper::createInverted(numberOfGreyscaleColors);
155 break;
156
157 case PHOTOMETRIC_PALETTE:
158 if(!colormapHelper)
159 {
160 logger->warn("Photometric is palette, but tiff doesn't contain a colormap");
161 colormapHelper = ColormapHelper::create(1 << bps);
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
174 auto aspectRatio = getAspectRatio(tif);
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 {
194 bmd.type = CMYK;
195 }
196 else if(spp == 3)
197 {
198 bmd.type = RGB;
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
Scroom::Utils::Point< double > getAspectRatio(const PresentationInterface::Ptr &presentation)
Definition transformpresentation.cc:162

Referenced by Tiff::open(), and Scroom::Tiff::Source::reset().

Here is the call graph for this function:
Here is the caller graph for this function: