#include #include #include #include #include template int ReadImage(char* fileName,SquareGrid& red,SquareGrid& green,SquareGrid& blue){ char* ext=GetFileExtension(fileName); Image32 img; if(!strcasecmp(ext,"bmp")){BMPReadImage(fileName,img);} else if(!strcasecmp(ext,"jpg") || !strcasecmp(ext,"jpeg")){JPEGReadImage(fileName,img);} else{ fprintf(stderr,"Unsupported image extension: %s\n",ext); delete[] ext; return 0; } delete[] ext; int w,h,r; w=img.width(); h=img.height(); r=(w>h)?w:h; red.resize(r); green.resize(r); blue.resize(r); for(int i=0;i int WriteImage(SquareGrid& red,SquareGrid& green,SquareGrid& blue,char* fileName){ if(red.resolution()!=green.resolution() || red.resolution()!=blue.resolution()){ fprintf(stderr,"Resolutions differ: red[%d] green[%d] blue[%d]\n",red.resolution(),green.resolution(),blue.resolution()); return 0; } Image32 img; int r=red.resolution(); img.setSize(r,r); for(int i=0;i1){r=1;} if(g<0){g=0;} if(g>1){g=1;} if(b<0){b=0;} if(b>1){b=1;} p.r=(unsigned char)(r*255.0); p.g=(unsigned char)(g*255.0); p.b=(unsigned char)(b*255.0); img(i,j)=p; } } char* ext=GetFileExtension(fileName); if(!strcasecmp(ext,"bmp")){BMPWriteImage(img,fileName);} else if(!strcasecmp(ext,"jpg") || !strcasecmp(ext,"jpeg")){JPEGWriteImage(img,fileName);} else{ fprintf(stderr,"Unsupported image extension: %s\n",ext); delete[] ext; return 0; } delete[] ext; return 1; } int main(int argc,char* argv[]){ // Read the parameters in from the command line cmdLineString In,Pattern,Out; char* paramNames[]={"in","out","pattern"}; cmdLineReadable* params[]={&In,&Out,&Pattern}; cmdLineParse(argc-1,&argv[1],paramNames,sizeof(paramNames)/sizeof(char*),params); if(!In.set || !Out.set || !Pattern.set){ fprintf(stderr,"Usage %s:\n",argv[0]); fprintf(stderr,"\t--pattern \n"); fprintf(stderr,"\t--in \n"); fprintf(stderr,"\t--out \n"); return EXIT_FAILURE; } int res,pRes; SquareGrid<> input[3],pattern[3],mask; // Read in the red, green, and blue components of the image if(!ReadImage(In.value,input[0],input[1],input[2])){return EXIT_FAILURE;} res=input[0].resolution(); // Read in the red, green, and blue components of the pattern SquareGrid<> temp[3]; if(!ReadImage(Pattern.value,temp[0],temp[1],temp[2])){return EXIT_FAILURE;} pRes=temp[0].resolution(); // Check that the pattern is smaller than the image if(res<=pRes){ fprintf(stderr,"Pattern must be smaller than image: %d < %d\n",pRes,res); return EXIT_FAILURE; } // Copy the pattern to a re-centered, zero-padded image, and set the mask pattern[0].resize(res); pattern[1].resize(res); pattern[2].resize(res); mask.resize(res); for(int i=0;i