diff --git a/common/RawToConvertedGeometry.h b/common/RawToConvertedGeometry.h index a3a179f1f..21a997ca9 100644 --- a/common/RawToConvertedGeometry.h +++ b/common/RawToConvertedGeometry.h @@ -8,6 +8,18 @@ #include "Coord.h" #include "DiffractionExperiment.h" +// An assembled image back to raw module frames, the multipixels summed (SqueezeMultipixelsToModule) +template +void SqueezeToRawGeometry(const DiffractionExperiment &experiment, Td *destination, const Ts *source, + Ts min, Ts max, Td dest_min, Td dest_max) { + for (size_t module_number = 0; module_number < experiment.GetModulesNum(); module_number++) + SqueezeMultipixelsToModule(destination + module_number * RAW_MODULE_SIZE, source, + experiment.GetModuleSlowDirectionStep(module_number), min, max, + dest_min, dest_max, + experiment.GetModuleFastDirectionStep(module_number), + experiment.GetPixel0OfModuleConv(module_number)); +} + // Input coord is column + 1024 * (line + 512 * module) // Copies result over multipixel - can be used for example for mask calculation template diff --git a/common/RawToConvertedGeometryCore.h b/common/RawToConvertedGeometryCore.h index c7692f715..ca11dd33d 100644 --- a/common/RawToConvertedGeometryCore.h +++ b/common/RawToConvertedGeometryCore.h @@ -139,6 +139,56 @@ void TransferModuleAdjustMultipixels(Td *destination, const Ts *source, int64_t } } +// Inverse of TransferModuleAdjustMultipixels: each raw pixel of the module is the sum of the pixels its value was +// split over (2 for a multipixel, 4 at a chip corner), as the split conserves the count. A raw pixel is an error +// pixel where any of them is at or below min, saturated where any is at or above max or the sum reaches dest_max. +template +void SqueezeMultipixelsToModule(Td *destination, const Ts *source, int64_t slow_dir_step, Ts min, Ts max, + Td dest_min, Td dest_max, int64_t fast_dir_step = 1, int64_t offset_0 = 0) { + for (int64_t line = 0; line < RAW_MODULE_LINES; line++) { + int64_t y[2] = {line + ((line > 255) ? 2 : 0), -1}; + if (line == 255) + y[1] = 256; + else if (line == 256) + y[1] = 257; + + for (int64_t col = 0; col < RAW_MODULE_COLS; col++) { + const int64_t chip = col / 256, i = col % 256; + int64_t x[2] = {i + chip * 258, -1}; + if ((i == 255) && (chip < 3)) + x[1] = x[0] + 1; + else if ((i == 0) && (chip > 0)) + x[1] = x[0] - 1; + + int64_t sum = 0; + bool error = false, saturated = false; + for (int64_t yy: y) { + for (int64_t xx: x) { + if ((yy < 0) || (xx < 0)) + continue; + const Ts v = source[offset_0 + yy * slow_dir_step + xx * fast_dir_step]; + if (v <= min) + error = true; + else if (v >= max) + saturated = true; + else + sum += v; + } + } + + Td &out = destination[line * RAW_MODULE_COLS + col]; + if (error) + out = dest_min; + else if (saturated || (sum >= dest_max)) + out = dest_max; + else if (sum <= dest_min) + out = dest_min + 1; + else + out = static_cast(sum); + } + } +} + template void LineConvtToRaw(T *destination, const T *source, size_t fast_direction_step) { for (int chip = 0; chip < 4; chip++) { diff --git a/tests/RawToConvertedGeometryTest.cpp b/tests/RawToConvertedGeometryTest.cpp index 43e601e7e..5fa679486 100644 --- a/tests/RawToConvertedGeometryTest.cpp +++ b/tests/RawToConvertedGeometryTest.cpp @@ -517,3 +517,38 @@ TEST_CASE("RawToConvertedCoordinate_Coord","[RawToConvertedGeometry]") { REQUIRE(Catch::Approx(c.x) == CONVERTED_MODULE_COLS + 8 + 800.5 + 6); // col 800 becomes 806 due to 3 chip boundaries REQUIRE(Catch::Approx(c.y) == CONVERTED_MODULE_LINES * 2 + 36 - 1 - 300.25 - 2); // line 300 becomes 302 due to 1 chip boundary } + +TEST_CASE("RawToConvertedGeometry_SqueezeToRawGeometry","[RawToConvertedGeometry]") { + // Assembled with the multipixels split, then squeezed back: the raw frames, where the split divides evenly + DiffractionExperiment x(DetJF(4, 2, 8, 36, true)); + x.PixelSigned(true).BitDepthImage(16); + + std::vector raw(x.GetModulesNum() * RAW_MODULE_SIZE); + for (size_t i = 0; i < raw.size(); i++) + raw[i] = static_cast(4 * (i % 5000) - 400); + raw[255 * RAW_MODULE_COLS + 255] = INT16_MIN; // error, chip corner + raw[RAW_MODULE_SIZE + 10 * RAW_MODULE_COLS + 256] = INT16_MAX; // saturated, multipixel + + std::vector image(x.GetPixelsNum(), INT32_MIN); + for (int m = 0; m < x.GetModulesNum(); m++) + TransferModuleAdjustMultipixels(image.data(), raw.data() + m * RAW_MODULE_SIZE, + x.GetModuleSlowDirectionStep(m), INT16_MIN, INT16_MAX, + x.GetModuleFastDirectionStep(m), x.GetPixel0OfModuleConv(m)); + for (auto &v: image) { + if (v == INT16_MIN) + v = INT32_MIN; + else if (v == INT16_MAX) + v = INT32_MAX; + } + + std::vector squeezed(raw.size()); + SqueezeToRawGeometry(x, squeezed.data(), image.data(), INT32_MIN, INT32_MAX, + INT16_MIN, INT16_MAX); + CHECK(squeezed == raw); + + // A sum past the 16-bit range is saturated + image[x.GetPixel0OfModuleConv(0)] = 40000; + SqueezeToRawGeometry(x, squeezed.data(), image.data(), INT32_MIN, INT32_MAX, + INT16_MIN, INT16_MAX); + CHECK(squeezed[0] == INT16_MAX); +}