finish SaturationCorrection and pass simulation

This commit is contained in:
SikongJueluo
2024-06-30 17:47:56 +08:00
parent 7833c3a7b5
commit e4fffd21bb
4 changed files with 237 additions and 65 deletions

View File

@@ -36,7 +36,7 @@ struct color_gain {
} color_gain{1.1, 0.7, 1.3};
static const double gamma_value = 2.2;
static const double saturation_inc = -0.5;
static const double saturation_inc = 0.5;
using namespace sc_core;
using namespace sc_dt;
@@ -217,6 +217,9 @@ int sc_main(int argc, char* argv[]) {
sc_signal<uint32_t> gamma_inverse;
sc_signal<uint32_t> gamma_table[256];
sc_signal<bool> saturation_enable;
sc_signal<uint32_t> saturation_increase;
sc_signal<bool> flag_done;
// Construct the Verilated model, from inside Visp.h
@@ -246,6 +249,9 @@ int sc_main(int argc, char* argv[]) {
isp->gamma_enable(gamma_enable);
// isp->gamma_inverse(gamma_inverse);
isp->saturation_enable(saturation_enable);
isp->saturation_inc(saturation_increase);
blender_enable = true; // enable color correction
gain_red = (uint32_t)(color_gain.red * std::pow(2, 8));
gain_green = (uint32_t)(color_gain.green * std::pow(2, 8));
@@ -259,6 +265,11 @@ int sc_main(int argc, char* argv[]) {
gamma_table[i] = (uint32_t)(255 * pow(i / 255.0, 1.0 / gamma_value));
}
saturation_enable = true;
saturation_increase =
(int32_t)((saturation_inc >= 0) ? (saturation_inc * std::pow(2, 8))
: (saturation_inc * std::pow(2, 8)));
// Construct testbench module
TB_ISP tb_isp("tb_isp");
tb_isp.clk(clk);
@@ -321,17 +332,10 @@ int sc_main(int argc, char* argv[]) {
tfp = nullptr;
}
// Save output image
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],
// sizeof(tb_isp.out[0]));
// out_image.close();
// save to image
uint8_t* data =
new uint8_t[OUT_WIDTH * OUT_HEIGHT * 3]; // RGB24格式像素数据
// software algorthms analyze
for (int32_t y = 0; y < OUT_HEIGHT; ++y) {
for (int32_t x = 0; x < OUT_WIDTH; ++x) {
int32_t index = (y * OUT_WIDTH + x) * 3;
@@ -348,35 +352,61 @@ int sc_main(int argc, char* argv[]) {
// Adjust white balance
// Adjust vibrance
uint8_t max = std::max({red, green, blue});
uint8_t min = std::min({red, green, blue});
double delta = (max - min) / 255.0;
double value = (max + min) / 255.0;
if (delta != 0) {
double L = value / 2.0;
// double S = (L <= 0.5) ? delta / value : delta / (2 - value);
double S = delta / max;
double alpha = 0.0;
if (saturation_inc >= 0) {
if ((saturation_inc + S) >= 1)
alpha = S;
else
alpha = 1 - saturation_inc;
alpha = 1 / alpha - 1;
red = static_cast<uchar>(red + (red - L * 255) * alpha);
green =
static_cast<uchar>(green + (green - L * 255) * alpha);
blue = static_cast<uchar>(blue + (blue - L * 255) * alpha);
} else {
alpha = saturation_inc;
red = static_cast<uchar>(L * 255 +
(red - L * 255) * (1 + alpha));
green = static_cast<uchar>(L * 255 +
(green - L * 255) * (1 + alpha));
blue = static_cast<uchar>(L * 255 +
(blue - L * 255) * (1 + alpha));
}
}
// uint8_t max = std::max({red, green, blue});
// uint8_t min = std::min({red, green, blue});
// double delta = (max - min) / 255.0;
// double value = (max + min) / 255.0;
// if (delta != 0) {
// double L = value / 2.0;
// // double S = (L <= 0.5) ? delta / value : delta / (2 -
// value); double S = delta / max; double alpha = 0.0; if
// (saturation_inc >= 0) {
// if ((saturation_inc + S) >= 1)
// alpha = S;
// else
// alpha = 1 - saturation_inc;
// alpha = 1 / alpha - 1;
// red = static_cast<uchar>(red + (red - L * 255) * alpha);
// green =
// static_cast<uchar>(green + (green - L * 255) *
// alpha);
// blue = static_cast<uchar>(blue + (blue - L * 255) *
// alpha);
// } else {
// alpha = saturation_inc;
// red = static_cast<uchar>(L * 255 +
// (red - L * 255) * (1 + alpha));
// green = static_cast<uchar>(L * 255 +
// (green - L * 255) * (1 +
// alpha));
// blue = static_cast<uchar>(L * 255 +
// (blue - L * 255) * (1 +
// alpha));
// }
// }
data[index + 0] = red; // R
data[index + 1] = green; // G
data[index + 2] = blue; // B
}
}
// Save output image
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],
// sizeof(tb_isp.out[0]));
// out_image.close();
// save to image
for (int32_t y = 0; y < OUT_HEIGHT; ++y) {
for (int32_t x = 0; x < OUT_WIDTH; ++x) {
int32_t index = (y * OUT_WIDTH + x) * 3;
uint8_t red = data[index + 0];
uint8_t green = data[index + 1];
uint8_t blue = data[index + 2];
out_image.write((const char*)&red, sizeof(red));
out_image.write((const char*)&green, sizeof(green));
@@ -384,10 +414,6 @@ int sc_main(int argc, char* argv[]) {
printf("x=%4d, y=%4d, red=0x%02x, green=0x%02x, blue=0x%02x\n", x,
y, red, green, blue);
data[index + 0] = red; // R
data[index + 1] = green; // G
data[index + 2] = blue; // B
}
}