Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #15 from camrein/grayscale
Browse files Browse the repository at this point in the history
Grayscale
  • Loading branch information
camrein authored Feb 17, 2017
2 parents 7e8752e + 30953ad commit 94b23f4
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 26 deletions.
71 changes: 69 additions & 2 deletions EzGraverUi/imagelabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

#include <QPainter>

#include <algorithm>

#include "ezgraver.h"

ImageLabel::ImageLabel(QWidget* parent) : ClickLabel{parent}, _image{}, _flags{Qt::DiffuseDither} {}
ImageLabel::ImageLabel(QWidget* parent) : ClickLabel{parent}, _image{}, _flags{Qt::DiffuseDither},
_grayscale{false}, _layer{0}, _layerCount{3} {}

ImageLabel::~ImageLabel() {}

QImage ImageLabel::image() const {
Expand All @@ -28,6 +32,36 @@ void ImageLabel::setConversionFlags(Qt::ImageConversionFlags const& flags) {
emit conversionFlagsChanged(flags);
}

bool ImageLabel::grayscale() const {
return _grayscale;
}

void ImageLabel::setGrayscale(bool const& enabled) {
_grayscale = enabled;
updateDisplayedImage();
emit grayscaleChanged(enabled);
}

int ImageLabel::layer() const {
return _layer;
}

void ImageLabel::setLayer(int const& layer) {
_layer = layer;
updateDisplayedImage();
emit layerChanged(layer);
}

int ImageLabel::layerCount() const {
return _layerCount;
}

void ImageLabel::setLayerCount(int const& layerCount) {
_layerCount = layerCount;
updateDisplayedImage();
emit layerCountChanged(layerCount);
}

void ImageLabel::updateDisplayedImage() {
if(!imageLoaded()) {
return;
Expand All @@ -39,7 +73,40 @@ void ImageLabel::updateDisplayedImage() {
QPainter painter{&image};
painter.drawImage(0, 0, _image.scaled(image.size()));

setPixmap(QPixmap::fromImage(image.convertToFormat(QImage::Format_Mono, _flags)));
auto rendered = _grayscale
? QPixmap::fromImage(_createGrayscaleImage(image))
: QPixmap::fromImage(image.convertToFormat(QImage::Format_Mono, _flags));
setPixmap(rendered);
}

QImage ImageLabel::_createGrayscaleImage(QImage const& original) const {
auto colorTable = _createColorTable();
QImage grayed = original.convertToFormat(QImage::Format_Indexed8, colorTable, _flags);
if(_layer == 0) {
return grayed;
}

auto visibleLayer = _layer-1;
int i{0};
std::transform(colorTable.begin(), colorTable.end(), colorTable.begin(), [&i,visibleLayer](QRgb) {
return i++ == visibleLayer ? qRgb(0, 0, 0) : qRgb(255, 255, 255);
});
grayed.setColorTable(colorTable);

return grayed.convertToFormat(QImage::Format_Mono, _flags);
}

QVector<QRgb> ImageLabel::_createColorTable() const {
QVector<QRgb> colorTable(_layerCount - 1);

int i{0};
std::generate(colorTable.begin(), colorTable.end(), [this, &i] {
int gray = (256 / (_layerCount-1)) * (i++);
return qRgb(gray, gray, gray);
});
colorTable.push_back(qRgb(255, 255, 255));

return colorTable;
}

bool ImageLabel::imageLoaded() const {
Expand Down
71 changes: 71 additions & 0 deletions EzGraverUi/imagelabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class ImageLabel : public ClickLabel {
Q_OBJECT
Q_PROPERTY(QImage image READ image WRITE setImage NOTIFY imageChanged)
Q_PROPERTY(Qt::ImageConversionFlags conversionFlags READ conversionFlags WRITE setConversionFlags NOTIFY conversionFlagsChanged)
Q_PROPERTY(bool grayscale READ grayscale WRITE setGrayscale NOTIFY grayscaleChanged)
Q_PROPERTY(int layer READ layer WRITE setLayer NOTIFY layerChanged)
Q_PROPERTY(int layerCount READ layerCount WRITE setLayerCount NOTIFY layerCountChanged)
Q_PROPERTY(bool imageLoaded READ imageLoaded NOTIFY imageLoadedChanged)

public:
Expand Down Expand Up @@ -53,6 +56,48 @@ class ImageLabel : public ClickLabel {
*/
void setConversionFlags(Qt::ImageConversionFlags const& flags);

/*!
* Gets if grayscale is enabled.
*
* \return \c true if grayscale is enabled.
*/
bool grayscale() const;

/*!
* Enables/disables the grayscale setting of the image.
*
* \param enabled \c true if the image should be handled as grayscale.
*/
void setGrayscale(bool const& enabled);

/*!
* Gets the currently selected grayscale layer.
*
* \return The currently selected grayscale layer.
*/
int layer() const;

/*!
* Changes the displayed grayscale layer to the provided one.
*
* \param layer The layer to display.
*/
void setLayer(int const& layer);

/*!
* Gets the currently used number of layers.
*
* \return The currently user number of layers.
*/
int layerCount() const;

/*!
* Changes the used number of layers.
*
* \param layer The number of layers.
*/
void setLayerCount(int const& layerCount);

/*!
* Gets if an image has been loaded.
*
Expand Down Expand Up @@ -82,6 +127,27 @@ class ImageLabel : public ClickLabel {
*/
void conversionFlagsChanged(Qt::ImageConversionFlags const& flags);

/*!
* Fired as soon as grayscale has been enabled or disabled.
*
* \param enabled \c true if grayscale has been enabled.
*/
void grayscaleChanged(bool const& enabled);

/*!
* Fired as soon as the layer of the grayscale image was changed.
*
* \param layer The currently selected layer.
*/
void layerChanged(int const& layer);

/*!
* Fired as soon as the layer the number of layers changed.
*
* \param layerCount The currently active number of layers.
*/
void layerCountChanged(int const& layerCount);

/*!
* Fired as soon as an image has been loaded.
*
Expand All @@ -91,8 +157,13 @@ class ImageLabel : public ClickLabel {
private:
QImage _image;
Qt::ImageConversionFlags _flags;
bool _grayscale;
int _layer;
int _layerCount;

void updateDisplayedImage();
QImage _createGrayscaleImage(QImage const& original) const;
QVector<QRgb> _createColorTable() const;
};

#endif // IMAGELABEL_H
15 changes: 13 additions & 2 deletions EzGraverUi/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,24 @@ void MainWindow::_initBindings() {
connect(this, &MainWindow::connectedChanged, _ui->start, &QPushButton::setEnabled);
connect(this, &MainWindow::connectedChanged, _ui->pause, &QPushButton::setEnabled);
connect(this, &MainWindow::connectedChanged, _ui->reset, &QPushButton::setEnabled);
connect(_ui->conversionFlags, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int index) {
connect(_ui->conversionFlags, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int index) {
_ui->image->setConversionFlags(static_cast<Qt::ImageConversionFlags>(_ui->conversionFlags->itemData(index).toInt()));
});

auto uploadEnabled = [this] { _ui->upload->setEnabled(_ui->image->imageLoaded() && _connected); };
connect(_ui->layered, &QCheckBox::toggled, _ui->selectedLayer, &QSpinBox::setEnabled);
connect(_ui->layered, &QCheckBox::toggled, _ui->layerCount, &QSpinBox::setEnabled);
connect(_ui->layered, &QCheckBox::toggled, _ui->image, &ImageLabel::setGrayscale);
connect(_ui->layerCount, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), _ui->image, &ImageLabel::setLayerCount);
connect(_ui->layerCount, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), _ui->selectedLayer, &QSpinBox::setMaximum);
connect(_ui->selectedLayer, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), _ui->image, &ImageLabel::setLayer);

auto uploadEnabled = [this] {
_ui->upload->setEnabled(_ui->image->imageLoaded() && _connected && (!_ui->layered->isChecked() || _ui->selectedLayer->value() > 0));
};
connect(this, &MainWindow::connectedChanged, uploadEnabled);
connect(_ui->image, &ImageLabel::imageLoadedChanged, uploadEnabled);
connect(_ui->selectedLayer, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), uploadEnabled);
connect(_ui->layered, &QCheckBox::toggled, uploadEnabled);
}

void MainWindow::_initConversionFlags() {
Expand Down
111 changes: 97 additions & 14 deletions EzGraverUi/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,94 @@
</item>
<item>
<layout class="QGridLayout" name="gridLayout_2">
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Layer Count</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="layerCount">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimum">
<number>2</number>
</property>
<property name="value">
<number>3</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Current Layer</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="selectedLayer">
<property name="enabled">
<bool>false</bool>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>3</number>
</property>
<property name="value">
<number>0</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Burn time</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Conversion flags</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QCheckBox" name="layered">
<property name="text">
<string>Layered</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSlider" name="burnTime">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
Expand All @@ -68,6 +152,12 @@
</item>
<item row="1" column="2">
<widget class="QLabel" name="burnTimeLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>25</width>
Expand All @@ -83,19 +173,12 @@
</widget>
</item>
<item row="0" column="1" colspan="2">
<widget class="QComboBox" name="conversionFlags"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Conversion flags</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Burn time</string>
<widget class="QComboBox" name="conversionFlags">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
Expand Down
Loading

0 comments on commit 94b23f4

Please sign in to comment.