12 #include "Histogram.h"
14 #include "SpecialPixel.h"
15 #include "IException.h"
27 p_lis = Isis::LOW_INSTR_SAT8;
28 p_lrs = Isis::LOW_REPR_SAT8;
29 p_his = Isis::HIGH_INSTR_SAT8;
30 p_hrs = Isis::HIGH_REPR_SAT8;
48 void Stretch::AddPair(
const double input,
const double output) {
50 if(input <= p_input[p_pairs-1]) {
51 string msg =
"Input pairs must be in ascending order";
52 throw IException(IException::Programmer, msg, _FILEINFO_);
56 p_input.push_back(input);
57 p_output.push_back(output);
69 double Stretch::Map(
const double value)
const {
80 if(p_input.size() == 0)
return value;
83 if(value < p_input[0]) {
94 if(value > p_input[p_pairs-1]) {
106 if(value == p_input[0])
return p_output[0];
107 if(value == p_input[p_pairs-1])
return p_output[p_pairs-1];
111 int end = p_pairs - 1;
112 while(start != end) {
113 int middle = (start + end) / 2;
115 if(middle == start) {
118 else if(value < p_input[middle]) {
129 double slope = (p_output[end] - p_output[start]) / (p_input[end] - p_input[start]);
130 return slope * (value - p_input[end]) + p_output[end];
145 std::pair<double, double> Stretch::NextPair(QString &pairs) {
146 std::pair<double, double> io;
150 pairs = pairs.simplified().trimmed();
152 if (pairs.contains(
":")) {
155 QString firstPair = pairList.takeFirst();
157 QStringList firstPairValues = firstPair.split(
":");
159 if (firstPairValues.count() == 2) {
160 io.first =
toDouble(firstPairValues.first());
161 io.second =
toDouble(firstPairValues.last());
163 pairs = pairList.join(
" ");
181 void Stretch::Parse(
const QString &pairs) {
187 std::pair<double, double> pear;
189 QString p = pairs.simplified().trimmed();
190 p.replace(QRegExp(
"[\\s]*:"),
":");
191 p.replace(QRegExp(
":[\\s]*"),
":");
192 QStringList pairList = p.split(
" ", QString::SkipEmptyParts);
195 foreach(QString singlePair, pairList) {
196 pear = Stretch::NextPair(singlePair);
197 Stretch::AddPair(pear.first, pear.second);
201 throw IException(e, IException::User,
"Invalid stretch pairs [" + pairs +
"]", _FILEINFO_);
225 std::pair<double, double> pear;
229 std::vector<double> converted;
232 while(p.size() > 0) {
233 pear = Stretch::NextPair(p);
234 pear.first = hist->
Percent(pear.first);
239 bool collision =
false;
241 while(!collision && k < converted.size()) {
242 if(pear.first == converted[k]) {
250 Stretch::AddPair(pear.first, pear.second);
251 converted.push_back(pear.first);
257 throw IException(e, IException::User,
"Invalid stretch pairs [" +
258 pairs +
"]", _FILEINFO_);
268 QString Stretch::Text()
const {
270 if(p_pairs < 0)
return "";
273 for(
int i = 0; i < p_pairs; i++) {
287 double Stretch::Input(
int index)
const {
288 if(index >= p_pairs || index < 0) {
291 return p_input[index];
302 double Stretch::Output(
int index)
const {
303 if(index >= p_pairs || index < 0) {
306 return p_output[index];
323 void Stretch::Load(QString &file, QString &grpName) {
347 if(inputs.
size() != outputs.
size()) {
348 QString msg =
"Invalid Pvl file: The number of Input values must equal the number of Output values";
349 throw IException(IException::User, msg, _FILEINFO_);
351 for(
int i = 0; i < inputs.
size(); i++) {
367 void Stretch::Save(QString &file, QString &grpName) {
373 void Stretch::Save(
Isis::Pvl &pvl, QString &grpName) {
377 for(
int i = 0; i < Pairs(); i++) {
378 inputs.addValue(
toString(Input(i)));
379 outputs.addValue(
toString(Output(i)));
392 void Stretch::CopyPairs(
const Stretch &other) {