Преглед изворни кода

Add PNG utility to extract PNG data

habeebweeb пре 3 година
родитељ
комит
0a00741324
1 измењених фајлова са 53 додато и 0 уклоњено
  1. 53 0
      src/MeidoPhotoStudio.Converter/Utility/PngUtility.cs

+ 53 - 0
src/MeidoPhotoStudio.Converter/Utility/PngUtility.cs

@@ -0,0 +1,53 @@
+using System;
+using System.IO;
+
+namespace MeidoPhotoStudio.Converter.Utility
+{
+    internal static class PngUtility
+    {
+        private static readonly byte[] PngHeader = { 137, 80, 78, 71, 13, 10, 26, 10 };
+        private static readonly byte[] PngEnd = System.Text.Encoding.ASCII.GetBytes("IEND");
+
+        public static byte[]? ExtractPng(Stream stream)
+        {
+            var memoryStream = new MemoryStream();
+
+            var headerBuffer = new byte[PngHeader.Length];
+
+            stream.Read(headerBuffer, 0, headerBuffer.Length);
+
+            if (!MeidoPhotoStudio.Plugin.Utility.BytesEqual(headerBuffer, PngHeader))
+                return null;
+
+            memoryStream.Write(headerBuffer, 0, headerBuffer.Length);
+
+            var fourByteBuffer = new byte[4];
+            var chunkBuffer = new byte[1024];
+
+            do
+            {
+                // chunk length
+                var read = stream.Read(fourByteBuffer, 0, 4);
+                memoryStream.Write(fourByteBuffer, 0, read);
+
+                if (BitConverter.IsLittleEndian)
+                    Array.Reverse(fourByteBuffer);
+
+                var length = BitConverter.ToUInt32(fourByteBuffer, 0);
+
+                // chunk type
+                read = stream.Read(fourByteBuffer, 0, 4);
+                memoryStream.Write(fourByteBuffer, 0, read);
+
+                if (chunkBuffer.Length < length + 4L)
+                    chunkBuffer = new byte[length + 4L];
+
+                // chunk data + CRC
+                read = stream.Read(chunkBuffer, 0, (int)(length + 4L));
+                memoryStream.Write(chunkBuffer, 0, read);
+            } while (!MeidoPhotoStudio.Plugin.Utility.BytesEqual(fourByteBuffer, PngEnd));
+
+            return memoryStream.ToArray();
+        }
+    }
+}