USGS

Isis 3.0 Developer's Reference (API)

Home

SpecialPixel.h

Go to the documentation of this file.
00001 #ifndef SpecialPixel_h
00002 #define SpecialPixel_h
00003 
00024 #include <vector>
00025 #include <cmath>
00026 #include <cfloat>
00027 #include <QString>
00028 #include <QStringList>
00029 
00030 #include "IException.h"
00031 #include "IString.h"
00032 
00033 namespace Isis {
00034 
00084 
00085   //  Set up special kludge for double precision initialization
00086   typedef union {
00087     unsigned int i[2];
00088     double d;
00089   } DBL_UNION;
00090 
00091 #if ISIS_LITTLE_ENDIAN
00092 #  define DBL_INIT(a,b)  {(b), (a)}
00093 #else
00094 #  define DBL_INIT(a,b)  {(a), (b)}
00095 #endif
00097 
00098   // Define 8-byte special pixel values for IEEE floating point
00099   const DBL_UNION IVALID_MIN8 = { DBL_INIT(0xFFEFFFFF, 0xFFFFFFFA) };
00100   const double VALID_MIN8     = IVALID_MIN8.d;
00101   const double ValidMinimum   = IVALID_MIN8.d; 
00107   const DBL_UNION INULL8 = { DBL_INIT(0xFFEFFFFF, 0xFFFFFFFB) };
00108   const double NULL8     = INULL8.d;
00109   const double Null      = INULL8.d;  
00110 
00111   const DBL_UNION ILOW_REPR_SAT8 = { DBL_INIT(0xFFEFFFFF, 0xFFFFFFFC) };
00112   const double LOW_REPR_SAT8     = ILOW_REPR_SAT8.d;
00113   const double Lrs               = ILOW_REPR_SAT8.d;  
00117   const DBL_UNION ILOW_INSTR_SAT8 = { DBL_INIT(0xFFEFFFFF, 0xFFFFFFFD) };
00118   const double LOW_INSTR_SAT8     = ILOW_INSTR_SAT8.d;
00119   const double Lis                = ILOW_INSTR_SAT8.d;  
00123   const DBL_UNION IHIGH_INSTR_SAT8 = { DBL_INIT(0xFFEFFFFF, 0xFFFFFFFE) };
00124   const double HIGH_INSTR_SAT8     = IHIGH_INSTR_SAT8.d;
00125   const double His                 = IHIGH_INSTR_SAT8.d; 
00129   const DBL_UNION IHIGH_REPR_SAT8  = { DBL_INIT(0xFFEFFFFF, 0xFFFFFFFF) };
00130   const double HIGH_REPR_SAT8      = IHIGH_REPR_SAT8.d;
00131   const double Hrs                 = IHIGH_REPR_SAT8.d;  
00135   const double VALID_MAX8   = DBL_MAX;
00136   const double ValidMaximum = DBL_MAX;  
00140 #undef DBL_INIT
00141 
00142   // Define 4-byte special pixel values for IEEE floating point
00143   const int  IVALID_MIN4 = 0xFF7FFFFA;
00144   const float VALID_MIN4 = (*((const float *) &IVALID_MIN4));
00145 
00146   const int  INULL4 = 0xFF7FFFFB;
00147   const float NULL4 = (*((const float *) &INULL4));
00148 
00149   const int  ILOW_REPR_SAT4 = 0xFF7FFFFC;
00150   const float LOW_REPR_SAT4 = (*((const float *) &ILOW_REPR_SAT4));
00151 
00152   const int  ILOW_INSTR_SAT4 = 0xFF7FFFFD;
00153   const float LOW_INSTR_SAT4 = (*((const float *) &ILOW_INSTR_SAT4));
00154 
00155   const int  IHIGH_INSTR_SAT4 = 0xFF7FFFFE;
00156   const float HIGH_INSTR_SAT4 = (*((const float *) &IHIGH_INSTR_SAT4));
00157 
00158   const int  IHIGH_REPR_SAT4 = 0xFF7FFFFF;
00159   const float HIGH_REPR_SAT4 = (*((const float *) &IHIGH_REPR_SAT4));
00160 
00161   const float VALID_MAX4 = FLT_MAX;
00162   const int IVALID_MAX4  = (*((const int *) &VALID_MAX4));
00163 
00164   // 2-byte signed special pixel values
00165   const short VALID_MIN2     = ((short)(-32752));
00166   const short NULL2          = ((short)(-32768));
00167   const short LOW_REPR_SAT2  = ((short)(-32767));
00168   const short LOW_INSTR_SAT2 = ((short)(-32766));
00169   const short HIGH_INSTR_SAT2 = ((short)(-32765));
00170   const short HIGH_REPR_SAT2  = ((short)(-32764));
00171   const short VALID_MAX2      = ((short)   32767);
00172 
00173   // 2-byte unsigned special pixel values
00174   const unsigned short VALID_MINU2      = ((unsigned short)       3);
00175   const unsigned short NULLU2           = ((unsigned short)       0);
00176   const unsigned short LOW_REPR_SATU2   = ((unsigned short)       1);
00177   const unsigned short LOW_INSTR_SATU2  = ((unsigned short)       2);
00178   const unsigned short HIGH_INSTR_SATU2 = ((unsigned short)   65534);
00179   const unsigned short HIGH_REPR_SATU2  = ((unsigned short)   65535);
00180   const unsigned short VALID_MAXU2      = ((unsigned short)   65522);
00181 
00182   // 1-byte special pixel values
00183   const unsigned char VALID_MIN1      = ((unsigned char) 1);
00184   const unsigned char NULL1           = ((unsigned char) 0);
00185   const unsigned char LOW_REPR_SAT1   = ((unsigned char) 0);
00186   const unsigned char LOW_INSTR_SAT1  = ((unsigned char) 0);
00187   const unsigned char HIGH_INSTR_SAT1 = ((unsigned char) 255);
00188   const unsigned char HIGH_REPR_SAT1  = ((unsigned char) 255);
00189   const unsigned char VALID_MAX1      = ((unsigned char) 254);
00190 
00199   inline bool IsSpecial(const double d) {
00200     return (d < VALID_MIN8);
00201   }
00202 
00203 
00213   inline bool IsSpecial(const float f) {
00214     return (f < VALID_MIN4);
00215   }
00216 
00217 
00225   inline bool IsValidPixel(const double d) {
00226     return (d >= VALID_MIN8);
00227   }
00228 
00229 
00237   inline bool IsNullPixel(const double d) {
00238     return (d == NULL8);
00239   }
00240 
00241 
00249   inline bool IsHighPixel(const double d) {
00250     return (d == HIGH_REPR_SAT8) || (d == HIGH_INSTR_SAT8);
00251   }
00252 
00253 
00261   inline bool IsLowPixel(const double d) {
00262     return (d == LOW_REPR_SAT8) || (d == LOW_INSTR_SAT8);
00263   }
00264 
00265 
00273   inline bool IsHrsPixel(const double d) {
00274     return (d == HIGH_REPR_SAT8);
00275   }
00276 
00277 
00285   inline bool IsHisPixel(const double d) {
00286     return (d == HIGH_INSTR_SAT8);
00287   }
00288 
00289 
00297   inline bool IsLisPixel(const double d) {
00298     return (d == LOW_INSTR_SAT8);
00299   }
00300 
00301 
00309   inline bool IsLrsPixel(const double d) {
00310     return (d == LOW_REPR_SAT8);
00311   }
00312 
00313 
00321   inline double TestPixel(const float t) {
00322     if(t < VALID_MIN4) {
00323       if(t == NULL4) return (NULL8);
00324       if(t == LOW_REPR_SAT4) return (LOW_REPR_SAT8);
00325       if(t == LOW_INSTR_SAT4) return (LOW_INSTR_SAT8);
00326       if(t == HIGH_REPR_SAT4) return (HIGH_REPR_SAT8);
00327       if(t == HIGH_INSTR_SAT4) return (HIGH_INSTR_SAT8);
00328       return (LOW_REPR_SAT8);
00329     }
00330     else if(t > VALID_MAX4) {
00331       return (HIGH_REPR_SAT8);
00332     }
00333     else {
00334       return ((double) t);
00335     }
00336   }
00337 
00338 
00347   inline float TestPixel(const double t) {
00348     if(t < (double) VALID_MIN4) {
00349       if(t == NULL8) return (NULL4);
00350       if(t == LOW_REPR_SAT8) return (LOW_REPR_SAT4);
00351       if(t == LOW_INSTR_SAT8) return (LOW_INSTR_SAT4);
00352       if(t == HIGH_INSTR_SAT8) return (HIGH_INSTR_SAT4);
00353       if(t == HIGH_REPR_SAT8) return (HIGH_REPR_SAT4);
00354       return (LOW_REPR_SAT4);
00355     }
00356     else if(t > (double) VALID_MAX4) {
00357       return (HIGH_REPR_SAT8);
00358     }
00359     else {
00360       return ((float) t);
00361     }
00362   }
00363 
00364 
00373   inline QString PixelToString(double d) {
00374     if(Isis::IsSpecial(d)) {
00375       if(Isis::IsNullPixel(d)) return "Null";
00376       if(Isis::IsLrsPixel(d)) return "Lrs";
00377       if(Isis::IsHrsPixel(d)) return "Hrs";
00378       if(Isis::IsHisPixel(d)) return "His";
00379       if(Isis::IsLisPixel(d)) return "Lis";
00380       return "Invalid";
00381     }
00382 
00383     QString result;
00384     return result.setNum(d, 'g', 8);
00385   }
00386 
00387 
00396   inline double StringToPixel(const QString &str) {
00397 
00398     QString s = str.toUpper();
00399 
00400     QStringList legal;
00401     legal.push_back("NULL");
00402     legal.push_back("HRS");
00403     legal.push_back("LRS");
00404     legal.push_back("HIS");
00405     legal.push_back("LIS");
00406     int matches = 0;
00407     for(int i = 0; i < (int) legal.size(); i++) {
00408       if(legal[i].mid(0, s.size()) == s) {
00409         matches++;
00410       }
00411     }
00412     if(matches > 1) {
00413       QString msg = "Input [" + str + "] is not a unique abbreviation. Use " + s + "I or " + s + "R.";
00414       throw IException(IException::User, msg, _FILEINFO_);
00415     }
00416     if(matches == 0) {
00417       try {
00418         return toDouble(s);
00419       }
00420       catch(IException &e) {
00421         QString msg = "Input [" + str + "] does not appear to be a legal special pixel abbreviation or double value.";
00422         throw IException(e, IException::User, msg, _FILEINFO_);
00423       }
00424     }
00425     if(s[0] == 'N') return Null;
00426     if(s.mid(0, 2) == "HR") return Hrs;
00427     if(s.mid(0, 2) == "LR") return Lrs;
00428     if(s.mid(0, 2) == "HI") return His;
00429     else  return Lis;//(s.substr(0,2) == "LI")
00430 
00431   }
00432 }
00433 
00434 #endif
00435