unity中减少图片存储体积

背景

美术出图后,其实大量图片含有无用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;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张_0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值