copyRectify function

Image copyRectify(
  1. Image src, {
  2. required Point topLeft,
  3. required Point topRight,
  4. required Point bottomLeft,
  5. required Point bottomRight,
  6. Image? toImage,
})

Returns a copy of the src image, where the given rectangle has been mapped to the full image.

Implementation

Image copyRectify(Image src,
    {required Point topLeft,
    required Point topRight,
    required Point bottomLeft,
    required Point bottomRight,
    Image? toImage}) {
  final dst = toImage ?? Image.from(src);
  for (var y = 0; y < dst.height; ++y) {
    final v = y / (dst.height - 1);
    for (var x = 0; x < dst.width; ++x) {
      final u = x / (dst.width - 1);
      // bilinear interpolation
      final srcPixelCoord = topLeft * (1 - u) * (1 - v) +
          topRight * (u) * (1 - v) +
          bottomLeft * (1 - u) * (v) +
          bottomRight * (u) * (v);
      final srcPixel = src.getPixel(srcPixelCoord.xi, srcPixelCoord.yi);
      dst.setPixel(x, y, srcPixel);
    }
  }
  return dst;
}