お世話になります。
バイナリ形式の640×480のpgm画像をコピーし、
さらに640×480のpgm画像の、全てのx座標の縦方向の480ピクセルの濃度値の総和の値を640個分
.txt形式のファイルに出力するようなプログラムを組みました。しかし、どーもpgm形式の
#で始まるコメントアウトが上手く読み飛ばすことが出来ません。コメントが無い場合は読み込んでくれるのですが。。。仕方なくxeditというバイナリエディタでコメント文を削ったりしています。
不便なのでどこが上手くいっていないのか教えてください!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define IX 640 // horizontal resolution of input image
#define IY 480 // vertical resolution of input image
#define OX 640 // horizontal resolution of output image
#define OY 480 // vertical resolution of output image
unsigned char in_img[IX*IY]; // input image
unsigned char out_img[OX*OY]; // output image
int aa[IX];
main()
{
int ix, iy, lev, x, y, k;
char fname[256];
FILE* fp;
char str[256];
/****** Load Image ******/
printf("Input filename = ");
scanf("%s", fname);
printf("Load...\n");
if((fp = fopen(fname, "rb")) == NULL) {
printf("file open error !\n");
}
do {
fscanf(fp, "%s\n", str);
}while(str[0] == '#');
if(strncmp(str, "P5", 2) != 0) {
printf("image type is not P5\n");
}
do {
fscanf(fp, "%s\n", str);
}while(str[0] == '#');
sscanf(str, "%d", &ix);
fscanf(fp, "%d\n", &iy);
if((ix != IX) || (iy != IY)) {
printf("image size is not %d X %d\n", IX, IY);
}
do {
fscanf(fp, "%s\n", str);
}while(str[0] == '#');
sscanf(str, "%d", &lev);
if(lev != 255) {
printf("image graylevel not 255\n");
}
if(fread(in_img, IX*IY, 1, fp) != 1) {
printf("file read error !\n");
}
fclose(fp);
/****** Process Image ******/
printf("Process...\n");
for(x = 0; x < OX; ++x) {
for(y = 0; y < OY; ++y) {
out_img[(OX*y+x)] = in_img[(IX*y+x)];
aa[x] =out_img[(OX*y+x)] + aa[x];
// printf("%d\n",aa[x]);
}
printf("%d\n",aa[x]);
}
/****** Save Image ******/
printf("Save...\n");
if((fp = fopen("copyimg.pgm", "wb")) == NULL) {
printf("file open error !\n");
}
fprintf(fp, "P5\n");
fprintf(fp, "%d %d\n", OX, OY);
fprintf(fp, "255\n");
if(fwrite(out_img, OX*OY, 1, fp) != 1) {
printf("file write error !\n");
}
fclose(fp);
printf("Save2...\n");
if((fp = fopen("pgmdata.txt", "w")) == NULL) {
printf("file open error !\n");
}
for(x=0; x<IX; x++){
fprintf(fp,"%d\n",aa[x]); }
//if(fwrite(aa, IX, 1, fp) != 1) {
// printf("file write error !\n");
// }
fclose(fp);
}
コメント分が飛ばせない
Re:コメント分が飛ばせない
かわりに、インデントしておきました。
gnu indent しただけに近いですが。
gnu indent しただけに近いですが。
#include <stdio.h> #include <stdlib.h> #include <string.h> #define IX 640 // horizontal resolution of input image #define IY 480 // vertical resolution of input image #define OX 640 // horizontal resolution of output image #define OY 480 // vertical resolution of output image unsigned char in_img[IX * IY]; // input image unsigned char out_img[OX * OY]; // output image int aa[IX]; main() { int ix, iy, lev, x, y, k; char fname[256]; FILE *fp; char str[256]; /****** Load Image ******/ printf("Input filename = "); scanf("%s", fname); printf("Load...\n"); if ((fp = fopen(fname, "rb")) == NULL) { printf("file open error !\n"); } do { fscanf(fp, "%s\n", str); } while (str[0] == '#'); if (strncmp(str, "P5", 2) != 0) { printf("image type is not P5\n"); } do { fscanf(fp, "%s\n", str); } while (str[0] == '#'); sscanf(str, "%d", &ix); fscanf(fp, "%d\n", &iy); if ((ix != IX) || (iy != IY)) { printf("image size is not %d X %d\n", IX, IY); } do { fscanf(fp, "%s\n", str); } while (str[0] == '#'); sscanf(str, "%d", &lev); if (lev != 255) { printf("image graylevel not 255\n"); } if (fread(in_img, IX * IY, 1, fp) != 1) { printf("file read error !\n"); } fclose(fp); /****** Process Image ******/ printf("Process...\n"); for (x = 0; x < OX; ++x) { for (y = 0; y < OY; ++y) { out_img[(OX * y + x)] = in_img[(IX * y + x)]; aa[x] = out_img[(OX * y + x)] + aa[x]; // printf("%d\n",aa[x]); } printf("%d\n", aa[x]); } /****** Save Image ******/ printf("Save...\n"); if ((fp = fopen("copyimg.pgm", "wb")) == NULL) { printf("file open error !\n"); } fprintf(fp, "P5\n"); fprintf(fp, "%d %d\n", OX, OY); fprintf(fp, "255\n"); if (fwrite(out_img, OX * OY, 1, fp) != 1) { printf("file write error !\n"); } fclose(fp); printf("Save2...\n"); if ((fp = fopen("pgmdata.txt", "w")) == NULL) { printf("file open error !\n"); } for (x = 0; x < IX; x++) { fprintf(fp, "%d\n", aa[x]); } // if(fwrite(aa, IX, 1, fp) != 1) { // printf("file write error !\n"); // } fclose(fp); }
Re:コメント分が飛ばせない
do { fscanf(fp, "%s\n", str); } while (str[0] == '#');おそらく#の後に空白があるために、予想と異なる動作になったのでしょう。
fscanf の後で、str をプリントすれば、何を言いたいかのか分かると思います。
Re:コメント分が飛ばせない
なるほど。
確かに自分の使った絵のコメントの#の後にも空白がありました。
ですが、このような場合どうすれば空白を含めてコメントを飛ばすことができるでしょうか?
知恵を貸してください!
確かに自分の使った絵のコメントの#の後にも空白がありました。
ですが、このような場合どうすれば空白を含めてコメントを飛ばすことができるでしょうか?
知恵を貸してください!