Difference between revisions of "ForkLoop Images Assignment"

From CSE231 Wiki
Jump to navigation Jump to search
 
(28 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
=Background=
 
=Background=
 
Revisit the [[Race_Condition_Image_Batch_Assignment#Background|Image Batch Race Condition Exercise's Background]] section for information on pixels and images.
 
Revisit the [[Race_Condition_Image_Batch_Assignment#Background|Image Batch Race Condition Exercise's Background]] section for information on pixels and images.
 +
==HsvColor==
 +
{{HsvColorWithMethodLinks}}
 +
 +
==DefaultImmutableHsvImage==
 +
 +
==PixelFiler==
 +
{{PixelFilterWithApplyLinks}}
  
 
=Code To Investigate=
 
=Code To Investigate=
Line 10: Line 17:
  
 
==PixelFilters==
 
==PixelFilters==
<nowiki>public class PixelFilters {
+
Our Image Filter will apply a PixelFilter to every pixel in a specified image.
 +
 
 +
Some example pixel filters have been provided which use named classes, anonymous classes, and lambdas:
 +
 
 +
{{CollapsibleCode|class PixelFilters &nbsp;|<syntaxhighlight lang="java">
 +
public class PixelFilters {
 
private static class HueSettingPixelFilter implements PixelFilter {
 
private static class HueSettingPixelFilter implements PixelFilter {
 
private final double hue;
 
private final double hue;
Line 63: Line 75:
 
};
 
};
 
}
 
}
}</nowiki>
+
}</syntaxhighlight>}}
  
 
=Code To Implement=
 
=Code To Implement=
Line 70: Line 82:
  
 
===apply===
 
===apply===
{{Parallel|public HsvImage apply(HsvImage src, PixelFilter pixelFilter)}}
+
{{Parallel|public ImmutableHsvImage apply(ImmutableHsvImage src, PixelFilter pixelFilter)}}
{{warning | When saving the result of pixelFilter.apply(), always put the result in a new array. Don't overwrite values of the source array.}}
+
 
 +
Apply the provided pixelFilter to each pixel in the src HsvImage.
 +
 
 +
Each pixel is represented by an HsvColor.
 +
 
 +
PixelFilter has one interesting method '''apply''' which takes a source HsvColor and '''returns''' a new filtered HsvColor.  It will '''NOT''' modify the passed source HsvColor.  It would be impossible anyway, as HsvColor is immutable.
 +
 
 +
Example use of PixelFilter apply:
 +
 
 +
<syntaxhighlight lang="java">
 +
HsvColor dstPixel = pixelFilter.apply(srcPixel);
 +
</syntaxhighlight>
 +
 
 +
<!-- {{warning | When saving the result of pixelFilter.apply(), always put the result in a new array. Do '''NOT''' overwrite values of the source array.}}-->
  
To complete this method we must return a new instance of the interface HsvImage.  The provided class [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/image/impl/DefaultHsvImage.html DefaultHsvImage] implements HsvImage so it will suffice.
+
To complete this method we must return a new instance of the HsvImage class.
  
Nicely, DefaultHsvImage provides both:
+
Nicely, HsvImage provides both:
* a constructor which takes an [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/image/impl/DefaultHsvImage.html#%3Cinit%3E(edu.wustl.cse231s.color.HsvColor%5B%5D%5B%5D) <nowiki>List<HsvColor[]></nowiki>] and,
+
* a constructor which takes an [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/image/HsvImage.html#%3Cinit%3E(edu.wustl.cse231s.color.HsvColor%5B%5D%5B%5D) <nowiki>List<HsvColor[]></nowiki>] and,
* another constructor which takes an [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/image/impl/DefaultHsvImage.html#%3Cinit%3E(java.util.List) <nowiki>HsvColor[][]</nowiki>].
+
* another constructor which takes an [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/image/HsvImage.html#%3Cinit%3E(java.util.List) <nowiki>HsvColor[][]</nowiki>].
  
 
These two constructors match up well with the two different return types of the many incantations of join_fork_loop:
 
These two constructors match up well with the two different return types of the many incantations of join_fork_loop:
  
* <nowiki>List<R></nowiki>, and
+
* [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/spring23/apidocs/fj/FJ.html#join_fork_loop(T%5B%5D,fj.api.TaskFunction) <nowiki>List<R></nowiki>], and
* <nowiki>R[]</nowiki>
+
* [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/spring23/apidocs/fj/FJ.html#join_fork_loop(java.lang.Class,T%5B%5D,fj.api.TaskFunction) <nowiki>R[]</nowiki>]
 +
 
 +
where R is the return type of the [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/api/TaskFunction.html <nowiki>TaskFunction<T, R></nowiki>] passed to the join_fork_loop. 
 +
 
 +
What should the return type from the this TaskFunction be?
 +
 
 +
{{Requirements3|Create a task per row.|Do '''NOT''' create a task per pixel.|Use a parallel fork loop.}}
 +
 
  
where R is the return type of the TaskFunction passed to the join_fork_loop.  What should the return type from the this TaskFunction be?
 
  
 
<!--
 
<!--

Latest revision as of 20:49, 9 September 2024

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.

HsvColor

class HsvColor

double hue()
double saturation()
double value()

DefaultImmutableHsvImage

PixelFiler

interface PixelFilter extends UnaryOperator<HsvColor>

HsvColor apply(HsvColor color)

Code To Investigate

Image Batch Race Condition Exercise

Image Batch Race Condition Code To Investigate

PixelFilters

Our Image Filter will apply a PixelFilter to every pixel in a specified image.

Some example pixel filters have been provided which use named classes, anonymous classes, and lambdas:

class 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 Java.png
methods: apply
package: imagefilter.exercise
source folder: student/src/main/java

apply

method: public ImmutableHsvImage apply(ImmutableHsvImage src, PixelFilter pixelFilter) Parallel.svg (parallel implementation required)

Apply the provided pixelFilter to each pixel in the src HsvImage.

Each pixel is represented by an HsvColor.

PixelFilter has one interesting method apply which takes a source HsvColor and returns a new filtered HsvColor. It will NOT modify the passed source HsvColor. It would be impossible anyway, as HsvColor is immutable.

Example use of PixelFilter apply:

HsvColor dstPixel = pixelFilter.apply(srcPixel);


To complete this method we must return a new instance of the HsvImage class.

Nicely, HsvImage provides both:

These two constructors match up well with the two different return types of the many incantations of join_fork_loop:

where R is the return type of the TaskFunction<T, R> passed to the join_fork_loop.

What should the return type from the this TaskFunction be?

Requirements:
Green check.svgCreate a task per row.
Green check.svgDo NOT create a task per pixel.
Green check.svgUse a parallel fork loop.



Visualization

class: ImageFilterApp.java VIZ
package: imagefilter.viz
source folder: student/src/main/java

Image filter app.png

Testing Your Solution

class: __ParallelTaskPerRowImageFilterTestSuite.java Junit.png
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