From b8063f2018a619512d534185ab7f4e681713d8c0 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Wed, 8 Oct 2025 12:23:40 +0200 Subject: [PATCH] round pixel to next step --- src/kds_s2000w_pixel_converter.c | 12 ++++++++++++ tests/kds_s2000w_pixel_converter_tests.c | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/kds_s2000w_pixel_converter.c b/src/kds_s2000w_pixel_converter.c index 7b5717e..96e8d25 100644 --- a/src/kds_s2000w_pixel_converter.c +++ b/src/kds_s2000w_pixel_converter.c @@ -1,9 +1,21 @@ #include "kds_s2000w_pixel_converter.h" #define TENTH 10 +#define ROUND_STEP 30 + +uint32_t _kds_s2000w_pixel_converter_round_pixel(uint32_t pixel) +{ + uint32_t diff = pixel % 30; + diff = ROUND_STEP - diff; + if (diff < 30) + pixel = pixel + diff; + + return pixel; +} uint32_t kds_s2000w_pixel_converter_pixel_to_tenth_inch(uint32_t dpi, uint32_t pixel) { + pixel = _kds_s2000w_pixel_converter_round_pixel(pixel); return pixel * TENTH / dpi; } diff --git a/tests/kds_s2000w_pixel_converter_tests.c b/tests/kds_s2000w_pixel_converter_tests.c index 49c34e0..5f61d5b 100644 --- a/tests/kds_s2000w_pixel_converter_tests.c +++ b/tests/kds_s2000w_pixel_converter_tests.c @@ -15,6 +15,16 @@ void kds_s2000w_max_tl_x_pixel_to_tenth_inch_test() assert_int_equal(tenth_inch, 75); } +void kds_s2000w_pixel_odd_to_tenth_inch_test() +{ + uint32_t dpi = 300; + uint32_t pixel = 2235; + + uint32_t tenth_inch = kds_s2000w_pixel_converter_pixel_to_tenth_inch(dpi, pixel); + + assert_int_equal(tenth_inch, 75); +} + void kds_s2000w_max_tl_x_tenth_inch_to_pixel_test() { uint32_t dpi = 300; @@ -29,6 +39,7 @@ int main() { const struct CMUnitTest tests[] = { cmocka_unit_test(kds_s2000w_max_tl_x_pixel_to_tenth_inch_test), + cmocka_unit_test(kds_s2000w_pixel_odd_to_tenth_inch_test), cmocka_unit_test(kds_s2000w_max_tl_x_tenth_inch_to_pixel_test) }; -- 2.47.3