#define filterWidth 3
#define filterHeight 3
#define imageWidth 320
#define imageHeight 240
//declare image buffers
ColorRGB image[imageWidth][imageHeight];
ColorRGB result[imageWidth][imageHeight];
double filter[filterWidth][filterHeight] =
{
0, 0, 0,
0, 1, 0,
0, 0, 0
};
double factor = 1.0;
double bias = 0.0;
int main(int argc, char *argv[])
{
//set up the screen
screen(imageWidth, imageHeight, 0, "Filters");
//load the image into the buffer
loadBMP("pics/photo3.bmp", image[0], imageWidth , imageHeight);
//apply the filter
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
double red = 0.0, green = 0.0, blue = 0.0;
//multiply every value of the filter with corresponding image pixel
for(int filterX = 0; filterX < filterWidth; filterX++)
for(int filterY = 0; filterY < filterHeight; filterY++)
{
int imageX = (x  filterWidth / 2 + filterX + w) % w;
int imageY = (y  filterHeight / 2 + filterY + h) % h;
red += image[imageX][imageY].r * filter[filterX][filterY];
green += image[imageX][imageY].g * filter[filterX][filterY];
blue += image[imageX][imageY].b * filter[filterX][filterY];
}
//truncate values smaller than zero and larger than 255
result[x][y].r = min(max(int(factor * red + bias), 0), 255);
result[x][y].g = min(max(int(factor * green + bias), 0), 255);
result[x][y].b = min(max(int(factor * blue + bias), 0), 255);
}
//draw the result buffer to the screen
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
pset(x, y, result[x][y]);
}
//redraw & sleep
redraw();
sleep();
}

//take absolute value and truncate to 255

double filter[filterWidth][filterHeight] =
{
0.0, 0.2, 0.0,
0.2, 0.2, 0.2,
0.0, 0.2, 0.0
};
double factor = 1.0;
double bias = 0.0;

double filter[filterWidth][filterHeight] =
{
0, 0, 1, 0, 0,
0, 1, 1, 1, 0,
1, 1, 1, 1, 1,
0, 1, 1, 1, 0,
0, 0, 1, 0, 0,
};
double factor = 1.0 / 13.0;
double bias = 0.0;

double filter[filterWidth][filterHeight] =
{
1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1,
};
double factor = 1.0 / 9.0;
double bias = 0.0;

double filter[filterWidth][filterHeight] =
{
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
1, 1, 2, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
};
double factor = 1.0;
double bias = 0.0;

double filter[filterWidth][filterHeight] =
{
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 4, 0, 0,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
};
double factor = 1.0;
double bias = 0.0;

double filter[filterWidth][filterHeight] =
{
1, 0, 0, 0, 0,
0, 2, 0, 0, 0,
0, 0, 6, 0, 0,
0, 0, 0, 2, 0,
0, 0, 0, 0, 1,
};
double factor = 1.0;
double bias = 0.0;

double filter[filterWidth][filterHeight] =
{
1, 1, 1,
1, 8, 1,
1, 1, 1
};
double factor = 1.0;
double bias = 0.0;

double filter[filterWidth][filterHeight] =
{
1, 1, 1,
1, 9, 1,
1, 1, 1
};
double factor = 1.0;
double bias = 0.0;

double filter[filterWidth][filterHeight] =
{
1, 1, 1, 1, 1,
1, 2, 2, 2, 1,
1, 2, 8, 2, 1,
1, 2, 2, 2, 1,
1, 1, 1, 1, 1,
};
double factor = 1.0 / 8.0;
double bias = 0.0;

double filter[filterWidth][filterHeight] =
{
1, 1, 1,
1, 7, 1,
1, 1, 1
};
double factor = 1.0;
double bias = 0.0;

double filter[filterWidth][filterHeight] =
{
1, 1, 0,
1, 0, 1,
0, 1, 1
};
double factor = 1.0;
double bias = 128.0;

double filter[filterWidth][filterHeight] =
{
1, 1, 1, 1, 0,
1, 1, 1, 0, 1,
1, 1, 0, 1, 1,
1, 0, 1, 1, 1,
0, 1, 1, 1, 1
};
double factor = 1.0;
double bias = 128.0;

double filter[filterWidth][filterHeight] =
{
1, 1, 1,
1, 1, 1,
1, 1, 1
};
double factor = 1.0 / 9.0;
double bias = 0.0;

#define filterWidth 3
#define filterHeight 3
#define imageWidth 320
#define imageHeight 240
//image buffers
ColorRGB image[imageWidth][imageHeight];
ColorRGB result[imageWidth][imageHeight];
//color arrays
int red[filterWidth * filterHeight];
int green[filterWidth * filterHeight];
int blue[filterWidth * filterHeight];
void combsort(int * data, int amount);

int main(int argc, char *argv[])
{
//set up the screen
screen(imageWidth, imageHeight, 0, "Median Filter");
//load the image into the buffer
loadBMP("pics/noise.bmp", image[0], imageWidth , imageHeight);
//apply the filter
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int n = 0;
//set the color values in the arrays
for(int filterX = 0; filterX < filterWidth; filterX++)
for(int filterY = 0; filterY < filterHeight; filterY++)
{
int imageX = (x  filterWidth / 2 + filterX + w) % w;
int imageY = (y  filterHeight / 2 + filterY + h) % h;
red[n] = image[imageX][imageY].r;
green[n] = image[imageX][imageY].g;
blue[n] = image[imageX][imageY].b;
n++;
}
combsort(red, filterWidth * filterHeight);
combsort(green, filterWidth * filterHeight);
combsort(blue, filterWidth * filterHeight);
//take the median, if the length of the array is even, take the average of both center values
if((filterWidth * filterHeight) % 2 == 1)
{
result[x][y].r = red[filterWidth * filterHeight / 2];
result[x][y].g = green[filterWidth * filterHeight / 2];
result[x][y].b = blue[filterWidth * filterHeight / 2];
}
else if(filterWidth >= 2)
{
result[x][y].r = (red[filterWidth * filterHeight / 2] + red[filterWidth * filterHeight / 2 + 1]) / 2;
result[x][y].g = (green[filterWidth * filterHeight / 2] + green[filterWidth * filterHeight / 2 + 1]) / 2;
result[x][y].b = (blue[filterWidth * filterHeight / 2] + blue[filterWidth * filterHeight / 2 + 1]) / 2;
}
}
//draw the result buffer to the screen
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
pset(x, y, result[x][y]);
}
//redraw & sleep
redraw();
sleep();
}

//combsort: bubble sort made faster by using gaps to eliminate turtles
void combsort(int * data, int amount)
{
int gap = amount;
bool swapped = false;
while(gap > 1  swapped)
{
//shrink factor 1.3
gap = (gap * 10) / 13;
if(gap == 9  gap == 10) gap = 11;
if (gap < 1) gap = 1;
swapped = false;
for (int i = 0; i < amount  gap; i++)
{
int j = i + gap;
if (data[i] > data[j])
{
data[i] += data[j];
data[j] = data[i]  data[j];
data[i] = data[j];
swapped = true;
}
}
}
}
