image method

void image(
  1. Image subImage, {
  2. int? width,
  3. int? height,
  4. PrintAlign align = PrintAlign.left,
})

Draw image (resized)

Implementation

void image(
  img.Image subImage, {
  int? width,
  int? height,
  PrintAlign align = PrintAlign.left,
}) {
  final resized = copyResize(
    subImage,
    width: width ?? subImage.width,
    height: height ?? subImage.height,
  );

  _ensureHeight(runningHeight + resized.height + 10);

  int posX;
  final posY = runningHeight;

  if (align == PrintAlign.center) {
    posX = ((paperSize.width - resized.width) / 2).round();
  } else if (align == PrintAlign.right) {
    posX = paperSize.width - resized.width - margin.right;
  } else {
    posX = margin.left;
  }

  for (int sy = 0; sy < resized.height; sy++) {
    for (int sx = 0; sx < resized.width; sx++) {
      final pixel = resized.getPixel(sx, sy).current;

      // Ensure the pixel coordinates are within bounds
      final targetX = posX + sx;
      final targetY = posY + sy;
      if (targetX >= 0 &&
          targetX < utilImage.width &&
          targetY >= 0 &&
          targetY < utilImage.height) {
        utilImage.setPixel(targetX, targetY, pixel);
      }
    }
  }
  runningHeight += (resized.height + 10);
}