/* Input file : *.bmp */ /* Output file : *-sp_l.bmp */ /* This program was made introducing the programs */ /* appeared in the book, "C gengo de manabu jissen */ /* gazou shori," S.Inoue(eds.),Ohmsha:TOKYO,2002, */ /* by Shusaku Nomura,2006, Shojiro Tanaka, 2007 */ #include #include #pragma pack(push,1) #include #define PI 3.141592 typedef struct tagBITMAPFILEHEADER{ short bfType; long bfSize; short bfReserved1; short bfReserved2; long bfOffBits; } BITMAPFILEHEADER; typedef struct tagBITMAPINFOHEADER{ long biSize; long biWidth; long biHeight; short biPlanes; short biBitCount; long biCompression; long biSizeImage; long biXPelsPerMeter; long biYPelsPerMeter; long biClrUsed; long biClrImportant; } BITMAPINFOHEADER; typedef struct tagRGBTRIPLE { unsigned char RGB[3];/*0:blue, 1:Green, 2:Red*/ } RGBTRIPLE; RGBTRIPLE* readBMP(char filename[256],BITMAPFILEHEADER *bfile,BITMAPINFOHEADER *binfo){ FILE *fp; RGBTRIPLE *RGB; char d,buf[4]; int i; if((fp=fopen(filename,"rb"))==NULL){ fprintf(stderr,"file open error:%s\n",filename); return NULL; } fread(bfile,sizeof(*bfile),1,fp); fread(binfo,sizeof(*binfo),1,fp); if(bfile->bfType != 0x4d42){ fprintf(stderr,"error:file type\n"); return NULL; } if(binfo->biBitCount != 24){ fprintf(stderr,"error:bit Count\n"); return NULL; } if((d=((binfo->biWidth*3)%4))==0) d=0; else d=4-d; RGB =(RGBTRIPLE*)malloc(sizeof(RGBTRIPLE)*binfo->biWidth*binfo->biHeight); if(RGB==NULL){ fprintf(stderr,"--memory allocation error !\n"); return NULL; } for(i=0;ibiHeight;i++){ fread(RGB+binfo->biWidth*i,sizeof(RGBTRIPLE),binfo->biWidth,fp); fread(buf,1,d,fp); } fclose(fp); return RGB; } int writeBMP(char filename[256],BITMAPFILEHEADER bfile,BITMAPINFOHEADER binfo,RGBTRIPLE *RGB){ FILE *fp; char d,buf[4]={0,0,0,0}; int i; if(RGB == NULL){ return 0; } /*ファイルの書き出し*/ if((fp=fopen(filename,"wb"))==NULL){ fprintf(stderr,"file open error:%s\n",filename); return 0; } if((d=(binfo.biWidth*3)%4)==0) d=0; else d=4-d; fwrite(&bfile,sizeof(bfile),1,fp); fwrite(&binfo,sizeof(binfo),1,fp); for(i=0;i=0) m=(int)(y); else m=(int)(y-1); if(x>=0) n=(int)(x); else n=(int)(x-1); q=y-m; p=x-n; if(p==1){ p=0;n=n+1; } if((m+1+cy)>=(binfo.biHeight)) m=m-1; if((n+1+cx)>=(binfo.biWidth)) n=n-1; for(k=0;k<3;k++){ if((0<=(m+cy))&&((m+cy)255) d=255; if(flag==0 || back==1) RGB_out[binfo.biWidth*(i+cy)+(j+cx)].RGB[k]=d; else RGB_out[binfo.biWidth*(i+cy)+(j+cx)].RGB[k]=0; } } } return RGB_out; } int main(){ char str[256],filename[256]; BITMAPFILEHEADER bfile; BITMAPINFOHEADER binfo; RGBTRIPLE *RGB_in,*RGB_out; float r; int x,y; puts("input filename"); scanf("%s",str); sprintf(filename,"%s.bmp\0",str); if((RGB_in=readBMP(filename,&bfile,&binfo))==NULL) return 0; printf("Input:%s width:%d,height:%d\n",filename,binfo.biWidth,binfo.biHeight); printf("center = x,y ?:"); scanf("%d %d",&x,&y); printf("radius: (float) ?:"); scanf("%f",&r); RGB_out=sphere(binfo,RGB_in,x,y,r,0); sprintf(filename,"%s-sp_l.bmp\0",str); printf("Output:%s \n",filename); writeBMP(filename,bfile,binfo,RGB_out); free(RGB_in); free(RGB_out); return 0; }