From c14458d7ecf579f62c5e8b9edef1a155fd6972f7 Mon Sep 17 00:00:00 2001 From: JT Date: Sat, 9 Sep 2023 14:44:51 -0700 Subject: [PATCH] Add PngDataHelper.RemoveMetadata method --- .../Helpers/PngDataHelper.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/StabilityMatrix.Avalonia/Helpers/PngDataHelper.cs b/StabilityMatrix.Avalonia/Helpers/PngDataHelper.cs index e6b61c43..76657e90 100644 --- a/StabilityMatrix.Avalonia/Helpers/PngDataHelper.cs +++ b/StabilityMatrix.Avalonia/Helpers/PngDataHelper.cs @@ -57,6 +57,28 @@ public static class PngDataHelper return finalImage.ToArray(); } + public static byte[] RemoveMetadata(byte[] inputImage) + { + var firstTextIndex = SearchBytes(inputImage, Text); + if (firstTextIndex == -1) + return inputImage; + + // Don't want the size bytes either + firstTextIndex -= 4; + var existingHeader = inputImage[..firstTextIndex]; + + // Go back 4 from the idat index because we need the length of the data + var idatIndex = SearchBytes(inputImage, Idat) - 4; + + // Go forward 8 from the iend index because we need the crc + var iendIndex = SearchBytes(inputImage, Iend) + 8; + + var actualImageData = inputImage[idatIndex..iendIndex]; + var finalImage = existingHeader.Concat(actualImageData); + + return finalImage.ToArray(); + } + private static byte[] GetTextChunk(string key, string value) { var textData = $"{key}\0{value}";