Fix album art rendering on High DPI displays

This commit is contained in:
Fletcher Dostie
2023-03-22 21:33:22 -07:00
committed by Jonas Kvinge
parent ca176c319d
commit bb43cc63ec
5 changed files with 23 additions and 15 deletions

View File

@@ -63,7 +63,7 @@ ContextAlbum::ContextAlbum(QWidget *parent)
cover_loader_options_.desired_height_ = width(); cover_loader_options_.desired_height_ = width();
cover_loader_options_.pad_output_image_ = true; cover_loader_options_.pad_output_image_ = true;
cover_loader_options_.scale_output_image_ = true; cover_loader_options_.scale_output_image_ = true;
QImage image = ImageUtils::ScaleAndPad(image_strawberry_, cover_loader_options_.scale_output_image_, cover_loader_options_.pad_output_image_, cover_loader_options_.desired_height_); QImage image = ImageUtils::ScaleAndPad(image_strawberry_, cover_loader_options_.scale_output_image_, cover_loader_options_.pad_output_image_, cover_loader_options_.desired_height_, devicePixelRatioF());
if (!image.isNull()) { if (!image.isNull()) {
pixmap_current_ = QPixmap::fromImage(image); pixmap_current_ = QPixmap::fromImage(image);
} }
@@ -235,7 +235,7 @@ void ContextAlbum::FadePreviousCoverFinished(std::shared_ptr<PreviousCover> prev
void ContextAlbum::ScaleCover() { void ContextAlbum::ScaleCover() {
QImage image = ImageUtils::ScaleAndPad(image_original_, cover_loader_options_.scale_output_image_, cover_loader_options_.pad_output_image_, cover_loader_options_.desired_height_); QImage image = ImageUtils::ScaleAndPad(image_original_, cover_loader_options_.scale_output_image_, cover_loader_options_.pad_output_image_, cover_loader_options_.desired_height_, devicePixelRatioF());
if (image.isNull()) { if (image.isNull()) {
pixmap_current_ = QPixmap(); pixmap_current_ = QPixmap();
} }
@@ -248,7 +248,7 @@ void ContextAlbum::ScaleCover() {
void ContextAlbum::ScalePreviousCovers() { void ContextAlbum::ScalePreviousCovers() {
for (std::shared_ptr<PreviousCover> previous_cover : previous_covers_) { for (std::shared_ptr<PreviousCover> previous_cover : previous_covers_) {
QImage image = ImageUtils::ScaleAndPad(previous_cover->image, cover_loader_options_.scale_output_image_, cover_loader_options_.pad_output_image_, cover_loader_options_.desired_height_); QImage image = ImageUtils::ScaleAndPad(previous_cover->image, cover_loader_options_.scale_output_image_, cover_loader_options_.pad_output_image_, cover_loader_options_.desired_height_, devicePixelRatioF());
if (image.isNull()) { if (image.isNull()) {
previous_cover->pixmap = QPixmap(); previous_cover->pixmap = QPixmap();
} }

View File

@@ -442,12 +442,18 @@ void AlbumCoverChoiceController::ShowCover(const Song &song, const QImage &image
song.has_embedded_cover() song.has_embedded_cover()
) { ) {
QPixmap pixmap = ImageUtils::TryLoadPixmap(song.art_automatic(), song.art_manual(), song.url()); QPixmap pixmap = ImageUtils::TryLoadPixmap(song.art_automatic(), song.art_manual(), song.url());
if (!pixmap.isNull()) ShowCover(song, pixmap); if (!pixmap.isNull()) {
pixmap.setDevicePixelRatio(devicePixelRatioF());
ShowCover(song, pixmap);
}
} }
} }
else { else {
QPixmap pixmap = QPixmap::fromImage(image); QPixmap pixmap = QPixmap::fromImage(image);
if (!pixmap.isNull()) ShowCover(song, pixmap); if (!pixmap.isNull()) {
pixmap.setDevicePixelRatio(devicePixelRatioF());
ShowCover(song, pixmap);
}
} }
} }
@@ -481,21 +487,21 @@ void AlbumCoverChoiceController::ShowCover(const Song &song, const QPixmap &pixm
if (desktop_width < desktop_height) { if (desktop_width < desktop_height) {
const int new_width = static_cast<int>(static_cast<double>(desktop_width) * 0.95); const int new_width = static_cast<int>(static_cast<double>(desktop_width) * 0.95);
if (new_width < pixmap.width()) { if (new_width < pixmap.width()) {
label->setPixmap(pixmap.scaledToWidth(new_width, Qt::SmoothTransformation)); label->setPixmap(pixmap.scaledToWidth(new_width * pixmap.devicePixelRatioF(), Qt::SmoothTransformation));
} }
} }
else { else {
const int new_height = static_cast<int>(static_cast<double>(desktop_height) * 0.85); const int new_height = static_cast<int>(static_cast<double>(desktop_height) * 0.85);
if (new_height < pixmap.height()) { if (new_height < pixmap.height()) {
label->setPixmap(pixmap.scaledToHeight(new_height, Qt::SmoothTransformation)); label->setPixmap(pixmap.scaledToHeight(new_height * pixmap.devicePixelRatioF(), Qt::SmoothTransformation));
} }
} }
dialog->setWindowTitle(title_text); dialog->setWindowTitle(title_text);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
dialog->setFixedSize(label->pixmap(Qt::ReturnByValue).size()); dialog->setFixedSize(label->pixmap(Qt::ReturnByValue).size() / pixmap.devicePixelRatioF());
#else #else
dialog->setFixedSize(label->pixmap()->size()); dialog->setFixedSize(label->pixmap()->size() / pixmap.devicePixelRatioF());
#endif #endif
dialog->show(); dialog->show();

View File

@@ -138,14 +138,16 @@ QByteArray ImageUtils::FileToJpegData(const QString &filename) {
} }
QImage ImageUtils::ScaleAndPad(const QImage &image, const bool scale, const bool pad, const int desired_height) { QImage ImageUtils::ScaleAndPad(const QImage &image, const bool scale, const bool pad, const int desired_height, const qreal device_pixel_ratio) {
if (image.isNull()) return image; if (image.isNull()) return image;
const int scaled_height = desired_height * device_pixel_ratio;
// Scale the image down // Scale the image down
QImage image_scaled; QImage image_scaled;
if (scale) { if (scale) {
image_scaled = image.scaled(QSize(desired_height, desired_height), Qt::KeepAspectRatio, Qt::SmoothTransformation); image_scaled = image.scaled(QSize(scaled_height, scaled_height), Qt::KeepAspectRatio, Qt::SmoothTransformation);
} }
else { else {
image_scaled = image; image_scaled = image;
@@ -153,11 +155,11 @@ QImage ImageUtils::ScaleAndPad(const QImage &image, const bool scale, const bool
// Pad the image to height x height // Pad the image to height x height
if (pad) { if (pad) {
QImage image_padded(desired_height, desired_height, QImage::Format_ARGB32); QImage image_padded(scaled_height, scaled_height, QImage::Format_ARGB32);
image_padded.fill(0); image_padded.fill(0);
QPainter p(&image_padded); QPainter p(&image_padded);
p.drawImage((desired_height - image_scaled.width()) / 2, (desired_height - image_scaled.height()) / 2, image_scaled); p.drawImage((scaled_height - image_scaled.width()) / 2, (scaled_height - image_scaled.height()) / 2, image_scaled);
p.end(); p.end();
image_scaled = image_padded; image_scaled = image_padded;

View File

@@ -41,7 +41,7 @@ class ImageUtils {
static QByteArray SaveImageToJpegData(const QImage &image = QImage()); static QByteArray SaveImageToJpegData(const QImage &image = QImage());
static QByteArray FileToJpegData(const QString &filename); static QByteArray FileToJpegData(const QString &filename);
static QPixmap TryLoadPixmap(const QUrl &automatic, const QUrl &manual, const QUrl &url = QUrl()); static QPixmap TryLoadPixmap(const QUrl &automatic, const QUrl &manual, const QUrl &url = QUrl());
static QImage ScaleAndPad(const QImage &image, const bool scale, const bool pad, const int desired_height); static QImage ScaleAndPad(const QImage &image, const bool scale, const bool pad, const int desired_height, const qreal device_pixel_ratio = 1.0f);
static QImage CreateThumbnail(const QImage &image, const bool pad, const QSize size); static QImage CreateThumbnail(const QImage &image, const bool pad, const QSize size);
static QImage GenerateNoCoverImage(const QSize size = QSize()); static QImage GenerateNoCoverImage(const QSize size = QSize());

View File

@@ -333,7 +333,7 @@ void PlayingWidget::SetImage(const QImage &image) {
void PlayingWidget::ScaleCover() { void PlayingWidget::ScaleCover() {
QImage image = ImageUtils::ScaleAndPad(image_original_, cover_loader_options_.scale_output_image_, cover_loader_options_.pad_output_image_, cover_loader_options_.desired_height_); QImage image = ImageUtils::ScaleAndPad(image_original_, cover_loader_options_.scale_output_image_, cover_loader_options_.pad_output_image_, cover_loader_options_.desired_height_, devicePixelRatioF());
if (image.isNull()) pixmap_cover_ = QPixmap(); if (image.isNull()) pixmap_cover_ = QPixmap();
else pixmap_cover_ = QPixmap::fromImage(image); else pixmap_cover_ = QPixmap::fromImage(image);
update(); update();