質問編集履歴
2
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -251,3 +251,11 @@
|
|
251
251
|
|
252
252
|
|
253
253
|
```
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
追記
|
258
|
+
|
259
|
+
皆様のコメントのおかげで出来ました。
|
260
|
+
|
261
|
+
ありがとうございます。
|
1
プログラムを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -21,3 +21,233 @@
|
|
21
21
|
|
22
22
|
|
23
23
|
Macのターミナルでやっています。
|
24
|
+
|
25
|
+
プログラムはこのようにしてやっています。
|
26
|
+
|
27
|
+
```
|
28
|
+
|
29
|
+
#include <stdio.h>
|
30
|
+
|
31
|
+
#include <stdlib.h>
|
32
|
+
|
33
|
+
#include <math.h>
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
unsigned char *read_ppm(char *fname, int *width, int *height);
|
38
|
+
|
39
|
+
void save_pgm(char *fanme, unsigned char *gray, int width, int height);
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
int main(int argc, char* argv[])
|
44
|
+
|
45
|
+
{
|
46
|
+
|
47
|
+
int width, height; /*入力画像サイズ*/
|
48
|
+
|
49
|
+
unsigned char *src, *out; /*白黒画像:それぞれ入力用と出力用に対応*/
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
int f; /*入力の画素値*/
|
54
|
+
|
55
|
+
int g[256] = {0};/*出力の画素値*/
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
int f_L = 0,f_H = 75;
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
/*カラー画像ファイルを白黒化して読み込み*/
|
64
|
+
|
65
|
+
src = read_ppm(argv[1], &width, &height);
|
66
|
+
|
67
|
+
/*出力画像の保存*/
|
68
|
+
|
69
|
+
save_pgm(argv[2], src, width, height);
|
70
|
+
|
71
|
+
free(src);
|
72
|
+
|
73
|
+
return 0;
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
unsigned char *read_ppm(char *fname, int *width, int *height)
|
80
|
+
|
81
|
+
{
|
82
|
+
|
83
|
+
char str[256], c;
|
84
|
+
|
85
|
+
int max=0;
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
unsigned char *gray = NULL;
|
90
|
+
|
91
|
+
int r=0,g=0,b=0;
|
92
|
+
|
93
|
+
int size=0;
|
94
|
+
|
95
|
+
int i;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
FILE *fp=fopen(fname,"rb");
|
100
|
+
|
101
|
+
if(fp==NULL){
|
102
|
+
|
103
|
+
fprintf(stderr, "error: %s cannot open!\n",fname);
|
104
|
+
|
105
|
+
exit(-1);
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
/*-----------------Magic number------------*/
|
112
|
+
|
113
|
+
fscanf(fp, "%s", str);
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
c = str[1];
|
118
|
+
|
119
|
+
if(c=='3'||c=='6'){
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
/*-------------comment, space, or size ------------*/
|
124
|
+
|
125
|
+
fscanf(fp, "%s", str);
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
if(str[0]=='#'||str[0]==' '){
|
130
|
+
|
131
|
+
while(fscanf(fp,"%c",&str[0])){/* comment skip*/
|
132
|
+
|
133
|
+
if(str[0]=='\n')
|
134
|
+
|
135
|
+
break;
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
fscanf(fp, "%d %d",width,height);
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
else{
|
144
|
+
|
145
|
+
scanf(str,"%d",width);
|
146
|
+
|
147
|
+
fscanf(fp, "%d",height);
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
/* ------------ comment, space, or max value ---------- */
|
154
|
+
|
155
|
+
fscanf(fp, "%d", &max);
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
/* ------------ memory create ---------- */
|
160
|
+
|
161
|
+
size = (*width) * (*height);
|
162
|
+
|
163
|
+
gray = (unsigned char *)malloc(size);
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
/* ------------ pixel value ---------- */
|
168
|
+
|
169
|
+
if(c == '3'){ /* テキストデータ */
|
170
|
+
|
171
|
+
for(i = 0; i < size; i++){
|
172
|
+
|
173
|
+
fscanf(fp, "%d %d %d", &r, &g, &b);
|
174
|
+
|
175
|
+
gray[i] = (unsigned char)(0.299 * r + 0.587 * g + 0.114 * b + 0.5);
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
else{ /* バイナリデータ */
|
182
|
+
|
183
|
+
fread(&r,1,1,fp); /* LF */
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
for(i = 0; i < size; i++){
|
188
|
+
|
189
|
+
fread(&r,1,1,fp);
|
190
|
+
|
191
|
+
fread(&g,1,1,fp);
|
192
|
+
|
193
|
+
fread(&b,1,1,fp);
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
gray[i] = (unsigned char)(0.299 * r + 0.587 * g + 0.114 * b + 0.5);
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
else
|
206
|
+
|
207
|
+
fprintf(stderr, "[%s] is not a ppm-file!\n", fname);
|
208
|
+
|
209
|
+
fclose(fp);
|
210
|
+
|
211
|
+
return gray;
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
void save_pgm(char *fname, unsigned char *gray, int width, int height)
|
218
|
+
|
219
|
+
{
|
220
|
+
|
221
|
+
int x, y;
|
222
|
+
|
223
|
+
FILE *fp = fopen(fname, "w");
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
fprintf(fp, "P2\n");
|
228
|
+
|
229
|
+
fprintf(fp, "%d %d\n", width, height);
|
230
|
+
|
231
|
+
fprintf(fp, "255\n");
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
for(y = 0; y < height; y++){
|
236
|
+
|
237
|
+
for(x = 0; x < width; x++)
|
238
|
+
|
239
|
+
fprintf(fp, "%d ", gray[width * y + x]); /* (注) %d の後にスペースあり */ fprintf(fp, "\n");
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
fclose(fp);
|
246
|
+
|
247
|
+
return;
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
```
|