1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
#define R 242
#define C 308
char filtered[3][3];
class Image {
char original [R][C];
char median [R][C];
fstream imagefile;
public:
Image(string);
void more_images(const string);
void write_image(const string);
char _median(char block[3][3]);
void filter_median();
};
Image::Image(string input){
more_images(input);
}
void Image::more_images(const string filename){
ifstream::pos_type size;
string widestr = string(filename.begin(), filename.end());
const char* widecstr = widestr.c_str();
char *memblock;
imagefile.open(widecstr, ios::in | ios::binary);
if(imagefile.is_open()){
size = imagefile.tellg();
memblock = new char [size];
imagefile.seekg (0, ios::beg);
imagefile.read(memblock, size);
memcpy(original,memblock,strlen(memblock));
free (memblock);
} else { cout<<"file"<<filename<<" does not exist! \n";
exit(1);
}
}
void Image::write_image(const string filename){
ifstream::pos_type size;
string widestr = string(filename.begin(), filename.end());
const char* widecstr = widestr.c_str ();
imagefile.open(widecstr, ios::out | ios::app | ios::binary);
if(imagefile.is_open()){
size = imagefile.tellg();
imagefile<<*filtered;
imagefile.close();
}
else {
cout<<"file "<<filename<<" doesn't exist!.";
}
}
void Image::filter_median(){
int r, c;
char block[3][3];
for(r = 1; r< R-1; r++) {
for(c=1; c < C; c++){
block[0][0]=original[r-1][c-1];
block[0][1]=original[r-1][c];
block[0][2]=original[r-1][c+1];
block[1][0]=original[r][c-1];
block[1][1]=original[r][c];
block[1][2]=original[r][c+1];
block[2][0]=original[r+1][c-1];
block[2][1]=original[r+1][c];
block[2][2]=original[r+1][c+1];
filtered[r][c] = Image::_median (block); // not sure what is supposed to go in the [][], so i just put [r][c]
}
}
for(c=0; c<C; c++){
filtered [0][c]= original[0][c];
filtered [R-1][c]= original[R-1][c];
}
}
char Image::_median(char block[3][3]){
int greatest, least, n = 9, i, median;
char *pointer1 = &(block[0][0]);
char *pointer2 = &(block[0][0]);
do{
greatest = 0;
least = 0;
for( i=0, pointer2= &block[greatest][least]; i<n ; i++, pointer2++ ){ // not sure what is supposed to go in the [][], so i just put [greatest][least]
if(*pointer1>*pointer2) least++;
else if(*pointer1 < *pointer2) greatest++;
}
median = *pointer1;
pointer1++;
} while(!(((least <=(n-1)/2) && (greatest<= n/2)) || ((greatest <=(n-1)/2 && least <=n/2))));
return median;
}
int main(){
string input, output;
cout<<"Insert name of image(max 20 letters);";
cin>> input;
cout<<"Insert name of filtered image(max 20 letters):";
cin>> output;
Image to_filter(input);
to_filter.filter_median ();
to_filter.write_image(output);
}
|