Navigation Menu

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

Speedup drawing of reversed elements #64

Closed
wants to merge 1 commit into from

Conversation

M-Mommsen
Copy link
Contributor

InvertDraw calls are very slow, so this is an attempt to reduce them as much as possible by drawing only one inverted canvas for all reversed elements and even this one only if needed.
In my tests with a GLS label, the calculation time went from ~12s to 1s !

InvertDraw calls are very slow, so this is an attempt to reduce them as much as possible by drawing only one inverted canvas for all reversed elements and even this one only if needed.
@YipingRuan
Copy link
Contributor

Very cool.

@M-Mommsen
Copy link
Contributor Author

In theory there is one condition where the result would be "wrong". That would be reversing an already reversed pixel. But the calculation time of over 10s is simply not acceptable. Maybe there is a faster function available for combining the bitmaps.

@YipingRuan YipingRuan requested review from YipingRuan and removed request for tinohager August 30, 2021 15:33
@YipingRuan YipingRuan self-assigned this Aug 30, 2021
@YipingRuan
Copy link
Contributor

YipingRuan commented Aug 30, 2021

Is it possible to provide your sample label for my testing, if no privacy issue?

At the moment please leave this as draft.

@tinohager
Copy link
Collaborator

The result is no longer correct with the changes.

Correct
correct
Incorrect
Incorrect

@tinohager
Copy link
Collaborator

I have already thought about this topic. But I haven't got around to optimizing the logic yet. The time consuming part is the verification of each pixel. The bigger the label is, the longer it takes. A sensible solution would therefore be to determine where in the last step was drawn and to match only this area I think with this solution this step could be significantly accelerated.

@M-Mommsen
Copy link
Contributor Author

Is it possible to provide your sample label for my testing, if no privacy issue?

At the moment please leave this as draft.

https://pastebin.com/F4djyBEX

I did all this for EPL once. Back then I decided to just paint with reversed colors. I directly drew to a windows form and speed was essential (and I needed texts to be selectable, highlightable and to support text copy). There was no noticable delay, and now we need to switch from epl to zpl. With a 10 second delay nobody would want to use that app anymore. So maybe a 'draft' or 'preview' rendering mode could help ?

Btw. my final step will now be adding datamatrix support. But I guess I will open a separate issue for that. Those 2D barcodes are also part of every GLS label - and probably any other real life shipping label.

@M-Mommsen
Copy link
Contributor Author

What about using XOR SKBlendMode for reversed drawing in the first place ?
I did not attempt this, because it looks like a major change, and I don't programm that much C# , but at first glance, setting
skPaint.BlendMode = SKBlendMode.Xor;
should work.
Other possibility would be to use bitblit to combine the canvas'.

@tinohager
Copy link
Collaborator

What about using XOR SKBlendMode for reversed drawing in the first place ?
I did not attempt this, because it looks like a major change, and I don't programm that much C# , but at first glance, setting
skPaint.BlendMode = SKBlendMode.Xor;
should work.
Other possibility would be to use bitblit to combine the canvas'.

I'll check that I haven't had time to deal with this module yet.

@M-Mommsen
Copy link
Contributor Author

The result is no longer correct with the changes.

Correct
correct
Incorrect
Incorrect

btw. I think the images are switched, the correct one is the one that shows double inversions.

@tinohager
Copy link
Collaborator

@M-Mommsen Can you print our test label on an original Zebra Printer?

@tinohager
Copy link
Collaborator

@M-Mommsen i have create a test script with the SKBlendMode.Xor

The blend mode of SKBlendMode.Xor (exclusive OR) causes nothing to appear where the two areas overlap

using var skBitmap = new SKBitmap(100, 40);
using var skCanvas = new SKCanvas(skBitmap);
skCanvas.Clear(SKColors.White);

using var skPaint = new SKPaint();
skPaint.Style = SKPaintStyle.Fill;
skPaint.BlendMode = SKBlendMode.Xor;

skPaint.Color = SKColors.White;
skCanvas.DrawRect(10, 10, 20, 20, skPaint);
skPaint.Color = SKColors.White;
skCanvas.DrawRect(20, 10, 20, 20, skPaint);
skPaint.Color = SKColors.White;
skCanvas.DrawRect(30, 10, 20, 20, skPaint);

using var data = skBitmap.Encode(SKEncodedImageFormat.Png, 80);
using var fileStream = new FileStream("test.png", FileMode.Create);
data.SaveTo(fileStream);
fileStream.Flush();

But white to white should be black

^XA

^FO10,10
^GB20,20,10,W,0^FS

^FR
^FO20,10
^GB20,20,10,W,0^FS

^FR
^FO30,10
^GB20,20,10,W,0^FS

^XZ

@M-Mommsen
Copy link
Contributor Author

M-Mommsen commented Sep 1, 2021

The blend mode of SKBlendMode.Xor (exclusive OR) causes nothing to appear where the two areas overlap

That's great!

But white to white should be black

Why ? White = nothing is painted or reversed - so white to white should stay white! Only black and white (or white and black) become black!

Could you try:
^XA
^PR1
^FO100,100
^GB70,70,70,,3^FS
^FO200,100
^GB70,70,70,,3^FS
^FO300,100
^GB70,70,70,,3^FS
^FO400,100
^GB70,70,70,,3^FS
^FO107,110^CF0,70,93
^FR^FDREVERSE^FS
^XZ

This should result in:
Screenshot 2021-09-01 070653

One other doc says:
^FR
Configures the current field to reverse the background color, drawing black over white and white over black. This is also known as XOR mode drawing.

@M-Mommsen
Copy link
Contributor Author

@M-Mommsen Can you print our test label on an original Zebra Printer?

I don't know where the file is located :-( ...
But you could also just use http://labelary.com/viewer.html

@tinohager
Copy link
Collaborator

tinohager commented Sep 1, 2021

^XA

^FO10,10
^GB20,20,10,W,0^FS

^FR
^FO20,10
^GB20,20,10,W,0^FS

^FR
^FO30,10
^GB20,20,10,W,0^FS

^XZ

Just tested on an original Zebra printer. White on white results in black.

thumbnail_IMG_6924

@M-Mommsen
Copy link
Contributor Author

Just tested on an original Zebra printer. White on white results in black.

Hmm - documentation is different. So is it a printer quirk ? We could simulate this behaviour by ignoring the W parameter when printing reversed.

^XA

^FO10,10
^GB20,20,10,W,0^FS

^FR
^FO20,10
^GB20,20,10,B,0^FS

^FR
^FO30,10
^GB20,20,10,B,0^FS

^XZ

Yes - just tested it with the same result! So the zebra is simply ignoring the color in reverse mode!

@YipingRuan YipingRuan marked this pull request as draft September 1, 2021 11:30
@YipingRuan YipingRuan mentioned this pull request Sep 3, 2021
@YipingRuan
Copy link
Contributor

@M-Mommsen
Try the new master?
Will not be in NuGet until @tinohager increases the package version.

@M-Mommsen
Copy link
Contributor Author

@M-Mommsen
Try the new master?

The rendering now takes ~0.6s, which is still not great, but so much better than the 10s plus before!

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.

None yet

3 participants