Class BmpFile

java.lang.Object
ch.heigvd.dai.utilities.BmpFile

public class BmpFile extends Object
Implements the required logic to parse a bitmap file and modify its content.
Author:
Pedro Alves da Silva, Gonçalo Carvalheiro Heleno
  • Field Details

    • VALID_MAGICS

      private static final String[] VALID_MAGICS
    • MIN_MESSAGE_LENGTH

      public static final int MIN_MESSAGE_LENGTH
      See Also:
    • MAX_MESSAGE_LENGTH

      public static final int MAX_MESSAGE_LENGTH
      See Also:
    • MIN_BITS_PER_BYTE

      public static final int MIN_BITS_PER_BYTE
      See Also:
    • MAX_BITS_PER_BYTE

      public static final int MAX_BITS_PER_BYTE
      See Also:
    • BITS_PER_BYTE_SHIFT

      private static final int BITS_PER_BYTE_SHIFT
      See Also:
    • MAGIC_BYTES_LEN

      private static final int MAGIC_BYTES_LEN
      See Also:
    • HEADER_FIELDS_LEN

      private static final int HEADER_FIELDS_LEN
      See Also:
    • HEADER_LEN

      private static final int HEADER_LEN
      See Also:
    • _hasMessage

      private boolean _hasMessage
    • _pixelArray

      private byte[] _pixelArray
    • _fileSize

      private int _fileSize
    • _pixelArrayOffset

      private int _pixelArrayOffset
    • _messageLength

      private int _messageLength
    • _bitsPerByte

      private int _bitsPerByte
    • _bmpFilePath

      private final String _bmpFilePath
  • Constructor Details

  • Method Details

    • hasMessage

      public boolean hasMessage()
      Returns whether this file has a hidden message inside.
      Returns:
      true if a message is present, false if not
    • getMessageLength

      public int getMessageLength()
      Returns the hidden message's length.
      Returns:
      Hidden message's length if one is present, or 0 if no message is hidden
    • getBitsPerByte

      public int getBitsPerByte()
      Returns the number of bits belonging to the hidden message per data byte.
      Returns:
      an int with the number of bits per data byte
    • setData

      public void setData(byte[] data, int messageLength, int bitsPerByte)
      Changes the pixel array data, hidden message length, and number of bits per data byte.
      Parameters:
      data - new pixel array as a byte[]
      messageLength - new message length
      bitsPerByte - number of bits per data byte
    • getPixelArray

      public byte[] getPixelArray()
      Returns the pixel array that was read from the BMP file.

      This function returns a copy of the pixel array of the instance.

      Returns:
      a byte[] with the pixel array
    • saveFile

      public void saveFile(String outputFilePath) throws IOException
      Dumps the pixel array into the provided output file.

      Note that the original input file must still exist.

      Parameters:
      outputFilePath - a String with the path to the output file which must not be the same file as the input file
      Throws:
      IOException - if an IO error occurs with either the input or output file
    • isValidMagic

      private boolean isValidMagic(byte[] magicBytes)
      Checks whether the provided magic bytes are valid for a bitmap file.
      Parameters:
      magicBytes - a byte[] containing the magic bytes
      Returns:
      true if magic is valid, false if not
    • readBitmapHeader

      private void readBitmapHeader(BufferedInputStream fileBuffer) throws BmpFileException
      Reads and checks the file's header.
      Parameters:
      fileBuffer - a BufferedInputStream to read from
      Throws:
      BmpFileException - if the function fails to read the file's header
    • readPixelArray

      private void readPixelArray(BufferedInputStream fileBuffer) throws BmpFileException
      Reads the bitmap file's pixel array.
      Parameters:
      fileBuffer - a BufferedInputStream to read from
      Throws:
      BmpFileException - if the function fails to read the pixel array