The unified diff between revisions [4611588a..] and [878a5382..] is displayed below. It can also be downloaded as a raw diff.

#
#
# add_file "aca319/lab8q1.c"
#  content [5ff79d4a456314b39f4823189b9dd4dbd485aa7b]
#
# patch "aca319/Makefile"
#  from [0a413b453b5847cd99a947d35630a8cab9715933]
#    to [5177fd23b144ae8e94a7dec2ecc95dacf4e5f605]
#
============================================================
--- aca319/lab8q1.c	5ff79d4a456314b39f4823189b9dd4dbd485aa7b
+++ aca319/lab8q1.c	5ff79d4a456314b39f4823189b9dd4dbd485aa7b
@@ -0,0 +1,145 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+
+char *
+load_pgm(const char *filename, int *width, int *height)
+{
+	FILE *f = fopen(filename, "r");
+	int v;
+	char *rv;
+
+	if (!f) {
+		fprintf(stderr, "Unable to load input file: %s\n", filename);
+		return NULL;
+	}
+	if ((fscanf(f, "P%d\n", &v) == EOF) || (v != 6)) {
+		fprintf(stderr, "Wrong file version.\n");
+		return NULL;
+	}
+	if ((fscanf(f, "%d %d\n", width, height) == EOF)) {
+		fprintf(stderr, "Unable to read height and width.\n");
+		return NULL;
+	}
+	rv = malloc(sizeof(char) * *height * *width);
+	if (!rv) {
+		fprintf(stderr, "Unable to allocate image buffer\n");
+		return NULL;
+	}
+	fread(rv, sizeof(char), *height * *width, f);
+	return rv;
+}
+
+int
+minimum(const char *pixels, int count)
+{
+	int i, rv=pixels[0];
+	for (i=1;i<count;i++) {
+		if (pixels[i] < rv)
+			rv = pixels[i];
+	}
+	return rv;
+}
+
+int
+maximum(const char *pixels, int count)
+{
+	int i, rv=pixels[0];
+	for (i=1;i<count;i++) {
+		if (pixels[i] > rv)
+			rv = pixels[i];
+	}
+	return rv;
+}
+
+int
+average(const char *pixels, int count)
+{
+	int i, total;
+	for (i=0;i<count;i++) {
+		total += pixels[i];
+	}
+	return total / count;
+}
+
+int
+int_compare(const void *a, const void *b)
+{
+	int c = *(int *)a;
+	int d = *(int *)b;
+	return d - c;
+}
+int
+median(const char *pixels, int count)
+{
+	char *s = malloc(sizeof(char) * count);
+	if (!s) {
+		fprintf(stderr, "Out of memory in median()\n");
+		return -1;
+	}
+	memcpy(s, pixels, sizeof(char) * count);
+	qsort(s, count, sizeof(char), int_compare);
+	if (count % 2) {
+		/* odd number of elements, average the middle two */
+		return (pixels[count/2] + pixels[(count/2)+1])  / 2;
+	} else {
+		return pixels[count/2];
+	}
+}
+
+char *
+run_filter(const char *clean_image, const char *dirty_image,
+		int height, int width,
+		int (*filter_function)(const char *, int),
+		double *error)
+{
+	char *rv, i, j;
+	char cells[9];
+
+	assert(clean_image != NULL);
+	assert(dirty_image != NULL);
+	assert(error != NULL);
+
+	rv = malloc(sizeof(char) * height * width);
+	if (!rv) {
+		fprintf(stderr, "Out of memory in run_filter\n");
+		return NULL;
+	}
+
+	for (i=0;i<height;i++) {
+		for (j=0;j<width;j++) {
+			int clean, unfiltered, filtered;
+			int count=1;
+
+			cells[0] = unfiltered;
+			clean = clean_image[(i*width)+j];
+			unfiltered = dirty_image[(i*width)+j];
+
+			filtered = filter_function(cells, count);
+		}
+	}
+
+	return rv;
+}
+
+int
+main(int argc, char *argv[])
+{
+	int height, width;
+	double error;
+	char *clean, *noisy;
+	char *rv_minimum, *rv_maximum, *rv_average, *rv_median;
+
+	clean = load_pgm("ViGIR.ppm", &height, &width);
+	noisy = load_pgm("ViGIRnoisy.ppm", &height, &width);
+
+	rv_minimum = run_filter(clean, noisy, height, width, minimum, &error);
+	rv_maximum = run_filter(clean, noisy, height, width, maximum, &error);
+	rv_average = run_filter(clean, noisy, height, width, average, &error);
+	rv_median = run_filter(clean, noisy, height, width, median, &error);
+
+	return 0;
+}
+
============================================================
--- aca319/Makefile	0a413b453b5847cd99a947d35630a8cab9715933
+++ aca319/Makefile	5177fd23b144ae8e94a7dec2ecc95dacf4e5f605
@@ -1,8 +1,9 @@ LDFLAGS=-lpthread
 CC=gcc
 CFLAGS=-Wall -ggdb
 LDFLAGS=-lpthread
+PROGRAMS=lab1q1 lab1q2 lab2q1 lab2q2 lab3q1 lab3q2 lab3q3 lab4q1 lab4q1_up lab5q1 lab5q2 lab6q1 lab6q2 lab7q1 lab7q2 lab8q1

-all: lab1q1 lab1q2 lab2q1 lab2q2 lab3q1 lab3q2 lab3q3 lab4q1 lab4q1_up lab5q1 lab5q2 lab6q1 lab6q2 lab7q1 lab7q2
+all: $(PROGRAMS)

 lab1q1: lab1q1.o
 lab1q2: lab1q2.o
@@ -37,6 +38,9 @@ lab7q2: lab7q2.c
 lab7q2: lab7q2.c
 	mpicc $(CFLAGS) -lmpe -o lab7q2 lab7q2.c

+lab8q1: lab8q1.c
+	mpicc $(CFLAGS) -lmpe -o lab8q1 lab8q1.c
+
 lab4q1_up: lab4q1_up.o

 run_lab3q1:
@@ -64,5 +68,5 @@ clean:
 	mpirun -machinefile machines -np 5 lab7q2

 clean:
-	rm -f *.o lab1q1 lab1q2 lab2q1 lab2q2 lab3q1 lab3q2 lab3q3 lab4q1 lab4q1_up lab5q1 lab5q2 lab6q1 lab6q2 lab7q1 lab7q2
+	rm -f *.o $(PROGRAMS)