|
| 1 | +using System.Buffers.Binary; |
| 2 | +using System.IO.Compression; |
| 3 | + |
| 4 | +namespace DevWinUI; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Provides methods to render a barcode as a PNG image. |
| 8 | +/// </summary> |
| 9 | +public static partial class BarcodePngRenderer |
| 10 | +{ |
| 11 | + private static readonly byte[] PngSignature = [137, 80, 78, 71, 13, 10, 26, 10]; |
| 12 | + private static readonly byte[] IhdrChunkType = [73, 72, 68, 82]; |
| 13 | + private static readonly byte[] IdatChunkType = [73, 68, 65, 84]; |
| 14 | + private static readonly byte[] IendChunkType = [73, 69, 78, 68]; |
| 15 | + private static readonly uint[] Crc32Table = InitializeCrc32Table(); |
| 16 | + |
| 17 | + /// <summary>Renders the barcode as PNG bytes with default options.</summary> |
| 18 | + public static byte[] ToPng(this Barcode barcode) |
| 19 | + { |
| 20 | + return ToPng(barcode, new BarcodePngOptions()); |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary>Renders the barcode as PNG bytes with the specified options.</summary> |
| 24 | + public static byte[] ToPng(this Barcode barcode, BarcodePngOptions options) |
| 25 | + { |
| 26 | + using var stream = new MemoryStream(); |
| 27 | + WriteToPng(barcode, stream, options); |
| 28 | + return stream.ToArray(); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary>Writes the barcode as PNG to the specified stream with default options.</summary> |
| 32 | + public static void WriteToPng(this Barcode barcode, Stream stream) |
| 33 | + { |
| 34 | + WriteToPng(barcode, stream, new BarcodePngOptions()); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary>Writes the barcode as PNG to the specified stream with the specified options.</summary> |
| 38 | + public static void WriteToPng(this Barcode barcode, Stream stream, BarcodePngOptions options) |
| 39 | + { |
| 40 | + ArgumentNullException.ThrowIfNull(barcode); |
| 41 | + ArgumentNullException.ThrowIfNull(stream); |
| 42 | + ArgumentNullException.ThrowIfNull(options); |
| 43 | + ArgumentOutOfRangeException.ThrowIfLessThan(options.ModuleWidth, 1); |
| 44 | + ArgumentOutOfRangeException.ThrowIfLessThan(options.ModuleHeight, 1); |
| 45 | + ArgumentOutOfRangeException.ThrowIfNegative(options.QuietZoneModules); |
| 46 | + |
| 47 | + var width = GetTotalDimensionWithQuietZone(barcode.Width, options.QuietZoneModules, options.ModuleWidth, nameof(options.ModuleWidth)); |
| 48 | + var height = GetTotalDimension(barcode.Height, options.ModuleHeight, nameof(options.ModuleHeight)); |
| 49 | + var imageData = CreateImageData(barcode, width, height, options); |
| 50 | + var compressedImageData = CompressImageData(imageData); |
| 51 | + |
| 52 | + WritePng(stream, width, height, compressedImageData); |
| 53 | + } |
| 54 | + |
| 55 | + private static int GetTotalDimensionWithQuietZone(int size, int quietZoneModules, int moduleSize, string parameterName) |
| 56 | + { |
| 57 | + var value = ((long)size + (2L * quietZoneModules)) * moduleSize; |
| 58 | + if (value > int.MaxValue) |
| 59 | + { |
| 60 | + throw new ArgumentOutOfRangeException(parameterName, "The output image dimensions are too large."); |
| 61 | + } |
| 62 | + |
| 63 | + return (int)value; |
| 64 | + } |
| 65 | + |
| 66 | + private static int GetTotalDimension(int size, int moduleSize, string parameterName) |
| 67 | + { |
| 68 | + var value = (long)size * moduleSize; |
| 69 | + if (value > int.MaxValue) |
| 70 | + { |
| 71 | + throw new ArgumentOutOfRangeException(parameterName, "The output image dimensions are too large."); |
| 72 | + } |
| 73 | + |
| 74 | + return (int)value; |
| 75 | + } |
| 76 | + |
| 77 | + private static byte[] CreateImageData(Barcode barcode, int width, int height, BarcodePngOptions options) |
| 78 | + { |
| 79 | + var stride = (width * 4) + 1; |
| 80 | + var dataLength = (long)stride * height; |
| 81 | + if (dataLength > int.MaxValue) |
| 82 | + { |
| 83 | + throw new ArgumentOutOfRangeException(nameof(options), "The output image is too large."); |
| 84 | + } |
| 85 | + |
| 86 | + var result = new byte[(int)dataLength]; |
| 87 | + for (var row = 0; row < height; row++) |
| 88 | + { |
| 89 | + var rowOffset = row * stride; |
| 90 | + var sourceRow = row / options.ModuleHeight; |
| 91 | + for (var col = 0; col < width; col++) |
| 92 | + { |
| 93 | + var sourceCol = (col / options.ModuleWidth) - options.QuietZoneModules; |
| 94 | + var isDark = sourceRow >= 0 |
| 95 | + && sourceRow < barcode.Height |
| 96 | + && sourceCol >= 0 |
| 97 | + && sourceCol < barcode.Width |
| 98 | + && barcode[sourceRow, sourceCol]; |
| 99 | + |
| 100 | + var color = isDark ? options.DarkColor : options.LightColor; |
| 101 | + var pixelOffset = rowOffset + 1 + (col * 4); |
| 102 | + result[pixelOffset] = color.Red; |
| 103 | + result[pixelOffset + 1] = color.Green; |
| 104 | + result[pixelOffset + 2] = color.Blue; |
| 105 | + result[pixelOffset + 3] = color.Alpha; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return result; |
| 110 | + } |
| 111 | + |
| 112 | + private static byte[] CompressImageData(ReadOnlySpan<byte> imageData) |
| 113 | + { |
| 114 | + using var output = new MemoryStream(); |
| 115 | + using (var compressionStream = new ZLibStream(output, CompressionLevel.SmallestSize, leaveOpen: true)) |
| 116 | + { |
| 117 | + compressionStream.Write(imageData); |
| 118 | + } |
| 119 | + |
| 120 | + return output.ToArray(); |
| 121 | + } |
| 122 | + |
| 123 | + private static void WritePng(Stream stream, int width, int height, ReadOnlySpan<byte> compressedImageData) |
| 124 | + { |
| 125 | + stream.Write(PngSignature); |
| 126 | + |
| 127 | + Span<byte> ihdrData = stackalloc byte[13]; |
| 128 | + BinaryPrimitives.WriteUInt32BigEndian(ihdrData, (uint)width); |
| 129 | + BinaryPrimitives.WriteUInt32BigEndian(ihdrData[4..], (uint)height); |
| 130 | + ihdrData[8] = 8; |
| 131 | + ihdrData[9] = 6; |
| 132 | + ihdrData[10] = 0; |
| 133 | + ihdrData[11] = 0; |
| 134 | + ihdrData[12] = 0; |
| 135 | + |
| 136 | + WriteChunk(stream, IhdrChunkType, ihdrData); |
| 137 | + WriteChunk(stream, IdatChunkType, compressedImageData); |
| 138 | + WriteChunk(stream, IendChunkType, ReadOnlySpan<byte>.Empty); |
| 139 | + } |
| 140 | + |
| 141 | + private static void WriteChunk(Stream stream, ReadOnlySpan<byte> chunkType, ReadOnlySpan<byte> data) |
| 142 | + { |
| 143 | + Span<byte> uintBuffer = stackalloc byte[4]; |
| 144 | + BinaryPrimitives.WriteUInt32BigEndian(uintBuffer, (uint)data.Length); |
| 145 | + stream.Write(uintBuffer); |
| 146 | + stream.Write(chunkType); |
| 147 | + stream.Write(data); |
| 148 | + |
| 149 | + var crc = ComputeCrc32(chunkType, data); |
| 150 | + BinaryPrimitives.WriteUInt32BigEndian(uintBuffer, crc); |
| 151 | + stream.Write(uintBuffer); |
| 152 | + } |
| 153 | + |
| 154 | + private static uint ComputeCrc32(ReadOnlySpan<byte> chunkType, ReadOnlySpan<byte> data) |
| 155 | + { |
| 156 | + var crc = uint.MaxValue; |
| 157 | + crc = UpdateCrc32(crc, chunkType); |
| 158 | + crc = UpdateCrc32(crc, data); |
| 159 | + |
| 160 | + return ~crc; |
| 161 | + } |
| 162 | + |
| 163 | + private static uint UpdateCrc32(uint crc, ReadOnlySpan<byte> data) |
| 164 | + { |
| 165 | + foreach (var value in data) |
| 166 | + { |
| 167 | + crc = Crc32Table[(int)((crc ^ value) & 0xFF)] ^ (crc >> 8); |
| 168 | + } |
| 169 | + |
| 170 | + return crc; |
| 171 | + } |
| 172 | + |
| 173 | + private static uint[] InitializeCrc32Table() |
| 174 | + { |
| 175 | + var table = new uint[256]; |
| 176 | + for (uint index = 0; index < table.Length; index++) |
| 177 | + { |
| 178 | + var crc = index; |
| 179 | + for (var bit = 0; bit < 8; bit++) |
| 180 | + { |
| 181 | + crc = (crc & 1) == 0 ? (crc >> 1) : (0xEDB88320u ^ (crc >> 1)); |
| 182 | + } |
| 183 | + |
| 184 | + table[index] = crc; |
| 185 | + } |
| 186 | + |
| 187 | + return table; |
| 188 | + } |
| 189 | +} |
0 commit comments