apply method

  1. @override
TensorImage apply(
  1. TensorImage image
)
override

Applies the defined resizing with cropping or/and padding on image and returns the result.

Note: the content of input image will change, and image is the same instance with the output.

Implementation

@override
TensorImage apply(TensorImage image) {
  Image input = image.image;
  int srcL;
  int srcR;
  int srcT;
  int srcB;
  int dstL;
  int dstR;
  int dstT;
  int dstB;
  int w = input.width;
  int h = input.height;
  int? cropWidthCustomPosition = _cropLeft;
  int? cropHeightCustomPosition = _cropTop;

  _checkCropPositionArgument(w, h);

  if (_targetWidth > w) {
    // padding
    srcL = 0;
    srcR = w;
    dstL = (_targetWidth - w) ~/ 2;
    dstR = dstL + w;
  } else {
    // cropping
    dstL = 0;
    dstR = _targetWidth;
    // custom crop position. First item of the tuple represent the desired position for left position
    // and the second item the right position
    Tuple2<int, int> cropPos =
        _computeCropPosition(_targetWidth, w, cropWidthCustomPosition);
    srcL = cropPos.item1;
    srcR = cropPos.item2;
  }
  if (_targetHeight > h) {
    // padding
    srcT = 0;
    srcB = h;
    dstT = (_targetHeight - h) ~/ 2;
    dstB = dstT + h;
  } else {
    // cropping
    dstT = 0;
    dstB = _targetHeight;
    // custom crop position. First item of the tuple represent the desired position for top position
    // and the second item the bottom position
    Tuple2<int, int> cropPos =
        _computeCropPosition(_targetHeight, h, cropHeightCustomPosition);
    srcT = cropPos.item1;
    srcB = cropPos.item2;
  }

  Image resized = _drawImage(_output, image.image,
      dstX: dstL,
      dstY: dstT,
      dstH: dstB - dstT,
      dstW: dstR - dstL,
      srcX: srcL,
      srcY: srcT,
      srcH: srcB - srcT,
      srcW: srcR - srcL);

  image.loadImage(resized);

  return image;
}