process method

  1. @override
ProcessedResult process(
  1. ProcessInput input
)
override

Implementation

@override
ProcessedResult process(ProcessInput input) {
  img.Image image = input.image;

  var width = image.width;
  var height = image.height;

  int alpha, red;
  int gray;

  int error = 0;
  var errors = List.generate(
    width,
    (i) => List.filled(height, 0),
    growable: false,
  );
  for (int y = 0; y < height - 1; y++) {
    for (int x = 2; x < width - 2; x++) {
      var pixel = ImagebTool.extractColor(image, x, y);
      alpha = pixel.a.toInt();
      red = pixel.r.toInt();

      gray = red;
      if (gray + errors[x][y] < threshold) {
        error = gray + errors[x][y];
        gray = 0;
      } else {
        error = gray + errors[x][y] - 255;
        gray = 255;
      }

      errors[x + 1][y] += (4 * error) ~/ 16;
      errors[x + 2][y] += (3 * error) ~/ 16;

      errors[x - 2][y + 1] += (1 * error) ~/ 16;
      errors[x - 1][y + 1] += (2 * error) ~/ 16;
      errors[x][y + 1] += (3 * error) ~/ 16;
      errors[x + 1][y + 1] += (2 * error) ~/ 16;
      errors[x + 2][y + 1] += (1 * error) ~/ 16;
      image.setPixelRgba(x, y, gray, gray, gray, alpha);
    }
  }

  var ret = img.encodePng(image);
  return ProcessedResult(
    origin: input.originBytes,
    result: ProcessedImage(
      data: Uint8List.fromList(ret),
      width: width,
      height: height,
    ),
  );
}