CIS 260 - Reading in and checking a binary (BMP) file

The objectives of this assignment are:

  1. handle command line arguments
  2. read a binary format file
  3. allocate memory dynamically
  4. create a function
  5. validate an image (BMP) file
  6. 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

  1. if there is less than one argument, display an error message and exit with an error exit code
  2. for each command line argument do the following:
    1. display a message with the file name stating that it is being checked
    2. get the size of the file in bytes
    3. if the file is less than 54 bytes in length, display an error message and move on to the next file
    4. try to open the file for binary reading
    5. display an error message and go to the next file if this one failed to open
    6. create a byte array the size of the file to use as a buffer
    7. read the entire file into the buffer
    8. close the BMP file
    9. call your isValidFile function, sending it the buffer and the file length
    10. depending on what isValidFile returns, display a message stating whether the file passed its validation check
  3. return a success exit status

method: isValidFile

  1. returns a boolean
  2. parameters should be a byte array for the image file buffer and an int for the file length
  3. create a boolean variable to keep track of whether the file is OK and initialize it to true
  4. check the following locations in the buffer:
    1. if location 0x00 is not 0x42 or location 0x01 is not 0x4d, then display "Signature invalid" and the file is not OK
    2. 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
    3. if the int at location 0x06 is not 0, then display "Reserved area invalid" and the file is not OK
    4. if the int at location 0x0a is not 54, then display "Offset invalid" and the file is not OK
    5. if the int at location 0x0e is not 40, then display "DIB header size invalid" and the file is not OK
    6. let the width be the int at location 0x12
    7. let the height be the int at location 0x16
    8. let the padding per row be the width modulus 4
    9. let the total file size be: height * (width * 3 + padding per row) + 54
    10. 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
    11. if location 0x1a is not 1 or location 0x1b is not 0, then display "Invalid number of planes" and the file is not OK
    12. if location 0x1c is not 24 or location 0x1d is not 0, then display "Invalid bits per pixel" and the file is not OK
    13. if the int at location 0x1e is not 0, then display "Compression scheme invalid" and the file is not OK
    14. 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
    15. do not check locations 0x26 - 0x2d (horizontal and vertical resolution)
    16. if the int at location 0x2e is not 0, then display "Number of colors invalid" and the file is not OK
    17. if the int at location 0x32 is not 0, then display "Number of important colors invalid" and the file is not OK
  5. return the boolean value for whether the file is OK

Grading rubric

Note: Program must be able to be compiled and run.

  1. 5 pts: style conventions followed (no tabs, proper indentation, documentation comments)
  2. 5 pts: command line arguments are handled properly
  3. 5 pts: files are read in properly
  4. 5 pts: dynamic memory is allocated properly
  5. 5 pts: main works properly
  6. 5 pts: isValidFile is called and used properly
  7. 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