Skip to content

Commit 419e50a

Browse files
authored
Merge pull request #404 from psieg/dithering_setting
Add a setting for dithering, default false.
2 parents 493c763 + 20dd64f commit 419e50a

12 files changed

+518
-404
lines changed

Software/src/AbstractLedDevice.cpp

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ void AbstractLedDevice::setBrightnessCap(int value, bool updateColors) {
4747
setColors(m_colorsSaved);
4848
}
4949

50+
void AbstractLedDevice::setDitheringEnabled(bool value, bool updateColors) {
51+
m_isDitheringEnabled = value;
52+
if (updateColors)
53+
setColors(m_colorsSaved);
54+
}
55+
5056
void AbstractLedDevice::setLedMilliAmps(const int value, const bool updateColors) {
5157
m_ledMilliAmps = value;
5258
if (updateColors)
@@ -90,6 +96,7 @@ void AbstractLedDevice::updateDeviceSettings()
9096
setBrightnessCap(Settings::getDeviceBrightnessCap(), false);
9197
setLuminosityThreshold(Settings::getLuminosityThreshold(), false);
9298
setMinimumLuminosityThresholdEnabled(Settings::isMinimumLuminosityEnabled(), false);
99+
setDitheringEnabled(Settings::isDeviceDitheringEnabled(), false);
93100
updateWBAdjustments(Settings::getLedCoefs(), false);
94101

95102
setColors(m_colorsSaved);
@@ -195,31 +202,33 @@ void AbstractLedDevice::applyDithering(QList<StructRgb>& colors, int colorDepth)
195202
colors[i].g = (double)colors[i].g / k;
196203
colors[i].b = (double)colors[i].b / k;
197204

198-
carryR += tempR - (double)colors[i].r * k;
199-
carryG += tempG - (double)colors[i].g * k;
200-
carryB += tempB - (double)colors[i].b * k;
205+
if (m_isDitheringEnabled) {
206+
carryR += tempR - (double)colors[i].r * k;
207+
carryG += tempG - (double)colors[i].g * k;
208+
carryB += tempB - (double)colors[i].b * k;
201209

202-
meanIndR += (tempR - (double)colors[i].r * k) * i;
203-
meanIndG += (tempG - (double)colors[i].g * k) * i;
204-
meanIndB += (tempB - (double)colors[i].b * k) * i;
210+
meanIndR += (tempR - (double)colors[i].r * k) * i;
211+
meanIndG += (tempG - (double)colors[i].g * k) * i;
212+
meanIndB += (tempB - (double)colors[i].b * k) * i;
205213

206-
if (carryR >= k) {
207-
int ind = (meanIndR - (carryR - k) * i) / k;
208-
colors[ind].r++;
209-
carryR -= k;
210-
meanIndR = carryR * i;
211-
}
212-
if (carryG >= k) {
213-
int ind = (meanIndG - (carryG - k) * i) / k;
214-
colors[ind].g++;
215-
carryG -= k;
216-
meanIndG = carryG * i;
217-
}
218-
if (carryB >= k) {
219-
int ind = (meanIndB - (carryB - k) * i) / k;
220-
colors[ind].b++;
221-
carryB -= k;
222-
meanIndB = carryB * i;
214+
if (carryR >= k) {
215+
int ind = (meanIndR - (carryR - k) * i) / k;
216+
colors[ind].r++;
217+
carryR -= k;
218+
meanIndR = carryR * i;
219+
}
220+
if (carryG >= k) {
221+
int ind = (meanIndG - (carryG - k) * i) / k;
222+
colors[ind].g++;
223+
carryG -= k;
224+
meanIndG = carryG * i;
225+
}
226+
if (carryB >= k) {
227+
int ind = (meanIndB - (carryB - k) * i) / k;
228+
colors[ind].b++;
229+
carryB -= k;
230+
meanIndB = carryB * i;
231+
}
223232
}
224233
}
225234
}

Software/src/AbstractLedDevice.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public slots:
7272
virtual void setGamma(double value, bool updateColors = true);
7373
virtual void setBrightness(int value, bool updateColors = true);
7474
virtual void setBrightnessCap(int value, bool updateColors = true);
75+
virtual void setDitheringEnabled(bool value, bool updateColors = true);
7576
virtual void setLedMilliAmps(const int value, const bool updateColors = true);
7677
virtual void setPowerSupplyAmps(const double value, const bool updateColors = true);
7778
virtual void setColorSequence(QString value) = 0;
@@ -107,6 +108,7 @@ public slots:
107108
double m_powerSupplyAmps{0.0};
108109
int m_luminosityThreshold;
109110
bool m_isMinimumLuminosityEnabled;
111+
bool m_isDitheringEnabled;
110112

111113
QList<WBAdjustment> m_wbAdjustments;
112114

Software/src/LedDeviceManager.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,21 @@ void LedDeviceManager::setMinimumLuminosityEnabled(bool value)
295295
}
296296
}
297297

298+
void LedDeviceManager::setDitheringEnabled(bool isEnabled) {
299+
DEBUG_MID_LEVEL << Q_FUNC_INFO << isEnabled
300+
<< "Is last command completed:" << m_isLastCommandCompleted;
301+
302+
if (m_isLastCommandCompleted) {
303+
m_cmdTimeoutTimer->start();
304+
m_isLastCommandCompleted = false;
305+
emit ledDeviceSetDitheringEnabled(isEnabled);
306+
}
307+
else {
308+
m_savedDitheringEnabled = isEnabled;
309+
cmdQueueAppend(LedDeviceCommands::SetDitheringEnabled);
310+
}
311+
}
312+
298313
void LedDeviceManager::setColorSequence(QString value)
299314
{
300315
DEBUG_MID_LEVEL << Q_FUNC_INFO << value << "Is last command completed:" << m_isLastCommandCompleted;
@@ -542,7 +557,8 @@ void LedDeviceManager::connectSignalSlotsLedDevice()
542557
connect(this, SIGNAL(ledDeviceSetBrightnessCap(int, bool)), m_ledDevice, SLOT(setBrightnessCap(int, bool)), Qt::QueuedConnection);
543558
connect(this, SIGNAL(ledDeviceSetColorSequence(QString)), m_ledDevice, SLOT(setColorSequence(QString)), Qt::QueuedConnection);
544559
connect(this, SIGNAL(ledDeviceSetLuminosityThreshold(int, bool)), m_ledDevice, SLOT(setLuminosityThreshold(int, bool)), Qt::QueuedConnection);
545-
connect(this, SIGNAL(ledDeviceSetMinimumLuminosityEnabled(bool, bool)), m_ledDevice, SLOT(setMinimumLuminosityThresholdEnabled(bool, bool)), Qt::QueuedConnection);
560+
connect(this, SIGNAL(ledDeviceSetMinimumLuminosityEnabled(bool, bool)), m_ledDevice,SLOT(setMinimumLuminosityThresholdEnabled(bool, bool)), Qt::QueuedConnection);
561+
connect(this, SIGNAL(ledDeviceSetDitheringEnabled(bool)), m_ledDevice, SLOT(setDitheringEnabled(bool)), Qt::QueuedConnection);
546562
connect(this, SIGNAL(ledDeviceRequestFirmwareVersion()), m_ledDevice, SLOT(requestFirmwareVersion()), Qt::QueuedConnection);
547563
connect(this, SIGNAL(ledDeviceUpdateWBAdjustments()), m_ledDevice, SLOT(updateWBAdjustments()), Qt::QueuedConnection);
548564
connect(this, SIGNAL(ledDeviceUpdateDeviceSettings()), m_ledDevice, SLOT(updateDeviceSettings()), Qt::QueuedConnection);
@@ -666,6 +682,11 @@ void LedDeviceManager::cmdQueueProcessNext()
666682
emit ledDeviceSetMinimumLuminosityEnabled(m_savedIsMinimumLuminosityEnabled, m_backlightStatus != Backlight::StatusOff);
667683
break;
668684

685+
case LedDeviceCommands::SetDitheringEnabled:
686+
m_cmdTimeoutTimer->start();
687+
emit ledDeviceSetDitheringEnabled(m_savedDitheringEnabled);
688+
break;
689+
669690
case LedDeviceCommands::RequestFirmwareVersion:
670691
m_cmdTimeoutTimer->start();
671692
emit ledDeviceRequestFirmwareVersion();

Software/src/LedDeviceManager.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class LedDeviceManager : public QObject
6363
void ledDeviceSetBrightnessCap(int value, bool);
6464
void ledDeviceSetLuminosityThreshold(int value, bool);
6565
void ledDeviceSetMinimumLuminosityEnabled(bool, bool);
66+
void ledDeviceSetDitheringEnabled(bool isEnabled);
6667
void ledDeviceSetColorSequence(QString value);
6768
void ledDeviceRequestFirmwareVersion();
6869
void ledDeviceUpdateWBAdjustments();
@@ -86,6 +87,7 @@ public slots:
8687
void setBrightnessCap(int value);
8788
void setLuminosityThreshold(int value);
8889
void setMinimumLuminosityEnabled(bool value);
90+
void setDitheringEnabled(bool isEnabled);
8991
void setColorSequence(QString value);
9092
void requestFirmwareVersion();
9193
void updateWBAdjustments();
@@ -124,6 +126,7 @@ private slots:
124126
int m_savedBrightnessCap;
125127
int m_savedLuminosityThreshold;
126128
bool m_savedIsMinimumLuminosityEnabled;
129+
bool m_savedDitheringEnabled;
127130
QString m_savedColorSequence;
128131

129132
QList<AbstractLedDevice *> m_ledDevices;

Software/src/LightpackApplication.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ void LightpackApplication::startLedDeviceManager()
646646
connect(settings(), SIGNAL(deviceRefreshDelayChanged(int)), m_ledDeviceManager, SLOT(setRefreshDelay(int)), Qt::QueuedConnection);
647647
connect(settings(), SIGNAL(deviceUsbPowerLedDisabledChanged(bool)), m_ledDeviceManager, SLOT(setUsbPowerLedDisabled(bool)), Qt::QueuedConnection);
648648
connect(settings(), SIGNAL(deviceGammaChanged(double)), m_ledDeviceManager, SLOT(setGamma(double)), Qt::QueuedConnection);
649+
connect(settings(), SIGNAL(deviceDitheringEnabledChanged(bool)), m_ledDeviceManager, SLOT(setDitheringEnabled(bool)), Qt::QueuedConnection);
649650
connect(settings(), SIGNAL(deviceBrightnessChanged(int)), m_ledDeviceManager, SLOT(setBrightness(int)), Qt::QueuedConnection);
650651
connect(settings(), SIGNAL(deviceBrightnessCapChanged(int)), m_ledDeviceManager, SLOT(setBrightnessCap(int)), Qt::QueuedConnection);
651652
connect(settings(), SIGNAL(luminosityThresholdChanged(int)), m_ledDeviceManager, SLOT(setLuminosityThreshold(int)), Qt::QueuedConnection);

Software/src/Settings.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ static const QString Brightness = "Device/Brightness";
229229
static const QString BrightnessCap = "Device/BrightnessCap";
230230
static const QString ColorDepth = "Device/ColorDepth";
231231
static const QString Gamma = "Device/Gamma";
232+
static const QString IsDitheringEnabled = "Device/IsDitheringEnabled";
232233
}
233234
// [LED_i]
234235
namespace Led
@@ -289,7 +290,6 @@ Settings::Settings() : QObject(NULL) {
289290
qRegisterMetaType<QColor>("QColor");
290291
qRegisterMetaType<SupportedDevices::DeviceType>("SupportedDevices::DeviceType");
291292
qRegisterMetaType<Lightpack::Mode>("Lightpack::Mode");
292-
293293
}
294294

295295
// Desktop should be initialized before call Settings::Initialize()
@@ -1488,6 +1488,18 @@ void Settings::setDeviceGamma(double gamma)
14881488
m_this->deviceGammaChanged(gamma);
14891489
}
14901490

1491+
bool Settings::isDeviceDitheringEnabled()
1492+
{
1493+
return value(Profile::Key::Device::IsDitheringEnabled).toBool();
1494+
}
1495+
1496+
void Settings::setDeviceDitheringEnabled(bool isEnabled)
1497+
{
1498+
DEBUG_LOW_LEVEL << Q_FUNC_INFO;
1499+
setValue(Profile::Key::Device::IsDitheringEnabled, isEnabled);
1500+
m_this->deviceDitheringEnabledChanged(isEnabled);
1501+
}
1502+
14911503
Grab::GrabberType Settings::getGrabberType()
14921504
{
14931505
DEBUG_LOW_LEVEL << Q_FUNC_INFO;
@@ -2105,12 +2117,13 @@ void Settings::initCurrentProfile(bool isResetDefault)
21052117
#endif
21062118
// [Device]
21072119
setNewOption(Profile::Key::Device::RefreshDelay, Profile::Device::RefreshDelayDefault, isResetDefault);
2108-
setNewOption(Profile::Key::Device::IsUsbPowerLedDisabled, Profile::Device::IsUsbPowerLedDisabled, isResetDefault);
2120+
setNewOption(Profile::Key::Device::IsUsbPowerLedDisabled, Profile::Device::IsUsbPowerLedDisabledDefault, isResetDefault);
21092121
setNewOption(Profile::Key::Device::Brightness, Profile::Device::BrightnessDefault, isResetDefault);
21102122
setNewOption(Profile::Key::Device::BrightnessCap, Profile::Device::BrightnessCapDefault, isResetDefault);
21112123
setNewOption(Profile::Key::Device::Smooth, Profile::Device::SmoothDefault, isResetDefault);
21122124
setNewOption(Profile::Key::Device::Gamma, Profile::Device::GammaDefault, isResetDefault);
21132125
setNewOption(Profile::Key::Device::ColorDepth, Profile::Device::ColorDepthDefault, isResetDefault);
2126+
setNewOption(Profile::Key::Device::IsDitheringEnabled, Profile::Device::IsDitheringEnabledDefault, isResetDefault);
21142127

21152128

21162129
QPoint ledPosition;

Software/src/Settings.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ class Settings : public QObject
199199
static void setDeviceColorDepth(int value);
200200
static double getDeviceGamma();
201201
static void setDeviceGamma(double gamma);
202+
static bool isDeviceDitheringEnabled();
203+
static void setDeviceDitheringEnabled(bool isEnabled);
202204

203205
static Grab::GrabberType getGrabberType();
204206
static void setGrabberType(Grab::GrabberType grabMode);
@@ -363,6 +365,7 @@ class Settings : public QObject
363365
void deviceSmoothChanged(int value);
364366
void deviceColorDepthChanged(int value);
365367
void deviceGammaChanged(double gamma);
368+
void deviceDitheringEnabledChanged(bool isEnabled);
366369
void deviceColorSequenceChanged(QString value);
367370
void grabberTypeChanged(const Grab::GrabberType grabMode);
368371
#ifdef D3D10_GRAB_SUPPORT

Software/src/SettingsDefaults.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ static const int RefreshDelayMin = 64;
220220
static const int RefreshDelayDefault = 100;
221221
static const int RefreshDelayMax = 1023;
222222

223-
static const bool IsUsbPowerLedDisabled = false;
223+
static const bool IsUsbPowerLedDisabledDefault = false;
224224

225225
static const int BrightnessMin = 0;
226226
static const int BrightnessDefault = 100;
@@ -241,6 +241,8 @@ static const int ColorDepthMax = 255;
241241
static const double GammaMin = 0.01;
242242
static const double GammaDefault = 2.0;
243243
static const double GammaMax = 10.0;
244+
245+
static const bool IsDitheringEnabledDefault = false;
244246
}
245247
// [LED_i]
246248
namespace Led

Software/src/SettingsWindow.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ SettingsWindow::SettingsWindow(QWidget *parent) :
150150

151151
initGrabbersRadioButtonsVisibility();
152152
initLanguages();
153-
initVirtualLeds(Settings::getNumberOfLeds(SupportedDevices::DeviceTypeVirtual));
154153

155154
loadTranslation(Settings::getLanguage());
156155

@@ -265,6 +264,7 @@ void SettingsWindow::connectSignalsSlots()
265264
connect(ui->spinBox_DeviceBrightnessCap, SIGNAL(valueChanged(int)), this, SLOT(onDeviceBrightnessCap_valueChanged(int)));
266265
connect(ui->spinBox_DeviceColorDepth, SIGNAL(valueChanged(int)), this, SLOT(onDeviceColorDepth_valueChanged(int)));
267266
connect(ui->doubleSpinBox_DeviceGamma, SIGNAL(valueChanged(double)), this, SLOT(onDeviceGammaCorrection_valueChanged(double)));
267+
connect(ui->checkBox_EnableDithering, SIGNAL(toggled(bool)), this, SLOT(onDeviceDitheringEnabled_toggled(bool)));
268268
connect(ui->horizontalSlider_GammaCorrection, SIGNAL(valueChanged(int)), this, SLOT(onSliderDeviceGammaCorrection_valueChanged(int)));
269269
connect(ui->checkBox_SendDataOnlyIfColorsChanges, SIGNAL(toggled(bool)), this, SLOT(onDeviceSendDataOnlyIfColorsChanged_toggled(bool)));
270270

@@ -1393,6 +1393,13 @@ void SettingsWindow::onSliderDeviceGammaCorrection_valueChanged(int value)
13931393
emit updateGamma(Settings::getDeviceGamma());
13941394
}
13951395

1396+
void SettingsWindow::onDeviceDitheringEnabled_toggled(bool state) {
1397+
DEBUG_LOW_LEVEL << Q_FUNC_INFO << state;
1398+
1399+
Settings::setDeviceDitheringEnabled(state);
1400+
}
1401+
1402+
13961403
void SettingsWindow::onLightpackModes_currentIndexChanged(int index)
13971404
{
13981405
if (updatingFromSettings) return;
@@ -1926,25 +1933,26 @@ void SettingsWindow::updateUiFromSettings()
19261933

19271934
ui->pushButton_SelectColorSoundVizMin->setColor (Settings::getSoundVisualizerMinColor());
19281935
ui->pushButton_SelectColorSoundVizMax->setColor (Settings::getSoundVisualizerMaxColor());
1929-
ui->radioButton_SoundVizConstantMode->setChecked (!Settings::isSoundVisualizerLiquidMode());
1936+
ui->radioButton_SoundVizConstantMode->setChecked (!Settings::isSoundVisualizerLiquidMode());
19301937
ui->radioButton_SoundVizLiquidMode->setChecked (Settings::isSoundVisualizerLiquidMode());
19311938
ui->horizontalSlider_SoundVizLiquidSpeed->setValue (Settings::getSoundVisualizerLiquidSpeed());
19321939
#endif
19331940

19341941
ui->checkBox_DisableUsbPowerLed->setChecked (Settings::isDeviceUsbPowerLedDisabled());
19351942
ui->horizontalSlider_DeviceRefreshDelay->setValue (Settings::getDeviceRefreshDelay());
19361943
ui->horizontalSlider_DeviceBrightness->setValue (Settings::getDeviceBrightness());
1937-
ui->horizontalSlider_DeviceBrightnessCap->setValue (Settings::getDeviceBrightnessCap());
1944+
ui->horizontalSlider_DeviceBrightnessCap->setValue (Settings::getDeviceBrightnessCap());
19381945
ui->horizontalSlider_DeviceSmooth->setValue (Settings::getDeviceSmooth());
19391946
ui->horizontalSlider_DeviceColorDepth->setValue (Settings::getDeviceColorDepth());
19401947
ui->doubleSpinBox_DeviceGamma->setValue (Settings::getDeviceGamma());
1941-
ui->horizontalSlider_GammaCorrection->setValue (floor((Settings::getDeviceGamma() * 100 + 0.5)));
1942-
1943-
ui->groupBox_Api->setChecked (Settings::isApiEnabled());
1944-
ui->checkBox_listenOnlyOnLoInterface->setChecked (Settings::isListenOnlyOnLoInterface());
1945-
ui->lineEdit_ApiPort->setText (QString::number(Settings::getApiPort()));
1946-
ui->lineEdit_ApiPort->setValidator(new QIntValidator(1, 49151));
1947-
ui->lineEdit_ApiKey->setText (Settings::getApiAuthKey());
1948+
ui->horizontalSlider_GammaCorrection->setValue (floor((Settings::getDeviceGamma() * 100 + 0.5)));
1949+
ui->checkBox_EnableDithering->setChecked (Settings::isDeviceDitheringEnabled());
1950+
1951+
ui->groupBox_Api->setChecked (Settings::isApiEnabled());
1952+
ui->checkBox_listenOnlyOnLoInterface->setChecked (Settings::isListenOnlyOnLoInterface());
1953+
ui->lineEdit_ApiPort->setText (QString::number(Settings::getApiPort()));
1954+
ui->lineEdit_ApiPort->setValidator (new QIntValidator(1, 49151));
1955+
ui->lineEdit_ApiKey->setText (Settings::getApiAuthKey());
19481956
ui->spinBox_LoggingLevel->setValue (g_debugLevel);
19491957

19501958
if (g_debugLevel == Debug::DebugLevels::ZeroLevel) {
@@ -2125,6 +2133,11 @@ void SettingsWindow::on_pushButton_GammaCorrectionHelp_clicked()
21252133
showHelpOf(ui->horizontalSlider_GammaCorrection);
21262134
}
21272135

2136+
void SettingsWindow::on_pushButton_DitheringHelp_clicked()
2137+
{
2138+
showHelpOf(ui->checkBox_EnableDithering);
2139+
}
2140+
21282141
void SettingsWindow::on_pushButton_BrightnessCapHelp_clicked()
21292142
{
21302143
showHelpOf(ui->horizontalSlider_DeviceBrightnessCap);

Software/src/SettingsWindow.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ private slots:
183183
void onDeviceColorDepth_valueChanged(int value);
184184
void onDeviceGammaCorrection_valueChanged(double value);
185185
void onSliderDeviceGammaCorrection_valueChanged(int value);
186+
void onDeviceDitheringEnabled_toggled(bool state);
186187
void onDeviceSendDataOnlyIfColorsChanged_toggled(bool state);
187188
void onDx1011CaptureEnabledChanged(bool isEnabled);
188189
void onDx9CaptureEnabledChanged(bool isEnabled);
@@ -215,6 +216,7 @@ private slots:
215216
void on_pushButton_LightpackRefreshDelayHelp_clicked();
216217

217218
void on_pushButton_GammaCorrectionHelp_clicked();
219+
void on_pushButton_DitheringHelp_clicked();
218220
void on_pushButton_BrightnessCapHelp_clicked();
219221

220222
void on_pushButton_lumosityThresholdHelp_clicked();

0 commit comments

Comments
 (0)