Skip to content

Commit

Permalink
Added opacity parameter to ApplyColormap and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
smistad committed Sep 4, 2023
1 parent cbfe060 commit d976b39
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 8 deletions.
46 changes: 39 additions & 7 deletions source/FAST/Algorithms/ApplyColormap/ApplyColormap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
#include <FAST/Data/Image.hpp>

namespace fast {
ApplyColormap::ApplyColormap(Colormap colormap, float minValue, float maxValue) {
ApplyColormap::ApplyColormap(Colormap colormap, float opacity, float minValue, float maxValue) {
createInputPort(0, "Image");
createOutputPort(0, "Image");
setColormap(colormap);
setMinValue(minValue);
setMaxValue(maxValue);
setOpacity(opacity);
createOpenCLProgram(Config::getKernelSourcePath() + "/Algorithms/ApplyColormap/ApplyColormap.cl");
}

Expand All @@ -30,7 +31,7 @@ void ApplyColormap::execute() {
if(m_colormap.isGrayscale()) {
output = Image::create(input->getSize(), TYPE_UINT8, 1);
} else {
if(m_colormap.hasOpacity()) {
if(m_colormap.hasOpacity() || m_opacity < 1.0f) {
output = Image::create(input->getSize(), TYPE_UINT8, 4);
} else {
output = Image::create(input->getSize(), TYPE_UINT8, 3);
Expand All @@ -44,7 +45,7 @@ void ApplyColormap::execute() {
auto outputAccess = output->getOpenCLImageAccess(ACCESS_READ_WRITE, device);

if(!m_bufferUpToDate) {
m_colormapBuffer = m_colormap.getAsOpenCLBuffer(device);
m_colormapBuffer = m_colormap.getAsOpenCLBuffer(device, m_opacity);
m_bufferUpToDate = true;
}

Expand Down Expand Up @@ -96,6 +97,14 @@ void ApplyColormap::setMaxValue(float maxValue) {
setModified(true);
}

void ApplyColormap::setOpacity(float opacity) {
if(opacity < 0.0f || opacity > 1.0f)
throw Exception("Opacity must be within range [0, 1] in ApplyColormap");

m_opacity = opacity;
setModified(true);
}

bool Colormap::isGrayscale() const {
return m_grayscale;
}
Expand Down Expand Up @@ -154,15 +163,38 @@ void Colormap::checkData() const {
}
}

cl::Buffer Colormap::getAsOpenCLBuffer(OpenCLDevice::pointer device) const {
cl::Buffer Colormap::getAsOpenCLBuffer(OpenCLDevice::pointer device, float opacity) const {
if(m_data.empty())
throw Exception("Trying to get OpenCL buffer of empty Colormap");
checkData();
auto dataToTransfer = m_data;
if(opacity < 1.0f) {
int elements = 4;
if(m_grayscale) {
elements = 2;
} else {
if(m_hasOpacity)
elements = 5;
if(!hasOpacity()) {
// Have to add opacity channel
dataToTransfer = {};
for(int i = 0; i < m_data.size(); ++i) {
dataToTransfer.push_back(m_data[i]);
if((i+1) % elements == 0)
dataToTransfer.push_back(opacity);
}
} else {
for(int i = elements-1; i < m_data.size(); i += elements) {
dataToTransfer[i] *= opacity;
}
}
}
}
return cl::Buffer(
device->getContext(),
CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
m_data.size()*sizeof(float),
(void*)m_data.data()
dataToTransfer.size()*sizeof(float),
(void*)dataToTransfer.data()
);
}

Expand Down Expand Up @@ -233,7 +265,7 @@ Colormap Colormap::Inferno(bool withOpacity) {
{0.3, Color(80.0f/255, 35.0f/255, 0, 0.1f*enableOpacity)},
{0.4, Color(140.0f/255, 30.0f/255, 0, 0.3f*enableOpacity)},
{0.6, Color(200.0f/255, 160.0f/255, 0, 0.5f*enableOpacity)},
{0.85, Color(255.0f/255, 255.0f/255, 255.0f/255, 0.8f*enableOpacity)},
{1.0, Color(255.0f/255, 255.0f/255, 255.0f/255, 0.8f*enableOpacity)},
}, true, true);
}

Expand Down
40 changes: 39 additions & 1 deletion source/FAST/Algorithms/ApplyColormap/ApplyColormap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,30 @@ class FAST_EXPORT Colormap {
/**
* @brief Create an OpenCL buffer from the colormap data.
* @param device OpenCL device to transfer data to
* @param opacity Opacity to apply to colormap. If lower than 1 opacity will be added to the colormap.
* If the colormap already has opacity, this opacity will be multiplied with the existing opacity.
* @return OpenCL buffer
*/
cl::Buffer getAsOpenCLBuffer(OpenCLDevice::pointer device) const;
cl::Buffer getAsOpenCLBuffer(OpenCLDevice::pointer device, float opacity = 1.0f) const;
/**
* @brief Has this colormap opacity defined
* @return
*/
bool hasOpacity() const;
/**
* @brief Is this colormap grayscale
* @return
*/
bool isGrayscale() const;
/**
* @brief Is this colormap grayscale
* @return
*/
bool isInterpolated() const;
/**
* @brief Is this colormap intensity invariant
* @return
*/
bool isIntensityInvariant() const;
int getSteps() const;
/**
Expand All @@ -59,7 +77,18 @@ class FAST_EXPORT Colormap {
*/
std::vector<float> getData() const;

/**
* @brief Ultrasound S-curve colormap (grayscale and color (with a hint of blue))
* @param grayscale
* @return ultrasound colormap
*/
static Colormap Ultrasound(bool grayscale = false);
/**
* @brief Inferno heatmap
* @param withOpacity Create inferno heatmap with custom opacity.
* If you will use this heatmap as an overlay you should enable this.
* @return inferno heatmap
*/
static Colormap Inferno(bool withOpacity = false);
private:
std::vector<float> m_data;
Expand Down Expand Up @@ -89,17 +118,25 @@ class FAST_EXPORT ApplyColormap : public ProcessObject {
/**
* @brief Create instance
* @param colormap Colormap to apply
* @param opacity Apply colormap with an opacity.
* If the colormap already has opacity, this opacity will be multiplied with the existing opacity.
* @param minValue Set the minimum value of the input data to scale the colormap to. This is only used
* on intensity invariant colormaps. If not set, the true minimum value of the input data is used.
* @param maxValue Set the maximum value of the input data to scale the colormap to. This is only used
* on intensity invariant colormaps. If not set, the true maximum value of the input data is used.
* @return instance
*/
FAST_CONSTRUCTOR(ApplyColormap,
Colormap, colormap,,
float, opacity, = 1.0f,
float, minValue, = std::nanf(""),
float, maxValue, = std::nanf("")
);
void setColormap(Colormap colormap);
Colormap getColormap() const;
void setMinValue(float minValue);
void setMaxValue(float maxValue);
void setOpacity(float opacity);
protected:
ApplyColormap();
void execute() override;
Expand All @@ -108,6 +145,7 @@ class FAST_EXPORT ApplyColormap : public ProcessObject {
cl::Buffer m_colormapBuffer;
float m_minValue;
float m_maxValue;
float m_opacity = 1.0f;
};

} // end namespace

0 comments on commit d976b39

Please sign in to comment.