Package ch.heigvd.dai.utilities
Class BmpFile
java.lang.Object
ch.heigvd.dai.utilities.BmpFile
Implements the required logic to parse a bitmap file and modify its content.
- Author:
- Pedro Alves da Silva, Gonçalo Carvalheiro Heleno
-
Field Summary
Modifier and TypeFieldDescriptionprivate int
private final String
private int
private boolean
private int
private byte[]
private int
private static final int
private static final int
private static final int
private static final int
static final int
static final int
static final int
static final int
private static final String[]
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
Returns the number of bits belonging to the hidden message per data byte.int
Returns the hidden message's length.byte[]
Returns the pixel array that was read from the BMP file.boolean
Returns whether this file has a hidden message inside.private boolean
isValidMagic
(byte[] magicBytes) Checks whether the provided magic bytes are valid for a bitmap file.private void
readBitmapHeader
(BufferedInputStream fileBuffer) Reads and checks the file's header.private void
readPixelArray
(BufferedInputStream fileBuffer) Reads the bitmap file's pixel array.void
Dumps the pixel array into the provided output file.void
setData
(byte[] data, int messageLength, int bitsPerByte) Changes the pixel array data, hidden message length, and number of bits per data byte.
-
Field Details
-
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
-
-
Constructor Details
-
BmpFile
Parses and splits a bitmap file into useful data.- Parameters:
bmpFilePath
- aString
with the path to a bitmap file- Throws:
BmpFileException
- if the bitmap file is not validIOException
- if there is an IO error when handling the bitmap file
-
-
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 abyte[]
messageLength
- new message lengthbitsPerByte
- 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
Dumps the pixel array into the provided output file.Note that the original input file must still exist.
- Parameters:
outputFilePath
- aString
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
- abyte[]
containing the magic bytes- Returns:
true
if magic is valid,false
if not
-
readBitmapHeader
Reads and checks the file's header.- Parameters:
fileBuffer
- aBufferedInputStream
to read from- Throws:
BmpFileException
- if the function fails to read the file's header
-
readPixelArray
Reads the bitmap file's pixel array.- Parameters:
fileBuffer
- aBufferedInputStream
to read from- Throws:
BmpFileException
- if the function fails to read the pixel array
-