Class BitMap

java.lang.Object
imagingbook.common.util.bits.BitMap

public class BitMap extends Object
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
    Constructor
    Description
    BitMap(int width, int height)
    Constructor, creates an empty bitmap (with all elements set to 0).
    BitMap(int width, int height, byte[] bytes)
    Constructor, creates a bitmap from a one-dimensional byte array.
  • Method Summary

    Modifier and Type
    Method
    Description
    static BitMap
    create(int width, int height)
    Creates a new 2D bitmap of the specified size.
    boolean
    get(int x, int y)
    Returns true is the specified element is set (1), false otherwise (0).
    boolean
    Returns true is the specified element is set (1), false otherwise (0).
    Returns the underlying 1D BitVector.
    int
    Returns the height of this BitMap.
    int
    Returns the width of this BitMap.
    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 for true, 0 for false).
    void
    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 for true, 0 for false).
    void
    Sets all elements to 1.
    byte[]
    Returns the contents of this bitmap as a one-dimensional byte 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
    Sets all elements to 0.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BitMap

      public BitMap(int width, int height)
      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 bitmap
      height - the height of the new bitmap
    • BitMap

      public BitMap(int width, int height, byte[] bytes)
      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. Passing null 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 bitmap
      height - the height of the new bitmap
      bytes - a byte array (null is allowed)
  • Method Details

    • getWidth

      public int getWidth()
      Returns the width of this BitMap.
      Returns:
      the width
    • getHeight

      public int getHeight()
      Returns the height of this BitMap.
      Returns:
      the height
    • get

      public boolean get(int x, int y)
      Returns true is the specified element is set (1), false otherwise (0).
      Parameters:
      x - the x-coordinate
      y - the y-coordinate
      Returns:
      as described
    • get

      public boolean get(Pnt2d.PntInt p)
      Returns true is the specified element is set (1), false otherwise (0).
      Parameters:
      p - the x/y-coordinate (point)
      Returns:
      as described
    • set

      public void set(int x, int y, boolean val)
      Sets the specified bit-element to the given boolean value (1 for true, 0 for false).
      Parameters:
      x - the x-coordinate
      y - the y-coordinate
      val - a boolean value
    • set

      public void set(Pnt2d.PntInt p, boolean val)
      Sets the specified bit-element to the given boolean value (1 for true, 0 for false).
      Parameters:
      p - the x/y-coordinate (point)
      val - a boolean value
    • set

      public void set(int x, int y)
      Sets the specified element (to bit-value 1).
      Parameters:
      x - the x-coordinate
      y - the y-coordinate
    • set

      public void set(Pnt2d.PntInt p)
      Sets the specified element (to bit-value 1).
      Parameters:
      p - the x/y-coordinate (point)
    • unset

      public void unset(int x, int y)
      Unsets the specified element (to bit-value 0).
      Parameters:
      x - the x-coordinate
      y - the y-coordinate
    • unset

      public void unset(Pnt2d.PntInt p)
      Unsets the specified element (to bit-value 0).
      Parameters:
      p - the x/y-coordinate (point)
    • setAll

      public void setAll()
      Sets all elements to 1.
    • unsetAll

      public void unsetAll()
      Sets all elements to 0.
    • getBitVector

      Returns the underlying 1D BitVector.
      Returns:
      the bit vector
    • toByteArray

      public byte[] toByteArray()
      Returns the contents of this bitmap as a one-dimensional byte 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

      public static BitMap create(int width, int height)
      Creates a new 2D bitmap of the specified size.
      Parameters:
      width - the width of the new bitmap
      height - the height of the new bitmap
      Returns:
      the new bitmap