File indexing completed on 2024-04-21 15:12:03

0001 /************************************************************************
0002  *                                  *
0003  *  This file is part of Kooka, a scanning/OCR application using    *
0004  *  Qt <http://www.qt.io> and KDE Frameworks <http://www.kde.org>.  *
0005  *                                  *
0006  *  Copyright (C) 2013-2016 Jonathan Marten <jjm@keelhaul.me.uk>    *
0007  *                                  *
0008  *  Kooka is free software; you can redistribute it and/or modify it    *
0009  *  under the terms of the GNU Library General Public License as    *
0010  *  published by the Free Software Foundation and appearing in the  *
0011  *  file COPYING included in the packaging of this file;  either    *
0012  *  version 2 of the License, or (at your option) any later version.    *
0013  *                                  *
0014  *  As a special exception, permission is given to link this program    *
0015  *  with any version of the KADMOS OCR/ICR engine (a product of     *
0016  *  reRecognition GmbH, Kreuzlingen), and distribute the resulting  *
0017  *  executable without including the source code for KADMOS in the  *
0018  *  source distribution.                        *
0019  *                                  *
0020  *  This program is distributed in the hope that it will be useful, *
0021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of  *
0022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *
0023  *  GNU General Public License for more details.            *
0024  *                                  *
0025  *  You should have received a copy of the GNU General Public       *
0026  *  License along with this program;  see the file COPYING.  If     *
0027  *  not, see <http://www.gnu.org/licenses/>.                *
0028  *                                  *
0029  ************************************************************************/
0030 
0031 #include "imagetransform.h"
0032 
0033 #include <qurl.h>
0034 
0035 #include <klocalizedstring.h>
0036 #include "kooka_logging.h"
0037 
0038 
0039 ImageTransform::ImageTransform(const QImage &img, ImageTransform::Operation op,
0040                                const QString &fileName, QObject *parent)
0041     : QThread(parent)
0042 {
0043     mImage = img;
0044     mOperation = op;
0045     mFileName = fileName;
0046     qCDebug(KOOKA_LOG) << "operation" << mOperation;
0047 }
0048 
0049 
0050 void ImageTransform::run()
0051 {
0052     qCDebug(KOOKA_LOG) << "thread started for operation" << mOperation;
0053 
0054     QImage resultImg;
0055     QTransform m;
0056 
0057     switch (mOperation) {
0058     case ImageTransform::Rotate90:
0059         emit statusMessage(i18n("Rotate image +90 degrees"));
0060         m.rotate(+90);
0061         resultImg = mImage.transformed(m);
0062         break;
0063 
0064     case ImageTransform::MirrorBoth:
0065     case ImageTransform::Rotate180:
0066         emit statusMessage(i18n("Rotate image 180 degrees"));
0067         resultImg = mImage.mirrored(true, true);
0068         break;
0069 
0070     case ImageTransform::Rotate270:
0071         emit statusMessage(i18n("Rotate image -90 degrees"));
0072         m.rotate(-90);
0073         resultImg = mImage.transformed(m);
0074         break;
0075 
0076     case ImageTransform::MirrorHorizontal:
0077         emit statusMessage(i18n("Mirror image horizontally"));
0078         resultImg = mImage.mirrored(true, false);
0079         break;
0080 
0081     case ImageTransform::MirrorVertical:
0082         emit statusMessage(i18n("Mirror image vertically"));
0083         resultImg = mImage.mirrored(false, true);
0084         break;
0085 
0086     default:
0087         qCWarning(KOOKA_LOG) << "Unknown operation" << mOperation;
0088         break;
0089     }
0090 
0091     if (resultImg.save(mFileName)) {
0092         emit done(QUrl::fromLocalFile(mFileName));
0093     } else {
0094         emit statusMessage(i18n("Error updating image %1", mFileName));
0095     }
0096 
0097     qCDebug(KOOKA_LOG) << "thread finished";
0098 }