1 module ithox.qrcode.encoder.bytematrix; 2 /** 3 * Byte matrix. 4 * author donglei 5 */ 6 class ByteMatrix 7 { 8 /** 9 * Bytes in the matrix, represented as array. 10 * 11 * @var SplFixedArray 12 */ 13 protected int[][] _bytes; 14 /** 15 * Width of the matrix. 16 * 17 * @var integer 18 */ 19 protected int _width; 20 /** 21 * Height of the matrix. 22 * 23 * @var integer 24 */ 25 protected int _height; 26 /** 27 * Creates a new byte matrix. 28 * 29 * @param integer $width 30 * @param integer $height 31 */ 32 public this(int width, int height) 33 { 34 this._height = height; 35 this._width = width; 36 //this._bytes[][] = new int[height][]; 37 for (auto i = 0; i < height; i++) 38 this._bytes ~= new int[width]; 39 40 } 41 42 public @property int width() 43 { 44 return _width; 45 } 46 47 public @property void width(int _m) 48 { 49 _width = _m; 50 } 51 52 public @property int height() 53 { 54 return _height; 55 } 56 57 public @property void height(int _m) 58 { 59 _height = _m; 60 } 61 62 /** 63 * Gets the internal representation of the matrix. 64 * 65 * @return SplFixedArray 66 */ 67 public int[][] getArray() 68 { 69 return this._bytes; 70 } 71 /** 72 * Gets the byte for a specific position. 73 * 74 * @param integer $x 75 * @param integer $y 76 * @return integer 77 */ 78 public int get(int x, int y) 79 { 80 return this._bytes[y][x]; 81 } 82 /** 83 * Sets the byte for a specific position. 84 * 85 * @param integer $x 86 * @param integer $y 87 * @param integer $value 88 * @return void 89 */ 90 public void set(int x, int y, int value) 91 { 92 this._bytes[y][x] = value; 93 } 94 /** 95 * Clears the matrix with a specific value. 96 * 97 * @param integer $value 98 * @return void 99 */ 100 public void clear(int value) 101 { 102 for (auto i = 0; i < height; i++) 103 this._bytes[i][] = value; 104 } 105 /** 106 * Returns a string representation of the matrix. 107 * 108 * @return string 109 */ 110 public override string toString() 111 { 112 import std.array; 113 114 Appender!string result = appender!string(); 115 for (int y = 0; y < this._height; y++) 116 { 117 for (int x = 0; x < this._width; x++) 118 { 119 switch (this._bytes[y][x]) 120 { 121 case 0: 122 result.put(" 0"); 123 break; 124 case 1: 125 result.put(" 1"); 126 break; 127 default: 128 result.put(" "); 129 break; 130 } 131 } 132 result.put("\n"); 133 } 134 return result.data; 135 } 136 }