convert method

  1. @override
T convert(
  1. Buffer<int> input, {
  2. Iterable<String> comments = const [],
  3. NetpbmFormat? format,
})
override

Converts the pixel data to a Netpbm image.

The comments are added to the image.

If the format is omitted, and NetpbmEncoder.format is not set, the format is inferred from the pixel data.

Implementation

@override
T convert(
  Buffer<int> input, {
  Iterable<String> comments = const [],
  NetpbmFormat? format,
}) {
  format ??= _getOrInferFormat(input);
  final header = NetpbmHeader(
    width: input.width,
    height: input.height,
    max: switch (format) {
      NetpbmFormat.bitmap => null,
      NetpbmFormat.graymap => gray8.maxGray,
      NetpbmFormat.pixmap => rgb888.maxRed,
    },
    format: format,
    comments: comments,
  );
  final Iterable<int> pixels;
  switch (format) {
    case NetpbmFormat.bitmap:
      pixels = input.data.map((value) {
        return value == input.format.zero ? 0 : 1;
      });
    case NetpbmFormat.graymap:
      pixels = input.data.map((value) {
        return gray8.convert(value, from: input.format);
      });
    case NetpbmFormat.pixmap:
      pixels = input.data.map((value) {
        final rgb = rgb888.convert(value, from: input.format);
        return [
          rgb888.getRed(rgb),
          rgb888.getGreen(rgb),
          rgb888.getBlue(rgb),
        ];
      }).expand((p) => p);
  }
  return _convert(header, pixels);
}