Optimal segments

pull/45/head
manuelbl 7 years ago
parent c66db6a105
commit a0e70ee56f

@ -966,7 +966,7 @@ namespace Io.Nayuki.QrCodeGen
// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
// QR Code of the given version number and error correction level, with remainder bits discarded. // QR Code of the given version number and error correction level, with remainder bits discarded.
// This stateless pure function could be implemented as a (40*4)-cell lookup table. // This stateless pure function could be implemented as a (40*4)-cell lookup table.
private static int GetNumDataCodewords(int ver, Ecc ecl) internal static int GetNumDataCodewords(int ver, Ecc ecl)
{ {
return GetNumRawDataModules(ver) / 8 return GetNumRawDataModules(ver) / 8
- EccCodewordsPerBlock[ecl.Ordinal, ver] - EccCodewordsPerBlock[ecl.Ordinal, ver]

@ -329,7 +329,7 @@ namespace Io.Nayuki.QrCodeGen
// The set of all legal characters in alphanumeric mode, where // The set of all legal characters in alphanumeric mode, where
// each character value maps to the index in the string. // each character value maps to the index in the string.
static readonly string AlphanumericCharset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; internal static readonly string AlphanumericCharset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
#endregion #endregion

@ -24,7 +24,9 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Text;
using static Io.Nayuki.QrCodeGen.QrSegment; using static Io.Nayuki.QrCodeGen.QrSegment;
namespace Io.Nayuki.QrCodeGen namespace Io.Nayuki.QrCodeGen
@ -39,6 +41,282 @@ namespace Io.Nayuki.QrCodeGen
/// <seealso cref="QrCode"/> /// <seealso cref="QrCode"/>
public static class QrSegmentAdvanced public static class QrSegmentAdvanced
{ {
#region Optimal list of segments encoder
/// <summary>
/// Returns a list of zero or more segments to represent the specified Unicode text string.
/// The resulting list optimally minimizes the total encoded bit length, subjected to the constraints
/// in the specified {error correction level, minimum version number, maximum version number}.
/// </summary>
/// <remarks>
/// This function can utilize all four text encoding modes: numeric, alphanumeric, byte (UTF-8),
/// and kanji. This can be considered as a sophisticated but slower replacement for
/// <see cref="MakeSegments"/>. This requires more input parameters because it searches a
/// range of versions, like <see cref="QrCode.EncodeSegments(List{QrSegment},QrCode.Ecc)"/>.
/// </remarks>
/// <param name="text">the text to be encoded (not <c>null</c>), which can be any Unicode string</param>
/// <param name="ecl">the error correction level to use (not <c>null</c>)</param>
/// <param name="minVersion">the minimum allowed version of the QR Code (at least 1)</param>
/// <param name="maxVersion">the maximum allowed version of the QR Code (at most 40)</param>
/// <returns>a new mutable list (not <c>null</c>) of segments (not <c>null</c>)
/// containing the text, minimizing the bit length with respect to the constraints</returns>
/// <exception cref="ArgumentNullException">Thrown if the text or error correction level is <c>null</c></exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown if 1 &#x2264; minVersion &#x2264; maxVersion &#x2264; 40 is violated</exception>
/// <exception cref="DataTooLongException">Thrown if the text fails to fit in the maxVersion QR Code at the ECL</exception>
public static List<QrSegment> MakeSegmentsOptimally(string text, QrCode.Ecc ecl, int minVersion, int maxVersion)
{
// Check arguments
Objects.RequireNonNull(text);
Objects.RequireNonNull(ecl);
if (minVersion < QrCode.MinVersion || minVersion > maxVersion)
{
throw new ArgumentOutOfRangeException(nameof(minVersion), "Invalid value");
}
if (maxVersion > QrCode.MaxVersion)
{
throw new ArgumentOutOfRangeException(nameof(maxVersion), "Invalid value");
}
// Iterate through version numbers, and make tentative segments
List<QrSegment> segs = null;
var codePoints = ToCodePoints(text);
for (int version = minVersion;; version++)
{
if (version == minVersion || version == 10 || version == 27)
segs = MakeSegmentsOptimally(codePoints, version);
Debug.Assert(segs != null);
// Check if the segments fit
int dataCapacityBits = QrCode.GetNumDataCodewords(version, ecl) * 8;
int dataUsedBits = GetTotalBits(segs, version);
if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)
return segs; // This version number is found to be suitable
if (version < maxVersion) continue;
// All versions in the range could not fit the given text
var msg = "Segment too long";
if (dataUsedBits != -1)
msg = $"Data length = {dataUsedBits} bits, Max capacity = {dataCapacityBits} bits";
throw new DataTooLongException(msg);
}
}
// Returns a new list of segments that is optimal for the given text at the given version number.
private static List<QrSegment> MakeSegmentsOptimally(int[] codePoints, int version)
{
if (codePoints.Length == 0)
return new List<QrSegment>();
var charModes = ComputeCharacterModes(codePoints, version);
return SplitIntoSegments(codePoints, charModes);
}
// Returns a new array representing the optimal mode per code point based on the given text and version.
private static Mode[] ComputeCharacterModes(int[] codePoints, int version)
{
if (codePoints.Length == 0)
{
throw new ArgumentOutOfRangeException(nameof(codePoints));
}
Mode[] modeTypes = {Mode.Byte, Mode.Alphanumeric, Mode.Numeric, Mode.Kanji}; // Do not modify
int numModes = modeTypes.Length;
// Segment header sizes, measured in 1/6 bits
var headCosts = new int[numModes];
for (var i = 0; i < numModes; i++)
{
headCosts[i] = (4 + modeTypes[i].NumCharCountBits(version)) * 6;
}
// charModes[i][j] represents the mode to encode the code point at
// index i such that the final segment ends in modeTypes[j] and the
// total number of bits is minimized over all possible choices
var charModes = new Mode[codePoints.Length, numModes];
// At the beginning of each iteration of the loop below,
// prevCosts[j] is the exact minimum number of 1/6 bits needed to
// encode the entire string prefix of length i, and end in modeTypes[j]
var prevCosts = (int[]) headCosts.Clone();
// Calculate costs using dynamic programming
for (var i = 0; i < codePoints.Length; i++)
{
int c = codePoints[i];
var curCosts = new int[numModes];
{
// Always extend a byte mode segment
curCosts[0] = prevCosts[0] + CountUtf8Bytes(c) * 8 * 6;
charModes[i, 0] = modeTypes[0];
}
// Extend a segment if possible
if (AlphanumericCharset.IndexOf((char) c) != -1)
{
// Is alphanumeric
curCosts[1] = prevCosts[1] + 33; // 5.5 bits per alphanumeric char
charModes[i, 1] = modeTypes[1];
}
if ('0' <= c && c <= '9')
{
// Is numeric
curCosts[2] = prevCosts[2] + 20; // 3.33 bits per digit
charModes[i, 2] = modeTypes[2];
}
if (IsKanji(c))
{
curCosts[3] = prevCosts[3] + 78; // 13 bits per Shift JIS char
charModes[i, 3] = modeTypes[3];
}
// Start new segment at the end to switch modes
for (var j = 0; j < numModes; j++)
{
// To mode
for (var k = 0; k < numModes; k++)
{
// From mode
int newCost = (curCosts[k] + 5) / 6 * 6 + headCosts[j];
if (charModes[i, k] == null || (charModes[i, j] != null && newCost >= curCosts[j]))
continue;
curCosts[j] = newCost;
charModes[i, j] = modeTypes[k];
}
}
prevCosts = curCosts;
}
// Find optimal ending mode
Mode curMode = null;
for (int i = 0, minCost = 0; i < numModes; i++)
{
if (curMode != null && prevCosts[i] >= minCost) continue;
minCost = prevCosts[i];
curMode = modeTypes[i];
}
// Get optimal mode for each code point by tracing backwards
var result = new Mode[codePoints.Length];
for (int i = result.Length - 1; i >= 0; i--)
{
for (var j = 0; j < numModes; j++)
{
if (modeTypes[j] != curMode) continue;
curMode = charModes[i, j];
result[i] = curMode;
break;
}
}
return result;
}
// Returns a new list of segments based on the given text and modes, such that
// consecutive code points in the same mode are put into the same segment.
private static List<QrSegment> SplitIntoSegments(int[] codePoints, Mode[] charModes)
{
if (codePoints.Length == 0)
throw new ArgumentOutOfRangeException(nameof(codePoints));
var result = new List<QrSegment>();
// Accumulate run of modes
var curMode = charModes[0];
var start = 0;
for (var i = 1;; i++)
{
if (i < codePoints.Length && charModes[i] == curMode)
continue;
string s = FromCodePoints(codePoints, start, i - start);
if (curMode == Mode.Byte)
{
result.Add(MakeBytes(Encoding.UTF8.GetBytes(s)));
}
else if (curMode == Mode.Numeric)
{
result.Add(MakeNumeric(s));
}
else if (curMode == Mode.Alphanumeric)
{
result.Add(MakeAlphanumeric(s));
}
else if (curMode == Mode.Kanji)
{
result.Add(MakeKanji(s));
}
else
{
Debug.Assert(false);
}
if (i >= codePoints.Length)
{
return result;
}
curMode = charModes[i];
start = i;
}
}
public static string FromCodePoints(int[] codepoints, int startIndex, int count)
{
bool useBigEndian = !BitConverter.IsLittleEndian;
Encoding utf32 = new UTF32Encoding(useBigEndian, false , true);
var octets = new byte[count * 4];
for (int i = startIndex, j = 0; i < startIndex + count; i++, j += 4)
{
var bytes = BitConverter.GetBytes(codepoints[i]);
octets[j] = bytes[0];
octets[j + 1] = bytes[1];
octets[j + 2] = bytes[2];
octets[j + 3] = bytes[3];
}
return utf32.GetString(octets);
}
// Returns a new array of Unicode code points (effectively
// UTF-32 / UCS-4) representing the given UTF-16 string.
private static int[] ToCodePoints(string s)
{
bool useBigEndian = !BitConverter.IsLittleEndian;
Encoding utf32 = new UTF32Encoding(useBigEndian, false , true);
var octets = utf32.GetBytes(s) ;
var result = new int[octets.Length / 4];
for (int i = 0, j = 0; i < octets.Length; i += 4, j++)
{
result[j] = BitConverter.ToInt32(octets, i);
}
return result;
}
// Returns the number of UTF-8 bytes needed to encode the given Unicode code point.
private static int CountUtf8Bytes(int cp)
{
if (cp < 0) throw new ArgumentOutOfRangeException(nameof(cp), "Invalid code point");
if (cp < 0x80) return 1;
if (cp < 0x800) return 2;
if (cp < 0x10000) return 3;
if (cp < 0x110000) return 4;
throw new ArgumentOutOfRangeException(nameof(cp), "Invalid code point");
}
#endregion
#region Kanji mode segment encoder #region Kanji mode segment encoder
@ -85,7 +363,7 @@ namespace Io.Nayuki.QrCodeGen
/// Examples of non-encodable characters include {ordinary ASCII, half-width katakana, /// Examples of non-encodable characters include {ordinary ASCII, half-width katakana,
/// more extensive Chinese hanzi}. /// more extensive Chinese hanzi}.
/// </remarks> /// </remarks>
/// <param name="text">the string to test for encodability (not {@code null})</param> /// <param name="text">the string to test for encodability (not <c>null</c>)</param>
/// <returns><c>true</c> iff each character is in the kanji mode character set</returns> /// <returns><c>true</c> iff each character is in the kanji mode character set</returns>
/// <exception cref="ArgumentNullException">Thrown if the string is <c>null</c></exception> /// <exception cref="ArgumentNullException">Thrown if the string is <c>null</c></exception>
public static bool IsEncodableAsKanji(string text) { public static bool IsEncodableAsKanji(string text) {
@ -237,6 +515,5 @@ namespace Io.Nayuki.QrCodeGen
#endregion #endregion
} }
} }

@ -40,13 +40,13 @@ namespace Io.Nayuki.QrCodeGen.Demo
} }
/*---- Demo suite ----*/ #region Demo suite
// Creates a single QR Code, then writes it to a PNG file and an SVG file. // Creates a single QR Code, then writes it to a PNG file and an SVG file.
private static void DoBasicDemo() private static void DoBasicDemo()
{ {
const string text = "Hello, world!"; // User-supplied Unicode text const string text = "Hello, world!"; // User-supplied Unicode text
QrCode.Ecc errCorLvl = QrCode.Ecc.Low; // Error correction level var errCorLvl = QrCode.Ecc.Low; // Error correction level
var qr = QrCode.EncodeText(text, errCorLvl); // Make the QR Code symbol var qr = QrCode.EncodeText(text, errCorLvl); // Make the QR Code symbol
@ -62,10 +62,8 @@ namespace Io.Nayuki.QrCodeGen.Demo
// Creates a variety of QR Codes that exercise different features of the library, and writes each one to file. // Creates a variety of QR Codes that exercise different features of the library, and writes each one to file.
private static void DoVarietyDemo() { private static void DoVarietyDemo() {
QrCode qr;
// Numeric mode encoding (3.33 bits per digit) // Numeric mode encoding (3.33 bits per digit)
qr = QrCode.EncodeText("314159265358979323846264338327950288419716939937510", QrCode.Ecc.Medium); var qr = QrCode.EncodeText("314159265358979323846264338327950288419716939937510", QrCode.Ecc.Medium);
SaveAsPng(qr, "pi-digits-QR.png", 13, 1); SaveAsPng(qr, "pi-digits-QR.png", 13, 1);
// Alphanumeric mode encoding (5.5 bits per character) // Alphanumeric mode encoding (5.5 bits per character)
@ -92,16 +90,13 @@ namespace Io.Nayuki.QrCodeGen.Demo
// Creates QR Codes with manually specified segments for better compactness. // Creates QR Codes with manually specified segments for better compactness.
private static void DoSegmentDemo() private static void DoSegmentDemo()
{ {
QrCode qr;
List<QrSegment> segs;
// Illustration "silver" // Illustration "silver"
var silver0 = "THE SQUARE ROOT OF 2 IS 1."; const string silver0 = "THE SQUARE ROOT OF 2 IS 1.";
var silver1 = "41421356237309504880168872420969807856967187537694807317667973799"; const string silver1 = "41421356237309504880168872420969807856967187537694807317667973799";
qr = QrCode.EncodeText(silver0 + silver1, QrCode.Ecc.Low); var qr = QrCode.EncodeText(silver0 + silver1, QrCode.Ecc.Low);
SaveAsPng(qr, "sqrt2-monolithic-QR.png", 10, 3); SaveAsPng(qr, "sqrt2-monolithic-QR.png", 10, 3);
segs = new List<QrSegment> var segs = new List<QrSegment>
{ {
QrSegment.MakeAlphanumeric(silver0), QrSegment.MakeAlphanumeric(silver0),
QrSegment.MakeNumeric(silver1) QrSegment.MakeNumeric(silver1)
@ -110,10 +105,10 @@ namespace Io.Nayuki.QrCodeGen.Demo
SaveAsPng(qr, "sqrt2-segmented-QR.png", 10, 3); SaveAsPng(qr, "sqrt2-segmented-QR.png", 10, 3);
// Illustration "golden" // Illustration "golden"
string golden0 = "Golden ratio φ = 1."; const string golden0 = "Golden ratio φ = 1.";
string golden1 = const string golden1 =
"6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374"; "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374";
string golden2 = "......"; const string golden2 = "......";
qr = QrCode.EncodeText(golden0 + golden1 + golden2, QrCode.Ecc.Low); qr = QrCode.EncodeText(golden0 + golden1 + golden2, QrCode.Ecc.Low);
SaveAsPng(qr, "phi-monolithic-QR.png", 8, 5); SaveAsPng(qr, "phi-monolithic-QR.png", 8, 5);
@ -128,14 +123,11 @@ namespace Io.Nayuki.QrCodeGen.Demo
SaveAsPng(qr, "phi-segmented-QR.png", 8, 5); SaveAsPng(qr, "phi-segmented-QR.png", 8, 5);
// Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters // Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters
string madoka = "「魔法少女まどか☆マギカ」って、 ИАИ desu κα?"; const string madoka = "「魔法少女まどか☆マギカ」って、 ИАИ desu κα?";
qr = QrCode.EncodeText(madoka, QrCode.Ecc.Low); qr = QrCode.EncodeText(madoka, QrCode.Ecc.Low);
SaveAsPng(qr, "madoka-utf8-QR.png", 9, 4); SaveAsPng(qr, "madoka-utf8-QR.png", 9, 4);
segs = new List<QrSegment> segs = new List<QrSegment> { QrSegmentAdvanced.MakeKanji(madoka) };
{
// QrSegmentAdvanced.MakeKanji(madoka)
};
qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Low); qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Low);
SaveAsPng(qr, "madoka-kanji-QR.png", 9, 4); SaveAsPng(qr, "madoka-kanji-QR.png", 9, 4);
} }
@ -143,12 +135,9 @@ namespace Io.Nayuki.QrCodeGen.Demo
// Creates QR Codes with the same size and contents but different mask patterns. // Creates QR Codes with the same size and contents but different mask patterns.
private static void DoMaskDemo() { private static void DoMaskDemo() {
QrCode qr;
List<QrSegment> segs;
// Project Nayuki URL // Project Nayuki URL
segs = QrSegment.MakeSegments("https://www.nayuki.io/"); var segs = QrSegment.MakeSegments("https://www.nayuki.io/");
qr = QrCode.EncodeSegments(segs, QrCode.Ecc.High, QrCode.MinVersion, QrCode.MaxVersion, -1, true); // Automatic mask var qr = QrCode.EncodeSegments(segs, QrCode.Ecc.High, QrCode.MinVersion, QrCode.MaxVersion, -1, true);
SaveAsPng(qr, "project-nayuki-automask-QR.png", 8, 6); SaveAsPng(qr, "project-nayuki-automask-QR.png", 8, 6);
qr = QrCode.EncodeSegments(segs, QrCode.Ecc.High, QrCode.MinVersion, QrCode.MaxVersion, 3, true); // Force mask 3 qr = QrCode.EncodeSegments(segs, QrCode.Ecc.High, QrCode.MinVersion, QrCode.MaxVersion, 3, true); // Force mask 3
SaveAsPng(qr, "project-nayuki-mask3-QR.png", 8, 6); SaveAsPng(qr, "project-nayuki-mask3-QR.png", 8, 6);
@ -165,6 +154,9 @@ namespace Io.Nayuki.QrCodeGen.Demo
SaveAsPng(qr, "unicode-mask7-QR.png", 10, 3); SaveAsPng(qr, "unicode-mask7-QR.png", 10, 3);
} }
#endregion
#region Utilities #region Utilities
private static void SaveAsPng(QrCode qrCode, string filename, int scale, int border) private static void SaveAsPng(QrCode qrCode, string filename, int scale, int border)

@ -0,0 +1,77 @@
/*
* QR Code generator library (.NET)
*
* Copyright (c) Project Nayuki. (MIT License)
* https://www.nayuki.io/page/qr-code-generator-library
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
using System.Collections.Generic;
using Xunit;
namespace Io.Nayuki.QrCodeGen.Test
{
public class OptimalSegmentTest
{
private const string Text1 = "2342342340ABC234234jkl~~";
private static readonly string[] Modules1 = {
"XXXXXXX XXXX XXXX X XXXXXXX",
"X X X XX X X",
"X XXX X X XXX XXX X X XXX X",
"X XXX X XXXXX X XX X XXX X",
"X XXX X X XX XX X XXX X",
"X X X X X X X",
"XXXXXXX X X X X X X X XXXXXXX",
" X XX XXX ",
" X XXX XX XXX X XX X X",
" X XX X XX X XXXXX",
" XXX X XXXX X ",
" X X X X XX X XXX",
"XX X XXXXX XXXXXXXXX XXXX ",
" X X X XX X X X X ",
" XXXXX XXX XXX XX X X",
"XXXXX XX XX X XXX X XXX ",
"XXX XXX XXX X XX ",
" X X XX X X X X ",
"X X XXXX XXXX X X X X X ",
" X X X XX X X XXX X XX XXX",
"X X XX X XXX XX XXXXXXX X",
" X X X X XXXX ",
"XXXXXXX X XX X XXX X X X ",
"X X X X XX X XX X XX ",
"X XXX X XXXX XX X X XXXXX ",
"X XXX X X X X XX XX X ",
"X XXX X X X XXXXXX X X",
"X X XXX XX X X XXX X ",
"XXXXXXX XXXX X XX XX X"
};
[Fact]
private void OptimalSegmentCode()
{
var segments = QrSegmentAdvanced.MakeSegmentsOptimally(Text1, QrCode.Ecc.High, 1, 40);
var qrCode = QrCode.EncodeSegments(segments, QrCode.Ecc.High);
Assert.Same(QrCode.Ecc.High, qrCode.ErrorCorrectionLevel);
Assert.Equal(29, qrCode.Size);
Assert.Equal(0, qrCode.Mask);
Assert.Equal(Modules1, TestHelper.ToStringArray(qrCode));
}
}
}
Loading…
Cancel
Save