code_device/hgdriver/ImageProcess/ImageApplyCustomGamma.cpp

64 lines
1.5 KiB
C++

#include "ImageApplyCustomGamma.h"
CImageApplyCustomGamma::CImageApplyCustomGamma(unsigned char* table,int length)
: emptyPtr(false)
{
if(table==nullptr)
emptyPtr = true;
init_gamma_table();
setLUT(table,length == 256 ? 1 : 3);
}
CImageApplyCustomGamma::~CImageApplyCustomGamma()
{
}
void CImageApplyCustomGamma::setLUT(const unsigned char* table, int byteCount)
{
if (emptyPtr)
return;
if (byteCount == 1)
memcpy(m_table_bit8, table, 256);
else if (byteCount == 3)
memcpy(m_table_bit24, table, 768);
}
void CImageApplyCustomGamma::apply(cv::Mat& pDib, int side)
{
(void)side;
if (emptyPtr)
return;
int numOfChannels = pDib.channels();
cv::Mat mat_table(1, 256, CV_8UC(numOfChannels), (numOfChannels == 3) ? m_table_bit24 : m_table_bit8);
cv::LUT(pDib, mat_table, pDib);
}
void CImageApplyCustomGamma::apply(std::vector<cv::Mat>& mats, bool isTwoSide)
{
(void)isTwoSide;
int i = 0;
for (cv::Mat& var : mats) {
if (i != 0 && isTwoSide == false)
break;
if (!var.empty())
apply(var, 0);
i++;
}
}
void CImageApplyCustomGamma::setTableData(const unsigned char* data, int length)
{
setLUT(data, length == 256 ? 1 : 3);
}
void CImageApplyCustomGamma::init_gamma_table()
{
for (size_t i = 0; i < 256; ++i)
{
m_table_bit8[i] = static_cast<unsigned char>(i);
m_table_bit24[i * 3] = m_table_bit24[i * 3 + 1] = m_table_bit24[i * 3 + 2] = static_cast<unsigned char>(i);
}
}