decodeImageRgb function

DecodedRgbImage decodeImageRgb(
  1. Uint8List encoded, {
  2. int maxEncodedBytes = defaultMaxEncodedImageBytes,
  3. int maxPixels = defaultMaxDecodedImagePixels,
})

Decodes one still PNG or JPEG to RGB8 without resizing. JPEG EXIF orientation is applied by the decoder. Alpha is composited over white in channel space. PNG samples use stored orientation; ICC color conversion is not performed. Pixel limits are supplied to both decoders before raster allocation.

Implementation

DecodedRgbImage decodeImageRgb(
  Uint8List encoded, {
  int maxEncodedBytes = defaultMaxEncodedImageBytes,
  int maxPixels = defaultMaxDecodedImagePixels,
}) {
  _checkLimits(maxEncodedBytes, maxPixels);
  _checkSize(encoded.length, maxEncodedBytes);
  final image.Decoder decoder;
  if (encoded.length >= 8 && image.PngDecoder().isValidFile(encoded)) {
    decoder = image.PngDecoder(maxPixels: maxPixels);
  } else if (encoded.length >= 2 && encoded[0] == 0xff && encoded[1] == 0xd8) {
    decoder = image.JpegDecoder(maxPixels: maxPixels);
  } else {
    throw const FormatException('expected a still PNG or JPEG image');
  }
  try {
    final info = decoder.startDecode(encoded);
    // Division keeps malicious PNG dimensions from overflowing an int product.
    if (info == null ||
        info.width <= 0 ||
        info.height <= 0 ||
        info.width > maxPixels ~/ info.height) {
      throw FormatException('invalid image or more than $maxPixels pixels');
    }
    if (info.numFrames != 1 || (info is image.PngInfo && info.isAnimated)) {
      throw const FormatException('animated images are not supported');
    }
    final decoded = decoder.decodeFrame(0);
    if (decoded == null ||
        decoded.width <= 0 ||
        decoded.height <= 0 ||
        decoded.width > maxPixels ~/ decoded.height) {
      throw const FormatException('invalid decoded image dimensions');
    }
    final rgb = Uint8List(decoded.width * decoded.height * 3);
    var offset = 0;
    final grayscale = !decoded.hasPalette && decoded.numChannels < 3;
    for (final pixel in decoded) {
      final alpha = pixel.aNormalized;
      int channel(num value) =>
          ((value * alpha + 1 - alpha) * 255).round().clamp(0, 255);
      rgb[offset++] = channel(pixel.rNormalized);
      rgb[offset++] = channel(
        grayscale ? pixel.rNormalized : pixel.gNormalized,
      );
      rgb[offset++] = channel(
        grayscale ? pixel.rNormalized : pixel.bNormalized,
      );
    }
    return (pixels: rgb, width: decoded.width, height: decoded.height);
  } on image.ImageException catch (error) {
    throw FormatException('invalid PNG/JPEG image: $error');
  }
}