Debian Codegraph @

src/imageclasses/jlfilter2d_lib.h

raw symbols SCIP
1 /**********************************************************************
2 jlfilter2d_lib.h: program to filter raster images in spatial domain
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 #ifndef _JLFILTER2D_LIB_H_
22 #define _JLFILTER2D_LIB_H_
23
24 #include "imageclasses/Jim.h"
25 #include "apps/AppFactory.h"
26
27 //todo: add support for different padding strategies
28 template<typename T> void Jim::firfilter2d_t(Jim& imgWriter, app::AppFactory& app){
29 Optionjl<double> taps_opt("taps", "taps", "taps used for spatial filtering");
30 Optionjl<size_t> dimx_opt("dimx", "dimx", "number of taps used for columns");
31 Optionjl<size_t> dimy_opt("dimy", "dimy", "number of taps used for rows");
32 Optionjl<double> nodata_opt("nodata", "nodata", "list of no data values not taken into account");
33 /* Optionjl<std::string> padding_opt("pad","pad", "Padding method for filtering (how to handle edge effects). Choose between: symmetric, replicate, circular, zero (pad with 0).", "symmetric"); */
34 /* Optionjl<bool> abs_opt("abs", "abs", "use absolute values when filtering",false); */
35 Optionjl<bool> norm_opt("norm", "norm", "normalize tap values values when filtering",false);
36 Optionjl<short> verbose_opt("v", "verbose", "verbose mode if > 0", 0,2);
37
38 bool doProcess;//stop process when program was invoked with help option (-h --help)
39 try{
40 doProcess=taps_opt.retrieveOption(app);
41 dimx_opt.retrieveOption(app);
42 dimy_opt.retrieveOption(app);
43 nodata_opt.retrieveOption(app);
44 /* abs_opt.retrieveOption(app); */
45 norm_opt.retrieveOption(app);
46 /* padding_opt.retrieveOption(app); */
47 verbose_opt.retrieveOption(app);
48 if(!doProcess){
49 std::cout << std::endl;
50 std::ostringstream helpStream;
51 helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
52 throw(helpStream.str());//help was invoked, stop processing
53 }
54
55 std::vector<double> taps(taps_opt.begin(),taps_opt.end());
56
57 if(norm_opt[0]&&nodata_opt.empty()){
58 if(verbose_opt[0])
59 std::cout << "normalizing taps" << std::endl;
60 double norm=0;
61 for(size_t itap=0;itap<taps.size();++itap){
62 norm+=abs(static_cast<double>(taps[itap]));
63 }
64 if(norm){
65 for(size_t itap=0;itap<taps.size();++itap){
66 taps[itap]/=norm;
67 }
68 }
69 }
70 size_t ncol=nrOfCol();
71 size_t nrow=nrOfRow();
72
73 if(dimx_opt.empty()){
74 std::ostringstream errorStream;
75 errorStream << "Error: could not determine dimx of taps" << std::endl;
76 throw(errorStream.str());
77 }
78 if(dimy_opt.empty()){
79 std::ostringstream errorStream;
80 errorStream << "Error: could not determine dimy of taps" << std::endl;
81 throw(errorStream.str());
82 }
83 int dimX=dimx_opt[0];//horizontal!!!
84 int dimY=dimy_opt[0];//vertical!!!
85 if(verbose_opt[0]){
86 std::cout << "dimX: " << dimX << std::endl;
87 std::cout << "dimY: " << dimY << std::endl;
88 }
89 #if JIPLIB_PROCESS_IN_PARALLEL == 1
90 #pragma omp parallel for
91 #else
92 #endif
93 for(size_t iband=0;iband<nrOfBand();++iband){
94 if(verbose_opt[0])
95 std::cout << "processing band " << iband << std::endl;
96 T* pin=static_cast<T*>(getDataPointer(iband));
97 T* pout=static_cast<T*>(imgWriter.getDataPointer(iband));
98
99 int indexI=0;
100 int indexJ=0;
101 if(nodata_opt.size()){
102 for(int y=0;y<nrow;++y){
103 for(int x=0;x<ncol;++x){
104 double norm=0;
105 for(int j=-(dimY-1)/2;j<=dimY/2;++j){
106 for(int i=-(dimX-1)/2;i<=dimX/2;++i){
107 indexI=x+i;
108 indexJ=y+j;
109 //check if out of bounds
110 if(x<(dimX-1)/2)
111 indexI=x+abs(i);
112 else if(x>=ncol-(dimX-1)/2)
113 indexI=x-abs(i);
114 if(y<(dimY-1)/2)
115 indexJ=y+abs(j);
116 else if(y>=nrow-(dimY-1)/2)
117 indexJ=y-abs(j);
118 //do not take masked values into account
119 bool masked=false;
120 for(int imask=0;imask<nodata_opt.size();++imask){
121 if(pin[indexJ*ncol+indexI]==nodata_opt[imask]){
122 masked=true;
123 break;
124 }
125 }
126 if(!masked){
127 norm+=abs(taps[((dimY-1)/2+j)*dimX+(dimX-1)/2+i]);
128 pout[y*ncol+x]+=(taps[((dimY-1)/2+j)*dimX+(dimX-1)/2+i]*pin[indexJ*ncol+indexI]);
129 }
130 }
131 }
132 if(norm>0)
133 pout[y*ncol+x]/=norm;
134 }
135 }
136 }
137 else{
138 for(int y=0;y<nrow;++y){
139 for(int x=0;x<ncol;++x){
140 for(int j=-(dimY-1)/2;j<=dimY/2;++j){
141 for(int i=-(dimX-1)/2;i<=dimX/2;++i){
142 indexI=x+i;
143 indexJ=y+j;
144 //check if out of bounds
145 if(x<(dimX-1)/2)
146 indexI=x+abs(i);
147 else if(x>=ncol-(dimX-1)/2)
148 indexI=x-abs(i);
149 if(y<(dimY-1)/2)
150 indexJ=y+abs(j);
151 else if(y>=nrow-(dimY-1)/2)
152 indexJ=y-abs(j);
153 pout[y*ncol+x]+=(taps[((dimY-1)/2+j)*dimX+(dimX-1)/2+i]*pin[indexJ*ncol+indexI]);
154 }
155 }
156 }
157 }
158 }
159 }
160 }
161 catch(std::string predefinedString){
162 std::cout << predefinedString << std::endl;
163 throw;
164 }
165 }
166
167 #endif // _JLFILTER2D_LIB_H_