`timescale 1ns/1ps

// 三通道图像合成一个RGB图像
module chanels_to_RGB #(
    parameter IN_DEPTH = 12,        // 输入图像的色深
    parameter OUT_DEPTH = 8         // 输出图像的色深
) (
    input clk,
    input reset,

    input in_en,
    input [IN_DEPTH - 1:0] data_in [2:0], // 0:R 1:G 2:B

    output reg out_en,
    output reg [3 * OUT_DEPTH - 1:0] data_out
);
    reg [31:0] data_cal [2:0]; // 用于保存运算结果，防止溢出

    always @(posedge clk or posedge reset) begin
        if (reset) begin
            // 初始化
            out_en <= 0;
            data_out <= 0;
        end
        else begin
            if (in_en) begin
                data_cal[0] <= data_in[0] * OUT_DEPTH / IN_DEPTH;
                data_cal[1] <= data_in[1] * OUT_DEPTH / IN_DEPTH;
                data_cal[2] <= data_in[2] * OUT_DEPTH / IN_DEPTH;

                data_out <= {data_cal[0][OUT_DEPTH - 1:0], data_cal[1][OUT_DEPTH - 1:0],data_cal[2][OUT_DEPTH - 1:0]};
            end
            out_en <= in_en;
        end
    end
endmodule