C# Class ImageUtility.Bitmap

The Bitmap class should be used to replace the standard System.Drawing.Bitmap The big advantage of the Bitmap class is to accurately read back the color profile and gamma correction data stored in the image's metadata so that, internally, the image is stored: • As device-independent CIE XYZ (http://en.wikipedia.org/wiki/CIE_1931_color_space) format, our Profile Connection Space • In linear space (i.e. no gamma curve is applied) • NOT pre-multiplied alpha (you can later re-pre-multiply if needed) This helps to ensure that whatever the source image format stored on disk, you always deal with a uniformized image internally. Later, you can cast from the CIE XYZ device-independent format into any number of pre-defined texture profiles: • sRGB or Linear space textures (for 8bits per component images only) • Compressed (BC1-BC5) or uncompressed (for 8bits per component images only) • 8-, 16-, 16F- 32- or 32F-bits per component • Pre-multiplied alpha or not The following image formats are currently supported: • JPG • PNG • TIFF • TGA • BMP • GIF • HDR • Any RAW camera format supported by the LibRaw library
The Bitmap class has been tested with various formats, various bit depths and color profiles all created from Adobe Photoshop CS4 using the "Save As" dialog and the "Save for Web & Devices" dialog box. In a general manner, you should NOT use the latter save option but rather select your working color profile from the "Edit > Color Settings" menu, then save your files and make sure you tick the "ICC Profile" checkbox using the DEFAULT save file dialog box to embed that profile in the image.
Inheritance: IDisposable
显示文件 Open project: Patapom/GodComplex Class Usage Examples

Protected Properties

Property Type Description
m_Aperture float
m_Bitmap ].float4[
m_ColorProfile ColorProfile
m_FocalLength float
m_Height int
m_ISOSpeed float
m_ShutterSpeed float
m_Type FILE_TYPE
m_Width int
m_bHasAlpha bool
m_bHasValidShotInfo bool
ms_ConvertContent2XYZ bool
ms_ReadContent bool

Public Methods

Method Description
BilinearSample ( float X, float Y ) : float4

Performs bilinear sampling of the XYZ content

Bitmap ( System _ImageFileName ) : System

Creates a bitmap from a file

Bitmap ( System _ImageFileName, ColorProfile _ProfileOverride ) : System

Creates a bitmap from a file

Bitmap ( System _ImageStream, FILE_TYPE _FileType ) : System

Creates a bitmap from a stream

Bitmap ( System _ImageStream, FILE_TYPE _FileType, ColorProfile _ProfileOverride ) : System

Creates a bitmap from a stream

Bitmap ( System _ImageStream, System _ImageFileNameName ) : System

Creates a bitmap from a stream

Bitmap ( System _ImageStream, System _ImageFileNameName, ColorProfile _ProfileOverride ) : System

Creates a bitmap from a stream

Bitmap ( byte _ImageFileContent, FILE_TYPE _FileType ) : System

Creates a bitmap from memory

Bitmap ( byte _ImageFileContent, FILE_TYPE _FileType, ColorProfile _ProfileOverride ) : System

Creates a bitmap from memory

Bitmap ( byte _ImageFileContent, System _ImageFileNameName ) : System

Creates a bitmap from memory

Bitmap ( byte _ImageFileContent, System _ImageFileNameName, ColorProfile _ProfileOverride ) : System

Creates a bitmap from memory

Bitmap ( int _Width, int _Height, ColorProfile _Profile ) : System

Manual creation

DecodeRGBEImage ( PF_RGBE _Source, bool _bSourceIsXYZ, bool _bTargetNeedsXYZ, ColorProfile _ColorProfile ) : ].float4[

Decodes a RGBE formatted image into a plain floating-point image

DecodeRGBEImage ( PF_RGBE _Source, bool _bSourceIsXYZ, float4 _Target, bool _bTargetNeedsXYZ, ColorProfile _ColorProfile ) : void

Decodes a RGBE formatted image into a plain floating-point image

Dispose ( ) : void
GetFileType ( System _ImageFileNameName ) : FILE_TYPE

Retrieves the image file type based on the image file name

Load ( System _ImageFileName, FILE_TYPE _FileType ) : void

Loads from disk

Load ( System _ImageFileName, FILE_TYPE _FileType, ColorProfile _ProfileOverride ) : void
Load ( byte _ImageFileContent, FILE_TYPE _FileType ) : void

Actual load from a byte[] in memory

Load ( byte _ImageFileContent, FILE_TYPE _FileType, ColorProfile _ProfileOverride ) : void
LoadAndDecodeHDRFormat ( byte _HDRFormatBinary, bool _bTargetNeedsXYZ, ColorProfile _ProfileOverride, ColorProfile &_ColorProfile ) : ].float4[

Loads a bitmap in .HDR format into a float4 array directly useable by the image constructor

LoadBitmap ( System _Bitmap, int &_Width, int &_Height ) : byte[]

Loads a System.Drawing.Bitmap into a byte[] containing RGBARGBARG... pixels

LoadHDRFormat ( byte _HDRFormatBinary, ColorProfile _ProfileOverride, bool &_bIsXYZ, ColorProfile &_ColorProfile ) : ].PF_RGBE[

Loads a bitmap in .HDR format into a RGBE array

Save ( System _FileName ) : void

Save to a file

Save ( System _Stream, FILE_TYPE _FileType, FORMAT_FLAGS _Parms, FormatEncoderOptions _Options ) : void

Save to a stream

Save ( System _FileName, FORMAT_FLAGS _Parms ) : void
Save ( System _FileName, FORMAT_FLAGS _Parms, FormatEncoderOptions _Options ) : void

Protected Methods

Method Description
ConvertFrame ( System _Format ) : System.Windows.Media.Imaging.BitmapFrame

Converts the generic XYZ+A bitmap to the specified format frame

ConvertFrame ( System.Windows.Media.Imaging.BitmapSource _Frame ) : void

Converts the source bitmap to a generic RGBA32F format

I cannot use the FormatConvertedBitmap class because it applies some unwanted gamma correction depending on the source pixel format. For example, if the image is using the Bgr24 format that uses a 1/2.2 gamma internally, converting that to our generic format Rgba128Float (that uses a gamma of 1 internally) will automatically apply a pow( 2.2 ) to the RGB values, which is NOT what we're looking for since we're handling gamma correction ourselves here !

FLOAT_TO_BYTE ( float v ) : byte
FLOAT_TO_WORD ( float v ) : UInt16
RadianceFileFindInHeader ( List _HeaderLines, string _Search ) : string

Method Details

BilinearSample() public method

Performs bilinear sampling of the XYZ content
public BilinearSample ( float X, float Y ) : float4
X float A column index in [0,Width[ (will be clamped if out of range)
Y float A row index in [0,Height[ (will be clamped if out of range)
return float4

Bitmap() public method

Creates a bitmap from a file
public Bitmap ( System _ImageFileName ) : System
_ImageFileName System
return System

Bitmap() public method

Creates a bitmap from a file
public Bitmap ( System _ImageFileName, ColorProfile _ProfileOverride ) : System
_ImageFileName System
_ProfileOverride ColorProfile
return System

Bitmap() public method

Creates a bitmap from a stream
public Bitmap ( System _ImageStream, FILE_TYPE _FileType ) : System
_ImageStream System The image stream to load the bitmap from
_FileType FILE_TYPE The image type
return System

Bitmap() public method

Creates a bitmap from a stream
public Bitmap ( System _ImageStream, FILE_TYPE _FileType, ColorProfile _ProfileOverride ) : System
_ImageStream System The image stream to load the bitmap from
_FileType FILE_TYPE The image type
_ProfileOverride ColorProfile
return System

Bitmap() public method

Creates a bitmap from a stream
public Bitmap ( System _ImageStream, System _ImageFileNameName ) : System
_ImageStream System The image stream to load the bitmap from
_ImageFileNameName System The name of the image file the stream is coming from originally (used to identify image file type)
return System

Bitmap() public method

Creates a bitmap from a stream
public Bitmap ( System _ImageStream, System _ImageFileNameName, ColorProfile _ProfileOverride ) : System
_ImageStream System The image stream to load the bitmap from
_ImageFileNameName System The name of the image file the stream is coming from originally (used to identify image file type)
_ProfileOverride ColorProfile
return System

Bitmap() public method

Creates a bitmap from memory
public Bitmap ( byte _ImageFileContent, FILE_TYPE _FileType ) : System
_ImageFileContent byte The memory buffer to load the bitmap from
_FileType FILE_TYPE The image type
return System

Bitmap() public method

Creates a bitmap from memory
public Bitmap ( byte _ImageFileContent, FILE_TYPE _FileType, ColorProfile _ProfileOverride ) : System
_ImageFileContent byte The memory buffer to load the bitmap from
_FileType FILE_TYPE The image type
_ProfileOverride ColorProfile
return System

Bitmap() public method

Creates a bitmap from memory
public Bitmap ( byte _ImageFileContent, System _ImageFileNameName ) : System
_ImageFileContent byte The memory buffer to load the bitmap from
_ImageFileNameName System The name of the image file the stream is coming from originally (used to identify image file type)
return System

Bitmap() public method

Creates a bitmap from memory
public Bitmap ( byte _ImageFileContent, System _ImageFileNameName, ColorProfile _ProfileOverride ) : System
_ImageFileContent byte The memory buffer to load the bitmap from
_ImageFileNameName System The name of the image file the stream is coming from originally (used to identify image file type)
_ProfileOverride ColorProfile
return System

Bitmap() public method

Manual creation
public Bitmap ( int _Width, int _Height, ColorProfile _Profile ) : System
_Width int
_Height int
_Profile ColorProfile An optional color profile, you will need a valid profile if you wish to save the bitmap!
return System

ConvertFrame() protected method

Converts the generic XYZ+A bitmap to the specified format frame
protected ConvertFrame ( System _Format ) : System.Windows.Media.Imaging.BitmapFrame
_Format System The format to convert into
return System.Windows.Media.Imaging.BitmapFrame

ConvertFrame() protected method

Converts the source bitmap to a generic RGBA32F format
I cannot use the FormatConvertedBitmap class because it applies some unwanted gamma correction depending on the source pixel format. For example, if the image is using the Bgr24 format that uses a 1/2.2 gamma internally, converting that to our generic format Rgba128Float (that uses a gamma of 1 internally) will automatically apply a pow( 2.2 ) to the RGB values, which is NOT what we're looking for since we're handling gamma correction ourselves here !
protected ConvertFrame ( System.Windows.Media.Imaging.BitmapSource _Frame ) : void
_Frame System.Windows.Media.Imaging.BitmapSource The source frame to convert
return void

DecodeRGBEImage() public static method

Decodes a RGBE formatted image into a plain floating-point image
public static DecodeRGBEImage ( PF_RGBE _Source, bool _bSourceIsXYZ, bool _bTargetNeedsXYZ, ColorProfile _ColorProfile ) : ].float4[
_Source PF_RGBE The source RGBE formatted image
_bSourceIsXYZ bool Tells if the source image is encoded as XYZE rather than RGBE
_bTargetNeedsXYZ bool Tells if the target needs to be in CIE XYZ space (true) or RGB (false)
_ColorProfile ColorProfile The color profile for the image
return ].float4[

DecodeRGBEImage() public static method

Decodes a RGBE formatted image into a plain floating-point image
public static DecodeRGBEImage ( PF_RGBE _Source, bool _bSourceIsXYZ, float4 _Target, bool _bTargetNeedsXYZ, ColorProfile _ColorProfile ) : void
_Source PF_RGBE The source RGBE formatted image
_bSourceIsXYZ bool Tells if the source image is encoded as XYZE rather than RGBE
_Target float4 The target float4 image
_bTargetNeedsXYZ bool Tells if the target needs to be in CIE XYZ space (true) or RGB (false)
_ColorProfile ColorProfile The color profile for the image
return void

Dispose() public method

public Dispose ( ) : void
return void

FLOAT_TO_BYTE() protected method

protected FLOAT_TO_BYTE ( float v ) : byte
v float
return byte

FLOAT_TO_WORD() protected method

protected FLOAT_TO_WORD ( float v ) : UInt16
v float
return System.UInt16

GetFileType() public static method

Retrieves the image file type based on the image file name
public static GetFileType ( System _ImageFileNameName ) : FILE_TYPE
_ImageFileNameName System The image file name
return FILE_TYPE

Load() public method

Loads from disk
public Load ( System _ImageFileName, FILE_TYPE _FileType ) : void
_ImageFileName System
_FileType FILE_TYPE
return void

Load() public method

public Load ( System _ImageFileName, FILE_TYPE _FileType, ColorProfile _ProfileOverride ) : void
_ImageFileName System
_FileType FILE_TYPE
_ProfileOverride ColorProfile
return void

Load() public method

Actual load from a byte[] in memory
Occurs if the image type is not supported by the Bitmap class Occurs if the source image format cannot be converted to RGBA32F which is the generic format we read from
public Load ( byte _ImageFileContent, FILE_TYPE _FileType ) : void
_ImageFileContent byte The source image content as a byte[]
_FileType FILE_TYPE The type of file to load
return void

Load() public method

public Load ( byte _ImageFileContent, FILE_TYPE _FileType, ColorProfile _ProfileOverride ) : void
_ImageFileContent byte
_FileType FILE_TYPE
_ProfileOverride ColorProfile
return void

LoadAndDecodeHDRFormat() public static method

Loads a bitmap in .HDR format into a float4 array directly useable by the image constructor
public static LoadAndDecodeHDRFormat ( byte _HDRFormatBinary, bool _bTargetNeedsXYZ, ColorProfile _ProfileOverride, ColorProfile &_ColorProfile ) : ].float4[
_HDRFormatBinary byte
_bTargetNeedsXYZ bool Tells if the target needs to be in CIE XYZ space (true) or RGB (false)
_ProfileOverride ColorProfile
_ColorProfile ColorProfile The color profile for the image
return ].float4[

LoadBitmap() public static method

Loads a System.Drawing.Bitmap into a byte[] containing RGBARGBARG... pixels
public static LoadBitmap ( System _Bitmap, int &_Width, int &_Height ) : byte[]
_Bitmap System The source System.Drawing.Bitmap to load
_Width int The bitmap's width
_Height int The bitmaps's height
return byte[]

LoadHDRFormat() public static method

Loads a bitmap in .HDR format into a RGBE array
public static LoadHDRFormat ( byte _HDRFormatBinary, ColorProfile _ProfileOverride, bool &_bIsXYZ, ColorProfile &_ColorProfile ) : ].PF_RGBE[
_HDRFormatBinary byte
_ProfileOverride ColorProfile
_bIsXYZ bool Tells if the image is encoded as XYZE rather than RGBE
_ColorProfile ColorProfile The color profile for the image
return ].PF_RGBE[

RadianceFileFindInHeader() protected static method

protected static RadianceFileFindInHeader ( List _HeaderLines, string _Search ) : string
_HeaderLines List
_Search string
return string

Save() public method

Save to a file
public Save ( System _FileName ) : void
_FileName System
return void

Save() public method

Save to a stream
Occurs if the image type is not supported by the Bitmap class Occurs if the source image format cannot be converted to RGBA32F which is the generic format we read from
public Save ( System _Stream, FILE_TYPE _FileType, FORMAT_FLAGS _Parms, FormatEncoderOptions _Options ) : void
_Stream System The stream to write the image to
_FileType FILE_TYPE The file type to save as
_Parms FORMAT_FLAGS Additional formatting flags
_Options FormatEncoderOptions An optional block of options for encoding
return void

Save() public method

public Save ( System _FileName, FORMAT_FLAGS _Parms ) : void
_FileName System
_Parms FORMAT_FLAGS
return void

Save() public method

public Save ( System _FileName, FORMAT_FLAGS _Parms, FormatEncoderOptions _Options ) : void
_FileName System
_Parms FORMAT_FLAGS
_Options FormatEncoderOptions
return void

Property Details

m_Aperture protected_oe property

protected float m_Aperture
return float

m_Bitmap protected_oe property

protected float4[,] m_Bitmap
return ].float4[

m_ColorProfile protected_oe property

protected ColorProfile,ImageUtility m_ColorProfile
return ColorProfile

m_FocalLength protected_oe property

protected float m_FocalLength
return float

m_Height protected_oe property

protected int m_Height
return int

m_ISOSpeed protected_oe property

protected float m_ISOSpeed
return float

m_ShutterSpeed protected_oe property

protected float m_ShutterSpeed
return float

m_Type protected_oe property

protected FILE_TYPE m_Type
return FILE_TYPE

m_Width protected_oe property

protected int m_Width
return int

m_bHasAlpha protected_oe property

protected bool m_bHasAlpha
return bool

m_bHasValidShotInfo protected_oe property

protected bool m_bHasValidShotInfo
return bool

ms_ConvertContent2XYZ protected_oe static_oe property

protected static bool ms_ConvertContent2XYZ
return bool

ms_ReadContent protected_oe static_oe property

protected static bool ms_ReadContent
return bool