Commit e4e430db authored by Marko Mattila's avatar Marko Mattila

Fixed and updated unit tests

parent 985c57d0
......@@ -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();
}
......@@ -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;
......@@ -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();
}
......@@ -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;
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -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
......
......@@ -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);
......@@ -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
......@@ -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)
......
......@@ -41,6 +41,7 @@ private slots:
void testScaleToSize();
void testDropMetadata();
void testUniqueFilePath();
void testOrientation();
};
#endif // UT_IMAGEOPERATION_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment