Skip to content

Commit

Permalink
Merge pull request #215 from iRath96/statistics-over-crops
Browse files Browse the repository at this point in the history
WIP: Statistics only over selected regions of the image
  • Loading branch information
Tom94 authored Jan 4, 2024
2 parents d33a46c + 1c25e99 commit 04e0ec1
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 102 deletions.
36 changes: 34 additions & 2 deletions include/tev/Box.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <tev/Common.h>


namespace tev {

template <typename T, uint32_t N_DIMS>
Expand All @@ -20,8 +19,15 @@ struct Box {
template <typename U>
Box(const Box<U, N_DIMS>& other) : min{other.min}, max{other.max} {}

Box(const std::vector<Vector>& points) : Box() {
for (const auto& point : points) {
min = nanogui::min(min, point);
max = nanogui::max(max, point);
}
}

Vector size() const {
return max - min;
return nanogui::max(max - min, Vector{(T)0});
}

Vector middle() const {
Expand All @@ -44,6 +50,26 @@ struct Box {
return result;
}

bool contains_inclusive(const Vector& pos) const {
bool result = true;
for (uint32_t i = 0; i < N_DIMS; ++i) {
result &= pos[i] >= min[i] && pos[i] <= max[i];
}
return result;
}

bool contains(const Box& other) const {
return contains_inclusive(other.min) && contains_inclusive(other.max);
}

Box intersect(const Box& other) const {
return {nanogui::max(min, other.min), nanogui::min(max, other.max)};
}

Box translate(const Vector& offset) const {
return {min + offset, max + offset};
}

bool operator==(const Box& other) const {
return min == other.min && max == other.max;
}
Expand All @@ -55,6 +81,12 @@ struct Box {
Vector min, max;
};

template <typename Stream, typename T, uint32_t N_DIMS, std::enable_if_t<std::is_base_of_v<std::ostream, Stream>, int> = 0>
Stream& operator<<(Stream& os, const Box<T, N_DIMS>& v) {
os << '[' << v.min << ", " << v.max << ']';
return os;
}

using Box2f = Box<float, 2>;
using Box3f = Box<float, 3>;
using Box4f = Box<float, 4>;
Expand Down
1 change: 1 addition & 0 deletions include/tev/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,5 +396,6 @@ void redrawWindow();

static const nanogui::Color IMAGE_COLOR = {0.35f, 0.35f, 0.8f, 1.0f};
static const nanogui::Color REFERENCE_COLOR = {0.7f, 0.4f, 0.4f, 1.0f};
static const nanogui::Color CROP_COLOR = {0.2f, 0.5f, 0.2f, 1.0f};

}
15 changes: 14 additions & 1 deletion include/tev/ImageCanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

#pragma once

#include <tev/UberShader.h>
#include <tev/Box.h>
#include <tev/Image.h>
#include <tev/Lazy.h>
#include <tev/UberShader.h>

#include <nanogui/canvas.h>

#include <memory>
#include <optional>

namespace tev {

Expand Down Expand Up @@ -65,6 +67,7 @@ class ImageCanvas : public nanogui::Canvas {
}

nanogui::Vector2i getImageCoords(const Image& image, nanogui::Vector2i mousePos);
nanogui::Vector2i getDisplayWindowCoords(const Image& image, nanogui::Vector2i mousePos);

void getValuesAtNanoPos(nanogui::Vector2i nanoPos, std::vector<float>& result, const std::vector<std::string>& channels);
std::vector<float> getValuesAtNanoPos(nanogui::Vector2i nanoPos, const std::vector<std::string>& channels) {
Expand Down Expand Up @@ -94,6 +97,14 @@ class ImageCanvas : public nanogui::Canvas {
mMetric = metric;
}

void setCrop(const std::optional<Box2i>& crop) {
mCrop = crop;
}

std::optional<Box2i> getCrop() {
return mCrop;
}

static float applyMetric(float value, float reference, EMetric metric);
float applyMetric(float value, float reference) const {
return applyMetric(value, reference, mMetric);
Expand Down Expand Up @@ -141,6 +152,7 @@ class ImageCanvas : public nanogui::Canvas {
std::shared_ptr<Image> reference,
const std::string& requestedChannelGroup,
EMetric metric,
const Box2i& region,
int priority
);

Expand Down Expand Up @@ -174,6 +186,7 @@ class ImageCanvas : public nanogui::Canvas {

ETonemap mTonemap = SRGB;
EMetric mMetric = Error;
std::optional<Box2i> mCrop;

std::map<std::string, std::shared_ptr<Lazy<std::shared_ptr<CanvasStatistics>>>> mCanvasStatistics;
std::map<int, std::vector<std::string>> mImageIdToCanvasStatisticsKey;
Expand Down
15 changes: 10 additions & 5 deletions include/tev/ImageViewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,18 @@ class ImageViewer : public nanogui::Screen {

HelpWindow* mHelpWindow = nullptr;

bool mIsDraggingSidebar = false;
bool mIsDraggingImage = false;
bool mIsDraggingImageButton = false;
enum class EMouseDragType {
None,
ImageDrag,
ImageCrop,
ImageButtonDrag,
SidebarDrag,
};

nanogui::Vector2i mDraggingStartPosition;
EMouseDragType mDragType = EMouseDragType::None;
size_t mDraggedImageButtonId;

nanogui::Vector2f mDraggingStartPosition;

size_t mClipboardIndex = 0;

bool mSupportsHdr = false;
Expand Down
10 changes: 8 additions & 2 deletions include/tev/UberShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

#pragma once

#include <tev/Box.h>

#include <nanogui/shader.h>
#include <nanogui/texture.h>
#include <nanogui/vector.h>

#include <optional>

namespace tev {

class UberShader {
Expand All @@ -27,7 +31,8 @@ class UberShader {
float offset,
float gamma,
bool clipToLdr,
ETonemap tonemap
ETonemap tonemap,
const std::optional<Box2i>& crop
);

// Draws a difference between a reference and an image.
Expand All @@ -43,7 +48,8 @@ class UberShader {
float gamma,
bool clipToLdr,
ETonemap tonemap,
EMetric metric
EMetric metric,
const std::optional<Box2i>& crop
);

const nanogui::Color& backgroundColor() {
Expand Down
5 changes: 3 additions & 2 deletions src/HelpWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ HelpWindow::HelpWindow(Widget* parent, bool supportsHdr, function<void()> closeC
addRow(imageSelection, "Home / End", "Select first / last image");
addRow(imageSelection, "Space", "Toggle playback of images as video");

addRow(imageSelection, "Click & Drag (+Shift/" + COMMAND + ")", "Translate image");
addRow(imageSelection, "Click & Drag (+Shift/" + COMMAND + ")", "Translate image");
addRow(imageSelection, "Click & Drag+C (hold)", "Select region of histogram");
addRow(imageSelection, "+ / - / Scroll (+Shift/" + COMMAND + ")", "Zoom in / out of image");

addRow(imageSelection, COMMAND + "+0", "Zoom to actual size");
Expand Down Expand Up @@ -110,7 +111,7 @@ HelpWindow::HelpWindow(Widget* parent, bool supportsHdr, function<void()> closeC
auto referenceSelection = new Widget{shortcuts};
referenceSelection->set_layout(new BoxLayout{Orientation::Vertical, Alignment::Fill, 0, 0});

addRow(referenceSelection, ALT + " (hold)", "View currently selected reference");
addRow(referenceSelection, "Shift (hold)", "View currently selected reference");
addRow(referenceSelection, "Shift+Left Click or Right Click", "Select hovered image as reference");
addRow(referenceSelection, "Shift+1…9", "Select N-th image as reference");
addRow(referenceSelection, "Shift+Down or Shift+S / Shift+Up or Shift+W", "Select next / previous image as reference");
Expand Down
Loading

0 comments on commit 04e0ec1

Please sign in to comment.