Lab 1: File Input/Output
Overview
In this assignment, you will read and write images in two custom formats.
Pre-Lab (DUE BEFORE LAB - see due date in Canvas)
As part of the pre-lab activity you will see how to use GitHub Classroom to start your lab assignment and submit it when you are finished. When watching the video below, consider the following.
- How to start a new project from your GitHub repository
- How to commit changes to your local repo
- How to push changes from your local repo to GitHub
- Watch [5:24] [link]
The project is cloned from the GitHub repo to your local machine at the three minute mark in the video. You will likely need to authorize IntelliJ to access your GitHub account. Hopefully the process is simple enough, but you can see screenshots for what to do in Figures 13-16 of this page.
What Must Be Done by the Pre-lab Due Date?
- Ensure you have a GitHub account
- Accept your instructor's invitation to the lab 1 assignment in Canvas
- Associate your GitHub account with your name in GitHub Classroom
- Clone the project, openning it in IntelliJ
- Rename the
usernamepackage to your username - Commit and push the changes
Assignment
Your program must accept the name of the input image file and the name of the output image file. The file extention determines whether you should read/write the file in text or binary format. The program should read the input file specified by the user and write it to the output file specified by the user.
Details
.msoe Text Image File Format
The custom .msoe file format is a text based file format for storing images. It is
designed to be easy to load and save, but results in comparatively large file sizes.
The contents of a very small image of a white-on-black + symbol is shown below:
MSOE
3 3
#000000 #FFFFFF #000000
#FFFFFF #FFFFFF #FFFFFF
#000000 #FFFFFF #000000
- The first line specifies that the file contains an
.msoeimage. - The second line specifies, in order, the width and height of the image.
- Each remaining line specifies the colors of the pixels for one row of the image.
- Line 3 specifies pixel colors for the first row in the image.
- Line 4 specifies pixel colors for the second row in the image.
- Line k specifies pixel colors for the k-2 row in the image.
Your program should be able to load images with an .msoe extension located
in the images folder of the repository.
.bmsoe Binary Image File Format
The custom .bmsoe file format is a binary based file format for storing
images. It is designed to be easy to load and save as well as produce smaller
file sizes than the .msoe format.
The file consists of a sequence of integer values representing the width, height, and pixel values for the image. Each pixel is stored as a single ARGB integer value representing the alpha, red, green, and blue components of the pixel.
ImageIO Class
You must implement the following private class methods:
readMSOE(Path path)— Reads in the specified image file in.msoeformat and returns amocked.Imageobject containing the image.writeMSOE(Path path, Image image)— Writes the specified image to the specified path using the.msoefile format.readBMSOE(Path path)— Reads in the specified image file in.bmsoeformat and returns amocked.Imageobject containing the image.writeBMSOE(Path path, Image image)— Writes the specified image to the specified path using the.bmsoefile format.
You must implement an ImageIO class with the following public class methods:
read(Path path)— Reads in the specified image file and returns amocked.Imageobject containing the image.write(Path path, Image image)— Writes the specified image to the specified path.
Based on the file extension of the Path object, each of these methods will call
the appropriate private method to do the work. If the path specifies an extension other
than msoe or bmsoe, the method must throw an IllegalArgumentException.
mocked Package
You are provided with a mocked.Image class implementation containing the following methods:
double getWidth()— returns the width of the image.double getHeight()— returns the height of the image.mocked.PixelReader getPixelReader()— returns an object that implements thePixelReaderinterface.
The mocked.PixelReader interface provides one method: int getArgb(int x, int y) which
returns the 32-bit integer value of the color of the pixel located at x, y.
You are also provided with a mocked.WritableImage class implementation which
extends mocked.Image and contains:
WritableImage(int width, int height)— creates a writable image of the specified size.mocked.PixelWriter getPixelWriter()— returns an object that implements thePixelWriterinterface.
The mocked.PixelWriter interface provides one method: int setArgb(int x, int y, int argb)
which sets the 32-bit integer value of the color of the pixel located at x, y.
In your readMSOE() and readBMSOE() methods you will need to read the
width and height of the image, and then create a WritableImage object
so that you can write the data read from the file into an object in memory.
As you read the pixel values, you will need to use a PixelWriter to store
those values in the WritableImage object that you created.
In your writeMSOE() and writeBMSOE() methods you will use a PixelReader
to get the pixel values to write to the output file.
Note that the IOImage class contains a method, hexColorToARGB(String color), that
returns the argb value of a pixel as a single integer. This can be used by your
readMSOE() method before setting the Argb value of the pixels in the PixelWriter.
It also contains argbToHexColor(int argb) that returns a six-digit hex color,
which you may also find useful.
ImageCopy Class
You must also implement the ImageCopy class that will contain your main program.
Exception Handling
If any problems are encountered with reading the input files or writing the output file, the program should display a useful error message to the console and terminate gracefully. The program should not crash or display any exceptions.
Just For Fun
Ambitious students may wish to:
- Read and write more common image formats.
Acknowledgement
This laboratory assignment, developed by Dr. Chris Taylor.