Unity 3D :解 RAW 8bit 高速版 ( 可調整大小 )

前言 :

單線程極速版,單線程最快代碼,估計不會有比這更快的了,我的解 RAW CPU 單線程旅程終於可以結束了…。

多線程估計能更快,這就交給你們改良了…


執行結果 ( 人類閱讀版 ) :

在这里插入图片描述


人類閱讀版 C # :

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class RAW8_Decoder : MonoBehaviour
{
    public RawImage img;
    public Text txt;

    public void Run()
    {
        string path = "C:/1335/RAW/8bit.raw";

        byte[] b = File.ReadAllBytes(path);

        int width = 4208;
        int height = 3120;

        System.Diagnostics.Stopwatch time = new System.Diagnostics.Stopwatch();
        time.Start();

        Texture2D t = new Texture2D(width / 2, height / 2, TextureFormat.RGB24, true);

        byte[] b2 = new byte[t.width * t.height * 4]; // 不知為啥要乘 4 ,理論上乘 3 就好..但會報錯...

        int i = 0; // i : Gr R B Gb
        int k = 0; // k : R  G B

        for (int y = 0; y < height; y += 2)
        {
            for (int x = 0; x < width; x += 2, i += 2, k += 3)
            {
                b2[k + 0] = b[i + 1];     // R
                b2[k + 1] = b[i];         // Gr
                b2[k + 2] = b[i + width]; // B
                // byte Gb = b[i + width + 1]; // 自行啟用
            }
            i += width;
        }

        t.LoadRawTextureData(b2);
        t.Apply();

        img.texture = t;

        time.Stop();
        txt.text = "解 RAW 花費 : " + time.Elapsed.TotalSeconds + " 秒";
    }
}

執行結果 ( 可調整大小版 ) :

與其說調整大小,不如說跳讀…。

所以解析度越小,讀取速度越快。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


可調整大小版 :

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class RAW8_Decoder : MonoBehaviour
{
    public RawImage img;
    public Text txt;

    public void Run()
    {
        txt.text = "";

        string path = "C:/1335/RAW/8bit.raw";

        byte[] b = File.ReadAllBytes(path);

        int width = 4208;
        int height = 3120;

        System.Diagnostics.Stopwatch time = new System.Diagnostics.Stopwatch();

        int division = 8; // 長寬各除以 division 倍。可以填入 : 2 4 8 16 的值。

        Texture2D t = new Texture2D(width / division, height / division, TextureFormat.RGB24, true);

        txt.text += t.width + " x " + t.height + "\n\n";

        byte[] b2 = new byte[t.width * t.height * 4]; // 不知為啥要乘 4 ,理論上乘 3 就好..但會報錯...

        int i = 0; // i : Gr R B Gb
        int k = 0; // k : R  G B

        int w = width * (division - 1); // 跳 w 行

        time.Start();

        for (int y = 0; y < height; y += division, i += w)
        {
            for (int x = 0; x < width; x += division, i += division, k += 3)
            {
                b2[k + 0] = b[i + 1];     // R
                b2[k + 1] = b[i];         // Gr
                b2[k + 2] = b[i + width]; // B
            }
        }

        t.LoadRawTextureData(b2);
        t.Apply();

        time.Stop();

        img.texture = t;

        txt.text += "解 RAW 花費 : " + time.Elapsed.TotalSeconds + " 秒 \n";
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值