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

Conversation

jjxtra
Copy link

@jjxtra jjxtra commented Nov 18, 2022

In some cases you have to chunk through images that are very tall (> 2048 pixels height). This allows to print from the raw bit pixel buffers of those images rather than having to save them to png and then pass the png bytes to the print image command.

Chunking code, for reference:

private static IEnumerable<Image<Rgba32>> ExtractChunks(Image<Rgba32> sourceImage)
{
    const int chunkSize = 256;
    for (var y = 0; y < sourceImage.Height; y += chunkSize)
    {
        Rectangle sourceArea = new(0, y, sourceImage.Width, Math.Min(sourceImage.Height - y, chunkSize));
        if (sourceArea.Height < 1)
        {
            yield break;
        }
        Image<Rgba32> targetImage = new(sourceArea.Width, sourceArea.Height);
        int height = sourceArea.Height;
        sourceImage.ProcessPixelRows(targetImage, (sourceAccessor, targetAccessor) =>
        {
            for (int i = 0; i < height; i++)
            {
                Span<Rgba32> sourceRow = sourceAccessor.GetRowSpan(sourceArea.Y + i);
                Span<Rgba32> targetRow = targetAccessor.GetRowSpan(i);
                sourceRow.Slice(sourceArea.X, sourceArea.Width).CopyTo(targetRow);
            }
        });
        yield return targetImage;
    }
}

And example usage:

var mainImage = SixLabors.ImageSharp.Image.Load<Rgba32>(img);
List<byte[]> items = new(16)
{
    e.CenterAlign(),
    e.PrintLine("CENTERED TEXT")
};
foreach (var image in ExtractChunks(mainImage))
{
    var bitPixels = image.ToSingleBitPixelByteArray();
    var bitPixelCommand = e.PrintImageSingleBitPixelByteArray(bitPixels, image.Width, image.Height, false, true);
    items.Add(bitPixelCommand);
}
items.Add(e.FullCutAfterFeed(4));
items.Add(e.FeedLines(4));
await printer.WriteAsync(items.ToArray());

In some cases you have to chunk through images that are very tall (> 2048 pixels height). This allows to print from the raw bit pixel buffers of those images rather than having to save them to png and then pass the png bytes to the print image command.
@jjxtra jjxtra changed the title Optimization for printing images Optimization for printing tall images Nov 18, 2022
@nivle
Copy link

nivle commented Jun 14, 2023

I couldn't get this to work, so I did this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants