Fixed wrong prediction for incomplete ranges.
[fic.git] / modules / stdDomains.h
blob2468a7d7647147259a25ec8781aa65f418f8f90d
1 #ifndef STDDOMAINS_HEADER_
2 #define STDDOMAINS_HEADER_
4 #include "../headers.h"
6 /// \ingroup modules
7 /** Standard domain-pool generator. Available settings:
8 * - the decrease of max. number of domains per level
9 * - how many of more-downscaled domains to use
10 * - how big portions of standard, horizontal, vertical and diamond domains to use
12 class MStdDomains: public ISquareDomains {
13 DECLARE_debugModule;
15 DECLARE_TypeInfo( MStdDomains, "Standard domains generator"
16 , "Creates domain blocks of many types (standard, horizontal, vertical, diamond)"
17 , {
18 label: "Per-level max-domain-count divisor",
19 desc: "How many times will the maximum domain count\n"
20 "decrease with every level\n"
21 "(dimensions are equal to 2^level)",
22 type: settingInt(0,0,4,IntLog2)
23 }, {
24 label: "More-downscaled domain count",
25 desc: "The development of count of domains\n"
26 "made from more downscaled images",
27 type: settingCombo("none\nno decrease\nhalf per scale",0)
28 }, {
29 label: "Standard domains portion",
30 desc: "How big portion from all domains\n"
31 "will be of standard type",
32 type: settingInt(0,1,8)
33 }, {
34 label: "Diamond domains portion",
35 desc: "How big portion from all domains\n"
36 "will be of diamond type",
37 type: settingInt(0,0,8)
38 }, {
39 label: "Horizontal domains portion",
40 desc: "How big portion from all domains\n"
41 "will be of horizontal type",
42 type: settingInt(0,0,8)
43 }, {
44 label: "Vertical domains portion",
45 desc: "How big portion from all domains\n"
46 "will be of vertical type",
47 type: settingInt(0,0,8)
48 } )
50 protected:
51 /** Indices for settings */
52 enum Settings { MaxDomCountLevelDivisor, MultiDownScaling
53 , DomPortion_Standard, DomPortion_Diamond, DomPortion_Horiz, DomPortion_Vert };
55 // Module's data
56 /// The list of domain pools, pool IDs are the indices, the Pool::pixels are owned
57 PoolList pools;
58 int width /// Width of the original image (not zoomed)
59 , height /// Height of the original image (not zoomed)
60 , zoom; ///< the zoom
62 // Construction and destruction
63 #ifndef NDEBUG
64 MStdDomains(): width(-1), height(-1), zoom(-1) {}
65 #endif
66 /** Only frees the #pools */
67 ~MStdDomains() {
68 for (PoolList::iterator it=pools.begin(); it!=pools.end(); ++it)
69 it->free();
72 public:
73 /** \name ISquareDomains interface
74 * @{ */
75 void initPools(const PlaneBlock &planeBlock);
76 void fillPixelsInPools(PlaneBlock &planeBlock);
78 const PoolList& getPools() const
79 { return pools; }
80 std::vector<short> getLevelDensities(int level,int stdDomCountLog2);
82 void writeSettings(std::ostream &file);
83 void readSettings(std::istream &file);
84 /* We have no data that need to be preserved */
85 void writeData(std::ostream &) {}
86 void readData(std::istream &) {}
87 /// @}
92 namespace MatrixWalkers {
93 /** A simple assigning operator - assigns its second argument into the first one */
94 struct ReverseAssigner: public OperatorBase {
95 template<class R1,class R2> void operator()(R1 &dest,R2 src) const { dest= src; }
97 /** Matrix iterator performing a 2x2 to 1 shrink */
98 template<class T,class I=PtrInt>
99 struct HalfShrinker: public Rotation_0<T,I> { ROTBASE_INHERIT
100 typedef Rotation_0<T,I> Base;
102 HalfShrinker( TMatrix matrix, I x0=0, I y0=0 )
103 : Base(matrix,x0,y0) {}
105 T get() {
106 TMatrix &c= current;
107 T *cs= c.start;
108 return ldexp( cs[0] + cs[1] + cs[c.colSkip] + cs[c.colSkip+1], -2 );
110 void outerStep() { Base::outerStep(); Base::outerStep(); }
111 void innerStep() { Base::innerStep(); Base::innerStep(); }
113 /** Matrix iterator performing a 3x3 to 1x2 shrink */
114 template<class T,class I=PtrInt>
115 struct HorizShrinker: public Rotation_0<T,I> { ROTBASE_INHERIT
116 typedef Rotation_0<T,I> Base;
118 bool addHalf; ///< Adds half a pixel to the current position
120 HorizShrinker(TMatrix matrix)
121 : Base(matrix), addHalf(false) {}
123 T get() {
124 Real groups[2]; // groups[0]= sum of a full line, groups[1]= sum of a half line
125 T *cs= current.start;
126 groups[addHalf]= cs[0] + cs[current.colSkip] + cs[current.colSkip*2];
127 ++cs;
128 groups[!addHalf]= cs[0] + cs[current.colSkip] + cs[current.colSkip*2];
129 return (ldexp(groups[0],1)+groups[1]) * Real(1.0/9.0);
131 void innerStep() {
132 // do a 1.5-pixel step
133 Base::innerStep();
134 if (addHalf)
135 Base::innerStep();
136 addHalf= !addHalf;
138 void outerStep() {
139 for (int i=0; i<3; ++i)
140 Base::outerStep();
143 /** Matrix iterator performing a 3x3 to 2x1 shrink */
144 template<class T,class I=PtrInt>
145 struct VertShrinker: public Rotation_0<T,I> { ROTBASE_INHERIT
146 typedef Rotation_0<T,I> Base;
148 bool addHalf; ///< Adds half a pixel to the current position
150 VertShrinker(TMatrix matrix)
151 : Base(matrix), addHalf(false) {}
153 T get() {
154 Real groups[2]; // groups[0]= sum of a full line, groups[1]= sum of a half line
155 T *cs= current.start;
156 groups[addHalf]= cs[0] + cs[1] + cs[2];
157 cs+= current.colSkip;
158 groups[!addHalf]= cs[0] + cs[1] + cs[2];
159 return (ldexp(groups[0],1)+groups[1]) * Real(1.0/9.0);
161 void innerStep() {
162 for (int i=0; i<3; ++i)
163 Base::innerStep();
165 void outerStep() {
166 // do a 1.5-pixel step
167 Base::outerStep();
168 if (addHalf)
169 Base::outerStep();
170 addHalf= !addHalf;
173 /** Matrix iterator performing a shrink of a diamond (rotated sub-square)
174 * into a square, decreasing the area of used pixels to 50% */
175 template<class T,class I=PtrInt>
176 struct DiamShrinker: public Rotation_0<T,I> { ROTBASE_INHERIT
177 typedef Rotation_0<T,I> Base;
179 DiamShrinker(TMatrix matrix,I side)
180 : Base( matrix, side-1, 0 ) {} // the diamond begins on top-middle
182 T get() {
183 TMatrix &c= current;
184 T *cs= c.start;
185 return ldexp( cs[0] + cs[1] + cs[c.colSkip] + cs[c.colSkip+1], -2 );
187 void outerStep() { lastStart+= current.colSkip+1; }
188 void innerStep() { current.start+= 1-current.colSkip; }
190 }// MatrixWalkers namespace
193 #endif // STDDOMAINS_HEADER_