とらりもん - C言語による画像処理: 画像の右90度回転 Diff
- Added parts are displayed like this.
- Deleted parts are displayed
like this.
前章の画像を, 右回りに90度、回転してみましょう。
これまではrawバイナリーの入出力でしたが, ヘッダーの入出力もプログラムに組み込み, PPM型式の入出力にしてみましょう。 プログラムは以下のようになります:
/* rotR.c
* reverse rotation to the right of an image
* usage: $ ./rotR < source_image.ppm > destin_image.ppm
* compile: $ gcc rotR.c -o rotR
* 2011/05/15 Kenlo Nishida
*/
# include <stdio.h>
# define XL 800
# define YL 600
# define BPP 3
int linescan(FILE *fp, char *buff)
{int in=0, i=0;
while((in=fgetc(fp))!='\n' && feof(fp)==0)
{buff[i]=in;
i++;}
buff[i]=0;
return(feof(fp));
}
int main()
{int xi, yi, xo, yo;
char head[255];
unsigned char data[BPP*XL*YL];
linescan(stdin, head);
printf("%s\n", head);
linescan(stdin, head);
printf("%s\n", head);
linescan(stdin, head);
printf("600 800\n");
linescan(stdin, head);
printf("%s\n", head);
fread(data, BPP, XL*YL, stdin);
for (yo=0; yo<XL; yo++)
for (xo=0; xo<YL; xo++)
{xi=yo;
yi=YL-1-xo;
fwrite(&data[BPP*(XL*yi+xi)], BPP, 1, stdout);
}
}
課題1: これを, 以下のようにコンパイルして実行してみて下さい:
$ gcc rotR.c -o rotR
$ ./rotR < lake.ppm > lake_rotR.ppm
$ display lake_rotR.ppm
{{attach_view(lake_rotR.jpg)}}
↑lake_rotR.ppm
課題2: 画像lake.ppmを, 左回りに90度, 回転してみよ。
→ [[画像解析入門]]に戻る!
これまではrawバイナリーの入出力でしたが, ヘッダーの入出力もプログラムに組み込み, PPM型式の入出力にしてみましょう。 プログラムは以下のようになります:
/* rotR.c
* reverse rotation to the right of an image
* usage: $ ./rotR < source_image.ppm > destin_image.ppm
* compile: $ gcc rotR.c -o rotR
* 2011/05/15 Kenlo Nishida
*/
# include <stdio.h>
# define XL 800
# define YL 600
# define BPP 3
int linescan(FILE *fp, char *buff)
{int in=0, i=0;
while((in=fgetc(fp))!='\n' && feof(fp)==0)
{buff[i]=in;
i++;}
buff[i]=0;
return(feof(fp));
}
int main()
{int xi, yi, xo, yo;
char head[255];
unsigned char data[BPP*XL*YL];
linescan(stdin, head);
printf("%s\n", head);
linescan(stdin, head);
printf("%s\n", head);
linescan(stdin, head);
printf("600 800\n");
linescan(stdin, head);
printf("%s\n", head);
fread(data, BPP, XL*YL, stdin);
for (yo=0; yo<XL; yo++)
for (xo=0; xo<YL; xo++)
{xi=yo;
yi=YL-1-xo;
fwrite(&data[BPP*(XL*yi+xi)], BPP, 1, stdout);
}
}
課題1: これを, 以下のようにコンパイルして実行してみて下さい:
$ gcc rotR.c -o rotR
$ ./rotR < lake.ppm > lake_rotR.ppm
$ display lake_rotR.ppm
{{attach_view(lake_rotR.jpg)}}
↑lake_rotR.ppm
課題2: 画像lake.ppmを, 左回りに90度, 回転してみよ。
→ [[画像解析入門]]に戻る!