// ****************************************************************************
//
//  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, RBitmapImage* pbmMask )
{
	int nImageIndex = m_ulImageCount ;

	if (!m_ulImageCount)
	{
		// Copy the palette of the incoming image
	}

	RIntRect rcDestRect(0, 0, 
		pbmImage->GetWidthInPixels(), 
		pbmImage->GetHeightInPixels()) ;

	RIntSize szOffset((m_ulImageCount - 1) * m_szImage.m_dx, 0) ;
	rcDestRect.Offset( szOffset ) ;

	while (GetWidthInPixels() < (uLONG) rcDestRect.m_Right)
	{
		uLONG cx = GetWidthInPixels() + m_uwGrow * m_szImage.m_dx ;
		uLONG cy = GetHeightInPixels() ;

		// Resize the bitmaps
//		Resize( cx, cy ) ;
		
		if (m_fMask)
		{
//			m_bmMask.Resize( cx, cy ) ;
		}
	}

	ROffscreenDrawingSurface dsMem ;

	dsMem.SetImage( (RBitmapImage*) this ) ;
	pbmImage->Render( dsMem, rcDestRect ) ;
	dsMem.ReleaseImage() ;

	if (m_fMask)
	{
		dsMem.SetImage( &m_bmMask ) ;
		pbmMask->Render( dsMem, rcDestRect ) ;
		dsMem.ReleaseImage() ;
	}

	// Update the image count
	m_ulImageCount += pbmImage->GetWidthInPixels() / m_szImage.m_dx ;

	return nImageIndex ;
}
// ****************************************************************************
//
//  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 ) ;
}
//////////////////////////////////////////////////////////////////////////////
//
//	void ExportGraphicComponent( RComponentView* pComponentView, RMBCString *pInitialDir ) 
//
//	export a graphic to file from a component view
//
//////////////////////////////////////////////////////////////////////////////
void ExportGraphicComponent( RComponentView* pComponentView, RMBCString *pInitialDir /*=NULL*/) 
{
	RImageLibrary rLibrary ;
	CArray<int, int> arrFileFormats ;

	//
	// Build the export file filter list...
	//
	CString strFilter, str ;

	for (int i = 0; i < kNumFormats - 1; i++)
	{
		if (rLibrary.IsFormatSupported( (EImageFormat) kImageExportFilters[i][0] ))
		{
			str.LoadString( kImageExportFilters[i][1] ) ;
			strFilter += str ;

			arrFileFormats.Add( kImageExportFilters[i][0] ) ;
		}
	}

	TpsAssert( kImageExportFilters[i][0] == kImageFormatXRX, "Invalid export format!" ) ;
	str.LoadString( kImageExportFilters[i][1] ) ;  
	strFilter += str ;
	strFilter += "|" ;

	arrFileFormats.Add( kImageExportFilters[i][0] ) ;

	//
	// Create and execute the dialog...
	//
	RExportGraphicDlg dlg( pComponentView, strFilter ) ;

	// Load in the dialog title string
	CString strTitle ;
	strTitle.LoadString( STRING_EXPORT_IMAGE_DIALOG_TITLE ) ;
	dlg.m_ofn.lpstrTitle = (LPCTSTR) strTitle ;
	if( pInitialDir )
	{
		dlg.m_ofn.lpstrInitialDir = (LPCTSTR)*pInitialDir;
	}

	if (IDOK == dlg.DoModal())
	{
		RMBCString   strPathName = dlg.GetPathName() ;
		RIntSize     szImageSize = dlg.GetSize() ;

		_nFilterIndex = dlg.m_ofn.nFilterIndex ;
		EImageFormat eFormat = (EImageFormat) arrFileFormats[dlg.m_ofn.nFilterIndex - 1] ;
		RRealSize pictureSize  = pComponentView->GetReferenceSize() ;

		try
		{
			if (kImageFormatXRX == eFormat)
			{
				RRealSize outputSize( szImageSize.m_dx, szImageSize.m_dy ) ; //   = pictureSize ;
				RRealSize screenDPI    = ::GetScreenDPI() ;

				RVectorImage vectorImage ;
				vectorImage.SetSuggestedWidthInInches( ::PixelsToInches( outputSize.m_dx, (uLONG) screenDPI.m_dx ) ) ;
				vectorImage.SetSuggestedHeightInInches( ::PixelsToInches( outputSize.m_dy, (uLONG) screenDPI.m_dy ) ) ;

				// Create an offscreen drawing surface and set the picture
				ROffscreenDrawingSurface drawingSurface;
				drawingSurface.SetImage( &vectorImage );

				R2dTransform transform ;

				// Scale to the device DPI
				RRealSize deviceDPI = drawingSurface.GetDPI( );
				transform.PreScale( (YFloatType) deviceDPI.m_dx / screenDPI.m_dy, 
					(YFloatType) deviceDPI.m_dy / screenDPI.m_dy );

				transform.PreScale( 
					outputSize.m_dx / pictureSize.m_dx,
					outputSize.m_dy / pictureSize.m_dy ) ;

				// Render the component
				RRealRect outputRect( pictureSize ) ;
				pComponentView->Render( drawingSurface, transform, outputRect ) ;

				drawingSurface.ReleaseImage( );

				rLibrary.ExportImage( vectorImage, strPathName, kImageFormatXRX ) ;
				if (rLibrary.GetLastException() != kNoError)
				{
					throw rLibrary.GetLastException() ;
				}
			} // if (XRX)
			else
			{
				// Check for a image interface
				RImageInterface* pInterface = (RImageInterface *) 
					pComponentView->GetInterface( kImageInterfaceId ) ;

				if (pInterface)
				{
					pInterface->Export( strPathName, eFormat ) ;
					delete pInterface ;
				}
				else
				{
					// Initialize the new bitmap at a bit depth of 24
					RBitmapImage image ;
					image.Initialize( szImageSize.m_dx, szImageSize.m_dy, 24 ) ;

					ROffscreenDrawingSurface dsMem ; 
					dsMem.SetImage( &image ) ;

					R2dTransform transform ;
					transform.PreScale( 
						szImageSize.m_dx / pictureSize.m_dx,
						szImageSize.m_dy / pictureSize.m_dy ) ;

					// Render the component
					dsMem.SetFillColor( RSolidColor( kWhite ) ) ;
					dsMem.FillRectangle( RRealRect( pictureSize ), transform ) ;
					pComponentView->Render( dsMem, transform, RIntRect( 
						RIntSize( pictureSize.m_dx, pictureSize.m_dy ) ) ) ;

					dsMem.ReleaseImage() ;
					
					rLibrary.ExportImage( image, strPathName, eFormat ) ;
				}

				if (rLibrary.GetLastException() != kNoError)
				{
					throw rLibrary.GetLastException() ;
				}
			
			} // else

		} // try

		catch( YException e)
		{
			ReportException( e ) ;
		}

	} // if (IDOK)
}
//////////////////////////////////////////////////////////////////////////////
//
//	void ExportGraphicImage( RBitmapImage* pImage, RMBCString *pInitialDir ) 
//
//	export a graphic to file from a bitmap image
//
//////////////////////////////////////////////////////////////////////////////
void ExportGraphicImage( RBitmapImage* pImage, RMBCString *pInitialDir /*=NULL*/) 
{
	RImageLibrary rLibrary ;
	CArray<int, int> arrFileFormats ;

	//
	// Build the export file filter list...
	//
	CString strFilter, str ;

	for (int i = 0; i < kNumFormats - 1; i++)
	{
		if (rLibrary.IsFormatSupported( (EImageFormat) kImageExportFilters[i][0] ))
		{
			str.LoadString( kImageExportFilters[i][1] ) ;
			strFilter += str ;

			arrFileFormats.Add( kImageExportFilters[i][0] ) ;
		}
	}

	TpsAssert( kImageExportFilters[i][0] == kImageFormatXRX, "Invalid export format!" ) ;
	str.LoadString( kImageExportFilters[i][1] ) ;  
	strFilter += str ;
	strFilter += "|" ;

	arrFileFormats.Add( kImageExportFilters[i][0] ) ;

	//
	// Create and execute the dialog...
	//
	RExportGraphicDlg dlg( pImage, strFilter ) ;

	// Load in the dialog title string
	CString strTitle ;
	strTitle.LoadString( STRING_EXPORT_IMAGE_DIALOG_TITLE ) ;
	dlg.m_ofn.lpstrTitle = (LPCTSTR) strTitle ;
	if( pInitialDir )
	{
		dlg.m_ofn.lpstrInitialDir = (LPCTSTR)*pInitialDir;
	}

	if (IDOK == dlg.DoModal())
	{
		RMBCString   strPathName = dlg.GetPathName() ;
		EImageFormat eFormat = (EImageFormat) arrFileFormats[dlg.m_ofn.nFilterIndex - 1] ;

		try
		{
			if (kImageFormatXRX == eFormat)		// exporting as WMF
			{
				// get our output image size
				RRealSize rRealOutputSize( dlg.GetSize() );
				RRealSize screenDPI = ::GetScreenDPI();

				// create a vector image
				RVectorImage vectorImage;
 				vectorImage.SetSuggestedWidthInInches( ::PixelsToInches( rRealOutputSize.m_dx, (uLONG) screenDPI.m_dx ) );
				vectorImage.SetSuggestedHeightInInches( ::PixelsToInches( rRealOutputSize.m_dy, (uLONG) screenDPI.m_dy ) );

				// Create an offscreen drawing surface and set the picture
				ROffscreenDrawingSurface rODS;
				rODS.SetImage( &vectorImage );

		  		// Scale to the device DPI
				RRealSize rRealImageSize = pImage->GetSizeInLogicalUnits();
				R2dTransform transform ;
				RRealSize deviceDPI = rODS.GetDPI();

				transform.PreScale( (YFloatType) deviceDPI.m_dx / screenDPI.m_dy, 
									(YFloatType) deviceDPI.m_dy / screenDPI.m_dy );

				transform.PreScale( rRealOutputSize.m_dx / rRealImageSize.m_dx,
									rRealOutputSize.m_dy / rRealImageSize.m_dy ) ;

				// calculate the output rect
				RRealRect outputRect( rRealOutputSize ) ;
				::ScreenUnitsToLogicalUnits( outputRect );
				outputRect *= transform;

				// render to the offscreen
				pImage->Render( rODS, outputRect ) ;

				// clean up
				rODS.ReleaseImage();

				// export
				rLibrary.ExportImage( vectorImage, strPathName, kImageFormatXRX ) ;
				if (rLibrary.GetLastException() != kNoError)
				{
					throw rLibrary.GetLastException() ;
				}
			}
			else					// all other formats
			{
				_nFilterIndex = dlg.m_ofn.nFilterIndex ;
				
				EImageFormat eFormat = (EImageFormat) arrFileFormats[dlg.m_ofn.nFilterIndex - 1] ;
				rLibrary.ExportImage( *pImage, strPathName, eFormat );
				if (rLibrary.GetLastException() != kNoError)
				{
					throw rLibrary.GetLastException() ;
				}
	
			}
		}
		catch( YException e)
		{
			ReportException( e ) ;
		}
	}
}
Example #5
0
RBitmapImage&	RWinColorPalette::GetPaletteBitmapImage( )
{
	static BOOLEAN			m_fPaletteInitialized = FALSE;
	static RBitmapImage	m_biPalette;

	if ( !m_fPaletteInitialized )
	{
		// find the resource in the resource file
		HRSRC hRsrc = FindResource( AfxGetResourceHandle(), 
			MAKEINTRESOURCE( m_uPaletteBitmapID ), RT_BITMAP );

		if ( hRsrc != NULL )
		{
			// get a handle to the resource data
			HGLOBAL hTemp = LoadResource( AfxGetResourceHandle(), hRsrc );

			if ( hTemp != NULL )
			{
				// Initlize the palette bitmap with the resource data
				m_biPalette.Initialize( LockResource( hTemp ) );

				// unlock and free the resource
				UnlockResource( hTemp );
				FreeResource( hTemp );
			}
			else
				AfxThrowResourceException( );
		}
		else
			AfxThrowResourceException( );

		m_fPaletteInitialized = TRUE;

		COLORMAP crColorMap[] =
		{
			{ RGB( 255, 255, 255 ), GetSysColor( COLOR_BTNHIGHLIGHT ) },
			{ RGB( 192, 192, 192 ), GetSysColor( COLOR_BTNFACE )      },
			{ RGB( 128, 128, 128 ), GetSysColor( COLOR_BTNSHADOW )    }
		};

		RIntPoint ptCells[] = 
		{
			FindColor( crColorMap[0].from ),
			FindColor( crColorMap[1].from ),
			FindColor( crColorMap[2].from )
		};

		void*     pRawData   = m_biPalette.GetRawData();
		RGBQUAD*  pColorData = (RGBQUAD *) RBitmapImage::GetColorData( pRawData );
		LPBYTE    pImageData = (LPBYTE) RBitmapImage::GetImageData( pRawData );

		for (int j = 0; (LPVOID) pColorData < (LPVOID) pImageData; j++, pColorData++)
		{
			for (int i = 0; i < NumElements( crColorMap ); i++)
			{
				if (crColorMap[i].from == RGB( pColorData->rgbRed, pColorData->rgbGreen, pColorData->rgbBlue ))
				{
					pColorData->rgbBlue  = GetBValue( crColorMap[i].to );
					pColorData->rgbRed   = GetRValue( crColorMap[i].to );
					pColorData->rgbGreen = GetGValue( crColorMap[i].to );
					pColorData->rgbReserved = 0;
				}
			}

			if (j == 9)
			{
				// We only need to look at the system colors, so
				// jump to the last 10 entries in the palette.
				pColorData += 235;
			}
		}

		m_biPalette.UpdatePalette();

		//
		// Relace the cells that got remapped
		//
		ROffscreenDrawingSurface dsMem;
		dsMem.SetImage( &m_biPalette );

		RSolidColor rSolid;

		for (int i = 0; i < NumElements( ptCells ); i++)
		{
			if (ptCells[i].m_x >= 0 && ptCells[i].m_y >= 0)
			{
				RIntRect rcCell( RIntSize( kCellSize.m_dx - 2, kCellSize.m_dy - 2 ) );
				rcCell.Offset( RIntSize( ptCells[i].m_x + 1, ptCells[i].m_y + 1 ) );

				rSolid = crColorMap[i].from;

				RColor rColor( rSolid );
				dsMem.SetFillColor( rColor );
				dsMem.FillRectangle( rcCell );
			}
		}

		dsMem.ReleaseImage();
	}

	return m_biPalette;
}