context: /*
  * Copyright 2008 ZXing authors
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
  
  using ZXing.Common;
  
  namespace ZXing.QrCode.Internal
  {
     /// <summary>
     /// 
     /// </summary>
     /// <author>
     /// satorux@google.com (Satoru Takabayashi) - creator
     /// </author>
     public static class MatrixUtil
     {
        private static readonly int[][] POSITION_DETECTION_PATTERN = new int[][]
                                                                        {
                                                                           new int[] { 1, 1, 1, 1, 1, 1, 1 }, 
                                                                           new int[] { 1, 0, 0, 0, 0, 0, 1 }, 
                                                                           new int[] { 1, 0, 1, 1, 1, 0, 1 }, 
                                                                           new int[] { 1, 0, 1, 1, 1, 0, 1 }, 
                                                                           new int[] { 1, 0, 1, 1, 1, 0, 1 }, 
                                                                           new int[] { 1, 0, 0, 0, 0, 0, 1 }, 
                                                                           new int[] { 1, 1, 1, 1, 1, 1, 1 }
                                                                        };
  
        private static readonly int[][] POSITION_ADJUSTMENT_PATTERN = new int[][]
                                                                         {
                                                                            new int[] { 1, 1, 1, 1, 1 }, 
                                                                            new int[] { 1, 0, 0, 0, 1 }, 
                                                                            new int[] { 1, 0, 1, 0, 1 }, 
                                                                            new int[] { 1, 0, 0, 0, 1 }, 
                                                                            new int[] { 1, 1, 1, 1, 1 }
                                                                         };
  
        // From Appendix E. Table 1, JIS0510X:2004 (p 71). The table was double-checked by komatsu.
        private static readonly int[][] POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE = new int[][]
                                                                                          {
                                                                                             new int[] { -1, -1, -1, -1, -1, -1, -1 }, 
                                                                                             new int[] { 6, 18, -1, -1, -1, -1, -1 }, 
                                                                                             new int[] { 6, 22, -1, -1, -1, -1, -1 }, 
                                                                                             new int[] { 6, 26, -1, -1, -1, -1, -1 }, 
                                                                                             new int[] { 6, 30, -1, -1, -1, -1, -1 }, 
                                                                                             new int[] { 6, 34, -1, -1, -1, -1, -1 }, 
                                                                                             new int[] { 6, 22, 38, -1, -1, -1, -1 }, 
                                                                                             new int[] { 6, 24, 42, -1, -1, -1, -1 }, 
                                                                                             new int[] { 6, 26, 46, -1, -1, -1, -1 }, 
                                                                                             new int[] { 6, 28, 50, -1, -1, -1, -1 }, 
                                                                                             new int[] { 6, 30, 54, -1, -1, -1, -1 }, 
                                                                                             new int[] { 6, 32, 58, -1, -1, -1, -1 }, 
                                                                                             new int[] { 6, 34, 62, -1, -1, -1, -1 }, 
                                                                                             new int[] { 6, 26, 46, 66, -1, -1, -1 }, 
                                                                                             new int[] { 6, 26, 48, 70, -1, -1, -1 }, 
                                                                                             new int[] { 6, 26, 50, 74, -1, -1, -1 }, 
                                                                                             new int[] { 6, 30, 54, 78, -1, -1, -1 }, 
                                                                                             new int[] { 6, 30, 56, 82, -1, -1, -1 }, 
                                                                                             new int[] { 6, 30, 58, 86, -1, -1, -1 }, 
                                                                                             new int[] { 6, 34, 62, 90, -1, -1, -1 }, 
                                                                                             new int[] { 6, 28, 50, 72, 94, -1, -1 }, 
                                                                                             new int[] { 6, 26, 50, 74, 98, -1, -1 }, 
                                                                                             new int[] { 6, 30, 54, 78, 102, -1, -1 }, 
                                                                                             new int[] { 6, 28, 54, 80, 106, -1, -1 }, 
                                                                                             new int[] { 6, 32, 58, 84, 110, -1, -1 }, 
                                                                                             new int[] { 6, 30, 58, 86, 114, -1, -1 }, 
                                                                                             new int[] { 6, 34, 62, 90, 118, -1, -1 }, 
                                                                                             new int[] { 6, 26, 50, 74, 98, 122, -1 }, 
                                                                                             new int[] { 6, 30, 54, 78, 102, 126, -1 },
                                                                                             new int[] { 6, 26, 52, 78, 104, 130, -1 }, 
                                                                                             new int[] { 6, 30, 56, 82, 108, 134, -1 }, 
                                                                                             new int[] { 6, 34, 60, 86, 112, 138, -1 },
                                                                                             new int[] { 6, 30, 58, 86, 114, 142, -1 },
                                                                                             new int[] { 6, 34, 62, 90, 118, 146, -1 }, 
                                                                                             new int[] { 6, 30, 54, 78, 102, 126, 150 },
                                                                                             new int[] { 6, 24, 50, 76, 102, 128, 154 }, 
                                                                                             new int[] { 6, 28, 54, 80, 106, 132, 158 }, 
                                                                                             new int[] { 6, 32, 58, 84, 110, 136, 162 }, 
                                                                                             new int[] { 6, 26, 54, 82, 110, 138, 166 }, 
                                                                                             new int[] { 6, 30, 58, 86, 114, 142, 170 }
                                                                                          };
  
        // Type info cells at the left top corner.
        private static readonly int[][] TYPE_INFO_COORDINATES = new int[][]
                                                                   {
                                                                      new int[] { 8, 0 }, 
                                                                      new int[] { 8, 1 }, 
                                                                      new int[] { 8, 2 }, 
                                                                      new int[] { 8, 3 }, 
                                                                      new int[] { 8, 4 }, 
                                                                      new int[] { 8, 5 }, 
                                                                      new int[] { 8, 7 }, 
                                                                      new int[] { 8, 8 }, 
                                                                      new int[] { 7, 8 }, 
                                                                      new int[] { 5, 8 }, 
                                                                      new int[] { 4, 8 }, 
                                                                      new int[] { 3, 8 }, 
                                                                      new int[] { 2, 8 }, 
                                                                      new int[] { 1, 8 }, 
                                                                      new int[] { 0, 8 }
                                                                   };
  
        // From Appendix D in JISX0510:2004 (p. 67)
        private const int VERSION_INFO_POLY = 0x1f25; // 1 1111 0010 0101
  
        // From Appendix C in JISX0510:2004 (p.65).
        private const int TYPE_INFO_POLY = 0x537;
        private const int TYPE_INFO_MASK_PATTERN = 0x5412;
  
        /// <summary>
        /// Set all cells to 2.  2 means that the cell is empty (not set yet).
        ///
        /// JAVAPORT: We shouldn't need to do this at all. The code should be rewritten to begin encoding
        /// with the ByteMatrix initialized all to zero.
        /// </summary>
        /// <param name="matrix">The matrix.</param>
        public static void clearMatrix(ByteMatrix matrix)
        {
           matrix.clear(2);
        }
  
        /// <summary>
        /// Build 2D matrix of QR Code from "dataBits" with "ecLevel", "version" and "getMaskPattern". On
        /// success, store the result in "matrix" and return true.
        /// </summary>
        /// <param name="dataBits">The data bits.</param>
        /// <param name="ecLevel">The ec level.</param>
        /// <param name="version">The version.</param>
        /// <param name="maskPattern">The mask pattern.</param>
        /// <param name="matrix">The matrix.</param>
        public static void buildMatrix(BitArray dataBits, ErrorCorrectionLevel ecLevel, Version version, int maskPattern, ByteMatrix matrix)
        {
           clearMatrix(matrix);
           embedBasicPatterns(version, matrix);
           // Type information appear with any version.
           embedTypeInfo(ecLevel, maskPattern, matrix);
           // Version info appear if version >= 7.
           maybeEmbedVersionInfo(version, matrix);
           // Data should be embedded at end.
           embedDataBits(dataBits, maskPattern, matrix);
        }
  
        /// <summary>
        /// Embed basic patterns. On success, modify the matrix and return true.
        /// The basic patterns are:
        /// - Position detection patterns
        /// - Timing patterns
        /// - Dark dot at the left bottom corner
        /// - Position adjustment patterns, if need be
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="matrix">The matrix.</param>
        public static void embedBasicPatterns(Version version, ByteMatrix matrix)
        {
           // Let's get started with embedding big squares at corners.
           embedPositionDetectionPatternsAndSeparators(matrix);
           // Then, embed the dark dot at the left bottom corner.
           embedDarkDotAtLeftBottomCorner(matrix);
  
           // Position adjustment patterns appear if version >= 2.
           maybeEmbedPositionAdjustmentPatterns(version, matrix);
           // Timing patterns should be embedded after position adj. patterns.
           embedTimingPatterns(matrix);
        }
  
        /// <summary>
        /// Embed type information. On success, modify the matrix.
        /// </summary>
        /// <param name="ecLevel">The ec level.</param>
        /// <param name="maskPattern">The mask pattern.</param>
        /// <param name="matrix">The matrix.</param>
        public static void embedTypeInfo(ErrorCorrectionLevel ecLevel, int maskPattern, ByteMatrix matrix)
        {
           BitArray typeInfoBits = new BitArray();
           makeTypeInfoBits(ecLevel, maskPattern, typeInfoBits);
  
           for (int i = 0; i < typeInfoBits.Size; ++i)
           {
              // Place bits in LSB to MSB order.  LSB (least significant bit) is the last value in
              // "typeInfoBits".
              int bit = typeInfoBits[typeInfoBits.Size - 1 - i] ? 1 : 0;
  
              // Type info bits at the left top corner. See 8.9 of JISX0510:2004 (p.46).
              int x1 = TYPE_INFO_COORDINATES[i][0];
              int y1 = TYPE_INFO_COORDINATES[i][1];
              matrix[x1, y1] = bit;
  
              if (i < 8)
              {
                 // Right top corner.
                 int x2 = matrix.Width - i - 1;
                 int y2 = 8;
                 matrix[x2, y2] = bit;
              }
              else
              {
                 // Left bottom corner.
                 int x2 = 8;
                 int y2 = matrix.Height - 7 + (i - 8);
                 matrix[x2, y2] = bit;
              }
           }
        }
  
        /// <summary>
        /// Embed version information if need be. On success, modify the matrix and return true.
        /// See 8.10 of JISX0510:2004 (p.47) for how to embed version information.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="matrix">The matrix.</param>
        public static void maybeEmbedVersionInfo(Version version, ByteMatrix matrix)
        {
           if (version.VersionNumber < 7)
           {
              // Version info is necessary if version >= 7.
              return; // Don't need version info.
           }
           BitArray versionInfoBits = new BitArray();
           makeVersionInfoBits(version, versionInfoBits);
  
           int bitIndex = 6 * 3 - 1; // It will decrease from 17 to 0.
           for (int i = 0; i < 6; ++i)
           {
              for (int j = 0; j < 3; ++j)
              {
                 // Place bits in LSB (least significant bit) to MSB order.
                 var bit = versionInfoBits[bitIndex] ? 1 : 0;
                 bitIndex--;
                 // Left bottom corner.
                 matrix[i, matrix.Height - 11 + j] = bit;
                 // Right bottom corner.
                 matrix[matrix.Height - 11 + j, i] = bit;
              }
           }
        }
  
        /// <summary>
        /// Embed "dataBits" using "getMaskPattern". On success, modify the matrix and return true.
        /// For debugging purposes, it skips masking process if "getMaskPattern" is -1.
        /// See 8.7 of JISX0510:2004 (p.38) for how to embed data bits.
        /// </summary>
        /// <param name="dataBits">The data bits.</param>
        /// <param name="maskPattern">The mask pattern.</param>
        /// <param name="matrix">The matrix.</param>
        public static void embedDataBits(BitArray dataBits, int maskPattern, ByteMatrix matrix)
        {
           int bitIndex = 0;
           int direction = -1;
           // Start from the right bottom cell.
           int x = matrix.Width - 1;
           int y = matrix.Height - 1;
           while (x > 0)
           {
              // Skip the vertical timing pattern.
              if (x == 6)
              {
                 x -= 1;
              }
              while (y >= 0 && y < matrix.Height)
              {
                 for (int i = 0; i < 2; ++i)
                 {
                    int xx = x - i;
                    // Skip the cell if it's not empty.
  
gt:                   if (!isEmpty(matrix[xx, y]))