Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization for printing tall images #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions ESCPOS_NET/Emitters/BaseCommandEmitter/ImageCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

using System.Drawing;

using static System.Net.Mime.MediaTypeNames;

namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
Expand Down Expand Up @@ -40,6 +44,22 @@ public virtual byte[] SetImageDensity(bool isHiDPI)
}

public virtual byte[] BufferImage(byte[] image, int maxWidth = -1, bool isLegacy = false, int color = 1)
{
int width;
int height;
byte[] imageData;
using (var img = SixLabors.ImageSharp.Image.Load<Rgba32>(image))
{
imageData = img.ToSingleBitPixelByteArray(maxWidth: maxWidth == -1 ? (int?)null : maxWidth);
height = img.Height;
width = img.Width;
}

return BufferImageSingleBitPixelByteArray(imageData, width, height, isLegacy, color);
}

public virtual byte[] BufferImageSingleBitPixelByteArray(byte[] singleBitPixelByteArray,
int width, int height, bool isLegacy = false, int color = 1)
{
ByteArrayBuilder imageCommand = new ByteArrayBuilder();

Expand All @@ -57,16 +77,6 @@ public virtual byte[] BufferImage(byte[] image, int maxWidth = -1, bool isLegacy
break;
}

int width;
int height;
byte[] imageData;
using (var img = Image.Load<Rgba32>(image))
{
imageData = img.ToSingleBitPixelByteArray(maxWidth: maxWidth == -1 ? (int?)null : maxWidth);
height = img.Height;
width = img.Width;
}

byte heightL = (byte)height;
byte heightH = (byte)(height >> 8);

Expand All @@ -84,7 +94,7 @@ public virtual byte[] BufferImage(byte[] image, int maxWidth = -1, bool isLegacy
imageCommand.Append(new byte[] { 0x30, 0x70, 0x30, 0x01, 0x01, colorByte, widthL, widthH, heightL, heightH });
}

imageCommand.Append(imageData);
imageCommand.Append(singleBitPixelByteArray);

// Load image to print buffer
ByteArrayBuilder response = new ByteArrayBuilder();
Expand Down Expand Up @@ -119,5 +129,17 @@ public virtual byte[] PrintImage(byte[] image, bool isHiDPI, bool isLegacy = fal
return ByteSplicer.Combine(SetImageDensity(isHiDPI), BufferImage(image, maxWidth, isLegacy, color), WriteImageFromBuffer());
}
}

public virtual byte[] PrintImageSingleBitPixelByteArray(byte[] singleBitPixelByteArray, int width, int height, bool isHiDPI, bool isLegacy = false, int maxWidth = -1, int color = 1)
{
if (isLegacy)
{
return ByteSplicer.Combine(BufferImageSingleBitPixelByteArray(singleBitPixelByteArray, width, height, isLegacy));
}
else
{
return ByteSplicer.Combine(SetImageDensity(isHiDPI), BufferImageSingleBitPixelByteArray(singleBitPixelByteArray, width, height, isLegacy, color), WriteImageFromBuffer());
}
}
}
}