Debian Codegraph @

src/imageclasses/jlstretch_lib.cc

raw symbols SCIP
1 /**********************************************************************
2 jlstretch_lib.cc: program to stretch raster images: histogram stretching
3 Author(s): [email protected]
4 Copyright (C) 2016-2020 European Union (Joint Research Centre)
5
6 This file is part of jiplib.
7
8 jiplib is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 jiplib is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with jiplib. If not, see <https://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 #include <string>
22 #include <iostream>
23 #include <memory>
24 #include "base/Optionjl.h"
25 #include "imageclasses/Jim.h"
26 #include "apps/AppFactory.h"
27
28 using namespace std;
29 using namespace app;
30
31 /**
32 * @param app application specific option arguments
33 * @return output stretched raster dataset
34 **/
35 shared_ptr<Jim> Jim::stretch(app::AppFactory& app){
36 try{
37 shared_ptr<Jim> imgWriter=createImg();
38 // imgWriter=this->clone();//create clone to first object, allowing for polymorphism in case of derived Jim objects (not working in Python)
39 stretch(*imgWriter, app);
40 return(imgWriter);
41 }
42 catch(string helpString){
43 cerr << helpString << endl;
44 throw;
45 }
46 }
47
48 /**
49 * @param imgWriter output stretched raster dataset
50 * @param app application specific option arguments
51 **/
52 void Jim::stretch(Jim& imgWriter, app::AppFactory& app){
53 Optionjl<double> nodata_opt("nodata", "nodata", "nodata value(s) (first value will be put in output image)");
54 Optionjl<double> src_min_opt("src_min","src_min","clip source below this minimum value");
55 Optionjl<double> src_max_opt("src_max","src_max","clip source above this maximum value");
56 Optionjl<double> fromValue_opt("dst_min", "dst_min", "minimum value in output image", 0);
57 Optionjl<double> toValue_opt("dst_max", "dst_max", "maximum value in output image", 255);
58 Optionjl<double> minp_opt("cc_min", "cc_min", "cumulative count cut from");
59 Optionjl<double> maxp_opt("cc_max", "cc_max", "cumulative count cut to");
60 Optionjl<int> band_opt("b", "b", "band");
61 Optionjl<bool> equalize_opt("eq", "eq", "Histogram equalization",false);
62 Optionjl<std::string> otype_opt("ot", "otype", "Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image");
63 Optionjl<short> verbose_opt("v", "verbose", "verbose mode if > 0", 0,2);
64
65 src_min_opt.setHide(1);
66 src_max_opt.setHide(1);
67 otype_opt.setHide(1);
68 bool doProcess;//stop process when program was invoked with help option (-h --help)
69 try{
70 doProcess=minp_opt.retrieveOption(app.getArgc(),app.getArgv());
71 nodata_opt.retrieveOption(app.getArgc(),app.getArgv());
72 src_min_opt.retrieveOption(app.getArgc(),app.getArgv());
73 src_max_opt.retrieveOption(app.getArgc(),app.getArgv());
74 maxp_opt.retrieveOption(app.getArgc(),app.getArgv());
75 fromValue_opt.retrieveOption(app.getArgc(),app.getArgv());
76 toValue_opt.retrieveOption(app.getArgc(),app.getArgv());
77 band_opt.retrieveOption(app.getArgc(),app.getArgv());
78 equalize_opt.retrieveOption(app.getArgc(),app.getArgv());
79 otype_opt.retrieveOption(app.getArgc(),app.getArgv());
80 verbose_opt.retrieveOption(app.getArgc(),app.getArgv());
81 if(!doProcess){
82 cout << endl;
83 std::ostringstream helpStream;
84 helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
85 throw(helpStream.str());//help was invoked, stop processing
86 }
87 if(src_min_opt.size()){
88 while(src_min_opt.size()<band_opt.size())
89 src_min_opt.push_back(src_min_opt[0]);
90 }
91 if(src_max_opt.size()){
92 while(src_max_opt.size()<band_opt.size())
93 src_max_opt.push_back(src_max_opt[0]);
94 }
95 if(minp_opt.empty()&&maxp_opt.empty())
96 minp_opt.push_back(0);
97 if(minp_opt.size()&&maxp_opt.empty())
98 maxp_opt.push_back(100.0-minp_opt[0]);
99 if(maxp_opt.size()&&minp_opt.empty())
100 minp_opt.push_back(100.0-maxp_opt[0]);
101 if(maxp_opt[0]<minp_opt[0]){
102 minp_opt.push_back(maxp_opt[0]);
103 maxp_opt[0]=minp_opt[0];
104 minp_opt[0]=minp_opt.back();
105 minp_opt.erase(minp_opt.begin()+1);
106 }
107 if(band_opt.empty()){
108 while(band_opt.size()<nrOfBand())
109 band_opt.push_back(band_opt.size());
110 }
111 GDALDataType theType=getGDALDataType();
112 if(otype_opt.size())
113 theType=string2GDAL(otype_opt[0]);
114 imgWriter.open(this->nrOfCol(),this->nrOfRow(),band_opt.size(),theType);
115 imgWriter.setProjection(this->getProjection());
116 imgWriter.copyGeoTransform(*this);
117
118 if(nodata_opt.size()){
119 setNoData(nodata_opt);
120 imgWriter.setNoData(nodata_opt);
121 for(unsigned int iband=0;iband<imgWriter.nrOfBand();++iband)
122 imgWriter.GDALSetNoDataValue(nodata_opt[0],iband);
123 }
124 for(int iband=0;iband<band_opt.size();++iband){
125 int theBand=band_opt[iband];
126 if(theBand<0||theBand>nrOfBand()){
127 string ErrorString="Error: band exceeds number of bands";
128 throw(ErrorString);
129 }
130 double minValue=(src_min_opt.size())? src_min_opt[0] : 0;
131 double maxValue=(src_max_opt.size())? src_max_opt[0] : 0;
132 getMinMax(minValue,maxValue,theBand);
133 if(src_min_opt.size())
134 minValue=src_min_opt[0];
135 if(src_max_opt.size())
136 maxValue=src_max_opt[0];
137 if(minValue>=maxValue)
138 getMinMax(minValue,maxValue,theBand);
139 int nbin=0;//calculate automatically from min and max
140 getMinMax(minValue,maxValue,theBand);
141 //nbin is calculated automatically in case of Byte/short integer types, else set to 100
142 if(getDataTypeSizeBytes()>2)
143 nbin=255;
144 std::vector<double> histogramValues;
145 std::vector<double> histogramOutput;
146 double nvalid=getHistogram(histogramOutput, minValue, maxValue, nbin, theBand);
147 double scale=static_cast<double>(nbin-1)/(maxValue-minValue);
148 //create cumulative histogram
149 std::vector<double>::iterator histit=histogramOutput.begin();
150 double value=0;
151 int ibin=0;
152 double minpv=minValue;//value corresponding to minp_opt[0]
153 double maxpv=maxValue;//value corresponding to maxp_opt[0]
154 while(histit!=histogramOutput.end()){
155 histogramValues.push_back(ibin/scale+minValue);
156 value+=*histit/nvalid;
157 *histit=value;
158 if(100*(*histit)<=minp_opt[0])
159 minpv=histogramValues.back();
160 if(100*(*histit)<=maxp_opt[0])
161 maxpv=histogramValues.back();
162 ++histit;
163 ++ibin;
164 // }
165 // else
166 // histogramOutput.erase(histit);
167 }
168 nbin=histogramOutput.size();
169 if(verbose_opt[0]){
170 std::cout << "minValue: " << minValue << std::endl;
171 std::cout << "maxValue: " << maxValue << std::endl;
172 std::cout << "nbin: " << nbin << std::endl;
173 std::cout << "total count cumulative histogram: " << value << std::endl;
174 std::cout << "nvalid: " << nvalid << std::endl;
175 std::cout << "minpv: " << minpv << std::endl;
176 std::cout << "maxpv: " << maxpv << std::endl;
177 }
178 std::vector<double> lineBuffer(nrOfCol());
179 for(int irow=0;irow<nrOfRow();++irow){
180 readData(lineBuffer,irow,theBand);
181 for(int icol=0;icol<nrOfCol();++icol){
182 if(isNoData(lineBuffer[icol]))
183 continue;
184 else if(lineBuffer[icol]>maxValue)
185 lineBuffer[icol]=toValue_opt[0];
186 else if(lineBuffer[icol]<minValue)
187 lineBuffer[icol]=fromValue_opt[0];
188 else{
189 int theBin=static_cast<unsigned long int>(scale*(lineBuffer[icol]-minValue));
190 double cdf_min=100*histogramOutput[0];
191 if(cdf_min<minp_opt[0])
192 cdf_min=minp_opt[0];
193 double cdf_max=maxp_opt[0];
194 double cdf=100*histogramOutput[theBin];
195 double value=0;
196 if(lineBuffer[icol]<minpv)
197 value=fromValue_opt[0];
198 else if(lineBuffer[icol]>maxpv)
199 value=toValue_opt[0];
200 else if(equalize_opt[0])
201 value=(cdf-cdf_min)/(cdf_max-cdf_min)*(toValue_opt[0]-fromValue_opt[0])+fromValue_opt[0];
202 else
203 value=(lineBuffer[icol]-minpv)/(maxpv-minpv)*(toValue_opt[0]-fromValue_opt[0])+fromValue_opt[0];
204 //round values if output data type is not float or double
205 if(theType==GDT_Float32||theType==GDT_Float64)
206 lineBuffer[icol]=value;
207 else
208 lineBuffer[icol]=static_cast<int>(value+0.5);
209 }
210 }
211 imgWriter.writeData(lineBuffer,irow,theBand);
212 }
213 }
214 }
215 catch(string predefinedString){
216 std::cout << predefinedString << std::endl;
217 throw;
218 }
219 }