photosgallery/slideshow/utils/shwgeometryutilities.h
changeset 0 4e91876724a2
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2007-2008 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:    Thin callback wrapper
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // include guard for the whole file content
       
    22 #ifndef __SHWGEOMETRYUTILITIES_H__
       
    23 #define __SHWGEOMETRYUTILITIES_H__
       
    24 
       
    25 namespace NShwGeometryUtilities
       
    26     {
       
    27     /**
       
    28      * Utility to calculate the fitting for coordinates and maintaining the 
       
    29      * aspect ratio
       
    30      * @param aFitted reference to second coordinate, this value gets 
       
    31      *          multiplied with aNewAssigned / aAssigned
       
    32      * @param aAssigned reference to first coordinate, this value gets 
       
    33      *          assigned to aNewAssigned
       
    34      * @param aNewAssigned new value for first coordinate
       
    35      * 
       
    36      * Example: X coordinate needs to be assigned a new value X2 and you need
       
    37      * to calculate new Y coordinate maintaining the aspect ratio:
       
    38      * FitDimension( Y, X, X2 );
       
    39      * 
       
    40      * @author Kimmo Hoikka
       
    41      */
       
    42     template< typename T>
       
    43     void FitDimension( T& aFitted, T& aAssigned, T aNewAssigned )
       
    44         {
       
    45         // formula is:
       
    46         // aFitted = ( aFitted *  aNewAssigned ) / aAssigned
       
    47         // aAssigned = aNewAssigned
       
    48         // calculate second coordinate
       
    49         aFitted =
       
    50             ( aFitted * aNewAssigned ) /
       
    51                 aAssigned;
       
    52         // assign the first coordinate
       
    53         aAssigned = aNewAssigned;
       
    54         }
       
    55 
       
    56     /**
       
    57      * Utility to fit width and height inside given box
       
    58      * maintaining the aspect ratio. Fit inside means that the image
       
    59      * does not fill the whole box and it is not stretched.
       
    60      * @param aWidth reference to the width to fit
       
    61      * @param aHeight reference to the height to fit
       
    62      * @param aBoxWidth the bounding box width
       
    63      * @param aBoxHeight the bounding box height
       
    64      */
       
    65     template< typename T>
       
    66     void FitInsideBox( T& aWidth, T& aHeight, T aBoxWidth, T aBoxHeight )
       
    67         {
       
    68         // calculate width difference
       
    69         T wdiff = aWidth - aBoxWidth;
       
    70         // calculate height difference
       
    71         T hdiff = aHeight - aBoxHeight;
       
    72         // check if wdiff is larger and positive
       
    73         if( ( wdiff > hdiff ) && wdiff > 0 )
       
    74             {
       
    75             // width is larger and too big for box so need to fit height
       
    76             FitDimension( aHeight, aWidth, aBoxWidth );
       
    77             }
       
    78         else if( hdiff > 0 )
       
    79             {
       
    80             // height is larger and too big for box so need to fit width
       
    81             FitDimension( aWidth, aHeight, aBoxHeight );
       
    82             }
       
    83         }
       
    84 
       
    85     /**
       
    86      * Utility to set width and height to cover given box
       
    87      * maintaining the aspect ratio. Fit cover means that the image
       
    88      * fills the whole box and some part of it does not fit inside the box.
       
    89      * @param aWidth reference to the width to fit
       
    90      * @param aHeight reference to the height to fit
       
    91      * @param aBoxWidth the bounding box width
       
    92      * @param aBoxHeight the bounding box height
       
    93      */
       
    94     template< typename T>
       
    95     void FitToCoverBox( T& aWidth, T& aHeight, T aBoxWidth, T aBoxHeight )
       
    96         {
       
    97         // try to fit with width first
       
    98         T newWidth = aWidth;
       
    99         T newHeight = aHeight;
       
   100         // calculate new height according to the width change
       
   101         FitDimension( newHeight, newWidth, aBoxWidth );
       
   102         // check if height is inside box
       
   103         if( newHeight < aBoxHeight )
       
   104             {
       
   105             // ok, it did not fill so fit according to height
       
   106             FitDimension( aWidth, aHeight, aBoxHeight );
       
   107             }
       
   108         else
       
   109             {
       
   110             // did fill the image so assign new values
       
   111             aWidth = newWidth;
       
   112             aHeight = newHeight;
       
   113             }
       
   114         }
       
   115     }
       
   116  
       
   117 
       
   118 #endif // __SHWGEOMETRYUTILITIES_H__