summary refs log tree commit diff
path: root/include/Utils.h
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-02-19 23:32:37 +0200
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-02-19 23:32:37 +0200
commit1764bacd4b1ef3542d3c223cec1c1a64f3db5071 (patch)
treef64ec27e78b0e6f284d3eaa8d807aab237b498ad /include/Utils.h
parentMinor adjustments (diff)
downloadnheko-1764bacd4b1ef3542d3c223cec1c1a64f3db5071.tar.xz
Move scaleImage() in Utils
Diffstat (limited to 'include/Utils.h')
-rw-r--r--include/Utils.h28
1 files changed, 28 insertions, 0 deletions
diff --git a/include/Utils.h b/include/Utils.h

index 30e592cd..fa825b7c 100644 --- a/include/Utils.h +++ b/include/Utils.h
@@ -26,4 +26,32 @@ firstChar(const QString &input); //! Get a human readable file size with the appropriate units attached. QString humanReadableFileSize(uint64_t bytes); + +//! Scale down an image to fit to the given width & height limitations. +template<class ImageType> +ImageType +scaleDown(uint64_t max_width, uint64_t max_height, const ImageType &source) +{ + if (source.isNull()) + return source; + + auto width_ratio = (double)max_width / (double)source.width(); + auto height_ratio = (double)max_height / (double)source.height(); + + auto min_aspect_ratio = std::min(width_ratio, height_ratio); + + int final_width = 0; + int final_height = 0; + + if (min_aspect_ratio > 1) { + final_width = source.width(); + final_height = source.height(); + } else { + final_width = source.width() * min_aspect_ratio; + final_height = source.height() * min_aspect_ratio; + } + + return source.scaled( + final_width, final_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); +} }