File indexing completed on 2024-05-12 04:20:03

0001 /**
0002  Copyright (C) 2008  Unai Garro <ugarro@gmail.com>
0003 
0004  This program is free software; you can redistribute it and/or modify
0005  it under the terms of the GNU General Public License as published by
0006  the Free Software Foundation; either version 2 of the License, or
0007  (at your option) any later version.
0008 
0009  This program is distributed in the hope that it will be useful,
0010  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  GNU General Public License for more details.
0013 
0014  You should have received a copy of the GNU General Public License
0015  along with this program; if not, write to the Free Software
0016  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
0017  USA
0018 **/
0019 
0020 #include "rawcreator.h"
0021 
0022 #include <QImage>
0023 
0024 #include <kdcraw/kdcraw.h>
0025 #include <kexiv2/kexiv2.h>
0026 
0027 #include <KPluginFactory>
0028 
0029 K_PLUGIN_CLASS_WITH_JSON(RAWCreator, "rawthumbnail.json")
0030 
0031 RAWCreator::RAWCreator(QObject *parent, const QVariantList &args)
0032     : KIO::ThumbnailCreator(parent, args)
0033 {
0034 }
0035 
0036 RAWCreator::~RAWCreator()
0037 {
0038 }
0039 
0040 KIO::ThumbnailResult RAWCreator::create(const KIO::ThumbnailRequest &request)
0041 {
0042     //load the image into the QByteArray
0043     QByteArray data;
0044     bool loaded=KDcrawIface::KDcraw::loadEmbeddedPreview(data,request.url().toLocalFile());
0045 
0046     if (!loaded) {
0047         return KIO::ThumbnailResult::fail();
0048     }
0049     //Load the image into a QImage
0050     QImage preview;
0051     if (!preview.loadFromData(data) || preview.isNull())
0052         return KIO::ThumbnailResult::fail();
0053 
0054     //And its EXIF info
0055     KExiv2Iface::KExiv2 exiv;
0056     if (exiv.loadFromData(data))
0057     {
0058         //We managed reading the EXIF info, rotate the image
0059         //according to the EXIF orientation flag
0060         KExiv2Iface::KExiv2::ImageOrientation orient=exiv.getImageOrientation();
0061 
0062         //Rotate according to the EXIF orientation flag
0063         switch(orient)
0064         {
0065             case KExiv2Iface::KExiv2::ORIENTATION_UNSPECIFIED:
0066             case KExiv2Iface::KExiv2::ORIENTATION_NORMAL:
0067                 break; //we do nothing
0068             case KExiv2Iface::KExiv2::ORIENTATION_HFLIP:
0069                 preview = preview.mirrored(true,false);
0070                 break;
0071             case KExiv2Iface::KExiv2::ORIENTATION_ROT_180:
0072                 preview = preview.transformed(QTransform().rotate(180));
0073                 break;
0074             case KExiv2Iface::KExiv2::ORIENTATION_VFLIP:
0075                 preview = preview.mirrored(false,true);
0076                 break;
0077             case KExiv2Iface::KExiv2::ORIENTATION_ROT_90_HFLIP:
0078                 preview = preview.mirrored(true,false);
0079                 preview = preview.transformed(QTransform().rotate(90));
0080                 break;
0081             case KExiv2Iface::KExiv2::ORIENTATION_ROT_90:
0082                 preview = preview.transformed(QTransform().rotate(90));
0083                 break;
0084             case KExiv2Iface::KExiv2::ORIENTATION_ROT_90_VFLIP:
0085                 preview = preview.mirrored(false,true);
0086                 preview = preview.transformed(QTransform().rotate(90));
0087                 break;
0088             case KExiv2Iface::KExiv2::ORIENTATION_ROT_270:
0089                 preview = preview.transformed(QTransform().rotate(270));
0090                 break;
0091             default:
0092                 break;
0093         }
0094     }
0095 
0096     //Scale the image as requested by the thumbnailer
0097     QImage img=preview.scaled(request.targetSize(),Qt::KeepAspectRatio);
0098 
0099     return KIO::ThumbnailResult::pass(img);
0100 }
0101 
0102 #include "rawcreator.moc"