cropDownsamplePdfDecodedPixels function

PdfDecodedPixels? cropDownsamplePdfDecodedPixels(
  1. PdfDecodedPixels decoded,
  2. int sourceX,
  3. int sourceY,
  4. int sourceWidth,
  5. int sourceHeight,
  6. int targetWidth,
  7. int targetHeight,
)

Crops premultiplied decoded to the source rectangle and downsamples it to targetWidth×targetHeight. Returns null when the rectangle falls outside decoded. Shared by decodePdfImage's full-decode fallback and callers that already hold a decoded image to slice (a cached whole-image raster).

Implementation

PdfDecodedPixels? cropDownsamplePdfDecodedPixels(
  PdfDecodedPixels decoded,
  int sourceX,
  int sourceY,
  int sourceWidth,
  int sourceHeight,
  int targetWidth,
  int targetHeight,
) {
  if (sourceX < 0 ||
      sourceY < 0 ||
      sourceWidth <= 0 ||
      sourceHeight <= 0 ||
      sourceX + sourceWidth > decoded.width ||
      sourceY + sourceHeight > decoded.height) {
    return null;
  }
  final cropped = Uint8List(sourceWidth * sourceHeight * 4);
  for (var y = 0; y < sourceHeight; y++) {
    final srcOffset = ((sourceY + y) * decoded.width + sourceX) * 4;
    final dstOffset = y * sourceWidth * 4;
    cropped.setRange(
        dstOffset, dstOffset + sourceWidth * 4, decoded.rgba, srcOffset);
  }
  final pixels = PdfDecodedPixels(cropped, sourceWidth, sourceHeight);
  return downsamplePdfDecodedPixels(pixels, targetWidth, targetHeight);
}