Unity 3D : 10~16bit RAW 圖像轉 8bit 圖像

前言 :

這段程式碼可以把 10bit, 12bit, 14bit, 16bit 轉成 8bit。

用途就不用我說明瞭,我想大家都知道,雖然我覺得會做 Unity 3D 的人估計沒啥人會看我這篇文章哈。

這是給特殊需求的人使用的,例如我…。

效果圖 :

左邊是 10bit RAW,右邊是經過我的程式轉成 8bit RAW 的結果圖。人眼估計很難看出差別,但是在做一些圖像處理或拉增益時,10 bit 通常會比 8 bit 好很多。

在這裏插入圖片描述

C# 程式碼 :

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

public class RAW10_To_RAW8 : MonoBehaviour
{
    void Start()
    {
        string input_path = "C:/1335/RAW/10bit.raw"; // 輸入路徑 ( 10 bit )

        string output_path = "C:/1335/RAW/8bit.raw"; // 保存路徑 ( 8 bit )

        byte[] b10 = File.ReadAllBytes(input_path);

        byte [] b8 = raw10_to_raw8(b10);

        File.WriteAllBytes(output_path, b8);
    }


    byte[] raw10_to_raw8(byte[] b10)
    {
        byte[] b8 = new byte[b10.Length / 2];

        for (int i = 0, k = 0; i < b8.Length; i++, k += 2)
        {
            byte L = b10[k + 0];
            byte H = b10[k + 1];
            b8[i] = (byte)((H << 8 | L) / 4);
        }

        return b8;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章