copyCrop function
Returns a cropped copy of src
.
Implementation
Image copyCrop(Image src, int x, int y, int w, int h) {
// Make sure crop rectangle is within the range of the src image.
x = x.clamp(0, src.width - 1).toInt();
y = y.clamp(0, src.height - 1).toInt();
if (x + w > src.width) {
w = src.width - x;
}
if (y + h > src.height) {
h = src.height - y;
}
final dst =
Image(w, h, channels: src.channels, exif: src.exif, iccp: src.iccProfile);
for (var yi = 0, sy = y; yi < h; ++yi, ++sy) {
for (var xi = 0, sx = x; xi < w; ++xi, ++sx) {
dst.setPixel(xi, yi, src.getPixel(sx, sy));
}
}
return dst;
}