Skip to content

Commit

Permalink
Add IsColorOrderUndefined, Push/PopColorOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Oct 8, 2024
1 parent 61faf20 commit 3d82cf3
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 19 deletions.
7 changes: 7 additions & 0 deletions src/immvision/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ namespace ImmVision
bool IsUsingRgbColorOrder();
// Returns true if we are using BGR color order
bool IsUsingBgrColorOrder();
// Returns true if the color order is undefined (i.e. UseRgbColorOrder or UseBgrColorOrder was not called)
bool IsColorOrderUndefined();

// Temporary change of color order (useful for displaying a single image with a different color order)
void PushColorOrderRgb();
void PushColorOrderBgr();
void PopColorOrder();

// Are we using the stats on the full image, on the Visible ROI, or are we using Min/Max values
enum class ColorMapStatsTypeId
Expand Down
61 changes: 52 additions & 9 deletions src/immvision/internal/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "imgui_internal.h"

#include <map>
#include <stack>
#include <vector>
#include <optional>
#include <iostream>
Expand All @@ -27,15 +28,46 @@

namespace ImmVision
{
// With Image and ImageDisplay we can rely on the ID stack,
// since calls to Image & ImageDisplay will have a reproducible id stack
static bool sDoUseIdStack = true;
// ================================ Color Order stuff ================================

enum class PrivColorOrder
{
RGB,
BGR
};

std::stack<PrivColorOrder> sColorOrderStack;

void PushColorOrderRgb()
{
sColorOrderStack.push(PrivColorOrder::RGB);
}
void PushColorOrderBgr()
{
sColorOrderStack.push(PrivColorOrder::BGR);
}
void PopColorOrder()
{
if (sColorOrderStack.empty())
{
const char* errorMessage = R"(
Error in ImmVision
==================
PopColorOrder() called too many times. The color order stack is empty.
Ensure that each PushColorOrderRgb()/PushColorOrderBgr() call is paired with a PopColorOrder() call.
)";
fprintf(stderr, "%s", errorMessage);
throw std::runtime_error(errorMessage);
}
sColorOrderStack.pop();
}


static std::optional<bool> sUseBgrOrder = std::nullopt;
void UseRgbColorOrder() { sUseBgrOrder = false; }
void UseBgrColorOrder() { sUseBgrOrder = true; }
bool Priv_IsColorOrderBgr()
void UseRgbColorOrder() { PushColorOrderRgb(); }
void UseBgrColorOrder() { PushColorOrderBgr(); }

static bool Priv_IsColorOrderBgr()
{
const char* errorMessage = R"(
Error in ImmVision
Expand All @@ -47,12 +79,12 @@ or
This is a required setup step. (Breaking change - October 2024)
)";
if (!sUseBgrOrder.has_value())
if (sColorOrderStack.empty())
{
fprintf(stderr, "%s", errorMessage);
throw std::runtime_error(errorMessage);
}
return sUseBgrOrder.value();
return sColorOrderStack.top() == PrivColorOrder::BGR;
}
bool IsUsingRgbColorOrder()
{
Expand All @@ -62,6 +94,17 @@ This is a required setup step. (Breaking change - October 2024)
{
return Priv_IsColorOrderBgr();
}
bool IsColorOrderUndefined()
{
return sColorOrderStack.empty();
}


// ================================ Rest ================================

// With Image and ImageDisplay we can rely on the ID stack,
// since calls to Image & ImageDisplay will have a reproducible id stack
static bool sDoUseIdStack = true;


void ClearTextureCache()
Expand Down
69 changes: 59 additions & 10 deletions src_all_in_one/immvision/immvision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ namespace ImmVision
bool IsUsingRgbColorOrder();
// Returns true if we are using BGR color order
bool IsUsingBgrColorOrder();
// Returns true if the color order is undefined (i.e. UseRgbColorOrder or UseBgrColorOrder was not called)
bool IsColorOrderUndefined();

// Temporary change of color order (useful for displaying a single image with a different color order)
void PushColorOrderRgb();
void PushColorOrderBgr();
void PopColorOrder();

// Are we using the stats on the full image, on the Visible ROI, or are we using Min/Max values
enum class ColorMapStatsTypeId
Expand Down Expand Up @@ -9487,22 +9494,54 @@ namespace ImmVision
// src/immvision/internal/image.cpp continued //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <stack>

#ifndef IMMVISION_VERSION
#define IMMVISION_VERSION "unknown version"
#endif

namespace ImmVision
{
// With Image and ImageDisplay we can rely on the ID stack,
// since calls to Image & ImageDisplay will have a reproducible id stack
static bool sDoUseIdStack = true;
// ================================ Color Order stuff ================================

enum class PrivColorOrder
{
RGB,
BGR
};

std::stack<PrivColorOrder> sColorOrderStack;

void PushColorOrderRgb()
{
sColorOrderStack.push(PrivColorOrder::RGB);
}
void PushColorOrderBgr()
{
sColorOrderStack.push(PrivColorOrder::BGR);
}
void PopColorOrder()
{
if (sColorOrderStack.empty())
{
const char* errorMessage = R"(
Error in ImmVision
==================
PopColorOrder() called too many times. The color order stack is empty.

Ensure that each PushColorOrderRgb()/PushColorOrderBgr() call is paired with a PopColorOrder() call.
)";
fprintf(stderr, "%s", errorMessage);
throw std::runtime_error(errorMessage);
}
sColorOrderStack.pop();
}


static std::optional<bool> sUseBgrOrder = std::nullopt;
void UseRgbColorOrder() { sUseBgrOrder = false; }
void UseBgrColorOrder() { sUseBgrOrder = true; }
bool Priv_IsColorOrderBgr()
void UseRgbColorOrder() { PushColorOrderRgb(); }
void UseBgrColorOrder() { PushColorOrderBgr(); }

static bool Priv_IsColorOrderBgr()
{
const char* errorMessage = R"(
Error in ImmVision
Expand All @@ -9514,12 +9553,12 @@ or

This is a required setup step. (Breaking change - October 2024)
)";
if (!sUseBgrOrder.has_value())
if (sColorOrderStack.empty())
{
fprintf(stderr, "%s", errorMessage);
throw std::runtime_error(errorMessage);
}
return sUseBgrOrder.value();
return sColorOrderStack.top() == PrivColorOrder::BGR;
}
bool IsUsingRgbColorOrder()
{
Expand All @@ -9529,6 +9568,17 @@ This is a required setup step. (Breaking change - October 2024)
{
return Priv_IsColorOrderBgr();
}
bool IsColorOrderUndefined()
{
return sColorOrderStack.empty();
}


// ================================ Rest ================================

// With Image and ImageDisplay we can rely on the ID stack,
// since calls to Image & ImageDisplay will have a reproducible id stack
static bool sDoUseIdStack = true;


void ClearTextureCache()
Expand Down Expand Up @@ -10913,7 +10963,6 @@ namespace ImmVision
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <sstream>
#include <stack>

namespace ImGuiImm
{
Expand Down
7 changes: 7 additions & 0 deletions src_all_in_one/immvision/immvision.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ namespace ImmVision
bool IsUsingRgbColorOrder();
// Returns true if we are using BGR color order
bool IsUsingBgrColorOrder();
// Returns true if the color order is undefined (i.e. UseRgbColorOrder or UseBgrColorOrder was not called)
bool IsColorOrderUndefined();

// Temporary change of color order (useful for displaying a single image with a different color order)
void PushColorOrderRgb();
void PushColorOrderBgr();
void PopColorOrder();

// Are we using the stats on the full image, on the Visible ROI, or are we using Min/Max values
enum class ColorMapStatsTypeId
Expand Down

0 comments on commit 3d82cf3

Please sign in to comment.