Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sungaila committed Jul 24, 2024
1 parent 9410571 commit db5055d
Show file tree
Hide file tree
Showing 25 changed files with 597 additions and 596 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/githubpages_staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ jobs:
name: Deploy
needs: publish
runs-on: ubuntu-latest

concurrency:
group: "pages-staging"
cancel-in-progress: true

true
# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
permissions:
pages: write # to deploy to Pages
Expand Down
1 change: 1 addition & 0 deletions Console/Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Nullable>enable</Nullable>
<Features>strict</Features>
<WarningsAsErrors>nullable</WarningsAsErrors>
<NoWarn>CS0618</NoWarn>
</PropertyGroup>

<!-- References -->
Expand Down
3 changes: 1 addition & 2 deletions Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Reflection;
using static PDFtoZPL.Conversion;

namespace PDFtoZPL.Console
{
Expand Down Expand Up @@ -36,7 +35,7 @@ public static int Main(string[] args)
{
".pdf" =>
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()
OperatingSystem.IsWindows() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()
#else
true
#endif
Expand Down
8 changes: 4 additions & 4 deletions PDFtoZPL/ConversionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ namespace PDFtoZPL
{
internal static class ConversionUtils
{
private static readonly IReadOnlyDictionary<string, byte> _hexLookupTable;
private static readonly Dictionary<string, byte> _hexLookupTable;

static ConversionUtils()

Check warning on line 14 in PDFtoZPL/ConversionUtils.cs

View workflow job for this annotation

GitHub Actions / SonarCloud

Initialize all 'static fields' inline and remove the 'static constructor'. (https://rules.sonarsource.com/csharp/RSPEC-3963)
{
_hexLookupTable = new Dictionary<string, byte>();
_hexLookupTable = [];

for (int i = 0; i <= 255; i++)
{
((Dictionary<string, byte>)_hexLookupTable).Add(i.ToString("X2"), (byte)i);
_hexLookupTable.Add(i.ToString("X2"), (byte)i);
}
}

Expand Down Expand Up @@ -193,7 +193,7 @@ public static string CompressHex(string code, int widthBytes)
/// The mapping table used for compression.
/// Each character count (the key) is represented by a certain char (the value).
/// </summary>
private static readonly IReadOnlyDictionary<int, string> CompressionCountMapping = new Dictionary<int, string>()
private static readonly Dictionary<int, string> CompressionCountMapping = new()
{
{ 1, "G" },
{ 2, "H" },
Expand Down
311 changes: 155 additions & 156 deletions PDFtoZPL/DitheringUtils.cs
Original file line number Diff line number Diff line change
@@ -1,165 +1,164 @@
using SkiaSharp;
using System;
using System.Runtime.CompilerServices;
using static PDFtoZPL.Conversion;

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("PDFtoZPL.WebConverter, PublicKey=0024000004800000940000000602000000240000525341310004000001000100fd0018feed1ba4fac91744d868cb1bb8a4b55a97eec0e02e90a8d57db56bff5a32f03813b1a6c8a7ccd50eed8880be4e91ad281c9ee81fd4182de0905c0590019e9bf87a8f83ce93c09da9c4eaae5bd8aa63816c4a3bef8bd16bb358d3aed64cc2ec3ae17698c336b63df4fbd719dc13bd9a88d9fcfa87a2426d34db78d05fb7")]

namespace PDFtoZPL
{
internal static class DitheringUtils
{
private static readonly uint _black = (uint)new SKColor(0, 0, 0, 255);
private static readonly uint _white = (uint)new SKColor(255, 255, 255, 255);

public static SKBitmap ToMonochrome(this SKBitmap input, byte threshold = 128, DitheringKind dithering = DitheringKind.None)
{
var width = input.Width;
var height = input.Height;

var output = input.Copy(SKColorType.Rgba8888);

IntPtr pixelsAddr = output.GetPixels();

unsafe
{
unchecked
{
uint* ptr = (uint*)pixelsAddr.ToPointer();

for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
uint oldPixel = *ptr;

uint sum = oldPixel % 256;
sum += (oldPixel >> 8) % 256;
sum += (oldPixel >> 16) % 256;

uint newPixel = (sum / 3) < threshold
? _black
: _white;

*ptr = newPixel;

if (dithering == DitheringKind.FloydSteinberg)
{
DitherFloydSteinberg(oldPixel, newPixel, row, col, width, height, ptr);
}
else if (dithering == DitheringKind.Atkinson)
{
DitherAtkinson(oldPixel, newPixel, row, col, width, height, ptr);
}
else if (dithering != DitheringKind.None)
{
throw new ArgumentOutOfRangeException(nameof(dithering));
}

ptr++;
}
}
}
}

return output;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte HandleByteError(byte input, int error)
{
if ((input & 0xff) + error < 0)
return 0;

else if ((input & 0xff) + error > 255)
return 255;

return (byte)(input + error);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint HandlePixel(uint input, int red, int green, int blue)
{
uint result = HandleByteError((byte)(input % 256), red);
result += (uint)HandleByteError((byte)((input >> 8) % 256), green) << 8;
result += (uint)HandleByteError((byte)((input >> 16) % 256), blue) << 16;
result += 0xFF000000;

return result;
}

private static unsafe void DitherFloydSteinberg(uint oldPixel, uint newPixel, int row, int col, int width, int height, uint* ptr)
{
int errorR = ((byte)(oldPixel % 256)) - ((byte)(newPixel % 256));
int errorG = ((byte)((oldPixel >> 8) % 256)) - ((byte)((newPixel >> 8) % 256));
int errorB = ((byte)((oldPixel >> 16) % 256)) - ((byte)((newPixel >> 16) % 256));

bool left = col != 0;
bool right = col != (width - 1);
bool below = row != (height - 1);

if (right)
{
*(ptr+1) = HandlePixel(*(ptr+1), (errorR * 7) >> 4, (errorG * 7) >> 4, (errorB * 7) >> 4);

if (below)
{
*(ptr+1+width) = HandlePixel(*(ptr+1+width), (errorR * 1) >> 4, (errorG * 1) >> 4, (errorB * 1) >> 4);
}
}

if (below)
{
*(ptr+width) = HandlePixel(*(ptr+width), (errorR * 5) >> 4, (errorG * 5) >> 4, (errorB * 5) >> 4);

if (left)
{
*(ptr-1+width) = HandlePixel(*(ptr-1+width), (errorR * 3) >> 4, (errorG * 3) >> 4, (errorB * 3) >> 4);
}
}
}

private static unsafe void DitherAtkinson(uint oldPixel, uint newPixel, int row, int col, int width, int height, uint* ptr)
{
int errorR = ((byte)(oldPixel % 256)) - ((byte)(newPixel % 256));
int errorG = ((byte)((oldPixel >> 8) % 256)) - ((byte)((newPixel >> 8) % 256));
int errorB = ((byte)((oldPixel >> 16) % 256)) - ((byte)((newPixel >> 16) % 256));

bool left = col != 0;
bool right = col != (width - 1);
bool right2 = right && col != (width - 2);
bool below = row != (height - 1);
bool below2 = below && row != (height - 2);

if (right)
{
*(ptr+1) = HandlePixel(*(ptr+1), errorR >> 3, errorG >> 3, errorB >> 3);

if (below)
{
*(ptr+1+width) = HandlePixel(*(ptr+1+width), errorR >> 3, errorG >> 3, errorB >> 3);
}
}

if (below)
{
*(ptr+width) = HandlePixel(*(ptr+width), errorR >> 3, errorG >> 3, errorB >> 3);

if (left)
{
*(ptr-1+width) = HandlePixel(*(ptr-1+width), errorR >> 3, errorG >> 3, errorB >> 3);
}
}

if (right2)
{
*(ptr+2) = HandlePixel(*(ptr+2), errorR >> 3, errorG >> 3, errorB >> 3);
}

if (below2)
{
*(ptr+width*2) = HandlePixel(*(ptr+width*2), errorR >> 3, errorG >> 3, errorB >> 3);
}
}
}
internal static class DitheringUtils
{
private static readonly uint _black = (uint)new SKColor(0, 0, 0, 255);
private static readonly uint _white = (uint)new SKColor(255, 255, 255, 255);

public static SKBitmap ToMonochrome(this SKBitmap input, byte threshold = 128, DitheringKind dithering = DitheringKind.None)
{
var width = input.Width;
var height = input.Height;

var output = input.Copy(SKColorType.Rgba8888);

IntPtr pixelsAddr = output.GetPixels();

unsafe

Check warning on line 23 in PDFtoZPL/DitheringUtils.cs

View workflow job for this annotation

GitHub Actions / SonarCloud

Make sure that using "unsafe" is safe here. (https://rules.sonarsource.com/csharp/RSPEC-6640)
{
unchecked
{
uint* ptr = (uint*)pixelsAddr.ToPointer();

for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
uint oldPixel = *ptr;

uint sum = oldPixel % 256;
sum += (oldPixel >> 8) % 256;
sum += (oldPixel >> 16) % 256;

uint newPixel = (sum / 3) < threshold
? _black
: _white;

*ptr = newPixel;

if (dithering == DitheringKind.FloydSteinberg)
{
DitherFloydSteinberg(oldPixel, newPixel, row, col, width, height, ptr);
}
else if (dithering == DitheringKind.Atkinson)
{
DitherAtkinson(oldPixel, newPixel, row, col, width, height, ptr);
}
else if (dithering != DitheringKind.None)
{
throw new ArgumentOutOfRangeException(nameof(dithering));
}

ptr++;
}
}
}
}

return output;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte HandleByteError(byte input, int error)
{
if ((input & 0xff) + error < 0)
return 0;

else if ((input & 0xff) + error > 255)
return 255;

return (byte)(input + error);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint HandlePixel(uint input, int red, int green, int blue)
{
uint result = HandleByteError((byte)(input % 256), red);
result += (uint)HandleByteError((byte)((input >> 8) % 256), green) << 8;
result += (uint)HandleByteError((byte)((input >> 16) % 256), blue) << 16;
result += 0xFF000000;

return result;
}

private static unsafe void DitherFloydSteinberg(uint oldPixel, uint newPixel, int row, int col, int width, int height, uint* ptr)

Check warning on line 90 in PDFtoZPL/DitheringUtils.cs

View workflow job for this annotation

GitHub Actions / SonarCloud

Make sure that using "unsafe" is safe here. (https://rules.sonarsource.com/csharp/RSPEC-6640)
{
int errorR = ((byte)(oldPixel % 256)) - ((byte)(newPixel % 256));
int errorG = ((byte)((oldPixel >> 8) % 256)) - ((byte)((newPixel >> 8) % 256));
int errorB = ((byte)((oldPixel >> 16) % 256)) - ((byte)((newPixel >> 16) % 256));

bool left = col != 0;
bool right = col != (width - 1);
bool below = row != (height - 1);

if (right)
{
*(ptr+1) = HandlePixel(*(ptr+1), (errorR * 7) >> 4, (errorG * 7) >> 4, (errorB * 7) >> 4);

if (below)
{
*(ptr+1+width) = HandlePixel(*(ptr+1+width), (errorR * 1) >> 4, (errorG * 1) >> 4, (errorB * 1) >> 4);
}
}

if (below)
{
*(ptr+width) = HandlePixel(*(ptr+width), (errorR * 5) >> 4, (errorG * 5) >> 4, (errorB * 5) >> 4);

if (left)
{
*(ptr-1+width) = HandlePixel(*(ptr-1+width), (errorR * 3) >> 4, (errorG * 3) >> 4, (errorB * 3) >> 4);
}
}
}

private static unsafe void DitherAtkinson(uint oldPixel, uint newPixel, int row, int col, int width, int height, uint* ptr)

Check warning on line 121 in PDFtoZPL/DitheringUtils.cs

View workflow job for this annotation

GitHub Actions / SonarCloud

Make sure that using "unsafe" is safe here. (https://rules.sonarsource.com/csharp/RSPEC-6640)
{
int errorR = ((byte)(oldPixel % 256)) - ((byte)(newPixel % 256));
int errorG = ((byte)((oldPixel >> 8) % 256)) - ((byte)((newPixel >> 8) % 256));
int errorB = ((byte)((oldPixel >> 16) % 256)) - ((byte)((newPixel >> 16) % 256));

bool left = col != 0;
bool right = col != (width - 1);
bool right2 = right && col != (width - 2);
bool below = row != (height - 1);
bool below2 = below && row != (height - 2);

if (right)
{
*(ptr+1) = HandlePixel(*(ptr+1), errorR >> 3, errorG >> 3, errorB >> 3);

if (below)
{
*(ptr+1+width) = HandlePixel(*(ptr+1+width), errorR >> 3, errorG >> 3, errorB >> 3);
}
}

if (below)
{
*(ptr+width) = HandlePixel(*(ptr+width), errorR >> 3, errorG >> 3, errorB >> 3);

if (left)
{
*(ptr-1+width) = HandlePixel(*(ptr-1+width), errorR >> 3, errorG >> 3, errorB >> 3);
}
}

if (right2)
{
*(ptr+2) = HandlePixel(*(ptr+2), errorR >> 3, errorG >> 3, errorB >> 3);
}

if (below2)
{
*(ptr+width*2) = HandlePixel(*(ptr+width*2), errorR >> 3, errorG >> 3, errorB >> 3);
}
}
}
}
2 changes: 1 addition & 1 deletion Tests/AntiAliasingTests.ExpectedResults.cs

Large diffs are not rendered by default.

Loading

0 comments on commit db5055d

Please sign in to comment.