FILE inpute and out

Ok guys this is gonna be a long one, I'm learning how to eliminate salt and pepper noise, there was this source code in C...but since i learned c++ i made a friend try to translate it for me...he tried to do a good job but....i think there are some errors...so i am gonna post the code here so that you gurus on the forum help me sort it out...am gonna post the code...some of my questions will be in the comments of the code...and the rest will be after the code...thank you in advance...


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
121
122
123
124
125
 #include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
using namespace std;

#define R 242
#define C 308

typedef char luma;

class Image {
luma originale [R][C];
luma mediana [R][C];
fstream imagefile;
public:
    Image(string);
    void leggi_immagine(const string);
    void scrivi_immagine(const string);
    void filtro_mediano();
};

int main(){
string input, output;

//reading of image
cout<<"Insert name of image(max 20 letters);";
cin>> input;
cout<<"Insert name of filtered image(max 20 letters):";
cin>> output;

Image dafiltrare(input);
dafiltrare.filtro_mediano ();
dafiltrare.scrivi_immagine(output);

}

Image::Image(string input){
leggi_immagine(input);
}

void Image::leggi_immagine(const string filename){
ifstream::pos_type size;

wstring widestr = wstring(filename.begin(), filename.end()); // what is this? why these arguments?
const wchar_t* widecstr = widestr.c_str();

char * memblock;


imagefile.open(widecstr, ios::in | ios::binary)
if(imagefile.is_open()){
    size = imagefile.tellg();
    membloc = new char [size];
    imagefile.seekg (o, ios::beg);
    imagefile.read(memblock, size);
    memcpy(originale,memblock,strlen(memblock)); // what  is the meaning of this line???
    free (memblock); //did he confuse himself with "delete" ??? i bet he is used to "free" in C and is not me who is a jerk :D
} else { cout<<"file"<<filename<<" does not exist! \n";
exit(-1);
}

void Image::scrivi_immagine(const string filename){
ifstream::pos_type size;

wstring widestr = wstring(filename.beging(), filename.end())// same as the line up there...
const wchar_t* = widestr.c_str ();

imagefile.open(widecstr, ios::out | ios::app | ios::binary);
if(imagefile.is_open()){
    size = imagefile.tellg();
    imagefile<<*filtrata; // what was done here??? is "write" not needed in binary? why star filtrata? a pointer?
    imagefile.close();
}
else {
    cout<<"file "<<filname<<" doesn't exist!.";
}
}

void Image::filtro_mediano(){
int r, c;
luma blocco[3][3];

for(r = 1; r< R-1; r++)
for(c=1; c < C; c++){
 blocco[0][0]=originale[r-1][c-1];
 blocco[0][1]=originale[r-1][c];
 blocco[0][2]=originale[r-1][c+1];
 blocco[1][0]=originale[r][c-1];
 blocco[1][1]=originale[r][c];
 blocco[1][2]=originale[r][c+1];
 blocco[2][0]=originale[r+1][c-1];
blocco[2][1]=originale[r+1][c];
blocco[2][2]=originale[r+1][c+1];
filtrata[][] = mediana (blocco);
}
}
//padding before last row
for(c=0; c<C; c++){
    filtrata [0][c]= originale[0][c];
    filtrata [R-1][c]= originale[R-1][c];
}
}

luma Image::mediana(luma blocco[3][3]){
int maggiori, minori, n = 9, i, mediana;
luma *puntatore1 = &(blocco[0][0]);
luma *puntatore2 = &(blocco[0][0]);

do{
    maggiori = 0;
    minori = 0;
    for( i=0, puntatore2= &blocco[][]; i<n ; i++, puntatore2++ ){
        if(*puntatore1>*puntatore2) minore++;
        else if(*puntatore1 < *puntatore2) maggiori++;
    }
    mediana = *puntatore1;
    puntatore1++;
} while(!(((minori <=(n-1)/2) && (maggiori<= n/2)) || ((maggiori <=(n-1)/2 && minori <=n/2))))
return mediana;

}

 


the most confusing line to me is line 58/59...we must copy the file from disk and place it in the matrix RxC... but...over there i guess my friend did some magic...someone help me....thanks
Last edited on
closed account (Dy7SLyTq)
c doesnt have new and delete so yes he probably meant that. however its not quite as simple as just replacing free with delete i believe
closed account (18hRX9L8)
Fixed syntax errors, spelling mistakes, and function errors.
Does compile but I still have to test it.

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);
}
Last edited on
I am sorry usandfriends...but this is not much helpful...ok am gonna place the question again..
in the edit you did...Can you explain to me line 42??? what was done there??? can you explain in simple words? and the other places i asked questions about...thank you
Topic archived. No new replies allowed.