fix(unionimage): rotate TIFF and SVG images correctly#179
Conversation
Log: 修复TIFF和SVG图片旋转 PMS: BUG-283037 Influence: 相册和图片查看器可正确旋转TIFF及非正方形SVG图片
|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Reviewer's GuideAdds 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 rotateImageFIlesequenceDiagram
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
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
deepin pr auto review★ 总体评分:95分■ 【总体评价】
■ 【详细分析】
■ 【改进建议代码示例】 // 在 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; |
There was a problem hiding this comment.
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 / 90and% 2, as non‑multiples will produce inconsistentrotatedSizeand transform behavior. - The updated SVG translation logic now always uses
image_copy.height()for positive rotations andimage_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 fromrotatedSizeandrealangelrather 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.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Summary
Verification
PMS: BUG-283037