Skip to content

Commit

Permalink
faster
Browse files Browse the repository at this point in the history
  • Loading branch information
depler committed Jun 2, 2021
1 parent 65b6ff1 commit c0cdb11
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions wgconfig/QrCode/QrCodePng.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ public class QrCodePng
QrCode qrCode;
int scale;
int border;
int size;
int realSize;
int scaledSize;

public QrCodePng(QrCode qrCode, int scale, int border)
{
this.qrCode = qrCode;
this.scale = scale;
this.border = border;
this.size = (qrCode.Size + border * 2) * scale;
this.realSize = qrCode.Size + border * 2;
this.scaledSize = realSize * scale;
}

public byte[] GetBytes()
Expand All @@ -25,7 +27,7 @@ public byte[] GetBytes()
{
var data = Draw();

png.WriteHeader(size, size, 1, 0);
png.WriteHeader(scaledSize, scaledSize, 1, 0);
png.WriteData(data);
png.WriteEnd();

Expand All @@ -35,21 +37,32 @@ public byte[] GetBytes()

private byte[] Draw()
{
int bytesPerLine = (size + 7) / 8 + 1;
var data = new byte[bytesPerLine * size];
int bytesPerLine = (scaledSize + 7) / 8 + 1;
var data = new byte[bytesPerLine * scaledSize];

for (int y = 0; y < size; y++)
for (int x = 0; x < size; x++)
for (int y = 0; y < realSize; y++)
{
int offset = y * bytesPerLine * scale;

for (int x = 0; x < realSize; x++)
{
if (qrCode.GetModule(x / scale - border, y /scale - border))
if (qrCode.GetModule(x - border, y - border))
continue;

int offset = y * bytesPerLine;
int index = offset + x / 8 + 1;
int pos = x * scale;
int end = pos + scale;

data[index] |= (byte)(0x80 >> x % 8);
for (; pos < end; pos++)
{
int index = offset + pos / 8 + 1;
data[index] |= (byte)(0x80 >> pos % 8);
}
}

for (var i = 1; i < scale; i++)
Array.Copy(data, offset, data, offset + i * bytesPerLine, bytesPerLine);
}

return data;
}

Expand Down

0 comments on commit c0cdb11

Please sign in to comment.