前言 :
單線程極速版,單線程最快代碼,估計不會有比這更快的了,我的解 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";
}
}