javauis/lcdui_akn/lcdgd/src/collision.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 
       
    19 #include <fbs.h>
       
    20 #include "lcdtransform.h"
       
    21 #include "collision.h"
       
    22 
       
    23 /**
       
    24 Return ETrue if the mask pixel is fully opaque.
       
    25 */
       
    26 TBool IsOpaque(TDisplayMode aDisplayMode, TUint8* aAddress, TInt aLinePitch , TInt aX, TInt aY)
       
    27 {
       
    28     switch (aDisplayMode)
       
    29     {
       
    30         // binary transparent
       
    31     case EGray2:
       
    32     {
       
    33         TUint8 m1 = *(aAddress + aY*aLinePitch + (aX>>3));
       
    34         TUint8 srcMask1 = m1 >> (aX&0x7);
       
    35         return srcMask1 & 0x1;
       
    36     }
       
    37 
       
    38     // alpha transparent
       
    39     case EGray256:
       
    40     {
       
    41         TUint8 srcMask2 = *(TUint8*)(aAddress + aY*aLinePitch + aX*sizeof(TUint8));
       
    42         // semi-transparent is treated as transparent.
       
    43         return srcMask2 == 0xff;
       
    44     }
       
    45 
       
    46     // mask matched
       
    47     case EColor64K:
       
    48     {
       
    49         TUint16 srcMask3 = *(TUint16*)(aAddress + aY*aLinePitch + aX*sizeof(TUint16));
       
    50         return srcMask3 == 0xffff;
       
    51     }
       
    52     case EColor4K:
       
    53     {
       
    54         TUint16 srcMask4 = *(TUint16*)(aAddress + aY*aLinePitch + aX*sizeof(TUint16));
       
    55         return srcMask4 == 0xfff;
       
    56     }
       
    57     case EColor16MU:
       
    58     {
       
    59         TUint32 srcMask5  = *(TUint32*)(aAddress + aY*aLinePitch + aX*sizeof(TUint32));
       
    60         return srcMask5 == 0xffffff;
       
    61     }
       
    62 
       
    63     default:
       
    64     {
       
    65         __ASSERT_DEBUG(EFalse, User::Invariant());
       
    66     }
       
    67     }
       
    68     return EFalse;
       
    69 }
       
    70 
       
    71 TBool GenericDetectCollision
       
    72 (
       
    73     const TAcceleratedBitmapInfo*   aBitmap1,
       
    74     const TAcceleratedBitmapInfo*   aBitmap2,
       
    75     const TRect&                    aRect1,
       
    76     const TRect&                    aRect2,
       
    77     const TLcdTransform&            aTransform1,
       
    78     const TLcdTransform&            aTransform2
       
    79 )
       
    80 {
       
    81 
       
    82     // if only one mask, just need to check if it has any opaque pixels
       
    83     if (aBitmap1 == NULL)
       
    84     {
       
    85         return ::HasOpaquePixel(aBitmap2, aRect2);
       
    86     }
       
    87     if (aBitmap2 == NULL)
       
    88     {
       
    89         return ::HasOpaquePixel(aBitmap1, aRect1);
       
    90     }
       
    91 
       
    92     TLcdTransform transform = aTransform1 * aTransform2.Inverse();
       
    93     TRect rect = aRect2;
       
    94 
       
    95     TInt x1 = rect.iTl.iX;
       
    96     TInt x2 = rect.iBr.iX-1;    // inclusive!
       
    97     TInt y  = rect.iTl.iY;
       
    98     TInt h  = rect.Height();
       
    99 
       
   100     TPoint srcPoint = transform(rect.iTl);
       
   101     TInt dudx = transform.iDuDx;
       
   102     TInt dudy = transform.iDuDy;
       
   103     TInt dvdx = transform.iDvDx;
       
   104     TInt dvdy = transform.iDvDy;
       
   105     TInt u0   = srcPoint.iX;
       
   106     TInt v0   = srcPoint.iY;
       
   107     TInt u    = u0;
       
   108     TInt v    = v0;
       
   109 
       
   110     const TInt linePitch1   = aBitmap1->iLinePitch;
       
   111     const TInt linePitch2   = aBitmap2->iLinePitch;
       
   112     TUint8* address1        = aBitmap1->iAddress;
       
   113     TUint8* address2        = aBitmap2->iAddress;
       
   114 
       
   115     for (; h>0; --h)
       
   116     {
       
   117         u=u0;
       
   118         v=v0;
       
   119 
       
   120         for (TInt x=x1; x<=x2; x++)
       
   121         {
       
   122             TBool o1 = IsOpaque(aBitmap2->iDisplayMode, address2, linePitch2, x, y);
       
   123             TBool o2 = IsOpaque(aBitmap1->iDisplayMode, address1, linePitch1, u, v);
       
   124 
       
   125             if (o1 && o2)
       
   126             {
       
   127                 return ETrue;
       
   128             }
       
   129 
       
   130             u+=dudx;
       
   131             v+=dvdx;
       
   132         }
       
   133 
       
   134         u0+=dudy;
       
   135         v0+=dvdy;
       
   136         y++;
       
   137     }
       
   138 
       
   139     return EFalse;
       
   140 }
       
   141 
       
   142 
       
   143 TBool DetectCollisionGray2Gray2
       
   144 (
       
   145     const TAcceleratedBitmapInfo*   aGray2Bitmap1,
       
   146     const TAcceleratedBitmapInfo*   aGray2Bitmap2,
       
   147     const TRect&                    aRect1,
       
   148     const TRect&                    aRect2,
       
   149     const TLcdTransform&            aTransform1,
       
   150     const TLcdTransform&            aTransform2
       
   151 )
       
   152 {
       
   153 
       
   154     // if only one mask, just need to check if it has any opaque pixels
       
   155     if (aGray2Bitmap1 == NULL)
       
   156     {
       
   157         return ::HasOpaquePixelGray2(aGray2Bitmap2, aRect2);
       
   158     }
       
   159     if (aGray2Bitmap2 == NULL)
       
   160     {
       
   161         return ::HasOpaquePixelGray2(aGray2Bitmap1, aRect1);
       
   162     }
       
   163 
       
   164     ASSERT(aGray2Bitmap1->iDisplayMode == EGray2);
       
   165     ASSERT(aGray2Bitmap2->iDisplayMode == EGray2);
       
   166 
       
   167     TLcdTransform transform = aTransform1 * aTransform2.Inverse();
       
   168     TRect rect = aRect2;
       
   169 
       
   170     TInt x1 = rect.iTl.iX;
       
   171     TInt x2 = rect.iBr.iX-1;    // inclusive!
       
   172     TInt y  = rect.iTl.iY;
       
   173     TInt h  = rect.Height();
       
   174 
       
   175     TPoint srcPoint = transform(rect.iTl);
       
   176     TInt dudx = transform.iDuDx;
       
   177     TInt dudy = transform.iDuDy;
       
   178     TInt dvdx = transform.iDvDx;
       
   179     TInt dvdy = transform.iDvDy;
       
   180     TInt u0   = srcPoint.iX;
       
   181     TInt v0   = srcPoint.iY;
       
   182     TInt u    = u0;
       
   183     TInt v    = v0;
       
   184 
       
   185     const TInt linePitch1   = aGray2Bitmap1->iLinePitch;
       
   186     const TInt linePitch2   = aGray2Bitmap2->iLinePitch;
       
   187     TUint8* address1        = aGray2Bitmap1->iAddress;
       
   188     TUint8* address2        = aGray2Bitmap2->iAddress;
       
   189 
       
   190     address2 += y*linePitch2;
       
   191     for (; h>0; --h)
       
   192     {
       
   193         u=u0;
       
   194         v=v0;
       
   195 
       
   196         for (TInt x=x1; x<=x2; x++)
       
   197         {
       
   198             TUint8 m2 = address2[(x>>3)];
       
   199             TUint8 p2 = m2 >> (x&0x7);
       
   200 
       
   201             if (p2&0x1)
       
   202             {
       
   203                 TUint8 m1 = *(address1 + v*linePitch1 + (u>>3));
       
   204                 TUint8 p1 = m1 >> (u&0x7);
       
   205 
       
   206                 if (p1&0x1)
       
   207                 {
       
   208                     return ETrue;
       
   209                 }
       
   210             }
       
   211 
       
   212             u+=dudx;
       
   213             v+=dvdx;
       
   214         }
       
   215 
       
   216         u0+=dudy;
       
   217         v0+=dvdy;
       
   218 
       
   219         address2 += linePitch2;
       
   220     }
       
   221 
       
   222     return EFalse;
       
   223 }
       
   224 
       
   225 TBool DetectCollisionGray256Gray256
       
   226 (
       
   227     const TAcceleratedBitmapInfo*   aGray256Bitmap1,
       
   228     const TAcceleratedBitmapInfo*   aGray256Bitmap2,
       
   229     const TRect&                    aRect1,
       
   230     const TRect&                    aRect2,
       
   231     const TLcdTransform&            aTransform1,
       
   232     const TLcdTransform&            aTransform2
       
   233 )
       
   234 {
       
   235     // if only one mask, just need to check if it has any opaque pixels
       
   236     if (aGray256Bitmap1 == NULL)
       
   237     {
       
   238         return ::HasOpaquePixelGray256(aGray256Bitmap2, aRect2);
       
   239     }
       
   240     if (aGray256Bitmap2 == NULL)
       
   241     {
       
   242         return ::HasOpaquePixelGray256(aGray256Bitmap1, aRect1);
       
   243     }
       
   244 
       
   245     ASSERT(aGray256Bitmap1->iDisplayMode == EGray256);
       
   246     ASSERT(aGray256Bitmap2->iDisplayMode == EGray256);
       
   247 
       
   248     TLcdTransform transform = aTransform1 * aTransform2.Inverse();
       
   249     TRect rect = aRect2;
       
   250 
       
   251     const TInt x = rect.iTl.iX;
       
   252     const TInt y = rect.iTl.iY;
       
   253     const TInt w = rect.Width();
       
   254     TInt h = rect.Height();
       
   255 
       
   256     TPoint srcPoint = transform(rect.iTl);
       
   257     TInt dudx = transform.iDuDx;
       
   258     TInt dudy = transform.iDuDy;
       
   259     TInt dvdx = transform.iDvDx;
       
   260     TInt dvdy = transform.iDvDy;
       
   261     TInt u0   = srcPoint.iX;
       
   262     TInt v0   = srcPoint.iY;
       
   263 
       
   264 
       
   265     const TInt linePitch1   = aGray256Bitmap1->iLinePitch;
       
   266     const TInt linePitch2   = aGray256Bitmap2->iLinePitch;
       
   267     TUint8* address1        = aGray256Bitmap1->iAddress;
       
   268     TUint8* address2        = aGray256Bitmap2->iAddress;
       
   269 
       
   270     address2 += y*linePitch2 + x*sizeof(TUint8);
       
   271     for (; h>0; --h)
       
   272     {
       
   273         TInt u = u0;
       
   274         TInt v = v0;
       
   275 
       
   276         TUint8* ptr2 = address2;
       
   277         const TUint8* end = ptr2 + w;
       
   278 
       
   279         while (ptr2 < end)
       
   280         {
       
   281             TUint8 m2 = *ptr2++;
       
   282             if (m2 == 255)
       
   283             {
       
   284                 TUint8 m1 = *(TUint8*)(address1 + v*linePitch1 + u*sizeof(TUint8));
       
   285                 if (m1 == 255)
       
   286                 {
       
   287                     return ETrue;
       
   288                 }
       
   289             }
       
   290 
       
   291             u+=dudx;
       
   292             v+=dvdx;
       
   293         }
       
   294 
       
   295         u0+=dudy;
       
   296         v0+=dvdy;
       
   297         address2 += linePitch2;
       
   298     }
       
   299 
       
   300     return EFalse;
       
   301 }
       
   302 
       
   303 TBool DetectCollisionGray2Gray256
       
   304 (
       
   305     const TAcceleratedBitmapInfo*   aGray2Bitmap,
       
   306     const TAcceleratedBitmapInfo*   aGray256Bitmap,
       
   307     const TRect&                    aRect1,
       
   308     const TRect&                    aRect2,
       
   309     const TLcdTransform&            aTransform1,
       
   310     const TLcdTransform&            aTransform2
       
   311 )
       
   312 {
       
   313     // if only one mask, just need to check if it has any opaque pixels
       
   314     if (aGray2Bitmap == NULL)
       
   315     {
       
   316         return ::HasOpaquePixelGray256(aGray256Bitmap, aRect2);
       
   317     }
       
   318     if (aGray256Bitmap == NULL)
       
   319     {
       
   320         return ::HasOpaquePixelGray2(aGray2Bitmap, aRect1);
       
   321     }
       
   322 
       
   323     ASSERT(aGray2Bitmap->iDisplayMode == EGray2);
       
   324     ASSERT(aGray256Bitmap->iDisplayMode == EGray256);
       
   325 
       
   326     TLcdTransform transform = aTransform1 * aTransform2.Inverse();
       
   327     TRect rect = aRect2;
       
   328 
       
   329     const TInt x = rect.iTl.iX;
       
   330     const TInt y = rect.iTl.iY;
       
   331     const TInt w = rect.Width();
       
   332     TInt h = rect.Height();
       
   333 
       
   334     TPoint srcPoint = transform(rect.iTl);
       
   335     TInt dudx = transform.iDuDx;
       
   336     TInt dudy = transform.iDuDy;
       
   337     TInt dvdx = transform.iDvDx;
       
   338     TInt dvdy = transform.iDvDy;
       
   339     TInt u0   = srcPoint.iX;
       
   340     TInt v0   = srcPoint.iY;
       
   341 
       
   342     const TInt linePitch1   = aGray2Bitmap->iLinePitch;
       
   343     const TInt linePitch2   = aGray256Bitmap->iLinePitch;
       
   344     TUint8* address1        = aGray2Bitmap->iAddress;
       
   345     TUint8* address2        = aGray256Bitmap->iAddress;
       
   346 
       
   347     address2 += y*linePitch2 + x*sizeof(TUint8);
       
   348     for (; h>0; --h)
       
   349     {
       
   350         TInt u = u0;
       
   351         TInt v = v0;
       
   352 
       
   353         TUint8* ptr2 = address2;
       
   354         const TUint8* end = ptr2 + w;
       
   355         while (ptr2 < end)
       
   356         {
       
   357             TUint8 m2 = *ptr2++;
       
   358 
       
   359             if (m2 == 255)
       
   360             {
       
   361                 TUint8 m1 = *(address1 + v*linePitch1 + (u>>3));
       
   362                 TUint8 p1 = m1 >> (u&0x7);
       
   363 
       
   364                 if (p1&0x1)
       
   365                 {
       
   366                     return ETrue;
       
   367                 }
       
   368             }
       
   369 
       
   370             u+=dudx;
       
   371             v+=dvdx;
       
   372         }
       
   373 
       
   374         u0+=dudy;
       
   375         v0+=dvdy;
       
   376         address2 += linePitch2;
       
   377     }
       
   378 
       
   379     return EFalse;
       
   380 }
       
   381 
       
   382 TBool DetectCollisionGray256Gray2
       
   383 (
       
   384     const TAcceleratedBitmapInfo*   aGray256Bitmap,
       
   385     const TAcceleratedBitmapInfo*   aGray2Bitmap,
       
   386     const TRect&                    aRect1,
       
   387     const TRect&                    aRect2,
       
   388     const TLcdTransform&            aTransform1,
       
   389     const TLcdTransform&            aTransform2
       
   390 )
       
   391 {
       
   392     return DetectCollisionGray2Gray256
       
   393            (
       
   394                aGray2Bitmap,
       
   395                aGray256Bitmap,
       
   396                aRect2,
       
   397                aRect1,
       
   398                aTransform2,
       
   399                aTransform1
       
   400            );
       
   401 }
       
   402 
       
   403 TBool HasOpaquePixel
       
   404 (
       
   405     const TAcceleratedBitmapInfo*   aBitmap,
       
   406     const TRect&                    aRect
       
   407 )
       
   408 {
       
   409     const TInt linePitch    = aBitmap->iLinePitch;
       
   410     TUint8* address         = aBitmap->iAddress;
       
   411 
       
   412     TInt x1 = aRect.iTl.iX;
       
   413     TInt x2 = aRect.iBr.iX-1;   // inclusive!
       
   414     TInt y  = aRect.iTl.iY;
       
   415     TInt h  = aRect.Height();
       
   416 
       
   417     for (; h>0; --h)
       
   418     {
       
   419         for (TInt x=x1; x<=x2; x++)
       
   420         {
       
   421             if (IsOpaque(aBitmap->iDisplayMode, address, linePitch, x, y))
       
   422             {
       
   423                 return ETrue;
       
   424             }
       
   425         }
       
   426         y++;
       
   427     }
       
   428 
       
   429     return EFalse;
       
   430 }
       
   431 
       
   432 TBool HasOpaquePixelGray2
       
   433 (
       
   434     const TAcceleratedBitmapInfo*   aGray2Bitmap,
       
   435     const TRect&                    aRect
       
   436 )
       
   437 {
       
   438     ASSERT(aGray2Bitmap->iDisplayMode == EGray2);
       
   439 
       
   440     const TInt linePitch    = aGray2Bitmap->iLinePitch;
       
   441     TUint8* address         = aGray2Bitmap->iAddress;
       
   442 
       
   443     const TInt x1   = aRect.iTl.iX;
       
   444     const TInt x2   = aRect.iBr.iX-1;   // inclusive!
       
   445     const TInt y    = aRect.iTl.iY;
       
   446     TInt h  = aRect.Height();
       
   447 
       
   448     address += y*linePitch;
       
   449     for (; h>0; --h)
       
   450     {
       
   451         for (TInt x=x1; x<=x2; x++)
       
   452         {
       
   453             TUint8 m = address[(x>>3)];
       
   454             TUint8 p = m >> (x&0x7);
       
   455 
       
   456             if (p&0x1)
       
   457             {
       
   458                 return ETrue;
       
   459             }
       
   460         }
       
   461         address += linePitch;
       
   462     }
       
   463 
       
   464     return EFalse;
       
   465 }
       
   466 
       
   467 TBool HasOpaquePixelGray256
       
   468 (
       
   469     const TAcceleratedBitmapInfo*   aGray256Bitmap,
       
   470     const TRect&                    aRect
       
   471 )
       
   472 {
       
   473     ASSERT(aGray256Bitmap->iDisplayMode == EGray256);
       
   474 
       
   475     const TInt linePitch = aGray256Bitmap->iLinePitch;
       
   476     TUint8* address      = aGray256Bitmap->iAddress;
       
   477 
       
   478     const TInt x = aRect.iTl.iX;
       
   479     const TInt y = aRect.iTl.iY;
       
   480     const TInt w = aRect.Width();
       
   481     TInt h = aRect.Height();
       
   482 
       
   483     address += y*linePitch + x*sizeof(TUint8);
       
   484     for (; h>0; --h)
       
   485     {
       
   486         TUint8* ptr = address;
       
   487         const TUint8* end = ptr + w;
       
   488 
       
   489         while (ptr < end)
       
   490         {
       
   491             // semi-transparent is treated as transparent.
       
   492             TUint8 m = *ptr++;
       
   493             if (m == 255)
       
   494             {
       
   495                 return ETrue;
       
   496             }
       
   497         }
       
   498         address += linePitch;
       
   499     }
       
   500 
       
   501     return EFalse;
       
   502 }
       
   503