1313
1414namespace Net . Codecrete . QrCodeGenerator
1515{
16- #if DEBUG
17- public
18- #else
19- internal
20- #endif
21- static class QrCodeBuilder
16+ internal static class QrCodeBuilder
2217 {
2318 #region Caches
2419
@@ -31,28 +26,27 @@ static class QrCodeBuilder
3126
3227 #region Build
3328
34- internal static QrCode Build ( List < DataSegment > dataSegments , int ecc , int minVersion = 1 , int maxVersion = 40 , bool boostEcc = true )
29+ internal static QrCode Build ( List < DataSegment > dataSegments , int ecc , int minVersion = 1 , int maxVersion = 40 , bool boostEcc = true , EncodingInfo encodingInfo = null )
3530 {
36- #if DEBUG
37- if ( DebugAccess != null )
31+ if ( encodingInfo != null )
3832 {
39- DebugAccess . DataSegments = dataSegments ;
33+ encodingInfo . DataSegments = dataSegments ;
4034 }
41- #endif
35+
4236 var result = FindVersionAndEcc ( dataSegments , ecc , minVersion , maxVersion , boostEcc ) ;
4337 var version = result . Item1 ;
4438 ecc = result . Item2 ;
4539
4640 var codewords = BuildCodewords ( dataSegments , version , ecc ) ;
4741 codewords = AddErrorCorrection ( codewords , version , ecc ) ;
48- return Build ( codewords , ecc , version ) ;
42+ return Build ( codewords , ecc , version , encodingInfo ) ;
4943 }
5044
51- private static QrCode Build ( byte [ ] codewords , int ecc , int version )
45+ private static QrCode Build ( byte [ ] codewords , int ecc , int version , EncodingInfo encodingInfo )
5246 {
5347 var modules = CreateWithFixedPatterns ( version ) ;
5448 FillPayload ( modules , codewords , version ) ;
55- var pattern = ApplyBestPattern ( modules , version , ecc ) ;
49+ var pattern = ApplyBestPattern ( modules , version , ecc , encodingInfo ) ;
5650 return new QrCode ( modules , ( QrCode . Ecc ) ecc , pattern ) ;
5751 }
5852
@@ -697,7 +691,7 @@ private static BitMatrix CreatePattern(int patternIndex, int version)
697691 // "Mask Pattern Selection".
698692 private static readonly int [ ] PatternEvaluationOrder = { 2 , 3 , 7 , 4 , 6 , 5 , 0 , 1 } ;
699693
700- private static int ApplyBestPattern ( BitMatrix modules , int version , int ecc )
694+ private static int ApplyBestPattern ( BitMatrix modules , int version , int ecc , EncodingInfo encodingInfo = null )
701695 {
702696 var transposed = modules . Copy ( ) ;
703697 transposed . Transpose ( ) ;
@@ -714,18 +708,14 @@ private static int ApplyBestPattern(BitMatrix modules, int version, int ecc)
714708 modules . Xor ( mask ) ;
715709 transposed . Xor ( maskT ) ;
716710
717- #if DEBUG
718711 int penalty ;
719- if ( DebugAccess != null ) {
720- penalty = Penalty . CalculatePenaltyDebug ( modules , transposed , ref DebugAccess . Penalties [ pattern ] ) ;
712+ if ( encodingInfo != null ) {
713+ penalty = Penalty . CalculatePenaltyFully ( modules , transposed , ref encodingInfo . Penalties [ pattern ] ) ;
721714 }
722715 else
723716 {
724717 penalty = Penalty . CalculatePenalty ( modules , transposed , lowestPenalty ) ;
725718 }
726- #else
727- var penalty = Penalty . CalculatePenalty ( modules , transposed , lowestPenalty ) ;
728- #endif
729719
730720 // undo pattern
731721 modules . Xor ( mask ) ;
@@ -737,12 +727,10 @@ private static int ApplyBestPattern(BitMatrix modules, int version, int ecc)
737727 }
738728 }
739729
740- #if DEBUG
741- if ( DebugAccess != null && DebugAccess . ForcedDataMask >= 0 )
730+ if ( encodingInfo != null && encodingInfo . ForcedDataMask >= 0 )
742731 {
743- bestPattern = DebugAccess . ForcedDataMask ;
732+ bestPattern = encodingInfo . ForcedDataMask ;
744733 }
745- #endif
746734
747735 DrawFormatInformation ( modules , ecc , bestPattern ) ;
748736 var bestMask = GetDataMaskPattern ( bestPattern , version ) ;
@@ -993,31 +981,5 @@ internal static int GetVersionInformationBits(int version)
993981 } ;
994982
995983 #endregion
996-
997- #if DEBUG
998- #region Debugging
999-
1000- public struct PenaltyInfo
1001- {
1002- public int HorizontalStreaks { get ; set ; }
1003- public int VerticalStreaks { get ; set ; }
1004- public int Blocks { get ; set ; }
1005- public int HorizontalFinderPatterns { get ; set ; }
1006- public int VerticalFinderPatterns { get ; set ; }
1007- public int ColorBalance { get ; set ; }
1008- public int Total { get ; set ; }
1009- }
1010-
1011- public class DebugInfo
1012- {
1013- public PenaltyInfo [ ] Penalties { get ; } = new PenaltyInfo [ 8 ] ;
1014- public int ForcedDataMask { get ; set ; } = - 1 ;
1015- public List < DataSegment > DataSegments { get ; set ; }
1016- }
1017-
1018- public static DebugInfo DebugAccess { get ; set ; }
1019-
1020- #endregion
1021- #endif
1022984 }
1023985}
0 commit comments