encodePng method

Uint8List encodePng(
  1. SolutionGrid solution, {
  2. Palette palette = Palette.monochrome,
})

Renders a logical grid as one PNG pixel per cell.

Implementation

Uint8List encodePng(
  SolutionGrid solution, {
  Palette palette = Palette.monochrome,
}) {
  if (solution.cells.any((c) => c >= palette.length)) {
    throw ArgumentError('Solution references a missing palette color.');
  }
  final result = image.Image(
    width: solution.width,
    height: solution.height,
    numChannels: 4,
  );
  for (var r = 0; r < solution.height; r++) {
    for (var c = 0; c < solution.width; c++) {
      final argb = palette[solution.colorAt(r, c)].argb;
      result.setPixelRgba(
        c,
        r,
        (argb >> 16) & 255,
        (argb >> 8) & 255,
        argb & 255,
        (argb >> 24) & 255,
      );
    }
  }
  return image.encodePng(result);
}