Skip to content

Commit e92a6fb

Browse files
authored
Merge pull request #138 from EverseDevelopment/86b7dh14u/FixImageUnsuportedFeatures
Fix Image Gamma Unsupported By glTF
2 parents 3203698 + b58a474 commit e92a6fb

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Common_glTF_Exporter/Materials/BitmapsUtils.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ public static (string, ImageFormat) GetMimeType(string path)
3030
}
3131
}
3232

33+
/// <summary>
34+
/// Removes non-default gamma or ICC profile metadata from a PNG/JPG file
35+
/// by re-encoding it to a clean sRGB version in memory.
36+
/// </summary>
37+
/// <param name="path">Path to the source image</param>
38+
/// <returns>Byte array of the cleaned image (PNG)</returns>
39+
public static byte[] CleanGamma(string path, ImageFormat imageFormat)
40+
{
41+
using (var original = new Bitmap(path))
42+
using (var ms = new MemoryStream())
43+
{
44+
// Convert to standard sRGB (System.Drawing assumes sRGB by default)
45+
using (var converted = new Bitmap(original.Width, original.Height, PixelFormat.Format24bppRgb))
46+
using (var g = Graphics.FromImage(converted))
47+
{
48+
g.DrawImage(original, 0, 0, original.Width, original.Height);
49+
converted.Save(ms, imageFormat);
50+
}
51+
return ms.ToArray();
52+
}
53+
}
54+
3355
public static byte[] BlendImageWithColor(
3456
byte[] imageBytes,
3557
double fade,

Common_glTF_Exporter/Utils/GLTFBinaryDataUtils.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ public static int ExportImageBuffer(
211211

212212
if (material.pbrMetallicRoughness.baseColorTexture.index == -1)
213213
{
214-
byte[] imageBytes = File.ReadAllBytes(material.EmbeddedTexturePath);
215-
(string , ImageFormat) mimeType = BitmapsUtils.GetMimeType(material.EmbeddedTexturePath);
216-
214+
(string, ImageFormat) mimeType = BitmapsUtils.GetMimeType(material.EmbeddedTexturePath);
215+
byte[] imageBytes = BitmapsUtils.CleanGamma(material.EmbeddedTexturePath, mimeType.Item2);
216+
217217
byte[] blendedBytes = BitmapsUtils.BlendImageWithColor(imageBytes, material.Fadevalue,
218218
material.BaseColor, mimeType.Item2, material.TintColour);
219219

0 commit comments

Comments
 (0)