Advertisement
Bunny83

PNGTools.cs

Nov 30th, 2017
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.48 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using UnityEngine;
  4.  
  5. namespace B83.Image
  6. {
  7.     public enum EChunkType : uint
  8.     {
  9.         IHDR = 0x49484452,
  10.         sRBG = 0x73524742,
  11.         gAMA = 0x67414D41,
  12.         cHRM = 0x6348524D,
  13.         pHYs = 0x70485973,
  14.         IDAT = 0x49444154,
  15.         IEND = 0x49454E44,
  16.     }
  17.  
  18.     public class PNGFile
  19.     {
  20.         public static ulong m_Signature = 0x89504E470D0A1A0AU; //
  21.         public ulong Signature;
  22.         public List<PNGChunk> chunks;
  23.         public int FindChunk(EChunkType aType, int aStartIndex = 0)
  24.         {
  25.             if (chunks == null)
  26.                 return -1;
  27.             for(int i = aStartIndex; i < chunks.Count; i++)
  28.             {
  29.                 if (chunks[i].type == aType)
  30.                     return i;
  31.             }
  32.             return -1;
  33.         }
  34.     }
  35.  
  36.     public class PNGChunk
  37.     {
  38.         public uint length;
  39.         public EChunkType type;
  40.         public byte[] data;
  41.         public uint crc;
  42.         public uint CalcCRC()
  43.         {
  44.             var crc = PNGTools.UpdateCRC(0xFFFFFFFF, (uint)type);
  45.             crc = PNGTools.UpdateCRC(crc, data);
  46.             return crc ^ 0xFFFFFFFF;
  47.         }
  48.     }
  49.  
  50.     public class PNGTools
  51.     {
  52.         static uint[] crc_table = new uint[256];
  53.         static PNGTools()
  54.         {
  55.             for (int n = 0; n < 256; n++)
  56.             {
  57.                 uint c = (uint)n;
  58.                 for (int k = 0; k < 8; k++)
  59.                 {
  60.                     if ((c & 1) > 0)
  61.                         c = 0xedb88320 ^ (c >> 1);
  62.                     else
  63.                         c = c >> 1;
  64.                 }
  65.                 crc_table[n] = c;
  66.             }
  67.         }
  68.  
  69.         public static uint UpdateCRC(uint crc, byte aData)
  70.         {
  71.             return crc_table[(crc ^ aData) & 0xff] ^ (crc >> 8);
  72.         }
  73.  
  74.         public static uint UpdateCRC(uint crc, uint aData)
  75.         {
  76.             crc = crc_table[(crc ^ ((aData >> 24) & 0xFF)) & 0xff] ^ (crc >> 8);
  77.             crc = crc_table[(crc ^ ((aData >> 16) & 0xFF)) & 0xff] ^ (crc >> 8);
  78.             crc = crc_table[(crc ^ ((aData >> 8) & 0xFF)) & 0xff] ^ (crc >> 8);
  79.             crc = crc_table[(crc ^ (aData & 0xFF)) & 0xff] ^ (crc >> 8);
  80.             return crc;
  81.         }
  82.  
  83.  
  84.         public static uint UpdateCRC(uint crc, byte[] buf)
  85.         {
  86.             for (int n = 0; n < buf.Length; n++)
  87.                 crc = crc_table[(crc ^ buf[n]) & 0xff] ^ (crc >> 8);
  88.             return crc;
  89.         }
  90.  
  91.         public static uint CalculateCRC(byte[] aBuf)
  92.         {
  93.             return UpdateCRC(0xffffffff, aBuf) ^ 0xffffffff;
  94.         }
  95.         public static List<PNGChunk> ReadChunks(BinaryReader aReader)
  96.         {
  97.             var res = new List<PNGChunk>();
  98.             while (aReader.BaseStream.Position < aReader.BaseStream.Length - 4)
  99.             {
  100.                 var chunk = new PNGChunk();
  101.                 chunk.length = aReader.ReadUInt32BE();
  102.                 if (aReader.BaseStream.Position >= aReader.BaseStream.Length - 4 - chunk.length)
  103.                     break;
  104.                 res.Add(chunk);
  105.                 chunk.type = (EChunkType)aReader.ReadUInt32BE();
  106.                 chunk.data = aReader.ReadBytes((int)chunk.length);
  107.                 chunk.crc = aReader.ReadUInt32BE();
  108.  
  109.                 uint crc = chunk.CalcCRC();
  110.  
  111.                 if ((chunk.crc ^ crc) != 0)
  112.                     Debug.Log("Chunk CRC wrong. Got 0x" + chunk.crc.ToString("X8") + " expected 0x" + crc.ToString("X8"));
  113.                 if (chunk.type == EChunkType.IEND)
  114.                     break;
  115.             }
  116.             return res;
  117.         }
  118.  
  119.         public static PNGFile ReadPNGFile(BinaryReader aReader)
  120.         {
  121.             if (aReader == null || aReader.BaseStream.Position >= aReader.BaseStream.Length - 8)
  122.                 return null;
  123.             var file = new PNGFile();
  124.             file.Signature = aReader.ReadUInt64BE();
  125.             file.chunks = ReadChunks(aReader);
  126.             return file;
  127.         }
  128.         public static void WritePNGFile(PNGFile aFile, BinaryWriter aWriter)
  129.         {
  130.             aWriter.WriteUInt64BE(PNGFile.m_Signature);
  131.             foreach (var chunk in aFile.chunks)
  132.             {
  133.                 aWriter.WriteUInt32BE((uint)chunk.data.Length);
  134.                 aWriter.WriteUInt32BE((uint)chunk.type);
  135.                 aWriter.Write(chunk.data);
  136.                 aWriter.WriteUInt32BE(chunk.crc);
  137.             }
  138.         }
  139.  
  140.         public static void SetPPM(PNGFile aFile, uint aXPPM, uint aYPPM)
  141.         {
  142.             int pos = aFile.FindChunk(EChunkType.pHYs);
  143.             PNGChunk chunk;
  144.             if (pos > 0)
  145.             {
  146.                 chunk = aFile.chunks[pos];
  147.                 if (chunk.data == null || chunk.data.Length < 9)
  148.                     throw new System.Exception("PNG: pHYs chunk data size is too small. It should be at least 9 bytes");
  149.             }
  150.             else
  151.             {
  152.                 chunk = new PNGChunk();
  153.                 chunk.type = EChunkType.pHYs;
  154.                 chunk.length = 9;
  155.                 chunk.data = new byte[9];
  156.                 aFile.chunks.Insert(1, chunk);
  157.             }
  158.             var data = chunk.data;
  159.             data[0] = (byte)((aXPPM >> 24) & 0xFF);
  160.             data[1] = (byte)((aXPPM >> 16) & 0xFF);
  161.             data[2] = (byte)((aXPPM >> 8) & 0xFF);
  162.             data[3] = (byte)((aXPPM) & 0xFF);
  163.  
  164.             data[4] = (byte)((aYPPM >> 24) & 0xFF);
  165.             data[5] = (byte)((aYPPM >> 16) & 0xFF);
  166.             data[6] = (byte)((aYPPM >> 8) & 0xFF);
  167.             data[7] = (byte)((aYPPM) & 0xFF);
  168.  
  169.             data[8] = 1;
  170.             chunk.crc = chunk.CalcCRC();
  171.         }
  172.  
  173.         public static byte[] ChangePPM(byte[] aPNGData, uint aXPPM, uint aYPPM)
  174.         {
  175.             PNGFile file;
  176.             using (var stream = new MemoryStream(aPNGData))
  177.             using (var reader = new BinaryReader(stream))
  178.             {
  179.                 file = ReadPNGFile(reader);
  180.             }
  181.             SetPPM(file, aXPPM, aYPPM);
  182.             using (var stream = new MemoryStream())
  183.             using (var writer = new BinaryWriter(stream))
  184.             {
  185.                 WritePNGFile(file, writer);
  186.                 return stream.ToArray();
  187.             }
  188.         }
  189.         public static byte[] ChangePPI(byte[] aPNGData, float aXPPI, float aYPPI)
  190.         {
  191.             return ChangePPM(aPNGData, (uint)(aXPPI * 39.3701f), (uint)(aYPPI * 39.3701f));
  192.         }
  193.     }
  194.  
  195.     public static class BinaryReaderWriterExt
  196.     {
  197.         public static uint ReadUInt32BE(this BinaryReader aReader)
  198.         {
  199.             return ((uint)aReader.ReadByte() << 24) | ((uint)aReader.ReadByte() << 16)
  200.                 | ((uint)aReader.ReadByte() << 8) | ((uint)aReader.ReadByte());
  201.         }
  202.         public static ulong ReadUInt64BE(this BinaryReader aReader)
  203.         {
  204.             return (ulong)aReader.ReadUInt32BE()<<32 | aReader.ReadUInt32BE();
  205.         }
  206.         public static void WriteUInt32BE(this BinaryWriter aWriter, uint aValue)
  207.         {
  208.             aWriter.Write((byte)((aValue >> 24) & 0xFF));
  209.             aWriter.Write((byte)((aValue >> 16) & 0xFF));
  210.             aWriter.Write((byte)((aValue >> 8 ) & 0xFF));
  211.             aWriter.Write((byte)((aValue      ) & 0xFF));
  212.         }
  213.         public static void WriteUInt64BE(this BinaryWriter aWriter, ulong aValue)
  214.         {
  215.             aWriter.WriteUInt32BE((uint)(aValue >> 32));
  216.             aWriter.WriteUInt32BE((uint)(aValue));
  217.         }
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement