USGS

Isis 3.0 Application Source Code Reference

Home

std2isis.cpp

Go to the documentation of this file.
00001 #include "Isis.h"
00002 #include "ProcessByLine.h"
00003 #include "ProcessImport.h"
00004 #include "SpecialPixel.h"
00005 #include "UserInterface.h"
00006 #include "JP2Decoder.h"
00007 #include <QImage>
00008 
00009 using namespace std;
00010 using namespace Isis;
00011 
00012 QImage *qimage;
00013 int line;
00014 int band;
00015 
00016 //Initialize values to make special pixels invalid
00017 double null_min = DBL_MAX;
00018 double null_max = DBL_MIN;
00019 double hrs_min = DBL_MAX;
00020 double hrs_max = DBL_MIN;
00021 double lrs_min = DBL_MAX;
00022 double lrs_max = DBL_MIN;
00023 
00024 void toGrayCube(Buffer &out);
00025 void toRGBCube(Buffer &out);
00026 void toARGBCube(Buffer &out);
00027 
00028 double TestSpecial(const double pixel);
00029 
00030 void IsisMain() {
00031   UserInterface &ui = Application::GetUserInterface();
00032   ProcessByLine p;
00033 
00034 
00035   // Set special pixel ranges
00036   if(ui.GetBoolean("SETNULLRANGE")) {
00037     null_min = ui.GetDouble("NULLMIN");
00038     null_max = ui.GetDouble("NULLMAX");
00039   }
00040   if(ui.GetBoolean("SETHRSRANGE")) {
00041     hrs_min = ui.GetDouble("HRSMIN");
00042     hrs_max = ui.GetDouble("HRSMAX");
00043   }
00044   if(ui.GetBoolean("SETLRSRANGE")) {
00045     lrs_min = ui.GetDouble("LRSMIN");
00046     lrs_max = ui.GetDouble("LRSMAX");
00047   }
00048 
00049   qimage = new QImage(iString(ui.GetFilename("FROM")));
00050 
00051   line = 0;
00052   band = 0;
00053 
00054   // qimage is NULL on failure
00055   if(qimage->isNull()) {
00056     delete qimage;
00057     qimage = NULL;
00058 
00059     // Determine if input file is a JPEG2000 file
00060     try {
00061       JP2Decoder *JP2_decoder;
00062       JP2_decoder = new JP2Decoder(iString(ui.GetFilename("FROM")));
00063       JP2_decoder->OpenFile();
00064       int nsamps = JP2_decoder->GetSampleDimension();
00065       int nlines = JP2_decoder->GetLineDimension();
00066       int nbands = JP2_decoder->GetBandDimension();
00067       int pixelbytes = JP2_decoder->GetPixelBytes();
00068       bool is_signed = JP2_decoder->GetSignedData();
00069       delete JP2_decoder;
00070       ProcessImport jp;
00071       jp.SetDimensions(nsamps, nlines, nbands);
00072       if(pixelbytes == 1) {
00073         jp.SetPixelType(Isis::UnsignedByte);
00074       }
00075       else if(pixelbytes == 2) {
00076         if(is_signed) {
00077           jp.SetPixelType(Isis::SignedWord);
00078         }
00079         else {
00080           jp.SetPixelType(Isis::UnsignedWord);
00081         }
00082       }
00083       else {
00084         throw iException::Message(iException::User,
00085                                   "The file [" + ui.GetFilename("FROM") + "] contains unsupported data type.",
00086                                   _FILEINFO_);
00087       }
00088       jp.SetInputFile(iString(ui.GetFilename("FROM")));
00089       jp.SetOutputCube("TO");
00090       jp.SetOrganization(ProcessImport::JP2);
00091       jp.StartProcess();
00092       jp.EndProcess();
00093     }
00094     catch(Isis::iException &e) {
00095       throw iException::Message(iException::User,
00096                                 "The file [" + ui.GetFilename("FROM") + "] does not contain a recognized image format.",
00097                                 _FILEINFO_);
00098     }
00099   }
00100   else {
00101 
00102     string mode = ui.GetString("MODE");
00103     if(mode == "AUTO") {
00104       if(qimage->isGrayscale())
00105         mode = "GRAYSCALE";
00106       else if(qimage->hasAlphaChannel())
00107         mode = "ARGB";
00108       else
00109         mode = "RGB";
00110     }
00111 
00112     if(mode == "GRAYSCALE") {
00113       Cube *outCube = p.SetOutputCube("TO", qimage->width(), qimage->height());
00114       Pvl *label = outCube->getLabel();
00115       PvlGroup bandBin("BandBin");
00116       PvlKeyword name("Name");
00117       name += "Gray";
00118       bandBin += name;
00119       label->AddGroup(bandBin);
00120       p.StartProcess(toGrayCube);
00121     }
00122     else if(mode == "RGB") {
00123       Cube *outCube = p.SetOutputCube("TO", qimage->width(), qimage->height(), 3);
00124       Pvl *label = outCube->getLabel();
00125       PvlGroup bandBin("BandBin");
00126       PvlKeyword name("Name");
00127       name += "Red";
00128       name += "Green";
00129       name += "Blue";
00130       bandBin += name;
00131       label->AddGroup(bandBin);
00132       p.StartProcess(toRGBCube);
00133     }
00134     else {
00135       Cube *outCube = p.SetOutputCube("TO", qimage->width(), qimage->height(), 4);
00136       Pvl *label = outCube->getLabel();
00137       PvlGroup bandBin("BandBin");
00138       PvlKeyword name("Name");
00139       name += "Red";
00140       name += "Green";
00141       name += "Blue";
00142       name += "Alpha";
00143       bandBin += name;
00144       label->AddGroup(bandBin);
00145       p.StartProcess(toARGBCube);
00146     }
00147 
00148     delete qimage;
00149     qimage = NULL;
00150     p.EndProcess();
00151   }
00152 }
00153 
00154 void toGrayCube(Buffer &out) {
00155   for(int sample = 0; sample < out.SampleDimension(); sample ++) {
00156     double pixel = qGray(qimage->pixel(sample, line));
00157     out[sample] = TestSpecial(pixel);
00158   }
00159 
00160   line ++;
00161 }
00162 
00163 void toRGBCube(Buffer &out) {
00164   if(band == 0) {
00165     for(int sample = 0; sample < out.SampleDimension(); sample ++) {
00166       double pixel = qRed(qimage->pixel(sample, line));
00167       out[sample] = TestSpecial(pixel);
00168     }
00169   }
00170   else if(band == 1) {
00171     for(int sample = 0; sample < out.SampleDimension(); sample ++) {
00172       double pixel = qGreen(qimage->pixel(sample, line));
00173       out[sample] = TestSpecial(pixel);
00174     }
00175   }
00176   else if(band == 2) {
00177     for(int sample = 0; sample < out.SampleDimension(); sample ++) {
00178       double pixel = qBlue(qimage->pixel(sample, line));
00179       out[sample] = TestSpecial(pixel);
00180     }
00181   }
00182   else {
00183     string msg = "RGB cubes must have exactly three bands.";
00184     throw iException::Message(iException::Programmer, msg, _FILEINFO_);
00185   }
00186 
00187   line++;
00188   if(line == qimage->height()) {
00189     line = 0;
00190     band++;
00191   }
00192 }
00193 
00194 void toARGBCube(Buffer &out) {
00195   if(band == 0) {
00196     for(int sample = 0; sample < out.SampleDimension(); sample ++) {
00197       double pixel = qRed(qimage->pixel(sample, line));
00198       out[sample] = TestSpecial(pixel);
00199     }
00200   }
00201   else if(band == 1) {
00202     for(int sample = 0; sample < out.SampleDimension(); sample ++) {
00203       double pixel = qGreen(qimage->pixel(sample, line));
00204       out[sample] = TestSpecial(pixel);
00205     }
00206   }
00207   else if(band == 2) {
00208     for(int sample = 0; sample < out.SampleDimension(); sample ++) {
00209       double pixel = qBlue(qimage->pixel(sample, line));
00210       out[sample] = TestSpecial(pixel);
00211     }
00212   }
00213   else if(band == 3) {
00214     for(int sample = 0; sample < out.SampleDimension(); sample ++) {
00215       double pixel = qAlpha(qimage->pixel(sample, line));
00216       out[sample] = TestSpecial(pixel);
00217     }
00218   }
00219   else {
00220     string msg = "ARGB cubes must have exactly four bands.";
00221     throw iException::Message(iException::Programmer, msg, _FILEINFO_);
00222   }
00223 
00224   line++;
00225   if(line == qimage->height()) {
00226     line = 0;
00227     band++;
00228   }
00229 }
00230 
00231 /**
00232   * Tests the pixel. If it is valid it will return the dn value,
00233   * otherwise it will return the Isis special pixel value that
00234   * corresponds to it
00235   *
00236   * @param pixel The double precision value that represents a
00237   *              pixel.
00238   * @return double  The double precision value representing the
00239   *         pixel will return as a valid dn or changed to an isis
00240   *         special pixel.
00241   */
00242 double TestSpecial(const double pixel) {
00243   if(pixel <= null_max && pixel >= null_min) {
00244     return Isis::NULL8;
00245   }
00246   else if(pixel <= hrs_max && pixel >= hrs_min) {
00247     return Isis::HIGH_REPR_SAT8;
00248   }
00249   else if(pixel <= lrs_max && pixel >= lrs_min) {
00250     return Isis::LOW_REPR_SAT8;
00251   }
00252   else {
00253     return pixel;
00254   }
00255 }