javauis/lcdui_akn/lcdgd/src/calctransform.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2005 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 *
       
    16 */
       
    17 
       
    18 #include <lcdgdrv.h>
       
    19 #include "lcdtransform.h"
       
    20 #include "calctransform.h"
       
    21 
       
    22 //
       
    23 // Compute TLcdTransform mapping aSrcRect to aDstRect with aSrcTransform
       
    24 //
       
    25 // returns source to target transform
       
    26 //
       
    27 EXPORT_C TLcdTransform CalcTransform
       
    28 (
       
    29     const TRect& aDstRect,          // unclipped
       
    30     const TRect& aSrcRect,          // unclipped
       
    31     TTransformType aSrcTransform
       
    32 )
       
    33 {
       
    34     //
       
    35     // Transform calculates the TLcdTransform which transforms a rectangle of
       
    36     // size aSrcRect.Size(), positioned with its top left corner at the origin,
       
    37     // such that the top left corner of the transformed rectangle also lies at
       
    38     // the origin.
       
    39     //
       
    40     TLcdTransform transform = ::Transform(aSrcTransform, aSrcRect.Size());
       
    41 
       
    42     //
       
    43     // compose transform at origin with src and dst translations.
       
    44     //
       
    45     transform = Translate(aDstRect.iTl) * transform * Translate(-aSrcRect.iTl);
       
    46 
       
    47     //
       
    48     // Currently do not support scaling transforms. The following check will
       
    49     // catch an attempt to specify a destination rectangle that is not the
       
    50     // same size as the transformed source rectangle.
       
    51     //
       
    52     ASSERT(CheckTransform(aDstRect, aSrcRect, transform));
       
    53 
       
    54     return transform;
       
    55 }
       
    56 
       
    57 
       
    58 LOCAL_C TRect TransformRect(const TLcdTransform& aTransform, const TRect& aRect)
       
    59 {
       
    60     TRect rect;
       
    61     if ((aRect.iBr.iX >= aRect.iTl.iX) && (aRect.iBr.iY >= aRect.iTl.iY))
       
    62     {
       
    63         //
       
    64         // Forward propagate clipped rectangle corners to destination coords.
       
    65         // We have to use inclusive-inclusive coords and sort the transformed
       
    66         // points to produce a destination rectangle.
       
    67         //
       
    68         TPoint topLeft     = aRect.iTl;
       
    69         TPoint bottomRight = aRect.iBr;
       
    70         bottomRight.iX--;   // convert to inclusive coords
       
    71         bottomRight.iY--;
       
    72 
       
    73         //
       
    74         // Transform the points.
       
    75         //
       
    76         topLeft = aTransform(topLeft);
       
    77         bottomRight = aTransform(bottomRight);
       
    78 
       
    79         //
       
    80         // Now find min and max coords - still inclusive
       
    81         //
       
    82         TInt xMin = Min(topLeft.iX, bottomRight.iX);
       
    83         TInt xMax = Max(topLeft.iX, bottomRight.iX);
       
    84         TInt yMin = Min(topLeft.iY, bottomRight.iY);
       
    85         TInt yMax = Max(topLeft.iY, bottomRight.iY);
       
    86 
       
    87         rect.iTl.iX = xMin;
       
    88         rect.iTl.iY = yMin;
       
    89         rect.iBr.iX = xMax + 1; // exclusive
       
    90         rect.iBr.iY = yMax + 1; // exclusive
       
    91     }
       
    92     else
       
    93     {
       
    94         rect.iTl = aTransform(aRect.iTl);
       
    95         rect.iBr = rect.iTl;    // exclusive
       
    96         ASSERT(rect.IsEmpty());
       
    97     }
       
    98 
       
    99     return rect;
       
   100 }
       
   101 
       
   102 /**
       
   103  * Calculate clipped source and destination rectangles for
       
   104  * src to dst transform aSrcTransform. This function calculates
       
   105  * the largest corresponding subrectangles of aDstRect and aSrcRect
       
   106  * that also lie within aDstClipRect and aSrcClipRect respectively.
       
   107  */
       
   108 EXPORT_C void ClipTransformRect
       
   109 (
       
   110     TRect& aDstRect,                    // in/out
       
   111     TRect& aSrcRect,                    // in/out
       
   112     const TRect& aDstClipRect,          // input
       
   113     const TRect& aSrcClipRect,          // input
       
   114     TLcdTransform& aSrcTransform        // src to target transform
       
   115 )
       
   116 {
       
   117     TRect transRect;
       
   118 
       
   119     TLcdTransform& forward = aSrcTransform;
       
   120     TLcdTransform  reverse = forward.Inverse();
       
   121 
       
   122     //
       
   123     // Clip source rectangle.
       
   124     //
       
   125     aSrcRect.Intersection(aSrcClipRect);
       
   126 
       
   127     //
       
   128     // Clip destination rectangle.
       
   129     //
       
   130     aDstRect.Intersection(aDstClipRect);
       
   131 
       
   132     //
       
   133     // Transform clipped source rectangle to destination coords.
       
   134     //
       
   135     transRect = TransformRect(forward, aSrcRect);
       
   136 
       
   137     //
       
   138     // Clip againts destination rectangle.
       
   139     //
       
   140     aDstRect.Intersection(transRect);
       
   141 
       
   142     //
       
   143     // Reverse transform rectangle clipped against destination clip rect to source coords
       
   144     //
       
   145     aSrcRect = TransformRect(reverse, aDstRect);
       
   146 }
       
   147 
       
   148 //
       
   149 // Clip source and dst rects against source and dst clip rects, maintaining aspect ratio
       
   150 //
       
   151 EXPORT_C void ClipRects(TRect& aDstRect, const TRect& aDstClip, TRect& aSrcRect, const TRect& aSrcClip)
       
   152 {
       
   153     TPoint vector = aDstRect.iTl - aSrcRect.iTl;
       
   154 
       
   155     aDstRect.Intersection(aDstClip);
       
   156     aSrcRect.Intersection(aSrcClip);
       
   157 
       
   158     TRect srcRect(aDstRect.iTl - vector, aDstRect.Size());
       
   159     aSrcRect.Intersection(srcRect);
       
   160 
       
   161     aDstRect = aSrcRect;
       
   162     aDstRect.Move(vector);
       
   163 
       
   164     ASSERT(aDstRect.Size() == aSrcRect.Size());
       
   165 }
       
   166 
       
   167 
       
   168 #ifdef _DEBUG
       
   169 void TestDbgCheckTransform()
       
   170 {
       
   171     TLcdTransform transform = Translate(TPoint(-1,1));
       
   172 
       
   173     TRect srcRect(TPoint(1,0), TSize(1,2));
       
   174     TRect dstRect(TPoint(0,0), TSize(1,2));
       
   175 
       
   176     ASSERT(CheckTransform(dstRect, srcRect, transform));
       
   177 }
       
   178 #endif
       
   179 
       
   180 EXPORT_C TBool CheckTransform(const TRect& aDstRect, const TRect& aSrcRect, const TLcdTransform aTransform)
       
   181 {
       
   182     TPoint dst[4];
       
   183     TPoint src[4];
       
   184     const TPoint* dstPtr[4];
       
   185 
       
   186     TInt x1,y1,x2,y2;
       
   187 
       
   188     x1=aDstRect.iTl.iX;
       
   189     y1=aDstRect.iTl.iY;
       
   190     x2=aDstRect.iBr.iX-1;
       
   191     y2=aDstRect.iBr.iY-1;
       
   192 
       
   193     dst[0].iX = x1;
       
   194     dst[0].iY = y1;
       
   195 
       
   196     dst[1].iX = x2;
       
   197     dst[1].iY = y1;
       
   198 
       
   199     dst[2].iX = x1;
       
   200     dst[2].iY = y2;
       
   201 
       
   202     dst[3].iX = x2;
       
   203     dst[3].iY = y2;
       
   204 
       
   205     x1=aSrcRect.iTl.iX;
       
   206     y1=aSrcRect.iTl.iY;
       
   207     x2=aSrcRect.iBr.iX-1;
       
   208     y2=aSrcRect.iBr.iY-1;
       
   209 
       
   210     src[0].iX = x1;
       
   211     src[0].iY = y1;
       
   212 
       
   213     src[1].iX = x2;
       
   214     src[1].iY = y1;
       
   215 
       
   216     src[2].iX = x1;
       
   217     src[2].iY = y2;
       
   218 
       
   219     src[3].iX = x2;
       
   220     src[3].iY = y2;
       
   221 
       
   222     dstPtr[0] = &dst[0];
       
   223     dstPtr[1] = &dst[1];
       
   224     dstPtr[2] = &dst[2];
       
   225     dstPtr[3] = &dst[3];
       
   226 
       
   227     TPoint p;
       
   228 
       
   229     TBool f=ETrue;
       
   230     for (TInt i=0; i<4 && f; i++)
       
   231     {
       
   232         f = EFalse;
       
   233         p = aTransform(src[i]);
       
   234         for (TInt j=4; --j>=0;)
       
   235         {
       
   236             if (dstPtr[j] && (*dstPtr[j] == p))
       
   237             {
       
   238                 dstPtr[j] = NULL;
       
   239                 f = ETrue;
       
   240                 break;
       
   241             }
       
   242         }
       
   243         ASSERT(f);
       
   244     }
       
   245 
       
   246     return f;
       
   247 }
       
   248 
       
   249 EXPORT_C TBool CheckBounds(const TSize& aDstSize, const TSize& aSrcSize, const TRect& aDstRect, const TLcdTransform& aTransform)
       
   250 {
       
   251     if (!aDstRect.IsEmpty())
       
   252     {
       
   253         TPoint p1 = aDstRect.iTl;
       
   254         TPoint p2 = aDstRect.iBr;
       
   255 
       
   256         p2.iX--;
       
   257         p2.iY--;
       
   258 
       
   259         TRect dstClipRect(aDstSize);
       
   260 
       
   261         ASSERT(dstClipRect.Contains(p1));
       
   262         if (!dstClipRect.Contains(p1))
       
   263         {
       
   264             return EFalse;
       
   265         }
       
   266 
       
   267         ASSERT(dstClipRect.Contains(p2));
       
   268         if (!dstClipRect.Contains(p2))
       
   269         {
       
   270             return EFalse;
       
   271         }
       
   272 
       
   273         p1 = aTransform(p1);
       
   274         p2 = aTransform(p2);
       
   275 
       
   276         TRect srcClipRect(aSrcSize);
       
   277 
       
   278         ASSERT(srcClipRect.Contains(p1));
       
   279         if (!srcClipRect.Contains(p1))
       
   280         {
       
   281             return EFalse;
       
   282         }
       
   283 
       
   284         ASSERT(srcClipRect.Contains(p2));
       
   285         if (!srcClipRect.Contains(p2))
       
   286         {
       
   287             return EFalse;
       
   288         }
       
   289     }
       
   290     return ETrue;
       
   291 }