]> git.sesse.net Git - kdenlive/blob - src/colorplaneexport.cpp
Background color plane for Hueshift 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
39     sliderColor->setSliderPosition(128);
40
41     // 0  -> 1
42     // 50 -> 0.5
43     // 80 -> 0.2
44     sliderScaling->setInvertedAppearance(true);
45     sliderScaling->setRange(0, 80);
46     sliderScaling->setSliderPosition(50);
47
48     connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotExportPlane()));
49     connect(tResX, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
50     connect(tResY, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
51     connect(kurlrequester, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
52     connect(sliderColor, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateDisplays()));
53     connect(sliderScaling, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateDisplays()));
54     connect(cbColorspace, SIGNAL(currentIndexChanged(int)), this, SLOT(slotColormodeChanged()));
55
56     kurlrequester->setUrl(KUrl("/tmp/yuv-plane.png"));
57
58     slotColormodeChanged();
59     slotValidate();
60 }
61
62 ColorPlaneExport::~ColorPlaneExport()
63 {
64     delete m_colorTools;
65 }
66
67
68
69 ///// Helper functions /////
70
71 void ColorPlaneExport::enableSliderScaling(const bool &enable)
72 {
73     sliderScaling->setEnabled(enable);
74     lblScaling->setEnabled(enable);
75     lblScaleNr->setEnabled(enable);
76 }
77
78 void ColorPlaneExport::enableSliderColor(const bool &enable)
79 {
80     sliderColor->setEnabled(enable);
81     lblSliderName->setEnabled(enable);
82     lblColNr->setEnabled(enable);
83 }
84
85 void ColorPlaneExport::enableCbVariant(const bool &enable)
86 {
87    cbVariant->setEnabled(enable);
88    lblVariant->setEnabled(enable);
89    if (!enable) {
90        while (cbVariant->count() > 0) {
91            cbVariant->removeItem(0);
92        }
93    }
94 }
95
96
97
98 ///// Slots /////
99
100 void ColorPlaneExport::slotUpdateDisplays()
101 {
102     m_scaling = 1 - (float)sliderScaling->value()/100;
103
104     switch(cbColorspace->itemData(cbColorspace->currentIndex()).toInt()){
105     case CPE_RGB_CURVE:
106         lblScaleNr->setText(QChar(0xb1) + QString::number(sliderScaling->value(), 'f', 2));;
107         break;
108     case CPE_HSV_HUESHIFT:
109         lblScaleNr->setText(QString::number(sliderScaling->value()));
110         break;
111     default:
112         lblScaleNr->setText("0..." + QString::number(m_scaling, 'f', 2));
113         break;
114     }
115
116     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
117     case CPE_YUV_Y:
118         lblColNr->setText(i18n("%1°", QString::number(sliderColor->value())));
119         break;
120     default:
121         lblColNr->setText(QString::number(sliderColor->value()));
122         break;
123     }
124
125     lblSize->setText(i18n("%1 px", QVariant(tResX->text()).toInt()*QVariant(tResY->text()).toInt()));
126 }
127
128 void ColorPlaneExport::slotValidate()
129 {
130     bool ok;
131     int nr;
132
133     nr = QVariant(tResX->text()).toInt(&ok);
134     ok = ok && nr > 0;
135     if (ok) {
136         nr = QVariant(tResY->text()).toInt(&ok);
137         ok = ok && nr > 0;
138     }
139     if (ok) {
140         ok = kurlrequester->text().trimmed().length() > 0;
141 #ifdef DEBUG_CPE
142         qDebug() << "File given: " << ok;
143 #endif
144     }
145
146     if (ok) {
147         buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
148     } else {
149         buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
150     }
151
152     slotUpdateDisplays();
153 }
154
155 void ColorPlaneExport::slotExportPlane()
156 {
157 #ifdef DEBUG_CPE
158     qDebug() << "Exporting plane now to " <<  kurlrequester->text();
159 #endif
160     QString lower = kurlrequester->text().toLower();
161 #ifdef DEBUG_CPE
162     qDebug() << "Lower: " << lower;
163 #endif
164     if (!lower.endsWith(".png") && !lower.endsWith(".jpg") && !lower.endsWith(".tif") && !lower.endsWith(".tiff")) {
165         if (KMessageBox::questionYesNo(this, i18n("File has no extension. Add extension (%1)?", EXTENSION_PNG)) == KMessageBox::Yes) {
166             kurlrequester->setUrl(KUrl(kurlrequester->text() + ".png"));
167         }
168     }
169     QImage img;
170     QSize size(QVariant(tResX->text()).toInt(), QVariant(tResY->text()).toInt());
171     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
172     case CPE_YUV:
173         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, false, false);
174         break;
175     case CPE_YUV_Y:
176         img = m_colorTools->yuvVerticalPlane(size, sliderColor->value(), m_scaling);
177         break;
178     case CPE_YUV_MOD:
179         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, true, false);
180         break;
181     case CPE_RGB_CURVE:
182         img = m_colorTools->rgbCurvePlane(size, (ColorTools::ColorsRGB) (cbVariant->itemData(cbVariant->currentIndex()).toInt()),
183                                           (double)sliderScaling->value()/255);
184         break;
185     case CPE_YPbPr:
186         img = m_colorTools->yPbPrColorWheel(size, sliderColor->value(), m_scaling, false);
187         break;
188     case CPE_HSV_HUESHIFT:
189         img = m_colorTools->hsvHueShiftPlane(size, sliderColor->value(), sliderScaling->value(), -180, 180);
190         break;
191     }
192     img.save(kurlrequester->text());
193 }
194
195 void ColorPlaneExport::slotColormodeChanged()
196 {
197 #ifdef DEBUG_CPE
198     qDebug() << "Color mode changed to " << cbColorspace->itemData(cbColorspace->currentIndex()).toInt();
199 #endif
200     lblScaling->setText(i18n("Scaling"));
201     sliderScaling->setInvertedAppearance(true);
202     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
203     case CPE_YUV:
204     case CPE_YUV_MOD:
205     case CPE_YPbPr:
206         enableSliderScaling(true);
207         enableSliderColor(true);
208         enableCbVariant(false);
209         sliderColor->setRange(0,255);
210         sliderColor->setPageStep(128);
211         lblSliderName->setText(i18n("Y value"));
212         lblSliderName->setToolTip(i18n("The Y value describes the brightness of the colors."));
213         break;
214     case CPE_YUV_Y:
215 #ifdef DEBUG_CPE
216         qDebug() << "Changing slider range.";
217 #endif
218         enableSliderScaling(true);
219         enableSliderColor(true);
220         enableCbVariant(false);
221         sliderColor->setMaximum(321);
222         sliderColor->setRange(0,179);
223         sliderColor->setPageStep(90);
224         lblSliderName->setText(i18n("UV angle"));
225         lblSliderName->setToolTip(i18n("Angle through the UV plane, with all possible Y values."));
226         break;
227     case CPE_RGB_CURVE:
228         enableSliderScaling(true);
229         enableSliderColor(false);
230         enableCbVariant(true);
231         sliderScaling->setRange(1,255);
232         sliderScaling->setValue(255);
233         cbVariant->addItem(i18n("Red"), QVariant(ColorTools::COL_R));
234         cbVariant->addItem(i18n("Green"), QVariant(ColorTools::COL_G));
235         cbVariant->addItem(i18n("Blue"), QVariant(ColorTools::COL_B));
236         cbVariant->addItem(i18n("Luma"), QVariant(ColorTools::COL_Luma));
237         break;
238     case CPE_HSV_HUESHIFT:
239         enableSliderScaling(true);
240         enableSliderColor(true);
241         enableCbVariant(false);
242         sliderScaling->setRange(0,255);
243         sliderScaling->setValue(200);
244         sliderScaling->setInvertedAppearance(false);
245         sliderColor->setRange(0,255);
246         sliderColor->setValue(200);
247         lblSliderName->setText(i18n("HSV Saturation"));
248         lblScaling->setText(i18n("HSV Value"));
249         break;
250     default:
251         enableSliderScaling(false);
252         enableSliderColor(false);
253         enableCbVariant(true);
254         cbVariant->addItem(i18n("Red"), QVariant(ColorTools::COL_R));
255         cbVariant->addItem(i18n("Green"), QVariant(ColorTools::COL_G));
256         cbVariant->addItem(i18n("Blue"), QVariant(ColorTools::COL_B));
257         cbVariant->addItem(i18n("Luma"), QVariant(ColorTools::COL_Luma));
258         break;
259     }
260     this->update();
261     slotUpdateDisplays();
262 }