-
-
Notifications
You must be signed in to change notification settings - Fork 20
Memory Improvements #90
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c413244
add console app for easy perf testing
djrecipe 39b029c
memory optimizations
djrecipe 2a98a27
CI: Merge all ci into one pr validation & install NET8 before buildling
mee-ironsoftware c2b3758
Merge branch 'develop' into use-Span
mee-ironsoftware File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
...ng/IronSoftware.Drawing.Common.ConsoleTest/IronSoftware.Drawing.Common.ConsoleTest.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\IronSoftware.Drawing.Common\IronSoftware.Drawing.Common.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
12 changes: 12 additions & 0 deletions
12
IronSoftware.Drawing/IronSoftware.Drawing.Common.ConsoleTest/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // See https://aka.ms/new-console-template for more information | ||
|
|
||
| using IronSoftware.Drawing; | ||
|
|
||
| ReadOnlySpan<byte> bytes = File.ReadAllBytes("test.bmp"); | ||
|
|
||
| for (int i=0; i<10000; i++) | ||
| { | ||
| using AnyBitmap bmp = new AnyBitmap(bytes); | ||
| var bin = bmp.GetBytes(); | ||
| Console.WriteLine(bin.Length); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| using System.Net.Http; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
| using System.Runtime.InteropServices.ComTypes; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace IronSoftware.Drawing | ||
|
|
@@ -451,12 +452,19 @@ public T ToBitmap<T>() | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Create a new Bitmap from a a Byte Span. | ||
| /// </summary> | ||
| /// <param name="span">A Byte Span of image data in any common format.</param> | ||
| public static AnyBitmap FromSpan(ReadOnlySpan<byte> span) | ||
|
Contributor
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. I can say "Wow" every time I see the word |
||
| { | ||
| return new AnyBitmap(span); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Create a new Bitmap from a a Byte Array. | ||
| /// </summary> | ||
| /// <param name="bytes">A ByteArray of image data in any common format.</param> | ||
| /// <seealso cref="FromBytes"/> | ||
| /// <seealso cref="AnyBitmap(byte[])"/> | ||
| public static AnyBitmap FromBytes(byte[] bytes) | ||
| { | ||
| return new AnyBitmap(bytes); | ||
|
|
@@ -485,6 +493,17 @@ public static AnyBitmap FromStream(Stream stream) | |
| { | ||
| return new AnyBitmap(stream); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Construct a new Bitmap from binary data (byte span). | ||
| /// </summary> | ||
| /// <param name="span">A byte span of image data in any common format.</param> | ||
| /// <seealso cref="AnyBitmap"/> | ||
| public AnyBitmap(ReadOnlySpan<byte> span) | ||
| { | ||
| LoadImage(span); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Construct a new Bitmap from binary data (bytes). | ||
| /// </summary> | ||
|
|
@@ -540,7 +559,7 @@ public AnyBitmap(AnyBitmap original, int width, int height) | |
| /// <seealso cref="AnyBitmap"/> | ||
| public AnyBitmap(string file) | ||
| { | ||
| LoadImage(file); | ||
| LoadImage(File.ReadAllBytes(file)); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -1986,68 +2005,29 @@ private void CreateNewImageInstance(int width, int height, Color backgroundColor | |
| Image.SaveAsBmp(stream); | ||
| Binary = stream.ToArray(); | ||
| } | ||
|
|
||
| private void LoadImage(byte[] bytes) | ||
| private void LoadImage(ReadOnlySpan<byte> bytes) | ||
| { | ||
| Format = Image.DetectFormat(bytes); | ||
| try | ||
| { | ||
| #if NET6_0_OR_GREATER | ||
| Image = Image.Load(bytes); | ||
| Format = Image.Metadata.DecodedImageFormat; | ||
| #else | ||
| Image = Image.Load(bytes, out IImageFormat format); | ||
| Format = format; | ||
| #endif | ||
| Binary = bytes; | ||
| } | ||
| catch (DllNotFoundException e) | ||
| { | ||
| throw new DllNotFoundException( | ||
| "Please install SixLabors.ImageSharp from NuGet.", e); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| try | ||
| { | ||
| if(Format is TiffFormat) | ||
| OpenTiffToImageSharp(bytes); | ||
| } | ||
| catch (Exception e) | ||
| else | ||
| { | ||
| throw new NotSupportedException( | ||
| "Image could not be loaded. File format is not supported.", e); | ||
| Binary = bytes.ToArray(); | ||
| Image = Image.Load(bytes); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void LoadImage(string file) | ||
| { | ||
| try | ||
| { | ||
| #if NET6_0_OR_GREATER | ||
| Image = Image.Load(file); | ||
| Format = Image.Metadata.DecodedImageFormat; | ||
| #else | ||
| Image = Image.Load(file, out IImageFormat format); | ||
| Format = format; | ||
| #endif | ||
| Binary = File.ReadAllBytes(file); | ||
| } | ||
| catch (DllNotFoundException e) | ||
| { | ||
| throw new DllNotFoundException( | ||
| "Please install SixLabors.ImageSharp from NuGet.", e); | ||
| } | ||
| catch (Exception) | ||
| catch (Exception e) | ||
| { | ||
| try | ||
| { | ||
| OpenTiffToImageSharp(File.ReadAllBytes(file)); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| throw new NotSupportedException( | ||
| "Image could not be loaded. File format is not supported.", e); | ||
| } | ||
| throw new NotSupportedException( | ||
| "Image could not be loaded. File format is not supported.", e); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -2064,14 +2044,6 @@ private void LoadImage(Stream stream) | |
| LoadImage(ms.ToArray()); | ||
| } | ||
|
|
||
| private void SetBinaryFromImageSharp(Image<Rgba32> tiffImage) | ||
| { | ||
| using var memoryStream = new MemoryStream(); | ||
| tiffImage.Save(memoryStream, new TiffEncoder()); | ||
| _ = memoryStream.Seek(0, SeekOrigin.Begin); | ||
| LoadImage(memoryStream); | ||
| } | ||
|
|
||
| private static AnyBitmap LoadSVGImage(string file) | ||
| { | ||
| try | ||
|
|
@@ -2224,7 +2196,7 @@ private static SKBitmap OpenTiffToSKBitmap(AnyBitmap anyBitmap) | |
| } | ||
| } | ||
|
|
||
| private void OpenTiffToImageSharp(byte[] bytes) | ||
| private void OpenTiffToImageSharp(ReadOnlySpan<byte> bytes) | ||
| { | ||
| try | ||
| { | ||
|
|
@@ -2233,7 +2205,7 @@ private void OpenTiffToImageSharp(byte[] bytes) | |
| List<Image> images = new(); | ||
|
|
||
| // create a memory stream out of them | ||
| using MemoryStream tiffStream = new(bytes); | ||
| using MemoryStream tiffStream = new(bytes.ToArray()); | ||
|
|
||
| // open a TIFF stored in the stream | ||
| using (var tif = Tiff.ClientOpen("in-memory", "r", tiffStream, new TiffStream())) | ||
|
|
@@ -2254,29 +2226,50 @@ private void OpenTiffToImageSharp(byte[] bytes) | |
|
|
||
| using Image<Rgba32> bmp = new(width, height); | ||
|
|
||
| byte[] bits = PrepareByteArray(bmp, raster, width, height); | ||
| var bits = PrepareByteArray(bmp, raster, width, height); | ||
|
|
||
| images.Add(Image.LoadPixelData<Rgba32>(bits, bmp.Width, bmp.Height)); | ||
| } | ||
| } | ||
|
|
||
| Image?.Dispose(); | ||
|
|
||
|
|
||
| // find max | ||
| FindMaxWidthAndHeight(images, out int maxWidth, out int maxHeight); | ||
|
|
||
| using Image<Rgba32> tiffImage = CloneAndResizeImageSharp(images[0], maxWidth, maxHeight); | ||
| // mute first image | ||
| images[0].Mutate(img => img.Resize(new ResizeOptions | ||
| { | ||
| Size = new Size(maxWidth, maxHeight), | ||
| Mode = ResizeMode.BoxPad, | ||
| PadColor = SixLabors.ImageSharp.Color.Transparent | ||
| })); | ||
|
|
||
| // iterate through images past the first | ||
| for (int i = 1; i < images.Count; i++) | ||
| { | ||
| Image<Rgba32> image = CloneAndResizeImageSharp(images[i], maxWidth, maxHeight); | ||
| _ = tiffImage.Frames.AddFrame(image.Frames.RootFrame); | ||
| } | ||
| // mute image | ||
| images[i].Mutate(img => img.Resize(new ResizeOptions | ||
| { | ||
| Size = new Size(maxWidth, maxHeight), | ||
| Mode = ResizeMode.BoxPad, | ||
| PadColor = SixLabors.ImageSharp.Color.Transparent | ||
| })); | ||
|
|
||
| SetBinaryFromImageSharp(tiffImage); | ||
| // add frames to first image | ||
| _ = images[0].Frames.AddFrame(images[i].Frames.RootFrame); | ||
|
|
||
| foreach (Image image in images) | ||
| { | ||
| image.Dispose(); | ||
| // dispose images past the first | ||
| images[i].Dispose(); | ||
| } | ||
|
|
||
| // get raw binary | ||
| using var memoryStream = new MemoryStream(); | ||
| images[0].Save(memoryStream, new TiffEncoder()); | ||
| memoryStream.Seek(0, SeekOrigin.Begin); | ||
|
|
||
| // store result | ||
| Binary = memoryStream.ToArray(); | ||
| Image?.Dispose(); | ||
| Image = images[0]; | ||
| } | ||
| catch (DllNotFoundException e) | ||
| { | ||
|
|
@@ -2288,7 +2281,7 @@ private void OpenTiffToImageSharp(byte[] bytes) | |
| } | ||
| } | ||
|
|
||
| private byte[] PrepareByteArray(Image<Rgba32> bmp, int[] raster, int width, int height) | ||
| private ReadOnlySpan<byte> PrepareByteArray(Image<Rgba32> bmp, int[] raster, int width, int height) | ||
| { | ||
| byte[] bits = new byte[GetStride(bmp) * height]; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Good that we have test!
Alternatively, I think we could get away by grabbing memory usage information from either
GCorSystem.Diagnostics, and validate if it doesn't reach higher than what rough goal we set. Because I think having a separate console app, very less chance others will know or will test against it.