imagegenerationfunctor.cpp 13.7 KB
Newer Older
Gilles Caulier's avatar
Gilles Caulier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* ============================================================
 *
 * This file is a part of kipi-plugins project
 * http://www.digikam.org
 *
 * Date        : 2006-04-04
 * Description : A KIPI plugin to generate HTML image galleries
 *
 * Copyright (C) 2006-2010 by Aurelien Gateau <aurelien dot gateau at free.fr>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * ============================================================ */
Aurélien Gâteau's avatar
Aurélien Gâteau committed
22

Gilles Caulier's avatar
Gilles Caulier committed
23
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
Gilles Caulier's avatar
Gilles Caulier committed
24

Aurélien Gâteau's avatar
Aurélien Gâteau committed
25
26
#include "imagegenerationfunctor.h"

Gilles Caulier's avatar
Gilles Caulier committed
27
28
// Qt includes

Aurélien Gâteau's avatar
Aurélien Gâteau committed
29
#include <QFile>
30
#include <QFileInfo>
Aurélien Gâteau's avatar
Aurélien Gâteau committed
31
32
33
#include <QImage>
#include <QImageReader>

Gilles Caulier's avatar
Gilles Caulier committed
34
35
// KDE includes

Aurélien Gâteau's avatar
Aurélien Gâteau committed
36
#include <klocale.h>
37
#include <kdebug.h>
Aurélien Gâteau's avatar
Aurélien Gâteau committed
38

Gilles Caulier's avatar
Gilles Caulier committed
39
40
// LibKIPI includes

Aurélien Gâteau's avatar
Aurélien Gâteau committed
41
42
#include <libkipi/interface.h>

Gianluca Urgese's avatar
Gianluca Urgese committed
43
44
45
46
47
48
// LibKDcraw includes

#include <libkdcraw/dcrawinfocontainer.h>
#include <libkdcraw/version.h>
#include <libkdcraw/kdcraw.h>

Gilles Caulier's avatar
Gilles Caulier committed
49
// Local includes
Gianluca Urgese's avatar
Gianluca Urgese committed
50

Aurélien Gâteau's avatar
Aurélien Gâteau committed
51
52
53
#include "galleryinfo.h"
#include "generator.h"
#include "imageelement.h"
Gilles Caulier's avatar
Gilles Caulier committed
54
#include "kpmetadata.h"
Aurélien Gâteau's avatar
Aurélien Gâteau committed
55

Gianluca Urgese's avatar
Gianluca Urgese committed
56
57
using namespace KDcrawIface;

Gilles Caulier's avatar
Gilles Caulier committed
58
59
namespace KIPIHTMLExport
{
Aurélien Gâteau's avatar
Aurélien Gâteau committed
60
61

/**
62
63
 * Genearate a thumbnail from @fullImage of @size x @size pixels
 * If square == true, crop the result to a square
Aurélien Gâteau's avatar
Aurélien Gâteau committed
64
 */
Gilles Caulier's avatar
Gilles Caulier committed
65
66
static QImage generateThumbnail(const QImage& fullImage, int size, bool square)
{
Gilles Caulier's avatar
Gilles Caulier committed
67
68
69
    QImage image = fullImage.scaled(size, size, square ? Qt::KeepAspectRatioByExpanding
                                                       : Qt::KeepAspectRatio,
                                    Qt::SmoothTransformation);
70

Gilles Caulier's avatar
Gilles Caulier committed
71
72
    if (square && (image.width() != size || image.height() != size))
    {
73
        int sx=0, sy=0;
Gilles Caulier's avatar
Gilles Caulier committed
74
75
        if (image.width() > size)
        {
76
77
78
79
80
81
82
83
            sx=(image.width() - size) / 2;
        } else {
            sy=(image.height() - size) / 2;
        }
        image = image.copy(sx, sy, size, size);
    }

    return image;
Aurélien Gâteau's avatar
Aurélien Gâteau committed
84
85
}

86
ImageGenerationFunctor::ImageGenerationFunctor(Generator* generator, GalleryInfo* info, const QString& destDir)
Gilles Caulier's avatar
Gilles Caulier committed
87
88
89
    : mGenerator(generator), mInfo(info), mDestDir(destDir)
{
}
Aurélien Gâteau's avatar
Aurélien Gâteau committed
90

91
92
93
94
95
void ImageGenerationFunctor::operator()(ImageElement& element)
{
    // Load image
    QString path = element.mPath;
    QFile imageFile(path);
Gilles Caulier's avatar
Gilles Caulier committed
96
97
    if (!imageFile.open(QIODevice::ReadOnly))
    {
98
99
100
101
        emitWarning(i18n("Could not read image '%1'", path));
        return;
    }

Gilles Caulier's avatar
Gilles Caulier committed
102
103
    QImage     originalImage;
    QString    imageFormat;
104
105
    QByteArray imageData;

Gilles Caulier's avatar
Gilles Caulier committed
106
107
    // Check if RAW file.
    if (KPMetadata::isRawFile(path))
Gilles Caulier's avatar
Gilles Caulier committed
108
109
110
    {
        if (!KDcraw::loadDcrawPreview(originalImage, path))
        {
111
112
113
114
            emitWarning(i18n("Error loading RAW image '%1'", path));
            return;
        }
    }
Gilles Caulier's avatar
Gilles Caulier committed
115
116
    else
    {
117
        imageFormat = QImageReader::imageFormat(&imageFile);
Gilles Caulier's avatar
Gilles Caulier committed
118
119
        if (imageFormat.isEmpty())
        {
120
121
122
123
124
125
126
127
            emitWarning(i18n("Format of image '%1' is unknown", path));
            return;
        }
        imageFile.close();
        imageFile.open(QIODevice::ReadOnly);

        imageData = imageFile.readAll();

Gilles Caulier's avatar
Gilles Caulier committed
128
129
        if (!originalImage.loadFromData(imageData) )
        {
130
131
132
133
134
135
136
            emitWarning(i18n("Error loading image '%1'", path));
            return;
        }
    }

    // Process images
    QImage fullImage = originalImage;
Gilles Caulier's avatar
Gilles Caulier committed
137
138
139
140
141
    if (!mInfo->useOriginalImageAsFullImage())
    {
        if (mInfo->fullResize())
        {
            int size  = mInfo->fullSize();
142
143
            fullImage = fullImage.scaled(size, size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        }
Gilles Caulier's avatar
Gilles Caulier committed
144

Gilles Caulier's avatar
Gilles Caulier committed
145
        if (element.mOrientation != KPMetadata::ORIENTATION_UNSPECIFIED )
Gilles Caulier's avatar
Gilles Caulier committed
146
147
148
        {
            QMatrix matrix = RotationMatrix::toMatrix(element.mOrientation);
            fullImage      = fullImage.transformed(matrix);
149
150
151
        }
    }

152
    QImage thumbnail = generateThumbnail(fullImage, mInfo->thumbnailSize(), mInfo->thumbnailSquare());
153
154
155
156
157
158
159

    // Save images
    QString baseFileName = Generator::webifyFileName(element.mTitle);
    baseFileName = mUniqueNameHelper.makeNameUnique(baseFileName);

    // Save full
    QString fullFileName;
Gilles Caulier's avatar
Gilles Caulier committed
160
161
    if (mInfo->useOriginalImageAsFullImage())
    {
162
        fullFileName = baseFileName + '.' + imageFormat.toLower();
Gilles Caulier's avatar
Gilles Caulier committed
163
164
        if (!writeDataToFile(imageData, mDestDir + '/' + fullFileName))
        {
165
166
167
            return;
        }

Gilles Caulier's avatar
Gilles Caulier committed
168
169
170
    }
    else
    {
171
172
        fullFileName = baseFileName + '.' + mInfo->fullFormatString().toLower();
        QString destPath = mDestDir + '/' + fullFileName;
Gilles Caulier's avatar
Gilles Caulier committed
173
174
        if (!fullImage.save(destPath, mInfo->fullFormatString().toAscii(), mInfo->fullQuality()))
        {
175
176
177
178
179
180
181
182
            emitWarning(i18n("Could not save image '%1' to '%2'", path, destPath));
            return;
        }
    }
    element.mFullFileName = fullFileName;
    element.mFullSize = fullImage.size();

    // Save original
Gilles Caulier's avatar
Gilles Caulier committed
183
184
    if (mInfo->copyOriginalImage())
    {
185
        QString originalFileName = "original_" + fullFileName;
Gilles Caulier's avatar
Gilles Caulier committed
186
187
        if (!writeDataToFile(imageData, mDestDir + '/' + originalFileName))
        {
188
189
190
191
192
193
194
195
196
            return;
        }
        element.mOriginalFileName = originalFileName;
        element.mOriginalSize = originalImage.size();
    }

    // Save thumbnail
    QString thumbnailFileName = "thumb_" + baseFileName + '.' + mInfo->thumbnailFormatString().toLower();
    QString destPath = mDestDir + '/' + thumbnailFileName;
Gilles Caulier's avatar
Gilles Caulier committed
197
198
    if (!thumbnail.save(destPath, mInfo->thumbnailFormatString().toAscii(), mInfo->thumbnailQuality()))
    {
199
200
201
202
203
204
205
        mGenerator->logWarningRequested(i18n("Could not save thumbnail for image '%1' to '%2'", path, destPath));
        return;
    }
    element.mThumbnailFileName = thumbnailFileName;
    element.mThumbnailSize = thumbnail.size();

    element.mValid = true;
Gianluca Urgese's avatar
Gianluca Urgese committed
206

Gilles Caulier's avatar
Gilles Caulier committed
207
208
209
210
211
212
213
214
215
216
    // Read Exif Metadata
    QString unavailable(i18n("unavailable"));
    KPMetadata meta;
    meta.load(path);
    if (meta.hasExif() || meta.hasXmp())
    {
        // Try to use image metadata to get image info

        element.mExifImageMake = meta.getExifTagString("Exif.Image.Make");
        if (element.mExifImageMake.isEmpty())
Gianluca Urgese's avatar
Gianluca Urgese committed
217
        {
Gilles Caulier's avatar
Gilles Caulier committed
218
            element.mExifImageMake = meta.getXmpTagString("Xmp.tiff.Make");
Gianluca Urgese's avatar
Gianluca Urgese committed
219
        }
Gilles Caulier's avatar
Gilles Caulier committed
220
221
222

        element.mExifImageModel = meta.getExifTagString("Exif.Image.Model");
        if (element.mExifImageModel.isEmpty())
Gianluca Urgese's avatar
Gianluca Urgese committed
223
        {
Gilles Caulier's avatar
Gilles Caulier committed
224
225
            element.mExifImageModel = meta.getXmpTagString("Xmp.tiff.Model");
        }
Gianluca Urgese's avatar
Gianluca Urgese committed
226

Gilles Caulier's avatar
Gilles Caulier committed
227
228
229
230
231
        element.mExifImageOrientation = meta.getExifTagString("Exif.Image.Orientation");
        if (element.mExifImageOrientation.isEmpty())
        {
            element.mExifImageOrientation = meta.getXmpTagString("Xmp.tiff.Orientation");
        }
Gianluca Urgese's avatar
Gianluca Urgese committed
232

Gilles Caulier's avatar
Gilles Caulier committed
233
234
235
236
237
        element.mExifImageXResolution = meta.getExifTagString("Exif.Image.XResolution");
        if (element.mExifImageXResolution.isEmpty())
        {
            element.mExifImageXResolution = meta.getXmpTagString("Xmp.tiff.XResolution");
        }
Gianluca Urgese's avatar
Gianluca Urgese committed
238

Gilles Caulier's avatar
Gilles Caulier committed
239
240
241
242
243
        element.mExifImageYResolution = meta.getExifTagString("Exif.Image.YResolution");
        if (element.mExifImageYResolution.isEmpty())
        {
            element.mExifImageYResolution = meta.getXmpTagString("Xmp.tiff.YResolution");
        }
Gianluca Urgese's avatar
Gianluca Urgese committed
244

Gilles Caulier's avatar
Gilles Caulier committed
245
246
247
248
249
        element.mExifImageResolutionUnit = meta.getExifTagString("Exif.Image.ResolutionUnit");
        if (element.mExifImageResolutionUnit.isEmpty())
        {
            element.mExifImageResolutionUnit = meta.getXmpTagString("Xmp.tiff.ResolutionUnit");
        }
Gianluca Urgese's avatar
Gianluca Urgese committed
250

Gilles Caulier's avatar
Gilles Caulier committed
251
252
253
254
255
        if (meta.getImageDateTime().isValid())
        {
            element.mExifImageDateTime = KGlobal::locale()->formatDateTime(meta.getImageDateTime(),
                                                                            KLocale::ShortDate, true);
        }
Gianluca Urgese's avatar
Gianluca Urgese committed
256

Gilles Caulier's avatar
Gilles Caulier committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
        element.mExifImageYCbCrPositioning = meta.getExifTagString("Exif.Image.YCbCrPositioning");
        if (element.mExifImageYCbCrPositioning.isEmpty())
        {
            element.mExifImageYCbCrPositioning = meta.getXmpTagString("Xmp.tiff.YCbCrPositioning");
        }

        element.mExifPhotoFNumber = meta.getExifTagString("Exif.Photo.FNumber");
        if (element.mExifPhotoFNumber.isEmpty())
        {
            element.mExifPhotoFNumber = meta.getXmpTagString("Xmp.exif.FNumber");
        }

        element.mExifPhotoApertureValue = meta.getExifTagString("Exif.Photo.ApertureValue");
        if (element.mExifPhotoApertureValue.isEmpty())
        {
            element.mExifPhotoApertureValue = meta.getXmpTagString("Xmp.exif.ApertureValue");
        }
Gianluca Urgese's avatar
Gianluca Urgese committed
274

Gilles Caulier's avatar
Gilles Caulier committed
275
276
277
278
279
        element.mExifPhotoFocalLength = meta.getExifTagString("Exif.Photo.FocalLength");
        if (element.mExifPhotoFocalLength.isEmpty())
        {
            element.mExifPhotoFocalLength = meta.getXmpTagString("Xmp.exif.FocalLength");
        }
Gianluca Urgese's avatar
Gianluca Urgese committed
280

Gilles Caulier's avatar
Gilles Caulier committed
281
282
283
284
        element.mExifPhotoExposureTime = meta.getExifTagString("Exif.Photo.ExposureTime");
        if (element.mExifPhotoExposureTime.isEmpty())
        {
            element.mExifPhotoExposureTime = meta.getXmpTagString("Xmp.exif.ExposureTime");
Gianluca Urgese's avatar
Gianluca Urgese committed
285
286
        }

Gilles Caulier's avatar
Gilles Caulier committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
        element.mExifPhotoShutterSpeedValue = meta.getExifTagString("Exif.Photo.ShutterSpeedValue");
        if (element.mExifPhotoShutterSpeedValue.isEmpty())
        {
            element.mExifPhotoShutterSpeedValue = meta.getXmpTagString("Xmp.exif.ShutterSpeedValue");
        }

        element.mExifPhotoISOSpeedRatings = meta.getExifTagString("Exif.Photo.ISOSpeedRatings");
        if (element.mExifPhotoISOSpeedRatings.isEmpty())
        {
            element.mExifPhotoISOSpeedRatings = meta.getXmpTagString("Xmp.exif.ISOSpeedRatings");
        }

        element.mExifPhotoExposureProgram = meta.getExifTagString("Exif.Photo.ExposureIndex");
        if (element.mExifPhotoExposureProgram.isEmpty())
        {
            element.mExifPhotoExposureProgram = meta.getXmpTagString("Xmp.exif.ExposureIndex");
        }

        // Get GPS values
        double gpsvalue;
        if (meta.getGPSAltitude(&gpsvalue))
        {
            element.mExifGPSAltitude = QString::number(gpsvalue,'f',3);
        }

        if (meta.getGPSLatitudeNumber(&gpsvalue))
        {
            element.mExifGPSLatitude = QString::number(gpsvalue,'f',6);
        }

        if (meta.getGPSLongitudeNumber(&gpsvalue))
        {
            element.mExifGPSLongitude = QString::number(gpsvalue,'f',6);
        }
    }
    else
    {
        // Try to use libkdcraw interface to identify image.

        DcrawInfoContainer info;
        KDcraw             dcrawIface;
        dcrawIface.rawFileIdentify(info, path);

        if (info.isDecodable)
        {
            if (!info.make.isEmpty())
                element.mExifImageMake = info.make;

            if (!info.model.isEmpty())
                element.mExifImageModel = info.model;

            if (info.dateTime.isValid())
                element.mExifImageDateTime = KGlobal::locale()->formatDateTime(info.dateTime, KLocale::ShortDate, true);

            if (info.aperture != -1.0)
                element.mExifPhotoApertureValue = QString::number(info.aperture);

            if (info.focalLength != -1.0)
                element.mExifPhotoFocalLength = QString::number(info.focalLength);

            if (info.exposureTime != -1.0)
                element.mExifPhotoExposureTime = QString::number(info.exposureTime);

            if (info.sensitivity != -1)
                element.mExifPhotoISOSpeedRatings = QString::number(info.sensitivity);
        }
    }

    if (element.mExifImageMake.isEmpty()) element.mExifImageMake = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
356

Gilles Caulier's avatar
Gilles Caulier committed
357
    if (element.mExifImageModel.isEmpty()) element.mExifImageModel   = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
358

Gilles Caulier's avatar
Gilles Caulier committed
359
    if (element.mExifImageOrientation.isEmpty()) element.mExifImageOrientation = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
360

Gilles Caulier's avatar
Gilles Caulier committed
361
    if (element.mExifImageXResolution.isEmpty()) element.mExifImageXResolution = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
362

Gilles Caulier's avatar
Gilles Caulier committed
363
    if (element.mExifImageYResolution.isEmpty()) element.mExifImageYResolution = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
364

Gilles Caulier's avatar
Gilles Caulier committed
365
    if (element.mExifImageResolutionUnit.isEmpty()) element.mExifImageResolutionUnit = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
366

Gilles Caulier's avatar
Gilles Caulier committed
367
    if (element.mExifImageDateTime.isEmpty()) element.mExifImageDateTime = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
368

Gilles Caulier's avatar
Gilles Caulier committed
369
    if (element.mExifImageYCbCrPositioning.isEmpty()) element.mExifImageYCbCrPositioning = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
370

Gilles Caulier's avatar
Gilles Caulier committed
371
    if (element.mExifPhotoApertureValue.isEmpty()) element.mExifPhotoApertureValue   = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
372

Gilles Caulier's avatar
Gilles Caulier committed
373
    if (element.mExifPhotoFocalLength.isEmpty()) element.mExifPhotoFocalLength   = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
374

Gilles Caulier's avatar
Gilles Caulier committed
375
    if (element.mExifPhotoFNumber.isEmpty()) element.mExifPhotoFNumber   = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
376

Gilles Caulier's avatar
Gilles Caulier committed
377
    if (element.mExifPhotoExposureTime.isEmpty()) element.mExifPhotoExposureTime = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
378

Gilles Caulier's avatar
Gilles Caulier committed
379
    if (element.mExifPhotoShutterSpeedValue.isEmpty()) element.mExifPhotoShutterSpeedValue = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
380

Gilles Caulier's avatar
Gilles Caulier committed
381
    if (element.mExifPhotoISOSpeedRatings.isEmpty()) element.mExifPhotoISOSpeedRatings   = unavailable;
Gianluca Urgese's avatar
Gianluca Urgese committed
382

Gilles Caulier's avatar
Gilles Caulier committed
383
    if (element.mExifPhotoExposureProgram.isEmpty()) element.mExifPhotoExposureProgram   = unavailable;
Aurélien Gâteau's avatar
Aurélien Gâteau committed
384

Gilles Caulier's avatar
Gilles Caulier committed
385
    if (element.mExifGPSAltitude.isEmpty()) element.mExifGPSAltitude = unavailable;
Aurélien Gâteau's avatar
Aurélien Gâteau committed
386

Gilles Caulier's avatar
Gilles Caulier committed
387
    if (element.mExifGPSLatitude.isEmpty()) element.mExifGPSLatitude = unavailable;
Gilles Caulier's avatar
Gilles Caulier committed
388

Gilles Caulier's avatar
Gilles Caulier committed
389
    if (element.mExifGPSLongitude.isEmpty()) element.mExifGPSLongitude = unavailable;
Gilles Caulier's avatar
Gilles Caulier committed
390
391
392
393
}

bool ImageGenerationFunctor::writeDataToFile(const QByteArray& data, const QString& destPath)
{
394
    QFile destFile(destPath);
Gilles Caulier's avatar
Gilles Caulier committed
395
396
    if (!destFile.open(QIODevice::WriteOnly))
    {
397
398
399
        emitWarning(i18n("Could not open file '%1' for writing", destPath));
        return false;
    }
Gilles Caulier's avatar
Gilles Caulier committed
400
401
    if (destFile.write(data) != data.size())
    {
402
403
404
405
        emitWarning(i18n("Could not save image to file '%1'", destPath));
        return false;
    }
    return true;
Aurélien Gâteau's avatar
Aurélien Gâteau committed
406
407
}

Gilles Caulier's avatar
Gilles Caulier committed
408
409
void ImageGenerationFunctor::emitWarning(const QString& message)
{
Andi Clemens's avatar
Andi Clemens committed
410
    emit (mGenerator->logWarningRequested(message));
Aurélien Gâteau's avatar
Aurélien Gâteau committed
411
412
}

Gilles Caulier's avatar
Gilles Caulier committed
413
} // namespace KIPIHTMLExport