putImageObject method

Future<int> putImageObject(
  1. Uint8List rgba,
  2. int width,
  3. int height
)

Implementation

Future<int> putImageObject(Uint8List rgba, int width, int height) async {
  final int pixels = width * height;
  final Uint8List colour = Uint8List(pixels * 3);
  final Uint8List alpha = Uint8List(pixels);
  bool transparent = false;
  for (int i = 0; i < pixels; i++) {
    colour[i * 3] = rgba[i * 4];
    colour[i * 3 + 1] = rgba[i * 4 + 1];
    colour[i * 3 + 2] = rgba[i * 4 + 2];
    alpha[i] = rgba[i * 4 + 3];
    if (alpha[i] != 255) transparent = true;
  }
  int? maskNumber;
  if (transparent) {
    maskNumber = allocate();
    put(
      maskNumber,
      buildStream(
        alpha,
        extra: <String, Object?>{
          "Type": const UPdfName("XObject"),
          "Subtype": const UPdfName("Image"),
          "Width": width,
          "Height": height,
          "ColorSpace": const UPdfName("DeviceGray"),
          "BitsPerComponent": 8,
        },
      ),
    );
  }
  final int number = allocate();
  put(
    number,
    buildStream(
      colour,
      extra: <String, Object?>{
        "Type": const UPdfName("XObject"),
        "Subtype": const UPdfName("Image"),
        "Width": width,
        "Height": height,
        "ColorSpace": const UPdfName("DeviceRGB"),
        "BitsPerComponent": 8,
        if (maskNumber != null) "SMask": UPdfRef(maskNumber, 0),
      },
    ),
  );
  return number;
}