Skip to content

fix(unionimage): rotate TIFF and SVG images correctly#179

Open
Resurgamz wants to merge 1 commit into
linuxdeepin:develop/eaglefrom
Resurgamz:feature/fix-BUG283037-only
Open

fix(unionimage): rotate TIFF and SVG images correctly#179
Resurgamz wants to merge 1 commit into
linuxdeepin:develop/eaglefrom
Resurgamz:feature/fix-BUG283037-only

Conversation

@Resurgamz

@Resurgamz Resurgamz commented Jul 21, 2026

Copy link
Copy Markdown

Summary

  • Add TIF/TIFF to the Qt rotation format list.
  • Resize SVG output canvases for quarter-turn rotations.
  • Add TIFF and SVG rotation regression tests.

Verification

  • Main CMake build succeeded.
  • The full test-target build remains blocked by the pre-existing missing LibImageGraphicsView::OnFinishPinchAnimal() reference in tests/test_LibImageGraphicsView.cpp:232.

PMS: BUG-283037

Log: 修复TIFF和SVG图片旋转

PMS: BUG-283037

Influence: 相册和图片查看器可正确旋转TIFF及非正方形SVG图片
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Resurgamz

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

@sourcery-ai

sourcery-ai Bot commented Jul 21, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds TIFF/TIF to the set of formats rotated via Qt, fixes SVG rotation by correctly handling canvas size and transforms for quarter-turn rotations, and introduces regression tests for TIFF and SVG rotation including SVG output dimension verification.

Sequence diagram for updated SVG rotation in rotateImageFIle

sequenceDiagram
    participant Caller
    participant rotateImageFIle
    participant QSvgGenerator
    participant QPainter

    Caller->>rotateImageFIle: rotateImageFIle(angel, path, erroMsg)
    activate rotateImageFIle
    rotateImageFIle-->>rotateImageFIle: [format == SVG]
    rotateImageFIle-->>rotateImageFIle: rotatedSize = (qAbs(angel / 90) % 2 == 0)
    rotateImageFIle->>QSvgGenerator: setFileName(path)
    rotateImageFIle->>QSvgGenerator: setSize(rotatedSize)
    rotateImageFIle->>QSvgGenerator: setViewBox(QRect(QPoint(0, 0), rotatedSize))
    rotateImageFIle->>QPainter: begin(&generator)
    rotateImageFIle->>QPainter: resetTransform()
    rotateImageFIle-->>rotateImageFIle: realangel = angel / 90
    alt realangel > 0
        loop qAbs(realangel)
            rotateImageFIle->>QPainter: translate(image_copy.height(), 0)
            rotateImageFIle->>QPainter: rotate(90 * (realangel / qAbs(realangel)))
        end
    else realangel <= 0
        loop qAbs(realangel)
            rotateImageFIle->>QPainter: translate(0, image_copy.width())
            rotateImageFIle->>QPainter: rotate(90 * (realangel / qAbs(realangel)))
        end
    end
    rotateImageFIle->>QPainter: drawImage(image_copy.rect(), image_copy.scaled(image_copy.width(), image_copy.height()))
    rotateImageFIle->>QPainter: resetTransform()
    rotateImageFIle->>QPainter: end()
    rotateImageFIle-->>Caller: true
    deactivate rotateImageFIle
Loading

File-Level Changes

Change Details Files
Extend supported rotation formats to include TIFF/TIF and add regression tests for TIFF and SVG rotation, including SVG output size checks.
  • Include QSvgRenderer in the union image rotation test file to allow inspection of rotated SVG dimensions.
  • Add new tests that rotate TIFF and SVG images by 90 degrees and assert success of the rotation operation.
  • Use QSvgRenderer in the SVG rotation test to validate that the rotated SVG has the expected width and height.
tests/test_unionimage.cpp
Fix SVG rotation implementation to correctly resize the output canvas and adjust translation logic for quarter-turn rotations, and enable TIFF/TIF rotation via the Qt rotation path.
  • Extend the list of formats using Qt-based rotation to include TIF and TIFF so they go through the standard rotation pipeline.
  • Compute the rotated SVG canvas size based on the rotation angle, swapping width and height for odd multiples of 90 degrees.
  • Configure QSvgGenerator with the computed rotated size and corresponding view box before painting the rotated image.
  • Adjust rotation translation logic to use height for x-translation and width for y-translation so that quarter-turn rotations position the image correctly.
  • Remove the redundant final generator size reset that assumed the original image dimensions after rotation.
libimageviewer/unionimage/unionimage.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

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码修复了SVG图片旋转尺寸计算和视图框设置逻辑,并增加了对TIF/TIFF格式的支持
逻辑正确且修复了原有缺陷,代码质量良好,无安全漏洞,仅因缺少部分边界注释扣少量分数

■ 【详细分析】

  • 1.语法逻辑完全正确 ✓

rotateImageFIle 函数中,正确计算了旋转后的尺寸 rotatedSize,并在绘制前设置 setSizesetViewBox。平移坐标的修正(顺时针平移 height,逆时针平移 width)符合 90 度旋转的数学逻辑。
潜在问题:无
建议:无

  • 2.代码质量良好 ✓

代码结构清晰,变量命名准确,新增的单元测试覆盖了 TIFF 和 SVG 的旋转场景。
潜在问题:rotatedSize 的计算逻辑虽然正确,但缺乏对 angel 非 90 度整数倍情况下的注释说明。
建议:在 rotatedSize 计算处增加注释,说明该逻辑仅适用于 90 度倍数的旋转。

  • 3.代码性能无性能问题 ✓

修改未引入额外的循环或复杂的计算,仅修正了变量引用,对性能无负面影响。
潜在问题:无
建议:无

  • 4.代码安全存在0个安全漏洞 ✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次代码修改仅涉及图像尺寸计算和格式列表扩展,未引入外部输入解析或文件路径操作的风险。

  • 建议:无需额外修复措施

■ 【改进建议代码示例】

// 在 libimageviewer/unionimage/unionimage.cpp 中
+        // Calculate the size after rotation. Swap width and height for odd multiples of 90 degrees.
+        const QSize rotatedSize = qAbs(angel / 90) % 2 == 0
+                ? image_copy.size()
+                : QSize(image_copy.height(), image_copy.width());
         QSvgGenerator generator;
         generator.setFileName(path);
-        generator.setViewBox(QRect(0, 0, image_copy.width(), image_copy.height()));
+        generator.setSize(rotatedSize);
+        generator.setViewBox(QRect(QPoint(0, 0), rotatedSize));
         QPainter rotatePainter;
         rotatePainter.begin(&generator);
         rotatePainter.resetTransform();
@@ -527,18 +533,17 @@ UNIONIMAGESHARED_EXPORT bool rotateImageFIle(int angel, const QString &path, QSt
         int realangel = angel / 90;
         if (realangel > 0) {
             for (int i = 0; i < qAbs(realangel); i++) {
-                rotatePainter.translate(image_copy.width(), 0);
+                // Translate by height for 90-degree clockwise rotation
+                rotatePainter.translate(image_copy.height(), 0);
                 rotatePainter.rotate(90 * (realangel / qAbs(realangel)));
             }
         } else {
             for (int i = 0; i < qAbs(realangel); i++) {
-                rotatePainter.translate(0, image_copy.height());
+                // Translate by width for 90-degree counter-clockwise rotation
+                rotatePainter.translate(0, image_copy.width());
                 rotatePainter.rotate(90 * (realangel / qAbs(realangel)));
             }
         }
         rotatePainter.drawImage(image_copy.rect(), image_copy.scaled(image_copy.width(), image_copy.height()));
         rotatePainter.resetTransform();
-        generator.setSize(QSize(image_copy.width(), image_copy.height()));
         rotatePainter.end();
         return true;

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

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 SVG rotation branch, consider validating that the input angle is a multiple of 90 before using angel / 90 and % 2, as non‑multiples will produce inconsistent rotatedSize and transform behavior.
  • The updated SVG translation logic now always uses image_copy.height() for positive rotations and image_copy.width() for negative ones; this looks fragile for 180° and 270° rotations and would be clearer and more robust if the translation were derived from rotatedSize and realangel rather than hardcoded width/height swaps.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the SVG rotation branch, consider validating that the input angle is a multiple of 90 before using `angel / 90` and `% 2`, as non‑multiples will produce inconsistent `rotatedSize` and transform behavior.
- The updated SVG translation logic now always uses `image_copy.height()` for positive rotations and `image_copy.width()` for negative ones; this looks fragile for 180° and 270° rotations and would be clearer and more robust if the translation were derived from `rotatedSize` and `realangel` rather than hardcoded width/height swaps.

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.

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.

2 participants