view DemoCube/src/DemoCubeAppView.cpp @ 1:0a2b5a968ce7

Modifications that turn this Nokia-supplied DemoCube demo into a web-browser mockup that uses optical flow for panning operations.
author Atul Varma <varmaa@toolness.com>
date Tue, 12 Feb 2008 17:32:01 -0600
parents 43361e733c66
children f546bb5370c7
line wrap: on
line source

/*=====================================================================

    Copyright © 2007 Nokia Corporation. All rights reserved.

======================================================================*/

/*
============================================================================
 Name        : DemoCubeAppView.cpp
 Author      : Nokia Computer Vision Team in NRC Palo Alto
 Copyright   : Your copyright notice
 Description : Application view implementation
============================================================================
*/

// INCLUDE FILES
#include <coemain.h>
#include "DemoCubeAppView.h"

#include "DemoCamera.h"
#include "ncvImage.h"
#include "ncvCamus.h"
#include "ncvOpticalFlow.h"
#include "ncvEgoMovement.h"
#include "DemoCube.h"
#include "ImageLoader.h"

#define PADDING_WIDTH  40
#define PADDING_HEIGHT 30

_LIT( KPicture, "firefox.png" );

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CDemoCubeAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CDemoCubeAppView* CDemoCubeAppView::NewL(const TRect& aRect, CDemoCubeAppUi* aAppUi)
	{
	CDemoCubeAppView* self = new(ELeave) CDemoCubeAppView(aAppUi);
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	CleanupStack::Pop();
	return self;
	}

// -----------------------------------------------------------------------------
// CDemoCubeAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CDemoCubeAppView::ConstructL( const TRect& aRect )
    {
	Reset();
	
    if ( CCamera::CamerasAvailable() ) //Yingen added for detecting camera
        {//Yingen added for detecting camera
			iCamera = CDemoCamera::NewL(aRect.Size() - TSize(PADDING_WIDTH, PADDING_HEIGHT), this);
			iCamera->Acquire();
		
			iCamusImage = CNokiaCVImage::NewL();
			iCamusImage->CreateL(iBitmapSize, EColor16M);
        }//Yingen added for detecting camera
	iCube = CDemoCube::NewL(aRect);
	iCubePoints = new(ELeave) CArrayFixFlat<TPoint>(5);
	for(TInt i = 0; i < 5; i++)
		{
		TPoint tmp(0, 0);
		iCubePoints->AppendL(tmp);
		}    	

    // Create a window for this application view
    CreateWindowL();

    // Set the windows size
    SetRect( aRect );

    // Activate the window, which makes it ready to be drawn
    ActivateL();
    }

void CDemoCubeAppView::StartImgLoader()
{
  // was 50,50
  TSize iSize(493, 305);
  TInt color;
  TInt gray;
  TDisplayMode displayMode = CEikonEnv::Static()->WsSession().GetDefModeMaxNumColors( color, gray );

  iPicture = new (ELeave) CFbsBitmap;
  if (iPicture)
    iPicture->Create(iSize, displayMode); //EColor16MA
  iLoader = CImageLoader::NewL( 'c' );
  iLoader->LoadImage( KPicture, iPicture );
}

// -----------------------------------------------------------------------------
// CDemoCubeAppView::CDemoCubeAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CDemoCubeAppView::CDemoCubeAppView(CDemoCubeAppUi* aAppUi) :
	iAppUi(aAppUi),
	iCamera(NULL),
	iCamusImage(NULL),
	iBitmapSize(25, 18),
	iCamus(NULL),
	iOpticalFlow(NULL),
	iCube(NULL),
	iLoader(NULL),
	iPicture(NULL)
	{
	}
	
void CDemoCubeAppView::Reset()
	{
	iCubeWidth = 10;
	iMove.iX = 0.0;
	iMove.iY = 0.0;
	iAngle = 0.0;
	}
// -----------------------------------------------------------------------------
// CDemoCubeAppView::~CDemoCubeAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CDemoCubeAppView::~CDemoCubeAppView()
    {
    // No implementation required
	if(iCamera)
		{
		delete iCamera;
		iCamera = NULL;
		}
	if(iCamusImage)
		{
		delete iCamusImage;
		iCamusImage = NULL;
		}
	if(iCamus)
		{
		delete iCamus;
		iCamus = NULL;
		}
	if(iOpticalFlow)
		{
		delete iOpticalFlow;
		iOpticalFlow = NULL;
		}
	if(iCube)
		{
		delete iCube;
		iCube = NULL;
		}
	if(iCubePoints)
		{
		delete iCubePoints;
		iCubePoints = NULL;
		}    
	if(iLoader)
	  {
	    delete iLoader;
	    iLoader = NULL;
	  }
	if(iPicture)
	  {
	    delete iPicture;
	    iPicture = NULL;
	  }
    }

// -----------------------------------------------------------------------------
// CDemoCubeAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CDemoCubeAppView::Draw(CFbsBitmap& aBitmap, CArrayFixFlat<TPoint>* aCube)
	{
	CWindowGc& gc = SystemGc();
	gc.Activate(Window());
	gc.Clear();
	//gc.BitBlt(TPoint(PADDING_WIDTH / 2, PADDING_HEIGHT / 2), &aBitmap);


	//gc.BitBlt(TPoint(PADDING_WIDTH / 2, PADDING_HEIGHT / 2), iPicture);
	gc.BitBlt(TPoint( (TInt)iMove.iY.Real(), (TInt)iMove.iX.Real()), iPicture);
	//gc.SetPenColor(TRgb(0xff, 0, 0));
	//gc.DrawPolyLine(aCube);
	gc.Deactivate();
	iCoeEnv->WsSession().Flush();
	}

void CDemoCubeAppView::CameraReady()
	{
	iCamera->StartFeedL();
	}

void CDemoCubeAppView::SetCameraFrame(CFbsBitmap& aBitmap)
	{
	BitmapDownScale(aBitmap);

	if(!iCamus)
		{
		iCamus = CCamus::NewL(*iCamusImage, 5);
		iOpticalFlow = COpticalFlow::NewL(iCamusImage->Size());
		}

	iCamus->GetOpticalFlow(*iOpticalFlow, *iCamusImage, CAMUS_SETCAMUSOPTS(CCamus::EMedium, CCamus::EMedium, 3));
	CEgoMovement mean = iOpticalFlow->Mean();
	iMove.iX -= mean.iX * 6.3;
	iMove.iY -= mean.iY * 6.3;
	iAngle += iOpticalFlow->Rotation().Real() * 10.5;
	
	//
	iCubeWidth += (TInt)(iOpticalFlow->Depth().Real() * 5);
	iCubeWidth = Max(2, iCubeWidth);
	iCubeWidth = Min(iCubeWidth, 40);
	//
	
	
	iCube->Calculate(*iCubePoints, (TInt)iMove.iX.Real(), (TInt)iMove.iY.Real(), (TInt)iAngle, iCubeWidth);

	Draw(aBitmap, iCubePoints);
	}

void CDemoCubeAppView::BitmapDownScale(CFbsBitmap& aOriginal)
	{
	const TInt xBlockSize = aOriginal.SizeInPixels().iWidth / iBitmapSize.iWidth;
	const TInt yBlockSize = aOriginal.SizeInPixels().iHeight / iBitmapSize.iHeight;

	TRgb color;
	TPoint pos(0, 0);

	CPixelAccess* pa = CPixelAccess::NewLC(iCamusImage);
	
	
	for(TInt y = 0; y < iBitmapSize.iHeight; y++)
		{
		for(TInt x = 0; x < iBitmapSize.iWidth; x++)
			{
			TInt red = 0;
			TInt green = 0;
			TInt blue = 0;

			for(TInt j = 0; j < yBlockSize; j++)
				{
				for(TInt i = 0; i < xBlockSize; i++)
					{
					pos.SetXY(x * xBlockSize + i, y * yBlockSize + j);
					aOriginal.GetPixel(color, pos);
					red += color.Red();
					green += color.Green();
					blue += color.Blue();
					}
				}

			pos.SetXY(x, y);
			pa->SetPos(pos);
			const TInt pixels = xBlockSize * yBlockSize;
			color.SetBlue(blue / pixels);
			color.SetGreen(green / pixels);
			color.SetRed(red / pixels);
			pa->SetRGB(color.Value());
			}
		}
	CleanupStack::Pop(pa);
	delete pa;
	}

// End of File