MSOE 3 3 #000000FF #FFFFFFFF #000000FF #FFFFFFFF #FFFFFFFF #FFFFFFFF #000000FF #FFFFFFFF #000000FF* 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 remain 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. * The pixel color is a hexidecimal number where each pair of digits represents (from left to right) the red, green, blue, and alpha/opacity * Each pair of hexidecimal digits represents an integer value in the range [0, 255] * The alpha/opacity value specifies how transparent the pixel is (`00` completely transparent, `FF` not at all transparent) ### `.bmsoe` File Format The custom `.bmsoe` file format is a binary based file format for storing images. File sizes will be smaller than `.msoe` for the same image, but the format does not do any image compression so the files will be larger than more common formats. The file consists of a number of bytes. * The file begins with the bytes having the ASCII values of the following characters: `B`, `M`, `S`, `O`, and `E`. * Next in the file are two integers containing the **width** and **height** of the image. * The remainder of the file consists of the rows, starting with the top-most row. * Each row consists of a number of bytes. The number of bytes is evenly divisible by 16. * The bytes in the row specify all of the pixels in the row and, potentially, some padding bytes (to ensure the divisible-by-16 rule). * Each pixel is specified with four bytes, one byte for each of the four pixel components: red, green, blue, alpha/opacity. ### Loading and Saving Images The completed program must support loading and saving `.bmsoe`, `.gif`, `.jpg`, `.png`, and `.msoe` image file formats. The load button will open a `FileChooser` dialog that will allow the user to select the file to be loaded (`showOpenDialog()`). The program will use the extension to determine the format of the file and load the image (provided the extension type is supported). Pressing the reload button will reload the last loaded image. The save button will open a `FileChooser` dialog box that will allow the user to select the destination folder and type in the filename and extension (`showSaveDialog()`). The program will then write the image to that file using the file format that matches the file extension. If an unsupported file extension is entered, the program will display an error message without attempting to save the image. You must create an `ImageIO` class2) that is responsible for loading and saving images. The class must have the following public class methods: * **`read(File file)`** — Reads in the specified image file and returns a `javafx.scene.image.Image` object containing the image. * **`write(Image image, File file)`** — Writes the specified image to the specified file. If the extension on the file passed to either of these methods is `.msoe` or `.bmsoe`, then the appropriate private class method below is called to do the actual work. * **`readMSOE(File file)`** — Reads an image file in `.msoe` format. * **`writeMSOE(Image image, File file)`** — Writes an image file in `.msoe` format. * **`readBMSOE(File file)`** — Reads an image file in `.bmsoe` format. * **`writeBMSOE(Image image, File file)`** — Writes an image file in `.bmsoe` format. You are encouraged to use the [`javax.imageio.ImageIO`](http://docs.oracle.com/javase/8/docs/api/javax/imageio/ImageIO.html) class to read and write files in the `.gif`, `.jpg`, and `.png` formats. The [`SwingFXUtils`](https://docs.oracle.com/javase/8/javafx/api/javafx/embed/swing/SwingFXUtils.html) class can be used to convert from `Image` to `BufferedImage`. You must implement your own code to read and write `.msoe` and `.bmsoe` files. ### Image Transformations Your program will support a number of image transformations. #### Grayscale This transformation converts the image into a grayscale image. If the image is already grayscale, the transformation has no affect. In this transformation, the RGB components of each pixel are replaced with a single value. The replacement value can be calculated as: *gray = 0.2126R + 0.7152G + 0.0722B* [more info](http://en.wikipedia.org/wiki/Grayscale). #### Negative This transformation converts the image into a photo negative of the original image. In this transform, each RGB component is replaced with *1.0 - original value*. Thus, an RGB value of (1.0, 1.0, 0.0) would become (0.0, 0.0, 1.0). [more info](http://en.wikipedia.org/wiki/Negative_%28photography%29). #### Red Only This transformation acts as a red filter. The green and blue components of each pixel are set to zero. #### Red-Gray This transformation is based on an experiment demonstrated by Edwin Land, founder of Polaroid, in 1959 [more info](http://t-a-y-l-o-r.com/land/). The experiment was based on the Retinex Theory of Color Vision. [This page](http://neuronresearch.net/vision/files/retinex.htm) has a nice explanation of the experiment and the Retinex theory. The transform is quite simple: perform a Grayscale transformation on the pixels in alternating rows and perform a Red Only transformation on the pixels in the other rows3). ### Filter Kernel Window When clicked, the **Show Filter** button reveals a second window (shown below the main window in Figure 1), and changes the text on the button to **Hide Filter**. When **Hide Filter** is clicked, the second window is hidden, and the text on the button changes back to **Show Filter**. The Filter Kernel window contains a 3 x 3 grid4) of filter weights. The text field in the lower left can hold a value to be used as a divisor on all of the filter weights5). When clicked, the three buttons on the right clear the divisor field and change the filter weights as follows:
Blur Sharpen Edge 0 1 0 0 -1 0 0 -1 0 1 5 1 -1 5 -1 -1 4 -1 0 1 0 0 -1 0 0 -1 0The **Apply** button applies the filter kernel to the image. At a minimum, the button need only apply the filter kernel if the sum of the filter kernel weights is a positive value. For example, making the **Apply** button functional for the filter kernel weights provided by the **Edge** button is optional since the sum of the weights is zero (-1-1+4-1-1). The following code applies the **Blur** filter kernel to a [`BufferedImage`](https://docs.oracle.com/javase/8/docs/api/java/awt/image/BufferedImage.html) called `original` and stores the new `BufferedImage` in `result`: ``` float[] kernel = { 0F, 1/9F, 0F, 1/9F, 5/9F, 1/9F, 0F, 1/9F, 0F}; BufferedImageOp op = new ConvolveOp(new Kernel(width, height, kernel)); BufferedImage result = op.filter(original, null); ``` You may find the [`SwingFXUtils`](https://docs.oracle.com/javafx/2/api/javafx/embed/swing/SwingFXUtils.html) class helpful for integrating the above code in your application. If the **Apply** button is pressed when an unsupported set of weights are entered in the window, an exception should occur (and be handled in a way consistent with the exception handling requirements listed below). ### Class Structure In addition to the `ImageIO` class described above, you must implement the `Transformable` interface. The `Transformable` interface is a *functional interface*. A functional interface has a single method, and can therefore be implemented with a lambda expression. The method, named `tranform`, must accept three arguments: the x and y location of the pixel and its color. The method must return the color for the pixel after the applying the transformation. You must provide implementations of the `Transformable` interface for the **grayscale**, **red**, **redGray**, and **negative** image transformations. You must implement the following method in the class that handles the button events that performs the specified transformation: ``` private static Image transformImage(Image image, Transformable transform) { // ... } ``` All of your classes should be placed in a package whose name matches your MSOE username. The rest of the class design is up to you. ### Exception Handling There are a number of situations that could cause your program to throw an exception. For example, if the file is not found, cannot be opened, or contains incorrectly formatted data, it is likely that an exception will be thrown. In these cases, the program should display an useful message and log the error. Your program should write to the log file `Lab8.txt`. ### Responding to an Event other than an `ActionEvent` You must implement a feature that responds to an event other than an `ActionEvent`. For example, you could: * allow the user to draw on the image with the mouse * display the x, y coordinates of the location of the mouse on the image * display the color of the pixel at the location of the mouse on the image * play a sound when clicking on a pixel with a particular color * flip back and forth between the image and its negative whenever the window is minimized * select a region of the image ## Creating an Executable `.jar` File ## Just For Fun There are many additional enhancements that could be built on the required functionality. You are encouraged to enhance this application using your creativity. A number of suggested enhancements are included below; however, you should not feel limited to these suggestions. * Add a menu to the main window to replace (or in addition to) the buttons * Apply a transform to only a selected region of the image * Display the original and transformed images side-by-side * Toggle between original and transformed images when mouse button is pressed on the image * Create a meme generator by adding styled text on the image * Tonal adjustment (e.g., adding a red hue to the image underneath the mouse) * Provide undo functionality * Implement additional transformations, e.g. * Brighten * Darken * Decrease color saturation * Increase color saturation * Apply additional filters like the **Edge** filter [see here](http://lodev.org/cgtutor/filtering.html) ## Lab Deliverables > See your professor's instructions for details on submission guidelines and due dates. > * Dr. Taylor's students: See below > * All other students should refer to Blackboard > >If you have any questions, consult your instructor. ## Acknowledgment This laboratory assignment was developed by [Dr. Chris Taylor](/taylor/) and [Dr. Walter Schilling](http://www.waltschilling.us).
2) This [`ImageIO`](ImageIO.java) class is different than the [`javax.imageio.ImageIO`](http://docs.oracle.com/javase/8/docs/api/javax/imageio/ImageIO.html) class.
3) This is a slight simplification from the actual transformation. You may chose to implement the actual transformation, if you choose to (you'll need to look it up). Be sure to indicate with a comment if you choose to do this.
4) You may choose to make a 5 x 5, 7 x 7, or 9 x 9 grid instead.
5) For example, if **5** were entered into that field, then the filter weights should be **0.20** instead of **1** and **1** instead of **5**. If no value is entered, then the sum of all the filter weights should be used for the divisor. In the case of the example shown, the divisor should be **9** (0+1+0+1+5+1+0+1+0).