コメント分が飛ばせない
Posted: 2007年6月03日(日) 16:22
お世話になります。
バイナリ形式の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);
}
バイナリ形式の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);
}