ut_imageoperation.cpp 9.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/****************************************************************************************
**
** Copyright (C) 2013 Jolla Ltd.
** Contact: Marko Mattila <marko.mattila@jollamobile.com>
** All rights reserved.
**
** This file is part of Nemo Transfer Engine package.
**
** You may use this file under the terms of the GNU Lesser General
** Public License version 2.1 as published by the Free Software Foundation
** and appearing in the file license.lgpl included in the packaging
** of this file.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file license.lgpl included in the packaging
** of this file.
**
** This library 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
** Lesser General Public License for more details.
**
****************************************************************************************/

#include "ut_imageoperation.h"
#include <qtest.h>
29
#include <QuillMetadata>
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#include "imageoperation.h"
#include <QtDebug>
#include <QDir>

QString ut_imageoperation::createTestFile(const QString &fileName, bool writeContent)
{
    QString f = QDir::tempPath() + QDir::separator() + fileName;
    if (writeContent) {
        QFile file(f);
        file.open(QIODevice::WriteOnly);
        const char * msg = "content";
        file.write(msg, qstrlen(msg));
        file.close();
    }
    return f;
}

void ut_imageoperation::testUniqueFilePath()
{
    QStringList tempFiles;
    tempFiles << createTestFile("img_001.jpg");

    // Bad input
    QCOMPARE(ImageOperation::uniqueFilePath(QString()), QString());

    // Non existing file
    QCOMPARE(ImageOperation::uniqueFilePath(QLatin1String("/home/nemo/Pictures/img_001.jpg")), QString());

    // Existing file
    QStringList tmp;
    tmp << ImageOperation::uniqueFilePath(tempFiles.at(0));
    QCOMPARE(tmp.last().endsWith(QLatin1String("_1.jpg")), true);
    tmp.clear();

    // Add two more files
    tempFiles << createTestFile("img_002.jpg")
              << createTestFile("img_004.jpg");

    // Create couple of temp files from the same source file. Note files must exists
    // on a disk
    tmp << ImageOperation::uniqueFilePath(tempFiles.at(2));
    QFile::copy(tempFiles.at(2), tmp.last());
    QCOMPARE(tmp.last().endsWith(QLatin1String("_1.jpg")), true);

    tmp << ImageOperation::uniqueFilePath(tempFiles.at(2));
    QFile::copy(tempFiles.at(2), tmp.last());
    QCOMPARE(tmp.last().endsWith(QLatin1String("_2.jpg")), true);

    // Remove the first file. This should handle the case that we are not getting
    // a new temp file with _2.jpg as a suffix because this should take into account
    // the removed files too.
    QFile::remove(tmp.first());
    QCOMPARE(ImageOperation::uniqueFilePath(tempFiles.at(2)).endsWith("_3.jpg"), true);

    // Remove all the files generated for this test case
    foreach(QString f, tmp) {
        QFile::remove(f);
    }

    foreach(QString f, tempFiles) {
        QFile::remove(f);
    }
}


void ut_imageoperation::testScale()
{

98
    QImage img("images/testimage.jpg");
99 100
    QVERIFY(!img.isNull());

101
    QString filePath("images/testimage.jpg");
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

    QString target = ImageOperation::uniqueFilePath(filePath);

    // Invalid sourceFile -> fail
    QCOMPARE(ImageOperation::scaleImage("", 0.5, target), QString());

    // Valid source file, invalid scaleFactor -> fail
    QCOMPARE(ImageOperation::scaleImage(filePath, -1.0, target), QString());
    QCOMPARE(ImageOperation::scaleImage(filePath, 0, target), QString());
    QCOMPARE(ImageOperation::scaleImage(filePath, 1.0, target), QString());

    // Proper source file, proper scale factor, proper target file
    QString result = ImageOperation::scaleImage(filePath, 0.5, target);
    QCOMPARE(result, target);

117 118
    int angle;
    bool mirror;
Marko Mattila's avatar
Marko Mattila committed
119
    ImageOperation::imageOrientation(filePath, &angle, &mirror);
120 121 122
    QCOMPARE(angle, 90);
    QCOMPARE(mirror, false);

123
    QImage resImage(result);
124 125 126 127
    QTransform x;
    x.rotate(angle);
    resImage = resImage.transformed(x);

128 129 130 131 132
    QCOMPARE(resImage.size(), img.size()*0.5);
    QFile::remove(result);

    result = ImageOperation::scaleImage(filePath, 0.1, target);
    resImage.load(result);
133
    resImage = resImage.transformed(x);
134 135 136 137 138
    QCOMPARE(resImage.size(), img.size()*0.1);
    QFile::remove(result);

    result = ImageOperation::scaleImage(filePath, 0.9, target);
    resImage.load(result);
139
    resImage = resImage.transformed(x);
140 141 142 143 144
    QVERIFY(qAbs(resImage.width() - img.width()*0.9) < 2);
    QVERIFY(qAbs(resImage.height() - img.height()*0.9) < 2);
    QFile::remove(result);
}

145 146 147 148 149 150 151 152 153

void ut_imageoperation::testScaleToSize()
{
    QImage img("images/testimage.jpg");
    QVERIFY(!img.isNull());

    QString filePath("images/testimage.jpg");

    QString target = ImageOperation::uniqueFilePath(filePath);
154 155
    QVERIFY(!target.isEmpty());

156
    QFileInfo f("images/testimage.jpg");
157 158 159

    int angle;
    bool mirror;
Marko Mattila's avatar
Marko Mattila committed
160
    ImageOperation::imageOrientation(filePath, &angle, &mirror);
161 162 163 164 165 166 167
    QCOMPARE(angle, 90);
    QCOMPARE(mirror, false);

    QTransform x;
    x.rotate(angle);
    img = img.transformed(x);

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    int targetSize = f.size() * 0.5;

    // Invalid sourceFile -> fail
    QCOMPARE(ImageOperation::scaleImageToSize("", targetSize, target), QString());

    // Valid source file, invalid targetSize -> fail
    QCOMPARE(ImageOperation::scaleImageToSize(filePath, -1.0, target), QString());
    QCOMPARE(ImageOperation::scaleImageToSize(filePath, 0, target), QString());
    QCOMPARE(ImageOperation::scaleImageToSize(filePath, targetSize*40, target), QString());

    // Proper source file, proper scale factor, proper target file
    QString result = ImageOperation::scaleImageToSize(filePath, targetSize, target);
    QCOMPARE(result, target);

    QImage tImg(result);
    QFileInfo f2(result);
    QVERIFY(f2.size() <= targetSize);
    QVERIFY(tImg.width() < img.width());
    QVERIFY(tImg.height() < img.height());
    QFile::remove(result);
}


191 192 193 194 195 196
void ut_imageoperation::testDropMetadata()
{
    // NOTE: The test image doesn't contain all metadata fields such as
    // title description, but it contains the most essential fields related
    // to location and the author.
    // First make sure that there is metadata
197 198
    QCOMPARE(QuillMetadata::canRead("images/testimage.jpg"), true);
    QuillMetadata md("images/testimage.jpg");
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    QCOMPARE(md.isValid(), true);

    QVariant creator = md.entry(QuillMetadata::Tag_Creator);
    QVariant city    = md.entry(QuillMetadata::Tag_City);
    QVariant country = md.entry(QuillMetadata::Tag_Country);
    QVariant location= md.entry(QuillMetadata::Tag_Location);
    QVariant lat     = md.entry(QuillMetadata::Tag_GPSLatitude);
    QVariant lon     = md.entry(QuillMetadata::Tag_GPSLongitude);

    // These shound't be null for the original image
    QCOMPARE(creator.isNull(), false);
    QCOMPARE(city.isNull(), false);
    QCOMPARE(country.isNull(), false);
    QCOMPARE(location.isNull(),false);
    QCOMPARE(lat.isNull(), false);
    QCOMPARE(lon.isNull(), false);

216
    QString path = ImageOperation::removeImageMetadata("images/testimage.jpg");
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    QVERIFY(!path.isNull());
    QCOMPARE(QFile::exists(path), true);


    QCOMPARE(QuillMetadata::canRead(path), true);
    QuillMetadata md2(path);
    QCOMPARE(md2.isValid(), true);

    creator = md2.entry(QuillMetadata::Tag_Creator);
    city    = md2.entry(QuillMetadata::Tag_City);
    country = md2.entry(QuillMetadata::Tag_Country);
    location= md2.entry(QuillMetadata::Tag_Location);
    lat     = md2.entry(QuillMetadata::Tag_GPSLatitude);
    lon     = md2.entry(QuillMetadata::Tag_GPSLongitude);

    // After removing metadata these should be fine.
    QCOMPARE(creator.isNull(), true);
    QCOMPARE(city.isNull(), true);
    QCOMPARE(country.isNull(), true);
    QCOMPARE(location.isNull(),true);
    QCOMPARE(lat.isNull(), true);
    QCOMPARE(lon.isNull(), true);

    // Remove the test image
    QFile::remove(path);
}

244 245 246 247
void ut_imageoperation::testOrientation()
{
    int angle;
    bool mirrored;
Marko Mattila's avatar
Marko Mattila committed
248
    ImageOperation::imageOrientation("images/testimage-0.jpg", &angle, &mirrored);
249 250 251
    QCOMPARE(angle, 0);
    QCOMPARE(mirrored, false);

Marko Mattila's avatar
Marko Mattila committed
252
    ImageOperation::imageOrientation("images/testimage-0-mirrored.jpg", &angle, &mirrored);
253 254 255
    QCOMPARE(angle, 0);
    QCOMPARE(mirrored, true);

Marko Mattila's avatar
Marko Mattila committed
256
    ImageOperation::imageOrientation("images/testimage-90.jpg", &angle, &mirrored);
257 258 259
    QCOMPARE(angle, 90);
    QCOMPARE(mirrored, false);

Marko Mattila's avatar
Marko Mattila committed
260
    ImageOperation::imageOrientation("images/testimage-90-mirrored.jpg", &angle, &mirrored);
261 262 263
    QCOMPARE(angle, 90);
    QCOMPARE(mirrored, true);

Marko Mattila's avatar
Marko Mattila committed
264
    ImageOperation::imageOrientation("images/testimage-180.jpg", &angle, &mirrored);
265 266 267
    QCOMPARE(angle, 180);
    QCOMPARE(mirrored, false);

Marko Mattila's avatar
Marko Mattila committed
268
    ImageOperation::imageOrientation("images/testimage-180-mirrored.jpg", &angle, &mirrored);
269 270 271
    QCOMPARE(angle, 180);
    QCOMPARE(mirrored, true);

Marko Mattila's avatar
Marko Mattila committed
272
    ImageOperation::imageOrientation("images/testimage-270.jpg", &angle, &mirrored);
273 274 275
    QCOMPARE(angle, 270);
    QCOMPARE(mirrored, false);

Marko Mattila's avatar
Marko Mattila committed
276
    ImageOperation::imageOrientation("images/testimage-270-mirrored.jpg", &angle, &mirrored);
277 278 279 280
    QCOMPARE(angle, 270);
    QCOMPARE(mirrored, true);
}

281 282 283 284 285
/*
QTEST_MAIN(ut_imageoperation)

#include "ut_imageoperation.moc"
*/