Mercurial > s60_experiments
comparison DemoCube/src/DemoCubeAppView.cpp @ 0:43361e733c66
Origination.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Tue, 12 Feb 2008 17:18:02 -0600 |
parents | |
children | 0a2b5a968ce7 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:43361e733c66 |
---|---|
1 /*===================================================================== | |
2 | |
3 Copyright © 2007 Nokia Corporation. All rights reserved. | |
4 | |
5 ======================================================================*/ | |
6 | |
7 /* | |
8 ============================================================================ | |
9 Name : DemoCubeAppView.cpp | |
10 Author : Nokia Computer Vision Team in NRC Palo Alto | |
11 Copyright : Your copyright notice | |
12 Description : Application view implementation | |
13 ============================================================================ | |
14 */ | |
15 | |
16 // INCLUDE FILES | |
17 #include <coemain.h> | |
18 #include "DemoCubeAppView.h" | |
19 | |
20 #include "DemoCamera.h" | |
21 #include "ncvImage.h" | |
22 #include "ncvCamus.h" | |
23 #include "ncvOpticalFlow.h" | |
24 #include "ncvEgoMovement.h" | |
25 #include "DemoCube.h" | |
26 | |
27 #define PADDING_WIDTH 40 | |
28 #define PADDING_HEIGHT 30 | |
29 | |
30 // ============================ MEMBER FUNCTIONS =============================== | |
31 | |
32 // ----------------------------------------------------------------------------- | |
33 // CDemoCubeAppView::NewL() | |
34 // Two-phased constructor. | |
35 // ----------------------------------------------------------------------------- | |
36 // | |
37 CDemoCubeAppView* CDemoCubeAppView::NewL(const TRect& aRect, CDemoCubeAppUi* aAppUi) | |
38 { | |
39 CDemoCubeAppView* self = new(ELeave) CDemoCubeAppView(aAppUi); | |
40 CleanupStack::PushL(self); | |
41 self->ConstructL(aRect); | |
42 CleanupStack::Pop(); | |
43 return self; | |
44 } | |
45 | |
46 // ----------------------------------------------------------------------------- | |
47 // CDemoCubeAppView::ConstructL() | |
48 // Symbian 2nd phase constructor can leave. | |
49 // ----------------------------------------------------------------------------- | |
50 // | |
51 void CDemoCubeAppView::ConstructL( const TRect& aRect ) | |
52 { | |
53 Reset(); | |
54 | |
55 if ( CCamera::CamerasAvailable() ) //Yingen added for detecting camera | |
56 {//Yingen added for detecting camera | |
57 iCamera = CDemoCamera::NewL(aRect.Size() - TSize(PADDING_WIDTH, PADDING_HEIGHT), this); | |
58 iCamera->Acquire(); | |
59 | |
60 iCamusImage = CNokiaCVImage::NewL(); | |
61 iCamusImage->CreateL(iBitmapSize, EColor16M); | |
62 }//Yingen added for detecting camera | |
63 iCube = CDemoCube::NewL(aRect); | |
64 iCubePoints = new(ELeave) CArrayFixFlat<TPoint>(5); | |
65 for(TInt i = 0; i < 5; i++) | |
66 { | |
67 TPoint tmp(0, 0); | |
68 iCubePoints->AppendL(tmp); | |
69 } | |
70 // Create a window for this application view | |
71 CreateWindowL(); | |
72 | |
73 // Set the windows size | |
74 SetRect( aRect ); | |
75 | |
76 // Activate the window, which makes it ready to be drawn | |
77 ActivateL(); | |
78 } | |
79 | |
80 // ----------------------------------------------------------------------------- | |
81 // CDemoCubeAppView::CDemoCubeAppView() | |
82 // C++ default constructor can NOT contain any code, that might leave. | |
83 // ----------------------------------------------------------------------------- | |
84 // | |
85 CDemoCubeAppView::CDemoCubeAppView(CDemoCubeAppUi* aAppUi) : | |
86 iAppUi(aAppUi), | |
87 iCamera(NULL), | |
88 iCamusImage(NULL), | |
89 iBitmapSize(25, 18), | |
90 iCamus(NULL), | |
91 iOpticalFlow(NULL), | |
92 iCube(NULL) | |
93 { | |
94 } | |
95 | |
96 void CDemoCubeAppView::Reset() | |
97 { | |
98 iCubeWidth = 10; | |
99 iMove.iX = 0.0; | |
100 iMove.iY = 0.0; | |
101 iAngle = 0.0; | |
102 } | |
103 // ----------------------------------------------------------------------------- | |
104 // CDemoCubeAppView::~CDemoCubeAppView() | |
105 // Destructor. | |
106 // ----------------------------------------------------------------------------- | |
107 // | |
108 CDemoCubeAppView::~CDemoCubeAppView() | |
109 { | |
110 // No implementation required | |
111 if(iCamera) | |
112 { | |
113 delete iCamera; | |
114 iCamera = NULL; | |
115 } | |
116 if(iCamusImage) | |
117 { | |
118 delete iCamusImage; | |
119 iCamusImage = NULL; | |
120 } | |
121 if(iCamus) | |
122 { | |
123 delete iCamus; | |
124 iCamus = NULL; | |
125 } | |
126 if(iOpticalFlow) | |
127 { | |
128 delete iOpticalFlow; | |
129 iOpticalFlow = NULL; | |
130 } | |
131 if(iCube) | |
132 { | |
133 delete iCube; | |
134 iCube = NULL; | |
135 } | |
136 if(iCubePoints) | |
137 { | |
138 delete iCubePoints; | |
139 iCubePoints = NULL; | |
140 } | |
141 } | |
142 | |
143 // ----------------------------------------------------------------------------- | |
144 // CDemoCubeAppView::Draw() | |
145 // Draws the display. | |
146 // ----------------------------------------------------------------------------- | |
147 // | |
148 void CDemoCubeAppView::Draw(CFbsBitmap& aBitmap, CArrayFixFlat<TPoint>* aCube) const | |
149 { | |
150 CWindowGc& gc = SystemGc(); | |
151 gc.Activate(Window()); | |
152 gc.Clear(); | |
153 gc.BitBlt(TPoint(PADDING_WIDTH / 2, PADDING_HEIGHT / 2), &aBitmap); | |
154 gc.SetPenColor(TRgb(0xff, 0, 0)); | |
155 gc.DrawPolyLine(aCube); | |
156 gc.Deactivate(); | |
157 iCoeEnv->WsSession().Flush(); | |
158 } | |
159 | |
160 void CDemoCubeAppView::CameraReady() | |
161 { | |
162 iCamera->StartFeedL(); | |
163 } | |
164 | |
165 void CDemoCubeAppView::SetCameraFrame(CFbsBitmap& aBitmap) | |
166 { | |
167 BitmapDownScale(aBitmap); | |
168 | |
169 if(!iCamus) | |
170 { | |
171 iCamus = CCamus::NewL(*iCamusImage, 5); | |
172 iOpticalFlow = COpticalFlow::NewL(iCamusImage->Size()); | |
173 } | |
174 | |
175 iCamus->GetOpticalFlow(*iOpticalFlow, *iCamusImage, CAMUS_SETCAMUSOPTS(CCamus::EMedium, CCamus::EMedium, 3)); | |
176 CEgoMovement mean = iOpticalFlow->Mean(); | |
177 iMove.iX -= mean.iX * 6.3; | |
178 iMove.iY += mean.iY * 6.3; | |
179 iAngle += iOpticalFlow->Rotation().Real() * 10.5; | |
180 | |
181 // | |
182 iCubeWidth += (TInt)(iOpticalFlow->Depth().Real() * 5); | |
183 iCubeWidth = Max(2, iCubeWidth); | |
184 iCubeWidth = Min(iCubeWidth, 40); | |
185 // | |
186 | |
187 | |
188 iCube->Calculate(*iCubePoints, (TInt)iMove.iX.Real(), (TInt)iMove.iY.Real(), (TInt)iAngle, iCubeWidth); | |
189 | |
190 Draw(aBitmap, iCubePoints); | |
191 } | |
192 | |
193 void CDemoCubeAppView::BitmapDownScale(CFbsBitmap& aOriginal) | |
194 { | |
195 const TInt xBlockSize = aOriginal.SizeInPixels().iWidth / iBitmapSize.iWidth; | |
196 const TInt yBlockSize = aOriginal.SizeInPixels().iHeight / iBitmapSize.iHeight; | |
197 | |
198 TRgb color; | |
199 TPoint pos(0, 0); | |
200 | |
201 CPixelAccess* pa = CPixelAccess::NewLC(iCamusImage); | |
202 | |
203 | |
204 for(TInt y = 0; y < iBitmapSize.iHeight; y++) | |
205 { | |
206 for(TInt x = 0; x < iBitmapSize.iWidth; x++) | |
207 { | |
208 TInt red = 0; | |
209 TInt green = 0; | |
210 TInt blue = 0; | |
211 | |
212 for(TInt j = 0; j < yBlockSize; j++) | |
213 { | |
214 for(TInt i = 0; i < xBlockSize; i++) | |
215 { | |
216 pos.SetXY(x * xBlockSize + i, y * yBlockSize + j); | |
217 aOriginal.GetPixel(color, pos); | |
218 red += color.Red(); | |
219 green += color.Green(); | |
220 blue += color.Blue(); | |
221 } | |
222 } | |
223 | |
224 pos.SetXY(x, y); | |
225 pa->SetPos(pos); | |
226 const TInt pixels = xBlockSize * yBlockSize; | |
227 color.SetBlue(blue / pixels); | |
228 color.SetGreen(green / pixels); | |
229 color.SetRed(red / pixels); | |
230 pa->SetRGB(color.Value()); | |
231 } | |
232 } | |
233 CleanupStack::Pop(pa); | |
234 delete pa; | |
235 } | |
236 | |
237 // End of File |