revert: refactor: improve image loading logic in image_item for better memory performance
This commit is contained in:
+9
-11
@@ -1,10 +1,11 @@
|
||||
/*
|
||||
* @Author: Uyanide pywang0608@foxmail.com
|
||||
* @Date: 2025-11-30 20:32:27
|
||||
* @LastEditTime: 2026-02-07 07:13:49
|
||||
* @LastEditTime: 2026-02-07 07:26:30
|
||||
* @Description: Image item widget for displaying an image.
|
||||
*/
|
||||
#include "image_item.h"
|
||||
#include <qimage.h>
|
||||
|
||||
#include <QImageReader>
|
||||
|
||||
@@ -14,10 +15,10 @@ using namespace GeneralLogger;
|
||||
|
||||
ImageData* ImageData::create(const QString& p, const int initWidth, const int initHeight) {
|
||||
ImageData* data = new ImageData(p);
|
||||
data->image = new QImage();
|
||||
|
||||
// Use QImageReader for better performance
|
||||
QImageReader reader(p);
|
||||
reader.setAutoTransform(true);
|
||||
if (!reader.canRead()) {
|
||||
warn(QString("Failed to load image from path: %1").arg(p));
|
||||
delete data;
|
||||
@@ -28,7 +29,7 @@ ImageData* ImageData::create(const QString& p, const int initWidth, const int in
|
||||
const QSize originalSize = reader.size();
|
||||
|
||||
// Scale the image to fit the target size while maintaining aspect ratio
|
||||
if (originalSize.isValid() && reader.supportsOption(QImageIOHandler::ScaledSize)) {
|
||||
if (originalSize.isValid()) {
|
||||
double widthRatio = (double)targetSize.width() / originalSize.width();
|
||||
double heightRatio = (double)targetSize.height() / originalSize.height();
|
||||
double scaleFactor = std::max(widthRatio, heightRatio);
|
||||
@@ -37,22 +38,19 @@ ImageData* ImageData::create(const QString& p, const int initWidth, const int in
|
||||
reader.setScaledSize(scaledSize);
|
||||
}
|
||||
|
||||
QImage tempImage;
|
||||
if (!reader.read(&tempImage)) {
|
||||
if (!reader.read(data->image)) {
|
||||
warn(QString("Failed to load image from path: %1").arg(p));
|
||||
delete data;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Crop to target size if necessary
|
||||
if (tempImage.size() != targetSize) {
|
||||
int x = (tempImage.width() - targetSize.width()) / 2;
|
||||
int y = (tempImage.height() - targetSize.height()) / 2;
|
||||
tempImage = tempImage.copy(x, y, targetSize.width(), targetSize.height());
|
||||
if (data->image->size() != targetSize) {
|
||||
int x = (data->image->width() - targetSize.width()) / 2;
|
||||
int y = (data->image->height() - targetSize.height()) / 2;
|
||||
*data->image = data->image->copy(x, y, targetSize.width(), targetSize.height());
|
||||
}
|
||||
|
||||
data->image = new QImage(std::move(tempImage));
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user