|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Text recognition algorithm implementation. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "HtiTextRcg.h" |
|
20 #include <HtiLogging.h> |
|
21 |
|
22 const static TInt KDefaultStrategy = EHintEdge; |
|
23 |
|
24 TInt CompareTPoint(const TPoint& aP1,const TPoint& aP2) |
|
25 { |
|
26 //this functions is used only to avoid equal points when creating FGA or BGA |
|
27 //so only equality of points is important, order doesnt matter |
|
28 if ( aP1.iY == aP2.iY ) |
|
29 return aP1.iX - aP2.iX; |
|
30 return aP1.iY - aP2.iY; |
|
31 } |
|
32 |
|
33 CHtiTextRcg::CHtiTextRcg() |
|
34 { |
|
35 iAvgDiffMin = KDefaultAvgDiffMin; |
|
36 //minimal SS for foreground, with plain color should be 0 |
|
37 iFgSSMin = KDefaultFgSSMin; |
|
38 iFgAvgDiffMin = KDefaultFgAvgDiffMin; |
|
39 |
|
40 iFGAAmount = KDefaultFGAAmount; |
|
41 iBGAAmount = KDefaultBGAAmount; |
|
42 |
|
43 SetHint(KDefaultStrategy); |
|
44 } |
|
45 |
|
46 CHtiTextRcg::~CHtiTextRcg() |
|
47 { |
|
48 iFGASet.Close(); |
|
49 iBGASet.Close(); |
|
50 } |
|
51 |
|
52 void CHtiTextRcg::SetHint(TInt aHint) |
|
53 { |
|
54 //selects strategy and algorithm parameters |
|
55 switch ( aHint ) |
|
56 { |
|
57 case EHintEdge: |
|
58 { |
|
59 //AA strategy |
|
60 iCurrentStrategy = EHintEdge; |
|
61 } |
|
62 break; |
|
63 case EHintNone: |
|
64 default: |
|
65 { |
|
66 //default strategy |
|
67 iCurrentStrategy = EHintNone; |
|
68 } |
|
69 } |
|
70 } |
|
71 |
|
72 |
|
73 TBool CHtiTextRcg::RecognizeTextL(CFbsBitmap* aScreenshot, |
|
74 const TDesC& aText, |
|
75 const CFont* aFont, |
|
76 TRect& aResult) |
|
77 { |
|
78 HTI_LOG_FUNC_IN("RecognizeTextL"); |
|
79 TInt returnValue = KWorstCase; |
|
80 |
|
81 CFbsBitmap* gray = ColorDownL(aScreenshot); |
|
82 CleanupStack::PushL(gray); |
|
83 |
|
84 switch ( iCurrentStrategy ) |
|
85 { |
|
86 case EHintEdge: |
|
87 { |
|
88 returnValue = RecognizeAAL(gray,aText, aFont, aResult); |
|
89 } |
|
90 break; |
|
91 case EHintNone: |
|
92 default: |
|
93 { |
|
94 returnValue = RecognizeBinL(gray,aText, aFont, aResult); |
|
95 } |
|
96 } |
|
97 |
|
98 CleanupStack::PopAndDestroy(gray); |
|
99 HTI_LOG_FUNC_OUT("RecognizeTextL"); |
|
100 return returnValue < KSuccessThresold; |
|
101 } |
|
102 |
|
103 TInt CHtiTextRcg::RecognizeBinL(CFbsBitmap* aScreenshot, |
|
104 const TDesC& aText, |
|
105 const CFont* aFont, |
|
106 TRect& aResult) |
|
107 { |
|
108 HTI_LOG_FUNC_IN("RecognizeBinL"); |
|
109 CFbsBitmap* searchFirstLetter = GetTextBitmapL(aText, aFont, 1); |
|
110 CleanupStack::PushL(searchFirstLetter); |
|
111 HTI_LOG_FORMAT("pattern size w %d", searchFirstLetter->SizeInPixels().iWidth); |
|
112 HTI_LOG_FORMAT("pattern size h %d", searchFirstLetter->SizeInPixels().iHeight); |
|
113 |
|
114 if ( !AnalyzePatternL(searchFirstLetter) ) |
|
115 { |
|
116 CleanupStack::PopAndDestroy(searchFirstLetter); |
|
117 return KWorstCase; |
|
118 } |
|
119 CFbsBitmap* searchText = GetTextBitmapL(aText, aFont); |
|
120 CleanupStack::PushL(searchText); |
|
121 |
|
122 //search range (0,0) - (reg.Size() - searchText.SizeInPixels) |
|
123 TPoint end(aScreenshot->SizeInPixels().iWidth, aScreenshot->SizeInPixels().iHeight); |
|
124 end -= searchText->SizeInPixels(); |
|
125 end += TPoint(1,1); |
|
126 |
|
127 //search itself |
|
128 for ( TPoint p( 0, 0 ); p.iY < end.iY; p.iY++ ) |
|
129 { |
|
130 for ( p.iX = 0; p.iX < end.iX; p.iX++ ) |
|
131 { |
|
132 TInt t = ImageDiffBinSampleL(aScreenshot, p, searchFirstLetter); |
|
133 if ( t == 0 ) |
|
134 { |
|
135 //check full word |
|
136 TInt wordD = ImageDiffBinFullL(aScreenshot, p, searchText); |
|
137 if ( wordD == 0 ) |
|
138 { |
|
139 aResult.iTl.iX = p.iX; |
|
140 aResult.iTl.iY = p.iY; |
|
141 aResult.SetSize(searchText->SizeInPixels()); |
|
142 CleanupStack::PopAndDestroy(searchText); |
|
143 CleanupStack::PopAndDestroy(searchFirstLetter); |
|
144 HTI_LOG_FUNC_OUT("RecognizeBinL"); |
|
145 return 0; |
|
146 } |
|
147 } |
|
148 } |
|
149 } |
|
150 CleanupStack::PopAndDestroy(searchText); |
|
151 CleanupStack::PopAndDestroy(searchFirstLetter); |
|
152 |
|
153 HTI_LOG_FUNC_OUT("RecognizeBinL"); |
|
154 return KWorstCase; |
|
155 } |
|
156 |
|
157 |
|
158 TInt CHtiTextRcg::RecognizeAAL(CFbsBitmap* aScreenshot, |
|
159 const TDesC& aText, |
|
160 const CFont* aFont, |
|
161 TRect& aResult) |
|
162 { |
|
163 HTI_LOG_FUNC_IN("RecognizeAAL"); |
|
164 CFbsBitmap* searchFirstLetter = GetTextBitmapL(aText, aFont, 1); |
|
165 CleanupStack::PushL(searchFirstLetter); |
|
166 if ( !AnalyzePatternL(searchFirstLetter) ) |
|
167 { |
|
168 CleanupStack::PopAndDestroy(searchFirstLetter); |
|
169 return KWorstCase; |
|
170 } |
|
171 |
|
172 CFbsBitmap* searchText = GetTextBitmapL(aText, aFont); |
|
173 CleanupStack::PushL(searchText); |
|
174 |
|
175 |
|
176 //search range (0,0) - (reg.Size() - searchText.SizeInPixels) |
|
177 TPoint end(aScreenshot->SizeInPixels().iWidth, aScreenshot->SizeInPixels().iHeight); |
|
178 end -= searchText->SizeInPixels(); |
|
179 end += TPoint(1,1); |
|
180 |
|
181 //search itself |
|
182 TInt min = KSuccessThresold; |
|
183 TInt wordMin = KSuccessThresold; |
|
184 |
|
185 for ( TPoint p( 0, 0 ); p.iY < end.iY; p.iY++ ) |
|
186 { |
|
187 for ( p.iX = 0; p.iX < end.iX; p.iX++ ) |
|
188 { |
|
189 TInt t = ImageDiffAASampleL(aScreenshot, p, searchFirstLetter); |
|
190 if ( t < min ) |
|
191 { |
|
192 //check full word |
|
193 TInt wordD = ImageDiffAAFullL(aScreenshot, p, searchText); |
|
194 if ( wordD < wordMin ) |
|
195 { |
|
196 wordMin = wordD; |
|
197 min = t; |
|
198 aResult.iTl.iX = p.iX; |
|
199 aResult.iTl.iY = p.iY; |
|
200 aResult.SetSize(searchText->SizeInPixels()); |
|
201 if ( wordMin == 0 ) |
|
202 { |
|
203 CleanupStack::PopAndDestroy(searchText); |
|
204 CleanupStack::PopAndDestroy(searchFirstLetter); |
|
205 HTI_LOG_FUNC_OUT("RecognizeAAL"); |
|
206 return 0; |
|
207 } |
|
208 } |
|
209 } |
|
210 } |
|
211 } |
|
212 |
|
213 CleanupStack::PopAndDestroy(searchText); |
|
214 CleanupStack::PopAndDestroy(searchFirstLetter); |
|
215 |
|
216 HTI_LOG_FUNC_OUT("RecognizeAAL"); |
|
217 |
|
218 return wordMin; |
|
219 } |
|
220 |
|
221 TBool CHtiTextRcg::AnalyzePatternL(CFbsBitmap * aPattern) |
|
222 { |
|
223 HTI_LOG_FUNC_IN("AnalyzePatternL"); |
|
224 if ( aPattern->SizeInPixels().iWidth == 0 || |
|
225 aPattern->SizeInPixels().iHeight == 0 ) |
|
226 { |
|
227 return EFalse; |
|
228 } |
|
229 //points are selected as follow |
|
230 //take pair of FG-BG points which located next to each other |
|
231 MinMax(aPattern, |
|
232 iMaskFgColor, //min, black font |
|
233 iMaskBgColor);//max, white bg |
|
234 |
|
235 if ( iMaskFgColor == iMaskBgColor ) //pattern is empty |
|
236 { |
|
237 return EFalse; |
|
238 } |
|
239 |
|
240 TLinearOrder<TPoint> pointOrder(CompareTPoint); |
|
241 TSize borders = aPattern->SizeInPixels(); |
|
242 iFGASet.Reset(); |
|
243 iBGASet.Reset(); |
|
244 |
|
245 TBitmapUtil bmpIterator(aPattern); |
|
246 //lock bitmap |
|
247 bmpIterator.Begin( TPoint(0,0)); |
|
248 |
|
249 //first take center lines and find take at least two pairs |
|
250 //vertical1 |
|
251 TPoint startPoint(borders.iWidth/2, 0); |
|
252 bmpIterator.SetPos(startPoint); |
|
253 |
|
254 TInt lastColor = bmpIterator.GetPixel()&0xff; |
|
255 TInt lastColorPos = 0; |
|
256 bmpIterator.IncYPos(); |
|
257 TInt i=1; |
|
258 TInt found = 0; |
|
259 |
|
260 while ( found < 2 && i < borders.iHeight ) |
|
261 { |
|
262 TInt c = bmpIterator.GetPixel()&0xff; |
|
263 |
|
264 if ( lastColor != c ) |
|
265 { |
|
266 if ( c == iMaskFgColor ) |
|
267 { |
|
268 iFGASet.InsertInOrder(TPoint(startPoint.iX, i), pointOrder); |
|
269 iBGASet.InsertInOrder(TPoint(startPoint.iX, lastColorPos), pointOrder); |
|
270 lastColor = c; |
|
271 lastColorPos = i; |
|
272 ++found; |
|
273 } |
|
274 else if ( c == iMaskBgColor ) |
|
275 { |
|
276 iBGASet.InsertInOrder(TPoint(startPoint.iX, i), pointOrder); |
|
277 iFGASet.InsertInOrder(TPoint(startPoint.iX, lastColorPos), pointOrder); |
|
278 lastColor = c; |
|
279 lastColorPos = i; |
|
280 ++found; |
|
281 } |
|
282 } |
|
283 else |
|
284 { |
|
285 lastColorPos = i; |
|
286 } |
|
287 |
|
288 ++i; |
|
289 bmpIterator.IncYPos(); |
|
290 } |
|
291 |
|
292 //horizontal1 |
|
293 startPoint.SetXY(0,borders.iHeight/2); |
|
294 bmpIterator.SetPos(startPoint); |
|
295 lastColor = bmpIterator.GetPixel()&0xff; |
|
296 bmpIterator.IncXPos(); |
|
297 i=1; |
|
298 found = 0; |
|
299 lastColorPos = 0; |
|
300 |
|
301 while ( found < 2 && i < borders.iWidth ) |
|
302 { |
|
303 TInt c = bmpIterator.GetPixel()&0xff; |
|
304 |
|
305 if ( lastColor != c ) |
|
306 { |
|
307 if ( c == iMaskFgColor ) |
|
308 { |
|
309 iFGASet.InsertInOrder(TPoint(i, startPoint.iY), pointOrder); |
|
310 iBGASet.InsertInOrder(TPoint(lastColorPos, startPoint.iY), pointOrder); |
|
311 lastColor = c; |
|
312 lastColorPos = i; |
|
313 ++found; |
|
314 } |
|
315 else if ( c == iMaskBgColor ) |
|
316 { |
|
317 iBGASet.InsertInOrder(TPoint(i, startPoint.iY), pointOrder); |
|
318 iFGASet.InsertInOrder(TPoint(lastColorPos, startPoint.iY), pointOrder); |
|
319 lastColor = c; |
|
320 lastColorPos = i; |
|
321 ++found; |
|
322 } |
|
323 } |
|
324 else |
|
325 { |
|
326 lastColorPos = i; |
|
327 } |
|
328 ++i; |
|
329 bmpIterator.IncXPos(); |
|
330 } |
|
331 |
|
332 //unlock bitmap |
|
333 bmpIterator.End(); |
|
334 |
|
335 iFGAAmount = iFGASet.Count(); |
|
336 iBGAAmount = iBGASet.Count(); |
|
337 |
|
338 HTI_LOG_FUNC_OUT("AnalyzePatternL"); |
|
339 return ETrue; |
|
340 |
|
341 } |
|
342 |
|
343 TInt CHtiTextRcg::ImageDiffAASampleL(CFbsBitmap * aBitmap1, TPoint aOrigin1, |
|
344 CFbsBitmap * aBitmap2) |
|
345 { |
|
346 |
|
347 if (iFGASet.Count()==0 || iBGASet.Count()==0) |
|
348 return KWorstCase; |
|
349 |
|
350 TSize aSize = aBitmap2->SizeInPixels(); |
|
351 |
|
352 //straight average difference |
|
353 TBitmapUtil bmpIterator1(aBitmap1); |
|
354 TBitmapUtil bmpIterator2(aBitmap2); |
|
355 |
|
356 bmpIterator1.Begin( aOrigin1 ); |
|
357 |
|
358 //1. check FGA points are equal |
|
359 bmpIterator1.SetPos( aOrigin1 + iFGASet[0]); |
|
360 iTestFgColor = bmpIterator1.GetPixel()&0xff; |
|
361 |
|
362 for ( TInt i = 1; i < iFGAAmount;++i ) |
|
363 { |
|
364 bmpIterator1.SetPos( aOrigin1 + iFGASet[i]); |
|
365 TInt c = bmpIterator1.GetPixel()&0xff; |
|
366 if ( Abs(c-iTestFgColor) > iFgAvgDiffMin ) |
|
367 { |
|
368 bmpIterator2.End(); |
|
369 bmpIterator1.End(); |
|
370 return KWorstCase; |
|
371 } |
|
372 } |
|
373 // if we are here all FGA points are equal to colorFGA |
|
374 //2. check that avg BGA point value is not equal to colorFGA |
|
375 iTestBgColor = 0; |
|
376 for ( TInt i = 0; i < iBGAAmount; ++i ) |
|
377 { |
|
378 bmpIterator1.SetPos( aOrigin1 + iBGASet[i]); |
|
379 iTestBgColor += bmpIterator1.GetPixel()&0xff; |
|
380 } |
|
381 iTestBgColor /= iBGAAmount; |
|
382 //if difference is too small leave with false |
|
383 if ( Abs(iTestBgColor-iTestFgColor) < iAvgDiffMin ) |
|
384 { |
|
385 bmpIterator2.End(); |
|
386 bmpIterator1.End(); |
|
387 return KWorstCase; |
|
388 } |
|
389 |
|
390 //all checking based on FGA and BGA are correct, chance to have match |
|
391 //3. calculate sum of diff between colorFGA and ALL FG points |
|
392 bmpIterator1.End(); |
|
393 bmpIterator1.Begin( aOrigin1 ); |
|
394 bmpIterator2.Begin( TPoint(0,0), bmpIterator1 ); |
|
395 |
|
396 TInt nofF = 0; |
|
397 TInt sum = 0; |
|
398 TBool iterFlag = EFalse; |
|
399 |
|
400 TInt rowDelta = 2; |
|
401 TInt columnDelta = 1; |
|
402 |
|
403 TBool intellFlagBG; |
|
404 iTestNormCoef = (Abs(iMaskFgColor-iMaskBgColor)<<KNormCoefAcc)/Abs(iTestFgColor-iTestBgColor); |
|
405 |
|
406 for ( TInt i = 0; i < aSize.iHeight; i += rowDelta ) |
|
407 { |
|
408 intellFlagBG = EFalse; |
|
409 for ( TInt j = 0; j < aSize.iWidth; j += columnDelta ) |
|
410 { |
|
411 TInt c1 = (bmpIterator1.GetPixel())&0xff; |
|
412 TInt c2 = (bmpIterator2.GetPixel())&0xff; |
|
413 |
|
414 if ( c2 != iMaskBgColor ) // if foreground |
|
415 { |
|
416 if ( c2 == iMaskFgColor ) //should be "pure" FG |
|
417 { |
|
418 if ( Abs( c1 - iTestFgColor ) > iFgAvgDiffMin ) |
|
419 { |
|
420 bmpIterator2.End(); |
|
421 bmpIterator1.End(); |
|
422 return KWorstCase; |
|
423 } |
|
424 intellFlagBG = ETrue; |
|
425 } |
|
426 else if ( intellFlagBG ) // AA pixels |
|
427 { |
|
428 //calculate diff. in relative diff in aa pixel |
|
429 //in mask and searh image |
|
430 //based on assumtion that aa pixels color |
|
431 // relative to fg color should correlate |
|
432 TInt normD = (Abs(c1-iTestFgColor)*iTestNormCoef)>>KNormCoefAcc; |
|
433 sum += Abs(Abs(iMaskFgColor-c2) - normD ); |
|
434 |
|
435 ++nofF; |
|
436 intellFlagBG = EFalse; |
|
437 |
|
438 } |
|
439 } |
|
440 for ( TInt l = 0; l < columnDelta; l++ ) |
|
441 { |
|
442 if ( iterFlag ) |
|
443 { |
|
444 bmpIterator1.DecXPos(); |
|
445 bmpIterator2.DecXPos(); |
|
446 } |
|
447 else |
|
448 { |
|
449 bmpIterator1.IncXPos(); |
|
450 bmpIterator2.IncXPos(); |
|
451 } |
|
452 } |
|
453 } |
|
454 |
|
455 for ( int k = 0; k < rowDelta; k++ ) |
|
456 { |
|
457 bmpIterator1.IncYPos(); |
|
458 bmpIterator2.IncYPos(); |
|
459 } |
|
460 for ( int l = 0; l < columnDelta; l++ ) |
|
461 { |
|
462 if ( iterFlag ) |
|
463 { |
|
464 bmpIterator1.IncXPos(); |
|
465 bmpIterator2.IncXPos(); |
|
466 } |
|
467 else |
|
468 { |
|
469 bmpIterator1.DecXPos(); |
|
470 bmpIterator2.DecXPos(); |
|
471 } |
|
472 } |
|
473 iterFlag = !iterFlag; |
|
474 } |
|
475 |
|
476 |
|
477 bmpIterator2.End(); |
|
478 bmpIterator1.End(); |
|
479 |
|
480 if ( nofF == 0 ) |
|
481 { |
|
482 return 0; |
|
483 } |
|
484 return sum / nofF; |
|
485 } |
|
486 |
|
487 |
|
488 TInt CHtiTextRcg::ImageDiffAAFullL(CFbsBitmap * aBitmap1, TPoint aOrigin1, |
|
489 CFbsBitmap * aBitmap2) |
|
490 { |
|
491 TSize aSize = aBitmap2->SizeInPixels(); |
|
492 //straight average difference |
|
493 TBitmapUtil bmpIterator1(aBitmap1); |
|
494 TBitmapUtil bmpIterator2(aBitmap2); |
|
495 |
|
496 bmpIterator1.Begin( aOrigin1 ); |
|
497 bmpIterator2.Begin( TPoint(0,0), bmpIterator1 ); |
|
498 |
|
499 TInt nofF = 0; |
|
500 TInt sumF = 0; |
|
501 TBool intellFlagBG; |
|
502 TBool iterFlag = EFalse; |
|
503 TInt rowDelta = 2; |
|
504 TInt columnDelta = 1; |
|
505 |
|
506 for ( TInt i = 0; i < aSize.iHeight; i += rowDelta ) |
|
507 { |
|
508 intellFlagBG = EFalse; |
|
509 for ( TInt j = 0; j < aSize.iWidth; j += columnDelta ) |
|
510 { |
|
511 TInt c1 = ( bmpIterator1.GetPixel() ) & 0xff; |
|
512 TInt c2 = ( bmpIterator2.GetPixel() ) & 0xff; |
|
513 |
|
514 if ( c2 != iMaskBgColor ) // if foreground |
|
515 { |
|
516 if ( c2 == iMaskFgColor ) //should be pure FG |
|
517 { |
|
518 if ( Abs(c1 - iTestFgColor) > iFgAvgDiffMin ) |
|
519 { |
|
520 bmpIterator2.End(); |
|
521 bmpIterator1.End(); |
|
522 return KWorstCase; |
|
523 } |
|
524 intellFlagBG = ETrue; |
|
525 } |
|
526 else if ( intellFlagBG ) // AA pixels |
|
527 { |
|
528 //calculate diff. in relative diff in aa pixel |
|
529 //in mask and searh image |
|
530 //based on assumtion that aa pixels color |
|
531 // relative to fg color should correlate |
|
532 TInt normD = (Abs(c1-iTestFgColor)*iTestNormCoef)>>KNormCoefAcc; |
|
533 sumF += Abs(Abs(iMaskFgColor-c2) - normD ); |
|
534 |
|
535 ++nofF; |
|
536 intellFlagBG = EFalse; |
|
537 } |
|
538 } |
|
539 for ( TInt l = 0; l < columnDelta; l++ ) |
|
540 { |
|
541 if ( iterFlag ) |
|
542 { |
|
543 bmpIterator1.DecXPos(); |
|
544 bmpIterator2.DecXPos(); |
|
545 } |
|
546 else |
|
547 { |
|
548 bmpIterator1.IncXPos(); |
|
549 bmpIterator2.IncXPos(); |
|
550 } |
|
551 } |
|
552 } |
|
553 |
|
554 for ( TInt k = 0; k < rowDelta; k++ ) |
|
555 { |
|
556 bmpIterator1.IncYPos(); |
|
557 bmpIterator2.IncYPos(); |
|
558 } |
|
559 for ( TInt l = 0; l < columnDelta; l++ ) |
|
560 { |
|
561 if ( iterFlag ) |
|
562 { |
|
563 bmpIterator1.IncXPos(); |
|
564 bmpIterator2.IncXPos(); |
|
565 } |
|
566 else |
|
567 { |
|
568 bmpIterator1.DecXPos(); |
|
569 bmpIterator2.DecXPos(); |
|
570 } |
|
571 } |
|
572 iterFlag = !iterFlag; |
|
573 } |
|
574 bmpIterator2.End(); |
|
575 bmpIterator1.End(); |
|
576 |
|
577 if ( nofF == 0 ) |
|
578 return 0; |
|
579 |
|
580 return sumF/nofF; |
|
581 } |
|
582 |
|
583 TInt CHtiTextRcg::ImageDiffBinSampleL(CFbsBitmap * aBitmap1, TPoint aOrigin1, |
|
584 CFbsBitmap * aBitmap2) |
|
585 { |
|
586 TSize aSize = aBitmap2->SizeInPixels(); |
|
587 if ( iFGASet.Count() == 0 || iBGASet.Count() == 0 ) |
|
588 return KWorstCase; |
|
589 |
|
590 //straight average difference |
|
591 TBitmapUtil bmpIterator1(aBitmap1); |
|
592 TBitmapUtil bmpIterator2(aBitmap2); |
|
593 |
|
594 bmpIterator1.Begin( aOrigin1 ); |
|
595 |
|
596 //1. check FGA points are equal |
|
597 bmpIterator1.SetPos( aOrigin1 + iFGASet[0]); |
|
598 TInt colorFGA = bmpIterator1.GetPixel()&0xff; |
|
599 |
|
600 for ( TInt i = 1; i < iFGAAmount; ++i ) |
|
601 { |
|
602 bmpIterator1.SetPos( aOrigin1 + iFGASet[i] ); |
|
603 TInt c = bmpIterator1.GetPixel()&0xff; |
|
604 if ( c != colorFGA ) |
|
605 { |
|
606 bmpIterator2.End(); |
|
607 bmpIterator1.End(); |
|
608 return KWorstCase; |
|
609 } |
|
610 } |
|
611 // if we are here all FGA points are equal to colorFGA |
|
612 //2. check that avg BGA point value is not equal to colorFGA |
|
613 TInt avgColorBGA = 0; |
|
614 for ( TInt i = 0; i < iBGAAmount; ++i ) |
|
615 { |
|
616 bmpIterator1.SetPos( aOrigin1 + iBGASet[i] ); |
|
617 avgColorBGA += bmpIterator1.GetPixel() & 0xff; |
|
618 } |
|
619 avgColorBGA /= iBGAAmount; |
|
620 //if difference is too small leave with false |
|
621 if ( Abs(avgColorBGA-colorFGA) < iAvgDiffMin ) |
|
622 { |
|
623 bmpIterator2.End(); |
|
624 bmpIterator1.End(); |
|
625 return KWorstCase; |
|
626 } |
|
627 |
|
628 //all checking based on FGA and BGA are correct, chance to have math |
|
629 //3. calculate sum of diff between colorFGA and ALL FG points |
|
630 bmpIterator1.End(); |
|
631 bmpIterator1.Begin( aOrigin1 ); |
|
632 bmpIterator2.Begin( TPoint(0,0), bmpIterator1 ); |
|
633 |
|
634 TBool iterFlag = EFalse; |
|
635 |
|
636 TInt rowDelta = 1; |
|
637 TInt columnDelta = 1; |
|
638 |
|
639 for ( TInt i = 0; i < aSize.iHeight; i += rowDelta ) |
|
640 { |
|
641 for ( TInt j = 0; j < aSize.iWidth; j += columnDelta ) |
|
642 { |
|
643 TInt c1 = ( bmpIterator1.GetPixel() ) & 0xff; |
|
644 TInt c2 = ( bmpIterator2.GetPixel() ) & 0xff; |
|
645 |
|
646 if ( c2 == iMaskFgColor ) // if foreground |
|
647 { |
|
648 if ( colorFGA != c1 ) |
|
649 { |
|
650 bmpIterator2.End(); |
|
651 bmpIterator1.End(); |
|
652 return KWorstCase; |
|
653 } |
|
654 } |
|
655 for ( TInt l = 0; l < columnDelta; l++ ) |
|
656 { |
|
657 if ( iterFlag ) |
|
658 { |
|
659 bmpIterator1.DecXPos(); |
|
660 bmpIterator2.DecXPos(); |
|
661 } |
|
662 else |
|
663 { |
|
664 bmpIterator1.IncXPos(); |
|
665 bmpIterator2.IncXPos(); |
|
666 } |
|
667 } |
|
668 } |
|
669 |
|
670 for ( TInt k = 0; k < rowDelta; k++ ) |
|
671 { |
|
672 bmpIterator1.IncYPos(); |
|
673 bmpIterator2.IncYPos(); |
|
674 } |
|
675 for ( TInt l = 0; l < columnDelta; l++ ) |
|
676 { |
|
677 if ( iterFlag ) |
|
678 { |
|
679 bmpIterator1.IncXPos(); |
|
680 bmpIterator2.IncXPos(); |
|
681 } |
|
682 else |
|
683 { |
|
684 bmpIterator1.DecXPos(); |
|
685 bmpIterator2.DecXPos(); |
|
686 } |
|
687 } |
|
688 iterFlag = !iterFlag; |
|
689 } |
|
690 |
|
691 |
|
692 bmpIterator2.End(); |
|
693 bmpIterator1.End(); |
|
694 |
|
695 return 0; |
|
696 } |
|
697 |
|
698 TInt CHtiTextRcg::ImageDiffBinFullL(CFbsBitmap * aBitmap1, TPoint aOrigin1, |
|
699 CFbsBitmap * aBitmap2) |
|
700 { |
|
701 TSize aSize = aBitmap2->SizeInPixels(); |
|
702 //straight average difference |
|
703 TBitmapUtil bmpIterator1(aBitmap1); |
|
704 TBitmapUtil bmpIterator2(aBitmap2); |
|
705 |
|
706 bmpIterator1.Begin( aOrigin1 ); |
|
707 bmpIterator2.Begin( TPoint(0,0), bmpIterator1 ); |
|
708 |
|
709 //TInt nofF = 0; |
|
710 TInt nofB = 0; |
|
711 |
|
712 TInt sumB = 0; |
|
713 //TInt sumF = 0; |
|
714 TBool intellFlagBG; |
|
715 TBool iterFlag = EFalse; |
|
716 TInt rowDelta = 1; |
|
717 TInt columnDelta = 1; |
|
718 TInt fgColor = -1; |
|
719 for ( TInt i = 0; i < aSize.iHeight; i += rowDelta ) |
|
720 { |
|
721 intellFlagBG = EFalse; |
|
722 for ( TInt j = 0; j < aSize.iWidth; j += columnDelta ) |
|
723 { |
|
724 TInt c1 = ( bmpIterator1.GetPixel() ) & 0xff; |
|
725 TInt c2 = ( bmpIterator2.GetPixel() ) & 0xff; |
|
726 |
|
727 if ( c2 == iMaskFgColor ) // if FG |
|
728 { |
|
729 if ( c1 != fgColor ) |
|
730 { |
|
731 if ( fgColor != -1 ) |
|
732 { |
|
733 //failed |
|
734 bmpIterator2.End(); |
|
735 bmpIterator1.End(); |
|
736 return KWorstCase; |
|
737 } |
|
738 else |
|
739 { |
|
740 fgColor = c1; //init fgColor |
|
741 } |
|
742 } |
|
743 intellFlagBG = ETrue; |
|
744 } |
|
745 else if ( c2 == iMaskBgColor && intellFlagBG ) |
|
746 { |
|
747 sumB += c1; |
|
748 ++nofB; |
|
749 intellFlagBG = EFalse; |
|
750 } |
|
751 for ( TInt l = 0; l < columnDelta; l++ ) |
|
752 { |
|
753 if ( iterFlag ) |
|
754 { |
|
755 bmpIterator1.DecXPos(); |
|
756 bmpIterator2.DecXPos(); |
|
757 } |
|
758 else |
|
759 { |
|
760 bmpIterator1.IncXPos(); |
|
761 bmpIterator2.IncXPos(); |
|
762 } |
|
763 } |
|
764 } |
|
765 |
|
766 for ( TInt k = 0; k < rowDelta; k++ ) |
|
767 { |
|
768 bmpIterator1.IncYPos(); |
|
769 bmpIterator2.IncYPos(); |
|
770 } |
|
771 for ( TInt l = 0; l < columnDelta; l++ ) |
|
772 { |
|
773 if ( iterFlag ) |
|
774 { |
|
775 bmpIterator1.IncXPos(); |
|
776 bmpIterator2.IncXPos(); |
|
777 } |
|
778 else |
|
779 { |
|
780 bmpIterator1.DecXPos(); |
|
781 bmpIterator2.DecXPos(); |
|
782 } |
|
783 } |
|
784 iterFlag = !iterFlag; |
|
785 } |
|
786 bmpIterator2.End(); |
|
787 bmpIterator1.End(); |
|
788 |
|
789 if ( nofB == 0 ) //something wrong, should be some BG |
|
790 return KWorstCase; |
|
791 |
|
792 TInt avgB = sumB / ( nofB ); |
|
793 |
|
794 if ( Abs( fgColor - avgB ) < iAvgDiffMin ) |
|
795 { |
|
796 return KWorstCase; |
|
797 } |
|
798 |
|
799 return 0; |
|
800 } |
|
801 |
|
802 void CHtiTextRcg::MinMax(CFbsBitmap * aBitmap, TInt& aMin, TInt& aMax) |
|
803 { |
|
804 //straight average difference |
|
805 TSize aSize = aBitmap->SizeInPixels(); |
|
806 TBitmapUtil bmpIterator(aBitmap); |
|
807 |
|
808 bmpIterator.Begin( TPoint(0,0) ); |
|
809 |
|
810 aMin = KMaxTInt; |
|
811 aMax = -1; |
|
812 for ( TInt i = 0; i < aSize.iHeight; ++i ) |
|
813 { |
|
814 for ( TInt j = 0; j < aSize.iWidth; ++j ) |
|
815 { |
|
816 TInt c = ( bmpIterator.GetPixel() ) & 0xff; |
|
817 |
|
818 if ( c < aMin ) |
|
819 { |
|
820 aMin = c; |
|
821 } |
|
822 else if ( c > aMax ) |
|
823 { |
|
824 aMax = c; |
|
825 } |
|
826 |
|
827 if ( i & 1 ) |
|
828 { |
|
829 bmpIterator.DecXPos(); |
|
830 } |
|
831 else |
|
832 { |
|
833 bmpIterator.IncXPos(); |
|
834 } |
|
835 } |
|
836 bmpIterator.IncYPos(); |
|
837 |
|
838 if ( i & 1 ) |
|
839 { |
|
840 bmpIterator.IncXPos(); |
|
841 } |
|
842 else |
|
843 { |
|
844 bmpIterator.DecXPos(); |
|
845 } |
|
846 } |
|
847 |
|
848 bmpIterator.End(); |
|
849 } |
|
850 |
|
851 CFbsBitmap* CHtiTextRcg::ColorDownL( CFbsBitmap * aBitmap ) |
|
852 { |
|
853 TSize bmpSize = aBitmap->SizeInPixels(); |
|
854 CFbsBitmap* result = new ( ELeave ) CFbsBitmap(); |
|
855 User::LeaveIfError( result->Create( bmpSize, EGray256 ) ); |
|
856 |
|
857 TBitmapUtil srcBmpIterator( aBitmap ); |
|
858 TBitmapUtil resultBmpIterator( result ); |
|
859 |
|
860 srcBmpIterator.Begin( TPoint( 0, 0 ) ); |
|
861 resultBmpIterator.Begin( TPoint( 0, 0 ), srcBmpIterator ); |
|
862 |
|
863 TPoint point( 0, 0 ); |
|
864 for ( point.iY = 0; point.iY < bmpSize.iHeight; ++point.iY ) |
|
865 { |
|
866 point.iX = 0; |
|
867 srcBmpIterator.SetPos( point ); |
|
868 resultBmpIterator.SetPos( point ); |
|
869 for ( ; point.iX < bmpSize.iWidth; ++point.iX ) |
|
870 { |
|
871 TUint32 c = srcBmpIterator.GetPixel(); |
|
872 TRgb col( c ); |
|
873 resultBmpIterator.SetPixel( col.Gray256() ); |
|
874 srcBmpIterator.IncXPos(); |
|
875 resultBmpIterator.IncXPos(); |
|
876 } |
|
877 } |
|
878 |
|
879 resultBmpIterator.End(); |
|
880 srcBmpIterator.End(); |
|
881 |
|
882 return result; |
|
883 } |
|
884 |
|
885 CFbsBitmap* CHtiTextRcg::GetTextBitmapL( const TDesC& aText, |
|
886 const CFont* fontUsed, |
|
887 const TInt aLength ) |
|
888 { |
|
889 return GetTextBitmapL( aText, fontUsed, KRgbBlack, KRgbWhite, |
|
890 EGray256, aLength ); |
|
891 } |
|
892 |
|
893 |
|
894 |
|
895 CFbsBitmap* CHtiTextRcg::GetTextBitmapL( const TDesC& aText, |
|
896 const CFont* fontUsed, |
|
897 TRgb aForeground, |
|
898 TRgb aBackground, |
|
899 TDisplayMode aDisplayMode, |
|
900 const TInt aLength ) |
|
901 { |
|
902 HTI_LOG_FUNC_IN( "CHtiTextRcg::GetTextBitmapL" ) |
|
903 // Measure the text to get needed bitmap size and baseline point |
|
904 CFont::TMeasureTextOutput output; |
|
905 TInt reqWidth = fontUsed->MeasureText( aText, NULL, &output ); |
|
906 reqWidth = Max( reqWidth, output.iBounds.Width() ); |
|
907 |
|
908 // If only partial text requested, calculate new width but keep the |
|
909 // height (and baseline) as it needs to be the same as for the full text |
|
910 // for the text recognition to work. |
|
911 if ( aLength < aText.Length() ) |
|
912 { |
|
913 CFont::TMeasureTextOutput partialOutput; |
|
914 reqWidth = fontUsed->MeasureText( aText.Left( aLength ), NULL, |
|
915 &partialOutput ); |
|
916 reqWidth = Max( reqWidth, partialOutput.iBounds.Width() ); |
|
917 } |
|
918 |
|
919 TSize bmpSize( reqWidth, output.iBounds.Height() ); |
|
920 HTI_LOG_FORMAT( "Bitmap width = %d", bmpSize.iWidth ); |
|
921 HTI_LOG_FORMAT( "Bitmap height = %d", bmpSize.iHeight ); |
|
922 |
|
923 // Create the bitmap |
|
924 CFbsBitmap* result = new ( ELeave ) CFbsBitmap(); |
|
925 User::LeaveIfError( result->Create( bmpSize, aDisplayMode ) ); |
|
926 |
|
927 CFbsBitGc* bitmapContext = NULL; |
|
928 CFbsBitmapDevice* bitmapDevice = CFbsBitmapDevice::NewL( result ); |
|
929 CleanupStack::PushL( bitmapDevice ); |
|
930 User::LeaveIfError( bitmapDevice->CreateContext( bitmapContext ) ); |
|
931 CleanupStack::PushL( bitmapContext ); |
|
932 bitmapContext->SetBrushColor( aBackground ); |
|
933 bitmapContext->Clear(); |
|
934 bitmapContext->UseFont( fontUsed ); |
|
935 bitmapContext->SetPenColor( aForeground ); |
|
936 |
|
937 // Set the baseline point and draw the text |
|
938 TPoint pos( 0, bmpSize.iHeight - output.iBounds.iBr.iY ); |
|
939 HTI_LOG_FORMAT( "Baseline Y = %d", pos.iY ); |
|
940 if ( aLength < aText.Length() ) |
|
941 { |
|
942 bitmapContext->DrawText( aText.Left( aLength ), pos ); |
|
943 } |
|
944 else |
|
945 { |
|
946 bitmapContext->DrawText( aText, pos ); |
|
947 } |
|
948 |
|
949 CleanupStack::PopAndDestroy( 2 ); |
|
950 HTI_LOG_FUNC_OUT( "CHtiTextRcg::GetTextBitmapL" ) |
|
951 return result; |
|
952 } |
|
953 |
|
954 |
|
955 // End of file |