-
Notifications
You must be signed in to change notification settings - Fork 1
fix(ArtQRCode): scale and dispose background image #88
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
Changes from all commits
2a5db5c
375b7ca
5384c38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -197,11 +197,23 @@ private SKBitmap RenderGraphicCore(int pixelsPerModule, SKColor darkSKColor, SKC | |
| if (backgroundImage != null) | ||
| { | ||
| if (backgroundImageStyle == BackgroundImageStyle.Fill) | ||
| graphics.DrawBitmap(Resize(backgroundImage, size), 0, 0); | ||
| { | ||
| using (var resizedImage = Resize(backgroundImage, size)) | ||
| { | ||
| if (resizedImage != null) | ||
| graphics.DrawBitmap(resizedImage, 0, 0); | ||
| graphics.Flush(); | ||
| } | ||
| } | ||
| else if (backgroundImageStyle == BackgroundImageStyle.DataAreaOnly) | ||
| { | ||
| var bgOffset = 4 - offset; | ||
| graphics.DrawBitmap(Resize(backgroundImage, size - (2 * bgOffset * pixelsPerModule)), 0 + (bgOffset * pixelsPerModule), (bgOffset * pixelsPerModule)); | ||
| using (var resizedImage = Resize(backgroundImage, size - (2 * bgOffset * pixelsPerModule))) | ||
| { | ||
| if (resizedImage != null) | ||
| graphics.DrawBitmap(resizedImage, 0 + (bgOffset * pixelsPerModule), (bgOffset * pixelsPerModule)); | ||
| graphics.Flush(); | ||
| } | ||
|
Comment on lines
+201
to
+216
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: Old code also leaked the scaledImage bitmap — fixed by this PR In the old Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -316,34 +328,34 @@ private static bool IsPartOfFinderPattern(int x, int y, int numModules, int offs | |
| /// <summary> | ||
| /// Resize to a square bitmap, but maintain the aspect ratio by padding transparently. | ||
| /// </summary> | ||
| /// <param name="image"></param> | ||
| /// <param name="newSize"></param> | ||
| /// <returns>Resized image as bitmap</returns> | ||
| private SKBitmap Resize(SKBitmap image, int newSize) | ||
| /// <param name="image">Source image to resize.</param> | ||
| /// <param name="newSize">Target side length of the square output.</param> | ||
| /// <returns>Resized image as bitmap, or null if the source image is invalid.</returns> | ||
| private static SKBitmap Resize(SKBitmap image, int newSize) | ||
| { | ||
| if (image == null) return null; | ||
| if (image == null || image.Width == 0 || image.Height == 0 || newSize <= 0) | ||
| return null; | ||
|
|
||
| float scale = Math.Min((float)newSize / image.Width, (float)newSize / image.Height); | ||
| var scaledWidth = (int)(image.Width * scale); | ||
| var scaledHeight = (int)(image.Height * scale); | ||
| var scaledWidth = Math.Max(1, (int)(image.Width * scale)); | ||
| var scaledHeight = Math.Max(1, (int)(image.Height * scale)); | ||
| var offsetX = (newSize - scaledWidth) / 2; | ||
| var offsetY = (newSize - scaledHeight) / 2; | ||
|
|
||
| var scaledImage = new SKBitmap(scaledWidth, scaledHeight); | ||
|
|
||
| var bm = new SKBitmap(newSize, newSize); | ||
|
|
||
| using (var graphics = new SKCanvas(bm)) | ||
| using (var scaledImage = image.Resize(new SKSizeI(scaledWidth, scaledHeight), new SKSamplingOptions(SKFilterMode.Linear))) | ||
| { | ||
| using (var brush = new SKPaint { Color = SKColors.Transparent, }) | ||
| { | ||
| graphics.DrawRect(new SKRect(0, 0, newSize, newSize), brush); | ||
| brush.IsAntialias = true; | ||
| if (scaledImage == null) | ||
| return null; | ||
|
|
||
| graphics.DrawBitmap(scaledImage, new SKRect(offsetX, offsetY, offsetX + scaledWidth, offsetY + scaledHeight)); | ||
| var bm = new SKBitmap(newSize, newSize); | ||
| using (var graphics = new SKCanvas(bm)) | ||
| { | ||
| graphics.Clear(SKColors.Transparent); | ||
| graphics.DrawBitmap(scaledImage, offsetX, offsetY); | ||
| graphics.Flush(); | ||
| } | ||
| return bm; | ||
| } | ||
| return bm; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Info: Redundant graphics.Flush() calls inside background image rendering blocks
The
graphics.Flush()calls at lines 205 and 215 are called immediately after drawing the background image, but before the module dots are rendered. There is already agraphics.Flush()at line 249 after all rendering (modules + finder patterns) is complete. The intermediate flushes are unnecessary —SKCanvas.Flush()forces pending draw operations to be submitted, but SkiaSharp doesn't require intermediate flushes between draw calls on the same canvas. These add minor overhead without benefit.Was this helpful? React with 👍 or 👎 to provide feedback.