Skip to content

Commit

Permalink
Fixed and updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko Mattila committed Nov 19, 2013
1 parent 985c57d commit e4e430d
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 5 deletions.
24 changes: 20 additions & 4 deletions lib/imageoperation.cpp
Expand Up @@ -210,7 +210,9 @@ QString ImageOperation::scaleImage(const QString &sourceFile, qreal scaleFactor,
}

if (!image.save(tmpFile)) {
qWarning() << Q_FUNC_INFO << "Failed to save scaled image to temp file!";
qWarning() << Q_FUNC_INFO
<< "Failed to save scaled image to temp file!"
<< tmpFile;
return QString();
}

Expand Down Expand Up @@ -280,13 +282,18 @@ QString ImageOperation::scaleImageToSize(const QString &sourceFile, quint64 targ
qreal a = originalSize / (w * h * 1.0); // The magic number, which combines depth and compression

qint32 newWidth = qSqrt((targetSize * r) / a);
qint32 newHeight = r / newWidth;
qint32 newHeight = newWidth / r;

QSize imageSize(ir.size());
imageSize = imageSize.scaled(newWidth, newHeight, Qt::KeepAspectRatio);
ir.setScaledSize(imageSize);
QImage image = ir.read();

if (image.isNull()) {
qWarning() << Q_FUNC_INFO
<< "NULL image";
return QString();
}
// Make sure orientation is right.
int angle;
bool mirrored;
Expand All @@ -303,7 +310,9 @@ QString ImageOperation::scaleImageToSize(const QString &sourceFile, quint64 targ
}

if (!image.save(tmpFile)) {
qWarning() << Q_FUNC_INFO << "Failed to save scaled image to temp file!";
qWarning() << Q_FUNC_INFO
<< "Failed to save scaled image to temp file!"
<< tmpFile;
return QString();
}

Expand All @@ -312,8 +321,15 @@ QString ImageOperation::scaleImageToSize(const QString &sourceFile, quint64 targ

void ImageOperation::imageOrientation(const QString &sourceFile, int &angle, bool *mirror)
{
if(!QuillMetadata::canRead(sourceFile)) {
qWarning() << Q_FUNC_INFO << "Can't read metadata";
angle = 0;
mirror = false;
return;
}
QuillMetadata md(sourceFile);
if (!md.isValid() || !md.hasExif()) {
if (!md.hasExif()) {
qWarning() << "Metadata invalid";
angle = 0;
mirror = false;
return;
Expand Down
Binary file added tests/images/testimage-0-mirrored.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/testimage-0.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/testimage-180-mirrored.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/testimage-180.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/testimage-270-mirrored.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/testimage-270.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/testimage-90-mirrored.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/testimage-90.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/testimage-90.jpg.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/tests.pro
Expand Up @@ -49,7 +49,7 @@ tests_install.path = $$PATH
tests_install.files = $$tests_xml.target
tests_install.CONFIG += no_check_exist

resources.files = images/testimage.jpg
resources.files = images/*.jpg
resources.path = $$PATH/images

target.path = $$PATH
Expand Down
62 changes: 62 additions & 0 deletions tests/ut_imageoperation.cpp
Expand Up @@ -114,17 +114,29 @@ void ut_imageoperation::testScale()
QString result = ImageOperation::scaleImage(filePath, 0.5, target);
QCOMPARE(result, target);

int angle;
bool mirror;
ImageOperation::imageOrientation(filePath, angle, &mirror);
QCOMPARE(angle, 90);
QCOMPARE(mirror, false);

QImage resImage(result);
QTransform x;
x.rotate(angle);
resImage = resImage.transformed(x);

QCOMPARE(resImage.size(), img.size()*0.5);
QFile::remove(result);

result = ImageOperation::scaleImage(filePath, 0.1, target);
resImage.load(result);
resImage = resImage.transformed(x);
QCOMPARE(resImage.size(), img.size()*0.1);
QFile::remove(result);

result = ImageOperation::scaleImage(filePath, 0.9, target);
resImage.load(result);
resImage = resImage.transformed(x);
QVERIFY(qAbs(resImage.width() - img.width()*0.9) < 2);
QVERIFY(qAbs(resImage.height() - img.height()*0.9) < 2);
QFile::remove(result);
Expand All @@ -139,7 +151,20 @@ void ut_imageoperation::testScaleToSize()
QString filePath("images/testimage.jpg");

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

QFileInfo f("images/testimage.jpg");

int angle;
bool mirror;
ImageOperation::imageOrientation(filePath, angle, &mirror);
QCOMPARE(angle, 90);
QCOMPARE(mirror, false);

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

int targetSize = f.size() * 0.5;

// Invalid sourceFile -> fail
Expand Down Expand Up @@ -216,6 +241,43 @@ void ut_imageoperation::testDropMetadata()
QFile::remove(path);
}

void ut_imageoperation::testOrientation()
{
int angle;
bool mirrored;
ImageOperation::imageOrientation("images/testimage-0.jpg", angle, &mirrored);
QCOMPARE(angle, 0);
QCOMPARE(mirrored, false);

ImageOperation::imageOrientation("images/testimage-0-mirrored.jpg", angle, &mirrored);
QCOMPARE(angle, 0);
QCOMPARE(mirrored, true);

ImageOperation::imageOrientation("images/testimage-90.jpg", angle, &mirrored);
QCOMPARE(angle, 90);
QCOMPARE(mirrored, false);

ImageOperation::imageOrientation("images/testimage-90-mirrored.jpg", angle, &mirrored);
QCOMPARE(angle, 90);
QCOMPARE(mirrored, true);

ImageOperation::imageOrientation("images/testimage-180.jpg", angle, &mirrored);
QCOMPARE(angle, 180);
QCOMPARE(mirrored, false);

ImageOperation::imageOrientation("images/testimage-180-mirrored.jpg", angle, &mirrored);
QCOMPARE(angle, 180);
QCOMPARE(mirrored, true);

ImageOperation::imageOrientation("images/testimage-270.jpg", angle, &mirrored);
QCOMPARE(angle, 270);
QCOMPARE(mirrored, false);

ImageOperation::imageOrientation("images/testimage-270-mirrored.jpg", angle, &mirrored);
QCOMPARE(angle, 270);
QCOMPARE(mirrored, true);
}

/*
QTEST_MAIN(ut_imageoperation)
Expand Down
1 change: 1 addition & 0 deletions tests/ut_imageoperation.h
Expand Up @@ -41,6 +41,7 @@ private slots:
void testScaleToSize();
void testDropMetadata();
void testUniqueFilePath();
void testOrientation();
};

#endif // UT_IMAGEOPERATION_H

0 comments on commit e4e430d

Please sign in to comment.