Skip to content

chore: update copyright year and improve image scaling#43

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
mhduiy:scale
Mar 12, 2026
Merged

chore: update copyright year and improve image scaling#43
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
mhduiy:scale

Conversation

@mhduiy
Copy link
Contributor

@mhduiy mhduiy commented Mar 10, 2026

Updated the copyright year range from 2022 to 2022-2026 in the file header.
Modified the image scaling logic in ErollThread::processCapturedImage to use center-cropping instead of simple scaling when the preview image size is not 800x600. This ensures the captured image maintains its aspect ratio and focuses on the central region, preventing distortion that could occur with non-uniform scaling. The new method calculates the appropriate source rectangle based on the larger required scale factor, crops from the center, and then scales to the target dimensions.

chore: 更新版权年份并改进图像缩放

更新了文件头中的版权年份范围,从2022年改为2022-2026年。
修改了ErollThread::processCapturedImage中的图像缩放逻辑,当预览图像尺寸 不是800x600时,使用中心裁剪代替简单的缩放。这确保了捕获的图像保持其宽高
比并聚焦于中央区域,防止了非均匀缩放可能导致的失真。新方法根据所需的最大
缩放因子计算适当的源矩形,从中心裁剪,然后缩放到目标尺寸。

PMS: BUG-304729

Summary by Sourcery

Update copyright metadata and refine captured image processing to avoid distorted previews.

Bug Fixes:

  • Adjust captured image processing to use center-cropping and scaling so non-800x600 previews maintain aspect ratio without distortion.

Chores:

  • Update file header copyright year range from 2022 to 2022-2026.

Updated the copyright year range from 2022 to 2022-2026 in the file
header.
Modified the image scaling logic in ErollThread::processCapturedImage
to use center-cropping instead of simple scaling when the preview image
size is not 800x600. This ensures the captured image maintains its
aspect ratio and focuses on the central region, preventing distortion
that could occur with non-uniform scaling. The new method calculates the
appropriate source rectangle based on the larger required scale factor,
crops from the center, and then scales to the target dimensions.

chore: 更新版权年份并改进图像缩放

更新了文件头中的版权年份范围,从2022年改为2022-2026年。
修改了ErollThread::processCapturedImage中的图像缩放逻辑,当预览图像尺寸
不是800x600时,使用中心裁剪代替简单的缩放。这确保了捕获的图像保持其宽高
比并聚焦于中央区域,防止了非均匀缩放可能导致的失真。新方法根据所需的最大
缩放因子计算适当的源矩形,从中心裁剪,然后缩放到目标尺寸。

PMS: BUG-304729
@sourcery-ai
Copy link

sourcery-ai bot commented Mar 10, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates the module’s SPDX copyright header year range and changes ErollThread::processCapturedImage to perform a center-crop followed by scaling to 800x600 instead of always uniformly scaling the full preview image when its size differs from 800x600, to preserve aspect ratio and focus on the central region.

File-Level Changes

Change Details Files
Adjust image processing so non-800x600 previews are center-cropped and then scaled to 800x600 to avoid distortion while preserving aspect ratio.
  • Keep the fast path where a 800x600 preview is used directly without additional processing.
  • For other preview sizes, compute the width and height scale factors to 800x600 and choose the larger to ensure full coverage.
  • Derive a centered source rectangle sized so that, after scaling by the chosen factor, it maps exactly to 800x600.
  • Copy the centered subimage and scale it to 800x600 with Qt::IgnoreAspectRatio and Qt::FastTransformation.
workmodule.cpp
Update SPDX copyright header year range to reflect coverage from 2022 to 2026.
  • Change the SPDX-FileCopyrightText comment from a single year to a year range of 2022 - 2026.
workmodule.cpp

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 - I've left some high level feedback:

  • In the new scaling branch, consider guarding against invalid preview sizes (e.g., width or height <= 0) before computing scale to avoid potential division-by-zero or negative dimension issues when unexpected images are passed in.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the new scaling branch, consider guarding against invalid preview sizes (e.g., width or height <= 0) before computing `scale` to avoid potential division-by-zero or negative dimension issues when unexpected images are passed in.

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.

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码主要修改了图片处理逻辑,从简单的缩放改为“居中裁剪+缩放”的方式。以下是对该修改的详细审查和改进建议:

1. 代码逻辑审查

  • 功能变更
    • 原逻辑:如果图片尺寸不是 800x600,直接拉伸/压缩到 800x600。这会导致图片变形(宽高比改变)。
    • 新逻辑:计算缩放比例,使得图片覆盖 800x600 的区域(类似 CSS 中的 object-fit: cover),然后从原图中心裁剪出对应区域,最后缩放到 800x600。这能保证目标图片始终填满 800x600 且不变形。
  • 逻辑正确性:新逻辑的数学计算是正确的。
    • scale 取宽高比中较大的值,确保缩放后能覆盖目标区域。
    • srcWsrcH 反推了缩放前需要截取的矩形大小。
    • srcXsrcY 计算了居中的起始坐标。

2. 代码质量与语法

  • 类型使用:代码中混用了 intdouble。虽然 Qt 的 qRound 处理了转换,但建议明确类型,或使用 qreal(Qt 的浮点类型)以提高跨平台一致性。
  • 常量定义800600 这两个“魔术数字”在代码中出现了多次。如果后续需要修改分辨率,需要改动多处,容易出错。建议定义为常量。
  • 代码可读性:计算 srcXsrcY 的逻辑略显紧凑,可以适当增加空行或注释。

3. 代码性能

  • 内存分配preview.copy() 会创建一个新的 QImage 对象(深拷贝),紧接着 .scaled() 又会创建另一个新对象。这会导致两次内存分配和数据拷贝。
  • 缩放质量:使用了 Qt::FastTransformation。这通常采用最近邻插值,速度很快,但在缩放比例较大时图片会有明显的锯齿或马赛克。如果应用场景对画质有要求,建议考虑 Qt::SmoothTransformation(双线性插值),但会牺牲一定的 CPU 性能。

4. 代码安全

  • 边界检查
    • preview.width()preview.height() 可能返回 0(空图片)。虽然代码开头有 if (preview.isNull()) return;,但在 preview.size() == QSize(800, 600) 判断后,如果进入 else 分支,pwph 是安全的。
    • 然而,如果 pwph 极小(例如 1x1 像素),计算 scale 时可能导致除零或数值异常,进而导致 srcWsrcH 计算为 0。QImage::copy 允许宽高为 0,但后续操作可能不稳定。
    • 建议:虽然不太可能发生,但增加对极小尺寸的防御性编程会更稳健。

改进后的代码建议

// 定义常量,方便维护
const int TARGET_WIDTH = 800;
const int TARGET_HEIGHT = 600;

void ErollThread::processCapturedImage(int id, const QImage &preview)
{
    if (preview.isNull()) {
        return;
    }

    QImage img;
    // 使用常量进行比较
    if (preview.size() == QSize(TARGET_WIDTH, TARGET_HEIGHT)) {
        img = preview;
    } else {
        const int pw = preview.width();
        const int ph = preview.height();
        
        // 防御性检查:防止原图尺寸过小导致计算异常
        if (pw <= 0 || ph <= 0) {
            return; // 或者处理错误情况
        }

        // 计算缩放比例,取较大值以覆盖目标区域
        qreal scale = qMax(static_cast<qreal>(TARGET_WIDTH) / pw, 
                           static_cast<qreal>(TARGET_HEIGHT) / ph);
        
        // 计算源图像中需要截取的区域(保持宽高比)
        int srcW = qRound(static_cast<qreal>(TARGET_WIDTH) / scale);
        int srcH = qRound(static_cast<qreal>(TARGET_HEIGHT) / scale);
        
        // 计算居中裁剪的起始坐标
        int srcX = (pw - srcW) / 2;
        int srcY = (ph - srcH) / 2;

        // 执行裁剪和缩放
        // 注意:这里会产生一次深拷贝和一次新图像分配
        // 根据需求选择 FastTransformation (快) 或 SmoothTransformation (画质好)
        img = preview.copy(srcX, srcY, srcW, srcH)
                  .scaled(TARGET_WIDTH, TARGET_HEIGHT, Qt::IgnoreAspectRatio, Qt::FastTransformation);
    }

    if (1 == id) {
        sendCapture(img);
    }
    // ...
}

总结修改点:

  1. 提取常量:将 800 和 600 定义为 TARGET_WIDTHTARGET_HEIGHT
  2. 类型安全:使用 static_cast<qreal> 进行显式类型转换,避免隐式转换带来的警告或问题。
  3. 防御性编程:增加了对 pwph 的有效性检查。
  4. 注释优化:增加了对关键计算步骤的注释,解释了“覆盖”和“居中”的逻辑。
  5. 性能提示:保留了 FastTransformation,但添加了注释提醒开发者根据实际场景(速度 vs 画质)进行选择。如果预览图对锯齿比较敏感,建议改为 Qt::SmoothTransformation

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mhduiy, robertkill

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

@mhduiy
Copy link
Contributor Author

mhduiy commented Mar 12, 2026

/merge

@deepin-bot deepin-bot bot merged commit 4ef08b6 into linuxdeepin:master Mar 12, 2026
11 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