Files
Jungfraujoch/preview/PreviewImage.h
T
leonarski_fandClaude Opus 5.5 4d90053899 Viewer and preview show a panel detector as it lies on the detector plane
With a panel table, the viewer and the broker preview gather the stored image
onto the detector plane (AssembleToPlane) and draw on that: spots,
predictions, rings, beam centre and ROIs are plane coordinates, so they now
land on the right pixels for a stored (stacked or moved-module) layout, where
they were drawn as image pixels before. The viewer scene is the plane; its
image item sits at the raster's origin (JFJochImage::origin_, zero for a flat
detector). Mouse readout, pixel labels and the magnifier read the shown
raster; top pixels and saturation crosses are mapped from the stored pixel to
the plane; an ROI turned into a user mask is tested per stored pixel at its
plane position.

A flat detector (empty table) takes the old path verbatim: the shown pixels
alias the image, origin (0, 0) - a JF/EIGER file renders identically.

common/AssembleToPlane.h is a TEMPORARY header-only version so this stage
builds on its own; stage 3 owns the file and its version replaces this one at
merge (callers use AssembleToPlane(modules, image, width, height,
PlaneBounds, gap) -> PlaneRaster{x0, y0, width, height, pixels}).

Test: PreviewImage_PanelLayout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu
2026-10-21 10:28:23 +02:00

92 lines
4.1 KiB
C++

// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <vector>
#include <mutex>
#include "../common/SpotToSave.h"
#include "../common/Reflection.h"
#include "../common/DiffractionExperiment.h"
#include "../common/PixelMask.h"
#include "PreviewCounter.h"
#include "../common/ColorScale.h"
enum class PreviewImageFormat {JPEG, TIFF};
struct PreviewImageSettings {
std::optional<float> saturation_value = 10;
std::optional<float> background_value = 0;
int64_t jpeg_quality = 70;
bool show_beam_center = true;
bool show_spots = true;
bool show_predictions = false;
bool show_roi = false;
bool show_user_mask = true;
bool show_res_est = false;
std::optional<float> resolution_ring;
ColorScaleEnum scale = ColorScaleEnum::Indigo;
PreviewImageFormat format = PreviewImageFormat::JPEG;
};
class PreviewImage {
mutable std::mutex m;
DiffractionExperiment experiment;
std::vector<uint8_t> mask;
std::vector<SpotToSave> spots;
size_t xpixel = 0;
size_t ypixel = 0;
// The picture being drawn on, in detector-plane pixels: its top-left pixel and size. The stored image
// for a flat detector, the image gathered onto the plane (AssembleToPlane) for one with a panel table.
// Set by GenerateRGB under m, and the overlays are drawn there in plane coordinates.
mutable int64_t canvas_x0 = 0, canvas_y0 = 0, canvas_width = 0, canvas_height = 0;
size_t pixel_depth_bytes = 2;
bool pixel_is_signed = false;
constexpr static uint8_t MaskGap = 1;
constexpr static uint8_t MaskUsr = 2;
constexpr static uint8_t MaskDet = 3;
constexpr static float auto_foreground_range = 0.001f;
template<class T>
std::vector<rgb> GenerateRGB(const uint8_t* value,
int64_t special_value,
int64_t sat_value,
const ColorScale &scale,
const PreviewImageSettings& settings) const;
std::vector<rgb> GenerateRGB(const PreviewImageSettings& settings, const DataMessage &msg,
size_t &width, size_t &height) const;
void AddResolutionRing(std::vector<rgb> &rgb_image, float d) const;
void AddBeamCenter(std::vector<rgb> &rgb_image) const;
void AddSpots(std::vector<rgb> &rgb_image, const std::vector<SpotToSave>& spots) const;
void AddPredictions(std::vector<rgb> &rgb_image, const std::vector<Reflection>& reflections, char centering) const;
void AddROI(std::vector<rgb> &rgb_image) const;
// Trace a fixed-resolution arc through the geometry (a circular arc on an untilted detector,
// a conic one on a tilted detector) by sweeping azimuth from phi_start to phi_end and joining
// the samples. A full ring uses phi_start=0, phi_end=2*pi.
void DrawArc(std::vector<rgb> &rgb_image, const DiffractionGeometry &geom, float d,
float phi_start, float phi_end, const rgb &color, int halfwidth) const;
void DrawThickLine(std::vector<rgb> &rgb_image, float x0, float y0, float x1, float y1,
const rgb &color, int halfwidth) const;
void DrawCircleOutline(std::vector<rgb> &rgb_image, float cx, float cy, float radius,
int width, const rgb &color) const;
void color_pixel(std::vector<rgb>& ret, int64_t xpixel, int64_t ypixel, const rgb &color) const;
void spot(std::vector<rgb>& ret, int64_t xpixel, int64_t ypixel, const rgb &color) const;
void roi(std::vector<rgb>& ret, int64_t xpixel, int64_t ypixel, int64_t roi_number) const;
std::string GenerateImage(const PreviewImageSettings& settings, const DataMessage &msg) const;
void ConfigurePixel(const std::vector<uint32_t> &mask, size_t pixel_begin, size_t pixel_end);
public:
void Configure(const DiffractionExperiment& experiment, const PixelMask& mask, size_t nthreads = 0);
[[nodiscard]] std::string GenerateImage(const PreviewImageSettings& settings, const std::vector<uint8_t>& cbor_format);
[[nodiscard]] static std::string GenerateTIFF(const std::vector<uint8_t>& cbor_format) ;
};