Class BitMatrix
Represents a 2D matrix of bits. In function arguments below, and throughout the common module, x is the column position, and y is the row position. The ordering is always x, y. The origin is at the top-left.
Internally the bits are represented in a 1-D array of 32-bit ints. However, each row begins with a new int. This is done intentionally so that we can copy out a row into a BitArray very efficiently.
The ordering of bits is row-major. Within each int, the least significant bits are used first, meaning they represent lower x values. This is compatible with BitArray's implementation.
public sealed class BitMatrix
- Inheritance
-
BitMatrix
- Inherited Members
Constructors
BitMatrix(int)
Creates an empty square BitMatrix.
public BitMatrix(int dimension)
Parameters
dimension
intheight and width
BitMatrix(int, int)
Creates an empty square BitMatrix.
public BitMatrix(int width, int height)
Parameters
Properties
Dimension
This method is for compatibility with older code. It's only logical to call if the matrix is square, so I'm throwing if that's not the case.
public int Dimension { get; }
Property Value
- int
row/column dimension of this matrix
Height
public int Height { get; }
Property Value
- int
The height of the matrix
this[int, int]
Gets the requested bit, where true means black.
public bool this[int x, int y] { get; set; }
Parameters
Property Value
- bool
value of given bit in matrix
RowSize
public int RowSize { get; }
Property Value
- int
The rowsize of the matrix
Width
public int Width { get; }
Property Value
- int
The width of the matrix
Methods
Clone()
Clones this instance.
public object Clone()
Returns
Equals(object)
Determines whether the specified object is equal to this instance.
public override bool Equals(object obj)
Parameters
Returns
GetHashCode()
Returns a hash code for this instance.
public override int GetHashCode()
Returns
- int
A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
ToString()
Returns a string that represents this instance.
public override string ToString()
Returns
ToString(string, string)
Returns a string that represents this instance.
public string ToString(string setString, string unsetString)
Parameters
Returns
ToString(string, string, string)
Returns a string that represents this instance.
public string ToString(string setString, string unsetString, string lineSeparator)
Parameters
setString
stringThe set string.
unsetString
stringThe unset string.
lineSeparator
stringThe line separator.
Returns
clear()
Clears all bits (sets to false).
public void clear()
flip()
Flips every bit in the matrix.
public void flip()
flip(int, int)
Flips the given bit.
public void flip(int x, int y)
Parameters
flipWhen(Func<int, int, bool>)
flip all of the bits, if shouldBeFlipped is true for the coordinates
public void flipWhen(Func<int, int, bool> shouldBeFlipped)
Parameters
shouldBeFlipped
Func<int, int, bool>should return true, if the bit at a given coordinate should be flipped
getBottomRightOnBit()
bottom right
public int[] getBottomRightOnBit()
Returns
- int[]
getEnclosingRectangle()
This is useful in detecting the enclosing rectangle of a 'pure' barcode.
public int[] getEnclosingRectangle()
Returns
- int[]
{left,top,width,height} enclosing rectangle of all 1 bits, or null if it is all white
getRow(int, BitArray)
A fast method to retrieve one row of data from the matrix as a BitArray.
public BitArray getRow(int y, BitArray row)
Parameters
y
intThe row to retrieve
row
BitArrayAn optional caller-allocated BitArray, will be allocated if null or too small
Returns
- BitArray
The resulting BitArray - this reference should always be used even when passing your own row
getTopLeftOnBit()
This is useful in detecting a corner of a 'pure' barcode.
public int[] getTopLeftOnBit()
Returns
- int[]
{x,y} coordinate of top-left-most 1 bit, or null if it is all white
parse(bool[][])
Interprets a 2D array of booleans as a BitMatrix, where "true" means an "on" bit.
public static BitMatrix parse(bool[][] image)
Parameters
image
bool[][]bits of the image, as a row-major 2D array. Elements are arrays representing rows
Returns
parse(string, string, string)
parse the string representation to a bitmatrix
public static BitMatrix parse(string stringRepresentation, string setString, string unsetString)
Parameters
Returns
rotate(int)
Modifies this {@code BitMatrix} to represent the same but rotated the given degrees(0, 90, 180, 270)
public void rotate(int degrees)
Parameters
degrees
intnumber of degrees to rotate through counter-clockwise(0, 90, 180, 270)
Exceptions
rotate180()
Modifies this {@code BitMatrix} to represent the same but rotated 180 degrees
public void rotate180()
rotate90()
Modifies this {@code BitMatrix} to represent the same but rotated 90 degrees counterclockwise
public void rotate90()
setRegion(int, int, int, int)
Sets a square region of the bit matrix to true.
public void setRegion(int left, int top, int width, int height)
Parameters
left
intThe horizontal position to begin at (inclusive)
top
intThe vertical position to begin at (inclusive)
width
intThe width of the region
height
intThe height of the region
setRow(int, BitArray)
Sets the row.
public void setRow(int y, BitArray row)
Parameters
xor(BitMatrix)
Exclusive-or (XOR): Flip the bit in this {@code BitMatrix} if the corresponding mask bit is set.
public void xor(BitMatrix mask)
Parameters
mask
BitMatrixThe mask.