graphicstest/graphicstestharness/src/graphicsimagecomparison.cpp
changeset 0 5d03bc08d59c
child 36 01a6848ebfd7
child 163 bbf46f59e123
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @test
       
    19  @internalComponent - Internal Symbian test code
       
    20 */
       
    21 
       
    22 #include "graphicsimagecomparison.h"
       
    23 
       
    24 /**
       
    25 Compares the contents of a rectangular region of one bitmap with a similarly sized
       
    26 rectangular region of another bitmap.
       
    27 
       
    28 @param aCompareSize, a const reference to a TSize object denoting the size of the
       
    29 rectangular region for comparison.Negative and zero dimensions of this argument can
       
    30 be passed in without returning a KErrArgument error.
       
    31 @param aBitmap1Point, a const reference to a TPoint object denoting the top left
       
    32 point of the rectangle in aBitmap1 used for comparison..
       
    33 @param aBitmap2Point, a const reference to a TPoint object denoting the top left
       
    34 point of the rectangle in aBitmap2 used for comparison.
       
    35 @param aBitmap1, a const reference to the first CFbsBitmap for use in comparison.
       
    36 @param aBitmap2, a const reference to the second CFbsBitmap for use in comparison.
       
    37 @param aComparisonMask, a bit mask to be applied to the bitmap pixel data before
       
    38 performing the comparison, in the form 0xAARRGGBB. Defaults to 0xFFFFFFFF 
       
    39 @pre The rectanglular comparison region must reside wholly inside both of the bitmaps
       
    40 @return KErrNone if the pixels contained in the bitmap rectangles are an exact match
       
    41 or, otherwise, the count of the first unmatched pixel. Pixels are compared from TRUE
       
    42 top-left to bottom-right in the horizontal direction (TRUE to cope with negative
       
    43 dimensions of the aCompareSize object) An area of zero size will return KErrNone so
       
    44 long as the pre-conditions are satisfied.
       
    45 KErrArgument if the pre-conditions are not met.
       
    46 */
       
    47 EXPORT_C TInt CTGraphicsImageComparison::CompareBitmaps(const TSize& aCompareSize,
       
    48                                                         const TPoint& aBitmap1Point,
       
    49                                                         const TPoint& aBitmap2Point,
       
    50                                                         const CFbsBitmap& aBitmap1,
       
    51                                                         const CFbsBitmap& aBitmap2,
       
    52                                                         const TUint32 aComparisonMask)
       
    53 	{
       
    54 	TInt err = CheckArguments(aCompareSize,
       
    55                               aBitmap1Point,
       
    56                               aBitmap2Point,
       
    57                               aBitmap1,
       
    58                               aBitmap2);
       
    59 
       
    60 	if(err == KErrNone)
       
    61 		{
       
    62 		//Take local copies as cannot modify static arguments
       
    63 		TSize localSize(aCompareSize);
       
    64 		TPoint localPoint1(aBitmap1Point);
       
    65 		TPoint localPoint2(aBitmap2Point);
       
    66 
       
    67 		//Cope with negative aCompareSize dimensions
       
    68 		if(aCompareSize.iWidth < 0)
       
    69 			{
       
    70 			localSize.iWidth = -localSize.iWidth;
       
    71 			localPoint1.iX = localPoint1.iX - localSize.iWidth;
       
    72 			localPoint2.iX = localPoint2.iX - localSize.iWidth;
       
    73 			}
       
    74 
       
    75 		if(aCompareSize.iHeight < 0)
       
    76 			{
       
    77 			localSize.iHeight = -localSize.iHeight;
       
    78 			localPoint1.iY = localPoint1.iY - localSize.iHeight;
       
    79 			localPoint2.iY = localPoint2.iY - localSize.iHeight;
       
    80 			}
       
    81 
       
    82         // Set up buffers for obtaining scanlines
       
    83         TInt scanLineLength1 = aBitmap1.ScanLineLength(localSize.iWidth, ERgb);
       
    84 		TUint8* buffer1 = new TUint8[scanLineLength1];
       
    85 		if(!buffer1)
       
    86 		    {
       
    87 		    return KErrNoMemory;
       
    88 		    }
       
    89         TPtr8 scanLine1(buffer1, scanLineLength1, scanLineLength1);
       
    90 		TInt scanLineLength2 = aBitmap2.ScanLineLength(localSize.iWidth, ERgb);
       
    91         TUint8* buffer2 = new TUint8[scanLineLength2];
       
    92         if(!buffer2)
       
    93             {
       
    94             delete[] buffer1;
       
    95             return KErrNoMemory;
       
    96             }
       
    97         TPtr8 scanLine2(buffer1, scanLineLength2, scanLineLength2);
       
    98 
       
    99         //Perform scanline to scanline comparison without comparison mask
       
   100 	    for(TInt y=0; y<localSize.iHeight; y++)
       
   101             {
       
   102             aBitmap1.GetScanLine(scanLine1, localPoint1+TPoint(0,y), localSize.iWidth, ERgb);
       
   103             aBitmap2.GetScanLine(scanLine2, localPoint2+TPoint(0,y), localSize.iWidth, ERgb);
       
   104             
       
   105             if(aComparisonMask!=0xFFFFFFFF || scanLine1.Compare(scanLine2)!=0)
       
   106                 {
       
   107                 //Comparison mask has been set, or scanlines are not equal
       
   108                 //so perform pixel by pixel comparison using mask
       
   109                 TRgb pixel1, pixel2;
       
   110                 for(TInt x=0; x<localSize.iWidth; x++)
       
   111                     {
       
   112                     pixel1 = *(((TRgb*)buffer1) + x);
       
   113                     pixel2 = *(((TRgb*)buffer1) + x);
       
   114 
       
   115                     if( (pixel1.Internal()& aComparisonMask) != (pixel2.Internal()& aComparisonMask))
       
   116                         {
       
   117                         RDebug::Print(_L("x = %d y = %d  pixel1= %x pixel2 = %x"), x, y, pixel1.Internal(), pixel2.Internal());
       
   118                         delete[] buffer2;
       
   119                         delete[] buffer1;
       
   120                         return (y*localSize.iWidth + x) + 1;
       
   121                         }
       
   122                     }
       
   123                 }
       
   124            }
       
   125 	    delete[] buffer2;
       
   126 	    delete[] buffer1;
       
   127         }
       
   128     return err;
       
   129 	}
       
   130 
       
   131 /**
       
   132 Compares the contents of a rectangular region of a bitmap with a reference colour.
       
   133 
       
   134 @param aCompareSize, a const reference to a TSize object denoting the size of the
       
   135 rectangular region for comparison.Negative and zero dimensions of this argument can
       
   136 be passed in without returning a KErrArgument error.
       
   137 @param aBitmapPoint, a const reference to a TPoint object denoting the top left
       
   138 point of the rectangle in aBitmap1 used for comparison..
       
   139 @param aBitmap, a const reference to the CFbsBitmap for use in comparison.
       
   140 @param aColor, the colour to test for
       
   141 @param aComparisonMask, a bit mask to be applied to both the bitmap pixel data and the
       
   142 colour data before performing the comparison, in the form 0xAARRGGBB. Defaults to 0xFFFFFFFF 
       
   143 @pre The rectanglular comparison region must reside wholly inside the bitmap
       
   144 @return KErrNone if the pixels contained in the bitmap rectangles are an exact match
       
   145 to the reference colour or, otherwise, the count of the first unmatched pixel. Pixels
       
   146 are compared from TRUE top-left to bottom-right in the horizontal direction (TRUE to
       
   147 cope with negative dimensions of the aCompareSize object) An area of zero size will
       
   148 return KErrNone so long as the pre-conditions are satisfied.
       
   149 KErrArgument if the pre-conditions are not met.
       
   150 */
       
   151 EXPORT_C TInt CTGraphicsImageComparison::CompareBitmaps(const TSize& aCompareSize,
       
   152                                                         const TPoint& aBitmapPoint,
       
   153                                                         const CFbsBitmap& aBitmap,
       
   154                                                         const TRgb& aColor,
       
   155                                                         const TUint32 aComparisonMask)
       
   156 	{
       
   157 	TInt err = CheckArgumentBitmap(aCompareSize,
       
   158                                    aBitmapPoint,
       
   159                                    aBitmap);
       
   160 
       
   161 	if(err == KErrNone)
       
   162 		{
       
   163 		//Take local copies as cannot modify static arguments
       
   164 		TSize localSize(aCompareSize);
       
   165 		TPoint localPoint(aBitmapPoint);
       
   166 
       
   167 		//Cope with negative aCompareSize dimensions
       
   168 		if(aCompareSize.iWidth < 0)
       
   169 			{
       
   170 			localSize.iWidth = -localSize.iWidth;
       
   171 			localPoint.iX = localPoint.iX - localSize.iWidth;
       
   172 			}
       
   173 
       
   174 		if(aCompareSize.iHeight < 0)
       
   175 			{
       
   176 			localSize.iHeight = -localSize.iHeight;
       
   177 			localPoint.iY = localPoint.iY - localSize.iHeight;
       
   178 			}
       
   179 
       
   180         // Set up buffers for obtaining scanlines
       
   181         TInt scanLineLength = aBitmap.ScanLineLength(localSize.iWidth, ERgb);
       
   182         TUint8* buffer = new TUint8[scanLineLength];
       
   183         if(!buffer)
       
   184             {
       
   185             return KErrNoMemory;
       
   186             }
       
   187         TPtr8 scanLine(buffer, scanLineLength, scanLineLength);
       
   188 
       
   189         //Perform the pixel by pixel comparison
       
   190         TRgb pixel;
       
   191 		for(TInt y=0; y<localSize.iHeight; y++)
       
   192 	 		{
       
   193             aBitmap.GetScanLine(scanLine, localPoint+TPoint(0,y), localSize.iWidth, ERgb);
       
   194 	 		for(TInt x=0; x<localSize.iWidth; x++)
       
   195 				{
       
   196                 pixel = *(((TRgb*)buffer) + x);
       
   197 
       
   198                 if( (pixel.Internal()& aComparisonMask) != (aColor.Internal()& aComparisonMask))
       
   199 					{
       
   200 					RDebug::Print(_L("x = %d y = %d  pixel= %x reference colour = %x"), x, y, pixel.Internal(), aColor.Internal());
       
   201                     delete[] buffer;
       
   202 					return (y*localSize.iWidth + x) + 1;
       
   203 					}
       
   204 				}
       
   205 	 		}
       
   206 	    delete[] buffer;
       
   207 		}
       
   208     return err;
       
   209 	}
       
   210 
       
   211 TInt CTGraphicsImageComparison::CheckArguments(const TSize& aCompareSize,
       
   212                                                const TPoint& aBitmap1Point,
       
   213                                                const TPoint& aBitmap2Point,
       
   214                                                const CFbsBitmap& aBitmap1,
       
   215                                                const CFbsBitmap& aBitmap2)
       
   216 	{
       
   217 	//Test that the arguments are valid for both bitmaps
       
   218 	TInt err = CheckArgumentBitmap(aCompareSize, aBitmap1Point, aBitmap1);
       
   219 
       
   220 	if(err == KErrNone)
       
   221 		{
       
   222 		err = CheckArgumentBitmap(aCompareSize, aBitmap2Point, aBitmap2);
       
   223 		}
       
   224 
       
   225 	return err;
       
   226 	}
       
   227 
       
   228 TInt CTGraphicsImageComparison::CheckArgumentBitmap(const TSize& aSize,
       
   229                                                     const TPoint& aPoint,
       
   230                                                     const CFbsBitmap& aBitmap)
       
   231 	{
       
   232 	//Top left-hand corner of the comparison rectangle is outside of the bitmap
       
   233 	if( (aPoint.iX < 0) || (aPoint.iY < 0) )
       
   234 		{
       
   235 		return KErrArgument;
       
   236 		}
       
   237 
       
   238 	if(aSize.iWidth >= 0)
       
   239 		{
       
   240 		//Comparison rectangle is outside of the bitmap (rhs)
       
   241 		if(aPoint.iX + aSize.iWidth > aBitmap.SizeInPixels().iWidth)
       
   242 			{
       
   243 			return KErrArgument;
       
   244 			}
       
   245 		}
       
   246 	else
       
   247 		{
       
   248 		//Comparison rectangle is outside of the bitmap (lhs)
       
   249 		if(aPoint.iX + aSize.iWidth < 0)
       
   250 			{
       
   251 			return KErrArgument;
       
   252 			}
       
   253 		}
       
   254 
       
   255 	if(aSize.iHeight >= 0)
       
   256 		{
       
   257 		//Comparison rectangle is outside of the bitmap (bottom)
       
   258 		if(aPoint.iY + aSize.iHeight > aBitmap.SizeInPixels().iHeight)
       
   259 			{
       
   260 			return KErrArgument;
       
   261 			}
       
   262 		}
       
   263 	else
       
   264 		{
       
   265 		//Comparison rectangle is outside of the bitmap (top)
       
   266 		if(aPoint.iY + aSize.iHeight < 0)
       
   267 			{
       
   268 			return KErrArgument;
       
   269 			}
       
   270 		}
       
   271 
       
   272 	return KErrNone;
       
   273 	}