switch lsp to clangd and reconstruct project

This commit is contained in:
2024-09-15 22:04:48 +08:00
parent 61b91e0688
commit 7e12105a3d
47 changed files with 1931 additions and 2859 deletions

140
src/pic_process/demosaic.cpp Executable file
View File

@@ -0,0 +1,140 @@
#include <fstream>
#include <iostream>
#include <vector>
#include "transform/bmp.hpp"
enum BayerPattern { GRBG, RGGB, BGGR, GBRG };
const int IN_WIDTH = 1920;
const int IN_HEIGHT = 1080;
const int IN_SIZE = IN_WIDTH * IN_HEIGHT;
const int OUT_WIDTH = 1280;
const int OUT_HEIGHT = 720;
const int OUT_SIZE = OUT_WIDTH * OUT_HEIGHT;
const BayerPattern RAW_TYPE = RGGB;
const int COLOR_DEPTH = 10;
// const float red_gain = 1.2f; // Adjust these values as necessary
// const float green_gain = 0.83f;
// const float blue_gain = 0.95f;
int main() {
std::ifstream in_image;
in_image.open("./test.RAW", std::ios::in | std::ios::binary);
auto image = std::vector<std::vector<uint16_t>>(
IN_HEIGHT, std::vector<uint16_t>(IN_WIDTH, 0));
for (int y = 0; y < IN_HEIGHT; y++)
for (int x = 0; x < IN_WIDTH; x++) {
uint8_t buf[2] = {0};
in_image.read((char*)buf, sizeof(buf));
image[y][x] = buf[0] + ((uint16_t)buf[1] << 8);
}
BayerPattern raw_type = RAW_TYPE;
uint8_t* data =
new uint8_t[OUT_WIDTH * OUT_HEIGHT * 3]; // RGB24格式像素数据
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;
uint16_t red = 0, green = 0, blue = 0;
uint16_t data_cache[9] = {0};
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
data_cache[i * 3 + j] = image[y + i][x + j];
}
// data case 0 case 1 case 2 case 3
// 0 1 2 G R G R G R B G B G B G
// 3 4 5 B G B G B G G R G R G R
// 6 7 8 G R G R G R B G B G B G
switch (raw_type) {
case 0: // Missing B, R on G
red = (data_cache[1] + data_cache[7]) / 2;
blue = (data_cache[3] + data_cache[5]) / 2;
green = data_cache[4];
break;
case 1: // Missing G, R on B
green = (data_cache[1] + data_cache[3] + data_cache[5] +
data_cache[7]) /
4;
red = (data_cache[0] + data_cache[2] + data_cache[6] +
data_cache[8]) /
4;
blue = data_cache[4];
break;
case 2: // Missing G, B on R
green = (data_cache[1] + data_cache[3] + data_cache[5] +
data_cache[7]) /
4;
blue = (data_cache[0] + data_cache[2] + data_cache[6] +
data_cache[8]) /
4;
red = data_cache[4];
break;
case 3: // Missing B, R on G
blue = (data_cache[1] + data_cache[7]) / 2;
red = (data_cache[3] + data_cache[5]) / 2;
green = data_cache[4];
break;
}
printf(
"x=%4d, y=%4d, red=0x%03x, green=0x%03x, blue=0x%03x, "
"raw_type=%d\n",
x, y, red, green, blue, raw_type);
switch (raw_type) {
case 0:
raw_type = RGGB;
break;
case 1:
raw_type = GRBG;
break;
case 2:
raw_type = GBRG;
break;
case 3:
raw_type = BGGR;
break;
}
data[index + 0] = red >> (COLOR_DEPTH - 8); // R
data[index + 1] = green >> (COLOR_DEPTH - 8); // G
data[index + 2] = blue >> (COLOR_DEPTH - 8); // B
}
if (y % 2) {
raw_type = RAW_TYPE;
} else {
switch (RAW_TYPE) {
case 0:
raw_type = BGGR;
break;
case 1:
raw_type = GBRG;
break;
case 2:
raw_type = GRBG;
break;
case 3:
raw_type = RGGB;
break;
}
}
}
// 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);
delete[] data;
return 0;
}

163
src/pic_process/demosaic2.cpp Executable file
View File

@@ -0,0 +1,163 @@
#include <fstream>
#include <iostream>
#include <vector>
#include "transform/bmp.hpp"
enum BayerPattern { GRBG, RGGB, BGGR, GBRG };
const int IN_WIDTH = 1936;
const int IN_HEIGHT = 1088;
const int IN_SIZE = IN_WIDTH * IN_HEIGHT;
const int OUT_WIDTH = 1280;
const int OUT_HEIGHT = 720;
const int OUT_SIZE = OUT_WIDTH * OUT_HEIGHT;
const BayerPattern RAW_TYPE = GBRG;
const int COLOR_DEPTH = 12;
// const float red_gain = 1.2f; // Adjust these values as necessary
// const float green_gain = 0.5f;
// const float blue_gain = 0.95f;
int main() {
std::ifstream in_image;
in_image.open("./test.bin", std::ios::in | std::ios::binary);
auto image = std::vector<std::vector<uint16_t>>(
IN_HEIGHT, std::vector<uint16_t>(IN_WIDTH, 0));
for (int y = 0; y < IN_HEIGHT; y++)
for (int x = 0; x < IN_WIDTH; x++) {
uint8_t buf[2] = {0};
in_image.read((char*)buf, sizeof(buf));
image[y][x] = buf[0] + ((uint16_t)buf[1] << 8);
}
BayerPattern raw_type = RAW_TYPE;
uint8_t* data =
new uint8_t[OUT_WIDTH * OUT_HEIGHT * 3]; // RGB24格式像素数据
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;
uint16_t red = 0, green = 0, blue = 0;
uint16_t cache[25] = {0};
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) {
cache[i * 5 + j] = image[y + i][x + j];
}
// data case GRBG case RGGB case BGGR case GBRG
// 00 01 02 03 04 G R G R G R G R G R B G B G B G B G B G
// 05 06 07 08 09 B G B G B G B G B G G R G R G R G R G R
// 10 11 12 13 14 G R G R G R G R G R B G B G B G B G B G
// 15 16 17 18 19 B G B G B G B G B G G R G R G R G R G R
// 20 21 22 23 24 G R G R G R G R G R B G B G B G B G B G
switch (raw_type) {
case GRBG:
green = cache[12];
blue =
(cache[7] + cache[17]) / 2 + cache[12] -
((cache[2] + cache[6] + cache[8] + cache[12]) / 4 +
(cache[22] + cache[16] + cache[18] + cache[12]) / 4) /
2;
red = (cache[11] + cache[13]) / 2 + cache[12] -
((cache[10] + cache[6] + cache[16] + cache[12]) / 4 +
(cache[14] + cache[8] + cache[18] + cache[12]) / 4) /
2;
break;
case RGGB:
red = cache[12];
green = (cache[7] + cache[11] + cache[13] + cache[17]) / 4;
blue =
(cache[6] + cache[8] + cache[16] + cache[18]) / 4 +
(cache[7] + cache[11] + cache[13] + cache[17]) / 4 -
((cache[1] + cache[5] + cache[7] + cache[11]) / 4 +
(cache[3] + cache[7] + cache[9] + cache[13]) / 4 +
(cache[11] + cache[15] + cache[17] + cache[18]) / 4 +
(cache[13] + cache[17] + cache[19] + cache[23]) / 4) /
4;
break;
case BGGR:
blue = cache[12];
green = (cache[7] + cache[11] + cache[13] + cache[17]) / 4;
red =
(cache[6] + cache[8] + cache[16] + cache[18]) / 4 +
(cache[7] + cache[11] + cache[13] + cache[17]) / 4 -
((cache[1] + cache[5] + cache[7] + cache[11]) / 4 +
(cache[3] + cache[7] + cache[9] + cache[13]) / 4 +
(cache[11] + cache[15] + cache[17] + cache[18]) / 4 +
(cache[13] + cache[17] + cache[19] + cache[23]) / 4) /
4;
break;
case GBRG:
green = cache[12];
red =
(cache[7] + cache[17]) / 2 + cache[12] -
((cache[2] + cache[6] + cache[8] + cache[12]) / 4 +
(cache[22] + cache[16] + cache[18] + cache[12]) / 4) /
2;
blue =
(cache[11] + cache[13]) / 2 + cache[12] -
((cache[10] + cache[6] + cache[16] + cache[12]) / 4 +
(cache[14] + cache[8] + cache[18] + cache[12]) / 4) /
2;
break;
}
printf(
"x=%4d, y=%4d, red=0x%03x, green=0x%03x, blue=0x%03x, "
"raw_type=%d\n",
x, y, red, green, blue, raw_type);
switch (raw_type) {
case 0:
raw_type = RGGB;
break;
case 1:
raw_type = GRBG;
break;
case 2:
raw_type = GBRG;
break;
case 3:
raw_type = BGGR;
break;
}
data[index + 0] = red >> (COLOR_DEPTH - 8); // R
data[index + 1] = green >> (COLOR_DEPTH - 8); // G
data[index + 2] = blue >> (COLOR_DEPTH - 8); // B
}
if (y % 2) {
raw_type = RAW_TYPE;
} else {
switch (RAW_TYPE) {
case 0:
raw_type = BGGR;
break;
case 1:
raw_type = GBRG;
break;
case 2:
raw_type = GRBG;
break;
case 3:
raw_type = RGGB;
break;
}
}
}
// 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("test1.bmp", data, OUT_WIDTH, OUT_HEIGHT);
delete[] data;
return 0;
}

23
src/sc_main.cpp Normal file
View File

@@ -0,0 +1,23 @@
// For std::unique_ptr
#include <memory>
// SystemC global header
#include <systemc>
// Include common routines
#include <sys/stat.h> // mkdir
#include <verilated.h>
#include <verilated_vcd_sc.h>
// Include model header, generated from Verilating "isp.v"
#include "obj_dir/Visp.h"
#include "tb_isp.hpp"
// Read/Write Files
#include <fstream>
#include <iostream>
int sc_main(int argc, const char** argv) {
return 0;
}

471
src/sc_main.cpp.bak Executable file
View File

@@ -0,0 +1,471 @@
// For std::unique_ptr
#include <memory>
// SystemC global header
#include <systemc>
// Include common routines
#include <sys/stat.h> // mkdir
#include <verilated.h>
#include <verilated_vcd_sc.h>
// Include model header, generated from Verilating "isp.v"
#include "Visp.h"
// Handle file
#include <fstream>
#include <iostream>
// math
#include <cmath>
#include "bmp.hpp"
static const uint16_t IN_WIDTH = 1936;
static const uint16_t IN_HEIGHT = 1088;
static const uint32_t IN_SIZE = (IN_WIDTH * IN_HEIGHT);
static const uint16_t OUT_WIDTH = 1920;
static const uint16_t OUT_HEIGHT = 1080;
static const uint32_t OUT_SIZE = (OUT_WIDTH * OUT_HEIGHT);
static const uint32_t FLAMES = 2;
// color gain for correcting color
struct color_gain {
double red;
double green;
double blue;
} color_gain{ 1.1, 0.7, 1.3 }, white_gain;
static const double gamma_value = 2.2;
static const double saturation_inc = 0.5;
static const double contrast = 1.2;
// static const double white_radio = 0.1;
using namespace sc_core;
using namespace sc_dt;
bool picProcess(uint32_t *image, uint16_t number);
SC_MODULE(TB_ISP) {
sc_in_clk clk;
sc_in<bool> reset;
sc_in<bool> in_ready;
sc_out<bool> out_valid;
sc_out<uint32_t> out_data[3];
sc_in<bool> im_clk;
sc_in<bool> im_en;
sc_out<bool> out_ready;
sc_in<uint32_t> im_data;
sc_out<bool> is_done;
std::unique_ptr<uint16_t[]> image = std::make_unique<uint16_t[]>(IN_SIZE);
std::unique_ptr<uint32_t[]> out = std::make_unique<uint32_t[]>(OUT_SIZE);
SC_CTOR(TB_ISP) {
SC_CTHREAD(send_Data, clk.pos());
reset_signal_is(reset, true);
SC_CTHREAD(read_Data, im_clk.pos());
}
void send_Data(void) {
uint16_t pos_x = 0, pos_y = 0, cnt_flame = 0;
bool is_finish = false;
while (true) {
if (in_ready.read() && !is_finish) {
out_valid.write(1);
printf("x=%4d, y=%4d, data=0x%04x\t", pos_x, pos_y,
image[(pos_y + 0) * IN_WIDTH + pos_x]);
printf("x=%4d, y=%4d, data=0x%04x\t", pos_x, pos_y,
image[(pos_y + 1) * IN_WIDTH + pos_x]);
printf("x=%4d, y=%4d, data=0x%04x\n", pos_x, pos_y,
image[(pos_y + 2) * IN_WIDTH + pos_x]);
out_data[0].write(image[(pos_y + 0) * IN_WIDTH + pos_x]);
out_data[1].write(image[(pos_y + 1) * IN_WIDTH + pos_x]);
out_data[2].write(image[(pos_y + 2) * IN_WIDTH + pos_x]);
pos_x++;
if (pos_x >= IN_WIDTH) {
pos_x = 0;
pos_y++;
}
if (pos_y >= IN_HEIGHT - 2) {
pos_y = 0;
cnt_flame++;
}
if (cnt_flame >= FLAMES) {
is_finish = true;
}
}
else {
out_valid.write(0);
}
wait();
}
}
void read_Data(void) {
is_done.write(0);
uint16_t pos_x = 0, pos_y = 0, cnt_flame = 0;
uint32_t last_data = 0, cnt = 0;
bool is_finish = false;
while (true) {
// when not finish, read data
if (!is_finish) {
out_ready.write(true);
if (im_en.read()) {
out[pos_y * OUT_WIDTH + pos_x] = im_data.read();
pos_x++;
if (pos_x >= IN_WIDTH) {
pos_x = 0;
pos_y++;
}
if (pos_y >= IN_HEIGHT - 2) {
pos_y = 0;
cnt_flame++;
}
}
}
else {
out_ready.write(false);
}
// when data didn't change some time, it end
if (last_data == im_data.read() && is_finish) {
cnt++;
if (cnt >= 100000L) {
is_done.write(1);
printf("x=%d, y=%d\n", pos_x, pos_y);
}
}
else {
cnt = 0;
}
last_data = im_data.read();
wait();
}
}
};
bool picProcess(uint32_t *image, uint16_t number) {
uint8_t *data =
new uint8_t[OUT_WIDTH * OUT_HEIGHT * 3]; // RGB24格式像素数据
// software algorthms analyze
uint32_t red_total = 0, green_total = 0, blue_total = 0;
uint8_t red_max = 0, green_max = 0, blue_max = 0;
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 = (image[y * OUT_WIDTH + x] & 0x00ff0000) >> 16;
uint8_t green = (image[y * OUT_WIDTH + x] & 0x0000ff00) >> 8;
uint8_t blue = (image[y * OUT_WIDTH + x] & 0x000000ff);
// Adjust gamma line
// 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);
// Calculate white balance data
// red_max = std::max(red_max, red);
// green_max = std::max(green_max, green);
// blue_max = std::max(blue_max, blue);
// red_total += red;
// green_total += green;
// blue_total += blue;
// 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));
// }
// }
// Contrast enhancement
// red = static_cast<uchar>(contrast * (red - 128) + 128);
// green = static_cast<uchar>(contrast * (green - 128) + 128);
// blue = static_cast<uchar>(contrast * (blue - 128) + 128);
// save data
data[index + 0] = red; // R
data[index + 1] = green; // G
data[index + 2] = blue; // B
}
}
// Adjust White Balance : Grey World Color Correction
// double K = static_cast<double>(red_total + green_total + blue_total) /
// (3 * OUT_SIZE);
// white_gain.red = static_cast<double>(K * OUT_SIZE) / red_total;
// white_gain.green = static_cast<double>(K * OUT_SIZE) / green_total;
// white_gain.blue = static_cast<double>(K * OUT_SIZE) / blue_total;
// printf("Gain: red = %f, green = %f, blue = %f", white_gain.red,
// white_gain.green, white_gain.blue);
// 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;
// data[index + 0] =
// static_cast<uint8_t>(white_gain.red * data[index + 0]);
// data[index + 1] =
// static_cast<uint8_t>(white_gain.green * data[index + 1]);
// data[index + 2] =
// static_cast<uint8_t>(white_gain.blue * data[index + 2]);
// }
// }
// save to bmp
std::cout << "Ready to save raw RGB image" << std::endl;
char file_name[64] = { 0 };
snprintf(file_name, sizeof(file_name), "pic_%d.bmp", number);
write_bmp(file_name, data, OUT_WIDTH, OUT_HEIGHT);
delete[] data;
return true;
}
int sc_main(int argc, char *argv[]) {
std::cout << "Get into sc_main" << std::endl;
// Open image
std::ifstream in_image;
in_image.open("./transform/test.bin", std::ios::in | std::ios::binary);
if (!in_image.is_open()) {
std::cout << "Open image fail" << std::endl;
exit(0);
}
else {
std::cout << "Ready to sim" << std::endl;
}
// Read image
auto buf = std::make_unique<uint8_t[]>(2 * IN_SIZE);
in_image.read((char *)buf.get(), IN_SIZE * 2);
in_image.close();
// Reshape data
auto image = std::make_unique<uint16_t[]>(IN_SIZE);
uint32_t i = 0;
for (int y = 0; y < IN_HEIGHT; y++) {
for (int x = 0; x < IN_WIDTH; x++) {
image[y * IN_WIDTH + x] =
(uint16_t)buf[i] + ((uint16_t)buf[i + 1] << 8);
i += 2;
}
}
std::cout << "Finish Reading data" << std::endl;
// This is a more complicated example, please also see the simpler
// examples/make_hello_c.
// Create logs/ directory in case we have traces to put under it
Verilated::mkdir("logs");
// Set debug level, 0 is off, 9 is highest presently used
// May be overridden by commandArgs argument parsing
Verilated::debug(0);
// Randomization reset policy
// May be overridden by commandArgs argument parsing
Verilated::randReset(2);
// Before any evaluation, need to know to calculate those signals only used
// for tracing
Verilated::traceEverOn(true);
// Pass arguments so Verilated code can see them, e.g. $value$plusargs
// This needs to be called before you create any model
Verilated::commandArgs(argc, argv);
// General logfile
std::ios::sync_with_stdio();
// Define clocks
sc_clock clk{ "clk", 10, SC_NS, 0.5, 3, SC_NS, true };
// Define interconnect
sc_signal<bool> reset;
sc_signal<bool> in_valid;
sc_signal<bool> in_ready;
sc_signal<uint32_t> in_data[3];
sc_signal<bool> out_clk;
sc_signal<bool> out_valid;
sc_signal<bool> out_ready;
sc_signal<bool> out_receive;
sc_signal<uint32_t> out_data;
sc_signal<bool> blender_enable;
sc_signal<uint32_t> gain_red;
sc_signal<uint32_t> gain_green;
sc_signal<uint32_t> gain_blue;
sc_signal<bool> gamma_enable;
sc_signal<uint32_t> gamma_inverse;
sc_signal<uint32_t> gamma_table[256];
sc_signal<uint32_t> white_gain[3];
sc_signal<uint32_t> flame_rate;
sc_signal<bool> white_enable;
sc_signal<bool> saturation_enable;
sc_signal<uint32_t> saturation_increase;
sc_signal<bool> flag_done;
// Construct the Verilated model, from inside Visp.h
// Using unique_ptr is similar to "Visp* isp = new Visp" then deleting at the end
const std::unique_ptr<Visp> isp{ new Visp{"isp"} };
// Attach Visp's signals to this upper model
isp->clk(clk);
isp->reset(reset);
isp->in_valid(in_valid);
isp->in_ready(in_ready);
isp->in_data[0](in_data[0]);
isp->in_data[1](in_data[1]);
isp->in_data[2](in_data[2]);
isp->out_valid(out_valid);
isp->out_ready(out_ready);
isp->out_data(out_data);
isp->gain_red(gain_red);
isp->gain_green(gain_green);
isp->gain_blue(gain_blue);
isp->blender_enable(blender_enable);
// isp->gamma_enable(gamma_enable);
// isp->gamma_inverse(gamma_inverse);
// isp->white_enable(white_enable);
// isp->flame_rate(flame_rate);
// isp->white_gain[0](white_gain[0]);
// isp->white_gain[1](white_gain[1]);
// isp->white_gain[2](white_gain[2]);
// isp->saturation_enable(saturation_enable);
// isp->saturation_inc(saturation_increase);
// blender_enable = true; // enable color correction
// gain_red = static_cast<uint32_t>(color_gain.red * std::pow(2, 8));
// gain_green = static_cast<uint32_t>(color_gain.green * std::pow(2, 8));
// gain_blue = static_cast<uint32_t>(color_gain.blue * std::pow(2, 8));
// gamma_enable = true;
// gamma_inverse = static_cast<uint32_t>((1.0 / gamma_value) * std::pow(2, 8));
// for (int i = 0; i < 256; i++) {
// // calculate gamma table
// isp->gamma_table[i](gamma_table[i]);
// gamma_table[i] = static_cast<uint32_t>(255 * pow(i / 255.0, 1.0 / gamma_value));
// }
// white_enable = true;
// flame_rate = 0;
// white_gain[0] = 255;
// white_gain[1] = 255;
// white_gain[2] = 255;
// 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);
tb_isp.reset(reset);
tb_isp.in_ready(out_ready);
tb_isp.out_valid(in_valid);
tb_isp.out_ready(in_ready);
// tb_isp.out_receceive(in_receive);
tb_isp.out_data[0](in_data[0]);
tb_isp.out_data[1](in_data[1]);
tb_isp.out_data[2](in_data[2]);
tb_isp.im_clk(out_clk);
tb_isp.im_en(out_valid);
tb_isp.im_data(out_data);
tb_isp.is_done(flag_done);
tb_isp.image = move(image);
// You must do one evaluation before enabling waves, in order to allow
// SystemC to interconnect everything for testing.
sc_start(SC_ZERO_TIME);
// If verilator was invoked with --trace argument,
// and if at run time passed the +trace argument, turn on tracing
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";
tfp = new VerilatedVcdSc;
isp->trace(tfp, 99); // Trace 99 levels of hierarchy
Verilated::mkdir("logs");
tfp->open("logs/vlt_dump.vcd");
}
// Simulate until $finish
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
if (tfp) tfp->flush();
// Apply inputs
if (sc_time_stamp() < sc_time(10, SC_NS)) {
reset.write(1); // Assert reset
}
else {
reset.write(0); // Deassert reset
}
if (flag_done.read()) break;
// Simulate 1ns
sc_start(1, SC_NS);
}
// Final model cleanup
isp->final();
// Close trace if opened
if (tfp) {
tfp->close();
tfp = nullptr;
}
// Return good completion status
return 0;
}

0
src/tb_isp.cpp Normal file
View File

24
src/tb_isp.hpp Normal file
View File

@@ -0,0 +1,24 @@
#ifndef __TB_ISP_HPP__
#define __TB_ISP_HPP__
#include "sysc/communication/sc_signal_ports.h"
#include <systemc>
namespace testbench {
using namespace sc_core;
using namespace sc_dt;
SC_MODULE(TB_ISP) {
sc_in_clk clk;
sc_in<bool> rst;
sc_in<bool> in_ready;
sc_out<bool> out_valid;
sc_out<sc_uint<8>> out_data[3];
sc_in<uint32_t> in_data;
};
} // namespace testbench
#endif

46
src/transform/bmp.cpp Executable file
View File

@@ -0,0 +1,46 @@
#include "bmp.hpp"
#include <fstream>
#include <iostream>
// 将RGB24格式像素数据封装为BMP图像
bool write_bmp(const char* filename, uint8_t* data, int32_t width,
int32_t height) {
BMPFileHeader file_header = {0};
BMPInfoHeader info_header = {0};
std::ofstream ofs(filename, std::ios::binary);
if (!ofs) {
std::cerr << "Failed to create file: " << filename << std::endl;
return false;
}
// BMP文件头
file_header.type = 0x4D42; // BM
file_header.size =
sizeof(BMPFileHeader) + sizeof(BMPInfoHeader) + width * height * 3;
file_header.offset = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader);
ofs.write(reinterpret_cast<char*>(&file_header), sizeof(file_header));
// BMP位图信息头
info_header.size = sizeof(BMPInfoHeader);
info_header.width = width;
info_header.height = height;
info_header.planes = 1;
info_header.bit_count = 24;
info_header.size_image = width * height * 3;
ofs.write(reinterpret_cast<char*>(&info_header), sizeof(info_header));
// 像素数据
int32_t row_size = (((width + 1) * 3) / 4) * 4; // 行字节数必须为4的倍数
uint8_t* row_data = new uint8_t[row_size];
for (int32_t y = height - 1; y >= 0; --y) { // BMP图像的行是从下往上存储的
for (int32_t x = 0; x < width; ++x) {
row_data[x * 3 + 2] = data[(y * width + x) * 3 + 0]; // B
row_data[x * 3 + 1] = data[(y * width + x) * 3 + 1]; // G
row_data[x * 3 + 0] = data[(y * width + x) * 3 + 2]; // R
}
ofs.write(reinterpret_cast<char*>(row_data), row_size);
}
delete[] row_data;
ofs.close();
return true;
}

35
src/transform/bmp.hpp Executable file
View File

@@ -0,0 +1,35 @@
#ifndef __BMP_H__
#define __BMP_H__
#include <stdint.h>
#pragma pack(push, 1) // 1字节对齐
// BMP文件头结构体
struct BMPFileHeader {
uint16_t type; // 文件类型,必须为"BM"
uint32_t size; // 文件大小,单位为字节
uint16_t reserved1; // 保留字段必须为0
uint16_t reserved2; // 保留字段必须为0
uint32_t offset; // 像素数据起始位置,单位为字节
};
// BMP位图信息头结构体
struct BMPInfoHeader {
uint32_t size; // 信息头大小必须为40
int32_t width; // 图像宽度,单位为像素
int32_t height; // 图像高度,单位为像素
uint16_t planes; // 颜色平面数必须为1
uint16_t bit_count; // 每个像素的位数必须为24
uint32_t compression; // 压缩方式必须为0
uint32_t size_image; // 像素数据大小,单位为字节
int32_t x_pels_per_meter; // X方向像素数/米
int32_t y_pels_per_meter; // Y方向像素数/米
uint32_t clr_used; // 使用的颜色数必须为0
uint32_t clr_important; // 重要的颜色数必须为0
};
#pragma pack(pop)
bool write_bmp(const char* filename, uint8_t* data, int32_t width, int32_t height);
#endif

BIN
src/transform/im.tif Executable file

Binary file not shown.

23
src/transform/raw_cut.py Executable file
View File

@@ -0,0 +1,23 @@
import imageio
import numpy as np
cut_width = 1936
cut_height = 1088
if __name__ == '__main__':
# txt = open('./test.dat', 'w')
binfile = open('./test.bin', "wb")
image = imageio.imread_v2('./im.tif')
print(image.shape)
cut = image[0:cut_height, 0:cut_width]
print(cut.shape)
cut = np.array(cut, dtype=np.int16)
for data in list(cut.flatten()):
# txt.write('%02x\n%02x\n' % (data & 0x00ff, (data & 0xff00) >> 4))
binfile.write(data)
# txt.close()
binfile.close()
# imageio.imsave('./test.tif', cut)