背景
美术出图后,其实大量图片含有无用turnk块,该功能就是通过解析png字节数据,删除了无用turnk块 tEXt, iTXt, and zTXt。
https://www.w3.org/TR/png/#11textIntro
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using UnityEngine;
class PngDeleteInvaildChunk
{
static byte[] header;
static byte[] ident = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };//图片头
static public float Start(string srcPng)
{
Stream srcStream = File.OpenRead(srcPng);
BinaryReader reader = new BinaryReader(srcStream);
List<byte> destFileBytes = new List<byte>();
header = reader.ReadBytes(8);
if ((header.Length != ident.Length) || !(header.Select((x, i) => (x == ident[i])).All(x => x)))
{
Debug.Log("File is not PNG");
reader.Close();
return 0f;
}
destFileBytes.AddRange(header);
while (srcStream.Position < srcStream.Length)
{
byte[] byteLength = reader.ReadBytes(4);
if (byteLength.Length < 4)
{
//文件尾部的错误直接忽略掉
Debug.Log("Unexpected End Of File");
break;
}
UInt32 length = (UInt32)((byteLength[0] << 24) | (byteLength[1] << 16) | (byteLength[2] << 8) | byteLength[3]);
byte[] chunkType = reader.ReadBytes(4);
if (chunkType.Length < 4)
{
Debug.Log("Unexpected End Of File");
break;
}
string chunkName = Encoding.ASCII.GetString(chunkType);
//这里需要防止length过长需要特殊读取
UInt32 tmpLength = length;
List<byte> byteList = new List<byte>();
while (tmpLength > 0)
{
if (tmpLength > 10000)
{
byte[] tmpData = reader.ReadBytes(10000);
byteList.AddRange(tmpData);
tmpLength -= 10000;
}
else
{
byte[] tmpData = reader.ReadBytes((int)tmpLength);
byteList.AddRange(tmpData);
tmpLength = 0;
}
}
byte[] data = byteList.ToArray();
if (data.Length < length)
{
//文件尾部的错误直接忽略掉
Debug.Log("Unexpected End Of File");
break;
}
byte[] CRC = reader.ReadBytes(4);
if (CRC.Length < 4)
{
//文件尾部的错误直接忽略掉
Debug.Log("Unexpected End Of File");
break;
}
switch (chunkName)
{
case "tEXt":
case "iTXt":
case "zTXt":
continue;
}
destFileBytes.AddRange(byteLength);
destFileBytes.AddRange(chunkType);
destFileBytes.AddRange(data);
destFileBytes.AddRange(CRC);
}
float size = 0f;
if (destFileBytes.Count < srcStream.Length)
{
size = (float)(srcStream.Length / 1024.0 / 1024.0 - destFileBytes.Count / 1024.0 / 1024.0);
Debug.LogFormat("{0}优化前:{1:f2}mb, 优化后:{2:f2}mb", srcPng, srcStream.Length / 1024.0 / 1024.0, destFileBytes.Count / 1024.0 / 1024.0);
reader.Close();
File.WriteAllBytes(srcPng, destFileBytes.ToArray());
}
else
{
Debug.LogFormat("{0}无需优化", srcPng);
reader.Close();
}
return size;
}
}