warp static method

RgbaImage warp(
  1. RgbaImage src,
  2. Quad pixelQuad
)

Perspective-warp src so that pixelQuad maps to a straight rectangle.

Implementation

static RgbaImage warp(RgbaImage src, Quad pixelQuad) {
  final (dw, dh) = pixelQuad.warpSize();
  final input = calloc<Uint8>(src.bytes.length);
  final corners = calloc<Float>(8);
  final out = calloc<Uint8>(dw * dh * 4);
  try {
    input.asTypedList(src.bytes.length).setAll(0, src.bytes);
    final cl = pixelQuad.toList();
    for (var i = 0; i < 8; i++) {
      corners[i] = cl[i];
    }
    final ok = _warp(input, src.width, src.height, corners, out, dw, dh);
    if (ok == 0) {
      throw StateError('Degenerate quad, homography solve failed');
    }
    return RgbaImage(
      Uint8List.fromList(out.asTypedList(dw * dh * 4)),
      dw,
      dh,
    );
  } finally {
    calloc.free(input);
    calloc.free(corners);
    calloc.free(out);
  }
}