Isis 3 Programmer Reference
SpecialPixel.h
1 #ifndef SpecialPixel_h
2 #define SpecialPixel_h
3 
8 /* SPDX-License-Identifier: CC0-1.0 */
9 #include <vector>
10 #include <cmath>
11 #include <cfloat>
12 #include <QString>
13 #include <QStringList>
14 
15 #include "IException.h"
16 #include "IString.h"
17 
18 namespace Isis {
19 
70  // Set up special kludge for double precision initialization
72  typedef union {
73  unsigned int i[2];
74  double d;
75  } DBL_UNION;
76 
77 #if ISIS_LITTLE_ENDIAN
78 # define DBL_INIT(a,b) {(b), (a)}
79 #else
80 # define DBL_INIT(a,b) {(a), (b)}
81 #endif
82 
84  // Define 8-byte special pixel values for IEEE floating point
85  const DBL_UNION IVALID_MIN8 = { DBL_INIT(0xFFEFFFFF, 0xFFFFFFFA) };
86  const double VALID_MIN8 = IVALID_MIN8.d;
87  const double ValidMinimum = IVALID_MIN8.d;
93  const DBL_UNION INULL8 = { DBL_INIT(0xFFEFFFFF, 0xFFFFFFFB) };
94  const double NULL8 = INULL8.d;
95  const double Null = INULL8.d;
96 
97  const DBL_UNION ILOW_REPR_SAT8 = { DBL_INIT(0xFFEFFFFF, 0xFFFFFFFC) };
98  const double LOW_REPR_SAT8 = ILOW_REPR_SAT8.d;
99  const double Lrs = ILOW_REPR_SAT8.d;
103  const DBL_UNION ILOW_INSTR_SAT8 = { DBL_INIT(0xFFEFFFFF, 0xFFFFFFFD) };
104  const double LOW_INSTR_SAT8 = ILOW_INSTR_SAT8.d;
105  const double Lis = ILOW_INSTR_SAT8.d;
109  const DBL_UNION IHIGH_INSTR_SAT8 = { DBL_INIT(0xFFEFFFFF, 0xFFFFFFFE) };
110  const double HIGH_INSTR_SAT8 = IHIGH_INSTR_SAT8.d;
111  const double His = IHIGH_INSTR_SAT8.d;
115  const DBL_UNION IHIGH_REPR_SAT8 = { DBL_INIT(0xFFEFFFFF, 0xFFFFFFFF) };
116  const double HIGH_REPR_SAT8 = IHIGH_REPR_SAT8.d;
117  const double Hrs = IHIGH_REPR_SAT8.d;
121  const double VALID_MAX8 = DBL_MAX;
122  const double ValidMaximum = DBL_MAX;
126 #undef DBL_INIT
127 
128  // Define 4-byte special pixel values for IEEE floating point
129  const int IVALID_MIN4 = 0xFF7FFFFA;
130  const float VALID_MIN4 = (*((const float *) &IVALID_MIN4));
131 
132 
133  const int INULL4 = 0xFF7FFFFB;
134  const float NULL4 = (*((const float *) &INULL4));
135 
136  const int ILOW_REPR_SAT4 = 0xFF7FFFFC;
137  const float LOW_REPR_SAT4 = (*((const float *) &ILOW_REPR_SAT4));
138 
139  const int ILOW_INSTR_SAT4 = 0xFF7FFFFD;
140  const float LOW_INSTR_SAT4 = (*((const float *) &ILOW_INSTR_SAT4));
141 
142  const int IHIGH_INSTR_SAT4 = 0xFF7FFFFE;
143  const float HIGH_INSTR_SAT4 = (*((const float *) &IHIGH_INSTR_SAT4));
144 
145  const int IHIGH_REPR_SAT4 = 0xFF7FFFFF;
146  const float HIGH_REPR_SAT4 = (*((const float *) &IHIGH_REPR_SAT4));
147 
148 
149  const float VALID_MAX4 = FLT_MAX;
150  const int IVALID_MAX4 = (*((const int *) &VALID_MAX4));
151 
152  // 2-byte signed special pixel values
153  const short VALID_MIN2 = ((short)(-32752));
154  const short NULL2 = ((short)(-32768));
155  const short LOW_REPR_SAT2 = ((short)(-32767));
156  const short LOW_INSTR_SAT2 = ((short)(-32766));
157  const short HIGH_INSTR_SAT2 = ((short)(-32765));
158  const short HIGH_REPR_SAT2 = ((short)(-32764));
159  const short VALID_MAX2 = ((short) 32767);
160 
161  // 2-byte unsigned special pixel values
162  const unsigned short VALID_MINU2 = ((unsigned short) 3);
163  const unsigned short NULLU2 = ((unsigned short) 0);
164  const unsigned short LOW_REPR_SATU2 = ((unsigned short) 1);
165  const unsigned short LOW_INSTR_SATU2 = ((unsigned short) 2);
166  const unsigned short HIGH_INSTR_SATU2 = ((unsigned short) 65534);
167  const unsigned short HIGH_REPR_SATU2 = ((unsigned short) 65535);
168  const unsigned short VALID_MAXU2 = ((unsigned short) 65522);
169 
170  // 4-byte unsigned special pixel values
171  const unsigned int VALID_MINUI4 = ((unsigned int) 3);
172  const unsigned int NULLUI4 = ((unsigned int) 0);
173  const unsigned int LOW_REPR_SATUI4 = ((unsigned int) 1);
174  const unsigned int LOW_INSTR_SATUI4 = ((unsigned int) 2);
175  const unsigned int HIGH_INSTR_SATUI4 = ((unsigned int) 4294967294);
176  const unsigned int HIGH_REPR_SATUI4 = ((unsigned int) 4294967295);
177  const unsigned int VALID_MAXUI4 = ((unsigned int) 4294967282);
178 
179 
180  // 1-byte special pixel values
181  const unsigned char VALID_MIN1 = ((unsigned char) 1);
182  const unsigned char NULL1 = ((unsigned char) 0);
183  const unsigned char LOW_REPR_SAT1 = ((unsigned char) 0);
184  const unsigned char LOW_INSTR_SAT1 = ((unsigned char) 0);
185  const unsigned char HIGH_INSTR_SAT1 = ((unsigned char) 255);
186  const unsigned char HIGH_REPR_SAT1 = ((unsigned char) 255);
187  const unsigned char VALID_MAX1 = ((unsigned char) 254);
188 
197  inline bool IsSpecial(const double d) {
198  return (d < VALID_MIN8);
199  }
200 
201 
211  inline bool IsSpecial(const float f) {
212  return (f < VALID_MIN4);
213  }
214 
215 
223  inline bool IsValidPixel(const double d) {
224  return (d >= VALID_MIN8);
225  }
226 
227 
235  inline bool IsNullPixel(const double d) {
236  return (d == NULL8);
237  }
238 
239 
247  inline bool IsHighPixel(const double d) {
248  return (d == HIGH_REPR_SAT8) || (d == HIGH_INSTR_SAT8);
249  }
250 
251 
259  inline bool IsLowPixel(const double d) {
260  return (d == LOW_REPR_SAT8) || (d == LOW_INSTR_SAT8);
261  }
262 
263 
271  inline bool IsHrsPixel(const double d) {
272  return (d == HIGH_REPR_SAT8);
273  }
274 
275 
283  inline bool IsHisPixel(const double d) {
284  return (d == HIGH_INSTR_SAT8);
285  }
286 
287 
295  inline bool IsLisPixel(const double d) {
296  return (d == LOW_INSTR_SAT8);
297  }
298 
299 
307  inline bool IsLrsPixel(const double d) {
308  return (d == LOW_REPR_SAT8);
309  }
310 
311 
319  inline double TestPixel(const float t) {
320  if(t < VALID_MIN4) {
321  if(t == NULL4) return (NULL8);
322  if(t == LOW_REPR_SAT4) return (LOW_REPR_SAT8);
323  if(t == LOW_INSTR_SAT4) return (LOW_INSTR_SAT8);
324  if(t == HIGH_REPR_SAT4) return (HIGH_REPR_SAT8);
325  if(t == HIGH_INSTR_SAT4) return (HIGH_INSTR_SAT8);
326  return (LOW_REPR_SAT8);
327  }
328  else if(t > VALID_MAX4) {
329  return (HIGH_REPR_SAT8);
330  }
331  else {
332  return ((double) t);
333  }
334  }
335 
336 
345  inline float TestPixel(const double t) {
346  if(t < (double) VALID_MIN4) {
347  if(t == NULL8) return (NULL4);
348  if(t == LOW_REPR_SAT8) return (LOW_REPR_SAT4);
349  if(t == LOW_INSTR_SAT8) return (LOW_INSTR_SAT4);
350  if(t == HIGH_INSTR_SAT8) return (HIGH_INSTR_SAT4);
351  if(t == HIGH_REPR_SAT8) return (HIGH_REPR_SAT4);
352  return (LOW_REPR_SAT4);
353  }
354  else if(t > (double) VALID_MAX4) {
355  return (HIGH_REPR_SAT8);
356  }
357  else {
358  return ((float) t);
359  }
360  }
361 
362 
371  inline QString PixelToString(double d) {
372  if(Isis::IsSpecial(d)) {
373  if(Isis::IsNullPixel(d)) return "Null";
374  if(Isis::IsLrsPixel(d)) return "Lrs";
375  if(Isis::IsHrsPixel(d)) return "Hrs";
376  if(Isis::IsHisPixel(d)) return "His";
377  if(Isis::IsLisPixel(d)) return "Lis";
378  return "Invalid";
379  }
380 
381  QString result;
382  return result.setNum(d, 'g', 8);
383  }
384 
385 
394  inline double StringToPixel(const QString &str) {
395 
396  QString s = str.toUpper();
397 
398  QStringList legal;
399  legal.push_back("NULL");
400  legal.push_back("HRS");
401  legal.push_back("LRS");
402  legal.push_back("HIS");
403  legal.push_back("LIS");
404  int matches = 0;
405  for(int i = 0; i < (int) legal.size(); i++) {
406  if(legal[i].mid(0, s.size()) == s) {
407  matches++;
408  }
409  }
410  if(matches > 1) {
411  QString msg = "Input [" + str + "] is not a unique abbreviation. Use " + s + "I or " + s + "R.";
412  throw IException(IException::User, msg, _FILEINFO_);
413  }
414  if(matches == 0) {
415  try {
416  return toDouble(s);
417  }
418  catch(IException &e) {
419  QString msg = "Input [" + str + "] does not appear to be a legal special pixel abbreviation or double value.";
420  throw IException(e, IException::User, msg, _FILEINFO_);
421  }
422  }
423  if(s[0] == 'N') return Null;
424  if(s.mid(0, 2) == "HR") return Hrs;
425  if(s.mid(0, 2) == "LR") return Lrs;
426  if(s.mid(0, 2) == "HI") return His;
427  else return Lis;//(s.substr(0,2) == "LI")
428 
429  }
430 }
431 
432 #endif
433 
Isis::ValidMaximum
const double ValidMaximum
The maximum valid double value for Isis pixels.
Definition: SpecialPixel.h:122
Isis::PixelToString
QString PixelToString(double d)
Takes a double pixel value and returns the name of the pixel type as a string.
Definition: SpecialPixel.h:371
Isis::His
const double His
Value for an Isis High Instrument Saturation pixel.
Definition: SpecialPixel.h:111
Isis::IsNullPixel
bool IsNullPixel(const double d)
Returns if the input pixel is null.
Definition: SpecialPixel.h:235
Isis::IsHrsPixel
bool IsHrsPixel(const double d)
Returns if the input pixel is high representation saturation.
Definition: SpecialPixel.h:271
QStringList
Isis::IsSpecial
bool IsSpecial(const double d)
Returns if the input pixel is special.
Definition: SpecialPixel.h:197
Isis::Hrs
const double Hrs
Value for an Isis High Representation Saturation pixel.
Definition: SpecialPixel.h:117
Isis::IsHighPixel
bool IsHighPixel(const double d)
Returns if the input pixel is one of the high saturation types.
Definition: SpecialPixel.h:247
Isis::Lrs
const double Lrs
Value for an Isis Low Representation Saturation pixel.
Definition: SpecialPixel.h:99
Isis::TestPixel
double TestPixel(const float t)
Converts float pixels to double pixels with special pixel translations.
Definition: SpecialPixel.h:319
Isis::StringToPixel
double StringToPixel(const QString &str)
Takes the name of the pixel type as a string and returns a double pixel value.
Definition: SpecialPixel.h:394
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::IsLisPixel
bool IsLisPixel(const double d)
Returns if the input pixel is low instrument saturation.
Definition: SpecialPixel.h:295
Isis::IsLrsPixel
bool IsLrsPixel(const double d)
Returns if the input pixel is low representation saturation.
Definition: SpecialPixel.h:307
Isis::Null
const double Null
Value for an Isis Null pixel.
Definition: SpecialPixel.h:95
Isis::IsHisPixel
bool IsHisPixel(const double d)
Returns if the input pixel is high instrument saturation.
Definition: SpecialPixel.h:283
Isis::Lis
const double Lis
Value for an Isis Low Instrument Saturation pixel.
Definition: SpecialPixel.h:105
Isis::toDouble
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition: IString.cpp:149
Isis::ValidMinimum
const double ValidMinimum
The minimum valid double value for Isis pixels.
Definition: SpecialPixel.h:87
Isis::IsValidPixel
bool IsValidPixel(const double d)
Returns if the input pixel is valid.
Definition: SpecialPixel.h:223
Isis::IsLowPixel
bool IsLowPixel(const double d)
Returns if the input pixel is one of the low saturation types.
Definition: SpecialPixel.h:259
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::IException::User
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition: IException.h:126
Isis::DBL_UNION
Manipulate special pixel values.
Definition: SpecialPixel.h:72