Skip to content
Open
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
16 changes: 14 additions & 2 deletions QRCoder/BitmapByteQRCode.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#if NETCOREAPP2_1_OR_GREATER
using System.Buffers.Binary;
#endif
using System.Diagnostics;

using static QRCoder.QRCodeGenerator;

namespace QRCoder;
Expand Down Expand Up @@ -67,11 +72,14 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightC
moduleLight[i + 2] = lightColorRgb[0];
}

// The size of each row must be rounded up to a multiple of 4 bytes by padding.
// Pre-calculate padding bytes
var paddingLen = sideLength % 4;
var rowByteLength = sideLength * 3;
var paddingLen = -rowByteLength & 3;
Debug.Assert(paddingLen >= 0 && paddingLen < 4 && (rowByteLength + paddingLen) % 4 == 0);

// Calculate filesize (header + pixel data + padding)
var fileSize = 54 + (3 * (sideLength * sideLength)) + (sideLength * paddingLen);
var fileSize = 54 + (rowByteLength + paddingLen) * sideLength;

// Bitmap container
byte[] bmp = new byte[fileSize];
Expand Down Expand Up @@ -142,13 +150,17 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightC
/// <param name="destinationArray">Destination byte array that receives the bytes</param>
private static void CopyIntAs4ByteToArray(int inp, int destinationIndex, byte[] destinationArray)
{
#if NETCOREAPP2_1_OR_GREATER
BinaryPrimitives.WriteInt32LittleEndian(destinationArray.AsSpan(destinationIndex), inp);
#else
unchecked
{
destinationArray[destinationIndex + 3] = (byte)(inp >> 24);
destinationArray[destinationIndex + 2] = (byte)(inp >> 16);
destinationArray[destinationIndex + 1] = (byte)(inp >> 8);
destinationArray[destinationIndex + 0] = (byte)(inp);
}
#endif
}
}

Expand Down