You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ByteMatrix.cs 4.3 kB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2008 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using System.Text;
  18. namespace ZXing.QrCode.Internal
  19. {
  20. /// <summary>
  21. /// JAVAPORT: The original code was a 2D array of ints, but since it only ever gets assigned
  22. /// 0, 1 and 2 I'm going to use less memory and go with bytes.
  23. /// </summary>
  24. /// <author>dswitkin@google.com (Daniel Switkin)</author>
  25. public sealed class ByteMatrix
  26. {
  27. private readonly byte[][] bytes;
  28. private readonly int width;
  29. private readonly int height;
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="ByteMatrix"/> class.
  32. /// </summary>
  33. /// <param name="width">The width.</param>
  34. /// <param name="height">The height.</param>
  35. public ByteMatrix(int width, int height)
  36. {
  37. bytes = new byte[height][];
  38. for (var i = 0; i < height; i++)
  39. bytes[i] = new byte[width];
  40. this.width = width;
  41. this.height = height;
  42. }
  43. /// <summary>
  44. /// Gets the height.
  45. /// </summary>
  46. public int Height
  47. {
  48. get { return height; }
  49. }
  50. /// <summary>
  51. /// Gets the width.
  52. /// </summary>
  53. public int Width
  54. {
  55. get { return width; }
  56. }
  57. /// <summary>
  58. /// Gets or sets the <see cref="System.Int32"/> with the specified x.
  59. /// </summary>
  60. public int this[int x, int y]
  61. {
  62. get { return bytes[y][x]; }
  63. set { bytes[y][x] = (byte)value; }
  64. }
  65. /// <summary>
  66. /// an internal representation as bytes, in row-major order. array[y][x] represents point (x,y)
  67. /// </summary>
  68. public byte[][] Array
  69. {
  70. get { return bytes; }
  71. }
  72. /// <summary>
  73. /// Sets the specified x.
  74. /// </summary>
  75. /// <param name="x">The x.</param>
  76. /// <param name="y">The y.</param>
  77. /// <param name="value">The value.</param>
  78. public void set(int x, int y, byte value)
  79. {
  80. bytes[y][x] = value;
  81. }
  82. /// <summary>
  83. /// Sets the specified x.
  84. /// </summary>
  85. /// <param name="x">The x.</param>
  86. /// <param name="y">The y.</param>
  87. /// <param name="value">if set to <c>true</c> [value].</param>
  88. public void set(int x, int y, bool value)
  89. {
  90. bytes[y][x] = (byte)(value ? 1 : 0);
  91. }
  92. /// <summary>
  93. /// Clears the specified value.
  94. /// </summary>
  95. /// <param name="value">The value.</param>
  96. public void clear(byte value)
  97. {
  98. for (int y = 0; y < height; ++y)
  99. {
  100. for (int x = 0; x < width; ++x)
  101. {
  102. bytes[y][x] = value;
  103. }
  104. }
  105. }
  106. /// <summary>
  107. /// Returns a <see cref="System.String"/> that represents this instance.
  108. /// </summary>
  109. /// <returns>
  110. /// A <see cref="System.String"/> that represents this instance.
  111. /// </returns>
  112. override public String ToString()
  113. {
  114. var result = new StringBuilder(2 * width * height + 2);
  115. for (int y = 0; y < height; ++y)
  116. {
  117. for (int x = 0; x < width; ++x)
  118. {
  119. switch (bytes[y][x])
  120. {
  121. case 0:
  122. result.Append(" 0");
  123. break;
  124. case 1:
  125. result.Append(" 1");
  126. break;
  127. default:
  128. result.Append(" ");
  129. break;
  130. }
  131. }
  132. result.Append('\n');
  133. }
  134. return result.ToString();
  135. }
  136. }
  137. }