MP4: 2D

As we have learned previously, computers are frequently used to transform and analyze data. And an increasing amount of that data is multi-dimensional: including both photo and video data. People are taking a huge amount of photos and video, and processing that data is almost entirely done on or by computers. For MP4 you’ll be working with two-dimensional photo pixel data as you complete the server part of a simple photo editing application.

MP4 is due Friday 3/9/2018 @ 5PM. To receive full credit, you must submit by this deadline. In addition, 10% of your grade on MP4 is for submitting code that earns at least 50 points by Monday 3/5/2018 @ 5PM.

As usual, late submissions will be subject to the MP late submission policy.

1. Learning Objectives

The purpose of MP4 is to continue introducing you to data transformation. You’ll begin to learn how to:

  1. implement various image transformations by performing transformations on 2D arrays

  2. avoid writing repetitive code by identifying similarities between various transformations

  3. work with existing object classes

We’ll also continue to reinforce the learning objectives from previous MPs (0, 1, 2, and 3).

2. Assignment Structure

Like MP3, MP4 consists of a simple client-server system. The HTML, CSS, and JavaScript for the client are provided for you. But it relies on your backend to work properly.

Your job is to complete the required transformations in These make use of a pixel class defined in These functions are called by the web server that is set up using

As always, you may find our official MP4 online documentation helpful in understanding what each function and class is supposed to do.

2.1. Obtaining MP4

Use this GitHub Classroom invitation link to fork your copy of MP4. Once your repository has been created, import it into IntelliJ following our assignment Git workflow guide.

2.2. Your Goal

At this point you should be familiar with the requirements from previous MPs. See the grading description below.

2.3. Running MP4

To run MP4, follow the instructions from MP3.

At first, nothing will work because you have not implemented any of the required transformations yet. Our web interface also uses some new-ish browser features, and of course Internet Explorer hasn’t implemented all of them yet. We have confirmed that Chrome, Firefox, and Safari work on both Mac and Windows 1.

3. Approaching MP4

All of our previous advice about how to approach the CS 125 MPs still applies. But MP4 has some unique features that are designed to continue your development as computer scientists and software developers.

3.1. Don’t Repeat Yourself (DRY)

Whenever you write software, you should try to adhere to the DRY principle: don’t repeat yourself.

What does this mean? It means that you should try to avoid writing duplicative code—multiple functions (or parts of the same function) that perform identical tasks.

Consider the fact that any computer program could be written as one giant function and with no subroutines at all 2. But the result would be awful, since any time the program needed to repeat a task it would have to repeat the code needed to accomplish that task. And any time you fixed a bug in one part of the code, you’d have to find every other identical part and fix the bug there as well. Code like this quickly becomes impossible to understand, test, debug, and improve—and will also make it hard to keep a job.

At first glance, MP4 may seem daunting. You have 22 functions to write! And yes, you can complete MP4 by writing 22 separate independent functions.

But there is a much smarter way to approach MP4. As a hint, the solution set consists of only six real functions—with the rest just calling into this much smaller set of helper functions. The reason is that many of the transformations perform similar operations. For example, a shift down is similar to a shift up, as well as a shift left and a shift right. So rather than writing four separate extremely-similar functions, I can write a slightly more complicate generic shift function and have it called by all of the others.

There is a balance here that requires practice to get right. Trying to reduce the needs of 22 different functions to one single method produces overly-complicate and brittle code. But writing them all separately misses opportunities to harness common subroutines. You’ll get better at this with practice, and MP4 is a good chance to get some. So do not implement 22 separate transformation functions, but also do not attempt to implement one function that does everything. There are some natural groupings that you will probably be able to identify.

3.2. Understand Your Coordinate System

If you are used to working with the coordinate plane in mathematics, the canvas coordinate system can take some getting used to. In particular:

  1. (0, 0) is at the top left, not the bottom left.

  2. Increasing Y values move the image down, not up—keep this in mind when implementing shiftUp and shiftDown.

  3. Increasing X values move the image to the right, which matches the mathematical coordinate system.

Another important pitfall is that the that way that two-dimensional arrays in Java are initialized does not match up with this coordinate system. So, for example, this static array:

int[][] myArray = {
  { 1, 2 },
  { 3, 4 }

corresponds to this image array:





We suggest that you do not try to interpret the statically initialized arrays in the test suite directly. Instead, use the output from RGBAPixel helper methods, which is correctly formatted.

3.2.1. Centering

Understanding the coordinate system is also important when centering the image around (0, 0), which you need to do to implement the rotate, flip, and resize transformations. This is probably one of the trickier parts of MP4, so think it through carefully.

It is helpful to work some simple examples. For example, consider vertically flipping a 2x2 array. In our coordinate system, the coordinate values of the pixels in the array would be:

(0, 0)

(1, 0)

(0, 1)

(1, 1)

Note that these are the coordinate values, not the pixel contents. In order to flip the array properly, we need to adjust the coordinate values as follows:

(-0.5, -0.5)

(0.5, -0.5)

(-0.5, 0.5)

(0.5, 0.5)

At this point I can swap either the X or Y values and achieve either a horizontal or vertical flip around the origin. There is only one problem—Java can’t use double types as array indices. So we need to do this transformation on a pixel-by-pixel basis. Roughly, here is the approach. For each pixel in the original image:

  1. Center the pixel

  2. Determine how to transform it to a new location in the transformed image

  3. Undo the centering transformation

  4. Move data from the original image to the transformed image

Once you have a centering procedure that works, you can use it for the rotations, flips, expands, and shrinks. But this is one of the tougher parts of the assignment, so you might want to start with the parts that don’t require centering: position shifts, color shifts, green screen and the mystery function.

3.2.2. Shrinking and expanding

As a final note about coordinates, please consider carefully how to implement the shrink and expand transformations. Specifically, if I start with this 2x6 array (with pixel values shown):













and expand it horizontally by a factor of 3, this is the correct result:













But it is easy to get this instead:













You will want to think about this carefully. As a hint, instead of starting with the original array and trying to figure out where each pixel goes in the transformed array, you may want to start with the transformed array and calculate where each pixel should come from. Also keep in mind that simply casting a double to an integer does not round the value properly. So (int) doubleValue != Math.round(doubleValue).

Finally, note that shrinking is not tested by the test suite. You can feel free to implement it to get your web interface to work like the solution, but it will not affect your score.

3.2.3. Testing diff helper

You will notice that the RGBAPixel class defines a not very helpful static method to show the difference between two photo arrays. You should feel free to improve this method so that it is more useful during your debugging.

3.3. Getting Help

The course staff is ready and willing to help you every step of the way! Please come to office hours, or post on the forum when you need help. You should also feel free to help each other, as long as you do not violate the academic integrity requirements.

3.4. How All Of MP4 Works

The video above is optional, but may interest those that are curious about how a modern web application works. It walks through most of what happens from the moment you click one of the image transformation buttons, to when transformed data returns from the server.

4. Grading

MP4 is worth 100 points total, broken down as follows:

  1. 80 points:

    • 20 points for completing the position shift transformations

    • 20 points for completing the rotation and flip transformations

    • 10 points for completing the color shift transformations

    • 15 points for completing the expand transformations

    • 10 points for completing the green screen transformation

    • 5 points for completing a mystery transformation

  2. 10 points for no checkstyle violations

  3. 10 points for submitting code that earns at least 50 points before Monday 3/5/2018 @ 5PM.

4.1. Test Cases

As in previous MPs, we have provided exhaustive test cases for each part of MP4. Please review the MP0 testing instructions.

4.2. Autograding

Like previous MPs we have provided you with an autograding script that you can use to estimate your current grade as often as you want. Please review the MP0 autograding instructions.

5. Submitting Your Work

Follow the instructions from the submitting portion of the CS 125 workflow instructions.

And remember, you must submit something that earns 50 points before Monday 2/5/2018 @ 5PM to earn 10 points on the assignment.

5.1. Academic Integrity

Here’s an example of the training that the CS 125 course staff undertakes to make sure we catch cheaters:

CS 125 is now CS 124

This site is no longer maintained, may contain incorrect information, and may not function properly.

Created 10/24/2021
Updated 10/24/2021
Commit a44ff35 // History // View
Built 10/24/2021 @ 21:29 EDT