Lab 1: File Input/Output
Overview
In this assignment, you will read and write images in two custom formats.
Commit 1 - Rename package
Due end of day Tuesday Week 1 (first day of class)
In order to complete commit 1, you will will learn 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 for Commit 1?
- 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
username
package to your username - Commit and push the changes
Commit 2 - ImageIO method stubs
Due end of class Wednesday Week 1
Here you will need to create method stubs for the ImageIO
class.
ImageIO
Class
The class has the following private class methods:
readMSOE(Path path)
— Reads in the specified image file in.msoe
format and returns amocked.Image
object containing the image.writeMSOE(Path path, Image image)
— Writes the specified image to the specified path using the.msoe
file format.readBMSOE(Path path)
— Reads in the specified image file in.bmsoe
format and returns amocked.Image
object containing the image.writeBMSOE(Path path, Image image)
— Writes the specified image to the specified path using the.bmsoe
file format.
The ImageIO
class has the following public class methods:
read(Path path)
— Reads in the specified image file and returns amocked.Image
object containing the image.write(Path path, Image image)
— Writes the specified image to the specified path.
The Image
reference type is supplied in the mocked
package. When creating the stubs,
you should import mocked.Image
so that the Image
references in your method stubs
are using mocked.Image
.
Commit 3 - ImageCopy implementation
End of class Wednesday Week 1
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.
The ImageCopy
class should contain your main()
method which will make use
of the ImageIO.read()
and ImageIO.write()
methods to do much of the work.
Your program should be able to load images with an .msoe
or .bmsoe
extension
located in the images
folder of the repository.
The read()
and write()
methods will throw an IllegalArgumentException
if
an invalid file type is specified in the Path
passed the methods. Your program
does not need to check for a valid file extension (that will be done in ImageIO
,
but it does need to anticpate that the calls to read()
and write()
may
produce exceptions.
Note: if the file being read in has more data than expected (based on the width and
height), it is considered invalid. You can use the available()
method from the
InputStream
class to determine if there is more data available.
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. All messages that respond to exceptions must be written to
System.err
instead of System.out
. The program should not crash or display any exceptions.
Commit 4 - ImageIO read() and readMSOE() implemented
End of class Wednesday Week 1
Based on the file extension of the Path
object, the public read()
method 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
.
.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
.msoe
image. - 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 readMSOE()
method will need to read this data from a text file and use it
to create a mocked.Image
object. Note that a mocked.WritableImage
is a
mocked.Image
. To implemented readMSOE()
, you'll need to read the width
and height from the text file, and then create a WriteableImage
object.
You will then read each pixel color from the text file and write that data
into the WritableImage
object in memory.
Notes:
- The
IOImage
class contains a method,hexColorToARGB(String color)
, that returns theargb
value of a pixel as a single integer. This can be used by yourreadMSOE()
method before setting the Argb value of the pixels in thePixelWriter
. It also containsargbToHexColor(int argb)
that returns a six-digit hex color, which you may also find useful. - The
WriteableImage
class does not have a way to directly update the color of a pixel, so you'll need to study the contents of themocked
package to figure out how to make this happen.
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 thePixelReader
interface.
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 thePixelWriter
interface.
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.
Commit 5 - ImageIO write and writeMSOE() implemented
End of class Thursday Week 1
Based on the file extension of the Path
object, the public write()
method 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
.
For this commit you will need to implement the write()
and writeMSOE()
methods that are similar to the read()
and readMSOE()
methods; however,
instead of reading from a file and writing to an object in memory, you must
read from an object in memory and write to a file.
Commit 6 - Lab completed
11pm Monday Week 2
This is the final commit for this lab assignment where you will need to complete
any remaining parts of the assignment. In particular, you will need to add
support for reading and writing .bmsoe
images.
.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. The pixel values are written from
right to left by row - the same order used by .msoe
.
Note:
- There are several images files contained in the
images
folder for the project. Files with names beginning withbad
are examples of corrupt image files that should not load correctly.
Just For Fun
Ambitious students may wish to:
- Read and write more common image formats.
Acknowledgement
This laboratory assignment, developed by Dr. Chris Taylor.