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