Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
transfer-engine
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
mer-core
transfer-engine
Commits
ca829235
Commit
ca829235
authored
May 24, 2013
by
Marko Mattila
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[nemo-transferengine] scaleImageToSize() function added and unit test updated.
parent
3a2b8f2e
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
384 additions
and
1 deletion
+384
-1
lib/imageoperation.cpp
lib/imageoperation.cpp
+74
-1
lib/imageoperation.cpp.autosave
lib/imageoperation.cpp.autosave
+275
-0
lib/imageoperation.h
lib/imageoperation.h
+1
-0
tests/ut_imageoperation.cpp
tests/ut_imageoperation.cpp
+33
-0
tests/ut_imageoperation.h
tests/ut_imageoperation.h
+1
-0
No files found.
lib/imageoperation.cpp
View file @
ca829235
...
...
@@ -31,7 +31,7 @@
#include <QtDebug>
#include <QImage>
#include <QSize>
#include <QtCore/qmath.h>)
/*!
\class ImageOperation
\brief The ImageOperation class is a helper class to manipulate images.
...
...
@@ -200,3 +200,76 @@ QString ImageOperation::scaleImage(const QString &sourceFile, qreal scaleFactor,
return
tmpFile
;
}
/*!
Scale image from a \a sourceFile to the \a targetSize. If user gives \a targetFile argument, it is used for
saving the scaled image to that location.
NOTE: It's hard to predict what will be the actual file size after scaling because it depends on image data.
Therefore it's good to remember that \a targetSize is used by this algorithm, but the final file size
may varie a lot.
Returns a path to the scaled image.
*/
QString
ImageOperation
::
scaleImageToSize
(
const
QString
&
sourceFile
,
quint64
targetSize
,
const
QString
&
targetFile
)
{
if
(
targetSize
==
0
)
{
qWarning
()
<<
Q_FUNC_INFO
<<
"Target size is 0. Can't scale image to 0 size!"
;
return
QString
();
}
if
(
!
QFile
::
exists
(
sourceFile
))
{
qWarning
()
<<
Q_FUNC_INFO
<<
sourceFile
<<
"doesn't exist!"
;
return
QString
();
}
QFileInfo
f
(
sourceFile
);
quint64
originalSize
=
f
.
size
();
if
(
originalSize
<=
targetSize
)
{
qWarning
()
<<
Q_FUNC_INFO
<<
"Target size can not be larger than the original size!"
;
return
QString
();
}
QString
tmpFile
=
targetFile
;
if
(
tmpFile
.
isEmpty
())
{
tmpFile
=
uniqueFilePath
(
sourceFile
);
}
QImage
tmpImage
(
sourceFile
);
if
(
tmpImage
.
isNull
())
{
qWarning
()
<<
Q_FUNC_INFO
<<
"NULL original image!"
;
return
QString
();
}
// NOTE: We really don't know the size on the disk after scaling and saving
//
// So this is a home made algorithm to downscale image based on give target size using the following
// logic:
//
// 1) First we figure out magic number (a) from the original image size (s) and width (w) and height(h).
// Magic number is basically a combination of the image depth (bits per pixel) and compression:
// a = s / (w * h)
//
// 2) We want the image to be the same aspect ratio (r) than the original image.
// r = w / h
//
// 3) Calculate the new width based on the following formula, where s' is the target size
// w * h * a = s' =>
// w * w / r * a = s' =>
// w * w = (s' * r) / a =>
// w = sqrt( (s' * r) / a )
qint32
w
=
tmpImage
.
width
();
// Width
qint32
h
=
tmpImage
.
height
();
// Height
qreal
r
=
w
/
(
h
*
1.0
);
// Aspect ratio
qreal
a
=
originalSize
/
(
w
*
h
*
1.0
);
// The magic number, which combines depth and compression
quint32
newWidth
=
qSqrt
((
targetSize
*
r
)
/
a
);
QImage
scaled
=
tmpImage
.
scaledToWidth
(
newWidth
,
Qt
::
SmoothTransformation
);
if
(
!
scaled
.
save
(
tmpFile
))
{
qWarning
()
<<
Q_FUNC_INFO
<<
"Failed to save scaled image to temp file!"
;
return
QString
();
}
return
tmpFile
;
}
lib/imageoperation.cpp.autosave
0 → 100644
View file @
ca829235
This diff is collapsed.
Click to expand it.
lib/imageoperation.h
View file @
ca829235
...
...
@@ -36,6 +36,7 @@ public:
static
QString
uniqueFilePath
(
const
QString
&
sourceFilePath
,
const
QString
&
path
=
QDir
::
tempPath
());
static
QString
removeImageMetadata
(
const
QString
&
sourceFile
);
static
QString
scaleImage
(
const
QString
&
sourceFile
,
qreal
scaleFactor
,
const
QString
&
targetFile
=
QString
());
static
QString
scaleImageToSize
(
const
QString
&
sourceFile
,
quint64
targetSize
,
const
QString
&
targetFile
=
QString
());
};
#endif // IMAGEOPERATION_H
tests/ut_imageoperation.cpp
View file @
ca829235
...
...
@@ -130,6 +130,39 @@ void ut_imageoperation::testScale()
QFile
::
remove
(
result
);
}
void
ut_imageoperation
::
testScaleToSize
()
{
QImage
img
(
"images/testimage.jpg"
);
QVERIFY
(
!
img
.
isNull
());
QString
filePath
(
"images/testimage.jpg"
);
QString
target
=
ImageOperation
::
uniqueFilePath
(
filePath
);
QFileInfo
f
(
"images/testimage.jpg"
);
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
);
}
void
ut_imageoperation
::
testDropMetadata
()
{
// NOTE: The test image doesn't contain all metadata fields such as
...
...
tests/ut_imageoperation.h
View file @
ca829235
...
...
@@ -38,6 +38,7 @@ public:
private
slots
:
void
testScale
();
void
testScaleToSize
();
void
testDropMetadata
();
void
testUniqueFilePath
();
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment