displayRectToSourceRect function
Convert a crop rect from display coordinates to source-image pixel coordinates.
The display rect (cropRect) and the image's current display rect
(imageDisplayRect) share the same coordinate space (whatever Stage
uses — typically the screen). The source image lives in its own pixel
space (sourceSize). This function maps the crop rect into that pixel
space via the scale ratio between the displayed image and its source.
Implementation
Rect displayRectToSourceRect({
required Rect cropRect,
required Rect imageDisplayRect,
required Size sourceSize,
}) {
if (imageDisplayRect.width == 0 || imageDisplayRect.height == 0) {
return Rect.zero;
}
final scaleX = sourceSize.width / imageDisplayRect.width;
final scaleY = sourceSize.height / imageDisplayRect.height;
return Rect.fromLTWH(
(cropRect.left - imageDisplayRect.left) * scaleX,
(cropRect.top - imageDisplayRect.top) * scaleY,
cropRect.width * scaleX,
cropRect.height * scaleY,
);
}