image method

List<int> image(
  1. Image imgSrc, {
  2. PosAlign align = PosAlign.center,
})

Print an image using (ESC *) command

image is an instanse of class from Image library

Implementation

List<int> image(Image imgSrc, {PosAlign align = PosAlign.center}) {
  final List<int> bytes = [];
  // Image alignment
  bytes.addAll(setStyles(PosStyles().copyWith(align: align)));

  // One pass straight from the RGBA bytes into 24-dot column strips. The
  // old way copied, inverted, flipped and rotated the whole receipt, padded
  // it into another copy, cropped and greyscaled every strip and packed
  // bits through a function call per pixel -- seconds on a long bill on a
  // TV box or cheap tablet, before the printer got a byte. The flip +
  // 270-degree rotation is a transpose, so a strip is just 24 source rows
  // read column by column; the bytes sent are the same (a pixel darker
  // than mid grey prints, top pixel in the high bit).
  final Image image =
      imgSrc.format == Format.uint8 && imgSrc.numChannels == 4
          ? imgSrc
          : imgSrc.convert(format: Format.uint8, numChannels: 4);
  final int width = image.width;
  final int height = image.height;
  final Uint8List rgba = image.getBytes(order: ChannelOrder.rgba);

  const int stripDots = 24; // 24-dot double density (m = 33)
  // As before: rows padded up past the last full strip (always at least
  // one blank strip more), so printers feed exactly as they did.
  final int strips = (height + stripDots - height % stripDots) ~/ stripDots;

  final List<int> header =
      List.from(cBitImg.codeUnits)
        ..add(1 + 32)
        ..addAll(_intLowHigh(width, 2));

  // A strip with no ink goes as a one-column white image (9 bytes, not
  // width x 3): still a 24-dot bit image, so the printer feeds exactly as
  // for the full strip. A receipt is a third or more white rows, and over
  // Bluetooth (10-30 KB/s) every byte is time.
  final List<int> blankStrip =
      List.from(cBitImg.codeUnits)
        ..add(1 + 32)
        ..addAll(_intLowHigh(1, 2))
        ..addAll(const [0, 0, 0, 0x0A]);

  // Adjust line spacing (for 16-unit line feeds): ESC 3 0x10
  bytes.addAll(const [27, 51, 16]);
  final Uint8List strip = Uint8List(width * 3);
  for (int s = 0; s < strips; s++) {
    strip.fillRange(0, strip.length, 0);
    var inked = false;
    final int top = s * stripDots;
    for (int dy = 0; dy < stripDots; dy++) {
      final int y = top + dy;
      if (y >= height) break;
      final int byteInCol = dy >> 3;
      final int bit = 0x80 >> (dy & 7);
      int src = y * width * 4;
      for (int x = 0; x < width; x++, src += 4) {
        if (_isInk(rgba, src)) {
          strip[x * 3 + byteInCol] |= bit;
          inked = true;
        }
      }
    }
    if (!inked) {
      bytes.addAll(blankStrip);
      continue;
    }
    bytes
      ..addAll(header)
      ..addAll(strip)
      ..add(0x0A);
  }
  // Reset line spacing: ESC 2
  bytes.addAll(const [27, 50]);
  return bytes;
}