imageRaster method

List<int> imageRaster(
  1. Image image, {
  2. PosAlign align = PosAlign.center,
  3. bool highDensityHorizontal = true,
  4. bool highDensityVertical = true,
  5. PosImageFn imageFn = PosImageFn.bitImageRaster,
})

Print an image using (GS v 0) obsolete command

image is an instanse of class from Image library

Implementation

List<int> imageRaster(
  Image image, {
  PosAlign align = PosAlign.center,
  bool highDensityHorizontal = true,
  bool highDensityVertical = true,
  PosImageFn imageFn = PosImageFn.bitImageRaster,
}) {
  List<int> bytes = [];
  // Image alignment
  bytes += setStyles(PosStyles().copyWith(align: align));

  final int widthPx = image.width;
  final int heightPx = image.height;
  final int widthBytes = (widthPx + 7) ~/ 8;
  final List<int> resterizedData = _toRasterFormat(image);

  if (imageFn == PosImageFn.bitImageRaster) {
    // GS v 0
    final int densityByte =
        (highDensityVertical ? 0 : 1) + (highDensityHorizontal ? 0 : 2);

    void raster(int xBytes, int rows, Iterable<int> data) =>
        bytes
          ..addAll(cRasterImg2.codeUnits)
          ..add(densityByte) // m
          ..addAll(_intLowHigh(xBytes, 2)) // xL xH
          ..addAll(_intLowHigh(rows, 2)) // yL yH
          ..addAll(data);

    // A run of blank rows goes as a one-byte-wide white raster of the same
    // height: the printer feeds the same dots, but sends `rows` bytes, not
    // `widthBytes x rows` (receipts are a third or more white -- seconds
    // over Bluetooth). Short gaps (under [minBlankRun] rows, e.g. between
    // text lines) stay inside the image so it is not cut into slivers.
    const int minBlankRun = 8;
    bool blankRow(int y) {
      final int o = y * widthBytes;
      for (int i = 0; i < widthBytes; i++) {
        if (resterizedData[o + i] != 0) return false;
      }
      return true;
    }

    int y = 0;
    while (y < heightPx) {
      int end = y;
      while (end < heightPx && blankRow(end)) {
        end++;
      }
      if (end - y >= minBlankRun || (end == heightPx && end > y)) {
        raster(1, end - y, List<int>.filled(end - y, 0));
        y = end;
        continue;
      }
      // Ink from y up to the next blank run long enough to split at.
      int inkEnd = end;
      while (inkEnd < heightPx) {
        int b = inkEnd;
        while (b < heightPx && blankRow(b)) {
          b++;
        }
        if (b - inkEnd >= minBlankRun || b == heightPx) break;
        inkEnd = b + 1;
      }
      if (inkEnd > heightPx) inkEnd = heightPx;
      raster(
        widthBytes,
        inkEnd - y,
        resterizedData.sublist(y * widthBytes, inkEnd * widthBytes),
      );
      y = inkEnd;
    }
  } else if (imageFn == PosImageFn.graphics) {
    // 'GS ( L' - FN_112 (Image data)
    final List<int> header1 = List.from(cRasterImg.codeUnits);
    header1.addAll(_intLowHigh(widthBytes * heightPx + 10, 2)); // pL pH
    header1.addAll([48, 112, 48]); // m=48, fn=112, a=48
    header1.addAll([1, 1]); // bx=1, by=1
    header1.addAll([49]); // c=49
    header1.addAll(_intLowHigh(widthBytes, 2)); // xL xH
    header1.addAll(_intLowHigh(heightPx, 2)); // yL yH
    bytes += List.from(header1)..addAll(resterizedData);

    // 'GS ( L' - FN_50 (Run print)
    final List<int> header2 = List.from(cRasterImg.codeUnits);
    header2.addAll([2, 0]); // pL pH
    header2.addAll([48, 50]); // m fn[2,50]
    bytes += List.from(header2);
  }
  return bytes;
}