ForkLoop Images Assignment
Contents
Motivation
We gain experience using a parallel fork loop. Image processing often presents the opportunity for a lot of parallelism. Here we will operate on a single image, making a parallel task for each row.
Background
Revisit the Image Batch Race Condition Exercise's Background section for information on pixels and images.
Code To Investigate
Image Batch Race Condition Exercise
Image Batch Race Condition Code To Investigate
PixelFilters
public class PixelFilters { private static class HueSettingPixelFilter implements PixelFilter { private final double hue; public HueSettingPixelFilter(double hue) { this.hue = hue; } @Override public HsvColor apply(HsvColor src) { double saturation = src.saturation(); double value = src.value(); return new HsvColor(hue, saturation, value); } } public static PixelFilter hueSettingPixelFilterOf(double hue) { // return new instance of named class HueSettingPixelFilter return new HueSettingPixelFilter(hue); } public static PixelFilter hueAdjustingPixelFilterOf(double deltaHue) { // return new instance of anonymous class return new PixelFilter() { @Override public HsvColor apply(HsvColor src) { double hue = src.hue(); double saturation = src.saturation(); double value = src.value(); return new HsvColor(hue + deltaHue, saturation, value); } }; } public static PixelFilter saturationAdjustingPixelFilterOf(double deltaSaturation) { // return anonymous function (also known as a lambda) with parameter type return (HsvColor src) -> { double hue = src.hue(); double saturation = src.saturation(); double value = src.value(); return new HsvColor(hue, saturation + deltaSaturation, value); }; } public static PixelFilter valueAdjustingPixelFilterOf(double deltaValue) { // return anonymous function (also known as a lambda) without parameter type return src -> { double hue = src.hue(); double saturation = src.saturation(); double value = src.value(); return new HsvColor(hue, saturation, value + deltaValue); }; } }
Code To Implement
ParallelTaskPerRowImageFilter
class: | ParallelTaskPerRowImageFilter.java | |
methods: | apply | |
package: | imagefilter.exercise | |
source folder: | student/src/main/java |
apply
method: public HsvImage apply(HsvImage src, PixelFilter pixelFilter)
(parallel implementation required)
Warning: When applying pixelFilter(), always put the result in a new array. Don't overwrite values of the source array. |
This code need only include one: join_fork_loop(array, (arrayValue) -> // etc... ) and an inner for loop using pixelFilter.apply(). Each join_fork_loop iteration should return an HsvColor[].
However, if you do this, join_fork_loop() will default to returning a List<HsvColor[]> which we don't want. To avoid this, hand join_fork_loop() the class you'll return: e.g.
HsvColor[][] destination = join_fork_loop(HsvColor[].class, array, (arrayValue) -> { // code returning HsvColor[]'s });
As opposed to:
List<HsvColor[]> destination = join_fork_loop(array, (arrayValue) -> { // code returning HsvColor[]'s });
Finish out the method by returning a new DefaultHsvImage() which accepts a HsvColor[][] in its constructor.
Visualization
class: | ImageFilterApp.java | VIZ |
package: | imagefilter.viz | |
source folder: | student/src/main/java |
Testing Your Solution
class: | __ParallelTaskPerRowImageFilterTestSuite.java | |
package: | imagefilter.exercise | |
source folder: | testing/src/test/java |
Pledge, Acknowledgments, Citations
file: | exercise-fork-loop-images-pledge-acknowledgments-citations.txt |
More info about the Honor Pledge