From 163a3484657d2ff657ad4d074363341cdb6ce3fb Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Sat, 10 Apr 2021 16:56:26 -0400 Subject: [PATCH] Image Editor: Add HDR setting for 2D cubemap viewer --- .../ImageEditor/CubeMapFaceViewer.Designer.cs | 45 +++++++++++++++++++ .../Editors/ImageEditor/CubeMapFaceViewer.cs | 22 ++++++++- .../Imaging/BitmapExtension.cs | 43 ++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) diff --git a/Switch_Toolbox_Library/Forms/Editors/ImageEditor/CubeMapFaceViewer.Designer.cs b/Switch_Toolbox_Library/Forms/Editors/ImageEditor/CubeMapFaceViewer.Designer.cs index f0cec930..c5593df5 100644 --- a/Switch_Toolbox_Library/Forms/Editors/ImageEditor/CubeMapFaceViewer.Designer.cs +++ b/Switch_Toolbox_Library/Forms/Editors/ImageEditor/CubeMapFaceViewer.Designer.cs @@ -39,6 +39,8 @@ this.btnLeftArray = new Toolbox.Library.Forms.STButton(); this.pbRightFace = new Toolbox.Library.Forms.PictureBoxCustom(); this.chkDisplayAlpha = new Toolbox.Library.Forms.STCheckBox(); + this.displayEncodedHDRAlphaChk = new Toolbox.Library.Forms.STCheckBox(); + this.gammaUD = new Toolbox.Library.Forms.NumericUpDownFloat(); this.contentContainer.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pbTopFace)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pbFrontFace)).BeginInit(); @@ -46,11 +48,14 @@ ((System.ComponentModel.ISupportInitialize)(this.pbBottomFace)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pbBackFace)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pbRightFace)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.gammaUD)).BeginInit(); this.SuspendLayout(); // // contentContainer // this.contentContainer.BackColor = System.Drawing.Color.White; + this.contentContainer.Controls.Add(this.gammaUD); + this.contentContainer.Controls.Add(this.displayEncodedHDRAlphaChk); this.contentContainer.Controls.Add(this.chkDisplayAlpha); this.contentContainer.Controls.Add(this.arrayLevelCounterLabel); this.contentContainer.Controls.Add(this.btnRightArray); @@ -72,6 +77,8 @@ this.contentContainer.Controls.SetChildIndex(this.btnRightArray, 0); this.contentContainer.Controls.SetChildIndex(this.arrayLevelCounterLabel, 0); this.contentContainer.Controls.SetChildIndex(this.chkDisplayAlpha, 0); + this.contentContainer.Controls.SetChildIndex(this.displayEncodedHDRAlphaChk, 0); + this.contentContainer.Controls.SetChildIndex(this.gammaUD, 0); // // pbTopFace // @@ -182,6 +189,41 @@ this.chkDisplayAlpha.UseVisualStyleBackColor = true; this.chkDisplayAlpha.CheckedChanged += new System.EventHandler(this.chkDisplayAlpha_CheckedChanged); // + // displayEncodedHDRAlphaChk + // + this.displayEncodedHDRAlphaChk.AutoSize = true; + this.displayEncodedHDRAlphaChk.Location = new System.Drawing.Point(421, 117); + this.displayEncodedHDRAlphaChk.Name = "displayEncodedHDRAlphaChk"; + this.displayEncodedHDRAlphaChk.Size = new System.Drawing.Size(163, 17); + this.displayEncodedHDRAlphaChk.TabIndex = 22; + this.displayEncodedHDRAlphaChk.Text = "Display Encoded HDR Alpha"; + this.displayEncodedHDRAlphaChk.UseVisualStyleBackColor = true; + this.displayEncodedHDRAlphaChk.CheckedChanged += new System.EventHandler(this.displayEncodedHDRAlphaChk_CheckedChanged); + // + // gammaUD + // + this.gammaUD.DecimalPlaces = 5; + this.gammaUD.Increment = new decimal(new int[] { + 5, + 0, + 0, + 196608}); + this.gammaUD.Location = new System.Drawing.Point(421, 149); + this.gammaUD.Maximum = new decimal(new int[] { + 1000000000, + 0, + 0, + 0}); + this.gammaUD.Minimum = new decimal(new int[] { + 100000000, + 0, + 0, + -2147483648}); + this.gammaUD.Name = "gammaUD"; + this.gammaUD.Size = new System.Drawing.Size(120, 20); + this.gammaUD.TabIndex = 23; + this.gammaUD.ValueChanged += new System.EventHandler(this.gammaUD_ValueChanged); + // // CubeMapFaceViewer // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -198,6 +240,7 @@ ((System.ComponentModel.ISupportInitialize)(this.pbBottomFace)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pbBackFace)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pbRightFace)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.gammaUD)).EndInit(); this.ResumeLayout(false); } @@ -214,5 +257,7 @@ private STButton btnLeftArray; private PictureBoxCustom pbRightFace; private STCheckBox chkDisplayAlpha; + private STCheckBox displayEncodedHDRAlphaChk; + private NumericUpDownFloat gammaUD; } } \ No newline at end of file diff --git a/Switch_Toolbox_Library/Forms/Editors/ImageEditor/CubeMapFaceViewer.cs b/Switch_Toolbox_Library/Forms/Editors/ImageEditor/CubeMapFaceViewer.cs index d998ddf5..00492b9d 100644 --- a/Switch_Toolbox_Library/Forms/Editors/ImageEditor/CubeMapFaceViewer.cs +++ b/Switch_Toolbox_Library/Forms/Editors/ImageEditor/CubeMapFaceViewer.cs @@ -13,6 +13,8 @@ namespace Toolbox.Library.Forms public partial class CubeMapFaceViewer : STForm { private bool DisplayAlpha = true; + private bool DisplayHDREncode = false; + private float Gamma = 2.2f; public CubeMapFaceViewer() { @@ -31,6 +33,8 @@ namespace Toolbox.Library.Forms pbTopFace.Paint += CreatePictureBoxText("Top"); pbBottomFace.Paint += CreatePictureBoxText("Bottom"); chkDisplayAlpha.Checked = DisplayAlpha; + displayEncodedHDRAlphaChk.Checked = DisplayHDREncode; + gammaUD.Value = (decimal)Gamma; } private PaintEventHandler CreatePictureBoxText(string Text) @@ -80,9 +84,14 @@ namespace Toolbox.Library.Forms for (int i = 0; i < 6; i++) { var CubeFaceBitmap = ActiveTexture.GetBitmap(i + (CurArrayDisplayLevel * 6)); - if (!DisplayAlpha) + if (DisplayHDREncode) + CubeFaceBitmap = BitmapExtension.EncodeHDRAlpha(CubeFaceBitmap, Gamma); + else if (!DisplayAlpha) BitmapExtension.SetChannel(CubeFaceBitmap, ActiveTexture.RedChannel, ActiveTexture.GreenChannel, ActiveTexture.BlueChannel, STChannelType.One); + + + if (i == FRONT_FACE) pbFrontFace.Image = CubeFaceBitmap; else if (i == BACK_FACE) @@ -129,5 +138,16 @@ namespace Toolbox.Library.Forms UpdateArrayLevel(); } + + private void displayEncodedHDRAlphaChk_CheckedChanged(object sender, EventArgs e) + { + DisplayHDREncode = displayEncodedHDRAlphaChk.Checked; + UpdateArrayLevel(); + } + + private void gammaUD_ValueChanged(object sender, EventArgs e) { + Gamma = (float)gammaUD.Value; + UpdateArrayLevel(); + } } } diff --git a/Switch_Toolbox_Library/Imaging/BitmapExtension.cs b/Switch_Toolbox_Library/Imaging/BitmapExtension.cs index a50eb955..c3a2495a 100644 --- a/Switch_Toolbox_Library/Imaging/BitmapExtension.cs +++ b/Switch_Toolbox_Library/Imaging/BitmapExtension.cs @@ -522,6 +522,49 @@ namespace Toolbox.Library return b; } + + public static Bitmap EncodeHDRAlpha(Image image, float gamma = 2.2f) + { + var b = new Bitmap(image); + + BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), + ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); + int stride = bmData.Stride; + System.IntPtr Scan0 = bmData.Scan0; + + unsafe + { + byte* p = (byte*)(void*)Scan0; + + int nOffset = stride - b.Width * 4; + + for (int y = 0; y < b.Height; ++y) + { + for (int x = 0; x < b.Width; ++x) + { + float alpha = p[3] / 255f; + for (int i = 0; i < 3; i++) + { + var col = (p[i] / 255f) * (float)Math.Pow(alpha, 4) * 1024; + col = col / (col + 1.0f); + col = (float)Math.Pow(col, 1.0f / gamma); + + p[i] = (byte)(col * 255); + } + + p[3] = 255; + + p += 4; + } + p += nOffset; + } + } + + b.UnlockBits(bmData); + + return b; + } + public static bool Invert(Bitmap b) { BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),