Unverified Commit 16d92405 authored by robotprogrammer22's avatar robotprogrammer22 Committed by GitHub
Browse files

stretch app conversion (#4074)



* stretch updates

* pull request changes

* modified setoutputcube to remove seg fault

* merge with dev + fixed bug

Co-authored-by: default avatarKelvin <kelvinrr@icloud.com>
parent 10fe7dd5
Loading
Loading
Loading
Loading
+15 −73
Original line number Diff line number Diff line
#include "Isis.h"
#include "TextFile.h"
#include "Statistics.h"
#include "ProcessByLine.h"
#include "SpecialPixel.h"
#include "Stretch.h"
#include "PvlGroup.h"
#include "PvlKeyword.h"

#include "Application.h"
#include "UserInterface.h"

#include "stretch_app.h"

using namespace std;
using namespace Isis;

void stretch(Buffer &in, Buffer &out);
Stretch str;
Statistics stats;

void IsisMain() {
  ProcessByLine p;
  Cube *inCube = p.SetInputCube("FROM");

  UserInterface &ui = Application::GetUserInterface();

  QString pairs;

  // first just get the pairs from where ever and worry about
  // whether they are dn values or %'s later
  if(ui.GetBoolean("READFILE")) {
    FileName pairsFileName = ui.GetFileName("INPUTFILE");
    TextFile pairsFile;
    pairsFile.SetComment("#");
    pairsFile.Open(pairsFileName.expanded());

    // concat all non-comment lines into one string (pairs)
    QString line = "";
    while(pairsFile.GetLine(line, true)) {
      pairs += " " + line;
    }
    pairs += line;
  }
  else {
    if(ui.WasEntered("PAIRS"))
      pairs = ui.GetString("PAIRS");
  Pvl results;
  try{
      stretch(ui, &results);
  }

  if(ui.GetBoolean("USEPERCENTAGES")) {
    str.Parse(pairs, inCube->histogram());
  catch(...){
      for (int resultIndex = 0; resultIndex < results.groups(); resultIndex++) {
          Application::Log(results.group(resultIndex));
      }
  else
    str.Parse(pairs);

  // Setup new mappings for special pixels if necessary
  if(ui.WasEntered("NULL"))
    str.SetNull(StringToPixel(ui.GetString("NULL")));
  if(ui.WasEntered("LIS"))
    str.SetLis(StringToPixel(ui.GetString("LIS")));
  if(ui.WasEntered("LRS"))
    str.SetLrs(StringToPixel(ui.GetString("LRS")));
  if(ui.WasEntered("HIS"))
    str.SetHis(StringToPixel(ui.GetString("HIS")));
  if(ui.WasEntered("HRS"))
    str.SetHrs(StringToPixel(ui.GetString("HRS")));

  p.SetOutputCube("TO");

  // Start the processing
  p.StartProcess(stretch);
  p.EndProcess();

  PvlKeyword dnPairs = PvlKeyword("StretchPairs");
  dnPairs.addValue(str.Text());

  PvlGroup results = PvlGroup("Results");
  results.addKeyword(dnPairs);

  Application::Log(results);

      throw;
  }

// Line processing routine
void stretch(Buffer &in, Buffer &out) {
  for(int i = 0; i < in.size(); i++) {
    out[i] = str.Map(in[i]);
  for (int resultIndex = 0; resultIndex < results.groups(); resultIndex++) {
      Application::Log(results.group(resultIndex));
  }
}
+93 −0
Original line number Diff line number Diff line
#include "TextFile.h"
#include "Statistics.h"
#include "ProcessByLine.h"
#include "SpecialPixel.h"
#include "Stretch.h"
#include "PvlGroup.h"
#include "PvlKeyword.h"

#include "stretch_app.h"

namespace Isis {
  void stretchProcess(Buffer &in, Buffer &out);
  Stretch str;
  Statistics stats;

  void stretch(UserInterface &ui, Pvl *log) {
    Cube *cubeFile = new Cube();
    CubeAttributeInput inAtt = ui.GetInputAttribute("FROM");
    if (inAtt.bands().size() != 0) {
      cubeFile->setVirtualBands(inAtt.bands());
    }
    cubeFile->open(ui.GetFileName("FROM"), "r");
    stretch(cubeFile, ui, log);
  }

  void stretch(Cube *inCube, UserInterface &ui, Pvl *log) {
    ProcessByLine p;
    p.SetInputCube(inCube);

    QString pairs;

    // first just get the pairs from where ever and worry about
    // whether they are dn values or %'s later
    if(ui.GetBoolean("READFILE")) {
      FileName pairsFileName = ui.GetFileName("INPUTFILE");
      TextFile pairsFile;
      pairsFile.SetComment("#");
      pairsFile.Open(pairsFileName.expanded());

      // concat all non-comment lines into one string (pairs)
      QString line = "";
      while(pairsFile.GetLine(line, true)) {
        pairs += " " + line;
      }
      pairs += line;
    }
    else {
      if(ui.WasEntered("PAIRS"))
        pairs = ui.GetString("PAIRS");
    }

    if(ui.GetBoolean("USEPERCENTAGES")) {
      str.Parse(pairs, inCube->histogram());
    }
    else
      str.Parse(pairs);

    // Setup new mappings for special pixels if necessary
    if(ui.WasEntered("NULL"))
      str.SetNull(StringToPixel(ui.GetString("NULL")));
    if(ui.WasEntered("LIS"))
      str.SetLis(StringToPixel(ui.GetString("LIS")));
    if(ui.WasEntered("LRS"))
      str.SetLrs(StringToPixel(ui.GetString("LRS")));
    if(ui.WasEntered("HIS"))
      str.SetHis(StringToPixel(ui.GetString("HIS")));
    if(ui.WasEntered("HRS"))
      str.SetHrs(StringToPixel(ui.GetString("HRS")));

    p.SetOutputCube("TO", &ui);

    // Start the processing
    p.StartProcess(stretchProcess);
    p.EndProcess();

    PvlKeyword dnPairs = PvlKeyword("StretchPairs");
    dnPairs.addValue(str.Text());

    PvlGroup results = PvlGroup("Results");
    results.addKeyword(dnPairs);

    if (log){
      log->addGroup(results);
    }
  }

  // Line processing routine
  void stretchProcess(Buffer &in, Buffer &out) {
    for(int i = 0; i < in.size(); i++) {
      out[i] = str.Map(in[i]);
    }
  }
}
+14 −0
Original line number Diff line number Diff line
#ifndef stretch_app_h
#define stretch_app_h

#include "PvlGroup.h"
#include "PvlKeyword.h"
#include "UserInterface.h"

namespace Isis {
  extern void stretch(UserInterface &ui, Pvl *log=nullptr);

  extern void stretch(Cube *inCube, UserInterface &ui, Pvl *log=nullptr);
}

#endif
+14 −6
Original line number Diff line number Diff line
@@ -263,7 +263,7 @@ namespace Isis {
   *
   * @throws Isis::iException::Message
   */
  Isis::Cube *Process::SetOutputCube(const QString &parameter) {
  Isis::Cube *Process::SetOutputCube(const QString &parameter, UserInterface *ui) {
    // Make sure we have an input cube to get a default size from
    if(InputCubes.size() == 0) {
      QString message = "No input images have been selected ... therefore";
@@ -274,7 +274,7 @@ namespace Isis {
    int nl = InputCubes[0]->lineCount();
    int ns = InputCubes[0]->sampleCount();
    int nb = InputCubes[0]->bandCount();
    return SetOutputCube(parameter, ns, nl, nb);
    return SetOutputCube(parameter, ns, nl, nb, ui);
  }

  /**
@@ -300,7 +300,7 @@ namespace Isis {
   * @throws Isis::iException::Message
   */
  Isis::Cube *Process::SetOutputCube(const QString &parameter, const int ns,
                                     const int nl, const int nb) {
                                     const int nl, const int nb, UserInterface *ui) {
    // Make sure we have good dimensions
    if((ns <= 0) || (nl <= 0) || (nb <= 0)) {
      ostringstream message;
@@ -308,8 +308,16 @@ namespace Isis {
              << ",nb=" << nb << "]";
      throw IException(IException::Programmer, message.str().c_str(), _FILEINFO_);
    }
    QString fname = Application::GetUserInterface().GetFileName(parameter);
    Isis::CubeAttributeOutput &atts = Application::GetUserInterface().GetOutputAttribute(parameter);
    QString fname;
    Isis::CubeAttributeOutput atts;
    if(ui==nullptr){
        fname = Application::GetUserInterface().GetFileName(parameter);
        atts = Application::GetUserInterface().GetOutputAttribute(parameter);
    }
    else{
        fname = ui->GetFileName(parameter);
        atts = ui->GetOutputAttribute(parameter);
    }
    return SetOutputCube(fname, atts, ns, nl, nb);
  }

+2 −2
Original line number Diff line number Diff line
@@ -240,9 +240,9 @@ namespace Isis {
      virtual void SetInputCube(Isis::Cube *inCube);


      virtual Isis::Cube *SetOutputCube(const QString &parameter);
      virtual Isis::Cube *SetOutputCube(const QString &parameter, UserInterface *ui=nullptr);
      virtual Isis::Cube *SetOutputCube(const QString &parameter, const int nsamps,
                                const int nlines, const int nbands = 1);
                                const int nlines, const int nbands = 1, UserInterface *ui=nullptr);
      virtual Isis::Cube *SetOutputCube(const QString &fname,
                                const Isis::CubeAttributeOutput &att,
                                const int nsamps, const int nlines,
Loading