PdfImage constructor

PdfImage(
  1. PdfDocument pdfDocument, {
  2. required Uint8List image,
  3. required int width,
  4. required int height,
  5. bool alpha = true,
  6. PdfImageOrientation orientation = PdfImageOrientation.topLeft,
})

Creates a new PdfImage instance.

Implementation

factory PdfImage(
  PdfDocument pdfDocument, {
  required Uint8List image,
  required int width,
  required int height,
  bool alpha = true,
  PdfImageOrientation orientation = PdfImageOrientation.topLeft,
}) {
  final im = PdfImage._(
    pdfDocument,
    width,
    height,
    orientation,
  );

  assert(() {
    im.startStopwatch();
    im.debugFill('RAW RGB${alpha ? 'A' : ''} Image ${width}x$height');
    return true;
  }());

  im.params['/BitsPerComponent'] = const PdfNum(8);
  im.params['/Name'] = PdfName(im.name);
  im.params['/ColorSpace'] = const PdfName('/DeviceRGB');

  if (alpha) {
    final _sMask = PdfImage._alpha(
      pdfDocument,
      image,
      width,
      height,
      orientation,
    );
    im.params['/SMask'] = PdfIndirect(_sMask.objser, 0);
  }

  final w = width;
  final h = height;
  final s = w * h;
  final out = Uint8List(s * 3);
  if (alpha) {
    for (var i = 0; i < s; i++) {
      out[i * 3] = image[i * 4];
      out[i * 3 + 1] = image[i * 4 + 1];
      out[i * 3 + 2] = image[i * 4 + 2];
    }
  } else {
    for (var i = 0; i < s; i++) {
      out[i * 3] = image[i * 3];
      out[i * 3 + 1] = image[i * 3 + 1];
      out[i * 3 + 2] = image[i * 3 + 2];
    }
  }

  im.buf.putBytes(out);
  assert(() {
    im.stopStopwatch();
    return true;
  }());
  return im;
}