fix demosaic not work correctly and polish project manage

This commit is contained in:
2024-10-25 23:12:57 +08:00
parent 1ab1467569
commit a8fa609228
17 changed files with 493 additions and 140 deletions

View File

@@ -21,7 +21,179 @@
// Include model header, generated from Verilating "isp.v"
#include "Visp.h"
#include "tb_isp.hpp"
// Write Pictures
#include "bitmap_image.hpp"
SC_MODULE(TB_ISP) {
sc_core::sc_in_clk clk;
sc_core::sc_in<bool> rst;
sc_core::sc_in<bool> in_ready; // next module ready to receive data
sc_core::sc_out<bool> out_valid; // next module data valid signal
sc_core::sc_out<uint32_t> out_data[3]; // next module receive data
sc_core::sc_in<bool> in_valid; // this module receive data valid signal
sc_core::sc_out<bool> out_ready; // this module ready to receive data
sc_core::sc_in<uint32_t> in_data; // this module receive data
const uint16_t IN_WIDTH;
const uint16_t IN_HEIGHT;
const uint32_t IN_SIZE;
const uint16_t OUT_WIDTH;
const uint16_t OUT_HEIGHT;
const uint32_t OUT_SIZE;
const uint32_t FLAMES;
const std::string OUT_DIR;
bool is_done; // when receive all data
std::vector<uint16_t> image; // the data of image
std::vector<uint32_t> process_image; // after isp process, the data of image
SC_CTOR(TB_ISP, const uint16_t in_width, const uint16_t in_height,
const uint16_t out_width, const uint16_t out_height,
const uint32_t cnt_flame, const std::string& out_dir)
: IN_WIDTH(in_width), IN_HEIGHT(in_height), IN_SIZE(in_width * in_height),
OUT_WIDTH(out_width), OUT_HEIGHT(out_height),
OUT_SIZE(out_width * out_height), FLAMES(cnt_flame),
OUT_DIR(out_dir),
process_image(std::vector<uint32_t>(out_width * out_height, 0)) {
SC_CTHREAD(sendData, clk.pos()); // when clk posedge, exec sendData
reset_signal_is(rst, true); // set rst signal
SC_CTHREAD(readData, clk.pos());
reset_signal_is(rst, true); // set rst signal
}
void sendData(void) {
// init var
uint16_t pos_x = 0, pos_y = 0, cnt_flame = 0;
bool is_finish = false; // when send all data
// reset
out_valid = false;
for (auto &data : out_data)
data = 0;
while (true) {
if (in_ready && !is_finish) {
// valid and send data
out_valid = true;
out_data[0] = image[(pos_y + 0) * IN_WIDTH + pos_x];
out_data[1] = image[(pos_y + 1) * IN_WIDTH + pos_x];
out_data[2] = image[(pos_y + 2) * IN_WIDTH + pos_x];
// print data
std::printf("x=%4d, y=%4d, data=0x%04x\t", pos_x, pos_y,
image[(pos_y + 0) * IN_WIDTH + pos_x]);
std::printf("x=%4d, y=%4d, data=0x%04x\t", pos_x, pos_y,
image[(pos_y + 1) * IN_WIDTH + pos_x]);
std::printf("x=%4d, y=%4d, data=0x%04x\n", pos_x, pos_y,
image[(pos_y + 2) * IN_WIDTH + pos_x]);
pos_x++;
// calculate position and recognize when to finish
if (pos_x >= IN_WIDTH) {
pos_x = 0;
pos_y++;
}
if (pos_y >= IN_HEIGHT - 1) {
pos_y = 0;
cnt_flame++;
}
if (cnt_flame >= FLAMES) {
is_finish = true;
}
} else {
out_valid = false;
}
// wait for next clk
wait();
}
}
void readData(void) {
// init local var
uint16_t pos_x = 0, pos_y = 0, cnt_flame = 0;
uint32_t last_data = 0, cnt = 0;
bool is_finish = false;
// reset
out_ready = false;
is_done = false;
while (true) {
if (!is_finish) {
out_ready = true;
// when data valid, write it down
if (in_valid) {
process_image[pos_y * OUT_WIDTH + pos_x] = in_data;
// calculate position
pos_x++;
if (pos_x >= OUT_WIDTH) {
pos_x = 0;
pos_y++;
}
if (pos_y >= OUT_HEIGHT) {
pos_y = 0;
saveData(
("output_img_" + std::to_string(cnt_flame) + ".bmp").c_str());
cnt_flame++;
}
if (cnt_flame >= FLAMES) {
is_finish = true;
}
}
} else {
out_ready = false;
}
// when no data send, give finish signal
if (is_finish && (last_data == in_data)) {
cnt++;
if (cnt >= 100000L) { // when receive many times the same data
is_done = true;
std::printf("Finish Reading data; pos_x = %d, pos_y = %d\n", pos_x,
pos_y);
}
} else {
cnt = 0;
}
last_data = in_data;
// wait for next clk
wait();
}
}
bool saveData(const char *name) {
bool ret = true;
// Check Image Size
if (process_image.size() > OUT_SIZE) {
std::cout << "Process Image Over Size!!!\n"
<< "Image Size:" << process_image.size() << "\n";
return false;
}
// Write BMP image
bitmap_image bmp(OUT_WIDTH, OUT_HEIGHT);
if (!bmp) {
std::cout << "Output Image Open Failed!!!\n";
return false;
}
for (int y = 0; y < OUT_HEIGHT; y++)
for (int x = 0; x < OUT_WIDTH; x++)
bmp.set_pixel(x, y,
(process_image[y * OUT_WIDTH + x] & 0x00ff0000) >> 16,
(process_image[y * OUT_WIDTH + x] & 0x0000ff00) >> 8,
(process_image[y * OUT_WIDTH + x] & 0x000000ff) >> 0);
bmp.save_image(std::string(OUT_DIR) + name);
return ret;
}
};
// Image Parameters
static const uint16_t IN_WIDTH = 1936;

View File

@@ -7,8 +7,12 @@
#include <iostream>
// SystemC global header
#include "spdlog/common.h"
#include "spdlog/logger.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "sysc/communication/sc_signal.h"
#include "sysc/kernel/sc_module.h"
#include <memory>
#include <systemc>
// Include common routines
@@ -24,6 +28,10 @@
// Include testbench written by systemc
#include "tb_isp.hpp"
// Spdlog library
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
// Image Parameters
static const uint16_t IN_WIDTH = 1936;
static const uint16_t IN_HEIGHT = 1088;
@@ -46,7 +54,7 @@ struct color_gain {
double red;
double green;
double blue;
} color_gain{1.1, 0.7, 1.3}, white_gain;
} color_gain{2, 1, 1}, white_gain;
static const double gamma_value = 2.2;
static const double sat_inc = 0.5;
@@ -56,17 +64,34 @@ using namespace sc_core;
using namespace sc_dt;
int sc_main(int argc, char *argv[]) {
std::printf("Enter into sc_main\n");
std::cout << "Enter into sc_main\n";
try {
auto logger = std::make_shared<spdlog::logger>(
"sc_main", spdlog::sinks_init_list(
{std::make_shared<spdlog::sinks::stdout_color_sink_mt>(),
std::make_shared<spdlog::sinks::basic_file_sink_mt>(
fmt::format("{:s}/ISP.txt", OUTPUT_DIR), true)}));
// When Debug, set Debug level
#ifdef DEBUG
logger->set_level(spdlog::level::debug);
#endif // DEBUG
spdlog::set_default_logger(logger);
spdlog::info("Succeefully init spdlog");
} catch (const spdlog::spdlog_ex &ex) {
std::cout << "[ERROR] Failed to Init Spdlog!!!\n";
return -1;
}
// Open Image
std::ifstream image;
image.open(INPUT_IMG, std::ios::in | std::ios::binary);
// Check image whether is open
if (!image.is_open()) {
std::printf("Open Image Failed!!!\n");
spdlog::error("Open Image Failed!!!");
exit(0);
} else {
std::printf("Open Image Successfully!!!\n");
spdlog::info("Open Image Successfully!!!\n");
}
// Read and Transform Image
@@ -84,7 +109,7 @@ int sc_main(int argc, char *argv[]) {
// Close and delete image
image.close();
delete[] buf;
std::printf("Finish Reading Image\n");
spdlog::info("Finish Reading Image");
// This is a more complicated example, please also see the simpler
// examples/make_hello_c.
@@ -207,7 +232,8 @@ int sc_main(int argc, char *argv[]) {
VerilatedVcdSc *tfp = nullptr;
const char *flag = Verilated::commandArgsPlusMatch("trace");
if (flag && 0 == std::strcmp(flag, "+trace")) {
std::cout << "Enabling waves into logs/vlt_dump.vcd...\n";
spdlog::info("Enabling waves into logs/vlt_dump.vcd...");
tfp = new VerilatedVcdSc;
isp.trace(tfp, 99); // Trace 99 levels of hierarchy
Verilated::mkdir("logs");
@@ -215,7 +241,7 @@ int sc_main(int argc, char *argv[]) {
}
// Simulate until $finish
std::cout << "Ready to simulate!\n";
spdlog::info("Ready to simulate!");
while (!Verilated::gotFinish()) {
// Flush the wave files each cycle so we can immediately see the output
// Don't do this in "real" programs, do it in an abort() handler instead

View File

@@ -2,17 +2,32 @@
#define __TB_ISP_H__
#include <cstdint>
#include <iostream>
#include <memory>
#include <sys/types.h>
#include <systemc>
// Write Pictures
#include "bitmap_image.hpp"
#include "spdlog/common.h"
#include "spdlog/logger.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
SC_MODULE(TB_ISP) {
private:
spdlog::logger log;
spdlog::logger img_log;
public:
sc_core::sc_in_clk clk;
sc_core::sc_in<bool> rst;
sc_core::sc_in<bool> in_ready; // next module ready to receive data
sc_core::sc_out<bool> out_valid; // next module data valid signal
sc_core::sc_in<bool> in_ready; // next module ready to receive data
sc_core::sc_out<bool> out_valid; // next module data valid signal
sc_core::sc_out<uint32_t> out_data; // next module receive data
sc_core::sc_in<bool> in_valid; // this module receive data valid signal
@@ -34,17 +49,34 @@ SC_MODULE(TB_ISP) {
SC_CTOR(TB_ISP, const uint16_t in_width, const uint16_t in_height,
const uint16_t out_width, const uint16_t out_height,
const uint32_t cnt_flame, const std::string& out_dir)
const uint32_t cnt_flame, const std::string &out_dir)
// Init class varibles
: IN_WIDTH(in_width), IN_HEIGHT(in_height), IN_SIZE(in_width * in_height),
OUT_WIDTH(out_width), OUT_HEIGHT(out_height),
OUT_SIZE(out_width * out_height), FLAMES(cnt_flame),
OUT_DIR(out_dir),
process_image(std::vector<uint32_t>(out_width * out_height, 0)) {
OUT_SIZE(out_width * out_height), FLAMES(cnt_flame), OUT_DIR(out_dir),
process_image(std::vector<uint32_t>(out_width * out_height, 0)),
// Global logger with file and stdout
log("TB_ISP",
spdlog::sinks_init_list(
{std::make_shared<spdlog::sinks::stdout_color_sink_mt>(),
std::make_shared<spdlog::sinks::basic_file_sink_mt>(
out_dir + "TB_ISP.txt", true)})),
// Image logger for debug image
img_log("IMG_LOG", std::make_shared<spdlog::sinks::basic_file_sink_mt>(
out_dir + "IMG.txt", true)) {
// When Debug, set Debug level
#ifdef DEBUG
log.set_level(spdlog::level::debug);
img_log.set_level(spdlog::level::debug);
#endif // DEBUG
SC_CTHREAD(sendData, clk.pos()); // when clk posedge, exec sendData
reset_signal_is(rst, true); // set rst signal
SC_CTHREAD(readData, clk.pos());
reset_signal_is(rst, true); // set rst signal
log.info("Created TB_ISP Modules");
}
void sendData(void) {
@@ -62,8 +94,8 @@ SC_MODULE(TB_ISP) {
out_data = image[(pos_y + 0) * IN_WIDTH + pos_x];
// print data
std::printf("x=%4d, y=%4d, data=0x%04x\n", pos_x, pos_y,
image[pos_y * IN_WIDTH + pos_x]);
log.debug("x={:4d}, y={:4d}, data=0x{:04x}", pos_x, pos_y,
image[pos_y * IN_WIDTH + pos_x]);
pos_x++;
// calculate position and recognize when to finish
@@ -75,7 +107,7 @@ SC_MODULE(TB_ISP) {
pos_y = 0;
cnt_flame++;
}
if (cnt_flame >= FLAMES) {
if (cnt_flame >= FLAMES + 1) {
is_finish = true;
}
} else {
@@ -130,8 +162,8 @@ SC_MODULE(TB_ISP) {
cnt++;
if (cnt >= 100000L) { // when receive many times the same data
is_done = true;
std::printf("Finish Reading data; pos_x = %d, pos_y = %d\n", pos_x,
pos_y);
img_log.info("Finish Reading data; pos_x = {:d}, pos_y = {:d}", pos_x,
pos_y);
}
} else {
cnt = 0;
@@ -148,23 +180,34 @@ SC_MODULE(TB_ISP) {
// Check Image Size
if (process_image.size() > OUT_SIZE) {
std::cout << "Process Image Over Size!!!\n"
<< "Image Size:" << process_image.size() << "\n";
log.error("Image Over Size!!!\nImage Size:{:d}", process_image.size());
return false;
}
// Write BMP image
bitmap_image bmp(OUT_WIDTH, OUT_HEIGHT);
if (!bmp) {
std::cout << "Output Image Open Failed!!!\n";
log.error("Output Image Open Failed!!!");
return false;
}
for (int y = 0; y < OUT_HEIGHT; y++)
for (int x = 0; x < OUT_WIDTH; x++)
bmp.set_pixel(x, y,
(process_image[y * OUT_WIDTH + x] & 0x00ff0000) >> 16,
(process_image[y * OUT_WIDTH + x] & 0x0000ff00) >> 8,
(process_image[y * OUT_WIDTH + x] & 0x000000ff) >> 0);
img_log.info("Image Receive: {:d} Pixels, {:d} Bytes", process_image.size(),
process_image.size() * 3);
unsigned char red = 0, green = 0, blue = 0;
for (int y = 0; y < OUT_HEIGHT; y++) {
for (int x = 0; x < OUT_WIDTH; x++) {
red = (process_image[y * OUT_WIDTH + x] & 0x00ff0000) >> 16;
green = (process_image[y * OUT_WIDTH + x] & 0x0000ff00) >> 8;
blue = (process_image[y * OUT_WIDTH + x] & 0x000000ff) >> 0;
img_log.debug(
"x={:4d}, y={:4d}, red=0x{:02x}, green=0x{:02x}, blue=0x{:02x}", x,
y, red, green, blue);
bmp.set_pixel(x, y, red, green, blue);
}
}
bmp.save_image(std::string(OUT_DIR) + name);
return ret;
}