Skip to content

fix(ui): scale large images and preserve original dimensions#380

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
add-uos:master
Dec 12, 2025
Merged

fix(ui): scale large images and preserve original dimensions#380
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
add-uos:master

Conversation

@add-uos
Copy link
Contributor

@add-uos add-uos commented Dec 12, 2025

Detect large images exceeding 4096px in either dimension and scale them down while maintaining aspect ratio for performance optimization. Store original dimensions in metadata and display them in the information dialog when available, falling back to scaled dimensions when original data is not present.

log: scale large images and preserve original dimensions
bug: https://pms.uniontech.com/bug-view-333195.html

Summary by Sourcery

Handle very large images more efficiently while preserving and exposing their original dimensions.

Enhancements:

  • Limit QImageReader memory allocation to 2048MB when loading images and reading metadata to support large image files.
  • Automatically scale down images larger than 4096px in either dimension while maintaining aspect ratio to improve performance and memory usage.
  • Store original image dimensions in metadata and expose them via new OriginalDimension/OriginalWidth/OriginalHeight fields.
  • Update the information dialog to show original dimensions when available, falling back to the scaled dimensions otherwise.

@sourcery-ai
Copy link

sourcery-ai bot commented Dec 12, 2025

Reviewer's Guide

Adds large-image handling by scaling images over 4096px with preserved aspect ratio, raises QImageReader memory limits, stores original dimensions in metadata, and updates the information dialog to prefer displaying original dimensions when available.

Sequence diagram for large image loading and scaling in loadStaticImageFromFile

sequenceDiagram
    participant Caller
    participant loadStaticImageFromFile
    participant QImageReader

    Caller->>loadStaticImageFromFile: loadStaticImageFromFile(path, image)
    loadStaticImageFromFile->>QImageReader: construct QImageReader(path)
    loadStaticImageFromFile->>QImageReader: setFormat(format)
    loadStaticImageFromFile->>QImageReader: setAutoTransform(true)
    loadStaticImageFromFile->>QImageReader: setAllocationLimit(2048)
    loadStaticImageFromFile->>QImageReader: size() -> originalSize

    alt originalSize.width > 4096 or originalSize.height > 4096
        loadStaticImageFromFile->>QImageReader: setScaledSize(scaledSize up to 4096, keep aspect ratio)
    end

    loadStaticImageFromFile->>QImageReader: read() -> res_qt
    QImageReader-->>loadStaticImageFromFile: scaled or original image
    loadStaticImageFromFile-->>Caller: res_qt
Loading

Sequence diagram for metadata extraction and dimension display for large images

sequenceDiagram
    actor User
    participant InformationDialog
    participant FileControl
    participant getAllMetaData
    participant QImageReader

    User->>InformationDialog: open info dialog(filePath)

    InformationDialog->>FileControl: slotGetInfo(Dimension, filePath)
    FileControl->>getAllMetaData: getAllMetaData(path)

    getAllMetaData->>QImageReader: construct QImageReader(path)
    getAllMetaData->>QImageReader: setAllocationLimit(2048)
    getAllMetaData->>QImageReader: size() -> originalSize

    alt originalSize.width > 4096 or originalSize.height > 4096
        getAllMetaData->>getAllMetaData: compute scaledSize (max 4096, keep aspect ratio)
        getAllMetaData->>getAllMetaData: store OriginalDimension, OriginalWidth, OriginalHeight (original)
        getAllMetaData->>getAllMetaData: update Dimension, Width, Height (scaled)
    else small image
        getAllMetaData->>getAllMetaData: store Dimension, Width, Height (original)
    end

    getAllMetaData-->>FileControl: metadata map
    FileControl-->>InformationDialog: Dimension value

    InformationDialog->>FileControl: slotGetInfo(OriginalDimension, filePath)
    FileControl-->>InformationDialog: OriginalDimension or "-"

    alt OriginalDimension exists and not "-"
        InformationDialog->>InformationDialog: description = OriginalDimension
    else
        InformationDialog->>InformationDialog: description = dimensionsStr (Dimension)
    end

    InformationDialog-->>User: show Dimensions property item
Loading

File-Level Changes

Change Details Files
Scale very large images during loading to a maximum dimension while preserving aspect ratio and increasing reader memory limits.
  • Set QImageReader allocation limit to 2048MB before loading images.
  • Detect images whose width or height exceeds 4096px and compute a scaled size using Qt::KeepAspectRatio.
  • Apply the computed scaled size to the reader via setScaledSize and log scaling operations.
src/src/unionimage/unionimage.cpp
Capture original dimensions in metadata for large images while exposing both original and possibly scaled dimensions.
  • Increase QImageReader allocation limit to 2048MB when reading metadata.
  • Read the image size, and when it exceeds 4096px in either dimension, compute a scaled size.
  • Store original width, height, and combined dimension under OriginalDimension, OriginalWidth, and OriginalHeight in the metadata map before overwriting width/height with scaled values.
  • Continue to populate Dimension, Width, and Height entries based on the final (possibly scaled) dimensions.
src/src/unionimage/unionimage.cpp
Prefer showing original dimensions in the information dialog when available.
  • Update the Dimensions property description to query OriginalDimension via FileControl.slotGetInfo.
  • Display OriginalDimension if present and not "-", otherwise fall back to the existing dimensionsStr value.
src/qml/PreviewImageViewer/InformationDialog/InformationDialog.qml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • The maxDimension (4096) and allocationLimit (2048) values are duplicated in multiple places; consider extracting them into shared constants or a config to avoid divergence and make future tuning easier.
  • Before using reader.size() to calculate scaled sizes, it would be safer to handle the case where the size is invalid (e.g. isEmpty() or negative dimensions) to avoid unexpected scaling behavior on malformed or unsupported images.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `maxDimension` (4096) and `allocationLimit` (2048) values are duplicated in multiple places; consider extracting them into shared constants or a config to avoid divergence and make future tuning easier.
- Before using `reader.size()` to calculate scaled sizes, it would be safer to handle the case where the size is invalid (e.g. `isEmpty()` or negative dimensions) to avoid unexpected scaling behavior on malformed or unsupported images.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Detect large images exceeding 4096px in either dimension and scale them down
while maintaining aspect ratio for performance optimization. Store original
dimensions in metadata and display them in the information dialog when
available, falling back to scaled dimensions when original data is not
present.

log: scale large images and preserve original dimensions
bug: https://pms.uniontech.com/bug-view-333195.html
@deepin-ci-robot
Copy link

deepin pr auto review

我来对这段代码进行审查,主要从以下几个方面分析:

  1. 语法逻辑
  2. 代码质量
  3. 代码性能
  4. 代码安全

1. 语法逻辑

代码的语法逻辑基本正确,但有一些需要改进的地方:

QML部分:

description: {
    var originalDim = FileControl.slotGetInfo("OriginalDimension", filePath);
    if (originalDim && originalDim !== "-") {
        return originalDim;
    } else {
        return dimensionsStr;
    }
}

这里使用了不必要的else分支,可以简化为:

description: {
    var originalDim = FileControl.slotGetInfo("OriginalDimension", filePath);
    return originalDim && originalDim !== "-" ? originalDim : dimensionsStr;
}

C++部分:

  • 内存限制的设置(2048MB)是硬编码的,应该定义为常量
  • 最大尺寸限制(4096)也是硬编码的,应该定义为常量
  • 重复的代码块(设置内存限制和尺寸缩放)可以提取为函数

2. 代码质量

存在的问题:

  1. 代码重复:内存限制设置和图片尺寸检查的逻辑在两个地方重复出现
  2. 魔法数字:2048MB和4096像素应该定义为常量
  3. 调试信息过多:qDebug()语句在生产环境中应该被移除或使用条件编译

改进建议:

// 定义常量
const int MAX_MEMORY_LIMIT_MB = 2048;
const int MAX_DIMENSION = 4096;

// 提取公共函数
static void configureImageReader(QImageReader& reader, const QSize& originalSize) {
    reader.setAllocationLimit(MAX_MEMORY_LIMIT_MB);
    
    if (originalSize.width() > MAX_DIMENSION || originalSize.height() > MAX_DIMENSION) {
        QSize scaledSize = originalSize;
        scaledSize.scale(MAX_DIMENSION, MAX_DIMENSION, Qt::KeepAspectRatio);
        reader.setScaledSize(scaledSize);
    }
}

3. 代码性能

  1. 重复的文件读取:在获取元数据时,可能需要多次读取文件信息
  2. 内存使用:虽然设置了内存限制,但2048MB的限制仍然较大,可能导致内存压力

改进建议:

  1. 考虑缓存图片元数据
  2. 根据系统可用内存动态调整内存限制
  3. 对于超大图片,考虑使用流式处理

4. 代码安全

  1. 输入验证:没有对filePath进行有效性验证
  2. 资源管理:QImageReader的资源管理需要更加谨慎
  3. 错误处理:需要更好的错误处理机制

改进建议:

// 添加文件路径验证
if (path.isEmpty() || !QFile::exists(path)) {
    return QMap<QString, QString>();
}

// 使用RAII管理资源
{
    QImageReader reader(path);
    if (!reader.canRead()) {
        return QMap<QString, QString>();
    }
    // ... 其他代码
}

总结建议

  1. 重构代码以消除重复
  2. 定义常量替代魔法数字
  3. 添加适当的错误处理和输入验证
  4. 优化内存使用和性能
  5. 改进日志系统,使用更合适的日志级别
  6. 考虑添加单元测试以验证大图片处理逻辑

这些改进将使代码更加健壮、可维护,同时提高性能和安全性。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@add-uos
Copy link
Contributor Author

add-uos commented Dec 12, 2025

/forcemerge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Dec 12, 2025

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit 797b229 into linuxdeepin:master Dec 12, 2025
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants