69,373
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
FILE *fp;
int i,j,k,el;
float sum, s;
char buf1[128],buf2[128],buf3[128],buf4[128];
int Height,Width,depth,DstHeight,DstWidth;
int SrcCX,SrcCY,DstCX,DstCY;
int x1,y1,x2,y2,x,y,dx,dy;
int int_sin,int_cos;
double A = 0.15;
double Sin=sin(A);
double Cos=cos(A);
unsigned char pluTBL[256*256];
unsigned char Top; //上端1次補間結果
unsigned char Bottom;
unsigned char** dens, **image;
fp= fopen("test4.pgm","rb");
if (fp==NULL){
printf("error!!");
}
fgets(buf1,128,fp);
puts(buf1);
fgets(buf2,128,fp);
puts(buf2);
sscanf(buf2,"%d",&Width);
fgets(buf3,128,fp);
puts(buf3);
sscanf(buf3,"%d",&Height);
fgets(buf4,128,fp);
puts(buf4);
sscanf(buf4,"%d",&depth);
dens = (unsigned char**) malloc(sizeof(unsigned char**)*Height);
dens[0]= (unsigned char*) malloc(sizeof(unsigned char*)*Height*Width);
for (i=0;i<Height;i++){
dens[i]= dens[0]+i*Width;
}
for (i=0;i<Height;i++){
for (j=0;j<Width;j++){
dens[i][j]= fgetc(fp);
}
}
for (i=0;i<20;i++) {
for (j=0;j<20;j++){
printf(" %d",dens[i][j]);
}
printf("\n");
}
DstWidth=(int)(fabs(Width*Cos)+fabs(Height*Sin)+0.5);
DstHeight=(int)(fabs(Width*Sin)+fabs(Height*Cos)+0.5);
printf(" %d,,,%d",DstWidth,DstHeight);
image= (unsigned char**) malloc(sizeof(unsigned char**)*DstHeight);
image[0]= (unsigned char*)malloc(sizeof(unsigned char*)*DstHeight*DstWidth);
for (i=0;i<DstHeight;i++){
image[i]= image[0]+i*DstWidth;
}
for (i=0;i<DstHeight;i++){
for (j=0;j<DstWidth;j++){
image[i][j]= 0;
}
}
for (i=0;i<20;i++) {
for (j=0;j<20;j++){
printf(" %d",image[i][j]);
}
printf("\n");
}
fclose(fp);
//--------------
SrcCX = Width / 2;
SrcCY = Height / 2;
DstCX = DstWidth / 2;
DstCY = DstHeight / 2;
printf(" %d,,,%d,,,%d,,,%d",SrcCX,SrcCY,DstCX,DstCY);
int_cos = (int)(Cos * 1024);
int_sin = (int)(Sin * 1024);
printf(" %d,,,%d",int_cos,int_sin);
for(y2=0; y2 < DstHeight; y2++) {
for(x2=0; x2 < DstWidth; x2++) {
// x = ((x2-DstCX) * int_cos - (y2-DstCY) * int_sin) + (SrcCX << 10);
// y = ((x2-DstCX) * int_sin + (y2-DstCY) * int_cos) + (SrcCY << 10);
//入力画像位置を計算
// x1 = x >> 10;
// y1 = y >> 10;
x1 = (((x2-DstCX)*int_cos - (y2-DstCY)*int_sin) >> 10) + SrcCX;
y1 = (((x2-DstCX)*int_sin + (y2-DstCY)*int_cos) >> 10) + SrcCY;
//printf(" %d,,,%d",x1,y1);
if( x1 >= 0 && x1 <Width-1 && y1 >= 0 && y1 < Height-1 ) {
//dx = (x - (x & 0xfffffc00)) >> 2;
// dy = (y - (y & 0xfffffc00)) >> 2;
// Top = pluTBL[(dens[y1][x1]<<8)+(255-dx)] + pluTBL[(dens[y1][x1+1]<<8)+dx];
// Bottom = pluTBL[(dens[y1+1][x1]<<8)+(255-dx)] + pluTBL[(dens[y1+1][x1+1]<<8)+dx];
// image[y2][x2] = pluTBL[(Top<<8)+(255-dy)] + pluTBL[(Bottom<<8)+dy];
image[y2][x2] = dens[y1][x1];
}
}
}
for (i=35;i<80;i++) {
for (j=35;j<80;j++){
printf(" %d",image[i][j]);
}
printf("\n");
}
free(dens);
fp= fopen("out.pgm","wb");
if (fp==NULL){
printf("error!!");
exit(0);
}
fputs(buf1,fp);//puts(buf1);
fputs(buf2,fp);//puts(buf2);
fputs(buf3,fp);//puts(buf3);
fputs(buf4,fp);//puts(buf4);
for (i=0;i<DstHeight;i++){
for (j=0;j<DstWidth;j++){
fputc(image[i][j],fp);
}
}
fclose(fp);
printf("image %d %d\n", DstHeight,DstWidth);
free(image);
}