From 07d96b2e10353ac951b40f23e3dae2a80b8f1f85 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sun, 26 Jan 2020 20:34:26 +0800 Subject: [PATCH] use scad --- src/pixel/_impl/_px_gray_impl.scad | 29 +++++++++++++++++++++++++++++ src/pixel/px_gray.scad | 29 ++--------------------------- 2 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 src/pixel/_impl/_px_gray_impl.scad diff --git a/src/pixel/_impl/_px_gray_impl.scad b/src/pixel/_impl/_px_gray_impl.scad new file mode 100644 index 00000000..1da800fb --- /dev/null +++ b/src/pixel/_impl/_px_gray_impl.scad @@ -0,0 +1,29 @@ +function _px_gray_row(r_count, row_bits, width, height, center, invert, normalize) = + let( + half_w = width / 2, + half_h = height / 2, + offset_x = center ? 0 : half_w, + offset_y = center ? -half_h : 0, + level = invert ? 0 : 255, + nmal = normalize ? 255 : 1 + ) + [ + for(i = 0; i < width; i = i + 1) + if(row_bits[i] != level) + [ + [i - half_w + offset_x, r_count + offset_y], + invert ? row_bits[i] / nmal : (255 - row_bits[i]) / nmal + ] + ]; + +function _px_gray_impl(levels, center, invert, normalize) = + let( + width = len(levels[0]), + height = len(levels), + offset_i = height / 2 + ) + [ + for(i = height - 1; i > -1; i = i - 1) + let(row = _px_gray_row(height - i - 1, levels[i], width, height, center, invert, normalize)) + if(row != []) each row + ]; \ No newline at end of file diff --git a/src/pixel/px_gray.scad b/src/pixel/px_gray.scad index acb9b701..9f6d7f29 100644 --- a/src/pixel/px_gray.scad +++ b/src/pixel/px_gray.scad @@ -8,32 +8,7 @@ * **/ -function _px_gray_row(r_count, row_bits, width, height, center, invert, normalize) = - let( - half_w = width / 2, - half_h = height / 2, - offset_x = center ? 0 : half_w, - offset_y = center ? -half_h : 0, - level = invert ? 0 : 255, - nmal = normalize ? 255 : 1 - ) - [ - for(i = 0; i < width; i = i + 1) - if(row_bits[i] != level) - [ - [i - half_w + offset_x, r_count + offset_y], - invert ? row_bits[i] / nmal : (255 - row_bits[i]) / nmal - ] - ]; +use ; function px_gray(levels, center = false, invert = false, normalize = false) = - let( - width = len(levels[0]), - height = len(levels), - offset_i = height / 2 - ) - [ - for(i = height - 1; i > -1; i = i - 1) - let(row = _px_gray_row(height - i - 1, levels[i], width, height, center, invert, normalize)) - if(row != []) each row - ]; \ No newline at end of file + _px_gray_impl(levels, center, invert, normalize); \ No newline at end of file