Summary: Learn how to invert images and how to display images in Ericson's PictureExplorer object.
This module is one of a series of modules designed to teach you about Object-Oriented Programming (OOP) using Java.
The program described in this module requires the use of the Guzdial-Ericson multimedia class library. You will find download, installation, and usage instructions for the library at Java OOP: The Guzdial-Ericson Multimedia Class Library .
I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the images and listings while you are reading about them.
The program that I will explain in this module is designed to be used as a test of the student's understanding of programming using Java and Ericson's media library.
The student is provided an image file named Prob02.jpg along with a pair of PictureExplorer windows containing the raw image and a modified version of the image. (See Image 1 and Image 2 .)
Deduce the algorithm
The first part of the test is to determine if the student can examine the raw image shown in the PictureExplorer window in Image 1 and deduce the algorithm required to produce the output shown in the PictureExplorer window in Image 2 .
Implement the algorithm
The second part of the test is to determine if the student can implement the algorithm once it is established and also satisfy some requirements for text output on the command line screen. Among other things, this requires that the student be able to:
Program output
The raw image is displayed in the PictureExplorer window shown in Image 1 .
| Image 1: The raw image. |
|---|
![]() |
The modified image is shown in the PictureExplorer window in Image 2 .
| Image 2: The modified image. |
|---|
![]() |
The required output on the command-line screen is shown by the last two lines of text in Image 3 . The other text in Image 3 is produced by the system during the compilation and execution process.
| Image 3: Output text on the command line screen. |
|---|
|
The algorithm
The algorithm required to transform the image from Image 1 to Image 2 is:
A color value is inverted by subtracting the value from 255.
Obvious that the blue color value is reduced to zero
It should be obvious to the student when comparing the two images in the PictureExplorer objects that the blue pixel value has been set to zero for every pixel in the modified image.
Color inversion is not quite so obvious
Deducing that the red and green colors in the output pixels are the inverse of the red and green colors in the input image isn't as straightforward. However, color inversion is one of the examples provided in the Ericson textbook, so that should serve as a clue to the student. I have also published several online tutorials that involve color inversion.
The implementation of the algorithm will be explained below.
Will explain in fragments
I will explain this program in fragments. A complete listing is provided in Listing 6 near the end of the module.
I will begin with the driver class named Prob02 , which is shown in its entirety in Listing 1 .
| Listing 1: The driver class. |
|---|
|
You should already be familiar with everything in Listing 1 . The most important aspect of Listing 1 for purposes of this discussion is the call to the run method belonging to the object instantiated from the Prob02Runner class. I will explain the run method shortly.
Beginning of the class named Prob02Runner
The class definition for the class named Prob02Runner begins in Listing 2 .
| Listing 2: Beginning of the class named Prob02Runner. |
|---|
|
Again, you should be familiar with everything in Listing 2 . I will simply highlight the instantiation of a new Picture object using an image file as input and the saving of a reference to that object in the private instance variable named pic .
The beginning of the run method
The run method begins in Listing 3 . This is where the action is, so to speak.
| Listing 3: The beginning of the run method. |
|---|
|
You are already familiar with the call to the addMessage method to add my name as text to the image encapsulated in the Picture object. ( See Image 1 .)
The explore method
The call to the explore method is new to this module.
The explore method is defined in the SimplePicture class, which is the superclass of the Picture class. The method is inherited into the Picture class.
javadocs description of the explore method
The javadocs description of this method is shown in Image 4 .
| Image 4: javadocs description of the explore method. |
|---|
|
Result of calling the explore method
The result of calling the explore method in Listing 3 is to create and display the PictureExplorer object shown in Image 1 .
Very important capability
The availability of the explore method and the PictureExplorer class is very important in at least two respects:
Implementing the algorithm
The code in Listing 4 implements the algorithm required to modify the original image to make it look like the image shown in Image 2 .
| Listing 4: Implementing the algorithm. |
|---|
|
In particular, the code in Listing 4 sets the blue color components to 0 and inverts the red and green color components for every pixel in the picture.
One of several approaches
There are several ways to do this, and this is only one of those ways. This approach makes use of a method named getPixels that is defined in the SimplePicture class and inherited into the Picture class.
Very useful when...
This approach is particularly useful when you want to perform the same action on every pixel in an image. The advantage is that you don't have to worry about horizontal and vertical coordinates with this approach. Access to all of the pixels is provided in a one-dimensional array.
javadocs description of the getPixels method
The javadocs description of this method is shown in Image 5 .
| Image 5: javadocs description of the getPixels method. |
|---|
|
What is a Pixel object?
An object of Ericson's Pixel class encapsulates an individual pixel from an image. Image 6 shows the javadocs description of the Pixel class.
| Image 6: javadocs description of the Pixel class. |
|---|
|
Many methods available
The Pixel class defines a large number of methods. Once you have a reference to a Pixel object, you can manipulate the underlying pixel encapsulated in that object in a variety of ways.
Get the pixels in the image
Recall that a reference to the Picture object that encapsulates our image is stored in the variable named pic . (See Listing 2 .)
Listing 4 begins by calling the getPixels method on that reference.
All of the pixels in the image are returned in a one-dimensional array.
A reference to the array is stored in a local reference variable of type Pixel[] named pixelArray .
A for-each loop
A special kind of for loop (often called a for-each loop) is used to access and process each pixel in the array. (A conventional for loop could also be used here.)
During each iteration of the loop...
The three statements inside the loop modify the color values of a single pixel.
The first two statements invert the red and green color values by subtracting the values from 255.
The third statement in the loop sets the blue color value to zero.
When the loop terminates...
Every pixel in the image will have been modified as described above.
Not a reversible process
Because the blue color values were set to zero, the image has now been modified in an irreversible manner.
A reversible process
However, if the blue color values had also been inverted, the process would be reversible.
All that would be necessary to recover the original image would be to invert all of the pixels again.
An important process
Color inversion is a very important process in many areas of computing that involve images. The process is:
Often used to highlight selected images
For example, many software program invert all of the colors in an image when it is selected for some purpose, such as copying to the clipboard. Then the colors are restored to their original values when the image is deselected.
Next to redeye correction, color inversion is probably the most commonly used color modification algorithm in use in modern image processing.
Display again and terminate
The variable named pic still contains a reference to the original Picture object. However, the image that is encapsulated in that object has been significantly modified.
Listing 5 calls the explore method again, creating and displaying another PictureExplorer object that encapsulates a copy of the Picture object with the modified image.
| Listing 5: Display again and terminate. |
|---|
|
The result is shown in Image 2 .
The end of the program
Listing 5 also signals the end of the run method and the end of the Prob02Runner class.
Return control to main
The run method terminates and returns control to the main method in Listing 1 .
The code in the main method calls a getter method to get a reference to the Picture object.
The reference is passed to the println method, which displays the information about the Picture object in the last line of Image 3 .
The program terminates
Then the main method terminates, at which time the program terminates and returns control to the operating system.
In this module, you learned how to invert images and how to display images in PictureExplorer objects.
Y ou will learn how to implement a space-wise linear color-modification algorithm in the next module.
Select the following links to view online video lectures on the material in this module.
This section contains a variety of miscellaneous information.
Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.
Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
A complete listing of the program discussed in this module is shown in Listing 6 below.
| Listing 6: Complete program listing. |
|---|
|
-end-