StandardizedDiffuseAlbedoMaps.CalibratedTexture.SavePack C# (CSharp) Method

SavePack() public method

Saves the texture pack (texture + swatches + xml manifest)
public SavePack ( System _FileName, TARGET_FORMAT _TargetFormat ) : void
_FileName System
_TargetFormat TARGET_FORMAT
return void
        public void SavePack( System.IO.FileInfo _FileName, TARGET_FORMAT _TargetFormat )
        {
            if ( m_Texture == null )
                throw new Exception( "No calibrated texture has been built! Can't save." );

            System.IO.DirectoryInfo	Dir = _FileName.Directory;
            string	RawFileName = System.IO.Path.GetFileNameWithoutExtension( _FileName.FullName );
            string	Extension = System.IO.Path.GetExtension( _FileName.FullName );

            System.IO.FileInfo		FileName_Manifest = new System.IO.FileInfo( System.IO.Path.Combine( Dir.FullName, RawFileName + ".xml" ) );
            System.IO.FileInfo		FileName_SwatchMin = new System.IO.FileInfo( System.IO.Path.Combine( Dir.FullName, RawFileName + "_Min" + Extension ) );
            System.IO.FileInfo		FileName_SwatchMax = new System.IO.FileInfo( System.IO.Path.Combine( Dir.FullName, RawFileName + "_Max" + Extension ) );
            System.IO.FileInfo		FileName_SwatchAvg = new System.IO.FileInfo( System.IO.Path.Combine( Dir.FullName, RawFileName + "_Avg" + Extension ) );
            System.IO.FileInfo[]	FileName_CustomSwatches = new System.IO.FileInfo[m_CustomSwatches.Length];
            for ( int CustomSwatchIndex=0; CustomSwatchIndex < m_CustomSwatches.Length; CustomSwatchIndex++ )
                FileName_CustomSwatches[CustomSwatchIndex] = new System.IO.FileInfo( System.IO.Path.Combine( Dir.FullName, RawFileName + "_Custom" + CustomSwatchIndex.ToString() + Extension ) );

            //////////////////////////////////////////////////////////////////////////
            // Build image type and format parameters as well as target color profile
            ImageUtility.Bitmap.FILE_TYPE		FileType = ImageUtility.Bitmap.FILE_TYPE.UNKNOWN;
            ImageUtility.Bitmap.FORMAT_FLAGS	Format = ImageUtility.Bitmap.FORMAT_FLAGS.NONE;
            switch ( _TargetFormat )
            {
                case TARGET_FORMAT.PNG8:
                    FileType = ImageUtility.Bitmap.FILE_TYPE.PNG;
                    Format = ImageUtility.Bitmap.FORMAT_FLAGS.SAVE_8BITS_UNORM;
                    break;

                case TARGET_FORMAT.PNG16:
                    FileType = ImageUtility.Bitmap.FILE_TYPE.PNG;
                    Format = ImageUtility.Bitmap.FORMAT_FLAGS.SAVE_16BITS_UNORM;
                    break;

                case TARGET_FORMAT.TIFF:
                    FileType = ImageUtility.Bitmap.FILE_TYPE.TIFF;
                    Format = ImageUtility.Bitmap.FORMAT_FLAGS.SAVE_16BITS_UNORM;
                    break;
            }
            if ( FileType == ImageUtility.Bitmap.FILE_TYPE.UNKNOWN )
                throw new Exception( "Unknown target file format!" );

            //////////////////////////////////////////////////////////////////////////
            // Save textures

            // Save main texture
            SaveImage( m_Texture, _FileName, FileType, Format );

            // Save default swatches
            SaveImage( m_SwatchMin.Texture, FileName_SwatchMin, FileType, Format );
            SaveImage( m_SwatchMax.Texture, FileName_SwatchMax, FileType, Format );
            SaveImage( m_SwatchAvg.Texture, FileName_SwatchAvg, FileType, Format );

            // Save custom swatches
            for ( int CustomSwatchIndex=0; CustomSwatchIndex < m_CustomSwatches.Length; CustomSwatchIndex++ )
                SaveImage( m_CustomSwatches[CustomSwatchIndex].Texture, FileName_CustomSwatches[CustomSwatchIndex], FileType, Format );

            //////////////////////////////////////////////////////////////////////////
            // Prepare the XML manifest
            XmlDocument	Doc = new XmlDocument();

            XmlComment	HeaderComment = Doc.CreateComment(
                "***Do not modify!***\r\n\r\n" +
                "This is a calibrated texture manifest file generated from the uncalibrated image \"" + m_CaptureParameters.SourceImageName + "\"\r\n" +
                "Resulting generated images have been stored using a standard sRGB profile and can be used directly as source or color-picked by artists\r\n" +
                " without any other processing. Colors in the textures will have the proper reflectance (assuming the original image has been properly captured\r\n" +
                " with specular removal using polarization filters) and after sRGB->Linear conversion will be directly useable as reflectance in the lighting equation.\r\n" +
                "The xyY values are given in device-independent xyY color space and can be used as linear-space colors directly.\r\n\r\n" +
                "***Do not modify!***" );
            Doc.AppendChild( HeaderComment );

            XmlElement	Root = Doc.CreateElement( "Manifest" );
            Doc.AppendChild( Root );

            // Save source image infos
            XmlElement	SourceInfosElement = AppendElement( Root, "SourceInfos" );
            SetAttribute( AppendElement( SourceInfosElement, "SourceImageName" ), "Value", m_CaptureParameters.SourceImageName );
            SetAttribute( AppendElement( SourceInfosElement, "ISOSpeed" ), "Value", m_CaptureParameters.ISOSpeed.ToString() );
            SetAttribute( AppendElement( SourceInfosElement, "ShutterSpeed" ), "Value", m_CaptureParameters.ShutterSpeed.ToString() );
            SetAttribute( AppendElement( SourceInfosElement, "Aperture" ), "Value", m_CaptureParameters.Aperture.ToString() );

            SetAttribute( AppendElement( SourceInfosElement, "SpatialCorrection" ), "Status", m_SpatialCorrectionEnabled ? "Enabled" : "Disabled" );
            SetAttribute( AppendElement( SourceInfosElement, "WhiteReflectanceCorrectionFactor" ), "Value", m_WhiteReflectanceCorrectionFactor.ToString() );
            if ( m_WhiteReflectanceReference.z > 0.0f )
                SetAttribute( AppendElement( SourceInfosElement, "WhiteBalance" ), "xyY", m_WhiteReflectanceReference.ToString() );

            SetAttribute( AppendElement( SourceInfosElement, "CropSource" ), "Value", m_CaptureParameters.CropSource.ToString() );
            SetAttribute( AppendElement( SourceInfosElement, "CropRectangleCenter" ), "X", m_CaptureParameters.CropRectangleCenter.x.ToString() ).SetAttribute( "Y", m_CaptureParameters.CropRectangleCenter.y.ToString() );
            SetAttribute( AppendElement( SourceInfosElement, "CropRectangleHalfSize" ), "X", m_CaptureParameters.CropRectangleHalfSize.x.ToString() ).SetAttribute( "Y", m_CaptureParameters.CropRectangleHalfSize.y.ToString() );
            SetAttribute( AppendElement( SourceInfosElement, "CropRectangleRotation" ), "Value", m_CaptureParameters.CropRectangleRotation.ToString() );

            SetAttribute( AppendElement( SourceInfosElement, "SwatchesSize" ), "Width", m_SwatchWidth.ToString() ).SetAttribute( "Height", m_SwatchHeight.ToString() );

            SetAttribute( AppendElement( SourceInfosElement, "TargetFormat" ), "Value", _TargetFormat.ToString() );

            // Save calibrated texture infos
            {
                XmlElement	CalibratedTextureElement = AppendElement( Root, "CalibratedTexture" );
                SetAttribute( CalibratedTextureElement, "Name", _FileName.Name ).SetAttribute( "Width", m_Texture.Width.ToString() ).SetAttribute( "Height", m_Texture.Height.ToString() );

                // Save default swatches
                XmlElement	DefaultSwatchesElement = AppendElement( CalibratedTextureElement, "DefaultSwatches" );
                XmlElement	MinSwatchElement = AppendElement( DefaultSwatchesElement, "Min" );
                SetAttribute( MinSwatchElement, "Name", FileName_SwatchMin.Name );
                m_SwatchMin.Save( this, MinSwatchElement );

                XmlElement	MaxSwatchElement = AppendElement( DefaultSwatchesElement, "Max" );
                SetAttribute( MaxSwatchElement, "Name", FileName_SwatchMax.Name );
                m_SwatchMax.Save( this, MaxSwatchElement );

                XmlElement	AvgSwatchElement = AppendElement( DefaultSwatchesElement, "Avg" );
                SetAttribute( AvgSwatchElement, "Name", FileName_SwatchAvg.Name );
                m_SwatchAvg.Save( this, AvgSwatchElement );
            }

            // Save custom swatches infos
            if ( m_CustomSwatches.Length > 0 )
            {
                XmlElement	CustomSwatchesElement = AppendElement( Root, "CustomSwatches" );
                SetAttribute( CustomSwatchesElement, "Count", m_CustomSwatches.Length.ToString() );
                for ( int CustomSwatchIndex=0; CustomSwatchIndex < m_CustomSwatches.Length; CustomSwatchIndex++ )
                {
                    XmlElement	CustomSwatchElement = AppendElement( CustomSwatchesElement, "Custom"+CustomSwatchIndex.ToString() );
                    SetAttribute( CustomSwatchElement, "Name", FileName_CustomSwatches[CustomSwatchIndex].Name );
                    m_CustomSwatches[CustomSwatchIndex].Save( this, CustomSwatchElement );
                }
            }

            Doc.Save( FileName_Manifest.FullName );
        }