File indexing completed on 2024-04-28 04:52:22

0001 /*
0002     SPDX-FileCopyrightText: 2010 Simon Andreas Eugster <simon.eu@gmail.com>
0003     This file is part of kdenlive. See www.kdenlive.org.
0004 
0005 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "colorplaneexport.h"
0009 
0010 #include "KLocalizedString"
0011 #include "utils/KMessageBox_KdenliveCompat.h"
0012 #include <KMessageBox>
0013 //#define DEBUG_CTE
0014 #ifdef DEBUG_CTE
0015 #include "kdenlive_debug.h"
0016 #endif
0017 
0018 ColorPlaneExport::ColorPlaneExport(QWidget *parent)
0019     : QDialog(parent)
0020 {
0021     setupUi(this);
0022 
0023     m_colorTools = new ColorTools();
0024 
0025     tResX->setText(QStringLiteral("800"));
0026     tResY->setText(QStringLiteral("800"));
0027 
0028     cbColorspace->addItem(i18n("YUV UV plane"), QVariant(ColorPlaneExport::CPE_YUV));
0029     cbColorspace->addItem(i18n("YUV Y plane"), QVariant(ColorPlaneExport::CPE_YUV_Y));
0030     cbColorspace->addItem(i18n("Modified YUV (Chroma)"), QVariant(ColorPlaneExport::CPE_YUV_MOD));
0031     cbColorspace->addItem(i18n("YCbCr CbCr plane"), QVariant(ColorPlaneExport::CPE_YPbPr));
0032     cbColorspace->addItem(i18n("RGB plane, one component varying"), QVariant(ColorPlaneExport::CPE_RGB_CURVE));
0033     cbColorspace->addItem(i18n("HSV Hue Shift"), QVariant(ColorPlaneExport::CPE_HSV_HUESHIFT));
0034     cbColorspace->addItem(i18n("HSV Saturation"), QVariant(ColorPlaneExport::CPE_HSV_SATURATION));
0035 
0036     sliderColor->setSliderPosition(128);
0037 
0038     // 0  -> 1
0039     // 50 -> 0.5
0040     // 80 -> 0.2
0041     sliderScaling->setInvertedAppearance(true);
0042     sliderScaling->setRange(0, 80);
0043     sliderScaling->setSliderPosition(50);
0044 
0045     connect(buttonBox, &QDialogButtonBox::accepted, this, &ColorPlaneExport::slotExportPlane);
0046     connect(tResX, &QLineEdit::textChanged, this, &ColorPlaneExport::slotValidate);
0047     connect(tResY, &QLineEdit::textChanged, this, &ColorPlaneExport::slotValidate);
0048     connect(kurlrequester, &KUrlRequester::textChanged, this, &ColorPlaneExport::slotValidate);
0049     connect(sliderColor, &QAbstractSlider::valueChanged, this, &ColorPlaneExport::slotUpdateDisplays);
0050     connect(sliderScaling, &QAbstractSlider::valueChanged, this, &ColorPlaneExport::slotUpdateDisplays);
0051     connect(cbColorspace, SIGNAL(currentIndexChanged(int)), this, SLOT(slotColormodeChanged()));
0052 
0053     kurlrequester->setUrl(QUrl(QStringLiteral("/tmp/yuv-plane.png")));
0054 
0055     slotColormodeChanged();
0056     slotValidate();
0057 }
0058 
0059 ColorPlaneExport::~ColorPlaneExport()
0060 {
0061     delete m_colorTools;
0062 }
0063 
0064 ///// Helper functions /////
0065 
0066 void ColorPlaneExport::enableSliderScaling(bool enable)
0067 {
0068     sliderScaling->setEnabled(enable);
0069     lblScaling->setEnabled(enable);
0070     lblScaleNr->setEnabled(enable);
0071 }
0072 
0073 void ColorPlaneExport::enableSliderColor(bool enable)
0074 {
0075     sliderColor->setEnabled(enable);
0076     lblSliderName->setEnabled(enable);
0077     lblColNr->setEnabled(enable);
0078 }
0079 
0080 void ColorPlaneExport::enableCbVariant(bool enable)
0081 {
0082     cbVariant->setEnabled(enable);
0083     lblVariant->setEnabled(enable);
0084     if (!enable) {
0085         cbVariant->clear();
0086     }
0087 }
0088 
0089 ///// Slots /////
0090 
0091 void ColorPlaneExport::slotUpdateDisplays()
0092 {
0093     m_scaling = 1 - sliderScaling->value() / 100.f;
0094 
0095     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
0096     case CPE_RGB_CURVE:
0097         lblScaleNr->setText(QChar(0xb1) + QString::number(sliderScaling->value(), 'f', 2));
0098         break;
0099     case CPE_HSV_HUESHIFT:
0100         lblScaleNr->setText(QString::number(sliderScaling->value()));
0101         break;
0102     default:
0103         lblScaleNr->setText("0..." + QString::number(double(m_scaling), 'f', 2));
0104         break;
0105     }
0106 
0107     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
0108     case CPE_YUV_Y:
0109         lblColNr->setText(i18n("%1°", QString::number(sliderColor->value())));
0110         break;
0111     default:
0112         lblColNr->setText(QString::number(sliderColor->value()));
0113         break;
0114     }
0115 
0116     lblSize->setText(i18n("%1 px", tResX->text().toInt() * tResY->text().toInt()));
0117 }
0118 
0119 void ColorPlaneExport::slotValidate()
0120 {
0121     bool ok;
0122     int nr;
0123 
0124     nr = QVariant(tResX->text()).toInt(&ok);
0125     ok = ok && nr > 0;
0126     if (ok) {
0127         nr = QVariant(tResY->text()).toInt(&ok);
0128         ok = ok && nr > 0;
0129     }
0130     if (ok) {
0131         ok = !kurlrequester->text().trimmed().isEmpty();
0132 #ifdef DEBUG_CPE
0133         qCDebug(KDENLIVE_LOG) << "File given: " << ok;
0134 #endif
0135     }
0136 
0137     if (ok) {
0138         buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0139     } else {
0140         buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
0141     }
0142 
0143     slotUpdateDisplays();
0144 }
0145 
0146 void ColorPlaneExport::slotExportPlane()
0147 {
0148 #ifdef DEBUG_CPE
0149     qCDebug(KDENLIVE_LOG) << "Exporting plane now to " << kurlrequester->text();
0150 #endif
0151     QString lower = kurlrequester->text().toLower();
0152 #ifdef DEBUG_CPE
0153     qCDebug(KDENLIVE_LOG) << "Lower: " << lower;
0154 #endif
0155     if (!lower.endsWith(QLatin1String(".png")) && !lower.endsWith(QLatin1String(".jpg")) && !lower.endsWith(QLatin1String(".tif")) &&
0156         !lower.endsWith(QLatin1String(".tiff"))) {
0157         if (KMessageBox::questionTwoActions(this, i18n("File has no valid extension. Add extension (%1)?", QStringLiteral(".png")), i18n("File Extension"),
0158                                             KStandardGuiItem::add(), KGuiItem(i18nc("@action:button", "Continue without"))) == KMessageBox::PrimaryAction) {
0159             kurlrequester->setUrl(QUrl(kurlrequester->text() + QStringLiteral(".png")));
0160         }
0161     }
0162     QImage img;
0163     QColor col;
0164     QSize size(QVariant(tResX->text()).toInt(), QVariant(tResY->text()).toInt());
0165     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
0166     case CPE_YUV:
0167         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, false, false);
0168         break;
0169     case CPE_YUV_Y:
0170         img = m_colorTools->yuvVerticalPlane(size, sliderColor->value(), m_scaling);
0171         break;
0172     case CPE_YUV_MOD:
0173         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, true, false);
0174         break;
0175     case CPE_RGB_CURVE:
0176         img = m_colorTools->rgbCurvePlane(size, ColorTools::ColorsRGB(cbVariant->itemData(cbVariant->currentIndex()).toInt()), sliderScaling->value() / 255.f);
0177         break;
0178     case CPE_YPbPr:
0179         img = m_colorTools->yPbPrColorWheel(size, sliderColor->value(), m_scaling, false);
0180         break;
0181     case CPE_HSV_HUESHIFT:
0182         img = m_colorTools->hsvHueShiftPlane(size, sliderColor->value(), sliderScaling->value(), -180, 180);
0183         break;
0184     case CPE_HSV_SATURATION:
0185         col.setHsv(0, 0, sliderColor->value());
0186         img = m_colorTools->hsvCurvePlane(size, col, ColorTools::COM_H, ColorTools::COM_S);
0187         break;
0188     default:
0189         Q_ASSERT(false);
0190     }
0191     img.save(kurlrequester->text());
0192 }
0193 
0194 void ColorPlaneExport::slotColormodeChanged()
0195 {
0196 #ifdef DEBUG_CPE
0197     qCDebug(KDENLIVE_LOG) << "Color mode changed to " << cbColorspace->itemData(cbColorspace->currentIndex()).toInt();
0198 #endif
0199     lblScaling->setText(i18n("Scaling"));
0200     sliderScaling->setInvertedAppearance(true);
0201     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
0202     case CPE_YUV:
0203     case CPE_YUV_MOD:
0204     case CPE_YPbPr:
0205         enableSliderScaling(true);
0206         enableSliderColor(true);
0207         enableCbVariant(false);
0208         sliderColor->setRange(0, 255);
0209         sliderColor->setPageStep(128);
0210         lblSliderName->setText(i18n("Y value"));
0211         lblSliderName->setToolTip(i18n("The Y value describes the brightness of the colors."));
0212         break;
0213     case CPE_YUV_Y:
0214 #ifdef DEBUG_CPE
0215         qCDebug(KDENLIVE_LOG) << "Changing slider range.";
0216 #endif
0217         enableSliderScaling(true);
0218         enableSliderColor(true);
0219         enableCbVariant(false);
0220         sliderColor->setMaximum(321);
0221         sliderColor->setRange(0, 179);
0222         sliderColor->setPageStep(90);
0223         lblSliderName->setText(i18n("UV angle"));
0224         lblSliderName->setToolTip(i18n("Angle through the UV plane, with all possible Y values."));
0225         break;
0226     case CPE_RGB_CURVE:
0227         enableSliderScaling(true);
0228         enableSliderColor(false);
0229         enableCbVariant(true);
0230         sliderScaling->setRange(1, 255);
0231         sliderScaling->setValue(255);
0232         cbVariant->addItem(i18n("Red"), QVariant(int(ColorTools::ColorsRGB::R)));
0233         cbVariant->addItem(i18n("Green"), QVariant(int(ColorTools::ColorsRGB::G)));
0234         cbVariant->addItem(i18n("Blue"), QVariant(int(ColorTools::ColorsRGB::B)));
0235         cbVariant->addItem(i18n("Luma"), QVariant(int(ColorTools::ColorsRGB::Luma)));
0236         break;
0237     case CPE_HSV_HUESHIFT:
0238         enableSliderScaling(true);
0239         enableSliderColor(true);
0240         enableCbVariant(false);
0241         sliderScaling->setRange(0, 255);
0242         sliderScaling->setValue(200);
0243         sliderScaling->setInvertedAppearance(false);
0244         sliderColor->setRange(0, 255);
0245         sliderColor->setValue(200);
0246         lblSliderName->setText(i18n("HSV Saturation"));
0247         lblScaling->setText(i18n("HSV Value"));
0248         break;
0249     case CPE_HSV_SATURATION:
0250         enableSliderScaling(false);
0251         enableSliderColor(true);
0252         sliderColor->setRange(0, 255);
0253         sliderColor->setValue(200);
0254         lblSliderName->setText(i18n("HSV Value"));
0255         break;
0256     default:
0257         enableSliderScaling(false);
0258         enableSliderColor(false);
0259         enableCbVariant(true);
0260         cbVariant->addItem(i18n("Red"), QVariant(int(ColorTools::ColorsRGB::R)));
0261         cbVariant->addItem(i18n("Green"), QVariant(int(ColorTools::ColorsRGB::G)));
0262         cbVariant->addItem(i18n("Blue"), QVariant(int(ColorTools::ColorsRGB::B)));
0263         cbVariant->addItem(i18n("Luma"), QVariant(int(ColorTools::ColorsRGB::Luma)));
0264         break;
0265     }
0266     this->update();
0267     slotUpdateDisplays();
0268 }