From b58a474a0d32bcb737b77ee61584b0ab755391b8 Mon Sep 17 00:00:00 2001 From: vnoves Date: Wed, 12 Nov 2025 11:33:47 -0300 Subject: [PATCH] Fix Image Gamma Unsupported By glTF --- .../Materials/BitmapsUtils.cs | 22 +++++++++++++++++++ .../Utils/GLTFBinaryDataUtils.cs | 6 ++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Common_glTF_Exporter/Materials/BitmapsUtils.cs b/Common_glTF_Exporter/Materials/BitmapsUtils.cs index b470f64..1d6f8ec 100644 --- a/Common_glTF_Exporter/Materials/BitmapsUtils.cs +++ b/Common_glTF_Exporter/Materials/BitmapsUtils.cs @@ -30,6 +30,28 @@ public static (string, ImageFormat) GetMimeType(string path) } } + /// + /// Removes non-default gamma or ICC profile metadata from a PNG/JPG file + /// by re-encoding it to a clean sRGB version in memory. + /// + /// Path to the source image + /// Byte array of the cleaned image (PNG) + public static byte[] CleanGamma(string path, ImageFormat imageFormat) + { + using (var original = new Bitmap(path)) + using (var ms = new MemoryStream()) + { + // Convert to standard sRGB (System.Drawing assumes sRGB by default) + using (var converted = new Bitmap(original.Width, original.Height, PixelFormat.Format24bppRgb)) + using (var g = Graphics.FromImage(converted)) + { + g.DrawImage(original, 0, 0, original.Width, original.Height); + converted.Save(ms, imageFormat); + } + return ms.ToArray(); + } + } + public static byte[] BlendImageWithColor( byte[] imageBytes, double fade, diff --git a/Common_glTF_Exporter/Utils/GLTFBinaryDataUtils.cs b/Common_glTF_Exporter/Utils/GLTFBinaryDataUtils.cs index 0039ad4..93bab77 100644 --- a/Common_glTF_Exporter/Utils/GLTFBinaryDataUtils.cs +++ b/Common_glTF_Exporter/Utils/GLTFBinaryDataUtils.cs @@ -211,9 +211,9 @@ public static int ExportImageBuffer( if (material.pbrMetallicRoughness.baseColorTexture.index == -1) { - byte[] imageBytes = File.ReadAllBytes(material.EmbeddedTexturePath); - (string , ImageFormat) mimeType = BitmapsUtils.GetMimeType(material.EmbeddedTexturePath); - + (string, ImageFormat) mimeType = BitmapsUtils.GetMimeType(material.EmbeddedTexturePath); + byte[] imageBytes = BitmapsUtils.CleanGamma(material.EmbeddedTexturePath, mimeType.Item2); + byte[] blendedBytes = BitmapsUtils.BlendImageWithColor(imageBytes, material.Fadevalue, material.BaseColor, mimeType.Item2, material.TintColour);