Files
Jungfraujoch/preview/PreviewImage.h
T
leonarski_fandClaude Fable 5 a4559e576d Preview: geometry-following arcs, true beam center, unified colours, predictions
Draw the resolution ring and azimuthal-ROI outlines by sweeping ResPhiToPxl
through the geometry (a shared DrawArc/DrawThickLine helper), so they trace the
true conic on a tilted detector instead of PONI-centred circles. Draw the beam
crosshair at GetDirectBeam_pxl() (the real beam/detector intersection) rather
than the stored PONI.

Unify the spot overlay colours with the viewer: green = not indexed, magenta =
indexed (primary lattice), cyan = ice ring, coral = secondary/further lattice.
Add Bragg-prediction overlay (dark-red circles, skipping systematic absences)
gated on a new show_predictions preview setting, wired through the OpenAPI spec,
the image.jpeg HTTP handler, and the frontend toggle (+regenerated TS client).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 12:04:00 +02:00

87 lines
3.7 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;
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) 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) ;
};