File indexing completed on 2024-05-12 04:19:35

0001 /*
0002 Gwenview: an image viewer
0003 Copyright 2022 Ilya Pominov <ipominov@astralinux.ru>
0004 
0005 This program is free software; you can redistribute it and/or
0006 modify it under the terms of the GNU General Public License
0007 as published by the Free Software Foundation; either version 2
0008 of the License, or (at your option) any later version.
0009 
0010 This program is distributed in the hope that it will be useful,
0011 but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 GNU General Public License for more details.
0014 
0015 You should have received a copy of the GNU General Public License
0016 along with this program; if not, write to the Free Software
0017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0018 
0019 */
0020 // Self
0021 #include "bcgimageoperation.h"
0022 
0023 // Qt
0024 #include <QImage>
0025 
0026 // KF
0027 #include <KLocalizedString>
0028 
0029 // Local
0030 #include "document/abstractdocumenteditor.h"
0031 #include "document/document.h"
0032 #include "document/documentjob.h"
0033 #include "gwenview_lib_debug.h"
0034 #include "imageutils.h"
0035 
0036 namespace Gwenview
0037 {
0038 class BCGJob : public ThreadedDocumentJob
0039 {
0040 public:
0041     BCGJob(const BCGImageOperation::BrightnessContrastGamma &bcg)
0042         : mBcg(bcg)
0043     {
0044     }
0045 
0046     void threadedStart() override
0047     {
0048         if (!checkDocumentEditor()) {
0049             return;
0050         }
0051         QImage img = document()->image();
0052         BCGImageOperation::apply(img, mBcg);
0053         document()->editor()->setImage(img);
0054         setError(NoError);
0055     }
0056 
0057 private:
0058     BCGImageOperation::BrightnessContrastGamma mBcg;
0059 };
0060 
0061 struct BCGImageOperationPrivate {
0062     QImage mOriginalImage;
0063     BCGImageOperation::BrightnessContrastGamma mBcg;
0064 };
0065 
0066 BCGImageOperation::BCGImageOperation(const BrightnessContrastGamma &bcg)
0067     : d(new BCGImageOperationPrivate)
0068 {
0069     setText(i18n("Adjust Colors"));
0070 
0071     d->mBcg = bcg;
0072 }
0073 
0074 BCGImageOperation::~BCGImageOperation()
0075 {
0076     delete d;
0077 }
0078 
0079 void BCGImageOperation::redo()
0080 {
0081     d->mOriginalImage = document()->image();
0082     redoAsDocumentJob(new BCGJob(d->mBcg));
0083 }
0084 
0085 void BCGImageOperation::undo()
0086 {
0087     if (!document()->editor()) {
0088         qCWarning(GWENVIEW_LIB_LOG) << "!document->editor()";
0089         return;
0090     }
0091     document()->editor()->setImage(d->mOriginalImage);
0092     finish(true);
0093 }
0094 
0095 void BCGImageOperation::apply(QImage &img, const BrightnessContrastGamma &bcg)
0096 {
0097     if (bcg.brightness != 0) {
0098         img = ImageUtils::changeBrightness(img, bcg.brightness);
0099     }
0100     if (bcg.contrast != 0) {
0101         img = ImageUtils::changeContrast(img, bcg.contrast + 100);
0102     }
0103     if (bcg.gamma != 0) {
0104         img = ImageUtils::changeGamma(img, bcg.gamma + 100);
0105     }
0106 }
0107 
0108 } // namespace
0109 
0110 #include "moc_bcgimageoperation.cpp"