Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASCII-ART vect #31

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/algos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void bgr2gray_opencv(const cv::Mat& src, cv::Mat& dst);
void boxFilter_halide(uint16_t* src, uint16_t* dst, int height, int width);
void boxFilter_opencv(const cv::Mat& src, cv::Mat& dst);
void ascii_art_ref(const uint8_t* src, uint8_t* dst, int height, int width);
void ascii_art_halide(uint8_t* src, uint8_t* dst, int input_height, int input_width);
void ascii_art_halide(uint8_t* src, float* dst, int input_height, int input_width);

void julia_ref(uint8_t* dst, int height, int width);
void halide_julia(uint8_t* dst, int height, int width);
Expand Down
4 changes: 2 additions & 2 deletions perf/perf_ascii.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ PERF_TEST(ascii_art, halide) {
const int height = 1080;
const std::string grey_scale = "$@B%8&WM#*OAHKDPQWMRZO0QLCJUYXVJFT/|()1{}[]?-_+~<>i!lI;:,^`'. ";

Mat src(height, width, CV_8UC1), dst(height/ry, width/rx, CV_8U);
Mat src(height, width, CV_8UC1), dst(height/ry, width/rx, CV_32F);
randu(src, 0, 256);
PERF_SAMPLE_BEGIN()
ascii_art_halide(src.ptr<uint8_t>(), dst.ptr<uint8_t>(), src.rows, src.cols);
ascii_art_halide(src.ptr<uint8_t>(), dst.ptr<float>(), src.rows, src.cols);
PERF_SAMPLE_END()

SANITY_CHECK_NOTHING();
Expand Down
33 changes: 20 additions & 13 deletions src/ASCII-ART.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,40 @@ static const int norm_h = 3;
static const int rx = 15;
static const int ry = 19;

void ascii_art_halide(uint8_t* src, uint8_t* dst, int input_height, int input_width) {
void ascii_art_halide(uint8_t* src, float* dst, int input_height, int input_width) {
int output_width = input_width / rx;
int output_height = input_height / ry;


Buffer<uint8_t> input(src, {input_width, input_height});
Buffer<uint8_t> output(dst, {input_width / rx, input_height / ry});
Buffer<float> output(dst, {input_width / rx, input_height / ry});
#ifdef __riscv
ascii_art(input, output);
#else
static Func ascii("ascii_art");
if (!ascii.defined()) {
// Func padded = BoundaryConditions::constant_exterior(input, 0);

Var x("x"), y("y");
Var x("x"), y("y"), yo("yo"), yi("yi");
RDom r(0, rx, 0, ry);
Expr s = sum(cast<uint32_t>(input(x*rx + r.x, y*ry + r.y)));


Func casted;
casted(x, y) = cast<uint32_t>(input(x, y));

Expr s = sum(casted(x*rx + r.x, y*ry + r.y));
ascii.bound(x, 0, 10).bound(y, 0, 10).split(y, yo, yi, 5).parallel(yo);
//s = Halide::clamp(s/(rx*ry),0,255);
ascii(x, y) = cast<uint8_t>(s/(rx*ry));
// ascii.realize(output);
ascii(x, y) = s/(rx*ry);

casted.compute_root();
ascii.vectorize(x, 4);

// Compile
Target target;
target.os = Target::OS::Linux;
target.arch = Target::Arch::RISCV;
target.bits = 64;
//target.vector_bits = factor * sizeof(uint8_t) * 8;
target.vector_bits = 128;

// Tested XuanTie C906 has 128-bit vector unit
CV_Assert(target.vector_bits <= 128);
Expand Down Expand Up @@ -76,11 +83,11 @@ void ascii_art_ref(const uint8_t* src, uint8_t* dst, int input_height, int input
float lum = 0;
for(int j = 0; j < ry; ++j){
for(int i = 0; i < rx; ++i){
lum += src[ (y * ry + j) * input_width + x * rx +i];
}
lum += src[ (y * ry + j) * input_width + x * rx +i];
}
}
dst[y * output_width + x] = static_cast<uint8_t>(lum/(rx*ry));
lum = 0;
}
}
}
}
18 changes: 9 additions & 9 deletions test/test_ascii.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ TEST(ascii_art, halide){
const std::string grey_scale = "$@B%8&WM#*OAHKDPQWMRZO0QLCJUYXVJFT/|()1{}[]?-_+~<>i!lI;:,^`'. ";

cv::Mat src = imread("cat.jpeg", cv::IMREAD_GRAYSCALE);
cv::Mat dst(src.rows/ry, src.cols/rx, CV_8U),
cv::Mat dst(src.rows/ry, src.cols/rx, CV_32F),
render(src.rows, src.cols, CV_8U, cv::Scalar(255));

ascii_art_halide(src.ptr<uint8_t>(), dst.ptr<uint8_t>(), src.rows, src.cols);
char *s;
s = (char*)dst.ptr<uint8_t>();
ascii_art_halide(src.ptr<uint8_t>(), dst.ptr<float>(), src.rows, src.cols);
std::vector<std::pair<float, char>> lums={};
std::vector<char> symbols(256);
float lum_min = 255;
Expand All @@ -42,14 +40,16 @@ TEST(ascii_art, halide){
for(int i = 0; i < dst.rows; i++)
for(int j = 0; j < dst.cols; j++){

uint8_t lum = dst.at<uint8_t>(i, j);
int index = (static_cast<float>(lum)/255)*(lums.size()-1);
float lum = dst.at<float>(i, j);
int index = (min(lum, 255.0f)/255.0f)*(lums.size()-1);
std::cout << lum << std::endl;
CV_Assert(index < lums.size());
cv::Mat roi = render.colRange(j*rx, (j+1)*rx).rowRange(i*ry, (i+1)*ry);
cv::putText(roi, std::string(1, grey_scale[index]), cv::Point(1, ry-1),
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0), 1, cv::LINE_AA);
}

imwrite("res_cat_ascii_halide.png", render);
imwrite("src_cat_ascii_halide.png", src);
ASSERT_EQ(true, true);
// imwrite("res_cat_ascii_halide.png", render);
// imwrite("src_cat_ascii_halide.png", src);
// ASSERT_EQ(true, true);
}
Loading