1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef bbf_SCANNER_EM_H
18 #define bbf_SCANNER_EM_H
19 
20 /* ---- includes ----------------------------------------------------------- */
21 
22 #include "b_BasicEm/Context.h"
23 #include "b_BasicEm/Basic.h"
24 #include "b_BasicEm/UInt32Arr.h"
25 #include "b_BasicEm/Int32Arr.h"
26 #include "b_BasicEm/Int16Arr.h"
27 #include "b_BasicEm/MemTbl.h"
28 #include "b_BasicEm/UInt16Arr.h"
29 #include "b_ImageEm/UInt32Image.h"
30 
31 #include "b_BitFeatureEm/Feature.h"
32 #include "b_BitFeatureEm/BitParam.h"
33 
34 /* ---- related objects  --------------------------------------------------- */
35 
36 /* ---- typedefs ----------------------------------------------------------- */
37 
38 /* ---- constants ---------------------------------------------------------- */
39 
40 /* data format version number */
41 #define bbf_SCANNER_VERSION 100
42 
43 /* ---- object definition -------------------------------------------------- */
44 
45 /** scans an image and provides patches as needed */
46 struct bbf_Scanner
47 {
48 	/* ---- private data --------------------------------------------------- */
49 
50 	/** downscale exponent */
51 	uint32 scaleExpE;
52 
53 	/** current scale (12.20) */
54 	uint32 scaleE;
55 
56 	/** current scan x-coordinate */
57 	int32 xE;
58 
59 	/** current scan y-coordinate */
60 	int32 yE;
61 
62 	/** effective maximum scale (12.20) */
63 	uint32 effMaxScaleE;
64 
65 	/** width of scaled image */
66 	uint32 currentWidthE;
67 
68 	/** height of scaled image */
69 	uint32 currentHeightE;
70 
71 	/** width of work image */
72 	uint32 workWidthE;
73 
74 	/** height of work image */
75 	uint32 workHeightE;
76 
77 	/** parameter for bit generation */
78 	struct bbf_BitParam bitParamE;
79 
80 	/** work image (two pixels per uint16)*/
81 	struct bbs_UInt16Arr workImageE;
82 
83 	/** summed-area table (ring buffer) */
84 	struct bim_UInt32Image satE;
85 
86 	/** bit image */
87 	struct bim_UInt32Image bitImageE;
88 
89 	/** patch buffer */
90 	struct bbs_UInt32Arr patchBufferE;
91 
92 	/** image line buffer */
93 	struct bbs_UInt16Arr lineBufE;
94 
95 
96 
97 	/** index position buffer */
98 	struct bbs_UInt32Arr idxArrE;
99 
100 	/** activity buffer */
101 	struct bbs_Int32Arr actArrE;
102 
103 	/** composite output buffer */
104 	struct bbs_Int32Arr outArrE;
105 
106 	/* internal positions detected */
107 	uint32 intCountE;
108 
109 	/* output positions detected */
110 	uint32 outCountE;
111 
112 	/** Face positions buffer size (approx.: max faces * 20...60)
113 	 *  This variable is not part of I/O and must be set before calling memRead
114 	 *  Default value: 1024 -> about 100...200 faces/image detectable
115 	 *
116 	 *  The Scanner allocates internally bufferSizeE * 10 bytes of exclusive memory
117 	 */
118 	uint32 bufferSizeE;
119 
120 	/* ---- public data ---------------------------------------------------- */
121 
122 	/** maximum image width */
123 	uint32 maxImageWidthE;
124 
125 	/** maximum image height */
126 	uint32 maxImageHeightE;
127 
128 	/** maximum filter radius */
129 	uint32 maxRadiusE;
130 
131 	/** patch width */
132 	uint32 patchWidthE;
133 
134 	/** patch height */
135 	uint32 patchHeightE;
136 
137 	/** minimum scale (12.20) */
138 	uint32 minScaleE;
139 
140 	/** maximum scale (12.20) (0: unlimited) */
141 	uint32 maxScaleE;
142 
143 	/** scale step factor (1.32) (leading bit is always one and therfore ignored) */
144 	uint32 scaleStepE;
145 
146 	/** x-border in pixels */
147 	uint32 borderWidthE;
148 
149 	/** y-border in pixels */
150 	uint32 borderHeightE;
151 
152 };
153 
154 /* ---- associated objects ------------------------------------------------- */
155 
156 /* ---- external functions ------------------------------------------------- */
157 
158 /* ---- \ghd{ constructor/destructor } ------------------------------------- */
159 
160 /** initializes bbf_Scanner  */
161 void bbf_Scanner_init( struct bbs_Context* cpA,
162 					   struct bbf_Scanner* ptrA );
163 
164 /** resets bbf_Scanner  */
165 void bbf_Scanner_exit( struct bbs_Context* cpA,
166 					   struct bbf_Scanner* ptrA );
167 
168 /* ---- \ghd{ operators } -------------------------------------------------- */
169 
170 /** copy operator */
171 void bbf_Scanner_copy( struct bbs_Context* cpA,
172 					   struct bbf_Scanner* ptrA,
173 					   const struct bbf_Scanner* srcPtrA );
174 
175 /** equal operator */
176 flag bbf_Scanner_equal( struct bbs_Context* cpA,
177 					    const struct bbf_Scanner* ptrA,
178 					    const struct bbf_Scanner* srcPtrA );
179 
180 /* ---- \ghd{ query functions } -------------------------------------------- */
181 
182 /** scan positions at current scale */
183 uint32 bbf_Scanner_positions( const struct bbf_Scanner* ptrA );
184 
185 /** current scan index */
186 uint32 bbf_Scanner_scanIndex( const struct bbf_Scanner* ptrA );
187 
188 /** returns current uls position relative to original image; x,y: 16.16; scale: 12.20 */
189 void bbf_Scanner_pos( const struct bbf_Scanner* ptrA,
190 					  int32* xPtrA, int32* yPtrA, uint32* scalePtrA );
191 
192 /** returns uls position relative to original image at index position; x,y: 16.16; scale: 12.20 */
193 void bbf_Scanner_idxPos( const struct bbf_Scanner* ptrA, uint32 scanIndexA,
194 					     int32* xPtrA, int32* yPtrA, uint32* scalePtrA );
195 
196 /* ---- \ghd{ modify functions } ------------------------------------------- */
197 
198 /** creates & initializes object */
199 void bbf_Scanner_create( struct bbs_Context* cpA,
200 						 struct bbf_Scanner* ptrA,
201 						 flag maximizeSharedMemoryA,
202 						 uint32 maxImageWidthA,
203 					 	 uint32 maxImageHeightA,
204 						 uint32 maxRadiusA,
205 						 uint32 patchWidthA,
206 						 uint32 patchHeightA,
207 						 uint32 minScaleA,
208 						 uint32 maxScaleA,
209 						 uint32 scaleStepA,
210 						 uint32 borderWidthA,
211 						 uint32 borderHeightA,
212 						 uint32 bufferSizeA,
213 						 struct bbs_MemTbl* mtpA );
214 
215 /** parameter for bit generation + recomputing bit image */
216 void bbf_Scanner_bitParam( struct bbs_Context* cpA,
217 						   struct bbf_Scanner* ptrA,
218 						   const struct bbf_BitParam* bitParamPtrA );
219 
220 /* ---- \ghd{ memory I/O } ------------------------------------------------- */
221 
222 /** word size (16-bit) object needs when written to memory */
223 uint32 bbf_Scanner_memSize( struct bbs_Context* cpA,
224 					        const struct bbf_Scanner* ptrA );
225 
226 /** writes object to memory; returns number of words (16-bit) written */
227 uint32 bbf_Scanner_memWrite( struct bbs_Context* cpA,
228 							 const struct bbf_Scanner* ptrA, uint16* memPtrA );
229 
230 /** reads object from memory; returns number of words (16-bit) read */
231 uint32 bbf_Scanner_memRead( struct bbs_Context* cpA,
232 							struct bbf_Scanner* ptrA,
233 							const uint16* memPtrA,
234 							struct bbs_MemTbl* mtpA );
235 
236 /* ---- \ghd{ exec functions } --------------------------------------------- */
237 
238 /** resets scan position at current scale level */
239 void bbf_Scanner_resetScan( struct bbs_Context* cpA, struct bbf_Scanner* ptrA );
240 
241 /** Assigns image; sets initial bit parameters; resets processor.
242  *  If roiPtrA is NULL, the whole image is considered for processsing
243  *  otherwise *roiPtrA specifies a section of the original image to which
244  *  procesing is limited. All coordinates refer to that section and must
245  *  eventually be corrected externally.
246  *  The roi rectangle must not include pixels outside of the original image
247  *  (checked -> error). The rectangle may be of uneven width.
248  */
249 void bbf_Scanner_assign( struct bbs_Context* cpA, struct bbf_Scanner* ptrA,
250 					     const void* imagePtrA,
251 						 uint32 imageWidthA,
252 						 uint32 imageHeightA,
253 						 const struct bts_Int16Rect* roiPtrA,
254 						 const struct bbf_BitParam* paramPtrA );
255 
256 /** goes to next scale position */
257 flag bbf_Scanner_nextScale( struct bbs_Context* cpA, struct bbf_Scanner* ptrA );
258 
259 /** returns pointer to patch data */
260 const uint32* bbf_Scanner_getPatch( const struct bbf_Scanner* ptrA );
261 
262 /** goes to next scan position */
263 flag bbf_Scanner_next( struct bbs_Context* cpA, struct bbf_Scanner* ptrA );
264 
265 /** goes to scan position */
266 void bbf_Scanner_goToXY( struct bbs_Context* cpA, struct bbf_Scanner* ptrA, int32 xA, int32 yA );
267 
268 /** goes to scan index position */
269 void bbf_Scanner_goToIndex( struct bbs_Context* cpA, struct bbf_Scanner* ptrA, uint32 scanIndexA );
270 
271 /** goes to scan position from image uls position (error if scales do not match); x,y: 16.16; scale: 12.20 */
272 void bbf_Scanner_goToUls( struct bbs_Context* cpA, struct bbf_Scanner* ptrA,
273 						  int32 xA, int32 yA, uint32 scaleA );
274 
275 /** The functions below offer positions management of temporary positions needed by the detector object */
276 
277 /** resets internal positions */
278 void bbf_Scanner_resetIntPos( struct bbs_Context* cpA, struct bbf_Scanner* ptrA );
279 
280 /** reset output positions */
281 void bbf_Scanner_resetOutPos( struct bbs_Context* cpA, struct bbf_Scanner* ptrA ) ;
282 
283 /* add internal position */
284 void bbf_Scanner_addIntPos( struct bbs_Context* cpA, struct bbf_Scanner* ptrA, uint32 idxA,	int32 actA );
285 
286 /* add external position */
287 void bbf_Scanner_addOutPos( struct bbs_Context* cpA,
288 							struct bbf_Scanner* ptrA,
289 							int32 xA,
290 							int32 yA,
291 							uint32 scaleA,
292 							int32 actA );
293 
294 /* removes internal overlaps */
295 uint32 bbf_Scanner_removeIntOverlaps( struct bbs_Context* cpA,
296 								      struct bbf_Scanner* ptrA,
297 									  uint32 overlapThrA );
298 
299 /** removes output overlaps */
300 uint32 bbf_Scanner_removeOutOverlaps( struct bbs_Context* cpA,
301 							          struct bbf_Scanner* ptrA,
302 									  uint32 overlapThrA );
303 
304 #endif /* bbf_SCANNER_EM_H */
305 
306