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

Upgraded ImageSharp #40

Closed
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/Barcoder.Renderer.Image/Barcoder.Renderer.Image.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta14" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15" />
</ItemGroup>

<ItemGroup>
Expand Down
97 changes: 57 additions & 40 deletions src/Barcoder.Renderer.Image/ImageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,72 +50,89 @@ public void Render(IBarcode barcode, Stream outputStream)
barcode = barcode ?? throw new ArgumentNullException(nameof(barcode));
outputStream = outputStream ?? throw new ArgumentNullException(nameof(outputStream));
if (barcode.Bounds.Y == 1)
Render1D(barcode, outputStream);
{
using (var image = Render1D(barcode))
{
image.Save(outputStream, _imageEncoder.Value);
}
}
else if (barcode.Bounds.Y > 1)
{
using (var image = Render2D(barcode))
{
image.Save(outputStream, _imageEncoder.Value);
}
}
else
throw new NotSupportedException($"Y value of {barcode.Bounds.Y} is invalid");
}

public Image<L8> Render(IBarcode barcode)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Types from ImageSharp should be isolated. Why was this public method added? And the code refactored? Doesn't seem required for the beta14 to beta15 upgrade.

{
barcode = barcode ?? throw new ArgumentNullException(nameof(barcode));
if (barcode.Bounds.Y == 1)
return Render1D(barcode);
else if (barcode.Bounds.Y > 1)
Render2D(barcode, outputStream);
return Render2D(barcode);
else
throw new NotSupportedException($"Y value of {barcode.Bounds.Y} is invalid");
}

private void Render1D(IBarcode barcode, Stream outputStream)
private Image<L8> Render1D(IBarcode barcode)
{
int margin = _options.CustomMargin ?? barcode.Margin;
int width = (barcode.Bounds.X + 2 * margin) * _options.PixelSize;
int height = (_options.BarHeightFor1DBarcode + 2 * margin) * _options.PixelSize;

using (var image = new Image<L8>(width, height))
var image = new Image<L8>(width, height);
image.Mutate(ctx =>
{
image.Mutate(ctx =>
ctx.Fill(Color.White);
for (var x = 0; x < barcode.Bounds.X; x++)
{
ctx.Fill(Color.White);
for (var x = 0; x < barcode.Bounds.X; x++)
{
if (!barcode.At(x, 0))
continue;
ctx.FillPolygon(
Color.Black,
new Vector2((margin + x) * _options.PixelSize, margin * _options.PixelSize),
new Vector2((margin + x + 1) * _options.PixelSize, margin * _options.PixelSize),
new Vector2((margin + x + 1) * _options.PixelSize, (_options.BarHeightFor1DBarcode + margin) * _options.PixelSize),
new Vector2((margin + x) * _options.PixelSize, (_options.BarHeightFor1DBarcode + margin) * _options.PixelSize));
}
});
if (!barcode.At(x, 0))
continue;
ctx.FillPolygon(
Color.Black,
new Vector2((margin + x) * _options.PixelSize, margin * _options.PixelSize),
new Vector2((margin + x + 1) * _options.PixelSize, margin * _options.PixelSize),
new Vector2((margin + x + 1) * _options.PixelSize, (_options.BarHeightFor1DBarcode + margin) * _options.PixelSize),
new Vector2((margin + x) * _options.PixelSize, (_options.BarHeightFor1DBarcode + margin) * _options.PixelSize));
}
});

if (_options.IncludeEanContentAsText && barcode.IsEanBarcode())
EanContentRenderer.Render(image, barcode, fontFamily: _options.EanFontFamily, scale: _options.PixelSize);
if (_options.IncludeEanContentAsText && barcode.IsEanBarcode())
EanContentRenderer.Render(image, barcode, fontFamily: _options.EanFontFamily, scale: _options.PixelSize);

image.Save(outputStream, _imageEncoder.Value);
}
return image;
}

private void Render2D(IBarcode barcode, Stream outputStream)
private Image<L8> Render2D(IBarcode barcode)
{
int margin = _options.CustomMargin ?? barcode.Margin;
int width = (barcode.Bounds.X + 2 * margin) * _options.PixelSize;
int height = (barcode.Bounds.Y + 2 * margin) * _options.PixelSize;

using (var image = new Image<L8>(width, height))
var image = new Image<L8>(width, height);
image.Mutate(ctx =>
{
image.Mutate(ctx =>
ctx.Fill(Color.White);
for (var y = 0; y < barcode.Bounds.Y; y++)
{
ctx.Fill(Color.White);
for (var y = 0; y < barcode.Bounds.Y; y++)
for (var x = 0; x < barcode.Bounds.X; x++)
{
for (var x = 0; x < barcode.Bounds.X; x++)
{
if (!barcode.At(x, y)) continue;
ctx.FillPolygon(
Color.Black,
new Vector2((margin + x) * _options.PixelSize, (margin + y) * _options.PixelSize),
new Vector2((margin + x + 1) * _options.PixelSize, (margin + y) * _options.PixelSize),
new Vector2((margin + x + 1) * _options.PixelSize, (margin + y + 1) * _options.PixelSize),
new Vector2((margin + x) * _options.PixelSize, (margin + y + 1) * _options.PixelSize));
}
if (!barcode.At(x, y)) continue;
ctx.FillPolygon(
Color.Black,
new Vector2((margin + x) * _options.PixelSize, (margin + y) * _options.PixelSize),
new Vector2((margin + x + 1) * _options.PixelSize, (margin + y) * _options.PixelSize),
new Vector2((margin + x + 1) * _options.PixelSize, (margin + y + 1) * _options.PixelSize),
new Vector2((margin + x) * _options.PixelSize, (margin + y + 1) * _options.PixelSize));
}
});
}
});

image.Save(outputStream, _imageEncoder.Value);
}
return image;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageReference Include="FluentAssertions" Version="6.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta14" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
17 changes: 17 additions & 0 deletions tests/Barcoder.Renderer.Image.Tests/ImageRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ public void Render_ImageFormatPng_ShouldRenderPng()
imageFormat.Name.Should().Be("PNG");
}

[Fact]
public void Render_Image_ShouldRender()
{
// Arrange
var renderer = new ImageRenderer(new ImageRendererOptions { ImageFormat = ImageFormat.Png });
IBarcode barcode = QrEncoder.Encode("Hello", ErrorCorrectionLevel.L, Encoding.Unicode);

// Act
var image = renderer.Render(barcode);

// Assert
image.Frames.Count.Should().Be(1);
image.Width.Should().Be(310);
image.Height.Should().Be(310);
image.PixelType.BitsPerPixel.Should().Be(8);
}

private static byte[] RenderBarcodeToByteArray(IRenderer renderer, IBarcode barcode)
{
using var stream = new MemoryStream();
Expand Down