// ****************************************************************************
//
//  Function Name:	BitmapImageList::Add()
//
//  Description:		Adds one or more images or an icon to the image 
//                   list.
//
//  Returns:			Zero-based index of the first new image if 
//                   successful; otherwise -1.
//
//  Exceptions:		None
//
// ****************************************************************************
int RBitmapImageList::Add( RBitmapImage* pbmImage, YPlatformColor crMask )
{
	if (m_fMask)
	{
		RBitmapImage bmMask ;

		try
		{
			uLONG ulWidth  = pbmImage->GetWidthInPixels() ;
			uLONG ulHeight = pbmImage->GetHeightInPixels() ;

			bmMask.Initialize( ulWidth, ulHeight, 1 ) ;

			ROffscreenDrawingSurface dsDest ;
			ROffscreenDrawingSurface dsSrc;

			dsDest.SetImage( &bmMask ) ;

			dsSrc.SetImage( pbmImage ) ;
			dsSrc.SetFontColor( RSolidColor( !crMask ) ) ;
			dsSrc.SetFillColor( RSolidColor( crMask ) ) ;

			RIntRect rcRect( 0, 0, ulWidth, ulHeight ) ;
			dsDest.BlitDrawingSurface( dsSrc, rcRect, rcRect, kBlitSourceCopy ) ;

			dsSrc.ReleaseImage() ;
			dsDest.ReleaseImage() ;

			return Add( pbmImage, &bmMask ) ;
		}
		catch( YException )
		{
			return -1 ;
		}
	}

	return Add( pbmImage, (RBitmapImage*) NULL ) ;
}