Skip to content

Commit

Permalink
stretch app conversion (#4074)
Browse files Browse the repository at this point in the history
* stretch updates

* pull request changes

* modified setoutputcube to remove seg fault

* merge with dev + fixed bug

Co-authored-by: Kelvin <kelvinrr@icloud.com>
  • Loading branch information
robotprogrammer22 and Kelvin authored Nov 2, 2020
1 parent 10fe7dd commit 16d9240
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 98 deletions.
88 changes: 15 additions & 73 deletions isis/src/base/apps/stretch/main.cpp
Original file line number Diff line number Diff line change
@@ -1,84 +1,26 @@
#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;
Pvl results;
try{
stretch(ui, &results);
}
else {
if(ui.WasEntered("PAIRS"))
pairs = ui.GetString("PAIRS");
}

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

}

// 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 changes: 93 additions & 0 deletions isis/src/base/apps/stretch/stretch_app.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#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 changes: 14 additions & 0 deletions isis/src/base/apps/stretch/stretch_app.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#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
20 changes: 14 additions & 6 deletions isis/src/base/objs/Process/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}

/**
Expand All @@ -300,16 +300,24 @@ 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;
message << "Invalid cube size specifications [ns=" << ns << ",nl=" << nl
<< ",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);
}

Expand Down Expand Up @@ -408,7 +416,7 @@ namespace Isis {

// Allocate the cube
cube->create(fname);

// Transfer labels from the first input cube
if((p_propagateLabels) && (InputCubes.size() > 0)) {
Isis::PvlObject &incube =
Expand Down
4 changes: 2 additions & 2 deletions isis/src/base/objs/Process/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 8 additions & 13 deletions isis/src/base/objs/ProcessImport/ProcessImport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1273,26 +1273,21 @@ namespace Isis {
*
* @throws Isis::iException::Message "Unsupported pixel type."
*/
Isis::Cube *ProcessImport::SetOutputCube(const QString &parameter) {
CubeAttributeOutput &att =
Application::GetUserInterface().GetOutputAttribute(parameter);
Isis::Cube *ProcessImport::SetOutputCube(const QString &parameter, UserInterface *ui) {
CubeAttributeOutput att;
if (!ui) {
att = Application::GetUserInterface().GetOutputAttribute(parameter);
}
else {
att = ui->GetOutputAttribute(parameter);
}

SetAttributes(att);

return Process::SetOutputCube(Application::GetUserInterface().GetFileName(parameter), att, p_ns, p_nl, p_nb);
}


/**
* Create the output file.
*
*/
Isis::Cube *ProcessImport::SetOutputCube(const QString &parameter, UserInterface &ui){
CubeAttributeOutput &att = ui.GetOutputAttribute(parameter);
SetAttributes(att);
return Isis::Process::SetOutputCube(ui.GetFileName(parameter), att, p_ns, p_nl, p_nb);
}

/**
* Create the output file. Note that all the appropiate calls to at least
* SetDimensions should be invoked prior to calling this method.
Expand Down
3 changes: 1 addition & 2 deletions isis/src/base/objs/ProcessImport/ProcessImport.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ namespace Isis {
// ProcessImport objects and child objects

using Isis::Process::SetOutputCube; // make parent functions visable
Isis::Cube *SetOutputCube(const QString &parameter);
Isis::Cube *SetOutputCube(const QString &parameter, UserInterface &ui);
Isis::Cube *SetOutputCube(const QString &parameter, UserInterface *ui=nullptr);
virtual Isis::Cube *SetOutputCube(const QString &fname,
Isis::CubeAttributeOutput &att);

Expand Down
4 changes: 2 additions & 2 deletions isis/src/newhorizons/apps/leisa2isis/leisa2isis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ namespace Isis {
PvlGroup extensionLabel = importFits.fitsImageLabel(5);
importFits.SetOrganization(ProcessImport::BIL);
importFits.setProcessFileStructure(5);
Cube *output = importFits.SetOutputCube("ERRORMAP", ui);
Cube *output = importFits.SetOutputCube("ERRORMAP", &ui);

// Save the input FITS label in the Cube original labels
Pvl origLabel;
Expand All @@ -269,7 +269,7 @@ namespace Isis {
PvlGroup extensionLabel = importFits.fitsImageLabel(6);
importFits.SetOrganization(ProcessImport::BIL);
importFits.setProcessFileStructure(6);
Cube *output = importFits.SetOutputCube("QUALITY", ui);
Cube *output = importFits.SetOutputCube("QUALITY", &ui);

// Save the input FITS label in the Cube original labels
Pvl origLabel;
Expand Down

0 comments on commit 16d9240

Please sign in to comment.