lsst.afw  13.0-98-g5a654715e+2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fitsCompression.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 #include "fitsio.h"
4 extern "C" {
5 #include "fitsio2.h"
6 }
7 
8 #include "lsst/pex/exceptions.h"
9 #include "lsst/afw/math/Random.h"
10 
12 
13 extern float* fits_rand_value; // Random numbers, defined in cfitsio
14 int const N_RESERVED_VALUES = 10; // Number of reserved values for float --> bitpix=32 conversions (cfitsio)
15 
16 namespace lsst {
17 namespace afw {
18 namespace fits {
19 
21 {
22  if (name == "NONE") return ImageCompressionOptions::NONE;
23  if (name == "GZIP") return ImageCompressionOptions::GZIP;
24  if (name == "GZIP_SHUFFLE") return ImageCompressionOptions::GZIP_SHUFFLE;
25  if (name == "RICE") return ImageCompressionOptions::RICE;
26  if (name == "HCOMPRESS") throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
27  "HCOMPRESS is unsupported");
28  if (name == "PLIO") return ImageCompressionOptions::PLIO;
29  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, "Unrecognised compression algorithm: " + name);
30 }
31 
33 {
34  switch (algorithm) {
35  case ImageCompressionOptions::NONE: return "NONE";
36  case ImageCompressionOptions::GZIP: return "GZIP";
37  case ImageCompressionOptions::GZIP_SHUFFLE: return "GZIP_SHUFFLE";
38  case ImageCompressionOptions::RICE: return "RICE";
39  case ImageCompressionOptions::PLIO: return "PLIO";
40  default:
41  std::ostringstream os;
42  os << "Unrecognized compression algorithm: " << algorithm;
43  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, os.str());
44  }
45 }
46 
48 {
49  switch (cfitsio) {
50  case 0: return ImageCompressionOptions::NONE;
51  case RICE_1: return ImageCompressionOptions::RICE;
52  case GZIP_1: return ImageCompressionOptions::GZIP;
53  case GZIP_2: return ImageCompressionOptions::GZIP_SHUFFLE;
54  case PLIO_1: return ImageCompressionOptions::PLIO;
55  case HCOMPRESS_1: throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
56  "Unsupported compression algorithm: HCOMPRESS_1");
57  default:
58  std::ostringstream os;
59  os << "Unrecognized cfitsio compression: " << cfitsio;
60  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, os.str());
61  }
62 }
63 
65 {
66  switch (algorithm) {
67  case ImageCompressionOptions::NONE: return 0;
68  case ImageCompressionOptions::GZIP: return GZIP_1;
69  case ImageCompressionOptions::GZIP_SHUFFLE: return GZIP_2;
70  case ImageCompressionOptions::RICE: return RICE_1;
71  case ImageCompressionOptions::PLIO: return PLIO_1;
72  default:
73  std::ostringstream os;
74  os << "Unrecognized compression algorithm: " << algorithm;
75  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, os.str());
76  }
77 }
78 
79 
82  int rows,
83  float quantizeLevel_
84 ) : algorithm(algorithm_), tiles(ndarray::allocate(MAX_COMPRESS_DIM)), quantizeLevel(quantizeLevel_) {
85  tiles[0] = 0;
86  tiles[1] = rows;
87  for (int ii = 2; ii < MAX_COMPRESS_DIM; ++ii) tiles[ii] = 1;
88 }
89 
90 
92 {
93  if (name == "NONE") return ImageScalingOptions::NONE;
94  if (name == "RANGE") return ImageScalingOptions::RANGE;
95  if (name == "STDEV_POSITIVE") return ImageScalingOptions::STDEV_POSITIVE;
96  if (name == "STDEV_NEGATIVE") return ImageScalingOptions::STDEV_NEGATIVE;
97  if (name == "STDEV_BOTH") return ImageScalingOptions::STDEV_BOTH;
98  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, "Unrecognized scaling algorithm: " + name);
99 }
100 
101 
103 {
104  switch (algorithm) {
105  case ImageScalingOptions::NONE: return "NONE";
106  case ImageScalingOptions::RANGE: return "RANGE";
107  case ImageScalingOptions::STDEV_POSITIVE: return "STDEV_POSITIVE";
108  case ImageScalingOptions::STDEV_NEGATIVE: return "STDEV_NEGATIVE";
109  case ImageScalingOptions::STDEV_BOTH: return "STDEV_BOTH";
110  default:
111  std::ostringstream os;
112  os << "Unrecognized scaling algorithm: " << algorithm;
113  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, os.str());
114  }
115 }
116 
117 
119  ScalingAlgorithm algorithm_,
120  int bitpix_,
121  std::vector<std::string> const& maskPlanes_,
122  int seed_,
123  float quantizeLevel_,
124  float quantizePad_,
125  bool fuzz_,
126  double bscale_,
127  double bzero_
128 ) : algorithm(algorithm_), bitpix(bitpix_), fuzz(fuzz_),
129  seed(std::abs(seed_ - 1) % (N_RANDOM - 1) + 1), // zero is bad (cfitsio uses non-deterministic method)
130  maskPlanes(maskPlanes_),
131  quantizeLevel(quantizeLevel_), quantizePad(quantizePad_),
132  bscale(bscale_), bzero(bzero_) {}
133 
134 
135 namespace {
136 
138 template <typename T, int N>
139 std::pair<T, T> calculateMedianStdev(
140  ndarray::Array<T const, N, N> const& image,
141  ndarray::Array<bool, N, N> const& mask
142 ) {
143  std::size_t num = 0;
144  auto const& flatMask = ndarray::flatten<1>(mask);
145  for (auto mm = flatMask.begin(); mm != flatMask.end(); ++mm) {
146  if (!*mm) ++num;
147  }
148  ndarray::Array<T, 1, 1> array = ndarray::allocate(num);
149  auto const& flatImage = ndarray::flatten<1>(image);
150  auto mm = ndarray::flatten<1>(mask).begin();
151  auto aa = array.begin();
152  for (auto ii = flatImage.begin(); ii != flatImage.end(); ++ii, ++mm) {
153  if (*mm) continue;
154  *aa = *ii;
155  ++aa;
156  }
157 
158  // Quartiles; from https://stackoverflow.com/a/11965377/834250
159  auto const q1 = num/4;
160  auto const q2 = num/2;
161  auto const q3 = q1 + q2;
162  std::nth_element(array.begin(), array.begin() + q1, array.end());
163  std::nth_element(array.begin() + q1 + 1, array.begin() + q2, array.end());
164  std::nth_element(array.begin() + q2 + 1, array.begin() + q3, array.end());
165 
166  T const median = num % 2 ? array[num/2] : 0.5*(array[num/2] + array[num/2 - 1]);
167  // No, we're not doing any interpolation for the lower and upper quartiles.
168  // We're estimating the noise, so it doesn't need to be super precise.
169  T const lq = array[q1];
170  T const uq = array[q3];
171  return std::make_pair(median, 0.741*(uq - lq));
172 }
173 
175 template <typename T, int N>
176 std::pair<T, T> calculateMinMax(
177  ndarray::Array<T const, N, N> const& image,
178  ndarray::Array<bool, N, N> const& mask
179 ) {
181  auto mm = ndarray::flatten<1>(mask).begin();
182  auto const& flatImage = ndarray::flatten<1>(image);
183  for (auto ii = flatImage.begin(); ii != flatImage.end(); ++ii, ++mm) {
184  if (*mm) continue;
185  if (!std::isfinite(*ii)) continue;
186  if (*ii > max) max = *ii;
187  if (*ii < min) min = *ii;
188  }
189  return std::make_pair(min, max);
190 }
191 
192 // Return range of values for target BITPIX
193 template <typename T>
194 double rangeForBitpix(int bitpix, bool cfitsioPadding) {
195  if (bitpix == 0) {
196  bitpix = detail::Bitpix<T>::value;
197  }
198  double range = std::pow(2.0, bitpix) - 1; // Range of values for target BITPIX
199  if (cfitsioPadding) {
200  range -= N_RESERVED_VALUES;
201  }
202  return range;
203 }
204 
205 } // anonymous namespace
206 
207 template <typename T, int N>
208 ImageScale ImageScalingOptions::determineFromRange(
209  ndarray::Array<T const, N, N> const& image,
210  ndarray::Array<bool, N, N> const& mask,
211  bool isUnsigned,
212  bool cfitsioPadding
213 ) const {
214  auto minMax = calculateMinMax(image, mask);
215  T const min = minMax.first;
216  T const max = minMax.second;
217  if (min == max) return ImageScale(bitpix, 1.0, min);
218  double range = rangeForBitpix<T>(bitpix, cfitsioPadding);
219  range -= 2; // To allow for rounding and fuzz at either end
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) {
223  bzero -= bscale*N_RESERVED_VALUES;
224  }
225  bzero -= bscale; // Allow for rounding and fuzz on the low end
226  return ImageScale(bitpix, bscale, bzero);
227 }
228 
229 template <typename T, int N>
230 ImageScale ImageScalingOptions::determineFromStdev(
231  ndarray::Array<T const, N, N> const& image,
232  ndarray::Array<bool, N, N> const& mask,
233  bool isUnsigned,
234  bool cfitsioPadding
235 ) const {
236  auto stats = calculateMedianStdev(image, mask);
237  auto const median = stats.first, stdev = stats.second;
238  double const bscale = static_cast<T>(stdev/quantizeLevel);
239 
241  auto minMax = calculateMinMax(image, mask);
242  T const min = minMax.first;
243  T const max = minMax.second;
244  double range = rangeForBitpix<T>(bitpix, cfitsioPadding); // Range of values for target BITPIX
245  double const numUnique = (max - min)/bscale; // Number of unique values
246 
247  double imageVal; // Value on image
248  long diskVal; // Corresponding quantized value
249  if (numUnique < range) {
250  imageVal = median;
251  diskVal = cfitsioPadding ? 0.5*N_RESERVED_VALUES : 0;
252  } else {
253  switch (algorithm) {
255  // Put (median - N sigma) at the lowest possible value: predominantly positive images
256  imageVal = median - quantizePad * stdev;
257  diskVal = (bitpix == 8) ? 0 : - (1L << (bitpix - 1)); // Lowest value: -2^(bitpix-1)
258  if (cfitsioPadding) diskVal -= N_RESERVED_VALUES;
259  break;
261  // Put (median + N sigma) at the highest possible value: predominantly negative images
262  imageVal = median + quantizePad * stdev;
263  diskVal = (bitpix == 8) ? 255 : (1L << (bitpix - 1)) - 1; // Lowest value: 2^(bitpix-1)-1
264  break;
266  // Put median right in the middle: images with an equal abundance of positive and negative values
267  imageVal = median;
268  diskVal = cfitsioPadding ? 0.5*N_RESERVED_VALUES : 0;
269  break;
270  default:
271  std::abort(); // Programming error: should never get here
272  }
273  }
274 
275  double bzero = static_cast<T>(imageVal - bscale*diskVal);
276  return ImageScale(bitpix, bscale, bzero);
277 }
278 
280 template <typename T, class Enable=void>
281 struct Bzero {
282  static double constexpr value = 0.0;
283 };
284 
285 // uint64 version
286 // 'double' doesn't have sufficient bits to represent the appropriate BZERO,
287 // so let cfitsio handle it.
288 template <>
289 struct Bzero<std::uint64_t> {
290  static double constexpr value = 0.0;
291 };
292 
293 // Unsigned integer version
294 template <typename T>
295 struct Bzero<T, typename std::enable_if<std::numeric_limits<T>::is_integer &&
296  !std::numeric_limits<T>::is_signed>::type> {
297  static double constexpr value = std::numeric_limits<T>::max() >> 1;
298 };
299 
300 
301 template <typename T, int N>
302 ImageScale ImageScalingOptions::determine(
303  ndarray::Array<T const, N, N> const& image,
304  ndarray::Array<bool, N, N> const& mask
305 ) const {
306  if (std::is_integral<T>::value && (bitpix != 0 || bitpix != detail::Bitpix<T>::value) &&
307  algorithm != NONE) {
308  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
309  "Image scaling not supported for integral types");
310  }
311  bool const isUnsigned = bitpix == 8 || (bitpix == 0 && detail::Bitpix<T>::value == 8);
312  bool const cfitsioPadding = !std::numeric_limits<T>::is_integer && bitpix == 32;
313  switch (algorithm) {
314  case NONE: return ImageScale(detail::Bitpix<T>::value, 1.0, Bzero<T>::value);
315  case RANGE: return determineFromRange(image, mask, isUnsigned, cfitsioPadding);
316  case MANUAL: return ImageScale(bitpix, bscale, bzero);
320  return determineFromStdev(image, mask, isUnsigned, cfitsioPadding);
321  default:
322  std::abort(); // should never get here
323  }
324 }
325 
326 namespace {
327 
332 class CfitsioRandom {
333  public:
335  CfitsioRandom(int seed) : _seed(seed) {
336  assert(seed != 0 && seed < N_RANDOM);
337  fits_init_randoms();
338  resetForTile(0);
339  }
340 
342  void resetForTile(int iTile) {
343  _start = (iTile + _seed - 1) % N_RANDOM;
344  reseed();
345  }
346 
348  float getNext() {
349  float const value = fits_rand_value[_index];
350  increment();
351  return value;
352  }
353 
355  void increment() {
356  ++_index;
357  if (_index == N_RANDOM) {
358  ++_start;
359  if (_start == N_RANDOM) {
360  _start = 0;
361  }
362  reseed();
363  }
364  }
365 
367  template <typename T>
368  ndarray::Array<T, 1, 1> forImage(
369  typename ndarray::Array<T const, 2, 2>::Index const& shape,
370  ndarray::Array<long, 1> const& tiles
371  ) {
372  std::size_t const xSize = shape[1], ySize = shape[0];
373  ndarray::Array<T, 1, 1> out = ndarray::allocate(xSize*ySize);
374  std::size_t const xTileSize = tiles[0] <= 0 ? xSize : tiles[0];
375  std::size_t const yTileSize = tiles[1] < 0 ? ySize : (tiles[1] == 0 ? 1 : tiles[1]);
376  int const xNumTiles = std::ceil(xSize/static_cast<float>(xTileSize));
377  int const yNumTiles = std::ceil(ySize/static_cast<float>(yTileSize));
378  for (int iTile = 0, yTile = 0; yTile < yNumTiles; ++yTile) {
379  int const yStart = yTile*yTileSize;
380  int const yStop = std::min(yStart + yTileSize, ySize);
381  for (int xTile = 0; xTile < xNumTiles; ++xTile, ++iTile) {
382  int const xStart = xTile*xTileSize;
383  int const xStop = std::min(xStart + xTileSize, xSize);
384  resetForTile(iTile);
385  for (int y = yStart; y < yStop; ++y) {
386  auto iter = out.begin() + y*xSize + xStart;
387  for (int x = xStart; x < xStop; ++x, ++iter) {
388  *iter = static_cast<T>(getNext());
389  }
390  }
391  }
392  }
393  return out;
394  }
395 
396  private:
398  void reseed() {
399  _index = static_cast<int>(fits_rand_value[_start]*500);
400  }
401 
402  int _seed; // Initial seed
403  int _start; // Starting index for tile; "iseed" in cfitsio
404  int _index; // Index of next value; "nextrand" in cfitsio
405 };
406 
407 } // anonymous namespace
408 
409 
410 template <typename T>
411 std::shared_ptr<detail::PixelArrayBase> ImageScale::toFits(
412  ndarray::Array<T const, 2, 2> const& image,
413  bool forceNonfiniteRemoval,
414  bool fuzz,
415  ndarray::Array<long, 1> const& tiles,
416  int seed
417 ) const {
418  if (!std::numeric_limits<T>::is_integer && bitpix < 0) {
419  if (bitpix != detail::Bitpix<T>::value) {
420  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
421  "Floating-point images may not be converted to different floating-point types");
422  }
423  if (bscale != 1.0 || bzero != 0.0) {
424  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
425  "Scaling may not be applied to floating-point images");
426  }
427  }
428 
429  if (bitpix < 0 || (bitpix == 0 && !std::numeric_limits<T>::is_integer) ||
430  (bscale == 1.0 && bzero == 0.0 && !fuzz)) {
431  if (!forceNonfiniteRemoval) {
432  // Type conversion only
433  return detail::makePixelArray(bitpix, ndarray::Array<T const, 1, 1>(ndarray::flatten<1>(image)));
434  }
435  if (!std::numeric_limits<T>::is_integer) {
436  ndarray::Array<T, 1, 1> out = ndarray::allocate(image.getNumElements());
437  auto outIter = out.begin();
438  auto const& flatImage = ndarray::flatten<1>(image);
439  for (auto inIter = flatImage.begin(); inIter != flatImage.end(); ++inIter, ++outIter) {
440  *outIter = std::isfinite(*inIter) ? *inIter : std::numeric_limits<T>::max();
441  }
442  return detail::makePixelArray(bitpix, out);
443  }
444  // Fall through for explicit scaling
445  }
446 
447  // Note: BITPIX=8 treated differently, since it uses unsigned values; the rest use signed */
448  double min = bitpix == 8 ? 0 : -std::pow(2.0, bitpix - 1);
449  double max = bitpix == 8 ? 255 : (std::pow(2.0, bitpix - 1) - 1.0);
450 
451  if (!std::numeric_limits<T>::is_integer && bitpix == 32) {
452  // cfitsio saves space for N_RESERVED_VALUES=10 values at the low end
453  min += N_RESERVED_VALUES;
454  }
455 
456  double const scale = 1.0/bscale;
457  std::size_t const num = image.getNumElements();
458  bool const applyFuzz = fuzz && !std::numeric_limits<T>::is_integer && bitpix > 0;
459  ndarray::Array<double, 1, 1> out;
460  if (applyFuzz) {
461  if (tiles.isEmpty()) {
462  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
463  "Tile sizes must be provided if fuzzing is desired");
464  }
465  out = CfitsioRandom(seed).forImage<double>(image.getShape(), tiles);
466  } else {
467  out = ndarray::allocate(num);
468  }
469  auto outIter = out.begin();
470  auto const& flatImage = ndarray::flatten<1>(image);
471  for (auto inIter = flatImage.begin(); inIter != flatImage.end(); ++inIter, ++outIter) {
472  double value = (*inIter - bzero)*scale;
473  if (!std::isfinite(value)) {
474  // This choice of "max" for non-finite and overflow pixels is mainly cosmetic --- it has to be
475  // something, and "min" would produce holes in the cores of bright stars.
476  *outIter = blank;
477  continue;
478  }
479  if (applyFuzz) {
480  // Add random factor [0.0,1.0): adds a variance of 1/12,
481  // but preserves the expectation value given the floor()
482  value += *outIter;
483  }
484  *outIter = (value < min ? blank : (value > max ? blank : std::floor(value)));
485  }
486  return detail::makePixelArray(bitpix, out);
487 }
488 
489 
490 template <typename T>
491 ndarray::Array<T, 2, 2> ImageScale::fromFits(ndarray::Array<T, 2, 2> const& image) const {
492  ndarray::Array<T, 2, 2> memory = ndarray::allocate(image.getShape());
493  memory.deep() = bscale*image + bzero;
494  return memory;
495 }
496 
497 
498 // Explicit instantiation
499 #define INSTANTIATE(TYPE) \
500  template ImageScale ImageScalingOptions::determine<TYPE, 2>( \
501  ndarray::Array<TYPE const, 2, 2> const& image, ndarray::Array<bool, 2, 2> const& mask) const; \
502  template std::shared_ptr<detail::PixelArrayBase> ImageScale::toFits<TYPE>( \
503  ndarray::Array<TYPE const, 2, 2> const&, bool, bool, ndarray::Array<long, 1> const&, int) const; \
504  template ndarray::Array<TYPE, 2, 2> ImageScale::fromFits<TYPE>( \
505  ndarray::Array<TYPE, 2, 2> const&) const;
506 
507 INSTANTIATE(std::uint8_t);
508 INSTANTIATE(std::uint16_t);
509 INSTANTIATE(std::int16_t);
510 INSTANTIATE(std::uint32_t);
511 INSTANTIATE(std::int32_t);
512 INSTANTIATE(std::uint64_t);
513 INSTANTIATE(std::int64_t);
514 INSTANTIATE(boost::float32_t);
515 INSTANTIATE(boost::float64_t);
516 
517 }}} // namespace lsst::afw::fits
int const N_RESERVED_VALUES
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.
ndarray::Array< T, 2, 2 > fromFits(ndarray::Array< T, 2, 2 > const &image) const
Convert to an array.
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.
Extent< int, N > ceil(Extent< double, N > const &input)
Return the component-wise ceil (round towards more positive).
#define INSTANTIATE(TYPE)
CompressionAlgorithm
Compression algorithms.
int _seed
FITS BITPIX header value by C++ type.
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.
float quantizeLevel
Divisor of the standard deviation for STDEV_* scaling.
double bzero
Zero-point to apply when reading from FITS.
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromString(std::string const &name)
Interpret compression algorithm expressed in string.
int _start
int max
Definition: BoundedField.cc:99
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.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
double bscale
Scale to apply when reading from FITS.
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.
double x
int _index
static double constexpr value
afw::table::Key< afw::table::Array< MaskPixelT > > mask
int min
Definition: Coord.cc:160
afw::table::Key< afw::table::Array< ImagePixelT > > image
Scale based on the standard deviation, dynamic range negative.
double bzero
Manually specified BSCALE and BZERO (for MANUAL scaling)
float * fits_rand_value
ImageCompressionOptions::CompressionAlgorithm compressionAlgorithmFromCfitsio(int cfitsio)
Convert compression algorithm from cfitsio to ImageCompressionOptions::CompressionAlgorithm.
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)
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)
Scale to apply to image.