From c2c65f29c9a2fa6695141532f104f00d1d7bfd17 Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Thu, 12 Sep 2019 22:39:20 +0300 Subject: [PATCH 1/3] genesys: Simplify genesys_fill_segmented_buffer() --- backend/genesys.cc | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/backend/genesys.cc b/backend/genesys.cc index 3974b9126..7e0673311 100644 --- a/backend/genesys.cc +++ b/backend/genesys.cc @@ -3457,7 +3457,7 @@ static void genesys_fill_segmented_buffer(Genesys_Device* dev, uint8_t* work_buf { DBG_HELPER(dbg); size_t count; - int depth, i, k; + int depth, k; depth = dev->settings.depth; if (dev->settings.scan_mode == ScanColorMode::LINEART && dev->settings.dynamic_lineart==SANE_FALSE) @@ -3469,6 +3469,13 @@ static void genesys_fill_segmented_buffer(Genesys_Device* dev, uint8_t* work_buf accurate_line_read(dev, dev->oe_buffer); } + auto read_desegmented = [&](size_t curr_byte, size_t curr_segment) -> uint8_t + { + curr_byte += dev->session.output_segment_start_offset; + curr_byte += dev->session.conseq_pixel_dist_bytes * dev->segment_order[curr_segment]; + return dev->oe_buffer.get_read_pos()[curr_byte]; + }; + /* copy size bytes of data, copying from a subwindow of each line * when last line of buffer is exhausted, read another one */ count = 0; @@ -3480,11 +3487,11 @@ static void genesys_fill_segmented_buffer(Genesys_Device* dev, uint8_t* work_buf work_buffer_dst[count+n] = 0; } /* interleaving is at bit level */ - for (i=0;i<8;i++) { + for (unsigned i = 0; i < 8; i++) { k = count + (i * dev->session.segment_count) / 8; for (unsigned n = 0; n < dev->session.segment_count; n++) { work_buffer_dst[k] = work_buffer_dst[k] << 1; - if ((dev->oe_buffer.get_read_pos()[dev->deseg_curr_byte + dev->session.output_segment_start_offset + dev->session.conseq_pixel_dist_bytes * dev->segment_order[n]])&(128>>i)) { + if (read_desegmented(dev->deseg_curr_byte, n) & (0x80 >> i)) { work_buffer_dst[k] |= 1; } } @@ -3498,7 +3505,7 @@ static void genesys_fill_segmented_buffer(Genesys_Device* dev, uint8_t* work_buf if (depth==8) { while (dev->deseg_curr_byte < dev->session.output_segment_pixel_group_count && count < size) { for (unsigned n = 0; n < dev->session.segment_count; n++) { - work_buffer_dst[count+n] = dev->oe_buffer.get_read_pos()[dev->deseg_curr_byte + dev->session.output_segment_start_offset + dev->session.conseq_pixel_dist_bytes *dev->segment_order[n]]; + work_buffer_dst[count + n] = read_desegmented(dev->deseg_curr_byte, n); } /* update counter and pointer */ count += dev->session.segment_count; @@ -3508,8 +3515,8 @@ static void genesys_fill_segmented_buffer(Genesys_Device* dev, uint8_t* work_buf if (depth==16) { while (dev->deseg_curr_byte < dev->session.output_segment_pixel_group_count && count < size) { for (unsigned n = 0; n < dev->session.segment_count; n++) { - work_buffer_dst[count+n*2] = dev->oe_buffer.get_read_pos()[dev->deseg_curr_byte + dev->session.output_segment_start_offset + dev->session.conseq_pixel_dist_bytes * dev->segment_order[n]]; - work_buffer_dst[count+n*2+1] = dev->oe_buffer.get_read_pos()[dev->deseg_curr_byte + dev->session.output_segment_start_offset + dev->session.conseq_pixel_dist_bytes * dev->segment_order[n] + 1]; + work_buffer_dst[count + n * 2] = read_desegmented(dev->deseg_curr_byte, n); + work_buffer_dst[count + n * 2 + 1] = read_desegmented(dev->deseg_curr_byte + 1, n); } /* update counter and pointer */ count += dev->session.segment_count * 2; From 502478a40d66030895abcc351250fc040bfe41a9 Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Thu, 12 Sep 2019 22:39:21 +0300 Subject: [PATCH 2/3] genesys: Reindent genesys_fill_segmented_buffer() --- backend/genesys.cc | 113 ++++++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/backend/genesys.cc b/backend/genesys.cc index 7e0673311..27beef6ad 100644 --- a/backend/genesys.cc +++ b/backend/genesys.cc @@ -3456,15 +3456,16 @@ static void genesys_fill_segmented_buffer(Genesys_Device* dev, uint8_t* work_buf size_t size) { DBG_HELPER(dbg); - size_t count; - int depth, k; - depth = dev->settings.depth; - if (dev->settings.scan_mode == ScanColorMode::LINEART && dev->settings.dynamic_lineart==SANE_FALSE) - depth = 1; + unsigned depth = dev->settings.depth; + if (dev->settings.scan_mode == ScanColorMode::LINEART && + dev->settings.dynamic_lineart == false) + { + depth = 1; + } - /* fill buffer if needed */ - if (dev->oe_buffer.avail() == 0) + // fill buffer if needed + if (dev->oe_buffer.avail() == 0) { accurate_line_read(dev, dev->oe_buffer); } @@ -3476,62 +3477,68 @@ static void genesys_fill_segmented_buffer(Genesys_Device* dev, uint8_t* work_buf return dev->oe_buffer.get_read_pos()[curr_byte]; }; - /* copy size bytes of data, copying from a subwindow of each line - * when last line of buffer is exhausted, read another one */ - count = 0; - while (count < size) - { - if (depth==1) { - while (dev->deseg_curr_byte < dev->session.output_segment_pixel_group_count && count < size) { + // copy size bytes of data, copying from a subwindow of each line + // when last line of buffer is exhausted, read another one + size_t count = 0; + while (count < size) { + if (depth == 1) { + while (dev->deseg_curr_byte < dev->session.output_segment_pixel_group_count && + count < size) + { + for (unsigned n = 0; n < dev->session.segment_count; n++) { + work_buffer_dst[count + n] = 0; + } + /* interleaving is at bit level */ + for (unsigned i = 0; i < 8; i++) { + unsigned k = count + (i * dev->session.segment_count) / 8; for (unsigned n = 0; n < dev->session.segment_count; n++) { - work_buffer_dst[count+n] = 0; - } - /* interleaving is at bit level */ - for (unsigned i = 0; i < 8; i++) { - k = count + (i * dev->session.segment_count) / 8; - for (unsigned n = 0; n < dev->session.segment_count; n++) { - work_buffer_dst[k] = work_buffer_dst[k] << 1; - if (read_desegmented(dev->deseg_curr_byte, n) & (0x80 >> i)) { - work_buffer_dst[k] |= 1; - } + work_buffer_dst[k] = work_buffer_dst[k] << 1; + if (read_desegmented(dev->deseg_curr_byte, n) & (0x80 >> i)) { + work_buffer_dst[k] |= 1; } } + } - /* update counter and pointer */ - count += dev->session.segment_count; - dev->deseg_curr_byte++; - } - } - if (depth==8) { - while (dev->deseg_curr_byte < dev->session.output_segment_pixel_group_count && count < size) { - for (unsigned n = 0; n < dev->session.segment_count; n++) { - work_buffer_dst[count + n] = read_desegmented(dev->deseg_curr_byte, n); - } - /* update counter and pointer */ - count += dev->session.segment_count; - dev->deseg_curr_byte++; - } - } - if (depth==16) { - while (dev->deseg_curr_byte < dev->session.output_segment_pixel_group_count && count < size) { - for (unsigned n = 0; n < dev->session.segment_count; n++) { - work_buffer_dst[count + n * 2] = read_desegmented(dev->deseg_curr_byte, n); - work_buffer_dst[count + n * 2 + 1] = read_desegmented(dev->deseg_curr_byte + 1, n); - } - /* update counter and pointer */ - count += dev->session.segment_count * 2; - dev->deseg_curr_byte += 2; - } + /* update counter and pointer */ + count += dev->session.segment_count; + dev->deseg_curr_byte++; } + } - /* go to next line if needed */ + if (depth == 8) { + while (dev->deseg_curr_byte < dev->session.output_segment_pixel_group_count && + count < size) + { + for (unsigned n = 0; n < dev->session.segment_count; n++) { + work_buffer_dst[count + n] = read_desegmented(dev->deseg_curr_byte, n); + } + /* update counter and pointer */ + count += dev->session.segment_count; + dev->deseg_curr_byte++; + } + } + + if (depth == 16) { + while (dev->deseg_curr_byte < dev->session.output_segment_pixel_group_count && + count < size) { + for (unsigned n = 0; n < dev->session.segment_count; n++) { + work_buffer_dst[count + n * 2] = read_desegmented(dev->deseg_curr_byte, n); + work_buffer_dst[count + n * 2 + 1] = read_desegmented(dev->deseg_curr_byte + 1, n); + } + /* update counter and pointer */ + count += dev->session.segment_count * 2; + dev->deseg_curr_byte += 2; + } + } + + // go to next line if needed if (dev->deseg_curr_byte == dev->session.output_segment_pixel_group_count) { - dev->oe_buffer.set_pos(dev->oe_buffer.pos() + dev->session.output_line_bytes_raw); + dev->oe_buffer.set_pos(dev->oe_buffer.pos() + dev->session.output_line_bytes_raw); dev->deseg_curr_byte = 0; } - /* read a new buffer if needed */ - if (dev->oe_buffer.pos() >= dev->oe_buffer.avail()) + // read a new buffer if needed + if (dev->oe_buffer.pos() >= dev->oe_buffer.avail()) { accurate_line_read(dev, dev->oe_buffer); } From 9b7ab9372786c63a90c01817f25ad17dbb12808e Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Thu, 12 Sep 2019 22:39:22 +0300 Subject: [PATCH 3/3] genesys: Add unit test for genesys_fill_segmented_buffer() --- backend/genesys.cc | 3 +- backend/genesys_device.h | 2 +- backend/genesys_low.h | 2 + testsuite/backend/genesys/Makefile.am | 3 +- testsuite/backend/genesys/tests.cc | 1 + testsuite/backend/genesys/tests.h | 1 + testsuite/backend/genesys/tests_sensor.cc | 95 +++++++++++++++++++++++ 7 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 testsuite/backend/genesys/tests_sensor.cc diff --git a/backend/genesys.cc b/backend/genesys.cc index 27beef6ad..f9323b37f 100644 --- a/backend/genesys.cc +++ b/backend/genesys.cc @@ -3452,8 +3452,7 @@ static void genesys_fill_line_interp_buffer(Genesys_Device* dev, uint8_t* work_b * back to back and must be interleaved to get usable by the other stages * of the backend */ -static void genesys_fill_segmented_buffer(Genesys_Device* dev, uint8_t* work_buffer_dst, - size_t size) +void genesys_fill_segmented_buffer(Genesys_Device* dev, uint8_t* work_buffer_dst, size_t size) { DBG_HELPER(dbg); diff --git a/backend/genesys_device.h b/backend/genesys_device.h index 21ee64aec..8f5a6b268 100644 --- a/backend/genesys_device.h +++ b/backend/genesys_device.h @@ -292,7 +292,7 @@ struct Genesys_Device // total bytes read to be sent to frontend size_t total_bytes_to_read = 0; - // The current byte during desegmentation process + // The current byte in line during desegmentation process size_t deseg_curr_byte = 0; // contains the real used values diff --git a/backend/genesys_low.h b/backend/genesys_low.h index 4233b6aeb..997439cf8 100644 --- a/backend/genesys_low.h +++ b/backend/genesys_low.h @@ -636,6 +636,8 @@ extern void sanei_genesys_generate_gamma_buffer(Genesys_Device* dev, void compute_session(Genesys_Device* dev, ScanSession& s, const Genesys_Sensor& sensor); +void genesys_fill_segmented_buffer(Genesys_Device* dev, uint8_t* work_buffer_dst, size_t size); + const SensorProfile& get_sensor_profile(AsicType asic_type, const Genesys_Sensor& sensor, unsigned dpi, unsigned ccd_size_divisor); diff --git a/testsuite/backend/genesys/Makefile.am b/testsuite/backend/genesys/Makefile.am index d3f82f416..e96a17dff 100644 --- a/testsuite/backend/genesys/Makefile.am +++ b/testsuite/backend/genesys/Makefile.am @@ -20,6 +20,7 @@ AM_CPPFLAGS += -I. -I$(srcdir) -I$(top_builddir)/include -I$(top_srcdir)/include -DBACKEND_NAME=genesys genesys_tests_SOURCES = tests.cc tests.h minigtest.cc minigtest.h \ - tests_calibration.cc + tests_calibration.cc \ + tests_sensor.cc genesys_tests_LDADD = $(TEST_LDADD) diff --git a/testsuite/backend/genesys/tests.cc b/testsuite/backend/genesys/tests.cc index c5be026e5..0e1c17228 100644 --- a/testsuite/backend/genesys/tests.cc +++ b/testsuite/backend/genesys/tests.cc @@ -28,5 +28,6 @@ int main() { test_calibration_parsing(); + test_sensor(); return finish_tests(); } diff --git a/testsuite/backend/genesys/tests.h b/testsuite/backend/genesys/tests.h index f4e4d2e4e..9a40dcc97 100644 --- a/testsuite/backend/genesys/tests.h +++ b/testsuite/backend/genesys/tests.h @@ -24,5 +24,6 @@ #define SANE_TESTSUITE_BACKEND_GENESYS_GENESYS_UNIT_TEST_H void test_calibration_parsing(); +void test_sensor(); #endif diff --git a/testsuite/backend/genesys/tests_sensor.cc b/testsuite/backend/genesys/tests_sensor.cc new file mode 100644 index 000000000..49f5c2e2f --- /dev/null +++ b/testsuite/backend/genesys/tests_sensor.cc @@ -0,0 +1,95 @@ +/* sane - Scanner Access Now Easy. + + Copyright (C) 2019 Povilas Kanapickas + + This file is part of the SANE package. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, + MA 02111-1307, USA. +*/ + +#define DEBUG_DECLARE_ONLY + +#include "tests.h" +#include "minigtest.h" + +#include "../../../backend/genesys_low.h" + +#include + +template +std::ostream& operator<<(std::ostream& str, const std::vector& arg) +{ + str << "{ "; + for (const auto& el : arg) { + str << (unsigned) el << ", "; + } + str << "}\n"; + return str; +} + +void test_fill_segmented_buffer_depth8() +{ + Genesys_Device dev; + dev.settings.scan_mode = ScanColorMode::COLOR_SINGLE_PASS; + dev.settings.dynamic_lineart = false; + dev.settings.depth = 8; + + // 2 lines, 4 segments, 5 bytes each + dev.deseg_curr_byte = 0; + dev.session.output_segment_start_offset = 0; + dev.session.output_segment_pixel_group_count = 5; + dev.session.segment_count = 4; + dev.session.conseq_pixel_dist_bytes = 5; + dev.session.output_line_bytes_raw = 20; + dev.segment_order = { 0, 2, 1, 3 }; + + dev.oe_buffer.alloc(1024); + uint8_t* data = dev.oe_buffer.get_write_pos(40); + std::array input_data = { { + 1, 5, 9, 13, 17, + 3, 7, 11, 15, 19, + 2, 6, 10, 14, 18, + 4, 8, 12, 16, 20, + 21, 25, 29, 33, 37, + 23, 27, 31, 35, 39, + 22, 26, 30, 34, 38, + 24, 28, 32, 36, 40, + 0 // one extra byte so that we don't attempt to refill the buffer + } }; + std::copy(input_data.begin(), input_data.end(), data); + dev.oe_buffer.produce(41); + + std::vector out_data; + out_data.resize(40, 0); + + genesys_fill_segmented_buffer(&dev, out_data.data(), 16); + ASSERT_EQ(dev.deseg_curr_byte, 4u); + genesys_fill_segmented_buffer(&dev, out_data.data() + 16, 4); + ASSERT_EQ(dev.deseg_curr_byte, 0u); + genesys_fill_segmented_buffer(&dev, out_data.data() + 20, 20); + ASSERT_EQ(dev.deseg_curr_byte, 0u); + + std::vector expected; + expected.resize(40, 0); + std::iota(expected.begin(), expected.end(), 1); // will fill with 1, 2, 3, ..., 40 + + ASSERT_EQ(out_data, expected); +} + +void test_sensor() +{ + test_fill_segmented_buffer_depth8(); +}