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 /* ---- includes ----------------------------------------------------------- */
18 
19 #include "b_BasicEm/Functions.h"
20 #include "b_BasicEm/Math.h"
21 #include "b_BitFeatureEm/L06Dns3x3Ftr.h"
22 
23 /* ------------------------------------------------------------------------- */
24 
25 /* ========================================================================= */
26 /*                                                                           */
27 /* ---- \ghd{ auxiliary functions } ---------------------------------------- */
28 /*                                                                           */
29 /* ========================================================================= */
30 
31 /* ------------------------------------------------------------------------- */
32 
33 /* ========================================================================= */
34 /*                                                                           */
35 /* ---- \ghd{ constructor / destructor } ----------------------------------- */
36 /*                                                                           */
37 /* ========================================================================= */
38 
39 /* ------------------------------------------------------------------------- */
40 
bbf_L06Dns3x3Ftr_init(struct bbs_Context * cpA,struct bbf_L06Dns3x3Ftr * ptrA)41 void bbf_L06Dns3x3Ftr_init( struct bbs_Context* cpA,
42 						    struct bbf_L06Dns3x3Ftr* ptrA )
43 {
44 	bbf_Feature_init( cpA, &ptrA->baseE );
45 	ptrA->baseE.typeE = ( uint32 )bbf_FT_L06_DNS_3X3_FTR;
46 	ptrA->baseE.vpActivityE = bbf_L06Dns3x3Ftr_activity;
47 	bbs_UInt32Arr_init( cpA, &ptrA->dataArrE );
48 	ptrA->activityFactorE = 0;
49 }
50 
51 /* ------------------------------------------------------------------------- */
52 
bbf_L06Dns3x3Ftr_exit(struct bbs_Context * cpA,struct bbf_L06Dns3x3Ftr * ptrA)53 void bbf_L06Dns3x3Ftr_exit( struct bbs_Context* cpA,
54 						    struct bbf_L06Dns3x3Ftr* ptrA )
55 {
56 	bbf_Feature_exit( cpA, &ptrA->baseE );
57 	bbs_UInt32Arr_exit( cpA, &ptrA->dataArrE );
58 	ptrA->activityFactorE = 0;
59 }
60 
61 /* ------------------------------------------------------------------------- */
62 
63 /* ========================================================================= */
64 /*                                                                           */
65 /* ---- \ghd{ operators } -------------------------------------------------- */
66 /*                                                                           */
67 /* ========================================================================= */
68 
69 /* ------------------------------------------------------------------------- */
70 
bbf_L06Dns3x3Ftr_copy(struct bbs_Context * cpA,struct bbf_L06Dns3x3Ftr * ptrA,const struct bbf_L06Dns3x3Ftr * srcPtrA)71 void bbf_L06Dns3x3Ftr_copy( struct bbs_Context* cpA,
72 						    struct bbf_L06Dns3x3Ftr* ptrA,
73 						    const struct bbf_L06Dns3x3Ftr* srcPtrA )
74 {
75 	bbf_Feature_copy( cpA, &ptrA->baseE, &srcPtrA->baseE );
76 	bbs_UInt32Arr_copy( cpA, &ptrA->dataArrE, &srcPtrA->dataArrE );
77 	ptrA->activityFactorE = srcPtrA->activityFactorE;
78 }
79 
80 /* ------------------------------------------------------------------------- */
81 
bbf_L06Dns3x3Ftr_equal(struct bbs_Context * cpA,const struct bbf_L06Dns3x3Ftr * ptrA,const struct bbf_L06Dns3x3Ftr * srcPtrA)82 flag bbf_L06Dns3x3Ftr_equal( struct bbs_Context* cpA,
83 						     const struct bbf_L06Dns3x3Ftr* ptrA,
84 						     const struct bbf_L06Dns3x3Ftr* srcPtrA )
85 {
86 	if( !bbf_Feature_equal( cpA, &ptrA->baseE, &srcPtrA->baseE ) ) return FALSE;
87 	if( !bbs_UInt32Arr_equal( cpA, &ptrA->dataArrE, &srcPtrA->dataArrE ) ) return FALSE;
88 	if( ptrA->activityFactorE != srcPtrA->activityFactorE ) return FALSE;
89 	return TRUE;
90 }
91 
92 /* ------------------------------------------------------------------------- */
93 
94 /* ========================================================================= */
95 /*                                                                           */
96 /* ---- \ghd{ query functions } -------------------------------------------- */
97 /*                                                                           */
98 /* ========================================================================= */
99 
100 /* ------------------------------------------------------------------------- */
101 
102 /* ========================================================================= */
103 /*                                                                           */
104 /* ---- \ghd{ modify functions } ------------------------------------------- */
105 /*                                                                           */
106 /* ========================================================================= */
107 
108 /* ------------------------------------------------------------------------- */
109 
110 /* ========================================================================= */
111 /*                                                                           */
112 /* ---- \ghd{ I/O } -------------------------------------------------------- */
113 /*                                                                           */
114 /* ========================================================================= */
115 
116 /* ------------------------------------------------------------------------- */
117 
bbf_L06Dns3x3Ftr_memSize(struct bbs_Context * cpA,const struct bbf_L06Dns3x3Ftr * ptrA)118 uint32 bbf_L06Dns3x3Ftr_memSize( struct bbs_Context* cpA,
119 							     const struct bbf_L06Dns3x3Ftr* ptrA )
120 {
121 	uint32 memSizeL = bbs_SIZEOF16( uint32 ) +
122 					  bbs_SIZEOF16( uint32 ); /* version */
123 
124 	memSizeL += bbf_Feature_memSize( cpA, &ptrA->baseE );
125 	memSizeL += bbs_UInt32Arr_memSize( cpA, &ptrA->dataArrE );
126 	memSizeL += bbs_SIZEOF16( ptrA->activityFactorE );
127 
128 	return memSizeL;
129 }
130 
131 /* ------------------------------------------------------------------------- */
132 
bbf_L06Dns3x3Ftr_memWrite(struct bbs_Context * cpA,const struct bbf_L06Dns3x3Ftr * ptrA,uint16 * memPtrA)133 uint32 bbf_L06Dns3x3Ftr_memWrite( struct bbs_Context* cpA,
134 							      const struct bbf_L06Dns3x3Ftr* ptrA,
135 								  uint16* memPtrA )
136 {
137 	uint32 memSizeL = bbf_L06Dns3x3Ftr_memSize( cpA, ptrA );
138 	memPtrA += bbs_memWrite32( &memSizeL, memPtrA );
139 	memPtrA += bbs_memWriteUInt32( bbf_L06_DNS_3X3_FTR_VERSION, memPtrA );
140 	memPtrA += bbf_Feature_memWrite( cpA, &ptrA->baseE, memPtrA );
141 	memPtrA += bbs_UInt32Arr_memWrite( cpA, &ptrA->dataArrE, memPtrA );
142 	memPtrA += bbs_memWrite32( &ptrA->activityFactorE, memPtrA );
143 	return memSizeL;
144 }
145 
146 /* ------------------------------------------------------------------------- */
147 
bbf_L06Dns3x3Ftr_memRead(struct bbs_Context * cpA,struct bbf_L06Dns3x3Ftr * ptrA,const uint16 * memPtrA,struct bbs_MemTbl * mtpA)148 uint32 bbf_L06Dns3x3Ftr_memRead( struct bbs_Context* cpA,
149 							     struct bbf_L06Dns3x3Ftr* ptrA,
150 							     const uint16* memPtrA,
151 							     struct bbs_MemTbl* mtpA )
152 {
153 	uint32 memSizeL, versionL;
154 	struct bbs_MemTbl memTblL = *mtpA;
155 	struct bbs_MemSeg* espL = bbs_MemTbl_fastestSegPtr( cpA, &memTblL, 0 );
156 	if( bbs_Context_error( cpA ) ) return 0;
157 	memPtrA += bbs_memRead32( &memSizeL, memPtrA );
158 	memPtrA += bbs_memReadVersion32( cpA, &versionL, bbf_L06_DNS_3X3_FTR_VERSION, memPtrA );
159 	memPtrA += bbf_Feature_memRead( cpA, &ptrA->baseE, memPtrA );
160 	memPtrA += bbs_UInt32Arr_memRead( cpA, &ptrA->dataArrE, memPtrA, espL );
161 	memPtrA += bbs_memRead32( &ptrA->activityFactorE, memPtrA );
162 	if( memSizeL != bbf_L06Dns3x3Ftr_memSize( cpA, ptrA ) )
163 	{
164 		bbs_ERR0( bbs_ERR_CORRUPT_DATA, "uint32 bbf_L06Dns3x3Ftr_memRead( struct bem_ScanGradientMove* ptrA, const uint16* memPtrA ):\n"
165 			        "size mismatch" );
166 		return 0;
167 	}
168 
169 	return memSizeL;
170 }
171 
172 /* ------------------------------------------------------------------------- */
173 
174 /* ========================================================================= */
175 /*                                                                           */
176 /* ---- \ghd{ exec functions } --------------------------------------------- */
177 /*                                                                           */
178 /* ========================================================================= */
179 
180 /* ------------------------------------------------------------------------- */
181 
bbf_L06Dns3x3Ftr_activity(const struct bbf_Feature * ptrA,const uint32 * patchA)182 int32 bbf_L06Dns3x3Ftr_activity( const struct bbf_Feature* ptrA, const uint32* patchA )
183 {
184 	const struct bbf_L06Dns3x3Ftr* ptrL = ( struct bbf_L06Dns3x3Ftr* )ptrA;
185 
186 	uint32 wL = ptrL->baseE.patchWidthE - 2;
187     uint32 hL = ptrL->baseE.patchHeightE - 2;
188 	const uint32* dataPtrL = ptrL->dataArrE.arrPtrE;
189 	uint32 iL;
190 
191 	uint32 borderMaskL = ( ( uint32 )1 << hL ) - 1;
192 
193 	uint32 sL[ 9 ];
194 	uint32 bL[ 6 ] = { 0, 0, 0, 0, 0, 0 }; /* bit sum */
195 
196 	for( iL = 0; iL < wL; iL++ )
197 	{
198 		uint32 vL, mL, tL; /* bit sum and thresholds */
199 
200 		uint32 s1L = patchA[ iL     ];
201 		uint32 s2L = patchA[ iL + 1 ];
202 		uint32 s3L = patchA[ iL + 2 ];
203 
204 		/* comparison of pixels with patchHeightE - 3 features */
205 		sL[ 0 ] = ( ( s1L      ) ^ dataPtrL[ 0 ] ) & borderMaskL;
206 		sL[ 1 ] = ( ( s1L >> 1 ) ^ dataPtrL[ 1 ] ) & borderMaskL;
207 		sL[ 2 ] = ( ( s1L >> 2 ) ^ dataPtrL[ 2 ] ) & borderMaskL;
208 
209 		sL[ 3 ] = ( ( s2L      ) ^ dataPtrL[ 3 ] ) & borderMaskL;
210 		sL[ 4 ] = ( ( s2L >> 1 ) ^ dataPtrL[ 4 ] ) & borderMaskL;
211 		sL[ 5 ] = ( ( s2L >> 2 ) ^ dataPtrL[ 5 ] ) & borderMaskL;
212 
213 		sL[ 6 ] = ( ( s3L      ) ^ dataPtrL[ 6 ] ) & borderMaskL;
214 		sL[ 7 ] = ( ( s3L >> 1 ) ^ dataPtrL[ 7 ] ) & borderMaskL;
215 		sL[ 8 ] = ( ( s3L >> 2 ) ^ dataPtrL[ 8 ] ) & borderMaskL;
216 
217 		/* parallel bit counting of patchHeightE - 2 features */
218 
219 		vL = 0;
220 
221 		mL = ( ( sL[ 0 ] & 0x11111111 ) + ( sL[ 1 ] & 0x11111111 ) + ( sL[ 2 ] & 0x11111111 ) +
222 		       ( sL[ 3 ] & 0x11111111 ) + ( sL[ 4 ] & 0x11111111 ) + ( sL[ 5 ] & 0x11111111 ) +
223 		  	   ( sL[ 6 ] & 0x11111111 ) + ( sL[ 7 ] & 0x11111111 ) + ( sL[ 8 ] & 0x11111111 ) );
224 
225 		tL = dataPtrL[ 9 ];
226 
227 		/* compare with thresholds and store results in vL */
228 		vL |= ( ( (   mL        & 0x0F0F0F0F ) + (   tL        & 0x0F0F0F0F ) ) & 0x10101010 ) >> 4;
229 		vL |= ( ( ( ( mL >> 4 ) & 0x0F0F0F0F ) + ( ( tL >> 4 ) & 0x0F0F0F0F ) ) & 0x10101010 );
230 
231 		/* shift values to prevent overflow in next summation */
232 		sL[ 0 ] >>= 1; 	sL[ 1 ] >>= 1; sL[ 2 ] >>= 1;
233 		sL[ 3 ] >>= 1; 	sL[ 4 ] >>= 1; sL[ 5 ] >>= 1;
234 		sL[ 6 ] >>= 1; 	sL[ 7 ] >>= 1; sL[ 8 ] >>= 1;
235 
236 		mL = ( ( sL[ 0 ] & 0x11111111 ) + ( sL[ 1 ] & 0x11111111 ) + ( sL[ 2 ] & 0x11111111 ) +
237 		       ( sL[ 3 ] & 0x11111111 ) + ( sL[ 4 ] & 0x11111111 ) + ( sL[ 5 ] & 0x11111111 ) +
238 		  	   ( sL[ 6 ] & 0x11111111 ) + ( sL[ 7 ] & 0x11111111 ) + ( sL[ 8 ] & 0x11111111 ) );
239 
240 		tL = dataPtrL[ 10 ];
241 
242 		/* compare with thresholds and store results in vL */
243 		vL |= ( ( (   mL        & 0x0F0F0F0F ) + (   tL        & 0x0F0F0F0F ) ) & 0x10101010 ) >> 3;
244 		vL |= ( ( ( ( mL >> 4 ) & 0x0F0F0F0F ) + ( ( tL >> 4 ) & 0x0F0F0F0F ) ) & 0x10101010 ) << 1;
245 
246 		mL = ( ( sL[ 0 ] & 0x02222222 ) + ( sL[ 1 ] & 0x02222222 ) + ( sL[ 2 ] & 0x02222222 ) +
247 		       ( sL[ 3 ] & 0x02222222 ) + ( sL[ 4 ] & 0x02222222 ) + ( sL[ 5 ] & 0x02222222 ) +
248 		  	   ( sL[ 6 ] & 0x02222222 ) + ( sL[ 7 ] & 0x02222222 ) + ( sL[ 8 ] & 0x02222222 ) ) >> 1;
249 
250 		tL = dataPtrL[ 11 ];
251 
252 		/* compare with thresholds and store results in vL */
253 		vL |= ( ( (   mL        & 0x0F0F0F0F ) + (   tL        & 0x0F0F0F0F ) ) & 0x10101010 ) >> 2;
254 		vL |= ( ( ( ( mL >> 4 ) & 0x0F0F0F0F ) + ( ( tL >> 4 ) & 0x0F0F0F0F ) ) & 0x10101010 ) << 2;
255 
256 		mL = ( ( sL[ 0 ] & 0x04444444 ) + ( sL[ 1 ] & 0x04444444 ) + ( sL[ 2 ] & 0x04444444 ) +
257 		       ( sL[ 3 ] & 0x04444444 ) + ( sL[ 4 ] & 0x04444444 ) + ( sL[ 5 ] & 0x04444444 ) +
258 		  	   ( sL[ 6 ] & 0x04444444 ) + ( sL[ 7 ] & 0x04444444 ) + ( sL[ 8 ] & 0x04444444 ) ) >> 2;
259 
260 		tL = dataPtrL[ 12 ];
261 
262 		/* compare with thresholds and store results in vL */
263 		vL |= ( ( (   mL        & 0x0F0F0F0F ) + (   tL        & 0x0F0F0F0F ) ) & 0x10101010 ) >> 1;
264 		vL |= ( ( ( ( mL >> 4 ) & 0x0F0F0F0F ) + ( ( tL >> 4 ) & 0x0F0F0F0F ) ) & 0x10101010 ) << 3;
265 
266 		vL = ~vL;
267 
268 		/* mask out and count bits */
269 		{
270 			uint32 vmL;
271 			vmL = vL & dataPtrL[ 13 ];
272 			bL[ 0 ] += bbf_BIT_SUM_32( vmL );
273 			vmL = vL & dataPtrL[ 14 ];
274 			bL[ 1 ] += bbf_BIT_SUM_32( vmL );
275 			vmL = vL & dataPtrL[ 15 ];
276 			bL[ 2 ] += bbf_BIT_SUM_32( vmL );
277 			vmL = vL & dataPtrL[ 16 ];
278 			bL[ 3 ] += bbf_BIT_SUM_32( vmL );
279 			vmL = vL & dataPtrL[ 17 ];
280 			bL[ 4 ] += bbf_BIT_SUM_32( vmL );
281 			vmL = vL & dataPtrL[ 18 ];
282 			bL[ 5 ] += bbf_BIT_SUM_32( vmL );
283 		}
284 
285 		dataPtrL += 19;
286 	}
287 
288 	/* compute final activity */
289 	{
290 		uint32 actL = ( ( bL[ 0 ] << 5 ) + ( bL[ 1 ] << 4 ) + ( bL[ 2 ] << 3 ) +
291 				        ( bL[ 3 ] << 2 ) + ( bL[ 4 ] << 1 ) + ( bL[ 5 ]      ) );
292 		return actL * ptrL->activityFactorE;
293 	}
294 }
295 
296 /* ------------------------------------------------------------------------- */
297 
298 /* ========================================================================= */
299 
300