From 897db1e699f720c9127864a058d9001df617bc83 Mon Sep 17 00:00:00 2001 From: SikongJueluo Date: Fri, 28 Jun 2024 21:20:29 +0800 Subject: [PATCH] just test gamma correction algorithm --- {ColorBlender => Color}/ColorBlender.v | 15 ++++++-- Color/GammaCorrection.v | 53 ++++++++++++++++++++++++++ isp.v | 4 +- sim/Makefile | 2 +- sim/sc_main.cpp | 44 ++++++++++----------- 5 files changed, 88 insertions(+), 30 deletions(-) rename {ColorBlender => Color}/ColorBlender.v (83%) create mode 100644 Color/GammaCorrection.v diff --git a/ColorBlender/ColorBlender.v b/Color/ColorBlender.v similarity index 83% rename from ColorBlender/ColorBlender.v rename to Color/ColorBlender.v index 3b3c108..27b591a 100644 --- a/ColorBlender/ColorBlender.v +++ b/Color/ColorBlender.v @@ -44,7 +44,7 @@ module ColorBlender #( always @(*) begin case (state) - READ_DATA: nextState = (in_en) ? (color_correction ? CALC_DATA : SEND_DATA) : READ_DATA; + READ_DATA: nextState = (in_en) ? CALC_DATA : READ_DATA; CALC_DATA: nextState = SATI_DATA; SATI_DATA: nextState = SEND_DATA; SEND_DATA: nextState = (in_receive) ? READ_DATA : SEND_DATA; @@ -75,9 +75,16 @@ module ColorBlender #( end CALC_DATA: begin - data_cal[0] <= (data_cal[0] * {{(BUFF_SIZE - 16){1'b0}}, gain_red}) >> 16; - data_cal[1] <= (data_cal[1] * {{(BUFF_SIZE - 16){1'b0}}, gain_green}) >> 16; - data_cal[2] <= (data_cal[2] * {{(BUFF_SIZE - 16){1'b0}}, gain_blue}) >> 16; + if (color_correction) begin + data_cal[0] <= (data_cal[0] * {{(BUFF_SIZE - 16){1'b0}}, gain_red}) >> 16; + data_cal[1] <= (data_cal[1] * {{(BUFF_SIZE - 16){1'b0}}, gain_green}) >> 16; + data_cal[2] <= (data_cal[2] * {{(BUFF_SIZE - 16){1'b0}}, gain_blue}) >> 16; + end + else begin + data_cal[0] <= data_cal[0] >> 8; + data_cal[1] <= data_cal[1] >> 8; + data_cal[2] <= data_cal[2] >> 8; + end end SATI_DATA: begin diff --git a/Color/GammaCorrection.v b/Color/GammaCorrection.v new file mode 100644 index 0000000..e1dd429 --- /dev/null +++ b/Color/GammaCorrection.v @@ -0,0 +1,53 @@ +`timescale 1ns / 1ps + +module GammaCorrection #( + parameter reg [4:0] COLOR_DEPTH = 8 +) ( + input wire clk, + input wire reset, + + input wire in_en, + input wire [COLOR_DEPTH - 1 : 0] in_data [3], + output wire out_ready, + output wire out_receive, + + output reg out_en, + output reg [COLOR_DEPTH - 1 : 0] out_data [3], + input wire in_ready, + input wire in_receive, + + input wire [8:0] gamma +); + reg [2:0] state, nextState; + localparam reg [2:0] READ_DATA = 0; + localparam reg [2:0] CALC_DATA = 1; + localparam reg [2:0] SEND_DATA = 2; + + reg [15:0] data_cal[3]; + + always @(posedge clk or posedge reset) begin + if (reset) state <= READ_DATA; + else state <= nextState; + end + + always @(*) begin + case (state) + READ_DATA: nextState = in_en ? CALC_DATA : READ_DATA; + CALC_DATA: nextState = SEND_DATA; + SEND_DATA: nextState = in_receive ? READ_DATA : SEND_DATA; + default: nextState = READ_DATA; + endcase + end + + always @(posedge clk or posedge reset) begin + if (reset) begin + out_en <= 0; + out_data <= 0; + end + else begin + + end + end + + +endmodule diff --git a/isp.v b/isp.v index 947ac92..ab901d4 100644 --- a/isp.v +++ b/isp.v @@ -24,11 +24,11 @@ module isp #( input wire in_ready, input wire in_receive, - // 是否启动颜色矫正 + // 颜色校正,低八位为小数位,高八位为整数位 input wire [15:0] gain_red, input wire [15:0] gain_green, input wire [15:0] gain_blue, - input wire color_correction + input wire color_correction // 是否启用颜色校正 ); localparam reg [15:0] BAYER_WIDTH = IN_WIDTH - 2; localparam reg [15:0] BAYER_HEIGHT = IN_HEIGHT - 2; diff --git a/sim/Makefile b/sim/Makefile index f578806..050454e 100644 --- a/sim/Makefile +++ b/sim/Makefile @@ -56,7 +56,7 @@ VERILATOR_FLAGS += --threads 14 TOP_MODULE = isp VERILATOR_FLAGS += -top $(TOP_MODULE) # Input files for Verilator -VERILATOR_INPUT = ../isp.v *.cpp ../Demosaic/Demosaic2.v ../Crop/*.v ../ColorBlender/*.v ../RAM/*.v +VERILATOR_INPUT = ../isp.v *.cpp ../Demosaic/Demosaic2.v ../Crop/*.v ../Color/*.v ../RAM/*.v # Check if SC exists via a verilator call (empty if not) SYSTEMC_EXISTS := $(shell $(VERILATOR) --get-supported SYSTEMC) diff --git a/sim/sc_main.cpp b/sim/sc_main.cpp index 4c881fe..f3a3609 100644 --- a/sim/sc_main.cpp +++ b/sim/sc_main.cpp @@ -28,15 +28,15 @@ static const uint16_t OUT_WIDTH = 1920; static const uint16_t OUT_HEIGHT = 1080; #define OUT_SIZE (OUT_WIDTH * OUT_HEIGHT) -struct color_gain -{ +// color gain for correcting color +struct color_gain { double red; double green; double blue; -}color_gain {1.0, 0.5, 1.1}; +} color_gain{1.3, 0.8, 1.3}; +static const double gamma_value = 2.2; -using namespace std; using namespace sc_core; using namespace sc_dt; @@ -56,8 +56,8 @@ SC_MODULE(TB_ISP) { sc_in im_data; sc_out is_done; - unique_ptr image = make_unique(IN_SIZE); - unique_ptr out = make_unique(OUT_SIZE); + std::unique_ptr image = std::make_unique(IN_SIZE); + std::unique_ptr out = std::make_unique(OUT_SIZE); SC_CTOR(TB_ISP) { SC_CTHREAD(send_Data, clk.pos()); @@ -137,25 +137,25 @@ SC_MODULE(TB_ISP) { }; int sc_main(int argc, char* argv[]) { - cout << "Get into sc_main" << endl; + std::cout << "Get into sc_main" << std::endl; // Open image - ifstream in_image; - ofstream out_image; - in_image.open("./transform/test.bin", ios::in | ios::binary); - out_image.open("./transform/out.bin", ios::out | ios::binary); + std::ifstream in_image; + std::ofstream out_image; + in_image.open("./transform/test.bin", std::ios::in | std::ios::binary); + out_image.open("./transform/out.bin", std::ios::out | std::ios::binary); if (!in_image.is_open()) { - cout << "Open image fail" << endl; + std::cout << "Open image fail" << std::endl; exit(0); } else { - cout << "Ready to sim" << endl; + std::cout << "Ready to sim" << std::endl; } // Read image - auto buf = make_unique(2 * IN_SIZE); + auto buf = std::make_unique(2 * IN_SIZE); in_image.read((char*)buf.get(), IN_SIZE * 2); in_image.close(); // Reshape data - auto image = make_unique(IN_SIZE); + auto image = std::make_unique(IN_SIZE); uint32_t i = 0; for (int y = 0; y < IN_HEIGHT; y++) { for (int x = 0; x < IN_WIDTH; x++) { @@ -164,7 +164,7 @@ int sc_main(int argc, char* argv[]) { i += 2; } } - cout << "Finish Reading data" << endl; + std::cout << "Finish Reading data" << std::endl; // This is a more complicated example, please also see the simpler // examples/make_hello_c. @@ -306,7 +306,7 @@ int sc_main(int argc, char* argv[]) { } // Save output image - cout << "Ready to save raw RGB image" << endl; + std::cout << "Ready to save raw RGB image" << std::endl; // for (int y = 0; y < OUT_HEIGHT; y++) // for(int x = 0; x < OUT_WIDTH; x++) // out_image.write((const char *)&tb_isp.out[y * OUT_WIDTH + x], @@ -324,6 +324,10 @@ int sc_main(int argc, char* argv[]) { uint8_t green = (tb_isp.out[y * OUT_WIDTH + x] & 0x0000ff00) >> 8; uint8_t blue = (tb_isp.out[y * OUT_WIDTH + x] & 0x000000ff); + // red = 255 * std::pow(red / 255.0, 1 / gamma_value); + // green = 255 * std::pow(green / 255.0, 1 / gamma_value); + // blue = 255 * std::pow(blue / 255.0, 1 / gamma_value); + out_image.write((const char*)&red, sizeof(red)); out_image.write((const char*)&green, sizeof(green)); out_image.write((const char*)&blue, sizeof(blue)); @@ -337,12 +341,6 @@ int sc_main(int argc, char* argv[]) { } } - // for (int i = 0; i < OUT_WIDTH * OUT_HEIGHT * 3; i += 3) { - // data[i + 0] = std::min(255, static_cast(data[i + 0] * - // red_gain)); data[i + 1] = std::min(255, static_cast(data[i + 1] - // * green_gain)); data[i + 2] = std::min(255, static_cast(data[i + - // 2] * blue_gain)); - // } write_bmp("test.bmp", data, OUT_WIDTH, OUT_HEIGHT); delete[] data;