×

Discussion Board

Results 1 to 15 of 16

Hybrid View

  1. #1
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Hyderabad, India
    Posts
    90

    Question How to rotate an image?

    Hi,

    I have an image which I want to rotate, how can I do that?

    CBitmapRotator rotates the image but is rotates only increments of 90 degrees. I want more gradual rotations some thing like a clock' hand.

    Let me know if it is possible or not?
    Regards,
    Ashish

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,570

    Re: How to rotate an image?

    OpenGL (where available) can rotate images for you by arbitrary angles.
    However if you are familiar with computer graphics, implementing a DDA rotator is a more viable approach.

  3. #3
    Regular Contributor
    Join Date
    Aug 2004
    Location
    France
    Posts
    124

    Re: How to rotate an image?

    Hi wizard_hu_,

    Can you give me a piece of code, to see how to use DDA Rotator. Because, I tried to use CBitmapRotator :

    http://discussion.forum.nokia.com/fo...ad.php?t=78470

    But it doesn't work.

    Thank you very much.

    Bernie

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,570

    Re: How to rotate an image?

    For example, you can implement a DDA class:
    Code:
    class TBasicDda
    {
    public:
        TBasicDda(TInt aFrom,TInt aTo,TInt aSteps);
        void Step();
    
        TInt iCurrent;
    private:
        TInt iDir;
        TInt iDelta;
        TInt iCounter;
        TInt iDivident;
    };
    
    TBasicDda::TBasicDda(TInt aFrom,TInt aTo,TInt aSteps)
    {
        iCurrent=aFrom;
        iDir=aFrom<aTo?1:-1;
        iDelta=iDir*(aTo-aFrom);
        iCounter=-iDelta;
        iDivident=aSteps;
    }
    
    void TBasicDda::Step()
    {
        if(iDivident==0)
            return;
        iCounter+=iDelta;
        while(iCounter>=iDivident)
        {
            iCurrent+=iDir;
            iCounter-=iDivident;
        }
    }

  5. #5
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,570

    Re: How to rotate an image?

    And wrap around a 2D texture mapper:
    Code:
    class CRender:public CBase
    {
    public:
        static CRender *NewL(TRect aRect);
        ~CRender();
        void Line(TPoint aP1,TPoint aT1,TPoint aP2,TPoint aT2);
        void Flush(CFbsBitmap &aTexture,CGraphicsContext &aTarget);
    private:
        void ConstructL();
        TRect iRect;
        TInt *iLeft;
        TInt *iLeftU;
        TInt *iLeftV;
        TInt *iRight;
        TInt *iRightU;
        TInt *iRightV;
    };
    
    CRender* CRender::NewL(TRect aRect)
    {
        CRender *self=new(ELeave)CRender;
        CleanupStack::PushL(self);
        self->iRect=aRect;
        self->ConstructL();
        CleanupStack::Pop();
        return self;
    }
    
    CRender* CRender::NewL(TRect aRect)
    {
        CRender *self=new(ELeave)CRender;
        CleanupStack::PushL(self);
        self->iRect=aRect;
        self->ConstructL();
        CleanupStack::Pop();
        return self;
    }
    
    void CRender::ConstructL()
    {
        TInt h=iRect.Height();
        iLeft=new(ELeave)TInt[h];
        iLeftU=new(ELeave)TInt[h];
        iLeftV=new(ELeave)TInt[h];
        iRight=new(ELeave)TInt[h];
        iRightU=new(ELeave)TInt[h];
        iRightV=new(ELeave)TInt[h];
        for(TInt i=0;i<h;i++)
        {
            iLeft[i]=iRect.iBr.iX+1;
            iRight[i]=iRect.iTl.iX-1;
        }
    }
     
    CRender::~CRender()
    {
        delete[] iLeft;
        delete[] iLeftU;
        delete[] iLeftV;
        delete[] iRight;
        delete[] iRightU;
        delete[] iRightV;
    }
    
    void CRender::Line(TPoint aP1,TPoint aT1,TPoint aP2,TPoint aT2)
    {
        if(aP1.iY==aP2.iY)
            return;
        if(aP1.iY>aP2.iY)
        {
            TPoint temp=aP1;
            aP1=aP2;
            aP2=temp;
            temp=aT1;
            aT1=aT2;
            aT2=temp;
        }
        aP1.iY-=iRect.iTl.iY;
        aP2.iY-=iRect.iTl.iY;
        TInt h=iRect.Height();
        TBasicDda x(aP1.iX,aP2.iX,aP2.iY-aP1.iY);
        TBasicDda u(aT1.iX,aT2.iX,aP2.iY-aP1.iY);
        TBasicDda v(aT1.iY,aT2.iY,aP2.iY-aP1.iY);
        for(TInt y=aP1.iY;y<=aP2.iY;y++)
        {
            if(y>=0 && y<h)
            {
                if(x.iCurrent<iLeft[y])
                {
                    iLeft[y]=x.iCurrent;
                    iLeftU[y]=u.iCurrent;
                    iLeftV[y]=v.iCurrent;
                }
                if(x.iCurrent>iRight[y])
                {
                    iRight[y]=x.iCurrent;
                    iRightU[y]=u.iCurrent;
                    iRightV[y]=v.iCurrent;
                }
            }
            x.Step();
            u.Step();
            v.Step();
        }
    }
    
    void CRender::Flush(CFbsBitmap &aTexture,CGraphicsContext &aTarget)
    {
        aTarget.SetPenStyle(CGraphicsContext::ESolidPen);
        aTarget.SetPenSize(TSize(1,1));
        aTarget.SetDrawMode(CGraphicsContext::EDrawModePEN);
    
        TInt h=iRect.Height();
        for(TInt y=0;y<h;y++)
        {
            if(iLeft[y]<=iRect.iBr.iX && iRight[y]>=iRect.iTl.iX)
            {
                TBasicDda u(iLeftU[y],iRightU[y],iRight[y]-iLeft[y]);
                TBasicDda v(iLeftV[y],iRightV[y],iRight[y]-iLeft[y]);
                for(TInt x=iLeft[y];x<=iRight[y];x++)
                {
                    if(x>=iRect.iTl.iX && x<=iRect.iBr.iX)
                    {
                        TRgb rgb;
                        aTexture.GetPixel(rgb,TPoint(u.iCurrent,v.iCurrent));
                        aTarget.SetPenColor(rgb);
                        aTarget.Plot(TPoint(x,y+iRect.iTl.iY));
                    }
                    u.Step();
                    v.Step();
                }
            }
        }
    }

  6. #6
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,570

    Re: How to rotate an image?

    Example usage:
    Code:
    void CTestAppView::Draw(const TRect& /*aRect*/) const
    {
        CWindowGc& gc = SystemGc();
        TRect drawRect = Rect();
        gc.Clear();
        drawRect.Shrink(70,10);
    
        CRender *r=CRender::NewL(drawRect);
        CleanupStack::PushL(r);
        TSize s=iBitmap->SizeInPixels();
        r->Line(TPoint(50,20),TPoint(0,0),TPoint(120,40),TPoint(s.iWidth,0));
        r->Line(TPoint(120,40),TPoint(s.iWidth,0),TPoint(100,110),TPoint(s.iWidth,s.iHeight));
        r->Line(TPoint(100,110),TPoint(s.iWidth,s.iHeight),TPoint(30,90),TPoint(0,s.iHeight));
        r->Line(TPoint(30,90),TPoint(0,s.iHeight),TPoint(50,20),TPoint(0,0));
        r->Flush(*iBitmap,gc);
        CleanupStack::PopAndDestroy();
    
        //some outlining
        gc.SetPenColor(TRgb(0,0,0));
        gc.DrawRect(drawRect);
        gc.DrawLine(TPoint(50,20),TPoint(120,40));
        gc.DrawLine(TPoint(120,40),TPoint(100,110));
        gc.DrawLine(TPoint(100,110),TPoint(30,90));
        gc.DrawLine(TPoint(30,90),TPoint(50,20));
    }

  7. #7
    Super Contributor
    Join Date
    Jul 2008
    Location
    Chennai,India
    Posts
    889

    Re: How to rotate an image?

    Sorry ppl...
    I am digging an old old file...

    Wizard_hu...

    I am following this code for rotation..
    I am getting the rotated image ...(Its around 35-50 degree,i think)
    But the image is too small in dimension...
    But where to set the dimensions and angles???

    Pls if u tell me i will continue with my work on rotation....

  8. #8
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Budapest, Hungary
    Posts
    28,570

    Re: How to rotate an image?

    You need some trigonometry for that.

    If you have an origo-centered rectangle with coordinates
    -a, b (top-left corner)
    a, b (top-right corner)
    a,-b (bottom-right corner)
    -a,-b (bottom-left corner)

    The new coordinates after rotating with 35 degrees will become
    -a * cos 35 - b * sin 35 , -a * sin 35 + b * cos 35
    a * cos 35 - b * sin 35 , a * sin 35 + b * cos 35
    a * cos 35 + b * sin 35 , a * sin 35 -b * cos 35
    -a * cos 35 + b * sin 35 , -a * sin 35 -b * cos 35

    Remember that Math::Sin/Cos expects argument in radians, so you will actually need 35*KPi/180 as argument.

  9. #9
    Super Contributor
    Join Date
    Jul 2008
    Location
    Chennai,India
    Posts
    889

    Re: How to rotate an image?

    Thanx wizard_hu_

    I will finish off this thing...
    Very much thanx..I hope that the rotation doesn't decrease the quality of the image..

  10. #10
    Regular Contributor
    Join Date
    Nov 2008
    Posts
    113

    Re: How to rotate an image?

    Hi wizard_hu
    can you give me a hint for your sample code in order to be understood ??
    Thanks in advance

Similar Threads

  1. --- ???save image problem??? ---
    By ferenn in forum Mobile Java Media (Graphics & Sounds)
    Replies: 6
    Last Post: 2007-10-01, 14:33
  2. Rotate an image
    By bernie31 in forum Symbian
    Replies: 0
    Last Post: 2006-04-13, 16:38
  3. HELP: Mutable Image to Immutable Image?
    By rj_cybersilver in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2005-03-26, 09:58
  4. Rotate and flip image at once
    By saucjedi in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2005-03-09, 16:13
  5. Rotate and flip image at once
    By saucjedi in forum Mobile Java General
    Replies: 0
    Last Post: 2005-03-09, 16:11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •