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