25 if (name ==
"HCOMPRESS")
38 return "GZIP_SHUFFLE";
45 os <<
"Unrecognized compression algorithm: " << algorithm;
64 "Unsupported compression algorithm: HCOMPRESS_1");
67 os <<
"Unrecognized cfitsio compression: " << cfitsio;
86 os <<
"Unrecognized compression algorithm: " << algorithm;
92 int rows,
float quantizeLevel_)
93 : algorithm(algorithm_), tiles(
ndarray::allocate(MAX_COMPRESS_DIM)), quantizeLevel(quantizeLevel_) {
96 for (
int ii = 2; ii < MAX_COMPRESS_DIM; ++ii)
tiles[ii] = 1;
115 return "STDEV_POSITIVE";
117 return "STDEV_NEGATIVE";
122 os <<
"Unrecognized scaling algorithm: " <<
algorithm;
129 float quantizeLevel_,
float quantizePad_,
bool fuzz_,
double bscale_,
134 seed(
std::abs(seed_ - 1) % (N_RANDOM - 1) +
136 maskPlanes(maskPlanes_),
138 quantizePad(quantizePad_),
145 template <
typename T,
int N>
147 ndarray::Array<bool, N, N>
const&
mask) {
149 auto const& flatMask = ndarray::flatten<1>(
mask);
150 for (
auto mm = flatMask.begin(); mm != flatMask.end(); ++mm) {
153 ndarray::Array<T, 1, 1> array = ndarray::allocate(num);
154 auto const& flatImage = ndarray::flatten<1>(
image);
155 auto mm = ndarray::flatten<1>(
mask).begin();
156 auto aa = array.begin();
157 for (
auto ii = flatImage.begin(); ii != flatImage.end(); ++ii, ++mm) {
164 auto const q1 = num / 4;
165 auto const q2 = num / 2;
166 auto const q3 = q1 + q2;
171 T
const median = num % 2 ? array[num / 2] : 0.5 * (array[num / 2] + array[num / 2 - 1]);
174 T
const lq = array[q1];
175 T
const uq = array[q3];
180 template <
typename T,
int N>
181 std::pair<T, T> calculateMinMax(ndarray::Array<T const, N, N>
const& image,
182 ndarray::Array<bool, N, N>
const& mask) {
184 auto mm = ndarray::flatten<1>(
mask).begin();
185 auto const& flatImage = ndarray::flatten<1>(
image);
186 for (
auto ii = flatImage.begin(); ii != flatImage.end(); ++ii, ++mm) {
190 if (*ii < min) min = *ii;
196 template <
typename T>
197 double rangeForBitpix(
int bitpix,
bool cfitsioPadding) {
201 double range =
std::pow(2.0, bitpix) - 1;
202 if (cfitsioPadding) {
210 template <
typename T,
int N>
211 ImageScale ImageScalingOptions::determineFromRange(ndarray::Array<T const, N, N>
const&
image,
212 ndarray::Array<bool, N, N>
const&
mask,
bool isUnsigned,
213 bool cfitsioPadding)
const {
214 auto minMax = calculateMinMax(image, mask);
215 T
const min = minMax.first;
216 T
const max = minMax.second;
218 double range = rangeForBitpix<T>(
bitpix, cfitsioPadding);
220 double const bscale =
static_cast<T
>((max -
min) / range);
221 double bzero =
static_cast<T
>(isUnsigned ?
min : min + 0.5 * range *
bscale);
222 if (cfitsioPadding) {
229 template <
typename T,
int N>
230 ImageScale ImageScalingOptions::determineFromStdev(ndarray::Array<T const, N, N>
const& image,
231 ndarray::Array<bool, N, N>
const& mask,
bool isUnsigned,
232 bool cfitsioPadding)
const {
233 auto stats = calculateMedianStdev(image, mask);
234 auto const median = stats.first, stdev = stats.second;
238 auto minMax = calculateMinMax(image, mask);
239 T
const min = minMax.first;
240 T
const max = minMax.second;
241 double range = rangeForBitpix<T>(
bitpix, cfitsioPadding);
242 double const numUnique = (max -
min) / bscale;
246 if (numUnique < range) {
254 diskVal = (bitpix == 8) ? 0 : -(1L << (bitpix - 1));
260 diskVal = (bitpix == 8) ? 255 : (1L << (bitpix - 1)) - 1;
273 double bzero =
static_cast<T
>(imageVal - bscale * diskVal);
278 template <
typename T,
class Enable =
void>
280 static double constexpr value = 0.0;
288 static double constexpr value = 0.0;
292 template <
typename T>
293 struct Bzero<T, typename
std::enable_if<std::numeric_limits<T>::is_integer &&
294 !std::numeric_limits<T>::is_signed>::type> {
298 #ifndef DOXYGEN // suppress a bogus Doxygen complaint about an documented symbol 299 template <
typename T,
int N>
301 ndarray::Array<bool, N, N>
const& mask)
const {
305 "Image scaling not supported for integral types");
313 return determineFromRange(image, mask, isUnsigned, cfitsioPadding);
319 return determineFromStdev(image, mask, isUnsigned, cfitsioPadding);
332 class CfitsioRandom {
335 CfitsioRandom(
int seed) : _seed(seed) {
336 assert(seed != 0 && seed < N_RANDOM);
342 void resetForTile(
int iTile) {
343 _start = (iTile + _seed - 1) % N_RANDOM;
357 if (_index == N_RANDOM) {
359 if (_start == N_RANDOM) {
367 template <
typename T>
368 ndarray::Array<T, 1, 1> forImage(
typename ndarray::Array<T const, 2, 2>::Index
const& shape,
369 ndarray::Array<long, 1>
const& tiles) {
370 std::size_t const xSize = shape[1], ySize = shape[0];
371 ndarray::Array<T, 1, 1> out = ndarray::allocate(xSize * ySize);
372 std::size_t const xTileSize = tiles[0] <= 0 ? xSize : tiles[0];
373 std::size_t const yTileSize = tiles[1] < 0 ? ySize : (tiles[1] == 0 ? 1 : tiles[1]);
374 int const xNumTiles =
std::ceil(xSize / static_cast<float>(xTileSize));
375 int const yNumTiles =
std::ceil(ySize / static_cast<float>(yTileSize));
376 for (
int iTile = 0, yTile = 0; yTile < yNumTiles; ++yTile) {
377 int const yStart = yTile * yTileSize;
378 int const yStop =
std::min(yStart + yTileSize, ySize);
379 for (
int xTile = 0; xTile < xNumTiles; ++xTile, ++iTile) {
380 int const xStart = xTile * xTileSize;
381 int const xStop =
std::min(xStart + xTileSize, xSize);
383 for (
int y = yStart;
y < yStop; ++
y) {
384 auto iter = out.begin() +
y * xSize + xStart;
385 for (
int x = xStart;
x < xStop; ++
x, ++iter) {
386 *iter =
static_cast<T
>(getNext());
396 void reseed() { _index =
static_cast<int>(
fits_rand_value[_start] * 500); }
405 template <
typename T>
407 bool forceNonfiniteRemoval,
bool fuzz,
408 ndarray::Array<long, 1>
const& tiles,
413 "Floating-point images may not be converted to different floating-point types");
417 "Scaling may not be applied to floating-point images");
423 if (!forceNonfiniteRemoval) {
428 ndarray::Array<T, 1, 1> out = ndarray::allocate(image.getNumElements());
429 auto outIter = out.begin();
430 auto const& flatImage = ndarray::flatten<1>(
image);
431 for (
auto inIter = flatImage.begin(); inIter != flatImage.end(); ++inIter, ++outIter) {
451 ndarray::Array<double, 1, 1> out;
453 if (tiles.isEmpty()) {
455 "Tile sizes must be provided if fuzzing is desired");
457 out = CfitsioRandom(seed).forImage<
double>(image.getShape(), tiles);
459 out = ndarray::allocate(num);
461 auto outIter = out.begin();
462 auto const& flatImage = ndarray::flatten<1>(
image);
463 for (
auto inIter = flatImage.begin(); inIter != flatImage.end(); ++inIter, ++outIter) {
464 double value = (*inIter -
bzero) * scale;
476 *outIter = (value < min ? blank : (value > max ? blank :
std::floor(value)));
481 template <
typename T>
483 ndarray::Array<T, 2, 2> memory = ndarray::allocate(image.getShape());
489 #define INSTANTIATE(TYPE) \ 490 template ImageScale ImageScalingOptions::determine<TYPE, 2>( \ 491 ndarray::Array<TYPE const, 2, 2> const& image, ndarray::Array<bool, 2, 2> const& mask) const; \ 492 template std::shared_ptr<detail::PixelArrayBase> ImageScale::toFits<TYPE>( \ 493 ndarray::Array<TYPE const, 2, 2> const&, bool, bool, ndarray::Array<long, 1> const&, int) const; \ 494 template ndarray::Array<TYPE, 2, 2> ImageScale::fromFits<TYPE>(ndarray::Array<TYPE, 2, 2> const&) const; int const N_RESERVED_VALUES
ImageScalingOptions()
Default Ctor.
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.
Scale to preserve dynamic range.
int compressionAlgorithmToCfitsio(ImageCompressionOptions::CompressionAlgorithm algorithm)
Convert ImageCompressionOptions::CompressionAlgorithm to cfitsio.
def scale(algorithm, min, max=None, frame=None)
#define INSTANTIATE(TYPE)
CompressionAlgorithm
Compression algorithms.
FITS BITPIX header value by C++ type.
int seed
Seed for random number generator when fuzzing.
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.
A base class for image defects.
float quantizeLevel
Divisor of the standard deviation for STDEV_* scaling.
ndarray::Array< T, 2, 2 > fromFits(ndarray::Array< T, 2, 2 > const &image) const
Convert to an array.
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromString(std::string const &name)
Interpret compression algorithm expressed in string.
double bscale
Manually specified BSCALE (for MANUAL scaling)
std::string scalingAlgorithmToString(ImageScalingOptions::ScalingAlgorithm algorithm)
Provide string version of compression algorithm.
Scaling zero-point, set according to pixel type.
std::string compressionAlgorithmToString(ImageCompressionOptions::CompressionAlgorithm algorithm)
Provide string version of compression algorithm.
#define LSST_EXCEPT(type,...)
Standard GZIP compression.
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 BZERO (for MANUAL scaling)
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)
bool fuzz
Fuzz the values when quantising floating-point values?
Scale based on the standard deviation. dynamic range positive.
ImageScalingOptions::ScalingAlgorithm scalingAlgorithmFromString(std::string const &name)
Interpret scaling algorithm expressed in string.
GZIP compression with shuffle (most-significant byte first)
std::shared_ptr< detail::PixelArrayBase > toFits(ndarray::Array< T const, 2, 2 > const &image, bool forceNonfiniteRemoval, bool fuzz=true, ndarray::Array< long, 1 > const &tiles=ndarray::Array< long, 1, 1 >(), int seed=1) const
Convert to an array of pixel values to write to FITS.