convert method

  1. @override
  2. @nonVirtual
Buffer<int> convert(
  1. T input, {
  2. PixelFormat<int, void>? format,
})
override

Converts the input to integer-based pixel data.

If the input is invalid, a FormatException is thrown.

The format is used to convert the pixel data to the desired format; if omitted, the decoder's NetpbmDecoder.format is used, which defaults to abgr8888.

Implementation

@override
@nonVirtual
Buffer<int> convert(T input, {PixelFormat<int, void>? format}) {
  final (header, error, offset) = _parseHeader(input);
  if (header == null) {
    throw FormatException(error, input);
  }
  final fmt = format ?? this.format;
  final data = _data(
    input,
    offset,
    bitmap: header.format == NetpbmFormat.bitmap,
  );
  final Iterable<int> pixels;
  switch (header.format) {
    case NetpbmFormat.bitmap:
      pixels = data.map((value) {
        return value == 0x0 ? fmt.zero : fmt.max;
      });
    case NetpbmFormat.graymap:
      pixels = data.map((value) {
        final gray = gray8.create(gray: value);
        return fmt.convert(gray, from: gray8);
      });
    case NetpbmFormat.pixmap:
      if (data.length % 3 != 0) {
        throw FormatException('Invalid pixel data', input, offset);
      }
      pixels = Iterable.generate(data.length ~/ 3, (i) {
        final r = data[i * 3];
        final g = data[i * 3 + 1];
        final b = data[i * 3 + 2];
        final rgb = rgb888.create(red: r, green: g, blue: b);
        return fmt.convert(rgb, from: rgb888);
      });
  }

  final buffer = IntPixels(header.width, header.height, format: fmt);
  buffer.data.setAll(0, pixels);
  return buffer;
}