imagehandlinglib/Src/CIHLBitmap.cpp
changeset 54 48dd0f169f0d
parent 42 2e2a89493e2b
equal deleted inserted replaced
42:2e2a89493e2b 54:48dd0f169f0d
     1 /*
       
     2 * Copyright (c) 2004 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:  Default implementation of bitmap class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "CIHLBitmap.h"
       
    21 #include <fbs.h>
       
    22 
       
    23 // ======================== STATIC FACTORY FUNCTIONS ===========================
       
    24 // -----------------------------------------------------------------------------
       
    25 // IHLBitmap::CreateL
       
    26 // -----------------------------------------------------------------------------
       
    27 //
       
    28 EXPORT_C MIHLBitmap* IHLBitmap::CreateL()
       
    29 	{
       
    30 	return CIHLBitmap::NewL();
       
    31 	}
       
    32 
       
    33 // ============================ MEMBER FUNCTIONS ===============================
       
    34 // -----------------------------------------------------------------------------
       
    35 // C++ default constructor can NOT contain any code, that
       
    36 // might leave.
       
    37 // -----------------------------------------------------------------------------
       
    38 CIHLBitmap::CIHLBitmap()
       
    39     {
       
    40     }
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 // Second phase constructors. Can leave.
       
    45 // -----------------------------------------------------------------------------
       
    46 void CIHLBitmap::ConstructL()
       
    47 	{
       
    48 	iBitmap = new (ELeave) CFbsBitmap;
       
    49 	iMask = new (ELeave) CFbsBitmap;
       
    50 	}
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 // Two-phased constructors.
       
    55 // -----------------------------------------------------------------------------
       
    56 CIHLBitmap* CIHLBitmap::NewL()
       
    57 	{
       
    58     CIHLBitmap* self = new( ELeave ) CIHLBitmap;
       
    59 	CleanupStack::PushL( self );
       
    60     self->ConstructL();
       
    61 	CleanupStack::Pop(); // self
       
    62     return self;
       
    63 	}
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // Destructor
       
    67 // -----------------------------------------------------------------------------
       
    68 CIHLBitmap::~CIHLBitmap()
       
    69     {
       
    70 	delete iBitmap;
       
    71 	delete iMask;
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CIHLBitmap::Create
       
    76 // -----------------------------------------------------------------------------
       
    77 TInt CIHLBitmap::Create( const TSize& aSize, TDisplayMode aDisplayMode )
       
    78 	{
       
    79 	Reset();
       
    80 	return iBitmap->Create( aSize, aDisplayMode );
       
    81 	}
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CIHLBitmap::Create
       
    85 // -----------------------------------------------------------------------------
       
    86 TInt CIHLBitmap::Create( const TSize& aSize, TDisplayMode aBitmapDisplayMode,
       
    87 											 TDisplayMode aMaskDisplayMode )
       
    88 	{
       
    89     Reset();
       
    90 	TInt err( KErrArgument );
       
    91     if( aMaskDisplayMode == EGray2 || aMaskDisplayMode == EGray256 )
       
    92         {
       
    93     	err = iBitmap->Create( aSize, aBitmapDisplayMode );
       
    94     	if( err )
       
    95     		{
       
    96     		return err;
       
    97     		}
       
    98     	err = iMask->Create( aSize, aMaskDisplayMode );
       
    99     	if( err )
       
   100     		{
       
   101     		iBitmap->Reset();
       
   102     		}
       
   103         }
       
   104 	return err;
       
   105 	}
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // CIHLBitmap::Copy
       
   109 // -----------------------------------------------------------------------------
       
   110 TInt CIHLBitmap::Copy( const CFbsBitmap& aBitmap, TBool aDuplicate )
       
   111 	{
       
   112 	Reset();
       
   113 	TInt err( KErrNone );
       
   114 	TInt bitmapHandle( aBitmap.Handle() );
       
   115 	if( bitmapHandle )
       
   116 		{
       
   117 		if( aDuplicate )
       
   118 			{
       
   119 			err = iBitmap->Duplicate( bitmapHandle );
       
   120 			}
       
   121 		else
       
   122 			{
       
   123 			err = CopyBitmap( aBitmap, *iBitmap );
       
   124 			}
       
   125 		}
       
   126 	return err;
       
   127 	}
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CIHLBitmap::Copy
       
   131 // -----------------------------------------------------------------------------
       
   132 TInt CIHLBitmap::Copy( const CFbsBitmap& aBitmap, const CFbsBitmap& aMask, TBool aDuplicate )
       
   133 	{
       
   134 	Reset();
       
   135 	TInt bitmapHandle( aBitmap.Handle() );
       
   136 	TInt maskHandle( aMask.Handle() );
       
   137 	if( bitmapHandle && maskHandle &&
       
   138 		aBitmap.SizeInPixels() != aMask.SizeInPixels() )
       
   139 		{
       
   140 		return KErrArgument;
       
   141 		}
       
   142 
       
   143 	if( maskHandle )
       
   144 		{
       
   145 		switch( aMask.DisplayMode() )
       
   146 		    {
       
   147 		    case EGray2:
       
   148 		    case EGray256:
       
   149 		        {
       
   150 		        break;
       
   151 		        }
       
   152 		    default:
       
   153 		        {
       
   154 		        return KErrArgument;
       
   155 		        }
       
   156 		    }
       
   157 		}
       
   158 
       
   159 	TInt err( KErrNone );
       
   160 	if( bitmapHandle )
       
   161 		{
       
   162 		if( aDuplicate )
       
   163 			{
       
   164 			err = iBitmap->Duplicate( bitmapHandle );
       
   165 			}
       
   166 		else
       
   167 			{
       
   168 			err = CopyBitmap( aBitmap, *iBitmap );
       
   169 			}
       
   170 		}
       
   171 
       
   172 	if( !err && maskHandle )
       
   173 		{
       
   174 		if( aDuplicate )
       
   175 			{
       
   176 			err = iMask->Duplicate( maskHandle );
       
   177 			}
       
   178 		else
       
   179 			{
       
   180 			err = CopyBitmap( aMask, *iMask );
       
   181 			}
       
   182 		if( err )
       
   183 			{
       
   184 			iBitmap->Reset();
       
   185 			}
       
   186 		}
       
   187 	return err;
       
   188 	}
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // CIHLBitmap::Copy
       
   192 // -----------------------------------------------------------------------------
       
   193 TInt CIHLBitmap::Copy( const MIHLBitmap& aBitmap, TBool aDuplicate )
       
   194 	{
       
   195 	return Copy( aBitmap.Bitmap(), aBitmap.Mask(), aDuplicate );
       
   196 	}
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CIHLBitmap::Reset
       
   200 // -----------------------------------------------------------------------------
       
   201 void CIHLBitmap::Reset()
       
   202 	{
       
   203 	iBitmap->Reset();
       
   204 	iMask->Reset();
       
   205 	iEditorPtr = NULL;
       
   206 	iEditorValue = 0;
       
   207 	}
       
   208 
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // CIHLBitmap::IsCreated
       
   212 // -----------------------------------------------------------------------------
       
   213 TBool CIHLBitmap::IsCreated() const
       
   214 	{
       
   215 	return ( iBitmap->Handle() != 0 );
       
   216 	}
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CIHLBitmap::Bitmap
       
   220 // -----------------------------------------------------------------------------
       
   221 const CFbsBitmap& CIHLBitmap::Bitmap() const
       
   222 	{
       
   223 	return *iBitmap;
       
   224 	}
       
   225 
       
   226 // -----------------------------------------------------------------------------
       
   227 // CIHLBitmap::HasMask
       
   228 // -----------------------------------------------------------------------------
       
   229 TBool CIHLBitmap::HasMask() const
       
   230 	{
       
   231 	return ( iMask->Handle() != 0 );
       
   232 	}
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CIHLBitmap::Mask
       
   236 // -----------------------------------------------------------------------------
       
   237 const CFbsBitmap& CIHLBitmap::Mask() const
       
   238 	{
       
   239 	return *iMask;
       
   240 	}
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CIHLBitmap::Draw
       
   244 // -----------------------------------------------------------------------------
       
   245 void CIHLBitmap::Draw( CBitmapContext& aContext, const TPoint& aPoint ) const
       
   246 	{
       
   247 	if( iBitmap->Handle() )
       
   248 		{
       
   249 		if( iMask->Handle() )
       
   250 			{
       
   251 			aContext.BitBltMasked( aPoint, iBitmap, iBitmap->SizeInPixels(), iMask, EFalse );
       
   252 			}
       
   253 		else
       
   254 			{
       
   255 			aContext.BitBlt( aPoint, iBitmap, iBitmap->SizeInPixels() );
       
   256 			}
       
   257 		}
       
   258 	}
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CIHLBitmap::Draw
       
   262 // -----------------------------------------------------------------------------
       
   263 void CIHLBitmap::Draw( CBitmapContext& aContext, const TPoint& aPoint,
       
   264                        const TRect& aSourceRect ) const
       
   265 	{
       
   266 	if( iBitmap->Handle() )
       
   267 		{
       
   268 		if( iMask->Handle() )
       
   269 			{
       
   270 			aContext.BitBltMasked( aPoint, iBitmap, aSourceRect, iMask, EFalse );
       
   271 			}
       
   272 		else
       
   273 			{
       
   274 			aContext.BitBlt( aPoint, iBitmap, aSourceRect );
       
   275 			}
       
   276 		}
       
   277 	}
       
   278 
       
   279 // Internal interface
       
   280 // -----------------------------------------------------------------------------
       
   281 // CIHLBitmap::SetFilename
       
   282 // -----------------------------------------------------------------------------
       
   283 CFbsBitmap& CIHLBitmap::BitmapModifyable()
       
   284 	{
       
   285 	return *iBitmap;
       
   286 	}
       
   287 
       
   288 // -----------------------------------------------------------------------------
       
   289 // CIHLBitmap::SetFilename
       
   290 // -----------------------------------------------------------------------------
       
   291 CFbsBitmap& CIHLBitmap::MaskModifyable()
       
   292 	{
       
   293 	return *iMask;
       
   294 	}
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // CIHLBitmap::SetEditorPtr
       
   298 // -----------------------------------------------------------------------------
       
   299 void CIHLBitmap::SetEditorPtr( const TAny* aEditorPtr )
       
   300 	{
       
   301 	iEditorPtr = aEditorPtr;
       
   302 	}
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CIHLBitmap::SetEditorValue
       
   306 // -----------------------------------------------------------------------------
       
   307 void CIHLBitmap::SetEditorValue( TInt aEditorValue )
       
   308 	{
       
   309 	iEditorValue = aEditorValue;
       
   310 	}
       
   311 
       
   312 // -----------------------------------------------------------------------------
       
   313 // CIHLBitmap::EditorPtr
       
   314 // -----------------------------------------------------------------------------
       
   315 const TAny* CIHLBitmap::EditorPtr() const
       
   316 	{
       
   317 	return iEditorPtr;
       
   318 	}
       
   319 
       
   320 // -----------------------------------------------------------------------------
       
   321 // CIHLBitmap::FrameIndex
       
   322 // -----------------------------------------------------------------------------
       
   323 TInt CIHLBitmap::EditorValue() const
       
   324 	{
       
   325 	return iEditorValue;
       
   326 	}
       
   327 
       
   328 // Private methods
       
   329 // -----------------------------------------------------------------------------
       
   330 // CIHLBitmap::CopyBitmap
       
   331 // -----------------------------------------------------------------------------
       
   332 TInt CIHLBitmap::CopyBitmap( const CFbsBitmap& aSource, CFbsBitmap& aDestination )
       
   333 	{
       
   334 	TSize size( aSource.SizeInPixels() );
       
   335 	TDisplayMode displayMode( aSource.DisplayMode() );
       
   336 	TInt err( aDestination.Create( size, displayMode ) );
       
   337 	if( !err )
       
   338 		{
       
   339 		err = CopyBitmapData( aSource, aDestination, size, displayMode );
       
   340 		if( err )
       
   341 			{
       
   342 			aDestination.Reset();
       
   343 			}
       
   344 		}
       
   345 	return err;
       
   346 	}
       
   347 
       
   348 // -----------------------------------------------------------------------------
       
   349 // CIHLBitmap::CopyBitmapData
       
   350 // -----------------------------------------------------------------------------
       
   351 TInt CIHLBitmap::CopyBitmapData( const CFbsBitmap& aSource, CFbsBitmap& aDestination,
       
   352 								 const TSize& aSize, const TDisplayMode& aDisplayMode )
       
   353 	{
       
   354 	HBufC8* scanLine = HBufC8::New( aSource.ScanLineLength( aSize.iWidth, aDisplayMode ) );
       
   355 	if( scanLine )
       
   356 		{
       
   357 		TPtr8 scanPtr( scanLine->Des() );
       
   358 		TPoint pp;
       
   359 		for( pp.iY = 0; pp.iY < aSize.iHeight; ++pp.iY )
       
   360 			{
       
   361 			aSource.GetScanLine( scanPtr, pp, aSize.iWidth, aDisplayMode );
       
   362 			aDestination.SetScanLine( scanPtr, pp.iY );
       
   363 			}
       
   364 
       
   365 		delete scanLine;
       
   366 		return KErrNone;
       
   367 		}
       
   368 	return KErrNoMemory; // scanLine alloc failed
       
   369 	}
       
   370 
       
   371 //  End of File