Skip to content
This repository has been archived by the owner on Oct 17, 2019. It is now read-only.

Commit

Permalink
Consider the scale ratio when scaling down images
Browse files Browse the repository at this point in the history
fixes #393
  • Loading branch information
mujx committed Aug 1, 2018
1 parent 1f50d13 commit b5b5faa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
32 changes: 31 additions & 1 deletion src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,41 @@ utils::scaleImageToPixmap(const QImage &img, int size)
return QPixmap();

const double sz =
ceil(QApplication::desktop()->screen()->devicePixelRatioF() * (double)size);
std::ceil(QApplication::desktop()->screen()->devicePixelRatioF() * (double)size);
return QPixmap::fromImage(
img.scaled(sz, sz, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
}

QPixmap
utils::scaleDown(uint64_t maxWidth, uint64_t maxHeight, const QPixmap &source)
{
if (source.isNull())
return QPixmap();

const double pixelRatio = QApplication::desktop()->screen()->devicePixelRatioF();

// Take into account the scale factor of the screen.
maxWidth = std::ceil(pixelRatio * (double)maxWidth);
maxHeight = std::ceil(pixelRatio * (double)maxHeight);

const double widthRatio = (double)maxWidth / (double)source.width();
const double heightRatio = (double)maxHeight / (double)source.height();
const double minAspectRatio = std::min(widthRatio, heightRatio);

// Size of the output image.
int w, h = 0;

if (minAspectRatio > 1) {
w = source.width();
h = source.height();
} else {
w = source.width() * minAspectRatio;
h = source.height() * minAspectRatio;
}

return source.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}

QString
utils::mxcToHttp(const QUrl &url, const QString &server, int port)
{
Expand Down
28 changes: 2 additions & 26 deletions src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,32 +116,8 @@ createDescriptionInfo(const Event &event, const QString &localUser, const QStrin
}

//! 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 QPixmap();

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);
}
QPixmap
scaleDown(uint64_t maxWidth, uint64_t maxHeight, const QPixmap &source);

//! Delete items in a container based on a predicate.
template<typename ContainerT, typename PredicateT>
Expand Down
2 changes: 1 addition & 1 deletion src/dialogs/ImageOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ImageOverlay::paintEvent(QPaintEvent *event)
int max_width = screen_.width() - 2 * outer_margin;
int max_height = screen_.height();

image_ = utils::scaleDown<QPixmap>(max_width, max_height, originalImage_);
image_ = utils::scaleDown(max_width, max_height, originalImage_);

int diff_x = max_width - image_.width();
int diff_y = max_height - image_.height();
Expand Down
2 changes: 1 addition & 1 deletion src/dialogs/PreviewUploadOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ PreviewUploadOverlay::init()
const auto maxHeight = winsize.height() * 0.8;

// Scale image preview to fit into the application window.
infoLabel_.setPixmap(utils::scaleDown<QPixmap>(maxWidth, maxHeight, image_));
infoLabel_.setPixmap(utils::scaleDown(maxWidth, maxHeight, image_));
move(center.x() - (width() * 0.5), center.y() - (height() * 0.5));
} else {
infoLabel_.setAlignment(Qt::AlignLeft);
Expand Down
4 changes: 2 additions & 2 deletions src/timeline/widgets/ImageItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void
ImageItem::setImage(const QPixmap &image)
{
image_ = image;
scaled_image_ = utils::scaleDown<QPixmap>(max_width_, max_height_, image_);
scaled_image_ = utils::scaleDown(max_width_, max_height_, image_);

width_ = scaled_image_.width();
height_ = scaled_image_.height();
Expand Down Expand Up @@ -165,7 +165,7 @@ ImageItem::resizeEvent(QResizeEvent *event)
if (!image_)
return QWidget::resizeEvent(event);

scaled_image_ = utils::scaleDown<QPixmap>(max_width_, max_height_, image_);
scaled_image_ = utils::scaleDown(max_width_, max_height_, image_);

width_ = scaled_image_.width();
height_ = scaled_image_.height();
Expand Down

0 comments on commit b5b5faa

Please sign in to comment.