Computer Science Courses
Related sites
HW 8 |
To use functional composition technique and lazy evaluation to create an image processing utility.
Note: this homework is based on exercises from Chapter 3of "Java SE 8 for the Really Impatient" book by Cay Hortsmann which weremodified a little.
Review the notion of λ-expressions and functional interfacesusing either lectures slides, or , or one of the resources listed in F2, last slide(or anything useful you can find online, just don't spread too wide and thing).
Clone the fork of your Gitlab repository (or, ifyou have clone it already, run git pull
command toupdate it, either on the command line, or from an IDE if your are using one). Change into hw8
directory and find three Java source files and a JPEG image file — these will be the resource which you shall use in this homework:
ColourFilter.java
ImageDemo.java
LatentImage.java
amazing-trees.jpg
Study the code of the ColourFilter.java
program (including the commented out "alternative" lines 29,30 whichcan be executed instead of the "composed" transform, line 32), understand themeaning and the effect of the ColourFilter.compose(op1, op2)
whichreturns a composition of two java.util.function.UnaryOperator
objects, similar to two functions composition in Mathematics:
y = f(x), z = g(y) → h = g o f, h(x) = g(f(x))
The UnaryOperator interface consists of the abstract method T apply(T t)
.
One obvious benefit of the image transformation composition is avoidance of intermediate image object(s). Multiple transformations can be first composed into one transformation, and then the original image is "broken into pixels", and each is transformed in the final value, the pixels combined back into the transformed image and returned.Such "only once transforming" approach is known as lazy computation (a computation is defined and ready to be used, but its actual use is postponed until it is absolutely necessary).
Next, study an alternative image processing which allows to combine multipletransformations. The client class makes use of the class LatentImage in the LatentImage.java
file which contains a list of unary operator objects — pending operations — and the terminal operation (a method) toImage()
which returns the result (an original image transformed in sequence by a number of colour operations).
Consider a transform(Image in, UnaryOperator
method from the class ColourFilter, and add the ColourTransformer functional interface:
@FunctionalInterfaceinterface ColourTransformer { Color apply(int x, int y, Color colorAtXY); }
to the class ColourFilter, and add another (overload name) static methodImage transform(Image in, ColorTransformer f)
whichperforms an image transforming operation similar to themethod Image transform(Image in, UnaryOperator
where the ColourTransformer interface object is used instead.
Make use of the new method to add a 10 pixel wide gray frame replacing the pixels on the border of an image by calling it with the appropriateλ-expression in place of ColourTransformer interface object.Observe the effect of closure (what are the captured variables?)
Ponder why did we need the distributed (which depends of pixel coordinate) colour filter to solve a problem like this.
Using the insight from the Part One, enhance (add new methods) the LatentImage class to allow it perform blur
operation in which every pixel colour value of the original is replaced by the average of itself and its eight neighbour pixels. Note, that such operations as blurring require the knowledge of pixels from the previous stages(which precedes blurring) and therefore cannot be done lazily.
You will get up to two marks (one for each task), if you submit your work by pushing the local repository using git push
command by Friday, 20 May 2016,in your GitLab repository following the instructions to be providedin the . The code should be placed in the same hw8
directory wherethe original source and image files where located when your cloned yourlocal repository for the first time.
HW 8 |
Updated: Sun 12 Jun 2016 17:27:37 AEST • Responsible Officer: JavaScript must be enabled to display this email address. • Page Contact:
+61 2 6125 5111
The Australian National University, Canberra
CRICOS Provider : 00120C ABN : 52 234 063 906