Scroom  0.14
Scroom::Tiff Namespace Reference

Classes

class  Source
 

Typedefs

using TIFFPtr = boost::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 std::string &fileName)
 

Typedef Documentation

◆ TIFFPtr

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

Function Documentation

◆ getAspectRatio()

boost::optional<Scroom::Utils::Point<double> > Scroom::Tiff::getAspectRatio ( const TIFFPtr tif)
92  {
93  float resolutionX{};
94  float resolutionY{};
95  uint16_t resolutionUnit{};
96 
97  if(TIFFGetField(tif.get(), TIFFTAG_XRESOLUTION, &resolutionX) && TIFFGetField(tif.get(), TIFFTAG_YRESOLUTION, &resolutionY)
98  && TIFFGetField(tif.get(), TIFFTAG_RESOLUTIONUNIT, &resolutionUnit))
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 
)
68  {
69  uint16_t* r{};
70  uint16_t* g{};
71  uint16_t* b{};
73 
74  const int result = TIFFGetField(tif.get(), TIFFTAG_COLORMAP, &r, &g, &b);
75  if(result == 1)
76  {
78  colormap->name = "Original";
79  const size_t count = 1UL << bps;
80  colormap->colors.resize(count);
81 
82  for(size_t i = 0; i < count; i++)
83  {
84  colormap->colors[i] = Color(1.0 * r[i] / 0xFFFF, 1.0 * g[i] / 0xFFFF, 1.0 * b[i] / 0xFFFF);
85  }
86  }
87 
88  return colormap;
89  }

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  }

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

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

Here is the call graph for this function:
Here is the caller graph for this function:
Scroom::Tiff::getColorMap
Colormap::Ptr getColorMap(const TIFFPtr &tif, uint16_t bps)
Definition: tiffsource.cc:67
TT
#define TT(x)
Definition: tiffsource.cc:61
Scroom::Tiff::getAspectRatio
boost::optional< Scroom::Utils::Point< double > > getAspectRatio(const TIFFPtr &tif)
Definition: tiffsource.cc:91
Scroom::Tiff::getColormapHelper
ColormapHelperBase::Ptr getColormapHelper(const TIFFPtr &tif, uint16_t bps)
Definition: tiffsource.cc:113
MonochromeColormapHelper::create
static Ptr create(int numberOfColors)
Definition: colormap-helpers.cc:72
Scroom::TiledBitmap::Colormapped
const std::string Colormapped
Definition: layerspecforbitmap.cc:56
anonymous_namespace{tiffsource.cc}::TIFFCloseUnlessNull
void TIFFCloseUnlessNull(TIFF *tif)
Definition: tiffsource.cc:52
Colormap::create
static Colormap::Ptr create()
Definition: colormappable.hh:48
Scroom::TiledBitmap::BitmapMetaData
Definition: opentiledbitmapinterface.hh:41
Scroom::Tiff::TIFFPtr
boost::shared_ptr< TIFF > TIFFPtr
Definition: tiffsource.hh:28
MonochromeColormapHelper::createInverted
static Ptr createInverted(int numberOfColors)
Definition: colormap-helpers.cc:77
Colormap::Ptr
boost::shared_ptr< Colormap > Ptr
Definition: colormappable.hh:31
colormap
const Colormap::Ptr colormap
Definition: colormaphelpers_test.cc:54
ColormapHelperBase::Ptr
boost::shared_ptr< ColormapHelperBase > Ptr
Definition: colormappable.hh:160
Scroom::TiledBitmap::CMYK
const std::string CMYK
Definition: layerspecforbitmap.cc:54
b
static void b(const B::Ptr &)
Definition: gtkhelper-tests.cc:32
ColormapHelper::create
static Ptr create(int numberOfColors)
Definition: colormap-helpers.cc:55
Color
Definition: color.hh:34
Scroom::TiledBitmap::Greyscale
const std::string Greyscale
Definition: layerspecforbitmap.cc:55
Scroom::TiledBitmap::RGB
const std::string RGB
Definition: layerspecforbitmap.cc:53