]> git.sesse.net Git - kdenlive/blob - src/colorplaneexport.cpp
HSV Saturation plane added
[kdenlive] / src / colorplaneexport.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com)      *
3  *   This file is part of kdenlive. See www.kdenlive.org.                  *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10
11 #include "colorplaneexport.h"
12 #include <KMessageBox>
13
14 //#define DEBUG_CTE
15 #ifdef DEBUG_CTE
16 #include <QDebug>
17 #endif
18
19 const QString EXTENSION_PNG = ".png";
20 const int SCALE_RANGE = 80;
21
22 ColorPlaneExport::ColorPlaneExport(QWidget *parent) :
23     QDialog(parent)
24 {
25     setupUi(this);
26
27     m_colorTools = new ColorTools();
28
29     tResX->setText("800");
30     tResY->setText("800");
31
32     cbColorspace->addItem(i18n("YUV UV plane"), QVariant(ColorPlaneExport::CPE_YUV));
33     cbColorspace->addItem(i18n("YUV Y plane"), QVariant(ColorPlaneExport::CPE_YUV_Y));
34     cbColorspace->addItem(i18n("Modified YUV (Chroma)"), QVariant(ColorPlaneExport::CPE_YUV_MOD));
35     cbColorspace->addItem(i18n("YCbCr CbCr plane"), QVariant(ColorPlaneExport::CPE_YPbPr));
36     cbColorspace->addItem(i18n("RGB plane, one component varying"), QVariant(ColorPlaneExport::CPE_RGB_CURVE));
37     cbColorspace->addItem(i18n("HSV Hue Shift"), QVariant(ColorPlaneExport::CPE_HSV_HUESHIFT));
38     cbColorspace->addItem(i18n("HSV Saturation"), QVariant(ColorPlaneExport::CPE_HSV_SATURATION));
39
40     sliderColor->setSliderPosition(128);
41
42     // 0  -> 1
43     // 50 -> 0.5
44     // 80 -> 0.2
45     sliderScaling->setInvertedAppearance(true);
46     sliderScaling->setRange(0, 80);
47     sliderScaling->setSliderPosition(50);
48
49     connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotExportPlane()));
50     connect(tResX, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
51     connect(tResY, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
52     connect(kurlrequester, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
53     connect(sliderColor, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateDisplays()));
54     connect(sliderScaling, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateDisplays()));
55     connect(cbColorspace, SIGNAL(currentIndexChanged(int)), this, SLOT(slotColormodeChanged()));
56
57     kurlrequester->setUrl(KUrl("/tmp/yuv-plane.png"));
58
59     slotColormodeChanged();
60     slotValidate();
61 }
62
63 ColorPlaneExport::~ColorPlaneExport()
64 {
65     delete m_colorTools;
66 }
67
68
69
70 ///// Helper functions /////
71
72 void ColorPlaneExport::enableSliderScaling(const bool &enable)
73 {
74     sliderScaling->setEnabled(enable);
75     lblScaling->setEnabled(enable);
76     lblScaleNr->setEnabled(enable);
77 }
78
79 void ColorPlaneExport::enableSliderColor(const bool &enable)
80 {
81     sliderColor->setEnabled(enable);
82     lblSliderName->setEnabled(enable);
83     lblColNr->setEnabled(enable);
84 }
85
86 void ColorPlaneExport::enableCbVariant(const bool &enable)
87 {
88    cbVariant->setEnabled(enable);
89    lblVariant->setEnabled(enable);
90    if (!enable) {
91        while (cbVariant->count() > 0) {
92            cbVariant->removeItem(0);
93        }
94    }
95 }
96
97
98
99 ///// Slots /////
100
101 void ColorPlaneExport::slotUpdateDisplays()
102 {
103     m_scaling = 1 - (float)sliderScaling->value()/100;
104
105     switch(cbColorspace->itemData(cbColorspace->currentIndex()).toInt()){
106     case CPE_RGB_CURVE:
107         lblScaleNr->setText(QChar(0xb1) + QString::number(sliderScaling->value(), 'f', 2));;
108         break;
109     case CPE_HSV_HUESHIFT:
110         lblScaleNr->setText(QString::number(sliderScaling->value()));
111         break;
112     default:
113         lblScaleNr->setText("0..." + QString::number(m_scaling, 'f', 2));
114         break;
115     }
116
117     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
118     case CPE_YUV_Y:
119         lblColNr->setText(i18n("%1°", QString::number(sliderColor->value())));
120         break;
121     default:
122         lblColNr->setText(QString::number(sliderColor->value()));
123         break;
124     }
125
126     lblSize->setText(i18n("%1 px", QVariant(tResX->text()).toInt()*QVariant(tResY->text()).toInt()));
127 }
128
129 void ColorPlaneExport::slotValidate()
130 {
131     bool ok;
132     int nr;
133
134     nr = QVariant(tResX->text()).toInt(&ok);
135     ok = ok && nr > 0;
136     if (ok) {
137         nr = QVariant(tResY->text()).toInt(&ok);
138         ok = ok && nr > 0;
139     }
140     if (ok) {
141         ok = kurlrequester->text().trimmed().length() > 0;
142 #ifdef DEBUG_CPE
143         qDebug() << "File given: " << ok;
144 #endif
145     }
146
147     if (ok) {
148         buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
149     } else {
150         buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
151     }
152
153     slotUpdateDisplays();
154 }
155
156 void ColorPlaneExport::slotExportPlane()
157 {
158 #ifdef DEBUG_CPE
159     qDebug() << "Exporting plane now to " <<  kurlrequester->text();
160 #endif
161     QString lower = kurlrequester->text().toLower();
162 #ifdef DEBUG_CPE
163     qDebug() << "Lower: " << lower;
164 #endif
165     if (!lower.endsWith(".png") && !lower.endsWith(".jpg") && !lower.endsWith(".tif") && !lower.endsWith(".tiff")) {
166         if (KMessageBox::questionYesNo(this, i18n("File has no extension. Add extension (%1)?", EXTENSION_PNG)) == KMessageBox::Yes) {
167             kurlrequester->setUrl(KUrl(kurlrequester->text() + ".png"));
168         }
169     }
170     QImage img;
171     QSize size(QVariant(tResX->text()).toInt(), QVariant(tResY->text()).toInt());
172     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
173     case CPE_YUV:
174         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, false, false);
175         break;
176     case CPE_YUV_Y:
177         img = m_colorTools->yuvVerticalPlane(size, sliderColor->value(), m_scaling);
178         break;
179     case CPE_YUV_MOD:
180         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, true, false);
181         break;
182     case CPE_RGB_CURVE:
183         img = m_colorTools->rgbCurvePlane(size, (ColorTools::ColorsRGB) (cbVariant->itemData(cbVariant->currentIndex()).toInt()),
184                                           (double)sliderScaling->value()/255);
185         break;
186     case CPE_YPbPr:
187         img = m_colorTools->yPbPrColorWheel(size, sliderColor->value(), m_scaling, false);
188         break;
189     case CPE_HSV_HUESHIFT:
190         img = m_colorTools->hsvHueShiftPlane(size, sliderColor->value(), sliderScaling->value(), -180, 180);
191         break;
192     case CPE_HSV_SATURATION:
193         img = m_colorTools->hsvSaturationPlane(size, sliderColor->value(), 0, 255);
194         break;
195     default:
196         Q_ASSERT(false);
197     }
198     img.save(kurlrequester->text());
199 }
200
201 void ColorPlaneExport::slotColormodeChanged()
202 {
203 #ifdef DEBUG_CPE
204     qDebug() << "Color mode changed to " << cbColorspace->itemData(cbColorspace->currentIndex()).toInt();
205 #endif
206     lblScaling->setText(i18n("Scaling"));
207     sliderScaling->setInvertedAppearance(true);
208     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
209     case CPE_YUV:
210     case CPE_YUV_MOD:
211     case CPE_YPbPr:
212         enableSliderScaling(true);
213         enableSliderColor(true);
214         enableCbVariant(false);
215         sliderColor->setRange(0,255);
216         sliderColor->setPageStep(128);
217         lblSliderName->setText(i18n("Y value"));
218         lblSliderName->setToolTip(i18n("The Y value describes the brightness of the colors."));
219         break;
220     case CPE_YUV_Y:
221 #ifdef DEBUG_CPE
222         qDebug() << "Changing slider range.";
223 #endif
224         enableSliderScaling(true);
225         enableSliderColor(true);
226         enableCbVariant(false);
227         sliderColor->setMaximum(321);
228         sliderColor->setRange(0,179);
229         sliderColor->setPageStep(90);
230         lblSliderName->setText(i18n("UV angle"));
231         lblSliderName->setToolTip(i18n("Angle through the UV plane, with all possible Y values."));
232         break;
233     case CPE_RGB_CURVE:
234         enableSliderScaling(true);
235         enableSliderColor(false);
236         enableCbVariant(true);
237         sliderScaling->setRange(1,255);
238         sliderScaling->setValue(255);
239         cbVariant->addItem(i18n("Red"), QVariant(ColorTools::COL_R));
240         cbVariant->addItem(i18n("Green"), QVariant(ColorTools::COL_G));
241         cbVariant->addItem(i18n("Blue"), QVariant(ColorTools::COL_B));
242         cbVariant->addItem(i18n("Luma"), QVariant(ColorTools::COL_Luma));
243         break;
244     case CPE_HSV_HUESHIFT:
245         enableSliderScaling(true);
246         enableSliderColor(true);
247         enableCbVariant(false);
248         sliderScaling->setRange(0,255);
249         sliderScaling->setValue(200);
250         sliderScaling->setInvertedAppearance(false);
251         sliderColor->setRange(0,255);
252         sliderColor->setValue(200);
253         lblSliderName->setText(i18n("HSV Saturation"));
254         lblScaling->setText(i18n("HSV Value"));
255         break;
256     case CPE_HSV_SATURATION:
257         enableSliderScaling(false);
258         enableSliderColor(true);
259         sliderColor->setRange(0, 255);
260         sliderColor->setValue(200);
261         lblSliderName->setText(i18n("HSV Value"));
262         break;
263     default:
264         enableSliderScaling(false);
265         enableSliderColor(false);
266         enableCbVariant(true);
267         cbVariant->addItem(i18n("Red"), QVariant(ColorTools::COL_R));
268         cbVariant->addItem(i18n("Green"), QVariant(ColorTools::COL_G));
269         cbVariant->addItem(i18n("Blue"), QVariant(ColorTools::COL_B));
270         cbVariant->addItem(i18n("Luma"), QVariant(ColorTools::COL_Luma));
271         break;
272     }
273     this->update();
274     slotUpdateDisplays();
275 }