Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcolorstyles.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qvboxlayout></qvboxlayout>
Toshihiro Shimizu 890ddd
#include <qcombobox></qcombobox>
Toshihiro Shimizu 890ddd
#include <qpushbutton></qpushbutton>
Toshihiro Shimizu 890ddd
#include <qicon></qicon>
Toshihiro Shimizu 890ddd
#include <qstackedwidget></qstackedwidget>
Toshihiro Shimizu 890ddd
#include <qpainter></qpainter>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/histogram.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const int HistogramGraph::drawMargin = 10;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*****************************************************************************
Toshihiro Shimizu 890ddd
//    Local namespace
Toshihiro Shimizu 890ddd
//*****************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void computeGreyValues(const TRasterGR8P &ras, int *greyValues)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int y, lx = ras->getLx(), ly = ras->getLy();
Toshihiro Shimizu 890ddd
	TPixelGR8 *pix, *endPix;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (y = 0; y < ly; ++y)
Toshihiro Shimizu 890ddd
		for (pix = ras->pixels(y), endPix = pix + lx; pix < endPix; ++pix)
Toshihiro Shimizu 890ddd
			++greyValues[pix->value];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void computeGreyValues(const TRasterGR16P &ras, int *greyValues)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int y, lx = ras->getLx(), ly = ras->getLy();
Toshihiro Shimizu 890ddd
	TPixelGR16 *pix, *endPix;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (y = 0; y < ly; ++y)
Toshihiro Shimizu 890ddd
		for (pix = ras->pixels(y), endPix = pix + lx; pix < endPix; ++pix)
Toshihiro Shimizu 890ddd
			++greyValues[pix->value >> 8];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void computeRGBMValues32(const TRaster32P &ras, int (*rgbmValues)[256])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int y, lx = ras->getLx(), ly = ras->getLy();
Toshihiro Shimizu 890ddd
	TPixel32 *pix, *endPix;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (y = 0; y < ly; ++y)
Toshihiro Shimizu 890ddd
		for (pix = ras->pixels(y), endPix = pix + lx; pix < endPix; ++pix) {
Toshihiro Shimizu 890ddd
			//NOTE: The insertion order is related to the way channels are
Toshihiro Shimizu 890ddd
			//displayed inside the histogram's QStackedWidget instance
Toshihiro Shimizu 890ddd
			if (pix->m) {
Toshihiro Shimizu 890ddd
				++rgbmValues[0][pix->r];
Toshihiro Shimizu 890ddd
				++rgbmValues[1][pix->g];
Toshihiro Shimizu 890ddd
				++rgbmValues[2][pix->b];
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			++rgbmValues[3][pix->m];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void computeRGBMValues64(const TRaster64P &ras, int (*rgbmValues)[256])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int y, lx = ras->getLx(), ly = ras->getLy();
Toshihiro Shimizu 890ddd
	TPixel64 *pix, *endPix;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (y = 0; y < ly; ++y)
Toshihiro Shimizu 890ddd
		for (pix = ras->pixels(y), endPix = pix + lx; pix < endPix; ++pix) {
Toshihiro Shimizu 890ddd
			//NOTE: The insertion order is related to the way channels are
Toshihiro Shimizu 890ddd
			//displayed inside the histogram's QStackedWidget instance
Toshihiro Shimizu 890ddd
			if (pix->m) {
Toshihiro Shimizu 890ddd
				++rgbmValues[0][pix->r >> 8];
Toshihiro Shimizu 890ddd
				++rgbmValues[1][pix->g >> 8];
Toshihiro Shimizu 890ddd
				++rgbmValues[2][pix->b >> 8];
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			++rgbmValues[3][pix->m >> 8];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void computeRGBMValues(const TRasterCM32P &ras, TPaletteP pal, int (*rgbmValues)[256])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(pal);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int y, lx = ras->getLx(), ly = ras->getLy();
Toshihiro Shimizu 890ddd
	TPixelCM32 *pix, *endPix;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int styleId;
Toshihiro Shimizu 890ddd
	TColorStyle *colorStyle;
Toshihiro Shimizu 890ddd
	TPixel32 color;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (y = 0; y < ly; ++y)
Toshihiro Shimizu 890ddd
		for (pix = ras->pixels(y), endPix = pix + lx; pix < endPix; ++pix) {
Toshihiro Shimizu 890ddd
			styleId = (pix->getTone() < 127) ? pix->getInk() : pix->getPaint();
Toshihiro Shimizu 890ddd
			colorStyle = pal->getStyle(styleId);
Toshihiro Shimizu 890ddd
			if (!colorStyle)
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			color = colorStyle->getAverageColor();
Toshihiro Shimizu 890ddd
			if (color.m) {
Toshihiro Shimizu 890ddd
				++rgbmValues[0][color.r];
Toshihiro Shimizu 890ddd
				++rgbmValues[1][color.g];
Toshihiro Shimizu 890ddd
				++rgbmValues[2][color.b];
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			++rgbmValues[3][color.m];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void computeRGB(int (*channelValues)[256], int *rgbValues)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < 256; ++i)
Toshihiro Shimizu 890ddd
		rgbValues[i] = channelValues[0][i] + channelValues[1][i] + channelValues[2][i];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void computeRGBM(int (*channelValues)[256], int *rgbmValues)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < 256; ++i)
Toshihiro Shimizu 890ddd
		rgbmValues[i] =
Toshihiro Shimizu 890ddd
			channelValues[0][i] + channelValues[1][i] +
Toshihiro Shimizu 890ddd
			channelValues[2][i] + channelValues[3][i];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// HistogramGraph
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
HistogramGraph::HistogramGraph(QWidget *parent, QColor color)
Toshihiro Shimizu 890ddd
	: QWidget(parent), m_color(color), m_height(120), m_values(0), m_logScale(false)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_color.alpha() == 0)
Toshihiro Shimizu 890ddd
		m_color = Qt::black;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	setMinimumWidth(256 + drawMargin * 2 + 2);
Toshihiro Shimizu 890ddd
	setMinimumHeight(m_height + drawMargin);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
HistogramGraph::~HistogramGraph()
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 HistogramGraph::setAlphaMask(int value)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_color.setAlpha(value);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void HistogramGraph::setValues(const int values[])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_values.clear();
Toshihiro Shimizu 890ddd
	m_values.resize(256);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double maxValue = 0;
Toshihiro Shimizu 890ddd
	for (i = 0; i < 256; i++) {
Toshihiro Shimizu 890ddd
		int count = m_values[i] = values[i];
Toshihiro Shimizu 890ddd
		if (maxValue < count)
Toshihiro Shimizu 890ddd
			maxValue = count;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_viewValues.clear();
Toshihiro Shimizu 890ddd
	m_logViewValues.clear();
Toshihiro Shimizu 890ddd
	m_viewValues.resize(256);
Toshihiro Shimizu 890ddd
	m_logViewValues.resize(256);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double logMaxValue = log(1.0 + maxValue);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < 256; i++) {
Toshihiro Shimizu 890ddd
		m_viewValues[i] = double(values[i]) * double(m_height) / maxValue;
Toshihiro Shimizu 890ddd
		m_logViewValues[i] = double(log(1.0 + values[i])) * double(m_height) / logMaxValue;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void HistogramGraph::draw(QPainter *p, QPoint translation)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int x0 = translation.x() + drawMargin;
Toshihiro Shimizu 890ddd
	int y0 = translation.y() + drawMargin - 2;
Toshihiro Shimizu 890ddd
	int w = width() - 2 * drawMargin;
Toshihiro Shimizu 890ddd
	int h = m_height + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p->setPen(Qt::NoPen);
Toshihiro Shimizu 890ddd
	p->setBrush(Qt::white);
Toshihiro Shimizu 890ddd
	p->drawRect(x0, y0, w - 1, h);
Toshihiro Shimizu 890ddd
	p->setBrush(Qt::NoBrush);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p->setPen(Qt::gray);
Toshihiro Shimizu 890ddd
	int step = w * 0.25;
Toshihiro Shimizu 890ddd
	int deltaX = x0 + 1 + step;
Toshihiro Shimizu 890ddd
	int y1 = y0 + h;
Toshihiro Shimizu 890ddd
	p->drawLine(deltaX, y0 + 1, deltaX, y1);
Toshihiro Shimizu 890ddd
	deltaX += step;
Toshihiro Shimizu 890ddd
	p->drawLine(deltaX, y0 + 1, deltaX, y1);
Toshihiro Shimizu 890ddd
	deltaX += step;
Toshihiro Shimizu 890ddd
	p->drawLine(deltaX, y0 + 1, deltaX, y1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p->drawRect(x0, y0, w - 1, h);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_values.size() == 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const QVector<int> &viewValues = m_logScale ? m_logViewValues : m_viewValues;</int>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p->setPen(m_color);
Toshihiro Shimizu 890ddd
	w = w - 2;
Toshihiro Shimizu 890ddd
	x0 = x0 + 1;
Toshihiro Shimizu 890ddd
	y1 = y1 - 1;
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	double gap = double(viewValues.size()) / double(w); /*-- 1チャンネル値あたりのグラフの横幅 --*/
Toshihiro Shimizu 890ddd
	for (i = 0; i < w; i++) {
Toshihiro Shimizu 890ddd
		int j = i * gap;
Toshihiro Shimizu 890ddd
		assert(j >= 0 && j < viewValues.size());
Toshihiro Shimizu 890ddd
		int v = viewValues[j];
Toshihiro Shimizu 890ddd
		if (v <= 0)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		int x = x0 + i;
Toshihiro Shimizu 890ddd
		p->drawLine(x, y1 - v + 1, x, y1);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void HistogramGraph::paintEvent(QPaintEvent *event)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPainter p(this);
Toshihiro Shimizu 890ddd
	draw(&p);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// ChannelBar
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ChannelBar::ChannelBar(QWidget *parent, QColor color, bool isHorizontal)
Toshihiro Shimizu 890ddd
	: QWidget(parent), m_color(color), m_isHorizontal(isHorizontal), m_colorBarLength(13), m_drawNumbers(true)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int d = 256 + HistogramGraph::drawMargin * 2 + 2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_isHorizontal)
Toshihiro Shimizu 890ddd
		setMinimumWidth(d);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		setFixedHeight(d); //La barra verticale ha la size fissa
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	setDrawNumbers(m_drawNumbers);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (color == Qt::black)
Toshihiro Shimizu 890ddd
		m_color = Qt::white;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ChannelBar::~ChannelBar()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ChannelBar::setDrawNumbers(bool onOff)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_isHorizontal)
Toshihiro Shimizu 890ddd
		setFixedHeight(onOff ? m_colorBarLength * 2 + 2 : m_colorBarLength + 2);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		setFixedWidth(onOff ? m_colorBarLength + 20 + 2 : m_colorBarLength + 2);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ChannelBar::draw(QPainter *p, QPoint translation)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//Calcolo i parametri necessari al draw a seconda del caso in cui mi trovo.
Toshihiro Shimizu 890ddd
	int space = HistogramGraph::drawMargin;
Toshihiro Shimizu 890ddd
	int w, h, x0, y0;
Toshihiro Shimizu 890ddd
	QRect rect;
Toshihiro Shimizu 890ddd
	QPoint initialPoint, finalPoint, delta;
Toshihiro Shimizu 890ddd
	QColor initialColor, finalColor;
Toshihiro Shimizu 890ddd
	if (m_isHorizontal) {
Toshihiro Shimizu 890ddd
		w = width() - 2 * HistogramGraph::drawMargin;
Toshihiro Shimizu 890ddd
		h = m_colorBarLength;
Toshihiro Shimizu 890ddd
		x0 = translation.x() + space;
Toshihiro Shimizu 890ddd
		y0 = translation.y();
Toshihiro Shimizu 890ddd
		initialPoint = QPoint(x0, 0);
Toshihiro Shimizu 890ddd
		finalPoint = QPoint(w + x0, 0);
Toshihiro Shimizu 890ddd
		initialColor = Qt::black;
Toshihiro Shimizu 890ddd
		finalColor = m_color;
Toshihiro Shimizu 890ddd
		rect = QRect(x0 - space, y0 + h, 20, 20);
Toshihiro Shimizu 890ddd
		delta = QPoint(w / 4, 0);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		w = m_colorBarLength;
Toshihiro Shimizu 890ddd
		h = height() - 2 * HistogramGraph::drawMargin;
Toshihiro Shimizu 890ddd
		x0 = translation.x() + width() - w;
Toshihiro Shimizu 890ddd
		y0 = translation.y() + space;
Toshihiro Shimizu 890ddd
		initialPoint = QPoint(0, y0);
Toshihiro Shimizu 890ddd
		finalPoint = QPoint(0, h + y0);
Toshihiro Shimizu 890ddd
		initialColor = m_color;
Toshihiro Shimizu 890ddd
		finalColor = Qt::black;
Toshihiro Shimizu 890ddd
		rect = QRect(0, h + y0 - space, 20, 20);
Toshihiro Shimizu 890ddd
		delta = QPoint(0, -h / 4);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_color == QColor(0, 0, 0, 0)) {
Toshihiro Shimizu 890ddd
		static QPixmap checkboard(":Resources/backg.png");
Toshihiro Shimizu 890ddd
		p->drawTiledPixmap(x0, y0, w, h, checkboard);
Toshihiro Shimizu 890ddd
		QColor color = initialColor;
Toshihiro Shimizu 890ddd
		initialColor = finalColor;
Toshihiro Shimizu 890ddd
		finalColor = color;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QLinearGradient linearGrad(initialPoint, finalPoint);
Toshihiro Shimizu 890ddd
	linearGrad.setColorAt(0, initialColor);
Toshihiro Shimizu 890ddd
	linearGrad.setColorAt(1, finalColor);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p->setBrush(QBrush(linearGrad));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	p->setPen(Qt::black);
Toshihiro Shimizu 890ddd
	p->drawRect(x0, y0, w - 1, h - 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_drawNumbers) {
Toshihiro Shimizu 890ddd
		p->drawText(rect, Qt::AlignCenter, QString::number(0));
Toshihiro Shimizu 890ddd
		rect.translate(delta);
Toshihiro Shimizu 890ddd
		p->drawText(rect, Qt::AlignCenter, QString::number(64));
Toshihiro Shimizu 890ddd
		rect.translate(delta);
Toshihiro Shimizu 890ddd
		p->drawText(rect, Qt::AlignCenter, QString::number(128));
Toshihiro Shimizu 890ddd
		rect.translate(delta);
Toshihiro Shimizu 890ddd
		p->drawText(rect, Qt::AlignCenter, QString::number(192));
Toshihiro Shimizu 890ddd
		rect.translate(delta);
Toshihiro Shimizu 890ddd
		p->drawText(rect, Qt::AlignCenter, QString::number(255));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ChannelBar::paintEvent(QPaintEvent *event)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QPainter p(this);
Toshihiro Shimizu 890ddd
	draw(&p);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// HistogramView
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
HistogramView::HistogramView(QWidget *parent, QColor color)
Toshihiro Shimizu 890ddd
	: QWidget(parent), m_drawnWidget(parent)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setMinimumWidth(256 + 10 * 2 + 2);  //10 == margin of internal widget
Toshihiro Shimizu 890ddd
	setMinimumHeight(120 + 10 * 2 + 2); //10 == margin of internal widget
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QVBoxLayout *mainLayout = new QVBoxLayout(this);
Toshihiro Shimizu 890ddd
	mainLayout->setMargin(0);
Toshihiro Shimizu 890ddd
	mainLayout->setSpacing(7);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_histogramGraph = new HistogramGraph(this, color);
Toshihiro Shimizu 890ddd
	m_colorBar = new ChannelBar(this, color);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	mainLayout->addWidget(m_histogramGraph);
Toshihiro Shimizu 890ddd
	mainLayout->addWidget(m_colorBar);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
Toshihiro Shimizu 890ddd
	setLayout(mainLayout);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void HistogramView::setDrawnWidget(QWidget *widget)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_drawnWidget = widget;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void HistogramView::setValues(const int values[])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_histogramGraph->setValues(values);
Toshihiro Shimizu 890ddd
	m_drawnWidget->update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void HistogramView::draw(QPainter *painter, QPoint translation)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_histogramGraph->draw(painter, translation);
Toshihiro Shimizu 890ddd
	m_colorBar->draw(painter, QPoint(translation.x(), translation.y() + m_histogramGraph->getHeight() + 17));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
HistogramView::~HistogramView()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// Histograms
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Histograms::Histograms(QWidget *parent, bool rgba)
Toshihiro Shimizu 890ddd
	: QStackedWidget(parent), m_raster(0), m_palette(0), m_computeAlsoRGBA(rgba), m_channelsCount(rgba ? 6 : 5)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	HistogramView *histogramViews[6];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int h = 0;
Toshihiro Shimizu 890ddd
	if (m_computeAlsoRGBA)
Toshihiro Shimizu 890ddd
		histogramViews[h++] = new HistogramView(this);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	histogramViews[h++] = new HistogramView(this);
Toshihiro Shimizu 890ddd
	histogramViews[h++] = new HistogramView(this, Qt::red);
Toshihiro Shimizu 890ddd
	histogramViews[h++] = new HistogramView(this, Qt::green);
Toshihiro Shimizu 890ddd
	histogramViews[h++] = new HistogramView(this, Qt::blue);
Toshihiro Shimizu 890ddd
	histogramViews[h++] = new HistogramView(this, QColor(0, 0, 0, 0));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < m_channelsCount; ++i)
Toshihiro Shimizu 890ddd
		addWidget(histogramViews[i]);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Histograms::~Histograms()
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 Histograms::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
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < count(); i++)
Toshihiro Shimizu 890ddd
		getHistogramView(i)->setValues(m_channelValue[i]);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Histograms::computeChannelsValue()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_channelsCount = m_computeAlsoRGBA ? 6 : 5;
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
Toshihiro Shimizu 890ddd
	int(*rgbChannelValues)[256] = m_channelValue + (m_computeAlsoRGBA ? 1 : 0);
Toshihiro Shimizu 890ddd
	int(*channelValues)[256] = rgbChannelValues + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterCM32P cmRaster = m_raster;
Toshihiro Shimizu 890ddd
	bool isCmRaster = !!cmRaster;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TRaster32P ras32(m_raster);
Toshihiro Shimizu 890ddd
		if (ras32) {
Toshihiro Shimizu 890ddd
			::computeRGBMValues32(ras32, channelValues);
Toshihiro Shimizu 890ddd
			goto computeChannelsSums;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TRaster64P ras64(m_raster);
Toshihiro Shimizu 890ddd
		if (ras64) {
Toshihiro Shimizu 890ddd
			::computeRGBMValues64(ras64, channelValues);
Toshihiro Shimizu 890ddd
			goto computeChannelsSums;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TRasterGR8P rasGR8(m_raster);
Toshihiro Shimizu 890ddd
		if (rasGR8) {
Toshihiro Shimizu 890ddd
			m_channelsCount = 1;
Toshihiro Shimizu 890ddd
			::computeGreyValues(rasGR8, m_channelValue[0]);
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TRasterGR16P rasGR16(m_raster);
Toshihiro Shimizu 890ddd
		if (rasGR16) {
Toshihiro Shimizu 890ddd
			m_channelsCount = 1;
Toshihiro Shimizu 890ddd
			::computeGreyValues(rasGR16, m_channelValue[0]);
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
computeChannelsSums:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_computeAlsoRGBA)
Toshihiro Shimizu 890ddd
		::computeRGBM(channelValues, m_channelValue[0]);
Toshihiro Shimizu 890ddd
	::computeRGB(channelValues, *rgbChannelValues);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
HistogramView *Histograms::getHistogramView(int indexType) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	HistogramView *ch = dynamic_cast<histogramview *="">(widget(indexType));</histogramview>
Toshihiro Shimizu 890ddd
	assert(ch);
Toshihiro Shimizu 890ddd
	return ch;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// Histogram
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Histogram::Histogram(QWidget *parent)
Toshihiro Shimizu 890ddd
	: QWidget(parent)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QVBoxLayout *mainLayout = new QVBoxLayout(this);
Toshihiro Shimizu 890ddd
	mainLayout->setMargin(0);
Toshihiro Shimizu 890ddd
	mainLayout->setSpacing(0);
Toshihiro Shimizu 890ddd
	setLayout(mainLayout);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QHBoxLayout *upperLayout = new QHBoxLayout;
Toshihiro Shimizu 890ddd
	mainLayout->addLayout(upperLayout);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_channelsListBox = new QComboBox(this);
Toshihiro Shimizu 890ddd
	m_channelsListBox->setFixedSize(100, 20);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	upperLayout->addSpacing(HistogramGraph::drawMargin);
Toshihiro Shimizu 890ddd
	upperLayout->addWidget(m_channelsListBox);
Toshihiro Shimizu 890ddd
	upperLayout->addStretch(1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QIcon icon;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString normal = QString(":Resources/histograms.png");
Toshihiro Shimizu 890ddd
	QString click = QString(":Resources/histograms_over.png");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	icon.addFile(normal, QSize(), QIcon::Normal, QIcon::Off);
Toshihiro Shimizu 890ddd
	icon.addFile(click, QSize(), QIcon::Normal, QIcon::On);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QPushButton *logScaleButton = new QPushButton(icon, "", this);
Toshihiro Shimizu 890ddd
	logScaleButton->setToolTip(tr("Logarithmic Scale"));
Toshihiro Shimizu 890ddd
	logScaleButton->setFixedSize(20, 20);
Toshihiro Shimizu 890ddd
	logScaleButton->setCheckable(true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	upperLayout->addWidget(logScaleButton);
Toshihiro Shimizu 890ddd
	upperLayout->addSpacing(HistogramGraph::drawMargin);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_histograms = new Histograms(this);
Toshihiro Shimizu 890ddd
	m_histograms->setCurrentIndex(0);
Toshihiro Shimizu 890ddd
	mainLayout->addWidget(m_histograms);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	connect(m_channelsListBox, SIGNAL(currentIndexChanged(int)), m_histograms, SLOT(setCurrentIndex(int)));
Toshihiro Shimizu 890ddd
	connect(logScaleButton, SIGNAL(toggled(bool)), this, SLOT(setLogScale(bool)));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	updateChannelsList();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Histogram::updateChannelsList()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_histograms->channelsCount() != m_channelsListBox->count()) {
Toshihiro Shimizu 890ddd
		QStringList channels;
Toshihiro Shimizu 890ddd
		m_channelsListBox->clear();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (m_histograms->channelsCount() == 1)
Toshihiro Shimizu 890ddd
			channels << "Value";
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			channels << "RGB"
Toshihiro Shimizu 890ddd
					 << "Red"
Toshihiro Shimizu 890ddd
					 << "Green"
Toshihiro Shimizu 890ddd
					 << "Blue"
Toshihiro Shimizu 890ddd
					 << "Alpha";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_channelsListBox->addItems(channels);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Histogram::setRaster(const TRasterP &raster, const TPaletteP &palette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_histograms->setRaster(raster, palette);
Toshihiro Shimizu 890ddd
	updateChannelsList();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Histogram::setLogScale(bool onOff)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, count = m_histograms->channelsCount();
Toshihiro Shimizu 890ddd
	for (i = 0; i < count; ++i) {
Toshihiro Shimizu 890ddd
		HistogramGraph *graph = m_histograms->getHistogramView(i)->histogramGraph();
Toshihiro Shimizu 890ddd
		graph->setLogScale(onOff);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	update();
Toshihiro Shimizu 890ddd
}