java.lang.Object
imagingbook.common.util.bits.BitMap
This class implements a true 2D bitmap container, i.e., each 0/1 element occupies only a single bit (unlike
boolean
arrays, which require at least 8 bits per element).- Version:
- 2022/09/13
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BitMap
create
(int width, int height) Creates a new 2D bitmap of the specified size.boolean
get
(int x, int y) Returnstrue
is the specified element is set (1),false
otherwise (0).boolean
get
(Pnt2d.PntInt p) Returnstrue
is the specified element is set (1),false
otherwise (0).Returns the underlying 1DBitVector
.int
Returns the height of thisBitMap
.int
getWidth()
Returns the width of thisBitMap
.void
set
(int x, int y) Sets the specified element (to bit-value 1).void
set
(int x, int y, boolean val) Sets the specified bit-element to the given boolean value (1 fortrue
, 0 forfalse
).void
set
(Pnt2d.PntInt p) Sets the specified element (to bit-value 1).void
set
(Pnt2d.PntInt p, boolean val) Sets the specified bit-element to the given boolean value (1 fortrue
, 0 forfalse
).void
setAll()
Sets all elements to 1.byte[]
Returns the contents of this bitmap as a one-dimensionalbyte
array, with elements in row-major order.void
unset
(int x, int y) Unsets the specified element (to bit-value 0).void
Unsets the specified element (to bit-value 0).void
unsetAll()
Sets all elements to 0.
-
Constructor Details
-
BitMap
Constructor, creates an empty bitmap (with all elements set to 0). Both dimensions must be at least 1.- Parameters:
width
- the width of the new bitmapheight
- the height of the new bitmap
-
BitMap
Constructor, creates a bitmap from a one-dimensional byte array. Elements of the specified byte array are assumed in row-major order, zero values map to 0, anything else to 1. Passingnull
for the byte array creates an empty bitmap (with all elements set to 0). Both dimensions must be at least 1.- Parameters:
width
- the width of the new bitmapheight
- the height of the new bitmapbytes
- a byte array (null
is allowed)
-
-
Method Details
-
getWidth
Returns the width of thisBitMap
.- Returns:
- the width
-
getHeight
Returns the height of thisBitMap
.- Returns:
- the height
-
get
Returnstrue
is the specified element is set (1),false
otherwise (0).- Parameters:
x
- the x-coordinatey
- the y-coordinate- Returns:
- as described
-
get
Returnstrue
is the specified element is set (1),false
otherwise (0).- Parameters:
p
- the x/y-coordinate (point)- Returns:
- as described
-
set
Sets the specified bit-element to the given boolean value (1 fortrue
, 0 forfalse
).- Parameters:
x
- the x-coordinatey
- the y-coordinateval
- a boolean value
-
set
Sets the specified bit-element to the given boolean value (1 fortrue
, 0 forfalse
).- Parameters:
p
- the x/y-coordinate (point)val
- a boolean value
-
set
Sets the specified element (to bit-value 1).- Parameters:
x
- the x-coordinatey
- the y-coordinate
-
set
Sets the specified element (to bit-value 1).- Parameters:
p
- the x/y-coordinate (point)
-
unset
Unsets the specified element (to bit-value 0).- Parameters:
x
- the x-coordinatey
- the y-coordinate
-
unset
Unsets the specified element (to bit-value 0).- Parameters:
p
- the x/y-coordinate (point)
-
setAll
Sets all elements to 1. -
unsetAll
Sets all elements to 0. -
getBitVector
Returns the underlying 1DBitVector
.- Returns:
- the bit vector
-
toByteArray
Returns the contents of this bitmap as a one-dimensionalbyte
array, with elements in row-major order. Bit-value 0 maps to byte value 0, value 1 maps to 1.- Returns:
- a one-dimensional
byte
array
-
create
Creates a new 2D bitmap of the specified size.- Parameters:
width
- the width of the new bitmapheight
- the height of the new bitmap- Returns:
- the new bitmap
-