CIS 260 - Reading in and checking a binary (BMP) file
The objectives of this assignment are:
- handle command line arguments
- read a binary format file
- allocate memory dynamically
- create a function
- validate an image (BMP) file
- convert data from one format to another
Assignment
Your task for this assignment is to read in BMP files and validate them to see
if they are of a certain type and the data is sensible. The names of the BMP
files are to be passed in to the program via the command line.
It is important to remember that we are checking for one specific type of
BMP image file, so files that are valid, but which are a different version,
have compression, etc. will be declared invalid by this program.
You should have at least two functions when you are done: main and isValidFile. There
are three test cases below. The files red4.bmp and tictactoe.bmp should validate.
The file badbmp.bmp should trigger almost every error message.
method: main
- if there is less than one argument, display an error message and exit with an error exit code
- for each command line argument do the following:
- display a message with the file name stating that it is being checked
- get the size of the file in bytes
- if the file is less than 54 bytes in length, display an error message and move on to the next file
- try to open the file for binary reading
- display an error message and go to the next file if this one failed to open
- create a byte array the size of the file to use as a buffer
- read the entire file into the buffer
- close the BMP file
- call your isValidFile function, sending it the buffer and the file length
- depending on what isValidFile returns, display a message stating whether the file passed its validation check
- return a success exit status
method: isValidFile
- returns a boolean
- parameters should be a byte array for the image file buffer and an int for the file length
- create a boolean variable to keep track of whether the file is OK and initialize it to true
- check the following locations in the buffer:
- if location 0x00 is not 0x42 or location 0x01 is not 0x4d, then display "Signature invalid" and the file is not OK
- if the int at location 0x02 is not the same as the file length, then display "File length invalid" and the file is not OK
- if the int at location 0x06 is not 0, then display "Reserved area invalid" and the file is not OK
- if the int at location 0x0a is not 54, then display "Offset invalid" and the file is not OK
- if the int at location 0x0e is not 40, then display "DIB header size invalid" and the file is not OK
- let the width be the int at location 0x12
- let the height be the int at location 0x16
- let the padding per row be the width modulus 4
- let the total file size be: height * (width * 3 + padding per row) + 54
- if the total file size is not equal to the file length, then display "Width and height don't match file size" and the file is not OK
- if location 0x1a is not 1 or location 0x1b is not 0, then display "Invalid number of planes" and the file is not OK
- if location 0x1c is not 24 or location 0x1d is not 0, then display "Invalid bits per pixel" and the file is not OK
- if the int at location 0x1e is not 0, then display "Compression scheme invalid" and the file is not OK
- if the int at location 0x22 is not equal to the file length - 54, then display "Image size including padding invalid" and the file is not OK
- do not check locations 0x26 - 0x2d (horizontal and vertical resolution)
- if the int at location 0x2e is not 0, then display "Number of colors invalid" and the file is not OK
- if the int at location 0x32 is not 0, then display "Number of important colors invalid" and the file is not OK
- return the boolean value for whether the file is OK
Grading rubric
Note: Program must be able to be compiled and run.
- 5 pts: style conventions followed (no tabs, proper indentation, documentation comments)
- 5 pts: command line arguments are handled properly
- 5 pts: files are read in properly
- 5 pts: dynamic memory is allocated properly
- 5 pts: main works properly
- 5 pts: isValidFile is called and used properly
- 20 pts: checks in isValidFile work properly and function returns correct value
Alternate option
The first option listed above is to read the whole file into a byte array
in the main function, and then pass that array and the file length to
isValidFile. An alternative is to open the file as a RandomAccessFile
in the main function and then just pass the file reference and file length
to isValidFile. You get to choose which option you prefer. Either way,
the isValidFile function must return true if the file is valid and false
if it is not valid.
Sample output
>java CheckBMP
Usage: java CheckBMP filelist
>java CheckBMP *.bmp
Checking file 33.bmp
File checks out OK
Checking file airplane.bmp
File checks out OK
Checking file badbmp.bmp
Signature invalid
File length invalid
Reserved area invalid
Offset invalid
DIB header size invalid
Width and height don't match file size
Invalid number of planes
Invalid bits per pixel
Compression scheme invalid
Image size including padding invalid
Number of colors invalid
Number of important colors invalid
File did not pass validation
Checking file duck.bmp
File checks out OK
Checking file red4.bmp
File checks out OK
Checking file tictactoe.bmp
File checks out OK