Toshihiro Shimizu 890ddd
#include "toonzqt/combohistogram.h"
Toshihiro Shimizu 890ddd
#include "tcolorstyles.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qvboxlayout></qvboxlayout>
Toshihiro Shimizu 890ddd
#include <qgridlayout></qgridlayout>
Toshihiro Shimizu 890ddd
#include <qcombobox></qcombobox>
Toshihiro Shimizu 890ddd
#include <qstackedwidget></qstackedwidget>
Toshihiro Shimizu 890ddd
#include <qpainter></qpainter>
Toshihiro Shimizu 890ddd
#include <qlabel></qlabel>
Toshihiro Shimizu 890ddd
#include <qstring></qstring>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/preferences.h"
Toshihiro Shimizu 890ddd
//iwsw commented out temporarily
Toshihiro Shimizu 890ddd
//#include "toonzqt/ghibli_3dlut_converter.h"
Toshihiro Shimizu 890ddd
#include <qpushbutton></qpushbutton>
Toshihiro Shimizu 890ddd
#include <qdialog></qdialog>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// ChannelHistoGraph
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ChannelHistoGraph::ChannelHistoGraph(QWidget *parent, int *channelValue)
Toshihiro Shimizu 890ddd
	: QWidget(parent), m_channelValuePtr(channelValue), m_pickedValue(-1)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// width 256+2 pixels, height 100+2 pixels (with margin)
Toshihiro Shimizu 890ddd
	setFixedSize(COMBOHIST_RESOLUTION_W + 2, COMBOHIST_RESOLUTION_H + 2);
Toshihiro Shimizu 890ddd
	m_values.reserve(COMBOHIST_RESOLUTION_W);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ChannelHistoGraph::~ChannelHistoGraph()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_values.clear();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ChannelHistoGraph::setValues()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_values.clear();
Toshihiro Shimizu 890ddd
	m_values.resize(COMBOHIST_RESOLUTION_W);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//normalize with the maximum value
Toshihiro Shimizu 890ddd
	int maxValue = 1;
Toshihiro Shimizu 890ddd
	for (i = 0; i < COMBOHIST_RESOLUTION_W; i++) {
Toshihiro Shimizu 890ddd
		int count = m_channelValuePtr[i];
Toshihiro Shimizu 890ddd
		if (maxValue < count)
Toshihiro Shimizu 890ddd
			maxValue = count;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < COMBOHIST_RESOLUTION_W; i++) {
Toshihiro Shimizu 890ddd
		int v = m_channelValuePtr[i];
Toshihiro Shimizu 890ddd
		m_values[i] = tround((double)(v * COMBOHIST_RESOLUTION_H) / (double)maxValue);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ChannelHistoGraph::paintEvent(QPaintEvent *event)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPainter p(this);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p.setPen(QColor(144, 144, 144));
Toshihiro Shimizu 890ddd
	p.setBrush(QColor(214, 214, 214));
Toshihiro Shimizu 890ddd
	p.drawRect(rect().x(), rect().y(), rect().width() - 1, rect().height() - 1);
Toshihiro Shimizu 890ddd
	p.setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// draw scale marks
Toshihiro Shimizu 890ddd
	p.setPen(QColor(144, 144, 144));
Toshihiro Shimizu 890ddd
	for (i = 1; i < 10; i++) {
Toshihiro Shimizu 890ddd
		int posx = rect().width() * i / 10;
Toshihiro Shimizu 890ddd
		p.drawLine(posx, 1, posx, COMBOHIST_RESOLUTION_H);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_values.size() == 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p.setPen(Qt::black);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//draw each histogram
Toshihiro Shimizu 890ddd
	for (i = 0; i < COMBOHIST_RESOLUTION_W; i++) {
Toshihiro Shimizu 890ddd
		int v = m_values[i];
Toshihiro Shimizu 890ddd
		if (v <= 0)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		int x = 1 + i;
Toshihiro Shimizu 890ddd
		p.drawLine(x, COMBOHIST_RESOLUTION_H + 1 - v, x, COMBOHIST_RESOLUTION_H);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//draw picked color's channel value
Toshihiro Shimizu 890ddd
	if (m_pickedValue > -1) {
Toshihiro Shimizu 890ddd
		p.setPen(Qt::white);
Toshihiro Shimizu 890ddd
		int x = 1 + m_pickedValue;
Toshihiro Shimizu 890ddd
		p.drawLine(x, 1, x, COMBOHIST_RESOLUTION_H);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ChannelHistoGraph::showCurrentChannelValue(int val)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_pickedValue = val;
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// ChannelHistoGraph
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
RGBHistoGraph::RGBHistoGraph(QWidget *parent, int *channelValue)
Toshihiro Shimizu 890ddd
	: ChannelHistoGraph(parent, channelValue)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_histoImg = QImage(COMBOHIST_RESOLUTION_W, COMBOHIST_RESOLUTION_H, QImage::Format_ARGB32_Premultiplied);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
RGBHistoGraph::~RGBHistoGraph()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int i = 0; i < 3; i++)
Toshihiro Shimizu 890ddd
		m_rgbValues[i].clear();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void RGBHistoGraph::setValues()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int chan = 0; chan < 3; chan++) {
Toshihiro Shimizu 890ddd
		m_rgbValues[chan].clear();
Toshihiro Shimizu 890ddd
		m_rgbValues[chan].resize(COMBOHIST_RESOLUTION_W);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//normalize with the maximum value
Toshihiro Shimizu 890ddd
		int maxValue = 1;
Toshihiro Shimizu 890ddd
		for (i = 0; i < COMBOHIST_RESOLUTION_W; i++) {
Toshihiro Shimizu 890ddd
			int count = m_channelValuePtr[COMBOHIST_RESOLUTION_W * chan + i];
Toshihiro Shimizu 890ddd
			if (maxValue < count)
Toshihiro Shimizu 890ddd
				maxValue = count;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (i = 0; i < COMBOHIST_RESOLUTION_W; i++) {
Toshihiro Shimizu 890ddd
			int v = m_channelValuePtr[COMBOHIST_RESOLUTION_W * chan + i];
Toshihiro Shimizu 890ddd
			m_rgbValues[chan][i] = tround((double)(v * COMBOHIST_RESOLUTION_H) / (double)maxValue);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QPainter imgPainter(&m_histoImg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	imgPainter.fillRect(m_histoImg.rect(), Qt::black);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_rgbValues[0].size() == 0 || m_rgbValues[1].size() == 0 || m_rgbValues[2].size() == 0) {
Toshihiro Shimizu 890ddd
		imgPainter.end();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	imgPainter.setCompositionMode(QPainter::CompositionMode_Plus);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int chan = 0; chan < 3; chan++) {
Toshihiro Shimizu 890ddd
		imgPainter.setPen((chan == 0) ? Qt::red : (chan == 1) ? Qt::green : Qt::blue);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int i = 0; i < COMBOHIST_RESOLUTION_W; i++) {
Toshihiro Shimizu 890ddd
			int v = m_rgbValues[chan][i];
Toshihiro Shimizu 890ddd
			if (v <= 0)
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
			int x = 1 + i;
Toshihiro Shimizu 890ddd
			imgPainter.drawLine(x, COMBOHIST_RESOLUTION_H + 1 - v, x, COMBOHIST_RESOLUTION_H);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	imgPainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
Toshihiro Shimizu 890ddd
	imgPainter.end();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void RGBHistoGraph::paintEvent(QPaintEvent *event)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPainter p(this);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p.setCompositionMode(QPainter::CompositionMode_SourceOver);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p.setPen(QColor(144, 144, 144));
Toshihiro Shimizu 890ddd
	p.setBrush(QColor(64, 64, 64));
Toshihiro Shimizu 890ddd
	p.drawRect(rect().x(), rect().y(), rect().width() - 1, rect().height() - 1);
Toshihiro Shimizu 890ddd
	p.setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p.drawImage(1, 1, m_histoImg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// ChannelColorBar
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
ChannelColorBar::ChannelColorBar(QWidget *parent, QColor color)
Toshihiro Shimizu 890ddd
	: QWidget(parent), m_color(color)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// 2 pixels margin for width
Toshihiro Shimizu 890ddd
	setFixedSize(COMBOHIST_RESOLUTION_W + 2, 6);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ChannelColorBar::paintEvent(QPaintEvent *event)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPainter p(this);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QLinearGradient linearGrad(QPoint(1, 0), QPoint(COMBOHIST_RESOLUTION_W, 0));
Toshihiro Shimizu 890ddd
	if (m_color == QColor(0, 0, 0, 0)) {
Toshihiro Shimizu 890ddd
		linearGrad.setColorAt(0, m_color);
Toshihiro Shimizu 890ddd
		linearGrad.setColorAt(1, Qt::white);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		linearGrad.setColorAt(0, Qt::black);
Toshihiro Shimizu 890ddd
		linearGrad.setColorAt(1, m_color);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p.setBrush(QBrush(linearGrad));
Toshihiro Shimizu 890ddd
	p.setPen(Qt::NoPen);
Toshihiro Shimizu 890ddd
	p.drawRect(rect());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// ChannelHisto
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
ChannelHisto::ChannelHisto(int channelIndex, int *channelValue, QWidget *parent)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString label;
Toshihiro Shimizu 890ddd
	QColor color;
Toshihiro Shimizu 890ddd
	switch (channelIndex) {
Toshihiro Shimizu 890ddd
	case 0:
Toshihiro Shimizu 890ddd
		label = tr("Red");
Toshihiro Shimizu 890ddd
		color = Qt::red;
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 1:
Toshihiro Shimizu 890ddd
		label = tr("Green");
Toshihiro Shimizu 890ddd
		color = Qt::green;
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 2:
Toshihiro Shimizu 890ddd
		label = tr("Blue");
Toshihiro Shimizu 890ddd
		color = Qt::blue;
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 3:
Toshihiro Shimizu 890ddd
		label = tr("Alpha");
Toshihiro Shimizu 890ddd
		color = QColor(0, 0, 0, 0);
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case 4:
Toshihiro Shimizu 890ddd
		label = tr("RGBA");
Toshihiro Shimizu 890ddd
		color = Qt::white;
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (channelIndex != 4)
Toshihiro Shimizu 890ddd
		m_histogramGraph = new ChannelHistoGraph(this, channelValue);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		m_histogramGraph = new RGBHistoGraph(this, channelValue);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_colorBar = new ChannelColorBar(this, color);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//show/hide the alpha channel
Toshihiro Shimizu 890ddd
	QPushButton *showAlphaChannelButton = 0;
Toshihiro Shimizu 890ddd
	if (channelIndex == 3) {
Toshihiro Shimizu 890ddd
		showAlphaChannelButton = new QPushButton("", this);
Toshihiro Shimizu 890ddd
		showAlphaChannelButton->setObjectName("FxSettingsPreviewShowButton");
Toshihiro Shimizu 890ddd
		showAlphaChannelButton->setFixedSize(15, 15);
Toshihiro Shimizu 890ddd
		showAlphaChannelButton->setCheckable(true);
Toshihiro Shimizu 890ddd
		showAlphaChannelButton->setChecked(false);
Toshihiro Shimizu 890ddd
		showAlphaChannelButton->setFocusPolicy(Qt::NoFocus);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//layout
Toshihiro Shimizu 890ddd
	QVBoxLayout *mainLayout = new QVBoxLayout(this);
Toshihiro Shimizu 890ddd
	mainLayout->setMargin(0);
Toshihiro Shimizu 890ddd
	mainLayout->setSpacing(2);
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		QHBoxLayout *titleLay = new QHBoxLayout();
Toshihiro Shimizu 890ddd
		titleLay->setMargin(0);
Toshihiro Shimizu 890ddd
		titleLay->setSpacing(2);
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			titleLay->addWidget(new QLabel(label, this), 0);
Toshihiro Shimizu 890ddd
			if (channelIndex == 3)
Toshihiro Shimizu 890ddd
				titleLay->addWidget(showAlphaChannelButton, 0);
Toshihiro Shimizu 890ddd
			titleLay->addStretch(1);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		mainLayout->addLayout(titleLay);
Toshihiro Shimizu 890ddd
		mainLayout->addSpacing(3);
Toshihiro Shimizu 890ddd
		mainLayout->addWidget(m_histogramGraph);
Toshihiro Shimizu 890ddd
		mainLayout->addWidget(m_colorBar);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	setLayout(mainLayout);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (channelIndex == 3) {
Toshihiro Shimizu 890ddd
		connect(showAlphaChannelButton, SIGNAL(toggled(bool)), this, SLOT(onShowAlphaButtonToggled(bool)));
Toshihiro Shimizu 890ddd
		onShowAlphaButtonToggled(false);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! update the picked color's channel value
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void ChannelHisto::showCurrentChannelValue(int val)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_histogramGraph->showCurrentChannelValue(val);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ChannelHisto::onShowAlphaButtonToggled(bool visible)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_histogramGraph->setVisible(visible);
Toshihiro Shimizu 890ddd
	m_colorBar->setVisible(visible);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// ComboHistoRGBLabel
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
ComboHistoRGBLabel::ComboHistoRGBLabel(QColor color, QWidget *parent)
Toshihiro Shimizu 890ddd
	: QWidget(parent), m_color(color)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setFixedSize(COMBOHIST_RESOLUTION_W, 30);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ComboHistoRGBLabel::setColorAndUpdate(QColor color)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_color == color)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_color = color;
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ComboHistoRGBLabel::paintEvent(QPaintEvent *pe)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPainter p(this);
Toshihiro Shimizu 890ddd
	p.setPen(Qt::black);
Toshihiro Shimizu 890ddd
	QRect bgRect = QRect(rect().left(), rect().top(), rect().width() - 1, rect().height() - 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_color.alpha() == 0) {
Toshihiro Shimizu 890ddd
		p.setBrush(Qt::gray);
Toshihiro Shimizu 890ddd
		p.drawRect(bgRect);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//iwsw commented out temporarily
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
	if(Preferences::instance()->isDoColorCorrectionByUsing3DLutEnabled())
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		QColor convertedColor(m_color);
Toshihiro Shimizu 890ddd
		Ghibli3DLutConverter::instance()->convert(convertedColor);
Toshihiro Shimizu 890ddd
		p.setBrush(convertedColor);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
	*/
Toshihiro Shimizu 890ddd
	p.setBrush(m_color);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p.drawRect(bgRect);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//white text for dark color, black text for light color
Toshihiro Shimizu 890ddd
	int val = m_color.red() * 30 +
Toshihiro Shimizu 890ddd
			  m_color.green() * 59 +
Toshihiro Shimizu 890ddd
			  m_color.blue() * 11;
Toshihiro Shimizu 890ddd
	if (val < 12800)
Toshihiro Shimizu 890ddd
		p.setPen(Qt::white);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		p.setPen(Qt::black);
Toshihiro Shimizu 890ddd
	p.setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p.drawText(rect(), Qt::AlignCenter,
Toshihiro Shimizu 890ddd
			   QString("R:%1 G:%2 B:%3").arg(m_color.red()).arg(m_color.green()).arg(m_color.blue()));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// ComboHistogram
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ComboHistogram::ComboHistogram(QWidget *parent)
Toshihiro Shimizu 890ddd
	: QWidget(parent), m_raster(0), m_palette(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int chan = 0; chan < 4; chan++)
Toshihiro Shimizu 890ddd
		m_histograms[chan] = new ChannelHisto(chan, &m_channelValue[chan][0], this);
Toshihiro Shimizu 890ddd
	m_histograms[4] = new ChannelHisto(4, &m_channelValue[0][0], this);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//RGB label
Toshihiro Shimizu 890ddd
	m_rgbLabel = new ComboHistoRGBLabel(QColor(128, 128, 128), this);
Toshihiro Shimizu 890ddd
	m_rgbLabel->setStyleSheet("font-size: 18px;");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_rectAverageRgbLabel = new ComboHistoRGBLabel(QColor(128, 128, 128), this);
Toshihiro Shimizu 890ddd
	m_rectAverageRgbLabel->setStyleSheet("font-size: 18px;");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_xPosLabel = new QLabel("", this);
Toshihiro Shimizu 890ddd
	m_yPosLabel = new QLabel("", this);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//layout
Toshihiro Shimizu 890ddd
	QVBoxLayout *mainLayout = new QVBoxLayout();
Toshihiro Shimizu 890ddd
	mainLayout->setMargin(5);
Toshihiro Shimizu 890ddd
	mainLayout->setSpacing(5);
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		mainLayout->addWidget(m_histograms[4]); //RGB
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		mainLayout->addWidget(new QLabel("Picked Color", this), 0, Qt::AlignLeft | Qt::AlignVCenter);
Toshihiro Shimizu 890ddd
		mainLayout->addWidget(m_rgbLabel, 0, Qt::AlignCenter);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		mainLayout->addWidget(new QLabel("Average Color (Ctrl + Drag)", this), 0, Qt::AlignLeft | Qt::AlignVCenter);
Toshihiro Shimizu 890ddd
		mainLayout->addWidget(m_rectAverageRgbLabel, 0, Qt::AlignCenter);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		QGridLayout *infoParamLay = new QGridLayout();
Toshihiro Shimizu 890ddd
		infoParamLay->setHorizontalSpacing(3);
Toshihiro Shimizu 890ddd
		infoParamLay->setVerticalSpacing(5);
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			infoParamLay->addWidget(new QLabel("X:", this), 0, 0, Qt::AlignRight | Qt::AlignVCenter);
Toshihiro Shimizu 890ddd
			infoParamLay->addWidget(m_xPosLabel, 0, 1, Qt::AlignLeft | Qt::AlignVCenter);
Toshihiro Shimizu 890ddd
			infoParamLay->addWidget(new QLabel("Y:", this), 1, 0, Qt::AlignRight | Qt::AlignVCenter);
Toshihiro Shimizu 890ddd
			infoParamLay->addWidget(m_yPosLabel, 1, 1, Qt::AlignLeft | Qt::AlignVCenter);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		mainLayout->addLayout(infoParamLay, 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		mainLayout->addWidget(m_histograms[0]); //R
Toshihiro Shimizu 890ddd
		mainLayout->addWidget(m_histograms[1]); //G
Toshihiro Shimizu 890ddd
		mainLayout->addWidget(m_histograms[2]); //B
Toshihiro Shimizu 890ddd
		mainLayout->addWidget(m_histograms[3]); //A
Toshihiro Shimizu 890ddd
		mainLayout->addStretch(1);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	setLayout(mainLayout);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_rectAverageRgbLabel->setColorAndUpdate(Qt::transparent);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ComboHistogram::~ComboHistogram()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	memset(m_channelValue, 0, sizeof m_channelValue);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ComboHistogram::setRaster(const TRasterP &raster, const TPaletteP &palette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (palette.getPointer())
Toshihiro Shimizu 890ddd
		m_palette = palette;
Toshihiro Shimizu 890ddd
	m_raster = raster;
Toshihiro Shimizu 890ddd
	computeChannelsValue();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < 5; i++)
Toshihiro Shimizu 890ddd
		m_histograms[i]->refleshValue();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ComboHistogram::computeChannelsValue()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	memset(m_channelValue, 0, sizeof m_channelValue);
Toshihiro Shimizu 890ddd
	if (!m_raster.getPointer())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	TRasterCM32P cmRaster = m_raster;
Toshihiro Shimizu 890ddd
	bool isCmRaster = !!cmRaster;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRaster64P raster64 = m_raster;
Toshihiro Shimizu 890ddd
	bool is64bit = !!raster64;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int lx = m_raster->getLx();
Toshihiro Shimizu 890ddd
	int ly = m_raster->getLy();
Toshihiro Shimizu 890ddd
	if (lx > 1 && ly > 1) {
Toshihiro Shimizu 890ddd
		int i, j;
Toshihiro Shimizu 890ddd
		if (is64bit) {
Toshihiro Shimizu 890ddd
			for (j = 0; j < ly; j++) {
Toshihiro Shimizu 890ddd
				TPixel64 *pix_64 = raster64->pixels(j);
Toshihiro Shimizu 890ddd
				for (i = 0; i < lx; i++, pix_64++) {
Toshihiro Shimizu 890ddd
					int mValue = (int)byteFromUshort(pix_64->m);
Toshihiro Shimizu 890ddd
					if (mValue != 0) {
Toshihiro Shimizu 890ddd
						++m_channelValue[0][(int)byteFromUshort(pix_64->r)];
Toshihiro Shimizu 890ddd
						++m_channelValue[1][(int)byteFromUshort(pix_64->g)];
Toshihiro Shimizu 890ddd
						++m_channelValue[2][(int)byteFromUshort(pix_64->b)];
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					++m_channelValue[3][mValue];
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else if (isCmRaster) {
Toshihiro Shimizu 890ddd
			assert(m_palette);
Toshihiro Shimizu 890ddd
			for (j = 0; j < ly; j++) {
Toshihiro Shimizu 890ddd
				TPixelCM32 *pix_cm = cmRaster->pixels(j);
Toshihiro Shimizu 890ddd
				for (i = 0; i < lx; i++, pix_cm++) {
Toshihiro Shimizu 890ddd
					int styleId = pix_cm->getTone() < 127 ? pix_cm->getInk() : pix_cm->getPaint();
Toshihiro Shimizu 890ddd
					TColorStyle *colorStyle = m_palette->getStyle(styleId);
Toshihiro Shimizu 890ddd
					if (!colorStyle)
Toshihiro Shimizu 890ddd
						continue;
Toshihiro Shimizu 890ddd
					TPixel color = colorStyle->getAverageColor();
Toshihiro Shimizu 890ddd
					int mValue = color.m;
Toshihiro Shimizu 890ddd
					if (mValue != 0) {
Toshihiro Shimizu 890ddd
						++m_channelValue[0][color.r];
Toshihiro Shimizu 890ddd
						++m_channelValue[1][color.g];
Toshihiro Shimizu 890ddd
						++m_channelValue[2][color.b];
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					++m_channelValue[3][color.m];
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else //8bpc raster
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			for (j = 0; j < ly; j++) {
Toshihiro Shimizu 890ddd
				TPixel *pix = (TPixel *)m_raster->getRawData(0, j);
Toshihiro Shimizu 890ddd
				for (i = 0; i < lx; i++, pix++) {
Toshihiro Shimizu 890ddd
					int mValue = pix->m;
Toshihiro Shimizu 890ddd
					if (mValue != 0) {
Toshihiro Shimizu 890ddd
						++m_channelValue[0][pix->r];
Toshihiro Shimizu 890ddd
						++m_channelValue[1][pix->g];
Toshihiro Shimizu 890ddd
						++m_channelValue[2][pix->b];
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					++m_channelValue[3][pix->m];
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ComboHistogram::updateInfo(const TPixel32 &pix, const TPointD &imagePos)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (pix == TPixel32::Transparent) {
Toshihiro Shimizu 890ddd
		m_histograms[0]->showCurrentChannelValue(-1);
Toshihiro Shimizu 890ddd
		m_histograms[1]->showCurrentChannelValue(-1);
Toshihiro Shimizu 890ddd
		m_histograms[2]->showCurrentChannelValue(-1);
Toshihiro Shimizu 890ddd
		m_rgbLabel->setColorAndUpdate(Qt::transparent);
Toshihiro Shimizu 890ddd
		m_xPosLabel->setText("");
Toshihiro Shimizu 890ddd
		m_yPosLabel->setText("");
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		//show picked color's channel values
Toshihiro Shimizu 890ddd
		m_histograms[0]->showCurrentChannelValue((int)pix.r);
Toshihiro Shimizu 890ddd
		m_histograms[1]->showCurrentChannelValue((int)pix.g);
Toshihiro Shimizu 890ddd
		m_histograms[2]->showCurrentChannelValue((int)pix.b);
Toshihiro Shimizu 890ddd
		m_rgbLabel->setColorAndUpdate(QColor((int)pix.r, (int)pix.g, (int)pix.b));
Toshihiro Shimizu 890ddd
		m_xPosLabel->setText(QString::number(tround(imagePos.x)));
Toshihiro Shimizu 890ddd
		m_yPosLabel->setText(QString::number(tround(imagePos.y)));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ComboHistogram::updateAverageColor(const TPixel32 &pix)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (pix == TPixel32::Transparent) {
Toshihiro Shimizu 890ddd
		m_rectAverageRgbLabel->setColorAndUpdate(Qt::transparent);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		m_rectAverageRgbLabel->setColorAndUpdate(QColor((int)pix.r, (int)pix.g, (int)pix.b));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------