RGB2XYZ() public method

Converts a RGB color to a CIEXYZ color
public RGB2XYZ ( float4 _RGB ) : float4
_RGB float4
return float4
Ejemplo n.º 1
0
		/// <summary>
		/// Creates a bitmap from a System.Drawing.Bitmap and a color profile
		/// </summary>
		/// <param name="_Device"></param>
		/// <param name="_Name"></param>
		/// <param name="_Bitmap">The System.Drawing.Bitmap</param>
		/// <param name="_ColorProfile">The color profile to use to transform the bitmap</param>
		public	Bitmap( System.Drawing.Bitmap _Bitmap, ColorProfile _ColorProfile )
		{
			if ( _ColorProfile == null )
				throw new Exception( "Invalid profile: can't convert to CIE XYZ!" );
			m_ColorProfile = _ColorProfile;

			// Load the bitmap's content and copy it to a double entry array
			byte[]	BitmapContent = LoadBitmap( _Bitmap, out m_Width, out m_Height );
			if ( BitmapContent == null )
				return;

			m_Bitmap = new float4[m_Width,m_Height];

			int	i=0;
			for ( int Y=0; Y < m_Height; Y++ )
				for ( int X=0; X < m_Width; X++ )
				{
					m_Bitmap[X,Y] = new float4(
							BYTE_TO_FLOAT * BitmapContent[i++],	// R
							BYTE_TO_FLOAT * BitmapContent[i++],	// G
							BYTE_TO_FLOAT * BitmapContent[i++],	// B
							BYTE_TO_FLOAT * BitmapContent[i++]	// A
						);
				}

			if ( ms_ConvertContent2XYZ ) {
				// Convert to CIE XYZ
				m_ColorProfile.RGB2XYZ( m_Bitmap, m_Bitmap );
			}
		}
Ejemplo n.º 2
0
		public void	Load( byte[] _ImageFileContent, FILE_TYPE _FileType, ColorProfile _ProfileOverride )
		{
			m_Type = _FileType;
			try
			{
				switch ( _FileType )
				{
					case FILE_TYPE.JPEG:
					case FILE_TYPE.PNG:
					case FILE_TYPE.TIFF:
					case FILE_TYPE.GIF:
					case FILE_TYPE.BMP:
						using ( System.IO.MemoryStream Stream = new System.IO.MemoryStream( _ImageFileContent ) )
						{
							// ===== 1] Load the bitmap source =====
							BitmapDecoder	Decoder = BitmapDecoder.Create( Stream, BitmapCreateOptions.IgnoreColorProfile | BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnDemand );
							if ( Decoder.Frames.Count == 0 )
								throw new Exception( "BitmapDecoder failed to read at least one bitmap frame!" );

							BitmapFrame	Frame = Decoder.Frames[0];
							if ( Frame == null )
								throw new Exception( "Invalid decoded bitmap!" );

// DEBUG
// int		StrideX = (Frame.Format.BitsPerPixel>>3)*Frame.PixelWidth;
// byte[]	DebugImageSource = new byte[StrideX*Frame.PixelHeight];
// Frame.CopyPixels( DebugImageSource, StrideX, 0 );
// DEBUG

// pas de gamma sur les JPEG si non spécifié !
// Il y a bien une magouille faite lors de la conversion par le FormatConvertedBitmap!


							// ===== 2] Build the color profile =====
							m_ColorProfile = _ProfileOverride != null ? _ProfileOverride : new ColorProfile( Frame.Metadata as BitmapMetadata, _FileType );

							// ===== 3] Convert the frame to generic RGBA32F =====
							ConvertFrame( Frame );

							// ===== 4] Convert to CIE XYZ (our device-independent profile connection space) =====
							if ( ms_ReadContent && ms_ConvertContent2XYZ )
								m_ColorProfile.RGB2XYZ( m_Bitmap, m_Bitmap );
						}
						break;

					case FILE_TYPE.TGA:
						{
							// Load as a System.Drawing.Bitmap and convert to float4
							using ( System.IO.MemoryStream Stream = new System.IO.MemoryStream( _ImageFileContent ) )
								using ( TargaImage TGA = new TargaImage( Stream, !ms_ReadContent ) ) {
									// Create a default sRGB linear color profile
									m_ColorProfile = _ProfileOverride != null ? _ProfileOverride
										: new ColorProfile(
											ColorProfile.Chromaticities.sRGB,	// Use default sRGB color profile
											ColorProfile.GAMMA_CURVE.STANDARD,	// But with a standard gamma curve...
											TGA.ExtensionArea.GammaRatio		// ...whose gamma is retrieved from extension data
										);

									if ( ms_ReadContent ) {
										// Convert
										byte[]	ImageContent = LoadBitmap( TGA.Image, out m_Width, out m_Height );
										m_Bitmap = new float4[m_Width,m_Height];
										byte	A;
										int		i = 0;
										for ( int Y=0; Y < m_Height; Y++ )
											for ( int X=0; X < m_Width; X++ )
											{
												m_Bitmap[X,Y].x = BYTE_TO_FLOAT * ImageContent[i++];
												m_Bitmap[X,Y].y = BYTE_TO_FLOAT * ImageContent[i++];
												m_Bitmap[X,Y].z = BYTE_TO_FLOAT * ImageContent[i++];

												A = ImageContent[i++];
												m_bHasAlpha |= A != 0xFF;

												m_Bitmap[X,Y].w = BYTE_TO_FLOAT * A;
											}

										if ( ms_ConvertContent2XYZ ) {
											// Convert to CIEXYZ
											m_ColorProfile.RGB2XYZ( m_Bitmap, m_Bitmap );
										}
									} else {
										// Only read dimensions
										m_Width = TGA.Header.Width;
										m_Height = TGA.Header.Height;
									}
								}
							return;
						}

					case FILE_TYPE.HDR:
						{
							// Load as XYZ
							m_Bitmap = LoadAndDecodeHDRFormat( _ImageFileContent, true, _ProfileOverride, out m_ColorProfile );
							m_Width = m_Bitmap.GetLength( 0 );
							m_Height = m_Bitmap.GetLength( 1 );
							return;
						}

				#if USE_LIB_RAW
					case FILE_TYPE.CRW:
					case FILE_TYPE.CR2:
					case FILE_TYPE.DNG:
						{
							using ( System.IO.MemoryStream Stream = new System.IO.MemoryStream( _ImageFileContent ) )
								using ( LibRawManaged.RawFile Raw = new LibRawManaged.RawFile() ) {
									Raw.UnpackRAW( Stream );

									ColorProfile.Chromaticities	Chroma = Raw.ColorProfile == LibRawManaged.RawFile.COLOR_PROFILE.ADOBE_RGB
																		? ColorProfile.Chromaticities.AdobeRGB_D65	// Use Adobe RGB
																		: ColorProfile.Chromaticities.sRGB;			// Use default sRGB color profile

									// Create a default sRGB linear color profile
									m_ColorProfile = _ProfileOverride != null ? _ProfileOverride
										: new ColorProfile(
											Chroma,
											ColorProfile.GAMMA_CURVE.STANDARD,	// But with a standard gamma curve...
											1.0f								// Linear
										);

									// Also get back valid camera shot info
									m_bHasValidShotInfo = true;
									m_ISOSpeed = Raw.ISOSpeed;
									m_ShutterSpeed = Raw.ShutterSpeed;
									m_Aperture = Raw.Aperture;
									m_FocalLength = Raw.FocalLength;

 									// Convert
									m_Width = Raw.Width;
									m_Height = Raw.Height;
//									float	ColorNormalizer = 1.0f / Raw.Maximum;
									float	ColorNormalizer = 1.0f / 65535.0f;

									if ( ms_ReadContent ) {
										m_Bitmap = new float4[m_Width,m_Height];
										UInt16[,][]	ImageContent = Raw.Image;
										for ( int Y=0; Y < m_Height; Y++ )
											for ( int X=0; X < m_Width; X++ )
											{
 												m_Bitmap[X,Y].x = ImageContent[X,Y][0] * ColorNormalizer;
 												m_Bitmap[X,Y].y = ImageContent[X,Y][1] * ColorNormalizer;
 												m_Bitmap[X,Y].z = ImageContent[X,Y][2] * ColorNormalizer;
 												m_Bitmap[X,Y].w = ImageContent[X,Y][3] * ColorNormalizer;
 											}

										if ( ms_ConvertContent2XYZ ) {
											// Convert to CIEXYZ
											m_ColorProfile.RGB2XYZ( m_Bitmap, m_Bitmap );
										}
									}
								}

#region My poor attempt at reading CRW files
// 							using ( System.IO.MemoryStream Stream = new System.IO.MemoryStream( _ImageFileContent ) )
// 								using ( CanonRawLoader CRWLoader = new CanonRawLoader( Stream ) )
// 								{
// 									ColorProfile.Chromaticities	Chroma = CRWLoader.m_ColorProfile == CanonRawLoader.DataColorProfile.COLOR_PROFILE.ADOBE_RGB
// 																		? ColorProfile.Chromaticities.AdobeRGB_D65	// Use Adobe RGB
// 																		: ColorProfile.Chromaticities.sRGB;			// Use default sRGB color profile
// 
// 									// Create a default sRGB linear color profile
// 									m_ColorProfile = new ColorProfile(
// 											Chroma,
// 											ColorProfile.GAMMA_CURVE.STANDARD,	// But with a standard gamma curve...
// 											1.0f								// Linear
// 										);
// 
//  									// Convert
// 									m_Width = CRWLoader.m_RAWImage.m_Width;
// 									m_Height = CRWLoader.m_RAWImage.m_Height;
// 
// 									m_Bitmap = new float4[m_Width,m_Height];
// 									UInt16[]	ImageContent = CRWLoader.m_RAWImage.m_DecodedImage;
// 									int			i = 0;
// // 									for ( int Y=0; Y < m_Height; Y++ )
// // 										for ( int X=0; X < m_Width; X++ )
// // 										{
// //  											m_Bitmap[X,Y].x = ImageContent[i++] / 4096.0f;
// //  											m_Bitmap[X,Y].y = ImageContent[i++] / 4096.0f;
// //  											m_Bitmap[X,Y].z = ImageContent[i++] / 4096.0f;
// // 											i++;
// //  										}
// 
// 									i=0;
// 									for ( int Y=0; Y < m_Height; Y++ )
// 										for ( int X=0; X < m_Width; X++ )
//  											m_Bitmap[X,Y].x = ImageContent[i++] / 4096.0f;
// 									i=0;
// 									for ( int Y=0; Y < m_Height; Y++ )
// 										for ( int X=0; X < m_Width; X++ )
//  											m_Bitmap[X,Y].y = ImageContent[i++] / 4096.0f;
// 									i=0;
// 									for ( int Y=0; Y < m_Height; Y++ )
// 										for ( int X=0; X < m_Width; X++ )
//  											m_Bitmap[X,Y].z = ImageContent[i++] / 4096.0f;
// 
// 									// Convert to CIEXYZ
// 									m_ColorProfile.RGB2XYZ( m_Bitmap );
// 								}
#endregion
							return;
 						}
					#endif

					default:
						throw new NotSupportedException( "The image file type \"" + _FileType + "\" is not supported by the Bitmap class!" );
				}
			}
			catch ( Exception )
			{
				throw;	// Go on !
			}
		}
Ejemplo n.º 3
0
		/// <summary>
		/// Decodes a RGBE formatted image into a plain floating-point image
		/// </summary>
		/// <param name="_Source">The source RGBE formatted image</param>
		/// <param name="_bSourceIsXYZ">Tells if the source image is encoded as XYZE rather than RGBE</param>
		/// <param name="_Target">The target float4 image</param>
		/// <param name="_bTargetNeedsXYZ">Tells if the target needs to be in CIE XYZ space (true) or RGB (false)</param>
		/// <param name="_ColorProfile">The color profile for the image</param>
		public static void			DecodeRGBEImage( PF_RGBE[,] _Source, bool _bSourceIsXYZ, float4[,] _Target, bool _bTargetNeedsXYZ, ColorProfile _ColorProfile )
		{
			if ( _bSourceIsXYZ ^ _bTargetNeedsXYZ )
			{	// Requires conversion...
				if ( _bSourceIsXYZ )
				{	// Convert from XYZ to RGB
					for ( int Y=0; Y < _Source.GetLength( 1 ); Y++ )
						for ( int X=0; X < _Source.GetLength( 0 ); X++ )
							_Target[X,Y] = _ColorProfile.XYZ2RGB( new float4( _Source[X,Y].DecodedColor.x, _Source[X,Y].DecodedColor.y, _Source[X,Y].DecodedColor.z, 1.0f ) );
				}
				else
				{	// Convert from RGB to XYZ
					for ( int Y=0; Y < _Source.GetLength( 1 ); Y++ )
						for ( int X=0; X < _Source.GetLength( 0 ); X++ )
							_Target[X,Y] = _ColorProfile.RGB2XYZ( new float4( _Source[X,Y].DecodedColor.x, _Source[X,Y].DecodedColor.y, _Source[X,Y].DecodedColor.z, 1.0f ) );
				}
				return;
			}

			// Simply decode vector and leave as-is
			for ( int Y=0; Y < _Source.GetLength( 1 ); Y++ )
				for ( int X=0; X < _Source.GetLength( 0 ); X++ )
					_Target[X,Y] = new float4( _Source[X,Y].DecodedColor.x, _Source[X,Y].DecodedColor.y, _Source[X,Y].DecodedColor.z, 1.0f );
		}