filter.c
Go to the documentation of this file.00001 #include <math.h>
00002 #include <stdlib.h>
00003 #include <ppm.h>
00004
00005 int comp_int(const void *int1, const void *int2) {
00006
00007 return(*((int *)int1) - *((int *)int2));
00008 }
00009
00010 enum ppm_error median_filter_pgm(PPM *im_in, PPM **im_out) {
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 int pixels[9];
00026 int num_pixels, median_pos;
00027 int x, y, i, j;
00028
00029 if (im_in->bytes_per_pixel != 1) {
00030 fprintf(stderr, "median_filter_pgm: ");
00031 return(ONE_PLANE_NEEDED);
00032 }
00033
00034
00035
00036 (*im_out) = new_ppm();
00037 add_comment((*im_out), "# Image created by median filtering.\n");
00038 (*im_out)->type = PGM_RAW;
00039 (*im_out)->width = im_in->width;
00040 (*im_out)->height = im_in->height;
00041 (*im_out)->max_col_comp = im_in->max_col_comp;
00042 (*im_out)->bytes_per_pixel = 1;
00043 (*im_out)->pixel = (byte *)malloc((*im_out)->width*(*im_out)->height*sizeof(byte));
00044
00045
00046 for (y = 0; y < im_in->height; y++) {
00047 for (x = 0; x < im_in->width; x++) {
00048 num_pixels = 0;
00049 for (j = -1; j <= 1; j++) {
00050 for (i = -1; i <= 1; i++) {
00051 if ( ((x + i) >= 0) &&
00052 ((x + i) < im_in->width) &&
00053 ((y + j) >= 0) &&
00054 ((y + j) < im_in->height)
00055 )
00056 pixels[num_pixels++] = im_in->pixel[(y+j)*im_in->width + x + i];
00057 }
00058 }
00059 qsort(pixels, num_pixels, sizeof(int), comp_int);
00060 median_pos = num_pixels/2;
00061 if (num_pixels % 2)
00062 median_pos += 1;
00063 (*im_out)->pixel[y*(*im_out)->width + x] = pixels[median_pos];
00064 }
00065 }
00066
00067 return(PPM_OK);
00068 }
00069
00070 enum ppm_error mode_filter_pgm(PPM *im_in, PPM **im_out) {
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 int pixels[9];
00082 int *pixel_count;
00083 int num_pixels;
00084 int max, max_pixel;
00085 int x, y, i, j;
00086
00087 if (im_in->bytes_per_pixel != 1) {
00088 fprintf(stderr, "mode_filter_pgm: ");
00089 return(ONE_PLANE_NEEDED);
00090 }
00091
00092
00093
00094 (*im_out) = new_ppm();
00095 add_comment((*im_out), "# Image created by mode filtering.\n");
00096 (*im_out)->type = PGM_RAW;
00097 (*im_out)->width = im_in->width;
00098 (*im_out)->height = im_in->height;
00099 (*im_out)->max_col_comp = im_in->max_col_comp;
00100 (*im_out)->bytes_per_pixel = 1;
00101 (*im_out)->pixel = (byte *)malloc((*im_out)->width*(*im_out)->height*sizeof(byte));
00102
00103
00104 pixel_count = (int *)malloc((*im_out)->max_col_comp*sizeof(int));
00105 for (i = 0; i < (*im_out)->max_col_comp; i++)
00106 pixel_count[i] = 0;
00107
00108
00109 for (y = 0; y < im_in->height; y++) {
00110 for (x = 0; x < im_in->width; x++) {
00111 num_pixels = 0;
00112 for (j = -1; j <= 1; j++) {
00113 for (i = -1; i <= 1; i++) {
00114 if ( ((x + i) >= 0) &&
00115 ((x + i) < im_in->width) &&
00116 ((y + j) >= 0) &&
00117 ((y + j) < im_in->height)
00118 ) {
00119 pixels[num_pixels] = (int)im_in->pixel[(y+j)*im_in->width + x + i];
00120 pixel_count[pixels[num_pixels]]++;
00121 num_pixels++;
00122 }
00123 }
00124 }
00125 max = 0;
00126 for (i = 0; i < num_pixels; i++) {
00127 if (pixel_count[pixels[i]] > max) {
00128 max = pixel_count[pixels[i]];
00129 max_pixel = pixels[i];
00130 pixel_count[pixels[i]] = 0;
00131 }
00132 }
00133 (*im_out)->pixel[y*(*im_out)->width + x] = (byte)max_pixel;
00134 }
00135 }
00136
00137 return(PPM_OK);
00138 }
00139
00140 enum ppm_error mean_filter_pgm(PPM *im_in, PPM **im_out) {
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 int pixels[9];
00152 int num_pixels;
00153 double mean;
00154 int x, y, i, j;
00155
00156 if (im_in->bytes_per_pixel != 1) {
00157 fprintf(stderr, "mode_filter_pgm: ");
00158 return(ONE_PLANE_NEEDED);
00159 }
00160
00161
00162
00163 (*im_out) = new_ppm();
00164 add_comment((*im_out), "# Image created by mean filtering.\n");
00165 (*im_out)->type = PGM_RAW;
00166 (*im_out)->width = im_in->width;
00167 (*im_out)->height = im_in->height;
00168 (*im_out)->max_col_comp = im_in->max_col_comp;
00169 (*im_out)->bytes_per_pixel = 1;
00170 (*im_out)->pixel = (byte *)malloc((*im_out)->width*(*im_out)->height*sizeof(byte));
00171
00172
00173 for (y = 0; y < im_in->height; y++) {
00174 for (x = 0; x < im_in->width; x++) {
00175 num_pixels = 0;
00176 mean = 0;
00177 for (j = -1; j <= 1; j++) {
00178 for (i = -1; i <= 1; i++) {
00179 if ( ((x + i) >= 0) &&
00180 ((x + i) < im_in->width) &&
00181 ((y + j) >= 0) &&
00182 ((y + j) < im_in->height)
00183 ) {
00184 mean += (double)im_in->pixel[(y+j)*im_in->width + x + i];
00185 num_pixels++;
00186 }
00187 }
00188 }
00189 (*im_out)->pixel[y*(*im_out)->width + x] = (byte)rint(mean/(double)num_pixels);
00190 }
00191 }
00192
00193 return(PPM_OK);
00194 }