just test gamma correction algorithm

This commit is contained in:
SikongJueluo 2024-06-28 21:20:29 +08:00
parent c527835ff6
commit 897db1e699
No known key found for this signature in database
GPG Key ID: D2D3D29A993716EA
5 changed files with 88 additions and 30 deletions

View File

@ -44,7 +44,7 @@ module ColorBlender #(
always @(*) begin always @(*) begin
case (state) 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; CALC_DATA: nextState = SATI_DATA;
SATI_DATA: nextState = SEND_DATA; SATI_DATA: nextState = SEND_DATA;
SEND_DATA: nextState = (in_receive) ? READ_DATA : SEND_DATA; SEND_DATA: nextState = (in_receive) ? READ_DATA : SEND_DATA;
@ -75,9 +75,16 @@ module ColorBlender #(
end end
CALC_DATA: begin CALC_DATA: begin
data_cal[0] <= (data_cal[0] * {{(BUFF_SIZE - 16){1'b0}}, gain_red}) >> 16; if (color_correction) begin
data_cal[1] <= (data_cal[1] * {{(BUFF_SIZE - 16){1'b0}}, gain_green}) >> 16; data_cal[0] <= (data_cal[0] * {{(BUFF_SIZE - 16){1'b0}}, gain_red}) >> 16;
data_cal[2] <= (data_cal[2] * {{(BUFF_SIZE - 16){1'b0}}, gain_blue}) >> 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 end
SATI_DATA: begin SATI_DATA: begin

53
Color/GammaCorrection.v Normal file
View File

@ -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

4
isp.v
View File

@ -24,11 +24,11 @@ module isp #(
input wire in_ready, input wire in_ready,
input wire in_receive, input wire in_receive,
// 是否启动颜色矫正 // 颜色校正低八位为小数位高八位为整数位
input wire [15:0] gain_red, input wire [15:0] gain_red,
input wire [15:0] gain_green, input wire [15:0] gain_green,
input wire [15:0] gain_blue, 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_WIDTH = IN_WIDTH - 2;
localparam reg [15:0] BAYER_HEIGHT = IN_HEIGHT - 2; localparam reg [15:0] BAYER_HEIGHT = IN_HEIGHT - 2;

View File

@ -56,7 +56,7 @@ VERILATOR_FLAGS += --threads 14
TOP_MODULE = isp TOP_MODULE = isp
VERILATOR_FLAGS += -top $(TOP_MODULE) VERILATOR_FLAGS += -top $(TOP_MODULE)
# Input files for Verilator # 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) # Check if SC exists via a verilator call (empty if not)
SYSTEMC_EXISTS := $(shell $(VERILATOR) --get-supported SYSTEMC) SYSTEMC_EXISTS := $(shell $(VERILATOR) --get-supported SYSTEMC)

View File

@ -28,15 +28,15 @@ static const uint16_t OUT_WIDTH = 1920;
static const uint16_t OUT_HEIGHT = 1080; static const uint16_t OUT_HEIGHT = 1080;
#define OUT_SIZE (OUT_WIDTH * OUT_HEIGHT) #define OUT_SIZE (OUT_WIDTH * OUT_HEIGHT)
struct color_gain // color gain for correcting color
{ struct color_gain {
double red; double red;
double green; double green;
double blue; 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_core;
using namespace sc_dt; using namespace sc_dt;
@ -56,8 +56,8 @@ SC_MODULE(TB_ISP) {
sc_in<uint32_t> im_data; sc_in<uint32_t> im_data;
sc_out<bool> is_done; sc_out<bool> is_done;
unique_ptr<uint16_t[]> image = make_unique<uint16_t[]>(IN_SIZE); std::unique_ptr<uint16_t[]> image = std::make_unique<uint16_t[]>(IN_SIZE);
unique_ptr<uint32_t[]> out = make_unique<uint32_t[]>(OUT_SIZE); std::unique_ptr<uint32_t[]> out = std::make_unique<uint32_t[]>(OUT_SIZE);
SC_CTOR(TB_ISP) { SC_CTOR(TB_ISP) {
SC_CTHREAD(send_Data, clk.pos()); SC_CTHREAD(send_Data, clk.pos());
@ -137,25 +137,25 @@ SC_MODULE(TB_ISP) {
}; };
int sc_main(int argc, char* argv[]) { int sc_main(int argc, char* argv[]) {
cout << "Get into sc_main" << endl; std::cout << "Get into sc_main" << std::endl;
// Open image // Open image
ifstream in_image; std::ifstream in_image;
ofstream out_image; std::ofstream out_image;
in_image.open("./transform/test.bin", ios::in | ios::binary); in_image.open("./transform/test.bin", std::ios::in | std::ios::binary);
out_image.open("./transform/out.bin", ios::out | ios::binary); out_image.open("./transform/out.bin", std::ios::out | std::ios::binary);
if (!in_image.is_open()) { if (!in_image.is_open()) {
cout << "Open image fail" << endl; std::cout << "Open image fail" << std::endl;
exit(0); exit(0);
} else { } else {
cout << "Ready to sim" << endl; std::cout << "Ready to sim" << std::endl;
} }
// Read image // Read image
auto buf = make_unique<uint8_t[]>(2 * IN_SIZE); auto buf = std::make_unique<uint8_t[]>(2 * IN_SIZE);
in_image.read((char*)buf.get(), IN_SIZE * 2); in_image.read((char*)buf.get(), IN_SIZE * 2);
in_image.close(); in_image.close();
// Reshape data // Reshape data
auto image = make_unique<uint16_t[]>(IN_SIZE); auto image = std::make_unique<uint16_t[]>(IN_SIZE);
uint32_t i = 0; uint32_t i = 0;
for (int y = 0; y < IN_HEIGHT; y++) { for (int y = 0; y < IN_HEIGHT; y++) {
for (int x = 0; x < IN_WIDTH; x++) { for (int x = 0; x < IN_WIDTH; x++) {
@ -164,7 +164,7 @@ int sc_main(int argc, char* argv[]) {
i += 2; 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 // This is a more complicated example, please also see the simpler
// examples/make_hello_c. // examples/make_hello_c.
@ -306,7 +306,7 @@ int sc_main(int argc, char* argv[]) {
} }
// Save output image // 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 y = 0; y < OUT_HEIGHT; y++)
// for(int x = 0; x < OUT_WIDTH; x++) // for(int x = 0; x < OUT_WIDTH; x++)
// out_image.write((const char *)&tb_isp.out[y * 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 green = (tb_isp.out[y * OUT_WIDTH + x] & 0x0000ff00) >> 8;
uint8_t blue = (tb_isp.out[y * OUT_WIDTH + x] & 0x000000ff); 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*)&red, sizeof(red));
out_image.write((const char*)&green, sizeof(green)); out_image.write((const char*)&green, sizeof(green));
out_image.write((const char*)&blue, sizeof(blue)); 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<int>(data[i + 0] *
// red_gain)); data[i + 1] = std::min(255, static_cast<int>(data[i + 1]
// * green_gain)); data[i + 2] = std::min(255, static_cast<int>(data[i +
// 2] * blue_gain));
// }
write_bmp("test.bmp", data, OUT_WIDTH, OUT_HEIGHT); write_bmp("test.bmp", data, OUT_WIDTH, OUT_HEIGHT);
delete[] data; delete[] data;