QuantizatorOp.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 #ifndef QUANTIZATOROP_H_
32 #define QUANTIZATOROP_H_
33 
34 #include "../util/Functor.h"
35 
36 
37 namespace drain
38 {
39 namespace image
40 {
41 
42 
43 // Consider moving to FunctorOps
45 
50 {
51 public:
52 
53  QuantizatorFunctor(int bits = 4) : UnaryFunctor(__FUNCTION__,
54  "Quantize to n bits. (For integer images)"), mask(0), bitShift(0) {
55  parameters.link("bits", this->bits = bits);
56  };
57 
58 
59  virtual
60  inline
61  int operator()(int s) const {
62  //return s;
63  return (s & mask);
64  // return (s & mask) << bitShift;
65  };
66 
67 
68  virtual
69  double operator()(double s) const {
70  //return this->scaleFinal * s;
71  return (1/255.0)*static_cast<double>(operator ()(static_cast<int>(255.0*s)));
72  }
73 
74 
75  virtual
76  inline
77  void update() const {
78 
79  drain::Logger mout(getImgLog(), __FILE__, __FUNCTION__);
80 
81  mask = 0;
82  // Create 111111...
83  for (unsigned int i = 0; i < bits; i++)
84  mask = (mask << 1) | 1;
85 
86  //mout.warn(mask );
87  mask = mask << (1*8 - bits); // assum uchar?
88  //mout.warn(mask );
89  updateScale();
90  /*
91  mout.note();
92  for (int i=0; i < 255; ++i) {
93  mout << i << '\t' << operator ()(i) << '\t' << operator ()(static_cast<double>(i)) << '\n';
94  }
95  mout << mout.endl;
96  */
97  }
98 
99  /*
100  inline
101  void initializeParameters(const ImageFrame &src, const ImageFrame & src2, const ImageFrame & dst) const {
102 
103 
104  mask = 0;
105 
106  // Create 111111...
107  for (unsigned int i = 0; i < bits; i++)
108  mask = (mask << 1) | 1;
109 
110  // Complete 1111110000
111  mask = mask << (src.getElementSize()*8 - bits);
112 
113  bitShift = (dst.getElementSize()-src.getElementSize())*8;
114  };
115  */
116 
117 
118  unsigned int bits;
119 
120 protected:
121 
122  mutable long int mask;
123 
124  mutable int bitShift;
125 
126 };
127 
128 
129 }
130 }
131 
132 #endif /*QUANTIZE_H_*/
133 
134 // Drain
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:308
TODO: CopyOP should be quite similar.
Definition: QuantizatorOp.h:50
Definition: DataSelector.cpp:1277
Definition: Functor.h:116