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