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