Skip to content

Commit b29673f

Browse files
committed
Try floats
1 parent beb96ce commit b29673f

4 files changed

Lines changed: 31 additions & 24 deletions

File tree

include/algos.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void bgr2gray_opencv(const cv::Mat& src, cv::Mat& dst);
2424
void boxFilter_halide(uint16_t* src, uint16_t* dst, int height, int width);
2525
void boxFilter_opencv(const cv::Mat& src, cv::Mat& dst);
2626
void ascii_art_ref(const uint8_t* src, uint8_t* dst, int height, int width);
27-
void ascii_art_halide(uint8_t* src, uint8_t* dst, int input_height, int input_width);
27+
void ascii_art_halide(uint8_t* src, float* dst, int input_height, int input_width);
2828

2929
void julia_ref(uint8_t* dst, int height, int width);
3030
void halide_julia(uint8_t* dst, int height, int width);

perf/perf_ascii.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ PERF_TEST(ascii_art, halide) {
2828
const int height = 1080;
2929
const std::string grey_scale = "$@B%8&WM#*OAHKDPQWMRZO0QLCJUYXVJFT/|()1{}[]?-_+~<>i!lI;:,^`'. ";
3030

31-
Mat src(height, width, CV_8UC1), dst(height/ry, width/rx, CV_8U);
31+
Mat src(height, width, CV_8UC1), dst(height/ry, width/rx, CV_32F);
3232
randu(src, 0, 256);
3333
PERF_SAMPLE_BEGIN()
34-
ascii_art_halide(src.ptr<uint8_t>(), dst.ptr<uint8_t>(), src.rows, src.cols);
34+
ascii_art_halide(src.ptr<uint8_t>(), dst.ptr<float>(), src.rows, src.cols);
3535
PERF_SAMPLE_END()
3636

3737
SANITY_CHECK_NOTHING();

src/ASCII-ART.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ static const int norm_h = 3;
2222
static const int rx = 15;
2323
static const int ry = 19;
2424

25-
void ascii_art_halide(uint8_t* src, uint8_t* dst, int input_height, int input_width) {
25+
void ascii_art_halide(uint8_t* src, float* dst, int input_height, int input_width) {
2626
int output_width = input_width / rx;
2727
int output_height = input_height / ry;
28-
29-
28+
29+
3030
Buffer<uint8_t> input(src, {input_width, input_height});
31-
Buffer<uint8_t> output(dst, {input_width / rx, input_height / ry});
31+
Buffer<float> output(dst, {input_width / rx, input_height / ry});
3232
#ifdef __riscv
3333
ascii_art(input, output);
3434
#else
@@ -38,17 +38,24 @@ void ascii_art_halide(uint8_t* src, uint8_t* dst, int input_height, int input_wi
3838

3939
Var x("x"), y("y");
4040
RDom r(0, rx, 0, ry);
41-
Expr s = sum(cast<uint32_t>(input(x*rx + r.x, y*ry + r.y)));
42-
41+
42+
Func casted;
43+
casted(x, y) = cast<float>(input(x, y));
44+
45+
Expr s = sum(casted(x*rx + r.x, y*ry + r.y));
46+
4347
//s = Halide::clamp(s/(rx*ry),0,255);
44-
ascii(x, y) = cast<uint8_t>(s/(rx*ry));
45-
// ascii.realize(output);
48+
ascii(x, y) = s/(rx*ry);
49+
50+
casted.compute_root();
51+
ascii.vectorize(x, 4);
52+
4653
// Compile
4754
Target target;
4855
target.os = Target::OS::Linux;
4956
target.arch = Target::Arch::RISCV;
5057
target.bits = 64;
51-
//target.vector_bits = factor * sizeof(uint8_t) * 8;
58+
target.vector_bits = 128;
5259

5360
// Tested XuanTie C906 has 128-bit vector unit
5461
CV_Assert(target.vector_bits <= 128);
@@ -76,11 +83,11 @@ void ascii_art_ref(const uint8_t* src, uint8_t* dst, int input_height, int input
7683
float lum = 0;
7784
for(int j = 0; j < ry; ++j){
7885
for(int i = 0; i < rx; ++i){
79-
lum += src[ (y * ry + j) * input_width + x * rx +i];
80-
}
86+
lum += src[ (y * ry + j) * input_width + x * rx +i];
87+
}
8188
}
8289
dst[y * output_width + x] = static_cast<uint8_t>(lum/(rx*ry));
8390
lum = 0;
84-
}
91+
}
8592
}
8693
}

test/test_ascii.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ TEST(ascii_art, halide){
1010
const std::string grey_scale = "$@B%8&WM#*OAHKDPQWMRZO0QLCJUYXVJFT/|()1{}[]?-_+~<>i!lI;:,^`'. ";
1111

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

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

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

52-
imwrite("res_cat_ascii_halide.png", render);
53-
imwrite("src_cat_ascii_halide.png", src);
54-
ASSERT_EQ(true, true);
52+
// imwrite("res_cat_ascii_halide.png", render);
53+
// imwrite("src_cat_ascii_halide.png", src);
54+
// ASSERT_EQ(true, true);
5555
}

0 commit comments

Comments
 (0)