Debian Codegraph @

src/filter/ndvi/ndvi.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 Brian Matherly ([email protected])
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #ifdef HAVE_CAIRO
19 #include <cairo.h>
20 #endif
21 #include "frei0r.hpp"cxx . . $ `<file>/include/frei0r.hpp`/
22 #include "frei0r/math.h"cxx . . $ `<file>/include/frei0r/math.h`/
23 #include "gradientlut.hpp"cxx . . $ `<file>/src/filter/ndvi/gradientlut.hpp`/
24 #include <string>
25 #include <stdlib.h>
26
27 /**
28 This filter calculates NDVI or VI values from near-infrared + visible video.
29 The index values are mapped to a false color image.
30 */
31
32 static inline double N2Pcxx . . $ N2P(447c67c6676f75c3).(double ndvi) {
33 // Convert an index value (-1.0 to 1.0) to position (0.0 to 1.0).
34 return (ndvi + 1.0) / 2.0;
35 }
36
37 static unsigned int ColorIndexcxx . . $ ColorIndex(3fcf6099b9b98586).(const stdcxx . . $ std/::stringcxx . . $ std/string#& str) {
38 // Convert a color initial to a component index.
39 if(str ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "r" || str ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "R") {
40 return 0;
41 } else if(str ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "g" || str ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "G") {
42 return 1;
43 } else { // "b"
44 return 2;
45 }
46 }
47
48 class Ndvicxx . . $ Ndvi# : public frei0rcxx . . $ frei0r/::filtercxx . . $ frei0r/filter#
49 {
50 public:
51 Ndvicxx . . $ Ndvi#Ndvi(b4d55b1286ff73e8).(unsigned int width, unsigned int height);
52 virtual void updatecxx . . $ Ndvi#update(da7b4a17ceb4e697).(double time,
53 uint32_t* out,
54 const uint32_t* in);
55
56 private:
57 void initLutcxx . . $ Ndvi#initLut(49f6e7a06ebc5aa8).();
58 double getComponentcxx . . $ Ndvi#getComponent(249721b29a66c6dd).(uint8_t *sample, unsigned int chan, double offset, double scale);
59 void setColorcxx . . $ Ndvi#setColor(17d12369f88f69ef).(uint8_t *sample, double index);
60 void drawLegendcxx . . $ Ndvi#drawLegend(218b13e5e7cbf689).(uint32_t *out);
61 void drawRectcxx . . $ Ndvi#drawRect(8dd88f2c2e7c6416).( uint32_t* out, uint8_t r, uint8_t g, uint8_t b, unsigned int x, unsigned int y, unsigned int w, unsigned int h );
62 void drawGradientcxx . . $ Ndvi#drawGradient(f63bf3ae367be14).( uint32_t* out, unsigned int x, unsigned int y, unsigned int w, unsigned int h );
63 void drawTextcxx . . $ Ndvi#drawText(b83af27c6302d697).( uint32_t* out, stdcxx . . $ std/::stringcxx . . $ std/string# text, unsigned int x, unsigned int y, unsigned int textHeight );
64
65 double paramLutLevelscxx . . $ Ndvi#paramLutLevels.;
66 stdcxx . . $ std/::stringcxx . . $ std/string# paramColorMapcxx . . $ Ndvi#paramColorMap.;
67 double paramVisScalecxx . . $ Ndvi#paramVisScale.;
68 double paramVisOffsetcxx . . $ Ndvi#paramVisOffset.;
69 double paramNirScalecxx . . $ Ndvi#paramNirScale.;
70 double paramNirOffsetcxx . . $ Ndvi#paramNirOffset.;
71 stdcxx . . $ std/::stringcxx . . $ std/string# paramVisChancxx . . $ Ndvi#paramVisChan.;
72 stdcxx . . $ std/::stringcxx . . $ std/string# paramNirChancxx . . $ Ndvi#paramNirChan.;
73 stdcxx . . $ std/::stringcxx . . $ std/string# paramIndexcxx . . $ Ndvi#paramIndex.;
74 stdcxx . . $ std/::stringcxx . . $ std/string# paramLegendcxx . . $ Ndvi#paramLegend.;
75 unsigned int lutLevelscxx . . $ Ndvi#lutLevels.;
76 stdcxx . . $ std/::stringcxx . . $ std/string# colorMapcxx . . $ Ndvi#colorMap.;
77 GradientLutcxx . . $ GradientLut# gradientcxx . . $ Ndvi#gradient.;
78 };
79
80 Ndvicxx . . $ Ndvi#::Ndvicxx . . $ Ndvi#Ndvi(b4d55b1286ff73e8).(unsigned int width, unsigned int height)
81 : paramLutLevelscxx . . $ Ndvi#paramLutLevels.(256.0 / 1000.0)
82 , paramColorMapcxx . . $ Ndvi#paramColorMap.("grayscale")
83 , paramVisScalecxx . . $ Ndvi#paramVisScale.(0.1)
84 , paramVisOffsetcxx . . $ Ndvi#paramVisOffset.(0.5)
85 , paramNirScalecxx . . $ Ndvi#paramNirScale.(0.1)
86 , paramNirOffsetcxx . . $ Ndvi#paramNirOffset.(0.5)
87 , paramVisChancxx . . $ Ndvi#paramVisChan.("b")
88 , paramNirChancxx . . $ Ndvi#paramNirChan.("r")
89 , paramIndexcxx . . $ Ndvi#paramIndex.("ndvi")
90 , paramLegendcxx . . $ Ndvi#paramLegend.("off")
91 , lutLevelscxx . . $ Ndvi#lutLevels.(0)
92 , colorMapcxx . . $ Ndvi#colorMap.("")
93 , gradientcxx . . $ Ndvi#gradient.()
94 {
95 register_paramcxx . . $ frei0r/fx#register_param(f828b6c35e9835e7).(paramColorMapcxx . . $ Ndvi#paramColorMap., "Color Map"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
96 "The color map to use. One of 'earth', 'grayscale', 'heat' or 'rainbow'."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
97 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(paramLutLevelscxx . . $ Ndvi#paramLutLevels., "Levels"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
98 "The number of color levels to use in the false image (divided by 1000)."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
99 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(paramVisScalecxx . . $ Ndvi#paramVisScale., "VIS Scale"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
100 "A scaling factor to be applied to the visible component (divided by 10)."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
101 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(paramVisOffsetcxx . . $ Ndvi#paramVisOffset., "VIS Offset"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
102 "An offset to be applied to the visible component (mapped to [-100%, 100%]."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
103 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(paramNirScalecxx . . $ Ndvi#paramNirScale., "NIR Scale"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
104 "A scaling factor to be applied to the near-infrared component (divided by 10)."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
105 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(paramNirOffsetcxx . . $ Ndvi#paramNirOffset., "NIR Offset"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
106 "An offset to be applied to the near-infrared component (mapped to [-100%, 100%]."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
107 register_paramcxx . . $ frei0r/fx#register_param(f828b6c35e9835e7).(paramVisChancxx . . $ Ndvi#paramVisChan., "Visible Channel"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
108 "The channel to use for the visible component. One of 'r', 'g', or 'b'."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
109 register_paramcxx . . $ frei0r/fx#register_param(f828b6c35e9835e7).(paramNirChancxx . . $ Ndvi#paramNirChan., "NIR Channel"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
110 "The channel to use for the near-infrared component. One of 'r', 'g', or 'b'."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
111 register_paramcxx . . $ frei0r/fx#register_param(f828b6c35e9835e7).(paramIndexcxx . . $ Ndvi#paramIndex., "Index Calculation"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
112 "The index calculation to use. One of 'ndvi' or 'vi'."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
113 register_paramcxx . . $ frei0r/fx#register_param(f828b6c35e9835e7).(paramLegendcxx . . $ Ndvi#paramLegend., "Legend"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
114 "Control legend display. One of 'off' or 'bottom'."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
115 }
116
117 void Ndvicxx . . $ Ndvi#::updatecxx . . $ Ndvi#update(da7b4a17ceb4e697).(double time,
118 uint32_t* out,
119 const uint32_t* in) {
120 uint8_t *inP = (uint8_t*)in;
121 uint8_t *outP = (uint8_t*)out;
122 double visScale = paramVisScalecxx . . $ Ndvi#paramVisScale. * 10.0;
123 double visOffset = (paramVisOffsetcxx . . $ Ndvi#paramVisOffset. * 510) - 255;
124 double nirScale = paramNirScalecxx . . $ Ndvi#paramNirScale. * 10.0;
125 double nirOffset = (paramNirOffsetcxx . . $ Ndvi#paramNirOffset. * 510) - 255;
126 unsigned int visChan = ColorIndexcxx . . $ ColorIndex(3fcf6099b9b98586).(paramVisChancxx . . $ Ndvi#paramVisChan.);
127 unsigned int nirChan = ColorIndexcxx . . $ ColorIndex(3fcf6099b9b98586).(paramNirChancxx . . $ Ndvi#paramNirChan.);
128
129 initLutcxx . . $ Ndvi#initLut(49f6e7a06ebc5aa8).();
130
131 if (paramIndexcxx . . $ Ndvi#paramIndex. ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "vi") {
132 for (unsigned int i = 0; i < sizecxx . . $ frei0r/fx#size.; i++) {
133 double vis = getComponentcxx . . $ Ndvi#getComponent(249721b29a66c6dd).(inP, visChan, visOffset, visScale);
134 double nir = getComponentcxx . . $ Ndvi#getComponent(249721b29a66c6dd).(inP, nirChan, nirOffset, nirScale);
135 double vi = (nir - vis) / 255.0;
136 setColorcxx . . $ Ndvi#setColor(17d12369f88f69ef).(outP, vi);
137 inP += 4;
138 outP += 4;
139 }
140 } else { // ndvi
141 for (unsigned int i = 0; i < sizecxx . . $ frei0r/fx#size.; i++) {
142 double vis = getComponentcxx . . $ Ndvi#getComponent(249721b29a66c6dd).(inP, visChan, visOffset, visScale);
143 double nir = getComponentcxx . . $ Ndvi#getComponent(249721b29a66c6dd).(inP, nirChan, nirOffset, nirScale);
144 double ndvi = (nir - vis) / (nir + vis);
145 setColorcxx . . $ Ndvi#setColor(17d12369f88f69ef).(outP, ndvi);
146 inP += 4;
147 outP += 4;
148 }
149 }
150
151 if( paramLegendcxx . . $ Ndvi#paramLegend. ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "bottom" ) {
152 drawLegendcxx . . $ Ndvi#drawLegend(218b13e5e7cbf689).(out);
153 }
154 }
155
156 void Ndvicxx . . $ Ndvi#::initLutcxx . . $ Ndvi#initLut(49f6e7a06ebc5aa8).() {
157 // Only update the LUT if a parameter has changed.
158 unsigned int paramLutLevelsInt = paramLutLevelscxx . . $ Ndvi#paramLutLevels. * 1000.0 + 0.5;
159 if (paramLutLevelsInt < 2) paramLutLevelsInt = 2;
160 if (paramLutLevelsInt > 1000) paramLutLevelsInt = 1000;
161 if (lutLevelscxx . . $ Ndvi#lutLevels. == paramLutLevelsInt &&
162 colorMapcxx . . $ Ndvi#colorMap. ==cxx . . $ std/`operator==`(2965e344aacb545b). paramColorMapcxx . . $ Ndvi#paramColorMap.) {
163 return;
164 } else {
165 lutLevelscxx . . $ Ndvi#lutLevels. = paramLutLevelsInt;
166 colorMapcxx . . $ Ndvi#colorMap. =cxx . . $ std/__cxx11/basic_string#`operator=`(c0b47be1fddc0e75). paramColorMapcxx . . $ Ndvi#paramColorMap.;
167 }
168
169 gradientcxx . . $ Ndvi#gradient..setDepthcxx . . $ GradientLut#setDepth(2c44cd906ae4e829).(lutLevelscxx . . $ Ndvi#lutLevels.);
170
171 if(colorMapcxx . . $ Ndvi#colorMap. ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "earth") {
172 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# water = {0x30, 0x70, 0xd0};
173 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# desert = {0xd0, 0xc0, 0x90};
174 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# grass = {0x00, 0xc0, 0x20};
175 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# forrest = {0x00, 0x30, 0x00};
176 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).(-1.0), water, N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.2), water );
177 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.2), water, N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.1), desert );
178 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.1), desert, N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.1), desert );
179 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.1), desert, N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.4), grass );
180 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.4), grass, N2Pcxx . . $ N2P(447c67c6676f75c3).( 1.0), forrest );
181 } else if(colorMapcxx . . $ Ndvi#colorMap. ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "heat") {
182 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# n10 = {0x00, 0x00, 0x00};
183 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# n08 = {0x10, 0x10, 0x70};
184 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# n06 = {0x10, 0x20, 0xf0};
185 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# n04 = {0x10, 0x60, 0xf0};
186 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# n02 = {0x20, 0xa0, 0xc0};
187 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# zer = {0x20, 0xb0, 0x20};
188 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# p02 = {0x90, 0xf0, 0x10};
189 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# p04 = {0xf0, 0xb0, 0x10};
190 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# p06 = {0xf0, 0xa0, 0x10};
191 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# p08 = {0xf0, 0x50, 0x10};
192 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# p10 = {0xff, 0x00, 0x00};
193 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).(-1.0), n10, N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.8), n08 );
194 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.8), n08, N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.6), n06 );
195 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.6), n06, N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.4), n04 );
196 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.4), n04, N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.2), n02 );
197 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.2), n02, N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.0), zer );
198 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.0), zer, N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.2), p02 );
199 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.2), p02, N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.4), p04 );
200 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.4), p04, N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.6), p06 );
201 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.6), p06, N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.8), p08 );
202 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.8), p08, N2Pcxx . . $ N2P(447c67c6676f75c3).( 1.0), p10 );
203 } else if(colorMapcxx . . $ Ndvi#colorMap. ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "rainbow") {
204 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# violet = {0x7f, 0x00, 0xff};
205 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# blue = {0x00, 0x00, 0xff};
206 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# green = {0x00, 0xff, 0x00};
207 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# yellow = {0xff, 0xff, 0x00};
208 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# orange = {0xff, 0x7f, 0x00};
209 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# red = {0xff, 0x00, 0x00};
210 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).(-1.0), violet, N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.6), blue );
211 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.6), blue, N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.2), green );
212 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).(-0.2), green, N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.2), yellow );
213 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.2), yellow, N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.6), orange );
214 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).( 0.6), orange, N2Pcxx . . $ N2P(447c67c6676f75c3).( 1.0), red );
215 } else { // grayscale
216 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# black = {0x00, 0x00, 0x00};
217 GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color# white = {0xff, 0xff, 0xff};
218 gradientcxx . . $ Ndvi#gradient..fillRangecxx . . $ GradientLut#fillRange(4e6784b12c917046).( N2Pcxx . . $ N2P(447c67c6676f75c3).(-1.0), black, N2Pcxx . . $ N2P(447c67c6676f75c3).( 1.0), white );
219 }
220 }
221
222 inline double Ndvicxx . . $ Ndvi#::getComponentcxx . . $ Ndvi#getComponent(249721b29a66c6dd).(uint8_t *sample, unsigned int chan, double offset, double scale)
223 {
224 double c = sample[chan];
225 c = (c + offset) * scale;
226 c = CLAMPcxx . . $ `include/frei0r/math.h:48:9`!(c, 0.0, 255.0);
227 return c;
228 }
229
230 inline void Ndvicxx . . $ Ndvi#::setColorcxx . . $ Ndvi#setColor(17d12369f88f69ef).(uint8_t *sample, double index)
231 {
232 double pos = N2Pcxx . . $ N2P(447c67c6676f75c3).(index);
233 const GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color#& falseColor = gradientcxx . . $ Ndvi#gradient.[cxx . . $ GradientLut#`operator[]`(d5da8d30c22ea07f).pos];
234 sample[0] = falseColor.rcxx . . $ GradientLut#Color#r.;
235 sample[1] = falseColor.gcxx . . $ GradientLut#Color#g.;
236 sample[2] = falseColor.bcxx . . $ GradientLut#Color#b.;
237 sample[3] = 0xff;
238 }
239
240 void Ndvicxx . . $ Ndvi#::drawLegendcxx . . $ Ndvi#drawLegend(218b13e5e7cbf689).(uint32_t* out)
241 {
242 unsigned int legendHeight = heightcxx . . $ frei0r/fx#height. / 20;
243
244 // Black border above legend
245 unsigned int borderHeight = legendHeight / 15;
246 unsigned int borderY = heightcxx . . $ frei0r/fx#height. - legendHeight;
247 drawRectcxx . . $ Ndvi#drawRect(8dd88f2c2e7c6416).( out, 0, 0, 0, 0, borderY, widthcxx . . $ frei0r/fx#width., borderHeight );
248
249 // Gradient
250 unsigned int gradientHeight = legendHeight - borderHeight;
251 unsigned int gradientY = heightcxx . . $ frei0r/fx#height. - gradientHeight;
252 drawGradientcxx . . $ Ndvi#drawGradient(f63bf3ae367be14).( out, 0, gradientY, widthcxx . . $ frei0r/fx#width., gradientHeight );
253
254 // Text
255 unsigned int textHeight = gradientHeight * 8 / 10;
256 unsigned int textY = heightcxx . . $ frei0r/fx#height. - ( gradientHeight - textHeight ) / 2;
257 unsigned int textX = widthcxx . . $ frei0r/fx#width. / 25;
258 if (paramIndexcxx . . $ Ndvi#paramIndex. ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "vi") {
259 drawTextcxx . . $ Ndvi#drawText(b83af27c6302d697).( out, "0"cxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f)., textX, textY , textHeight );
260 drawTextcxx . . $ Ndvi#drawText(b83af27c6302d697).( out, "VI"cxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f)., widthcxx . . $ frei0r/fx#width. / 2, textY , textHeight );
261 drawTextcxx . . $ Ndvi#drawText(b83af27c6302d697).( out, "1"cxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f)., widthcxx . . $ frei0r/fx#width. - textX, textY , textHeight );
262 } else { // ndvi
263 drawTextcxx . . $ Ndvi#drawText(b83af27c6302d697).( out, "-1"cxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f)., textX, textY , textHeight );
264 drawTextcxx . . $ Ndvi#drawText(b83af27c6302d697).( out, "NDVI"cxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f)., widthcxx . . $ frei0r/fx#width. / 2, textY , textHeight );
265 drawTextcxx . . $ Ndvi#drawText(b83af27c6302d697).( out, "1"cxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f)., widthcxx . . $ frei0r/fx#width. - textX, textY , textHeight );
266 }
267 }
268
269 void Ndvicxx . . $ Ndvi#::drawRectcxx . . $ Ndvi#drawRect(8dd88f2c2e7c6416).( uint32_t* out, uint8_t r, uint8_t g, uint8_t b, unsigned int x, unsigned int y, unsigned int w, unsigned int h )
270 {
271 for (unsigned int i = 0; i < h; i++) {
272 uint8_t *sample = (uint8_t*)(out + ((i + y) * widthcxx . . $ frei0r/fx#width.) + x);
273 for (unsigned int j = 0; j < w; j++) {
274 sample[0] = r;
275 sample[1] = g;
276 sample[2] = b;
277 sample += 4;
278 }
279 }
280 }
281
282 void Ndvicxx . . $ Ndvi#::drawGradientcxx . . $ Ndvi#drawGradient(f63bf3ae367be14).( uint32_t* out, unsigned int x, unsigned int y, unsigned int w, unsigned int h )
283 {
284 for (unsigned int i = 0; i < w; i++) {
285 double pos = (double)i / (double)w;
286 const GradientLutcxx . . $ GradientLut#::Colorcxx . . $ GradientLut#Color#& falseColor = gradientcxx . . $ Ndvi#gradient.[cxx . . $ GradientLut#`operator[]`(d5da8d30c22ea07f).pos];
287 uint8_t *sample = (uint8_t*)(out + (y * widthcxx . . $ frei0r/fx#width.) + x + i);
288 for (unsigned int j = 0; j < h; j++) {
289 sample[0] = falseColor.rcxx . . $ GradientLut#Color#r.;
290 sample[1] = falseColor.gcxx . . $ GradientLut#Color#g.;
291 sample[2] = falseColor.bcxx . . $ GradientLut#Color#b.;
292 sample += widthcxx . . $ frei0r/fx#width. * 4;
293 }
294 }
295 }
296
297 void Ndvicxx . . $ Ndvi#::drawTextcxx . . $ Ndvi#drawText(b83af27c6302d697).( uint32_t* out, stdcxx . . $ std/::stringcxx . . $ std/string# text, unsigned int x, unsigned int y, unsigned int textHeight )
298 {
299 #ifdef HAVE_CAIRO
300 int stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, width);
301 cairo_surface_t* surface = cairo_image_surface_create_for_data((unsigned char*)out,
302 CAIRO_FORMAT_ARGB32,
303 width,
304 height,
305 stride);
306 cairo_t *cr = cairo_create(surface);
307 cairo_text_extents_t te;
308 cairo_font_extents_t fe;
309
310 cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
311 cairo_set_font_size(cr, textHeight);
312 cairo_text_extents(cr, text.c_str(), &te);
313 cairo_font_extents(cr, &fe);
314
315 // Center text on x
316 x -= te.width / 2;
317 // Align bottom of text on y (assume no characters go below baseline)
318 y -= (fe.height - fe.ascent + 1) / 2;
319
320 cairo_move_to(cr, x, y);
321 cairo_text_path(cr, text.c_str());
322 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
323 cairo_fill_preserve(cr);
324 cairo_set_source_rgb(cr, 0, 0, 0);
325 cairo_set_line_width(cr, textHeight / 20);
326 cairo_stroke (cr);
327 cairo_surface_destroy (surface);
328 cairo_destroy(cr);
329 #endif
330 }
331
332 frei0rcxx . . $ frei0r/::constructcxx . . $ frei0r/construct#<Ndvicxx . . $ Ndvi#> plugincxx . . $ plugin.("NDVI filter"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
333 "This filter creates a false image from a visible + infrared source."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
334 "Brian Matherly"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
335 0,2,
336 F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!);