Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose of the assignment is to write a Java class that can be called by a user interface program to unscramble images in the

The purpose of the assignment is to write a Java class that can be called by a user interface program to unscramble images in the Portable GreyMap (PGM) format. To do this you need to write an object called Transform that inherits from an interface and implements all methods in that interface. It also instantiates and calls methods in PictureLibrary.java, and is in turn called from a graphical user interface (GUI) class called ImageProgram.java. Both of these are supplied below.

Instructions:

Part One Create a new project P8 and a class called Transforms, without a main method. Note: You may want to leverage code from the R11 recitation. Download and import PictureLibrary.java Download and import ImageProgram.java Download and import ImageInterface.java Make the class implement the ImageInterface interface, as shown in the lab and discussed in class. Do NOT modify any of the given files, just import them into your P8 src folder

Part Two At the top of the class, declare the following instance (non-static) variables: An object of type PictureLibrary, set to null. An integer to store the image width, set to 0. An integer to store the image height, set to 0. A 2-dimensional array of integers to store the image data, not allocated. Name your variables whatever you want Create a constructor for the Transforms class as shown below that instantiates an object of type PictureLibrary into the associated class instance variable.

public Transforms(){

//Instantiate PictureLibrary object }

Part Three Implement the methods in the ImageInterface interface. All methods need to be created before your code will compile. The readImage method should call the readPGM method in the PictureLibrary object, passing the input file name, then it should call the getHeight, getWidth, and getData methods to fill in the class instance data defined above. The writeImage method should call the setData method in the Picture object with the image data, then call the writePGM method passing the output file name. The parameters and return types of the methods in PictureLibrary.java are not documented here, so you must look at the file to find them. The calls to readImage and writeImage should be wrapped in a try catch block as follows: try { // Calls to readPGM or writePGM and associated code here } catch (Exception e) { System.out.println(e.getMessage()); } Implement imageData by simply returning a copy of the image array. The remaining methods manipulate the image data in some way or another to restore an image that has been scrambled.

Part Four Make sure that all methods exist in your Transforms.java, even though some of the transformations may not do anything. You should now be able to run the main program in ImageProgram.java to read and write PGM files. If you are having trouble integrating with the provided files, check that your method names and parameters match. We have provided a test file called Cam.pgm that you can download to the P8 project directory (not into the /src or /bin subdirectories).

Part Five Implement the remaining methods as follows: Calling decode restores an image in which each pixel has had the upper four bits negated. To unscramble the image, your code should negate them again. You can use bitwise operators or the following algorithm: To get the upper bits, divide the pixel by 16. To get the lower bits, modulo the pixel by 16. The resulting upper and lower values should be in the range 0..15. Negate the upper bits as follows: upper = 15 - upper; Then put the bits back together by multiplying the upper bits by 16 and adding the result to lower. Here's an example for your testing: original pixel = 115 = 0b01110011 upper value = 115 / 16 = 7 = 0b0111 (upper four bits of original) lower value = 115 % 16 = 3 = 0b0011 (lower four bits of original) negate upper value = 15 - 7 = 8 = 0b1000 new pixel = (upper * 16) + lower = (8 * 16) + 3 = 131 = 0b10000011

Calling swap restores an image in which each pixel has been scrambled by exhanging the lower 2 bits with the upper 2 bits. To do this requires that your code do the same exchange to restore the original pixel. Don't modify the middle four bits. By far the easiest way to do this is to use the bitwise operators. Here's an example for your testing: original pixel = 114 = 0b01110010 upper two bits of original = 0b01110010 & 0b11000000 = 0b01000000 middle four bits of original = 0b01110010 & 0b00111100 = 0b00110000 lower two bits of original = 0b01110010 & 0b00000011 = 0b00000010 new pixel = (lower << 6) | middle | (upper >> 6)

Calling mirror reverses the image top to bottom. To reverse top to bottom, exchange the first row for the last row, the second row for the second to last row, and so on until the entire image is reversed.

Calling exchange swaps the image area defined by a rectangle with width 150 and height 300 starting at row index 10 and column index 10 for a rectangle of the same size starting at row index 10, and column index 310. NOTE: The maximum value of a pixel (PictureLibrary.MAXVAL) is 255, so only 8 bits are valid for each pixel. These are numbered bits 0-7, where bit 0 is equal to 1 and bit 7 is equal to 128. There are no negative values allowed.

Image interface

public interface ImageInterface {

// Read the image

public void readImage(String inFile);

// Write the image

public void writeImage(String outFile);

// Get image data

public int[][] imageData();

// Decode the pixels

public void decode();

// Swap the nibbles in each pixel

public void swap();

// Mirror the image corner to corner

public void mirror();

// Exchange two rectangles in image

public void exchange();

}

ImageProgram

import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.swing.*;

public class ImageProgram extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; // get rid of warning

// Program flags private boolean mIsInitialized; // GUI initialized? private boolean mIsDirty; // image modified?

// Menu items private JMenuItem mOpenFile; private JMenuItem mSaveFile; private JMenuItem mExitCommand; private JMenuItem mDecode; private JMenuItem mSwap; private JMenuItem mMirror; private JMenuItem mExchange; private JLabel mLabel; // Transforms object from student private ImageInterface mStudent;

// Array and static code are used to convert a gray scale to RGB private static int[] pgm2RGB; static { pgm2RGB = new int[256];

for (int i = 0; i < 256; i++) { pgm2RGB[i] = (255 << 24) | (i << 16) | (i << 8) | i; } }

Picture Library

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.Scanner;

public class PictureLibrary { // Maximum intensity public static final int MAXVAL = 255;

// Image data private int[][] image;

// Get image height public int getHeight() { return image.length; }

// Get image width public int getWidth() { return image[0].length; }

// Get image data public int[][] getData() { return image; } // Set image data public void setData(int[][] data) { image = data; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

Ten narrow topics from yhe broad topic finacial responsibility

Answered: 1 week ago