lsst.afw  13.0-100-ge0459d59f+2
fitsCompression.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 #ifndef LSST_AFW_fitsCompression_h_INCLUDED
3 #define LSST_AFW_fitsCompression_h_INCLUDED
4 
5 #include <string>
6 #include <limits>
7 
8 #include "boost/cstdfloat.hpp"
9 
10 #include "lsst/pex/exceptions.h"
11 #include "lsst/daf/base.h"
12 #include "ndarray.h"
13 #include "ndarray/eigen.h"
14 
15 #include "lsst/afw/image/Image.h"
16 #include "lsst/afw/image/Mask.h"
17 
18 namespace lsst {
19 namespace afw {
20 namespace fits {
21 
22 // Forward declarations
23 class Fits;
24 
25 namespace detail {
26 
28 template <typename T>
29 struct Bitpix;
30 
31 template <>
32 struct Bitpix<std::uint8_t> {
33  static int const value = 8;
34 };
35 template <>
36 struct Bitpix<std::int16_t> {
37  static int const value = 16;
38 };
39 template <>
40 struct Bitpix<std::int32_t> {
41  static int const value = 32;
42 };
43 template <>
44 struct Bitpix<std::int64_t> {
45  static int const value = 64;
46 };
47 template <>
48 struct Bitpix<std::uint16_t> {
49  static int const value = 16;
50 };
51 template <>
52 struct Bitpix<std::uint32_t> {
53  static int const value = 32;
54 };
55 template <>
56 struct Bitpix<std::uint64_t> {
57  static int const value = 64;
58 };
59 template <>
60 struct Bitpix<float> {
61  static int const value = -32;
62 };
63 template <>
64 struct Bitpix<double> {
65  static int const value = -64;
66 };
67 
76  public:
77  virtual ~PixelArrayBase() {}
78 
80  virtual void const* getData() const = 0;
81 
83  std::size_t getNumElements() const { return _num; }
84 
85  protected:
86  PixelArrayBase(std::size_t num) : _num(num) {}
87 
88  private:
89  std::size_t _num; // Number of pixel values
90 };
91 
93 template <typename T>
94 class PixelArray : public PixelArrayBase {
95  public:
96  PixelArray() = delete;
97  PixelArray(PixelArray const&) = delete;
98 
102  PixelArray(ndarray::Array<T, 1, 1> const& array)
103  : PixelArrayBase(array.getNumElements()), _pixels(array.getData()), _manager(array.getManager()) {}
104 
108  template <typename U>
109  PixelArray(ndarray::Array<U, 1, 1> const& array)
110  : PixelArrayBase(array.getNumElements()) {
111  auto mem = ndarray::SimpleManager<U>::allocate(getNumElements());
112  _manager = mem.first;
113  _pixels = mem.second;
114  std::copy(array.begin(), array.end(),
115  const_cast<typename std::remove_const<T>::type *>(reinterpret_cast<T const*>(_pixels)));
116  }
117 
118  virtual ~PixelArray() {}
119 
120  void const* getData() const override { return _pixels; }
121 
122  private:
123  void const* _pixels; // The data
124  ndarray::Manager::Ptr _manager; // Memory manager; holds onto the data while we use it
125 };
126 
132 template <typename T>
133 std::shared_ptr<PixelArrayBase> makePixelArray(
134  int bitpix,
135  ndarray::Array<T, 1, 1> const& array
136 ) {
137  switch (bitpix) {
138  case 0: return std::make_shared<PixelArray<T>>(array);
139  case 8: return std::make_shared<PixelArray<std::uint8_t>>(array);
140  case 16: return std::make_shared<PixelArray<std::int16_t>>(array);
141  case 32: return std::make_shared<PixelArray<std::int32_t>>(array);
142  case 64: return std::make_shared<PixelArray<std::int64_t>>(array);
143  case -32: return std::make_shared<PixelArray<boost::float32_t>>(array);
144  case -64: return std::make_shared<PixelArray<boost::float64_t>>(array);
145  default:
146  std::ostringstream os;
147  os << "Unrecognized bitpix: " << bitpix;
148  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, os.str());
149  }
150 }
151 
152 } // namespace detail
153 
154 
189  };
190  typedef ndarray::Array<long, 1, 1> Tiles;
191 
193  Tiles tiles;
195 
198  CompressionAlgorithm algorithm_,
199  Tiles tiles_,
200  float quantizeLevel_=0.0
201  ) : algorithm(algorithm_), tiles(ndarray::copy(tiles_)), quantizeLevel(quantizeLevel_) {}
202 
204  CompressionAlgorithm algorithm_,
205  std::vector<long> tiles_,
206  float quantizeLevel_=0.0
207  ) : algorithm(algorithm_), tiles(ndarray::allocate(tiles_.size())), quantizeLevel(quantizeLevel_) {
208  std::copy(tiles_.cbegin(), tiles_.cend(), tiles.begin());
209  }
210 
216  explicit ImageCompressionOptions(
217  CompressionAlgorithm algorithm_,
218  int rows=1,
219  float quantizeLevel_=0.0
220  );
221 
226  template <typename T>
228  ImageCompressionOptions(image.getBBox().getArea() > 0 ? NONE : NONE) {}
229  template <typename T>
231  ImageCompressionOptions(mask.getBBox().getArea() > 0 ? NONE : NONE) {}
232 
233  // Disable compression for int64: cfitsio won't compress them
235  ImageCompressionOptions(NONE) {}
237  ImageCompressionOptions(NONE) {}
239  ImageCompressionOptions(NONE) {}
241  ImageCompressionOptions(NONE) {}
242 };
243 
246 
249 
252 
255 
256 
267 struct ImageScale {
268  int bitpix;
269  double bscale;
270  double bzero;
271  long blank;
272 
281  ImageScale(int bitpix_, double bscale_, double bzero_) :
282  bitpix(bitpix_), bscale(bscale_), bzero(std::floor(bzero_/bscale_ + 0.5)*bscale_),
283  blank(bitpix > 0 ? (bitpix == 8 ? 255 : (1L << (bitpix - 1)) - 1) : 0) {}
284 
295  template <typename T>
296  std::shared_ptr<detail::PixelArrayBase> toFits(
297  ndarray::Array<T const, 2, 2> const& image,
298  bool forceNonfiniteRemoval,
299  bool fuzz=true,
300  ndarray::Array<long, 1> const& tiles=ndarray::Array<long, 1, 1>(),
301  int seed=1
302  ) const;
303 
309  template <typename T>
310  ndarray::Array<T, 2, 2> fromFits(ndarray::Array<T, 2, 2> const& image) const;
311 };
312 
313 
364  public:
372  };
374  int bitpix;
375  bool fuzz;
376  int seed;
377  std::vector<std::string> maskPlanes;
379  float quantizePad;
380  double bscale, bzero;
381 
386  : ImageScalingOptions(NONE, 0, {}, 1, 4.0, 5.0, false, std::numeric_limits<double>::quiet_NaN(),
387  std::numeric_limits<double>::quiet_NaN()) {}
388 
401  ScalingAlgorithm algorithm_,
402  int bitpix_,
403  std::vector<std::string> const& maskPlanes_={},
404  int seed_=1,
405  float quantizeLevel_=4.0,
406  float quantizePad_=5.0,
407  bool fuzz_=true,
408  double bscale_=1.0,
409  double bzero_=0.0
410  );
411 
417  ImageScalingOptions(int bitpix_, double bscale_=1.0, double bzero_=0.0)
418  : ImageScalingOptions(MANUAL, bitpix_, {}, 1, 4.0, 5.0, false, bscale_, bzero_) {}
419 
420  //{
425  template <typename T>
427  image::ImageBase<T> const& image,
428  std::shared_ptr<image::Mask<image::MaskPixel> const> mask=nullptr
429  ) const {
430  auto const arrays = _toArray(image, mask);
431  return determine(arrays.first, arrays.second);
432  }
433 
434  template <typename T, int N>
435  ImageScale determine(
436  ndarray::Array<T const, N, N> const& image,
437  ndarray::Array<bool, N, N> const& mask
438  ) const;
439  //}
440 
441  private:
443  template <typename T>
444  std::pair<ndarray::Array<T const, 2, 2>, ndarray::Array<bool, 2, 2>> _toArray(
445  image::ImageBase<T> const& image,
446  std::shared_ptr<image::Mask<image::MaskPixel> const> mask=nullptr
447  ) const {
448  if (mask && image.getDimensions() != mask->getDimensions()) {
449  std::ostringstream os;
450  os << "Size mismatch between image and mask: ";
451  os << image.getWidth() << "x" << image.getHeight();
452  os << " vs ";
453  os << mask->getWidth() << "x" << mask->getHeight();
454  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, os.str());
455  }
456  ndarray::Array<T const, 2, 2> imageArray = ndarray::dynamic_dimension_cast<2>(image.getArray());
457  if (imageArray.empty()) imageArray = ndarray::copy(image.getArray());
458  ndarray::Array<bool, 2, 2> maskArray = ndarray::allocate(imageArray.getShape());
459  if (mask) {
460  maskArray.deep() = (mask->getArray() & mask->getPlaneBitMask(maskPlanes));
461  } else {
462  maskArray.deep() = false;
463  }
464  return std::make_pair(imageArray, maskArray);
465  }
466 
473  template <typename T, int N>
474  ImageScale determineFromRange(
475  ndarray::Array<T const, N, N> const& image,
476  ndarray::Array<bool, N, N> const& mask,
477  bool isUnsigned=false,
478  bool cfitsioPadding=true
479  ) const;
480 
487  template <typename T, int N>
488  ImageScale determineFromStdev(
489  ndarray::Array<T const, N, N> const& image,
490  ndarray::Array<bool, N, N> const& mask,
491  bool isUnsigned=false,
492  bool cfitsioPadding=true
493  ) const;
494 
495 };
496 
499 
502 
503 }}} // namespace lsst::afw::fits
504 
505 #endif // ifndef LSST_AFW_fitsCompression_h_INCLUDED
ImageCompressionOptions(image::Mask< std::uint64_t > const &mask)
int bitpix
Bits per pixel (0, 8,16,32,64,-32,-64)
std::shared_ptr< PixelArrayBase > makePixelArray(int bitpix, ndarray::Array< T, 1, 1 > const &array)
Create a PixelArray suitable for an image with the nominated BITPIX.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
afw::table::Key< afw::table::Array< MaskPixelT > > mask
Scale to preserve dynamic range.
int compressionAlgorithmToCfitsio(ImageCompressionOptions::CompressionAlgorithm algorithm)
Convert ImageCompressionOptions::CompressionAlgorithm to cfitsio.
long blank
Value for integer images indicating non-finite values.
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:299
int bitpix
Bits per pixel; negative means floating-point: 8,16,32,64,-32,-64.
PixelArray(ndarray::Array< T, 1, 1 > const &array)
Construct from an ndarray::Array of the same type.
ImageCompressionOptions(image::Mask< std::int64_t > const &mask)
STL namespace.
PixelArray(ndarray::Array< U, 1, 1 > const &array)
Construct from an ndarray::Array of different type.
CompressionAlgorithm
Compression algorithms.
std::size_t getNumElements() const
Return the number of pixels.
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:106
Typed array of pixel values.
FITS BITPIX header value by C++ type.
int seed
Seed for random number generator when fuzzing.
ImageCompressionOptions(image::Mask< T > const &mask)
ImageCompressionOptions(image::Image< std::uint64_t > const &image)
Extent< int, N > floor(Extent< double, N > const &input)
Return the component-wise floor (round towards more negative).
Tiles tiles
Tile size; a dimension with 0 means infinite (e.g., to specify one row: 0,1)
ImageCompressionOptions(CompressionAlgorithm algorithm_, Tiles tiles_, float quantizeLevel_=0.0)
Custom compression.
Options for tile compression of image pixels.
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: ImageBase.h:357
A base class for image defects.
Definition: cameraGeom.dox:3
float quantizeLevel
Divisor of the standard deviation for STDEV_* scaling.
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:83
double bzero
Zero-point to apply when reading from FITS.
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromString(std::string const &name)
Interpret compression algorithm expressed in string.
ImageCompressionOptions(image::Image< T > const &image)
Default compression for a particular style of image.
ndarray::Array< long, 1, 1 > Tiles
Options for scaling image pixels.
double bscale
Scale to apply when reading from FITS.
std::string scalingAlgorithmToString(ImageScalingOptions::ScalingAlgorithm algorithm)
Provide string version of compression algorithm.
std::string compressionAlgorithmToString(ImageCompressionOptions::CompressionAlgorithm algorithm)
Provide string version of compression algorithm.
ImageCompressionOptions(image::Image< std::int64_t > const &image)
void const * getData() const override
Return a void* array of the pixels.
Abstract base class for an array of pixel values.
int getWidth() const
Return the number of columns in the image.
Definition: ImageBase.h:297
float quantizeLevel
quantization level: 0.0 = none requires use of GZIP or GZIP_SHUFFLE
Scale based on the standard deviation, dynamic range negative.
double bzero
Manually specified BSCALE and BZERO (for MANUAL scaling)
ImageScale(int bitpix_, double bscale_, double bzero_)
Constructor.
CompressionAlgorithm algorithm
Compresion algorithm to use.
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromCfitsio(int cfitsio)
Convert compression algorithm from cfitsio to ImageCompressionOptions::CompressionAlgorithm.
ImageScale determine(image::ImageBase< T > const &image, std::shared_ptr< image::Mask< image::MaskPixel > const > mask=nullptr) const
Determine the scaling for a particular image.
ScalingAlgorithm algorithm
Scaling algorithm to use.
Scale based on the standard deviation, dynamic range positive+negative.
float quantizePad
Number of stdev to allow on the low/high side (for STDEV_POSITIVE/NEGATIVE)
ImageCompressionOptions(CompressionAlgorithm algorithm_, std::vector< long > tiles_, float quantizeLevel_=0.0)
bool fuzz
Fuzz the values when quantising floating-point values?
std::vector< std::string > maskPlanes
Mask planes to ignore when doing statistics.
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:68
Scale based on the standard deviation. dynamic range positive.
ImageScalingOptions(int bitpix_, double bscale_=1.0, double bzero_=0.0)
Manual scaling Ctor.
ImageScalingOptions::ScalingAlgorithm scalingAlgorithmFromString(std::string const &name)
Interpret scaling algorithm expressed in string.
GZIP compression with shuffle (most-significant byte first)
Scale to apply to image.