|
1 /* |
|
2 * Copyright (c) 2006 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: |
|
15 * DCSharpening |
|
16 * Sharpening using IMAAMI algorithm. |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 #include <fbs.h> |
|
24 #include <e32math.h> |
|
25 #include "DCSharpening.h" |
|
26 |
|
27 |
|
28 |
|
29 /* |
|
30 ----------------------------------------------------------------------------- |
|
31 |
|
32 Constructor |
|
33 |
|
34 Default constructor, initializes member variables to initial values |
|
35 |
|
36 Return Values: none |
|
37 |
|
38 ----------------------------------------------------------------------------- |
|
39 */ |
|
40 DCSharpening::DCSharpening() |
|
41 { |
|
42 // Set default values for parameters |
|
43 iParameters.SHARP_OVER = SharpOver; |
|
44 iParameters.SHARP_DZONE = SharpDZone; |
|
45 iParameters.SHARP_GAIN = SharpGain; |
|
46 } |
|
47 |
|
48 |
|
49 |
|
50 /* |
|
51 ----------------------------------------------------------------------------- |
|
52 |
|
53 NewLC |
|
54 |
|
55 Factory function to instantiate the class. |
|
56 This function leaves the class pointer to the cleanup stack |
|
57 May leave with KErrNoMemory if no memory available |
|
58 |
|
59 Return Values: DCSharpening* self: pointer to the class instance |
|
60 |
|
61 ----------------------------------------------------------------------------- |
|
62 */ |
|
63 DCSharpening* DCSharpening::NewLC() |
|
64 { |
|
65 DCSharpening* self = new (ELeave) DCSharpening(); |
|
66 CleanupStack::PushL(self); |
|
67 self->ConstructL(); |
|
68 return self; |
|
69 } |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 /* |
|
75 ----------------------------------------------------------------------------- |
|
76 |
|
77 NewL |
|
78 |
|
79 Factory function to instantiate the class. |
|
80 May leave with KErrNoMemory if no memory available |
|
81 |
|
82 Return Values: DCSharpening* self: pointer to the class instance |
|
83 |
|
84 ----------------------------------------------------------------------------- |
|
85 */ |
|
86 DCSharpening* DCSharpening::NewL() |
|
87 { |
|
88 DCSharpening* self = DCSharpening::NewLC(); |
|
89 CleanupStack::Pop(); |
|
90 return self; |
|
91 } |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 /* |
|
97 ----------------------------------------------------------------------------- |
|
98 |
|
99 ConstructL |
|
100 |
|
101 Second phase constructor. Does nothing at the moment |
|
102 |
|
103 Return Values: none |
|
104 |
|
105 ----------------------------------------------------------------------------- |
|
106 */ |
|
107 void DCSharpening::ConstructL() |
|
108 { |
|
109 // This function is intentionally left blank. |
|
110 } |
|
111 |
|
112 |
|
113 |
|
114 /* |
|
115 ----------------------------------------------------------------------------- |
|
116 |
|
117 Destructor |
|
118 |
|
119 Deletes the scanline buffer |
|
120 |
|
121 Return Values: none |
|
122 |
|
123 ----------------------------------------------------------------------------- |
|
124 */ |
|
125 DCSharpening::~DCSharpening() |
|
126 { |
|
127 // This function is intentionally left blank. |
|
128 } |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 /* |
|
134 ----------------------------------------------------------------------------- |
|
135 |
|
136 ProcessL |
|
137 |
|
138 Process image referenced by aSrcBmpPtr (modify aSrcBmpPtr). |
|
139 May leave with KErrNoMemory if no memory available |
|
140 |
|
141 Return Values: none |
|
142 |
|
143 ----------------------------------------------------------------------------- |
|
144 */ |
|
145 void DCSharpening::ProcessL(CFbsBitmap* aSrcBmpPtr) |
|
146 { |
|
147 |
|
148 HBufC8* iSrcDataBuffer; //Local memory for source data |
|
149 HBufC8* iDstDataBuffer; //Local memory for destination data |
|
150 TUint8* SrcDataPtr[3]; //Pointers to source image pixels in 3 successive lines |
|
151 TUint8* DstDataPtr; //Pointer to destianation pixels |
|
152 TInt y,u,v, out; //Color components |
|
153 TUint x; //Pixel indes |
|
154 TInt LineNo; //Line index |
|
155 TUint LineLength; //Image width |
|
156 TUint8 TmpLineIdx; //Temporary line index used for line swapping |
|
157 TInt a, b, c, d, e, f, g, h, o; |
|
158 |
|
159 //Get image width |
|
160 LineLength=aSrcBmpPtr->SizeInPixels().iWidth; |
|
161 |
|
162 //Allocate local memory for 3 source lines. |
|
163 //Each line contains RGB triplets in BGRBGRBGR... format |
|
164 //(VUYVUYVUY... after conversion). |
|
165 //Descriptor HBufC8 is used. |
|
166 iSrcDataBuffer=HBufC8::NewMaxL(3*LineLength*3); |
|
167 CleanupStack::PushL(iSrcDataBuffer); |
|
168 |
|
169 //Set line pointers to point into beginnings of lines in allocated memory. |
|
170 //Lines are stored consecutively. |
|
171 //SrcLinePtrs[0] points to 1st line, [1] to 2nd, etc... |
|
172 //Use descriptor TPtr8 instead of c++ pointer, because |
|
173 //Set- and GetScanLine functions require it. |
|
174 //Set also descriptor lengths. |
|
175 TPtr8 SrcLinePtrs[3]={ |
|
176 TPtr8((TUint8*)iSrcDataBuffer->Des().Ptr(),LineLength*3,LineLength*3), |
|
177 TPtr8((TUint8*)iSrcDataBuffer->Des().Ptr()+LineLength*3,LineLength*3,LineLength*3), |
|
178 TPtr8((TUint8*)iSrcDataBuffer->Des().Ptr()+2*LineLength*3,LineLength*3,LineLength*3) |
|
179 }; |
|
180 |
|
181 //Set source line indexes. |
|
182 //Line indexes are used to select proper line pointer. |
|
183 //SrcLineIndex[0] selects first line under filter window, [1] second, etc... |
|
184 //Indexes are needed because lines are swapped so that only one new line is |
|
185 //read from bitmap when filtering window moves down. |
|
186 //TPtr8 cannot be swapped (operator = copies data). Therefore swapping is |
|
187 //done through indexing. |
|
188 TUint8 SrcLineIndex[3]={0,1,2}; |
|
189 |
|
190 //Allocate local memory for destination data. |
|
191 //Descriptor HBufC8 is used. |
|
192 iDstDataBuffer=HBufC8::NewMaxL(LineLength*3); |
|
193 CleanupStack::PushL(iDstDataBuffer); |
|
194 |
|
195 //Set destination line pointer to beginning of allocated memory. |
|
196 //Use descriptor TPtr8 instead of c++ pointer, because |
|
197 //Set- and GetScanline require it. |
|
198 //Set also descriptor length. |
|
199 TPtr8 DstLinePtr((TUint8*)iDstDataBuffer->Des().Ptr(),LineLength*3,LineLength*3); |
|
200 |
|
201 |
|
202 //Get 1st line from source image to 1st line in local memory buffer. |
|
203 LineNo=0; |
|
204 aSrcBmpPtr->GetScanLine(SrcLinePtrs[SrcLineIndex[0]],TPoint(0,LineNo),LineLength,aSrcBmpPtr->DisplayMode()); |
|
205 |
|
206 //Get 2nd line from source image to 2nd line in local memory buffer. |
|
207 LineNo=1; |
|
208 aSrcBmpPtr->GetScanLine(SrcLinePtrs[SrcLineIndex[1]],TPoint(0,LineNo),LineLength,aSrcBmpPtr->DisplayMode()); |
|
209 |
|
210 // Step through image lines, do not process 1st and last line (3x3 filter mask). |
|
211 for (LineNo=1;LineNo<aSrcBmpPtr->SizeInPixels().iHeight-1;++LineNo) |
|
212 { |
|
213 //Get next line from image to 3rd line of the filtering window |
|
214 aSrcBmpPtr->GetScanLine(SrcLinePtrs[SrcLineIndex[2]],TPoint(0,LineNo+1),LineLength,aSrcBmpPtr->DisplayMode()); |
|
215 |
|
216 //Set destination pixel pointer to 2nd pixel of line (1st pixel is not processed (3x3 filter)) |
|
217 DstDataPtr=(TUint8*)iDstDataBuffer->Des().Ptr()+3; |
|
218 |
|
219 //Set source pixel pointers to beginnings of lines in filtering window |
|
220 SrcDataPtr[0]=(TUint8*)iSrcDataBuffer->Des().Ptr()+SrcLineIndex[0]*LineLength*3; |
|
221 SrcDataPtr[1]=(TUint8*)iSrcDataBuffer->Des().Ptr()+SrcLineIndex[1]*LineLength*3; |
|
222 SrcDataPtr[2]=(TUint8*)iSrcDataBuffer->Des().Ptr()+SrcLineIndex[2]*LineLength*3; |
|
223 |
|
224 //Copy the first and the last pixels from the original image |
|
225 *(DstDataPtr-3)=*(SrcDataPtr[1]); |
|
226 *(DstDataPtr+1-3)=*(SrcDataPtr[1]+1); |
|
227 *(DstDataPtr+2-3)=*(SrcDataPtr[1]+2); |
|
228 *(DstDataPtr+3*(LineLength-1)-3)=*(SrcDataPtr[1]+3*(LineLength-1)); |
|
229 *(DstDataPtr+1+3*(LineLength-1)-3)=*(SrcDataPtr[1]+1+3*(LineLength-1)); |
|
230 *(DstDataPtr+2+3*(LineLength-1)-3)=*(SrcDataPtr[1]+2+3*(LineLength-1)); |
|
231 |
|
232 //Step through image pixels, do not process 1st and last pixel (3x3 filter). |
|
233 for (x=LineLength-2;x!=0;--x) |
|
234 { |
|
235 |
|
236 //Compute filter output |
|
237 //Get input values from luminance component |
|
238 //.....abc.... o=processed pixel |
|
239 //.....doe.... |
|
240 //.....fgh.... |
|
241 //a,b,c,d come from saved previous values |
|
242 //pppppPPPpppp |
|
243 //.....Po..... |
|
244 //............ |
|
245 a = *(SrcDataPtr[0]+1); |
|
246 b = *(SrcDataPtr[0]+1+3); |
|
247 c = *(SrcDataPtr[0]+1+6); |
|
248 d = *(SrcDataPtr[1]+1); |
|
249 o = *(SrcDataPtr[1]+1+3); |
|
250 e = *(SrcDataPtr[1]+1+6); |
|
251 f = *(SrcDataPtr[2]+1); |
|
252 g = *(SrcDataPtr[2]+1+3); |
|
253 h = *(SrcDataPtr[2]+1+6); |
|
254 |
|
255 //Sharpen green component with IMAAMI sharpening algorithm |
|
256 out=Peak(a, b, c, d, e, f, g, h, o); |
|
257 |
|
258 //Scale result and limit to available dynamic range. |
|
259 y=Limit255(out); |
|
260 |
|
261 //Get B and R components |
|
262 v=*(SrcDataPtr[1]+3); //blue |
|
263 u=*(SrcDataPtr[1]+2+3); //red |
|
264 |
|
265 //Compute difference in green component due to sharpening and add it to B and R |
|
266 v=Limit255(v+(out-o)); //blue |
|
267 u=Limit255(u+(out-o)); //red |
|
268 |
|
269 //Set destination color components |
|
270 *DstDataPtr=(TUint8)v ; //blue |
|
271 *(DstDataPtr+1)=(TUint8)y; //green |
|
272 *(DstDataPtr+2)=(TUint8)u; //red |
|
273 |
|
274 //Move to next VUY/RGB triplet in line |
|
275 DstDataPtr+=3; |
|
276 SrcDataPtr[0]+=3; |
|
277 SrcDataPtr[1]+=3; |
|
278 SrcDataPtr[2]+=3; |
|
279 } |
|
280 |
|
281 //Set processed line |
|
282 aSrcBmpPtr->SetScanLine(DstLinePtr,LineNo); |
|
283 |
|
284 //Swap source line indexes: 0<-1<-2<-0. |
|
285 //Now [0] is indexing the previous [1] data, [1] previous [2], etc... |
|
286 //When filtering window is moved down the new line is read to index [2], |
|
287 //so previous [0] data (i.e. 1st line in filtering window) is discarded. |
|
288 TmpLineIdx=SrcLineIndex[0]; |
|
289 SrcLineIndex[0]=SrcLineIndex[1]; |
|
290 SrcLineIndex[1]=SrcLineIndex[2]; |
|
291 SrcLineIndex[2]=TmpLineIdx; |
|
292 } |
|
293 |
|
294 //Free memory |
|
295 CleanupStack::PopAndDestroy(2); //free iSrcDataBuffer and iDstDataBuffer |
|
296 } |
|
297 |
|
298 |
|
299 |
|
300 /* |
|
301 ----------------------------------------------------------------------------- |
|
302 |
|
303 Peak |
|
304 |
|
305 IMAAMI sharpening function |
|
306 |
|
307 Return Values: TInt pixel sharpening data |
|
308 |
|
309 ----------------------------------------------------------------------------- |
|
310 */ |
|
311 TInt DCSharpening::Peak(TInt aA, TInt aB, TInt aC, TInt aD, TInt aE, TInt aF, TInt aG, TInt aH, TInt aO) |
|
312 { |
|
313 TInt out, tmp; |
|
314 TInt lim = 0; |
|
315 TInt sign = 1; |
|
316 TInt over = (iParameters.SHARP_OVER << 2); |
|
317 TInt gain = (TInt)(iParameters.SHARP_GAIN * (TReal)(1 << 16) + 0.5); |
|
318 TInt gradHor, gradVer; |
|
319 TInt gradDip, gradDin; |
|
320 TInt max, min; |
|
321 TInt add, weig; |
|
322 TInt tmp1, tmp2; |
|
323 TInt SHARP_LIM2 = ((1 << 15) + (1 << 14)); |
|
324 |
|
325 |
|
326 gradHor = (aO << 1) - aD - aE; |
|
327 gradVer = (aO << 1) - aB - aG; |
|
328 gradDip = ((((aO << 1) - aA - aH) * 3) >> 2); |
|
329 gradDin = ((((aO << 1) - aC - aF) * 3) >> 2); |
|
330 |
|
331 findMinMax4(gradHor, gradVer, gradDip, gradDin, &min, &max); |
|
332 |
|
333 if(min < 0) |
|
334 { |
|
335 tmp = -min; |
|
336 |
|
337 if(tmp > max) |
|
338 { |
|
339 sign = -1; |
|
340 lim = tmp; |
|
341 tmp2 = tmp; |
|
342 if(max < 0) tmp1 = -max; |
|
343 else tmp1 = max; |
|
344 } |
|
345 else |
|
346 { |
|
347 lim = max; |
|
348 tmp2 = max; |
|
349 tmp1 = tmp; |
|
350 } |
|
351 } |
|
352 else if(max == 0) |
|
353 { |
|
354 tmp2 = 1; |
|
355 tmp1 = 1; |
|
356 } |
|
357 else |
|
358 { |
|
359 lim = max; |
|
360 tmp2 = max; |
|
361 tmp1 = min; |
|
362 } |
|
363 |
|
364 if((tmp1 << 2) > 3 * tmp2) |
|
365 { |
|
366 out = aO; |
|
367 } |
|
368 else if((tmp1 << 2) < tmp2) |
|
369 { |
|
370 add = sign * ((lim * gain) >> 16); |
|
371 if(lim < (TInt)(iParameters.SHARP_DZONE)) out = aO; |
|
372 else out = ADJUST_RANGE_TO_10BITS(aO + add); |
|
373 } |
|
374 else |
|
375 { |
|
376 tmp = (tmp1 << 16) / tmp2; |
|
377 weig = (SHARP_LIM2 - tmp); |
|
378 if(lim < (TInt)(iParameters.SHARP_DZONE)) out = aO; |
|
379 else |
|
380 { |
|
381 add = sign * ((((weig * lim) >> 16) * gain) >> 16); |
|
382 out = ADJUST_RANGE_TO_10BITS(aO + add); |
|
383 } |
|
384 } |
|
385 |
|
386 if(sign < 0) |
|
387 { |
|
388 tmp = aO - over; |
|
389 |
|
390 if(out < tmp) |
|
391 { |
|
392 lim = -lim; |
|
393 |
|
394 if(gradHor == lim) |
|
395 { |
|
396 if(aD < aE) |
|
397 { |
|
398 if(aD < aO) |
|
399 { |
|
400 tmp = aD - over; |
|
401 } |
|
402 } |
|
403 else |
|
404 { |
|
405 if(aE < aO) |
|
406 { |
|
407 tmp = aE - over; |
|
408 } |
|
409 } |
|
410 } |
|
411 else if(gradVer == lim) |
|
412 { |
|
413 if(aB < aG) |
|
414 { |
|
415 if(aB < aO) |
|
416 { |
|
417 tmp = aB - over; |
|
418 } |
|
419 } |
|
420 else |
|
421 { |
|
422 if(aG < aO) |
|
423 { |
|
424 tmp = aG - over; |
|
425 } |
|
426 } |
|
427 } |
|
428 else if(gradDip == lim) |
|
429 { |
|
430 if(aA < aH) |
|
431 { |
|
432 if(aA < aO) |
|
433 { |
|
434 tmp = aA - over; |
|
435 } |
|
436 } |
|
437 else |
|
438 { |
|
439 if(aH < aO) |
|
440 { |
|
441 tmp = aH - over; |
|
442 } |
|
443 } |
|
444 } |
|
445 else |
|
446 { |
|
447 if(aC < aF) |
|
448 { |
|
449 if(aC < aO) |
|
450 { |
|
451 tmp = aC - over; |
|
452 } |
|
453 } |
|
454 else |
|
455 { |
|
456 if(aF < aO) |
|
457 { |
|
458 tmp = aF - over; |
|
459 } |
|
460 } |
|
461 } |
|
462 |
|
463 if(out < tmp) |
|
464 { |
|
465 out = tmp; |
|
466 } |
|
467 } |
|
468 } |
|
469 else |
|
470 { |
|
471 tmp = aO + over; |
|
472 if(out > tmp) |
|
473 { |
|
474 if(gradHor == lim) |
|
475 { |
|
476 if(aD > aE) |
|
477 { |
|
478 if(aD > aO) |
|
479 { |
|
480 tmp = aD + over; |
|
481 } |
|
482 } |
|
483 else |
|
484 { |
|
485 if(aE > aO) |
|
486 { |
|
487 tmp = aE + over; |
|
488 } |
|
489 } |
|
490 } |
|
491 else if(gradVer == lim) |
|
492 { |
|
493 if(aB > aG) |
|
494 { |
|
495 if(aB > aO) |
|
496 { |
|
497 tmp = aB + over; |
|
498 } |
|
499 } |
|
500 else |
|
501 { |
|
502 if(aG > aO) |
|
503 { |
|
504 tmp = aG + over; |
|
505 } |
|
506 } |
|
507 } |
|
508 else if(gradDip == lim) |
|
509 { |
|
510 if(aA > aH) |
|
511 { |
|
512 if(aA > aO) |
|
513 { |
|
514 tmp = aA + over; |
|
515 } |
|
516 } |
|
517 else |
|
518 { |
|
519 if(aH > aO) |
|
520 { |
|
521 tmp = aH + over; |
|
522 } |
|
523 } |
|
524 } |
|
525 else |
|
526 { |
|
527 if(aC > aF) |
|
528 { |
|
529 if(aC > aO) |
|
530 { |
|
531 tmp = aC + over; |
|
532 } |
|
533 } |
|
534 else |
|
535 { |
|
536 if(aF > aO) |
|
537 { |
|
538 tmp = aF + over; |
|
539 } |
|
540 } |
|
541 } |
|
542 |
|
543 if(out > tmp) |
|
544 { |
|
545 out = tmp; |
|
546 } |
|
547 } |
|
548 } |
|
549 |
|
550 return(out); |
|
551 } |
|
552 |
|
553 |
|
554 |
|
555 /* |
|
556 ----------------------------------------------------------------------------- |
|
557 |
|
558 Median3 |
|
559 |
|
560 IMAAMI sharpening help function |
|
561 |
|
562 Return Values: TInt median of input values |
|
563 |
|
564 ----------------------------------------------------------------------------- |
|
565 */ |
|
566 TInt DCSharpening::Median3(TInt aA, TInt aB, TInt aC) |
|
567 { |
|
568 if(aA < aB) |
|
569 { |
|
570 if(aA > aC) return aA; |
|
571 else if(aB < aC) return aB; |
|
572 else return aC; |
|
573 } |
|
574 else |
|
575 { |
|
576 if(aA < aC) return aA; |
|
577 else if(aB > aC) return aB; |
|
578 else return aC; |
|
579 } |
|
580 } |
|
581 |
|
582 |
|
583 |
|
584 /* |
|
585 ----------------------------------------------------------------------------- |
|
586 |
|
587 findMinMax4 |
|
588 |
|
589 IMAAMI sharpening help function |
|
590 |
|
591 Finds minimum and maximum of A,B,C & D. Modifies min & max arguments |
|
592 |
|
593 Return Values: None |
|
594 |
|
595 ----------------------------------------------------------------------------- |
|
596 */ |
|
597 void DCSharpening::findMinMax4(TInt A, TInt B, TInt C, TInt D, TInt *min, TInt *max) |
|
598 { |
|
599 if(A < B) |
|
600 { |
|
601 if(C < D) |
|
602 { |
|
603 if(A < C) *min = A; |
|
604 else *min = C; |
|
605 if(B > D) *max = B; |
|
606 else *max = D; |
|
607 } |
|
608 else |
|
609 { |
|
610 if(A < D) *min = A; |
|
611 else *min = D; |
|
612 if(B > C) *max = B; |
|
613 else *max = C; |
|
614 } |
|
615 } |
|
616 else |
|
617 { |
|
618 if(C < D) |
|
619 { |
|
620 if(B < C) *min = B; |
|
621 else *min = C; |
|
622 if(A > D) *max = A; |
|
623 else *max = D; |
|
624 } |
|
625 else |
|
626 { |
|
627 if(B < D) *min = B; |
|
628 else *min = D; |
|
629 if(A > C) *max = A; |
|
630 else *max = C; |
|
631 } |
|
632 } |
|
633 } |
|
634 |
|
635 |
|
636 |
|
637 |
|
638 /* |
|
639 ----------------------------------------------------------------------------- |
|
640 |
|
641 SetParams |
|
642 |
|
643 Set processing parameters |
|
644 |
|
645 Return Values: none |
|
646 |
|
647 ----------------------------------------------------------------------------- |
|
648 */ |
|
649 void DCSharpening::SetParameters(DCSharpeningParams* params) |
|
650 { |
|
651 iParameters = *params; |
|
652 } |
|
653 |
|
654 |
|
655 |
|
656 /* |
|
657 ----------------------------------------------------------------------------- |
|
658 |
|
659 GetParams |
|
660 |
|
661 Get current processing parameters |
|
662 |
|
663 Return Values: none |
|
664 |
|
665 ----------------------------------------------------------------------------- |
|
666 */ |
|
667 void DCSharpening::GetParameters(DCSharpeningParams* params) |
|
668 { |
|
669 *params = iParameters; |
|
670 } |
|
671 //----IMAAMI---- |
|
672 |