Rewrite album cover loader
This commit is contained in:
@@ -18,14 +18,11 @@
|
||||
*/
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QByteArrayList>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QUrl>
|
||||
#include <QBuffer>
|
||||
#include <QImage>
|
||||
#include <QImageReader>
|
||||
#include <QPixmap>
|
||||
#include <QPainter>
|
||||
#include <QSize>
|
||||
|
||||
@@ -61,35 +58,6 @@ QStringList ImageUtils::SupportedImageFormats() {
|
||||
|
||||
}
|
||||
|
||||
QPixmap ImageUtils::TryLoadPixmap(const QUrl &art_automatic, const QUrl &art_manual, const QUrl &url) {
|
||||
|
||||
QPixmap ret;
|
||||
|
||||
if (!art_manual.path().isEmpty()) {
|
||||
if (art_manual.path() == Song::kManuallyUnsetCover) return ret;
|
||||
else if (art_manual.isLocalFile()) {
|
||||
ret.load(art_manual.toLocalFile());
|
||||
}
|
||||
else if (art_manual.scheme().isEmpty()) {
|
||||
ret.load(art_manual.path());
|
||||
}
|
||||
}
|
||||
if (ret.isNull() && !art_automatic.path().isEmpty()) {
|
||||
if (art_automatic.path() == Song::kEmbeddedCover && !url.isEmpty() && url.isLocalFile()) {
|
||||
ret = QPixmap::fromImage(TagReaderClient::Instance()->LoadEmbeddedArtAsImageBlocking(url.toLocalFile()));
|
||||
}
|
||||
else if (art_automatic.isLocalFile()) {
|
||||
ret.load(art_automatic.toLocalFile());
|
||||
}
|
||||
else if (art_automatic.scheme().isEmpty()) {
|
||||
ret.load(art_automatic.path());
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
QByteArray ImageUtils::SaveImageToJpegData(const QImage &image) {
|
||||
|
||||
if (image.isNull()) return QByteArray();
|
||||
@@ -124,28 +92,24 @@ QByteArray ImageUtils::FileToJpegData(const QString &filename) {
|
||||
|
||||
}
|
||||
|
||||
QImage ImageUtils::ScaleAndPad(const QImage &image, const bool scale, const bool pad, const int desired_height, const qreal device_pixel_ratio) {
|
||||
QImage ImageUtils::ScaleImage(const QImage &image, const QSize desired_size, const qreal device_pixel_ratio, const bool pad) {
|
||||
|
||||
if (image.isNull()) return image;
|
||||
|
||||
const int scaled_height = desired_height * device_pixel_ratio;
|
||||
|
||||
// Scale the image down
|
||||
QImage image_scaled;
|
||||
if (scale) {
|
||||
image_scaled = image.scaled(QSize(scaled_height, scaled_height), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||
}
|
||||
else {
|
||||
image_scaled = image;
|
||||
if (image.isNull() || (image.width() == desired_size.width() && image.height() == desired_size.height())) {
|
||||
return image;
|
||||
}
|
||||
|
||||
// Pad the image to height x height
|
||||
if (pad) {
|
||||
QImage image_padded(scaled_height, scaled_height, QImage::Format_ARGB32);
|
||||
QSize scale_size(desired_size.width() * device_pixel_ratio, desired_size.height() * device_pixel_ratio);
|
||||
|
||||
// Scale the image
|
||||
QImage image_scaled = image.scaled(scale_size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||
|
||||
// Pad the image
|
||||
if (pad && image_scaled.width() != image_scaled.height()) {
|
||||
QImage image_padded(scale_size, QImage::Format_ARGB32);
|
||||
image_padded.fill(0);
|
||||
|
||||
QPainter p(&image_padded);
|
||||
p.drawImage((scaled_height - image_scaled.width()) / 2, (scaled_height - image_scaled.height()) / 2, image_scaled);
|
||||
p.drawImage((image_padded.width() - image_scaled.width()) / 2, (image_padded.height() - image_scaled.height()) / 2, image_scaled);
|
||||
p.end();
|
||||
|
||||
image_scaled = image_padded;
|
||||
@@ -157,44 +121,23 @@ QImage ImageUtils::ScaleAndPad(const QImage &image, const bool scale, const bool
|
||||
|
||||
}
|
||||
|
||||
QImage ImageUtils::CreateThumbnail(const QImage &image, const bool pad, const QSize size) {
|
||||
|
||||
if (image.isNull()) return image;
|
||||
|
||||
QImage image_thumbnail;
|
||||
if (pad) {
|
||||
image_thumbnail = image.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||
QImage image_padded(size, QImage::Format_ARGB32_Premultiplied);
|
||||
image_padded.fill(0);
|
||||
|
||||
QPainter p(&image_padded);
|
||||
p.drawImage((image_padded.width() - image_thumbnail.width()) / 2, (image_padded.height() - image_thumbnail.height()) / 2, image_thumbnail);
|
||||
p.end();
|
||||
|
||||
image_thumbnail = image_padded;
|
||||
}
|
||||
else {
|
||||
image_thumbnail = image.scaledToHeight(size.height(), Qt::SmoothTransformation);
|
||||
}
|
||||
|
||||
return image_thumbnail;
|
||||
|
||||
}
|
||||
|
||||
QImage ImageUtils::GenerateNoCoverImage(const QSize size) {
|
||||
QImage ImageUtils::GenerateNoCoverImage(const QSize size, const qreal device_pixel_ratio) {
|
||||
|
||||
QImage image(":/pictures/cdcase.png");
|
||||
QSize scale_size(size.width() * device_pixel_ratio, size.height() * device_pixel_ratio);
|
||||
|
||||
// Get a square version of the nocover image with some transparency:
|
||||
QImage image_scaled = image.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||
QImage image_scaled = image.scaled(scale_size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||
|
||||
QImage image_square(size, QImage::Format_ARGB32);
|
||||
QImage image_square(scale_size, QImage::Format_ARGB32);
|
||||
image_square.fill(0);
|
||||
QPainter p(&image_square);
|
||||
p.setOpacity(0.4);
|
||||
p.drawImage((size.width() - image_scaled.width()) / 2, (size.height() - image_scaled.height()) / 2, image_scaled);
|
||||
p.drawImage((image_square.width() - image_scaled.width()) / 2, (image_square.height() - image_scaled.height()) / 2, image_scaled);
|
||||
p.end();
|
||||
|
||||
image_square.setDevicePixelRatio(device_pixel_ratio);
|
||||
|
||||
return image_square;
|
||||
|
||||
}
|
||||
|
||||
@@ -39,11 +39,8 @@ class ImageUtils {
|
||||
static QStringList SupportedImageFormats();
|
||||
static QByteArray SaveImageToJpegData(const QImage &image = QImage());
|
||||
static QByteArray FileToJpegData(const QString &filename);
|
||||
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, const qreal device_pixel_ratio = 1.0F);
|
||||
static QImage CreateThumbnail(const QImage &image, const bool pad, const QSize size);
|
||||
static QImage GenerateNoCoverImage(const QSize size = QSize());
|
||||
|
||||
static QImage ScaleImage(const QImage &image, const QSize desired_size, const qreal device_pixel_ratio = 1.0F, const bool pad = true);
|
||||
static QImage GenerateNoCoverImage(const QSize size, const qreal device_pixel_ratio);
|
||||
};
|
||||
|
||||
#endif // IMAGEUTILS_H
|
||||
|
||||
Reference in New Issue
Block a user