static Image* psdrawpage(Document *d, int page) { PSInfo *ps = d->extra; Image *im; if(ps->clueless) return readimage(display, ps->gsdfd, 0); waitgs(ps); if(goodps) pswritepage(d, ps->gsfd, page); else { pswritepage(d, ps->gsfd, -1); pswritepage(d, ps->gsfd, page); pswritepage(d, ps->gsfd, d->npage); } /* * If last line terminator is \r, gs will read ahead to check for \n * so send one to avoid deadlock. */ write(ps->gsfd, "\n", 1); im = readimage(display, ps->gsdfd, 0); if(im == nil) { fprint(2, "fatal: readimage error %r\n"); wexits("readimage"); } waitgs(ps); return im; }
int p3b(void) { // initialize data of input const char *inputname = "boat.raw"; int SizeW = 512; int SizeH = 512; int BytesPerPixel = 1; unsigned char inputdata[SizeH][SizeW][BytesPerPixel]; // initialize data of output const char *outputname = "3b boat_ED_ans.raw"; int newSizeW = SizeW; int newSizeH = SizeH; int newBytesPerPixel = BytesPerPixel; unsigned char outputdata[newSizeH][newSizeW][newBytesPerPixel]; // read input readimage(inputname, &inputdata[0][0][0], SizeH, SizeW, BytesPerPixel); // Error Diffusion int color = 255; FSErrorDiff(color, SizeH, SizeW, &inputdata[0][0][0], &outputdata[0][0][0]); writeimage(outputname, &outputdata[0][0][0], newSizeH, newSizeW, newBytesPerPixel); return 0; }
int main() { readimage(); horizontalruns(); verticalruns(); lrdilate(); tbdilate(); rldilate(); btdilate(); unsigned x, y; printf("P2\n%u %u\n255\n", w, h); for (y = 0; y < h; y++) for (x = 0; x < w; x++) { #if 0 printf("%d ", f[y][x]); continue; #endif if (f[y][x] == 7) printf("%d ", 255); else #if 0 printf("%d ", (7 & f[y][x]) << 4); #else printf("%d ", 128 - ((1 & f[y][x]) << 6)); #endif } }
int main(int argc, char *argv[]) { char *src_path,*dst_path; int row=0,col=0,max_value=255; char* loc;//int i,j; // <a.out> <src_file> <destination_file> if (argc != 3) { printf("Usage <a.out> <src_file> <destination_file> \n"); return -1; } src_path=argv[1]; dst_path=argv[2]; unsigned int **new_image = readimage(src_path,&row,&col,&max_value,&loc); new_image=contrast_stretch(new_image,&row,&col,max_value); writeimage(new_image,row,col,dst_path,max_value); return 0; }
void archive(GtkWidget *entrada) { name_archive = gtk_entry_get_text(GTK_ENTRY(entrada)); strcpy(archive_open, name_archive); strcat(archive_open, ".ppm"); readimage(&img, archive_open); }
int main(void) { // make and save IMAGE *img=makeimage(500,800); saveimage(img,"image3.ppm"); freeimage(img); // read and save img=readimage("image3.ppm"); image2hsv(img); saveimage(img,"image3c.ppm"); return 0; }
Image * eloadfile(char *path) { Image *img; int fd; fd = open(path, OREAD); if(fd < 0) { fprint(2, "cannot open image file %s: %r\n", path); exits("image"); } img = readimage(display, fd, 0); if(img == nil) sysfatal("cannot load image: %r"); close(fd); return img; }
int p3a(void) { // initialize data of input const char *inputname = "boat.raw"; int SizeW = 512; int SizeH = 512; int BytesPerPixel = 1; unsigned char inputdata[SizeH][SizeW][BytesPerPixel]; // initialize data of output const char *outputname1 = "3a boat_FT_ans.raw"; const char *outputname2 = "3a boat_RT_ans.raw"; const char *outputname3a = "3a boat_DM2_ans.raw"; const char *outputname3b = "3a boat_DM4_ans.raw"; int newSizeW = SizeW; int newSizeH = SizeH; int newBytesPerPixel = BytesPerPixel; unsigned char outputdata1[newSizeH][newSizeW][newBytesPerPixel]; unsigned char outputdata2[newSizeH][newSizeW][newBytesPerPixel]; unsigned char outputdata3a[newSizeH][newSizeW][newBytesPerPixel]; unsigned char outputdata3b[newSizeH][newSizeW][newBytesPerPixel]; // read input readimage(inputname, &inputdata[0][0][0], SizeH, SizeW, BytesPerPixel); //*** Fixed thresholding *** float FixedT = 128.0; Fixedthre(FixedT, SizeH, SizeW, &inputdata[0][0][0], &outputdata1[0][0][0]); writeimage(outputname1, &outputdata1[0][0][0], newSizeH, newSizeW, newBytesPerPixel); //*** Random thresholding *** RandomThre(SizeH, SizeW, &inputdata[0][0][0], &outputdata2[0][0][0]); writeimage(outputname2, &outputdata2[0][0][0], newSizeH, newSizeW, newBytesPerPixel); //*** Dithering Matrix Dithering(2, SizeH, SizeW, &inputdata[0][0][0], &outputdata3a[0][0][0]); Dithering(4, SizeH, SizeW, &inputdata[0][0][0], &outputdata3b[0][0][0]); writeimage(outputname3a, &outputdata3a[0][0][0], newSizeH, newSizeW, newBytesPerPixel); writeimage(outputname3b, &outputdata3b[0][0][0], newSizeH, newSizeW, newBytesPerPixel); return 0; }
int p1b(void) { // initialize data of input const char *filename = "Scarlett.raw"; int SizeW = 400; int SizeH = 300; int BytesPerPixel = 3; unsigned char ImageData[SizeH][SizeW][BytesPerPixel]; unsigned char R[SizeH][SizeW]; unsigned char G[SizeH][SizeW]; unsigned char B[SizeH][SizeW]; double dImageData[SizeH][SizeW][BytesPerPixel]; // initialize data of output const char *BGfilename = "1b Scarlett_lab_bf.raw"; const char *BGfilename1 = "1b Scarlett_rgb_bf.raw"; const char *Exfilename = "1b Scarlett_ex.raw"; const char *newfilename = "1b Scarlett_ans.raw"; int newSizeW = SizeW; int newSizeH = SizeH; int newBytesPerPixel = BytesPerPixel; double MBGImageData[newSizeH][newSizeW][newBytesPerPixel]; double MCImageData[newSizeH][newSizeW][newBytesPerPixel]; unsigned char BGImageData[newSizeH][newSizeW][newBytesPerPixel]; unsigned char BGImageData1[newSizeH][newSizeW][newBytesPerPixel]; unsigned char CBGImageData[newSizeH][newSizeW][newBytesPerPixel]; unsigned char newImageData[newSizeH][newSizeW][newBytesPerPixel]; // input readimage(filename, &ImageData[0][0][0], SizeH, SizeW, BytesPerPixel); double L[SizeH][SizeW]; double a[SizeH][SizeW]; double b[SizeH][SizeW]; for(int i = 0; i<SizeH; i++) { for(int j = 0; j < SizeW; j++) { R[i][j] = ImageData[i][j][0]; G[i][j] = ImageData[i][j][1]; B[i][j] = ImageData[i][j][2]; dImageData[i][j][0] = ((double) ImageData[i][j][0]/255.0); dImageData[i][j][1] = ((double) ImageData[i][j][1]/255.0); dImageData[i][j][2] = ((double) ImageData[i][j][2]/255.0); Rgb2Lab(&L[i][j], &a[i][j], &b[i][j], dImageData[i][j][0], dImageData[i][j][1], dImageData[i][j][2]); // printf("%d, %f = %u, %f = %u, %f = %u\n", i,dImageData[i][j][0], ImageData[i][j][0], dImageData[i][j][1],ImageData[i][j][1], dImageData[i][j][2], ImageData[i][j][2]); // printf("%d, %f, %f, %f\n", i,L[i][j], a[i][j],b[i][j]); } } // Bilateral Filter double newL[SizeH][SizeW]; double newa[SizeH][SizeW]; double newb[SizeH][SizeW]; double newnewL[SizeH][SizeW]; double newnewa[SizeH][SizeW]; double newnewb[SizeH][SizeW]; unsigned char newR[SizeH][SizeW]; unsigned char newG[SizeH][SizeW]; unsigned char newB[SizeH][SizeW]; unsigned char newnewR[SizeH][SizeW]; unsigned char newnewG[SizeH][SizeW]; unsigned char newnewB[SizeH][SizeW]; int FilterSize = 5; int sigmaC = 3; int sigmaC1 = 10; double sigmaS = 4.25; double sigmaS1 = 10.0; BilateralFilter(FilterSize, sigmaC1, sigmaS1, &R[0][0], SizeH, SizeW, &newR[0][0]); BilateralFilter(FilterSize, sigmaC1, sigmaS1, &G[0][0], SizeH, SizeW, &newG[0][0]); BilateralFilter(FilterSize, sigmaC1, sigmaS1, &B[0][0], SizeH, SizeW, &newB[0][0]); BFiforLab(FilterSize, sigmaC, sigmaS, &L[0][0], SizeH, SizeW, &newL[0][0]); BFiforLab(FilterSize, sigmaC, sigmaS, &a[0][0], SizeH, SizeW, &newa[0][0]); BFiforLab(FilterSize, sigmaC, sigmaS, &b[0][0], SizeH, SizeW, &newb[0][0]); for(int i = 0; i<SizeH; i++) { for(int j = 0; j < SizeW; j++) { Lab2Rgb(&MCImageData[i][j][0], &MCImageData[i][j][1], &MCImageData[i][j][2], newL[i][j], newa[i][j], newb[i][j]); CBGImageData[i][j][0] = (int) (MCImageData[i][j][0]*255); CBGImageData[i][j][1] = (int) (MCImageData[i][j][1]*255); CBGImageData[i][j][2] = (int) (MCImageData[i][j][2]*255); } } BilateralFilter(FilterSize, sigmaC1, sigmaS1, &newR[0][0], SizeH, SizeW, &newnewR[0][0]); BilateralFilter(FilterSize, sigmaC1, sigmaS1, &newG[0][0], SizeH, SizeW, &newnewG[0][0]); BilateralFilter(FilterSize, sigmaC1, sigmaS1, &newB[0][0], SizeH, SizeW, &newnewB[0][0]); BFiforLab(FilterSize, sigmaC, sigmaS, &newL[0][0], SizeH, SizeW, &newnewL[0][0]); BFiforLab(FilterSize, sigmaC, sigmaS, &newa[0][0], SizeH, SizeW, &newnewa[0][0]); BFiforLab(FilterSize, sigmaC, sigmaS, &newb[0][0], SizeH, SizeW, &newnewb[0][0]); BilateralFilter(FilterSize, sigmaC1, sigmaS1, &newnewR[0][0], SizeH, SizeW, &newR[0][0]); BilateralFilter(FilterSize, sigmaC1, sigmaS1, &newnewG[0][0], SizeH, SizeW, &newG[0][0]); BilateralFilter(FilterSize, sigmaC1, sigmaS1, &newnewB[0][0], SizeH, SizeW, &newB[0][0]); BFiforLab(FilterSize, sigmaC, sigmaS, &newnewL[0][0], SizeH, SizeW, &newL[0][0]); BFiforLab(FilterSize, sigmaC, sigmaS, &newnewa[0][0], SizeH, SizeW, &newa[0][0]); BFiforLab(FilterSize, sigmaC, sigmaS, &newnewb[0][0], SizeH, SizeW, &newb[0][0]); // BilateralFilter(FilterSize, sigmaC1, sigmaS1, &newR[0][0], SizeH, SizeW, &newnewR[0][0]); // BilateralFilter(FilterSize, sigmaC1, sigmaS1, &newG[0][0], SizeH, SizeW, &newnewG[0][0]); // BilateralFilter(FilterSize, sigmaC1, sigmaS1, &newB[0][0], SizeH, SizeW, &newnewB[0][0]); for(int i = 0; i<SizeH; i++) { for(int j = 0; j < SizeW; j++) { Lab2Rgb(&MBGImageData[i][j][0], &MBGImageData[i][j][1], &MBGImageData[i][j][2], newL[i][j], newa[i][j], newb[i][j]); BGImageData[i][j][0] = (int) (MBGImageData[i][j][0]*255); BGImageData[i][j][1] = (int) (MBGImageData[i][j][1]*255); BGImageData[i][j][2] = (int) (MBGImageData[i][j][2]*255); BGImageData1[i][j][0] = newR[i][j]; BGImageData1[i][j][1] = newG[i][j]; BGImageData1[i][j][2] = newB[i][j]; // printf("%d, %f, %f, %f\n", i,newL[i][j], newa[i][j],newb[i][j]); // printf("%d, %u = %f, %u = %f, %u = %f\n", i,newImageData[i][j][0], MImageData[i][j][0],newImageData[i][j][1], MImageData[i][j][1], newImageData[i][j][2], MImageData[i][j][2]); } } writeimage(BGfilename, &BGImageData[0][0][0], newSizeH, newSizeW, newBytesPerPixel); writeimage(BGfilename1, &BGImageData1[0][0][0], newSizeH, newSizeW, newBytesPerPixel); // XDoG // convert to gray-scale image unsigned char GrayImageData[newSizeH][newSizeW]; for(int i = 0; i<SizeH; i++) { for(int j = 0; j < SizeW; j++) { float Y = 0.21*CBGImageData[i][j][0] + 0.72*CBGImageData[i][j][1] +0.07*CBGImageData[i][j][2]; GrayImageData[i][j] = (int) Y; // printf("%d %u = %f\n", i, GrayImageData[i][j], Y); } } unsigned char GrayImageData0[newSizeH][newSizeW]; for(int i = 0; i<SizeH; i++) { for(int j = 0; j < SizeW; j++) { float Y = 0.21*ImageData[i][j][0] + 0.72*ImageData[i][j][1] +0.07*ImageData[i][j][2]; GrayImageData0[i][j] = (int) Y; } } //compute Ex int windowsize = 11; float sigma = 1.5; float k = sqrt(1.6); float t = 0.98, e = 0.5, f = 4; unsigned char ExImageData[newSizeH][newSizeW]; XDoG(windowsize, sigma, k, t, e, f, &GrayImageData[0][0], &ExImageData[0][0], SizeH, SizeW); writeimage(Exfilename, &ExImageData[0][0], newSizeH, newSizeW, 1); // Integration of Portrait Background and Contour for(int i=0; i<SizeH; i++) { for(int j=0; j<SizeW; j++) { newImageData[i][j][0] = (ExImageData[i][j]/255) * BGImageData[i][j][0]; newImageData[i][j][1] = (ExImageData[i][j]/255) * BGImageData[i][j][1]; newImageData[i][j][2] = (ExImageData[i][j]/255) * BGImageData[i][j][2]; } } writeimage(newfilename, &newImageData[0][0][0], newSizeH, newSizeW, newBytesPerPixel); // DoG unsigned char DoGExImageData[newSizeH][newSizeW]; unsigned char DoGImageData[newSizeH][newSizeW][newBytesPerPixel]; DoG(windowsize, sigma, k, e, f, &GrayImageData[0][0], &DoGExImageData[0][0], SizeH, SizeW); writeimage("1b Scarlett_DoG_ex.raw", &DoGExImageData[0][0], newSizeH, newSizeW, 1); for(int i=0; i<SizeH; i++) { for(int j=0; j<SizeW; j++) { DoGImageData[i][j][0] = (DoGExImageData[i][j]/255) * BGImageData[i][j][0]; DoGImageData[i][j][1] = (DoGExImageData[i][j]/255) * BGImageData[i][j][1]; DoGImageData[i][j][2] = (DoGExImageData[i][j]/255) * BGImageData[i][j][2]; } } writeimage("1b Scarlett_DoG_ans.raw", &DoGImageData[0][0][0], newSizeH, newSizeW, newBytesPerPixel); return 0; }
void main(int argc, char **argv) { int row, col, row_size, col_size; char input_image[512], qimage[512]; unsigned char **imgin; int *bound, *reco, input, label, output, numlev, numbits, c; int minvalue, maxvalue, range, level, stepsize, temp, end_flag; FILE *ofp; extern int optind; extern char *optarg; fprintf(stderr,"\n\n\t\tUniform Quantization of Images - Encoder\n"); ofp = stdout; strcpy(qimage,"standard out"); numlev = -1; numbits = -1; minvalue = 0; maxvalue = 255; while((c=getopt(argc,argv,"i:o:l:b:m:t:h"))!=EOF) { switch (c){ case 'i': strcpy(input_image,optarg); break; case 'o': strcpy(qimage,optarg); ofp = fopen(optarg,"wb"); break; case 'l': sscanf(optarg,"%d", &numlev); break; case 'b': sscanf(optarg,"%d", &numbits); break; case 'm': sscanf(optarg,"%d", &maxvalue); break; case 't': sscanf(optarg,"%d", &minvalue); break; case 'x': sscanf(optarg,"%d", &row_size); break; case 'y': sscanf(optarg,"%d", &col_size); break; case 'h': usage(); exit(1); } } if(numlev > 0 && numbits > 0) { temp = (int) pow((double) 2,(double) numbits); if(temp != numlev) { fprintf(stderr,"\n You have entered values for the number of levels and\n"); fprintf(stderr,"number of bits that are not compatible. The number of\n"); fprintf(stderr,"levels should be 2^(number of bits). If you want to use\n"); fprintf(stderr,"a number of levels which is not a power of 2 then only\n"); fprintf(stderr,"enter a value for the number of levels.\n"); exit(1); } } if(numlev < 0 && numbits < 0) { fprintf(stderr,"\n Enter number of bits per pixel: "); scanf("%d",&numbits); numlev = (int) pow((double) 2,(double) numbits); } if (numbits <= 0 || numbits >= 8) { fprintf(stderr,"\n You have entered values for the number of bits that\n"); fprintf(stderr,"are not compatible. The number of bits should between\n"); fprintf(stderr,"1 (one) - 7 (seven).\n"); exit(1); } if(numlev < 0 && numbits > 0) numlev = (int) pow((double) 2,(double) numbits); if(numlev > 0 && numbits < 0) numbits = (int) (log((double)numlev)/log((double) 2.) + 0.99999); /* Determine range, and stepsize for the quantizer */ range = maxvalue - minvalue + 1; stepsize = range/(numlev); /* Allocate space for the boundary values */ bound = (int *) calloc((numlev+1),sizeof(int)); /* Construct the boundary tables */ bound[0] = minvalue; for(level=1; level<=numlev; level++) { bound[level] = bound[level-1] + stepsize; } /* Find out how large the image is */ image_size(input_image, &row_size, &col_size); /* Allocate space for input image */ imgin = (unsigned char **) calloc(row_size,sizeof(char *)); for(row=0; row<row_size; row++) imgin[row] = (unsigned char *) calloc((col_size),sizeof(char)); /* Read the input image */ readimage(input_image, imgin, row_size, col_size); /* Store coding parameters in the output file */ fwrite(&numlev,1,sizeof(int),ofp); fwrite(&numbits,1,sizeof(int),ofp); fwrite(&maxvalue,1,sizeof(int),ofp); fwrite(&minvalue,1,sizeof(int),ofp); fwrite(&row_size,1,sizeof(int),ofp); fwrite(&col_size,1,sizeof(int),ofp); struct Queue* code_write; code_write = (struct Queue*) malloc(sizeof(struct Queue)); code_write->front = NULL; code_write->rear = NULL; code_write->size = 0; // Print(&(code_write)->front); /* encode each pixel into an integer label and store */ end_flag = 0; for(row=0; row<row_size; row++) for(col=0; col<col_size; col++) { if(row == row_size-1 && col == col_size - 1) end_flag = 1; input = imgin[row][col]; label = encuqi(input,bound,numlev); // printf("%d %d, ", input, label); // printf("%d ", &ofp); // stuffit(label,numbits,&ofp,end_flag); stuffit(label,numbits,&code_write,end_flag); } // Print(&code_write->front); while(code_write->size >= 8) { write_to_file(&ofp, &code_write); } // printf("%d", code_write->size); FILE *fp; fp = fopen(input_image, "rb"); double ratio_com = get_ratio(&fp, &ofp); printf("\n Ratio compression := %lf %%\n", ratio_com); fclose(fp); fclose(ofp); }
void main(int argc, char **argv) { int row, col, row_size, col_size, temp, mode,pred; char inimage[50], resimage[50]; unsigned char **image_in, **res_image; int c; FILE *ifp, *ofp; extern int optind; extern char *optarg; ofp = stdout; strcpy(resimage,"standard out"); mode = -1; row_size = -1; col_size = -1; while((c=getopt(argc,argv,"i:o:m:x:y:h"))!=EOF) { switch (c){ case 'i': strcpy(inimage,optarg); break; case 'o': strcpy(resimage,optarg); ofp = fopen(optarg,"wb"); break; case 'm': sscanf(optarg,"%d", &mode); break; case 'x': sscanf(optarg,"%d", &row_size); break; case 'y': sscanf(optarg,"%d", &col_size); break; case 'h': usage(); exit(1); } } if(mode < 0) { fprintf(stderr,"Enter lossless JPEG mode (0 .. 7) :"); scanf("%d",&mode); } fprintf(stderr,"\n\n\t\tJPEG Lossless Compression \n"); fprintf(stderr,"This program generates the prediction error (residuals)\n"); fprintf(stderr,"using the different JPEG lossless predictive modes.\n"); fprintf(stderr,"The selected JPEG mode is %d and the residual image is written\n",mode); fprintf(stderr,"to %s\n",resimage); fprintf(stderr,"\n In order to obtain compression, these residuals should\n"); fprintf(stderr,"be encoded using a variable rate coder.\n"); /* If the image dimensions have not been provided find the image dimensions */ if(row_size < 0 || col_size < 0) image_size(inimage, &row_size, &col_size); fprintf(stderr,"\n Image size: %d X %d\n",col_size,row_size); /* Assign space for the input and residual images */ image_in = (unsigned char **) calloc(row_size,sizeof(char *)); for(row=0; row<row_size; row++) { image_in[row] = (unsigned char *) calloc(col_size,sizeof(char)); } res_image = (unsigned char **) calloc(row_size,sizeof(char *)); for(row=0; row<row_size; row++) { res_image[row] = (unsigned char *) calloc(col_size,sizeof(char)); } /* Read the image to be decorrelated */ readimage(inimage, image_in, row_size, col_size); /* Generate prediction using the prediction mode selected */ for(row=0; row<row_size; row++) for(col=0; col<col_size; col++) { switch(mode) { case 0: pred = 0; break; case 1: if(row == 0 && col == 0) pred = 0; else if(row==0) pred = image_in[row][col -1]; else pred = image_in[row-1][col]; break; case 2: if(row == 0 && col == 0) pred = 0; else if(col==0) pred = image_in[row-1][col]; else pred = image_in[row][col -1]; break; case 3: if(row == 0 && col == 0) pred = 0; else if(col==0) pred = image_in[row-1][col]; else if(row==0) pred = image_in[row][col -1]; else pred = image_in[row-1][col - 1]; break; case 4: if(row == 0 && col == 0) pred = 0; else if(col==0) pred = image_in[row-1][col]; else if(row==0) pred = image_in[row][col -1]; else pred = image_in[row][col-1] + image_in[row-1][col] - image_in[row-1][col- 1]; break; case 5: if(row == 0 && col == 0) pred = 0; else if(col==0) pred = image_in[row-1][col]; else if(row==0) pred = image_in[row][col -1]; else pred = image_in[row][col-1] + (image_in[row-1][col] - image_in[row-1][col- 1])/2; break; case 6: if(row == 0 && col == 0) pred = 0; else if(col==0) pred = image_in[row-1][col]; else if(row==0) pred = image_in[row][col -1]; else pred = image_in[row-1][col] + (image_in[row][col-1] - image_in[row-1][col- 1])/2; break; case 7: if(row == 0 && col == 0) pred = 0; else if(col==0) pred = image_in[row-1][col]; else if(row==0) pred = image_in[row][col -1]; else pred = (image_in[row][col-1] + image_in[row-1][col])/2; break; default: fprintf(stderr,"No JPEG mode was specified"); exit(1); } /* Generate the residual */ temp = image_in[row][col] - pred; /* Represent the residual modulo 256 */ if(temp < 0) temp += 255; if(temp > 255) temp -= 255; res_image[row][col] = temp; } /* Store JPEG mode and dimensions of the image */ fwrite(&mode,1,sizeof(int),ofp); fwrite(&col_size,1,sizeof(int),ofp); fwrite(&row_size,1,sizeof(int),ofp); /* Store residual image */ for(row=0; row<row_size; row++) for(col=0; col<col_size; col++) putc(res_image[row][col],ofp); }
int p3c(void) { // initialize data of input const char *inputname = "trees.raw"; int SizeW = 350; int SizeH = 258; int BytesPerPixel = 3; unsigned char inputdata[SizeH][SizeW][BytesPerPixel]; // initialize data of output const char *outputname1 = "3c tree_SED_ans.raw"; const char *outputname2 = "3c tree_MBVQ_ans.raw"; int newSizeW = SizeW; int newSizeH = SizeH; int newBytesPerPixel = BytesPerPixel; unsigned char outputdata1[newSizeH][newSizeW][newBytesPerPixel]; // read input readimage(inputname, &inputdata[0][0][0], SizeH, SizeW, BytesPerPixel); double inputC[SizeH][SizeW]; double inputM[SizeH][SizeW]; double inputY[SizeH][SizeW]; for(int i=0; i<SizeH; i++) { for(int j=0; j<SizeW; j++) { inputC[i][j] = 1 - (inputdata[i][j][0] / 255.0); inputM[i][j] = 1 - (inputdata[i][j][1] / 255.0); inputY[i][j] = 1 - (inputdata[i][j][2] / 255.0); // printf("%d %f %f %f\n", i, inputC[i][j], inputM[i][j], inputY[i][j]); } } // Separable Error Diffusion double sedC[SizeH][SizeW]; double sedM[SizeH][SizeW]; double sedY[SizeH][SizeW]; FSErrorDiffFord(SizeH, SizeW, &inputC[0][0], &sedC[0][0]); FSErrorDiffFord(SizeH, SizeW, &inputM[0][0], &sedM[0][0]); FSErrorDiffFord(SizeH, SizeW, &inputY[0][0], &sedY[0][0]); for(int i=0; i<SizeH; i++) { for(int j=0; j<SizeW; j++) { outputdata1[i][j][0] = 255*(1 - sedC[i][j]); outputdata1[i][j][1] = 255*(1 - sedM[i][j]); outputdata1[i][j][2] = 255*(1 - sedY[i][j]); // printf("%d %f %f %f\n", i, sedC[i][j], sedM[i][j], sedY[i][j]); } } writeimage(outputname1, &outputdata1[0][0][0], newSizeH, newSizeW, newBytesPerPixel); // MBVQ-based Error diffusion MBVQErrorDiff(inputname, outputname2, SizeH, SizeW, BytesPerPixel); return 0; }
int main( int argc, char *argv[] ) { int ret = 1; int win_main, win_zoom, zoom_factor = 2; double p_x = -1, p_y = -1; unsigned char *zoom_buf = NULL; unsigned char *img_buf = NULL; int img_width, img_height, img_msk; int crosshairs = 1; if ( argc < 2 ) { fprintf(stderr,"[USAGE]\n"); fprintf(stderr,"%s filename.png\n",argv[0]); return 1; } /* zoom ウィンドゥ用の画像バッファ */ zoom_buf = (unsigned char *)malloc(ZOOMWN_WIDTH * ZOOMWN_HEIGHT * 4); if ( zoom_buf == NULL ) goto quit; //img_buf = readimage(NULL,argv[1],&img_width,&img_height,&img_msk); //img_buf = readimage("xbmtopbm",argv[1],&img_width,&img_height,&img_msk); #ifdef USE_IMAGEMAGICK img_buf = readimage("convert",argv[1],&img_width,&img_height,&img_msk); #else img_buf = readimage("pngtopnm",argv[1],&img_width,&img_height,&img_msk); #endif if ( img_buf == NULL ) { fprintf(stderr,"ERROR: cannot read file\n"); goto quit; } printf("mask value = %d\n",img_msk); /* 属性設定(左上を原点にする) */ gsetinitialattributes(DISABLE, BOTTOM_LEFT_ORIGIN); /* ウィンドゥのオープン */ win_zoom = gopen(ZOOMWN_WIDTH*2, ZOOMWN_HEIGHT); win_main = gopen(img_width, img_height); layer(win_zoom,0,1); /* メインウィンドゥへの画像の転送 */ gclr(win_main); gputimage(win_main, 0,0, img_buf, img_width, img_height, img_msk); /* カーソル描画のために,XOR での描画functionに設定 */ newgcfunction(win_main, GXxor); newcolor(win_main, "red"); #if 0 /* 保存のテスト */ //writeimage(img_buf,img_width,img_height,img_msk,"pnmtopng",256,"hoge.png"); //writeimage(img_buf,img_width,img_height,img_msk,NULL,256,"hoge.pam"); gsaveimage( win_main,0,0,0,1999,1999,"pnmtopng",256,"capt0.png"); { int im_w, im_h; unsigned char *im = ggetimage(win_main,0,0,0,1999,1999,&im_w,&im_h); writeimage(im_w,im_h,im,0,"pnmtopng",256,"capt.png"); if ( im != NULL ) free(im); } #endif /* メインループ */ while ( 1 ) { int win_ev, type, b; int needs_redraw = 0; double x, y; win_ev = ggetevent(&type,&b,&x,&y) ; if ( type == EnterNotify ) { fprintf(stderr,"event type = EnterNotify wid=%d\n",win_ev); } else if ( type == LeaveNotify ) { fprintf(stderr,"event type = LeaveNotify wid=%d\n",win_ev); } else { fprintf(stderr,"event type = %d wid=%d\n",type,win_ev); } if ( win_ev == win_main ) { if ( type == MotionNotify || type == EnterNotify || type == LeaveNotify ) { if ( type == LeaveNotify ) { x = -32000; y = -32000; } if ( crosshairs ) { /* crosshairs カーソルを消す */ drawline(win_main, 0,p_y, img_width,p_y); drawline(win_main, p_x,0, p_x,img_height); /* crosshairs カーソルを表示する */ drawline(win_main, 0,y, img_width,y); drawline(win_main, x,0, x,img_height); } needs_redraw = 1; p_x = x; p_y = y; } else if ( type == ButtonPress ) { if ( b == 1 ) zoom_factor++; else if ( b == 3 ) zoom_factor--; needs_redraw = 1; } } if ( type == KeyPress ) { if ( b == 'q' ) goto quit; else if ( b == '>' || b == '+' ) zoom_factor++; else if ( b == '<' || b == '-' ) zoom_factor--; else if ( b == ' ' ) { if ( crosshairs ) { /* crosshairs カーソルを消す */ drawline(win_main, 0,p_y, img_width,p_y); drawline(win_main, p_x,0, p_x,img_height); crosshairs = 0; } else { /* crosshairs カーソルを表示する */ drawline(win_main, 0,p_y, img_width,p_y); drawline(win_main, p_x,0, p_x,img_height); crosshairs = 1; } } needs_redraw = 1; } if ( needs_redraw ) { if ( zoom_factor < 2 ) zoom_factor = 2; else if ( 32 < zoom_factor ) zoom_factor = 32; get_zoom_image(img_width, img_height, img_buf, p_x, p_y, zoom_factor, ZOOMWN_WIDTH, ZOOMWN_HEIGHT, zoom_buf); gclr(win_zoom); /* 普通のコピー */ newgcfunction(win_zoom, GXcopy); gputimage(win_zoom, 0, 0, zoom_buf, ZOOMWN_WIDTH, ZOOMWN_HEIGHT, img_msk); /* 反転コピー */ newgcfunction(win_zoom, GXcopyInverted); gputarea(win_zoom, ZOOMWN_WIDTH, 0, win_zoom,1, 0,0,ZOOMWN_WIDTH-1, ZOOMWN_HEIGHT-1); /* ズームウィンドゥで,文字列を反転描画する */ newgcfunction(win_zoom, GXinvert); drawstr(win_zoom,2,14,14,0, "%dx zoom pos=(%g,%g)\n",zoom_factor,p_x,p_y); copylayer(win_zoom,1,0); } } ret = 0; quit: gcloseall(); if ( img_buf != NULL ) free(img_buf); if ( zoom_buf != NULL ) free(zoom_buf); return ret; }
void LoadImage(rawimage * image) { if (!image->loaded) { readimage(image); image->loaded=1; } }
int32_t main(int argc, char *argv[]) { struct xvimage *image, *edges; FILE *output, *amira_script; char output_format; double r, v, b; iv_scene* scene; list *ss_result, *ss_result2; complexe *temp, *intersect_edges, *point; uint32_t i, rs, ps, d, N, lim, erode, j, keep, k, max, num_pt, *tab_pt, kept; char name[BASE_ALLOC]; //******************************************* //Checking input values //******************************************* if (argc!=4) { fprintf(stderr, "usage: %s %s\n", argv[0], USAGE); return(-1); } //We read input image image=readimage(argv[1]); if (image==NULL) { fprintf(stderr, "Error: Could not read %s.\n", argv[1]); return(-1); } else if(datatype(image)!=VFF_TYP_1_BYTE) { fprintf(stderr, "Error: only CC image supported\n"); return(-1); } //We extract some info rs=rowsize(image1); cs=colsize(image1); d=depth(image1); N=rs*cs*d; //We produce the edges image edges=copyimage(image); if(edges==NULL) { fprintf(stderr, "Memory allocation error : not enough memory.\n"); return(-1); } //And keep only intersection edges in this image pix=0; for(k=0; k<d; k++) for(j=0; j<cs; j++) for(i=0; i<rs; i++) { if( (UCHARDATA(image1)[pix] & CC_AX) != 0) { t=cca_cardinal_containers(image1, pix, i, j, k, CC_AX, rs, rs*cs); if(t<3) UCHARDATA(image1)[pix]&=(255-CC_AX); } if( (UCHARDATA(image1)[pix] & CC_AY) != 0) { t=cca_cardinal_containers(image1, pix, i, j, k, CC_AY, rs, rs*cs); if(t<3) UCHARDATA(image1)[pix]&=(255-CC_AY); } if( (UCHARDATA(image1)[pix] & CC_AZ) != 0) { t=cca_cardinal_containers(image1, pix, i, j, k, CC_AZ, rs, rs*cs); if(t<3) UCHARDATA(image1)[pix]&=(255-CC_AZ); } pix++; } for(i=0; i<N; i++) UCHARDATA(image1)[i]&=255-(CC_VOL | CC_FXY | CC_FYZ | CC_FXZ | CC_PT); cca_makecomplex(image1); if(strcmp(argv[6], "keep")==0) { strategy=KEEP; } else if(strcmp(argv[6], "reject")==0) { strategy=REJECT; } else { fprintf(stderr, "usage: %s %s\n", argv[0], USAGE); return(-1); } mode_amira=0; if(argc==9) { if(strcmp(argv[8], "-amira")!=0) { fprintf(stderr, "usage: %s %s\n", argv[0], USAGE); freeimage(image); freeimage(edges); return(-1); } else { mode_amira=1; } } if(strcmp(argv[4], "fusion")==0) { mode=FUSION; } else if(strcmp(argv[4], "split")==0) { mode=SPLIT; } else { fprintf(stderr, "usage: %s %s\n", argv[0], USAGE); freeimage(image); freeimage(edges); return(-1); } rs=rowsize(image); ps=colsize(image)*rs; if(mode==SPLIT) { //Each surface will be written in different Inventor File //Create an AVIZO script which will allow to open all of them sprintf(name, "%s_avizo_load_script.hx", argv[3]); amira_script=fopen(name, "wb"); if(amira_script==NULL) { fprintf(stderr, "Error: could not create file %s. Check directory permission.\n", name); freeimage(image); freeimage(edges); return(-1); } amira_script_init(amira_script); } else { sprintf(name, "%s.iv", argv[3]); output = fopen(name, "wb"); if(output==NULL) { fprintf(stderr, "Error: could not create output file %s (directory exists?).\n", name); freeimage(image); freeimage(edges); return(-1); } } if(strcmp(argv[7], "NULL")!=0) { cca_image=allocimage(NULL, rs, ps/rs, depth(image), VFF_TYP_1_BYTE); if(cca_image==NULL) { fprintf(stderr, "Error: could not allocate memory\n"); freeimage(image); freeimage(edges); return(-1); } } else { cca_image=NULL; } //******************************************************* //Preparation of the image and the scene //******************************************************* //In case we don't have a complex, uncomment following //cca_makecomplex(image); scene=inventor_new_scene(10, NULL); //Initialize the scene... if(scene==NULL) { fprintf(stderr, "Error when creating new scene.\n"); return(-1); } //We add to our object the main materials (keep the surfaces for later) inventor_add_material_to_scene(scene, "MatPoint", POS_PT, 0.0, 0.0, 0.0, 0.1, 0.4, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); inventor_add_material_to_scene(scene, "MatAX", POS_AX, 0.0, 0.0, 0.0, 0.1, 0.1, 0.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); inventor_add_material_to_scene(scene, "MatAY", POS_AY, 0.0, 0.0, 0.0, 0.1, 0.1, 0.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); inventor_add_material_to_scene(scene, "MatAZ", POS_AZ, 0.0, 0.0, 0.0, 0.1, 0.1, 0.4, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); inventor_add_material_to_scene(scene, "MatVXY", POS_VXY, 0.0, 0.0, 0.0, 0.65, 0.65, 0.65, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); inventor_add_material_to_scene(scene, "MatVXZ", POS_VXZ, 0.0, 0.0, 0.0, 0.50, 0.50, 0.50, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); inventor_add_material_to_scene(scene, "MatVYZ", POS_VYZ, 0.0, 0.0, 0.0, 0.80, 0.80, 0.80, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); if(mode==FUSION) { scene->output=output; inventor_init_file(scene); } //******************************************************** //Make surface segmentation //******************************************************** //Get the intersection edges intersect_edges=cca_to_complexe(edges); freeimage(edges); if(intersect_edges==NULL) { fprintf(stderr, "Error in function cca_to_complexe()\n"); inventor_delete_scene(scene); freeimage(image); return (-1); } //Make separation of the complex into surface components ss_result=cca_simple_surface_segmentation_with_intersection_edges_and_exclusion_inclusion_image(image, intersect_edges, filter, strategy); if(ss_result==NULL) { fprintf(stderr, "Error: cca_simple_surface_segmentation_with_intersection_edges_and_exclusion_inclusion_image() failed.\n"); inventor_delete_scene(scene); freeimage(image); return(-1); } //We don't need the image anymore freeimage(image); fprintf(stdout, "Found %d surfaces\n", ss_result->cpt -2); //The first item is the set of all vertices... temp=(complexe*)list_pop(ss_result); if(mode==SPLIT) { //We don't care. complexe_free_complexe(temp); point=NULL; tab_pt=NULL; num_pt=0; } else if(mode==FUSION) { for(i=0; i<ss_result->cpt; i++) { //Choose a random color for surfaces r=((double)rand())/((double)RAND_MAX); v=((double)rand())/((double)RAND_MAX); b=((double)rand())/((double)RAND_MAX); sprintf(name, "SurfXY_%d", i); inventor_add_material_to_scene(scene, name, POS_FXY, r, v, b, r, v, b, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); r+=0.05; v+=0.05; b+=0.05; if(r>1.0) r=1.0; if(b>1.0) b=1.0; if(v>1.0) v=1.0; sprintf(name, "SurfXZ_%d", i); inventor_add_material_to_scene(scene, name, POS_FXZ, r, v, b, r, v, b, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); r+=0.05; v+=0.05; b+=0.05; if(r>1.0) r=1.0; if(b>1.0) b=1.0; if(v>1.0) v=1.0; sprintf(name, "SurfYZ_%d", i); inventor_add_material_to_scene(scene, name, POS_FYZ, r, v, b, r, v, b, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); inventor_write_material_to_file(scene, POS_FXY); inventor_write_material_to_file(scene, POS_FXZ); inventor_write_material_to_file(scene, POS_FYZ); } //All the surfaces will go in the same file. //The array of points therefore interest us, we keep it and write it in the inventor file point=temp; tab_pt=point->tab_pt_obj; num_pt=point->num_pt_obj; inventor_new_object(scene); //inventor_set_drawstyle(scene, INVISIBLE, 1, 1); inventor_declare_points(scene, "Points", tab_pt, num_pt, rs, ps, mode_amira); } //The second item is the set of all intersect edges... We don't care. temp=(complexe*)list_pop(ss_result); complexe_free_complexe(temp); //******************************************************* //Send the surfaces to output file //******************************************************* i=0; //Will be used for the filename kept=0; while(!list_isempty(ss_result)) { //Get the object to write temp=(complexe*)list_pop(ss_result); keep=1; if(keep==1) { if(cca_image!=NULL) complexe_add_to_cca(cca_image, temp); /* if(temp->num_fxy>0) {k=temp->tab_fxy[0]; f=CC_FXY;} else if(temp->num_fxz>0) {k=temp->tab_fxz[0]; f=CC_FXZ;} else if(temp->num_fyz>0) {k=temp->tab_fyz[0]; f=CC_FYZ;} else assert(0); l=cca_list_container(cca_image, k, getxfrompixnum(k, rs, ps), getyfrompixnum(k, rs, ps), getzfrompixnum(k, rs, ps), f, rs, ps); assert(l->cpt==2); while(!list_isempty(l)) { g=(face_desc*)list_pop(l); surf[kept][l->cpt -1]=LONGDATA(labels)[g->pixnumber]; free(g); } assert(surf[kept][0] != surf[kept][1]); if(surf[kept][0] > surf[kept][1]) { k=surf[kept][1]; surf[kept][1]=surf[kept][0]; surf[kept][0]=k; } */ kept++; if(mode==SPLIT) complexe_compute_vertex_array(temp, rs, ps); if(mode==SPLIT) { //Choose a random color for surfaces r=((double)rand())/((double)RAND_MAX); v=((double)rand())/((double)RAND_MAX); b=((double)rand())/((double)RAND_MAX); sprintf(name, "SurfXY_%d", i); inventor_add_material_to_scene(scene, name, POS_FXY, r, v, b, r, v, b, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); r+=0.05; v+=0.05; b+=0.05; if(r>1.0) r=1.0; if(b>1.0) b=1.0; if(v>1.0) v=1.0; sprintf(name, "SurfXZ_%d", i); inventor_add_material_to_scene(scene, name, POS_FXZ, r, v, b, r, v, b, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); r+=0.05; v+=0.05; b+=0.05; if(r>1.0) r=1.0; if(b>1.0) b=1.0; if(v>1.0) v=1.0; sprintf(name, "SurfYZ_%d", i); inventor_add_material_to_scene(scene, name, POS_FYZ, r, v, b, r, v, b, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); //Create an output file for the surface sprintf(name, "%s.surf%d.%d.iv", argv[3], temp->num_fxy+temp->num_fyz+temp->num_fxz, i); output = fopen(name, "wb"); if(output==NULL) { fprintf(stderr, "Error: could not create output file %s (directory exists?).\n", name); while(!list_isempty(ss_result)) { temp=(complexe*)list_pop(ss_result); complexe_free_complexe(temp); } list_delete(ss_result, NO_FREE_DATA); inventor_delete_scene(scene); return(-1); } //Link the output with the scene scene->output=output; //And initialise the scene and the points inventor_init_file(scene); inventor_new_object(scene); //inventor_set_drawstyle(scene, INVISIBLE, 1, 1); inventor_declare_points(scene, "Points", temp->tab_pt_obj, temp->num_pt_obj, rs, ps, mode_amira); tab_pt=temp->tab_pt_obj; num_pt=temp->num_pt_obj; temp->num_pt_obj=0; temp->tab_pt_obj=NULL; } inventor_new_object(scene); if(mode==FUSION) { sprintf(name, "SurfXY_%d", i); inventor_add_material_to_scene(scene, name, POS_FXY, r, v, b, r, v, b, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); sprintf(name, "SurfXZ_%d", i); inventor_add_material_to_scene(scene, name, POS_FXZ, r, v, b, r, v, b, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); sprintf(name, "SurfYZ_%d", i); inventor_add_material_to_scene(scene, name, POS_FYZ, r, v, b, r, v, b, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); } //inventor_call_defined(scene, "Points"); complexe_to_inventor(scene, temp, num_pt, tab_pt, rs, ps, mode_amira); inventor_close_object(scene); fflush(scene->output); if(mode==SPLIT) { inventor_close_object(scene); fclose(scene->output); //For the amira script k=0; max=0; while(name[k]!='\0') { if(name[k]=='/') max=k+1; k++; } amira_script_add_iv_file(amira_script, &name[max], 1, 30*(i+1)); free(tab_pt); } } i++; complexe_free_complexe(temp); } if(mode==FUSION) { inventor_close_object(scene); fclose(scene->output); } fprintf(stdout, "Kept %d surfaces\n", kept); /*for(i=0; i<kept-1; i++) for(k=i+1; k<kept; k++) { if( */ if(cca_image!=NULL) { writeimage(cca_image, argv[7]); freeimage(cca_image); } //**************************************************** //Program ends //**************************************************** inventor_delete_scene(scene); list_delete(ss_result, NO_FREE_DATA); if(filter!=NULL) freeimage(filter); if(mode==SPLIT) fclose(amira_script); return(0); }
int p1b(void) { // initialize data of input const char *filename = "jennifer.raw"; int SizeW = 512; int SizeH = 512; int BytesPerPixel = 3; unsigned char ImageData[SizeH][SizeW][BytesPerPixel]; // read input readimage(filename, &ImageData[0][0][0], SizeH, SizeW, BytesPerPixel); // int scaleW1 = 512 - (128*(576-512)/512); // int scaleH1 = 512; // unsigned char scaledImage1[scaleH1][scaleW1][3]; // resizeimage(&ImageData[0][0][0], &scaledImage1[0][0][0], SizeH, SizeW, scaleH1, scaleW1); // int scaleH2 = 512 - (128*(576-512)/512); // int scaleW2 = 512; // unsigned char scaledImage2[scaleH2][scaleW2][3]; // resizeimage(&ImageData[0][0][0], &scaledImage2[0][0][0], SizeH, SizeW, scaleH2, scaleW2); // writeimage("1b horzscaledJ.raw", &scaledImage1[0][0][0], scaleH1, scaleW1, 3); int horzH = 512, horzW = 576; unsigned char horzoutput[horzH][horzW][BytesPerPixel]; memset(horzoutput, 0, horzH*horzW*3*(sizeof(unsigned char))); int vertH = 576, vertW = 512; unsigned char vertoutput[vertH][vertW][BytesPerPixel]; memset(vertoutput, 0, vertH*vertW*3*(sizeof(unsigned char))); for(int i=0; i<128; i++) { for(int j=0; j<SizeW; j++) { int s = (576-512)*(i+1)/128.0; horzoutput[i][j+s][0] = ImageData[i][j][0]; horzoutput[i][j+s][1] = ImageData[i][j][1]; horzoutput[i][j+s][2] = ImageData[i][j][2]; } } for(int i=128; i<256; i++) { for(int j=0; j<SizeW; j++) { int s = (576-512)*((255-i)+1)/128.0; horzoutput[i][j+s][0] = ImageData[i][j][0]; horzoutput[i][j+s][1] = ImageData[i][j][1]; horzoutput[i][j+s][2] = ImageData[i][j][2]; } } for(int i=256; i<384; i++) { for(int j=0; j<SizeW; j++) { int s = (576-512)*((i-256)+1)/128.0; horzoutput[i][j+s][0] = ImageData[i][j][0]; horzoutput[i][j+s][1] = ImageData[i][j][1]; horzoutput[i][j+s][2] = ImageData[i][j][2]; } } for(int i=384; i<512; i++) { for(int j=0; j<SizeW; j++) { int s = (576-512)*((511-i)+1)/128.0; horzoutput[i][j+s][0] = ImageData[i][j][0]; horzoutput[i][j+s][1] = ImageData[i][j][1]; horzoutput[i][j+s][2] = ImageData[i][j][2]; } } writeimage("1b horizontal shear J.raw", &horzoutput[0][0][0], horzH, horzW, 3); for(int j=0; j<128; j++) { for(int i=0; i<SizeW; i++) { int s = (576-512)*(j+1)/128.0; vertoutput[i+s][j][0] = ImageData[i][j][0]; vertoutput[i+s][j][1] = ImageData[i][j][1]; vertoutput[i+s][j][2] = ImageData[i][j][2]; } } for(int j=128; j<256; j++) { for(int i=0; i<SizeW; i++) { int s = (576-512)*((255-j)+1)/128.0; vertoutput[i+s][j][0] = ImageData[i][j][0]; vertoutput[i+s][j][1] = ImageData[i][j][1]; vertoutput[i+s][j][2] = ImageData[i][j][2]; } } for(int j=256; j<384; j++) { for(int i=0; i<SizeW; i++) { int s = (576-512)*((j-256)+1)/128.0; vertoutput[i+s][j][0] = ImageData[i][j][0]; vertoutput[i+s][j][1] = ImageData[i][j][1]; vertoutput[i+s][j][2] = ImageData[i][j][2]; } } for(int j=384; j<512; j++) { for(int i=0; i<SizeW; i++) { int s = (576-512)*((511-j)+1)/128.0; vertoutput[i+s][j][0] = ImageData[i][j][0]; vertoutput[i+s][j][1] = ImageData[i][j][1]; vertoutput[i+s][j][2] = ImageData[i][j][2]; } } writeimage("1b vertical shear J.raw", &vertoutput[0][0][0], vertH, vertW, 3); return 0; }
int p1a(void) { // initialize data of input const char *filename1 = "Tiffany.raw"; int SizeW1 = 512; int SizeH1 = 512; int BytesPerPixel1 = 3; unsigned char ImageData1[SizeH1][SizeW1][BytesPerPixel1]; const char *filename2 = "Components.raw"; int SizeW2 = 350; int SizeH2 = 350; int BytesPerPixel2 = 3; unsigned char ImageData2[SizeH2][SizeW2][BytesPerPixel2]; // read input readimage(filename1, &ImageData1[0][0][0], SizeH1, SizeW1, BytesPerPixel1); readimage(filename2, &ImageData2[0][0][0], SizeH2, SizeW2, BytesPerPixel2); // find the center coordinates and the dimension of holes FindHoleInfo(&ImageData1[0][0][0], SizeH1, SizeW1); // find the center coordinates, the orientation angle and the dimension of the components FindPiecesInfo(&ImageData2[0][0][0], SizeH2, SizeW2); float mholeH = (60+4), mholeW = (110+4); float mholeI = 338, mholeJ = 320; float mouthH = 65.069191, mouthW = 127.279221; float mouthI = 107.620117, mouthJ = 106.122902; float mangle = 0 - 0.785398; float eholeH = (35+4), eholeW = (80+4); float eholeI = 211.5, eholeJ = 258; float eyeH = 52, eyeW = 112.946892; float eyeI = 251.533356, eyeJ = 226.533356; float eangle = 0.352553 - 3.14159; // rotation unsigned char rmImage[SizeH2][SizeW2][BytesPerPixel2]; memset(rmImage, 255, SizeH2*SizeW2*3*(sizeof(unsigned char))); rotation(&ImageData2[0][0][0], &rmImage[0][0][0], SizeH2, SizeW2, mouthH, mouthW, mouthI, mouthJ, mangle); writeimage("1a rotatedmouth.raw", &rmImage[0][0][0], SizeH2, SizeW2, 3); unsigned char reImage[SizeH2][SizeW2][BytesPerPixel2]; memset(reImage, 255, SizeH2*SizeW2*3*(sizeof(unsigned char))); rotation(&ImageData2[0][0][0], &reImage[0][0][0], SizeH2, SizeW2, eyeH, eyeW, eyeI, eyeJ, eangle); writeimage("1a rotatedeye.raw", &reImage[0][0][0], SizeH2, SizeW2, 3); // scaling unsigned char smImage[SizeH2][SizeW2][BytesPerPixel2]; memset(smImage, 255, SizeH2*SizeW2*3*(sizeof(unsigned char))); scaling(&rmImage[0][0][0], &smImage[0][0][0], SizeH2, SizeW2, mouthH, mouthW, mholeH, mholeW, mouthI, mouthJ); writeimage("1a scaledmouth.raw", &smImage[0][0][0], SizeH2, SizeW2, 3); unsigned char seImage[SizeH2][SizeW2][BytesPerPixel2]; memset(seImage, 255, SizeH2*SizeW2*3*(sizeof(unsigned char))); scaling(&reImage[0][0][0], &seImage[0][0][0], SizeH2, SizeW2, eyeH, eyeW, eholeH, eholeW, eyeI, eyeJ); writeimage("1a scaledeye.raw", &seImage[0][0][0], SizeH2, SizeW2, 3); // shift unsigned char shiftm[SizeH1][SizeW1][BytesPerPixel1]; memset(shiftm, 255, SizeH1*SizeW1*3*(sizeof(unsigned char))); shift(&smImage[0][0][0], &shiftm[0][0][0], SizeW2, SizeW1, mholeH, mholeW, mouthI, mouthJ, mholeI, mholeJ); writeimage("1a shiftedmouth.raw", &shiftm[0][0][0], SizeH1, SizeW1, 3); unsigned char shifte[SizeH1][SizeW1][BytesPerPixel1]; memset(shifte, 255, SizeH1*SizeW1*3*(sizeof(unsigned char))); shift(&seImage[0][0][0], &shifte[0][0][0], SizeW2, SizeW1, eholeH, eholeW, eyeI, eyeJ, eholeI, eholeJ); writeimage("1a shiftedeye.raw", &shifte[0][0][0], SizeH1, SizeW1, 3); // combine mouth & eye unsigned char MandE[SizeH1][SizeW1][BytesPerPixel1]; memset(MandE, 255, SizeH1*SizeW1*3*(sizeof(unsigned char))); combination(&shifte[0][0][0], &shiftm[0][0][0], &MandE[0][0][0], SizeH1, SizeW1); writeimage("1a mouth and eye.raw", &MandE[0][0][0], SizeH1, SizeW1, 3); // final combination unsigned char fullT[SizeH1][SizeW1][BytesPerPixel1]; memset(fullT, 255, SizeH1*SizeW1*3*(sizeof(unsigned char))); combination(&MandE[0][0][0], &ImageData1[0][0][0], &fullT[0][0][0], SizeH1, SizeW1); writeimage("1a fullTiffany.raw", &fullT[0][0][0], SizeH1, SizeW1, 3); return 0; }
int p1c(void) { // initialize data of input const char *filename = "fruits.raw"; int SizeW = 512; int SizeH = 512; int BytesPerPixel = 3; unsigned char ImageData[SizeH][SizeW][BytesPerPixel]; // read input readimage(filename, &ImageData[0][0][0], SizeH, SizeW, BytesPerPixel); unsigned char output[SizeH][SizeW][BytesPerPixel]; memset(output, 0, SizeH*SizeW*3*(sizeof(unsigned char))); float oldx, oldy, midi, midj; float diffi, diffj; int midi0, midj0, midi1, midj1, midi2, midj2, midi3, midj3; int R0, G0, B0, R1, G1, B1, R2, G2, B2, R3, G3, B3; for(int i=0; i<256; i++) { for(int j=256; (j <= 511-i) && (j <= 255 + i/0.4142); j++) { float x = j - 255; float y = 256 -i; oldx = 1.4142*x; oldy = 0.4142*x + y; midj = oldx + 255; midi = 256 - oldy; diffi = midi - ((int)midi); diffj = midj - ((int)midj); midi0 = midi; midj0 = midj; midi1 = midi0 + 1; midj1 = midj0; midi2 = midi0; midj2 = midj0 + 1; midi3 = midi0 + 1; midj3 = midj0 + 1; R0 = ImageData[midi0][midj0][0]; G0 = ImageData[midi0][midj0][1]; B0 = ImageData[midi0][midj0][2]; R1 = ImageData[midi1][midj1][0]; G1 = ImageData[midi1][midj1][1]; B1 = ImageData[midi1][midj1][2]; R2 = ImageData[midi2][midj2][0]; G2 = ImageData[midi2][midj2][1]; B2 = ImageData[midi2][midj2][2]; R3 = ImageData[midi3][midj3][0]; G3 = ImageData[midi3][midj3][1]; B3 = ImageData[midi3][midj3][2]; output[i][j][0] = (int)((1.0-diffj)*(1-diffi)*R0 + diffj*(1-diffi)*R1 + diffi*(1-diffj)*R2 + diffj*diffi*R3); output[i][j][1] = (int)((1.0-diffj)*(1-diffi)*G0 + diffj*(1-diffi)*G1 + diffi*(1-diffj)*G2 + diffj*diffi*G3); output[i][j][2] = (int)((1.0-diffj)*(1-diffi)*B0 + diffj*(1-diffi)*B1 + diffi*(1-diffj)*B2 + diffj*diffi*B3); } } for(int j=256; j<512; j++) { for(int i=255; (i >= 2.4142*j - 977.6593) && (i >= 511-j); i--) { float x = j - 255; float y = 256 -i; // printf("i = %d , j = %d, x = %f, y = %f\n", i, j, x, y); oldx = x + 0.4142*y; oldy = 1.4142*y; midj = oldx + 255; midi = 256 - oldy; diffi = midi - ((int)midi); diffj = midj - ((int)midj); midi0 = midi; midj0 = midj; midi1 = midi0 + 1; midj1 = midj0; midi2 = midi0; midj2 = midj0 + 1; midi3 = midi0 + 1; midj3 = midj0 + 1; R0 = ImageData[midi0][midj0][0]; G0 = ImageData[midi0][midj0][1]; B0 = ImageData[midi0][midj0][2]; R1 = ImageData[midi1][midj1][0]; G1 = ImageData[midi1][midj1][1]; B1 = ImageData[midi1][midj1][2]; R2 = ImageData[midi2][midj2][0]; G2 = ImageData[midi2][midj2][1]; B2 = ImageData[midi2][midj2][2]; R3 = ImageData[midi3][midj3][0]; G3 = ImageData[midi3][midj3][1]; B3 = ImageData[midi3][midj3][2]; output[i][j][0] = (int)((1.0-diffj)*(1-diffi)*R0 + diffj*(1-diffi)*R1 + diffi*(1-diffj)*R2 + diffj*diffi*R3); output[i][j][1] = (int)((1.0-diffj)*(1-diffi)*G0 + diffj*(1-diffi)*G1 + diffi*(1-diffj)*G2 + diffj*diffi*G3); output[i][j][2] = (int)((1.0-diffj)*(1-diffi)*B0 + diffj*(1-diffi)*B1 + diffi*(1-diffj)*B2 + diffj*diffi*B3); } } for(int j=256; j<512; j++) { for(int i=255; (i <= j) && (i <= 1488.6593 - 2.4142*j); i++) { float x = j - 255; float y = 255 -i; oldx = x - 0.4142*y; oldy = 1.4142*y; midj = oldx + 255; midi = 255 - oldy; // printf("i = %d , j = %d, x = %f, y = %f, midi = %f, midj = %f\n", i, j, x, y, midi, midj); diffi = midi - ((int)midi); diffj = midj - ((int)midj); midi0 = midi; midj0 = midj; midi1 = midi0 + 1; midj1 = midj0; midi2 = midi0; midj2 = midj0 + 1; midi3 = midi0 + 1; midj3 = midj0 + 1; R0 = ImageData[midi0][midj0][0]; G0 = ImageData[midi0][midj0][1]; B0 = ImageData[midi0][midj0][2]; R1 = ImageData[midi1][midj1][0]; G1 = ImageData[midi1][midj1][1]; B1 = ImageData[midi1][midj1][2]; R2 = ImageData[midi2][midj2][0]; G2 = ImageData[midi2][midj2][1]; B2 = ImageData[midi2][midj2][2]; R3 = ImageData[midi3][midj3][0]; G3 = ImageData[midi3][midj3][1]; B3 = ImageData[midi3][midj3][2]; output[i][j][0] = (int)((1.0-diffj)*(1-diffi)*R0 + diffj*(1-diffi)*R1 + diffi*(1-diffj)*R2 + diffj*diffi*R3); output[i][j][1] = (int)((1.0-diffj)*(1-diffi)*G0 + diffj*(1-diffi)*G1 + diffi*(1-diffj)*G2 + diffj*diffi*G3); output[i][j][2] = (int)((1.0-diffj)*(1-diffi)*B0 + diffj*(1-diffi)*B1 + diffi*(1-diffj)*B2 + diffj*diffi*B3); } } for(int i=256; i<512; i++) { for(int j=256; (j <= i) && (j <= 255 + (511-i)/0.4142); j++) { float x = j - 255; float y = 255 -i; oldx = 1.4142*x; oldy = y - 0.4142*x; midj = oldx + 255; midi = 255 - oldy; diffi = midi - ((int)midi); diffj = midj - ((int)midj); midi0 = midi; midj0 = midj; midi1 = midi0 + 1; midj1 = midj0; midi2 = midi0; midj2 = midj0 + 1; midi3 = midi0 + 1; midj3 = midj0 + 1; R0 = ImageData[midi0][midj0][0]; G0 = ImageData[midi0][midj0][1]; B0 = ImageData[midi0][midj0][2]; R1 = ImageData[midi1][midj1][0]; G1 = ImageData[midi1][midj1][1]; B1 = ImageData[midi1][midj1][2]; R2 = ImageData[midi2][midj2][0]; G2 = ImageData[midi2][midj2][1]; B2 = ImageData[midi2][midj2][2]; R3 = ImageData[midi3][midj3][0]; G3 = ImageData[midi3][midj3][1]; B3 = ImageData[midi3][midj3][2]; output[i][j][0] = (int)((1.0-diffj)*(1-diffi)*R0 + diffj*(1-diffi)*R1 + diffi*(1-diffj)*R2 + diffj*diffi*R3); output[i][j][1] = (int)((1.0-diffj)*(1-diffi)*G0 + diffj*(1-diffi)*G1 + diffi*(1-diffj)*G2 + diffj*diffi*G3); output[i][j][2] = (int)((1.0-diffj)*(1-diffi)*B0 + diffj*(1-diffi)*B1 + diffi*(1-diffj)*B2 + diffj*diffi*B3); } } for(int i=256; i<512; i++) { for(int j=255; (j >= 511-i) && (j >= 256 - (511-i)/0.4142); j--) { float x = j - 256; float y = 255 -i; oldx = 1.4142*x; oldy = 0.4142*x + y; midj = oldx + 256; midi = 255 - oldy; diffi = midi - ((int)midi); diffj = midj - ((int)midj); midi0 = midi; midj0 = midj; midi1 = midi0 + 1; midj1 = midj0; midi2 = midi0; midj2 = midj0 + 1; midi3 = midi0 + 1; midj3 = midj0 + 1; R0 = ImageData[midi0][midj0][0]; G0 = ImageData[midi0][midj0][1]; B0 = ImageData[midi0][midj0][2]; R1 = ImageData[midi1][midj1][0]; G1 = ImageData[midi1][midj1][1]; B1 = ImageData[midi1][midj1][2]; R2 = ImageData[midi2][midj2][0]; G2 = ImageData[midi2][midj2][1]; B2 = ImageData[midi2][midj2][2]; R3 = ImageData[midi3][midj3][0]; G3 = ImageData[midi3][midj3][1]; B3 = ImageData[midi3][midj3][2]; output[i][j][0] = (int)((1.0-diffj)*(1-diffi)*R0 + diffj*(1-diffi)*R1 + diffi*(1-diffj)*R2 + diffj*diffi*R3); output[i][j][1] = (int)((1.0-diffj)*(1-diffi)*G0 + diffj*(1-diffi)*G1 + diffi*(1-diffj)*G2 + diffj*diffi*G3); output[i][j][2] = (int)((1.0-diffj)*(1-diffi)*B0 + diffj*(1-diffi)*B1 + diffi*(1-diffj)*B2 + diffj*diffi*B3); } } for(int j=0; j<256; j++) { for(int i=256; (i <= 511-j) && (i <= 2.4142*j+255.0031); i++) { float x = j - 256; float y = 255 -i; // printf("i = %d , j = %d, x = %f, y = %f\n", i, j, x, y); oldx = x + 0.4142*y; oldy = 1.4142*y; midj = oldx + 256; midi = 255 - oldy; diffi = midi - ((int)midi); diffj = midj - ((int)midj); midi0 = midi; midj0 = midj; midi1 = midi0 + 1; midj1 = midj0; midi2 = midi0; midj2 = midj0 + 1; midi3 = midi0 + 1; midj3 = midj0 + 1; R0 = ImageData[midi0][midj0][0]; G0 = ImageData[midi0][midj0][1]; B0 = ImageData[midi0][midj0][2]; R1 = ImageData[midi1][midj1][0]; G1 = ImageData[midi1][midj1][1]; B1 = ImageData[midi1][midj1][2]; R2 = ImageData[midi2][midj2][0]; G2 = ImageData[midi2][midj2][1]; B2 = ImageData[midi2][midj2][2]; R3 = ImageData[midi3][midj3][0]; G3 = ImageData[midi3][midj3][1]; B3 = ImageData[midi3][midj3][2]; output[i][j][0] = (int)((1.0-diffj)*(1-diffi)*R0 + diffj*(1-diffi)*R1 + diffi*(1-diffj)*R2 + diffj*diffi*R3); output[i][j][1] = (int)((1.0-diffj)*(1-diffi)*G0 + diffj*(1-diffi)*G1 + diffi*(1-diffj)*G2 + diffj*diffi*G3); output[i][j][2] = (int)((1.0-diffj)*(1-diffi)*B0 + diffj*(1-diffi)*B1 + diffi*(1-diffj)*B2 + diffj*diffi*B3); } } for(int j=0; j<256; j++) { for(int i=255; (i >= j) && (i >= 255.9969 - 2.4142*j); i--) { float x = j - 256; float y = 256 -i; oldx = x - 0.4142*y; oldy = 1.4142*y; midj = oldx + 256; midi = 256 - oldy; // printf("i = %d , j = %d, x = %f, y = %f, midi = %f, midj = %f\n", i, j, x, y, midi, midj); diffi = midi - ((int)midi); diffj = midj - ((int)midj); midi0 = midi; midj0 = midj; midi1 = midi0 + 1; midj1 = midj0; midi2 = midi0; midj2 = midj0 + 1; midi3 = midi0 + 1; midj3 = midj0 + 1; R0 = ImageData[midi0][midj0][0]; G0 = ImageData[midi0][midj0][1]; B0 = ImageData[midi0][midj0][2]; R1 = ImageData[midi1][midj1][0]; G1 = ImageData[midi1][midj1][1]; B1 = ImageData[midi1][midj1][2]; R2 = ImageData[midi2][midj2][0]; G2 = ImageData[midi2][midj2][1]; B2 = ImageData[midi2][midj2][2]; R3 = ImageData[midi3][midj3][0]; G3 = ImageData[midi3][midj3][1]; B3 = ImageData[midi3][midj3][2]; output[i][j][0] = (int)((1.0-diffj)*(1-diffi)*R0 + diffj*(1-diffi)*R1 + diffi*(1-diffj)*R2 + diffj*diffi*R3); output[i][j][1] = (int)((1.0-diffj)*(1-diffi)*G0 + diffj*(1-diffi)*G1 + diffi*(1-diffj)*G2 + diffj*diffi*G3); output[i][j][2] = (int)((1.0-diffj)*(1-diffi)*B0 + diffj*(1-diffi)*B1 + diffi*(1-diffj)*B2 + diffj*diffi*B3); } } for(int i=0; i<256; i++) { for(int j=255; (j >= i) && (j >= 256 - i/0.4142); j--) { float x = j - 256; float y = 256 -i; oldx = 1.4142*x; oldy = y - 0.4142*x; midj = oldx + 256; midi = 256 - oldy; diffi = midi - ((int)midi); diffj = midj - ((int)midj); midi0 = midi; midj0 = midj; midi1 = midi0 + 1; midj1 = midj0; midi2 = midi0; midj2 = midj0 + 1; midi3 = midi0 + 1; midj3 = midj0 + 1; R0 = ImageData[midi0][midj0][0]; G0 = ImageData[midi0][midj0][1]; B0 = ImageData[midi0][midj0][2]; R1 = ImageData[midi1][midj1][0]; G1 = ImageData[midi1][midj1][1]; B1 = ImageData[midi1][midj1][2]; R2 = ImageData[midi2][midj2][0]; G2 = ImageData[midi2][midj2][1]; B2 = ImageData[midi2][midj2][2]; R3 = ImageData[midi3][midj3][0]; G3 = ImageData[midi3][midj3][1]; B3 = ImageData[midi3][midj3][2]; output[i][j][0] = (int)((1.0-diffj)*(1-diffi)*R0 + diffj*(1-diffi)*R1 + diffi*(1-diffj)*R2 + diffj*diffi*R3); output[i][j][1] = (int)((1.0-diffj)*(1-diffi)*G0 + diffj*(1-diffi)*G1 + diffi*(1-diffj)*G2 + diffj*diffi*G3); output[i][j][2] = (int)((1.0-diffj)*(1-diffi)*B0 + diffj*(1-diffi)*B1 + diffi*(1-diffj)*B2 + diffj*diffi*B3); } } writeimage("1c warped fruits.raw", &output[0][0][0], SizeH, SizeW, 3); return 0; }
// ****************** Problem 1: Edge Detection *********************** int p1a(void) { // initialize data of input const char *filename1 = "elaine.raw"; const char *filename2 = "noisy_elaine.raw"; int SizeW = 256; int SizeH = 256; int BytesPerPixel = 1; unsigned char ImageData1[SizeH][SizeW][BytesPerPixel]; unsigned char ImageData2[SizeH][SizeW][BytesPerPixel]; // initialize data of output const char *newfilename1a = "1a elaine_soble_ans.raw"; const char *newfilename1b = "1a elaine_LoG_ans.raw"; const char *newfilename2a = "1a noisyelaine_soble_ans.raw"; const char *newfilename2b = "1a noisyelaine_LoG_ans.raw"; int newSizeW = SizeW; int newSizeH = SizeH; int newBytesPerPixel = BytesPerPixel; unsigned char newImageData1a[newSizeH][newSizeW][newBytesPerPixel]; unsigned char newImageData1b[newSizeH][newSizeW][newBytesPerPixel]; unsigned char newImageData2a[newSizeH][newSizeW][newBytesPerPixel]; unsigned char newImageData2b[newSizeH][newSizeW][newBytesPerPixel]; // read input readimage(filename1, &ImageData1[0][0][0], SizeH, SizeW, BytesPerPixel); readimage(filename2, &ImageData2[0][0][0], SizeH, SizeW, BytesPerPixel); // --------------------Sobel Detector------------------------ int extend = 2; int thr1a = 24; int thr2a = 37; unsigned char Gradient1aR[SizeH][SizeW]; unsigned char Gradient1aC[SizeH][SizeW]; unsigned char Gradient1a[SizeH][SizeW]; const char *gradient1aR = "1a elaine_s_R_grad.raw"; const char *gradient1aC = "1a elaine_s_C_grad.raw"; const char *gradient1a = "1a elaine_soble_grad.raw"; unsigned char Gradient2aR[SizeH][SizeW]; unsigned char Gradient2aC[SizeH][SizeW]; unsigned char Gradient2a[SizeH][SizeW]; const char *gradient2aR = "1a noisingelaine_s_R_grad.raw"; const char *gradient2aC = "1a noisingelaine_s_C_grad.raw"; const char *gradient2a = "1a noisingelaine_soble_grad.raw"; GetSobleGradient(thr1a, &ImageData1[0][0][0], SizeH, SizeW, extend, &Gradient1aR[0][0], &Gradient1aC[0][0], &Gradient1a[0][0], &newImageData1a[0][0][0]); GetSobleGradient(thr2a, &ImageData2[0][0][0], SizeH, SizeW, extend, &Gradient2aR[0][0], &Gradient2aC[0][0], &Gradient2a[0][0], &newImageData2a[0][0][0]); // output writeimage(gradient1aR, &Gradient1aR[0][0], SizeH, SizeW, BytesPerPixel); writeimage(gradient1aC, &Gradient1aC[0][0], SizeH, SizeW, BytesPerPixel); writeimage(gradient1a, &Gradient1a[0][0], SizeH, SizeW, BytesPerPixel); writeimage(newfilename1a, &newImageData1a[0][0][0], newSizeH, newSizeW, newBytesPerPixel); writeimage(gradient2aR, &Gradient2aR[0][0], SizeH, SizeW, BytesPerPixel); writeimage(gradient2aC, &Gradient2aC[0][0], SizeH, SizeW, BytesPerPixel); writeimage(gradient2a, &Gradient2a[0][0], SizeH, SizeW, BytesPerPixel); writeimage(newfilename2a, &newImageData2a[0][0][0], newSizeH, newSizeW, newBytesPerPixel); // ----------------------LoG Detector------------------------------ int Dfiltersize = 9; float sigma = 1.0; float thr1b = 0.9; float thr2b = 1.5; unsigned char Gradient1b[SizeH][SizeW]; unsigned char ternarymap1[SizeH][SizeW]; unsigned char Gradient2b[SizeH][SizeW]; unsigned char ternarymap2[SizeH][SizeW]; const char *gradient1b = "1a elaine_LoG_grad.raw"; const char *ternary1 = "1a elaine_LoG_ter.raw"; const char *gradient2b = "1a noisingelaine_LoG_grad.raw"; const char *ternary2 = "1a noisingelaine_LoG_ter.raw"; LoG(Dfiltersize, sigma, thr1b, &ImageData1[0][0][0], SizeH, SizeW, &Gradient1b[0][0], &ternarymap1[0][0], &newImageData1b[0][0][0]); LoG(Dfiltersize, sigma, thr2b, &ImageData2[0][0][0], SizeH, SizeW, &Gradient2b[0][0], &ternarymap2[0][0], &newImageData2b[0][0][0]); writeimage(gradient1b, &Gradient1b[0][0], SizeH, SizeW, BytesPerPixel); writeimage(ternary1, &ternarymap1[0][0], SizeH, SizeW, BytesPerPixel); writeimage(newfilename1b, &newImageData1b[0][0][0], newSizeH, newSizeW, newBytesPerPixel); writeimage(gradient2b, &Gradient2b[0][0], SizeH, SizeW, BytesPerPixel); writeimage(ternary2, &ternarymap2[0][0], SizeH, SizeW, BytesPerPixel); writeimage(newfilename2b, &newImageData2b[0][0][0], newSizeH, newSizeW, newBytesPerPixel); return 0; }
void lockscreen(void) { enum { Nfld = 5, Fldlen = 12, Cursorlen = 2*4 + 2*2*16, }; char *s; char buf[Nfld*Fldlen], *flds[Nfld], newcmd[128], cbuf[Cursorlen]; int fd, dx, dy; Image *i; Point p; Rectangle r; Tm *tm; fd = open("/dev/screen", OREAD); if(fd < 0) error("can't open /dev/screen: %r"); if(read(fd, buf, Nfld*Fldlen) != Nfld*Fldlen) error("can't read /dev/screen: %r"); close(fd); buf[sizeof buf-1] = 0; if(tokenize(buf, flds, Nfld) != Nfld) error("can't tokenize /dev/screen header"); snprint(newcmd, sizeof newcmd, "-r %s %s %d %d", flds[1], flds[2], atoi(flds[3]) - 1, atoi(flds[4]) - 1); newwindow(newcmd); if (initdraw(nil, nil, "screenlock") < 0) sysfatal("initdraw failed"); if(display == nil) error("no display"); /* screen is now open and covered. grab mouse and hold on tight */ procrfork(grabmouse, nil, 4096, RFFDG); procrfork(blanker, nil, 4096, RFFDG); fd = open(pic, OREAD); if(fd > 0){ i = readimage(display, fd, 0); if(i){ r = screen->r; p = Pt(r.max.x / 2, r.max.y * 2 / 3); dx = (Dx(screen->r) - Dx(i->r)) / 2; r.min.x += dx; r.max.x -= dx; dy = (Dy(screen->r) - Dy(i->r)) / 2; r.min.y += dy; r.max.y -= dy; draw(screen, screen->r, display->black, nil, ZP); draw(screen, r, i, nil, i->r.min); flushimage(display, 1); } close(fd); /* identify the user on screen, centered */ tm = localtime(time(0)); s = smprint("user %s at %d:%02.2d", getuser(), tm->hour, tm->min); p = subpt(p, Pt(stringwidth(font, "m") * strlen(s) / 2, 0)); screenstring(p, s); } /* clear the cursor */ fd = open("/dev/cursor", OWRITE); if(fd > 0){ memset(cbuf, 0, sizeof cbuf); write(fd, cbuf, sizeof cbuf); /* leave it open */ } }
main(int argc, char **argv) { FILE *ifp, *ofp, *cb; int **codebook; int *input, dimension, index, codebook_size ; int c, i, j, iteration, numvecs, numbits, *count, end_flag, name_length; int k,l, blockr, blockc, cp_row, cp_col, row_size, col_size; float measure, total_distortion, distortion; char input_image[50], outfile[50]; unsigned char **inimg; char *cbfile; extern int optind; extern char *optarg; blockr = -1; blockc = -1; /* Obtain the filename for the input image */ ifp = stdin; ofp = stdout; cb = NULL; row_size = -1; col_size = -1; strcpy(input_image,"standard in"); strcpy(outfile,"standard out"); while((c=getopt(argc,argv,"i:o:c:x:y:h"))!=EOF) { switch (c){ case 'i': strcpy(input_image,optarg); ifp = fopen(optarg,"rb"); break; case 'o': strcpy(outfile,optarg); ofp = fopen(optarg,"wb"); break; case 'c': name_length = strlen(optarg); cbfile = (char *) malloc((name_length+1)*sizeof(char)); strcpy(cbfile,optarg); if((cb = fopen(optarg,"rb")) == NULL) fprintf(stderr,"Codebook file %s not found\n",cbfile); break; case 'x': sscanf(optarg,"%d", &row_size); break; case 'y': sscanf(optarg,"%d", &col_size); break; case 'h': usage(); exit(1); } } if(ifp == stdin ) fprintf(stderr,"Input will be read from standard in\n"); if(ofp == stdout) fprintf(stderr,"Output will be written to standard out\n"); /* Make sure you have the filename for the codebook */ if(cb ==NULL) { fprintf(stderr,"You *have* to enter a file name for the codebook\n"); fprintf(stderr,"A codebook can be created by using trvqsp_img. Remember\n"); fprintf(stderr,"this codebook will be needed to reconstruct the image.\n\n"); usage(); exit(1); } /* Write the name of the codebook file to the output file */ fwrite(&name_length,1,sizeof(int),ofp); fwrite(cbfile,name_length+1,sizeof(char),ofp); fprintf(stderr,"Codebook file is %s\n",cbfile); /* If the image dimensions have not been provided get image size */ if(row_size < 0 || col_size < 0) image_size(input_image, &row_size, &col_size); printf("Image: %s, Number of rows: %d, Number of columns: %d\n",input_image,row_size,col_size); /* Write image parameters to the output file */ fwrite(&row_size,1,sizeof(int),ofp); fwrite(&col_size,1,sizeof(int),ofp); /* Assign space for input image */ inimg = (unsigned char **) calloc(row_size,sizeof(char *)); for(i=0; i< row_size; i++) inimg[i] = (unsigned char *) calloc(col_size,sizeof(char)); /* Read the image to be compressed */ readimage(input_image, inimg, row_size, col_size); /* Read coding parameters from the codebook file */ fread(&blockr,1,sizeof(int),cb); fread(&blockc,1,sizeof(int),cb); fread(&codebook_size,1,sizeof(int),cb); numbits = (int) (log((double) codebook_size)/log((double) 2.) + 0.999); dimension = blockr*blockc; numvecs = (row_size*col_size)/dimension; printf("Codebook size = %d, dimensions = %d %d\n", codebook_size, blockr, blockc); fprintf(stderr,"Number of bits per vector = %d\n",numbits); /* Allocate space for the codebook */ codebook = (int **) calloc(codebook_size,sizeof(int *)); for(i=0; i< codebook_size; i++) codebook[i] = (int *) calloc(dimension,sizeof(int)); /* Read in the codebook */ for(i=0; i< codebook_size; i++) for(j=0; j< dimension; j++) { c=getc(cb); codebook[i][j] = (int) 0xff & c; } /* Allocate space for input and work arrays */ input = (int *) calloc(dimension,sizeof(int)); count = (int *) calloc(codebook_size,sizeof(int)); distortion = 0.0; total_distortion = 0.0; cp_row = 0; cp_col = 0; for(i=0; i< numvecs; i++) { l = 0; for(j=0; j < blockr; j++) for(k=0; k < blockc; k++) { input[l]=inimg[cp_row+j][cp_col+k]; l++; } index = vqencode(input,codebook,codebook_size,dimension,&distortion); /* Store index */ end_flag = 0; if(i == (numvecs-1)) end_flag = 1; stuffit(index,numbits,ofp,end_flag); total_distortion += distortion; count[index]++; cp_col += blockc; if(cp_col+blockc-1 > col_size) { cp_col = 0; cp_row += blockr; } } total_distortion = total_distortion/(float) numvecs; printf("distortion is %f\n",total_distortion); }
/** * \brief main function */ int main(int argc, char *argv[]) { int ierr; int num_procs; int tag = 1; double h = 1; int steps = 1; char *basedir = NULL; udata_t udata; udata.nx = 1; udata.ny = 1; udata.dimension = 16; udata.algorithm = 0; udata.picturesteps = 1; // initialize MPI ierr = MPI_Init(&argc, &argv); if (ierr) { fprintf(stderr, "cannot initialize MPI: %d\n", ierr); return EXIT_FAILURE; } // get MPI dimension parameters ierr = MPI_Comm_rank(MPI_COMM_WORLD, &udata.rank); ierr = MPI_Comm_size(MPI_COMM_WORLD, &num_procs); char rankprefix[10]; snprintf(rankprefix, sizeof(rankprefix), "%d", udata.rank); if (debug) { fprintf(stderr, "%s:%d[%d]: process id %d\n", __FILE__, __LINE__, udata.rank, getpid()); } // parse the command line int c; while (EOF != (c = getopt(argc, argv, "b:dh:r:s:t:x:y:n:a:?"))) switch (c) { case 'd': debug++; break; case 's': udata.picturesteps = atoi(optarg); break; case 't': udata.maxsteps = atof(optarg); break; case 'x': udata.nx = atoi(optarg); break; case 'y': udata.ny = atoi(optarg); break; case 'b': basedir = optarg; break; case 'n': udata.dimension = atoi(optarg); break; case 'a': udata.algorithm = atoi(optarg); break; case '?': usage(argv[0]); return EXIT_SUCCESS; } udata.maxsteps = udata.dimension + udata.dimension; udata.h = 1 / udata.dimension; // make sure the arguments are consistent if (num_procs != udata.nx * udata.ny) { fprintf(stderr, "number of processes does not match " "dimensions: %d != %d x %d\n", num_procs, udata.nx, udata.ny); usage(argv[0]); return EXIT_FAILURE; } // compute horizontal and vertical index of this rank udata.rh = udata.rank % udata.nx; udata.rv = udata.rank / udata.nx; if (debug) { fprintf(stderr, "%s:%d[%d]: rh = %d, rv = %d\n", __FILE__, __LINE__, udata.rank, udata.rh, udata.rv); } // next argument is image file name if (argc <= optind) { fprintf(stderr, "image file name argument missing\n"); usage(argv[0]); return EXIT_FAILURE; } char *imagefilename = argv[optind++]; // next argument is output file name char *netcdffilename = NULL; if (argc > optind) { netcdffilename = argv[optind++]; if (debug) { fprintf(stderr, "%s:%d: netcdffilename: %s\n", __FILE__, __LINE__, netcdffilename); } } // image file and output file heatfile_t *hf = NULL; image_t *image = NULL; // process zero initializes and writes data if (udata.rank == 0) { // read the image file image = readimage(imagefilename); if (NULL == image) { fprintf(stderr, "cannot read image\n"); return EXIT_FAILURE; } if (debug) { fprintf(stderr, "%s:%d[%d]: %d x %d image read\n", __FILE__, __LINE__, udata.rank, image->width, image->height); } // create the output file if (netcdffilename) { if (debug) { fprintf(stderr, "%s:%d: creating NetCDF %s\n", __FILE__, __LINE__, netcdffilename); } hf = output2_create(netcdffilename, h, steps * udata.ht, image->width, image->height); if (NULL == hf) { fprintf(stderr, "cannot create output file\n"); return EXIT_FAILURE; } } } // write the first image if ((basedir) && (udata.rank == 0)) { char outfilename[1024]; snprintf(outfilename, sizeof(outfilename), "%s/00000.fits", basedir); writeimage(image, outfilename); } // index ranges for each rank udata.ranges = (int *)malloc(4 * num_procs * sizeof(int)); if (udata.rank == 0) { partitiondomain(&udata, image); } // exchange range size information with all other ranks. The ranks // then pick the dimensions they need from the array, this is // the purpose of the range pointer MPI_Bcast(udata.ranges, 4 * num_procs, MPI_INT, 0, MPI_COMM_WORLD); int *range = &udata.ranges[4 * udata.rank]; if (debug) { fprintf(stderr, "%s:%d:[%d]: [%d,%d) x [%d,%d)\n", __FILE__, __LINE__, udata.rank, range[0], range[1], range[2], range[3]); } // allocate memory for the area we are responsible for udata.width = range[1] - range[0]; udata.height = range[3] - range[2]; allocate_u(&udata); double *unew = (double *)malloc(udata.length * sizeof(double)); if (debug) { fprintf(stderr, "%s:%d[%d]: arrays allocated, %d x %d\n", __FILE__, __LINE__, udata.rank, udata.width, udata.height); } // write initial data to the output file if ((hf) && (udata.rank == 0)) { output2_add(hf, 0, image->data); } // measure start time (after all allocations are done) double start = gettime(); // process 0 has to send the data to all the other processes if (udata.rank == 0) { for (int r = 1; r < num_procs; r++) { sendimagerange(&udata, image, r, tag); } copyfromimage(&udata, image); } else { // receive my part of the matrix receiverange(&udata, tag); } tag++; // make sure dimension is correct if (udata.dimension != udata.height + udata.width) { fprintf(stderr, "dimension does not match\n"); return EXIT_FAILURE; } // start the solver algorithm int stepcounter = 0; // counter for time steps while (stepcounter < udata.maxsteps) { stepcounter++; // copy everything to unew as the initial approximation for (int i = 0; i < udata.length; i++) { unew[i] = udata.u[i]; } // now perform <picturesteps> iterations for (int k = 0; k < udata.picturesteps; k++) { // synchronize current values of boundary with neighbors tag++; exchange_boundaries(&udata, tag); // perform iteration step iterate_u(unew, &udata); // copy the new u to the old u / only used for Jacobi if (udata.algorithm == 0) { for (int i = 0; i < udata.length; i++) { udata.u[i] = unew[i]; } } } // decide whether we have to output something if (0) { // time value for this data output int stepvalue = stepcounter / udata.picturesteps; // output needed, so we synchronize image data tag++; synchronize_image(&udata, image, tag); // write an image if ((basedir) && (udata.rank == 0)) { char outfilename[1024]; snprintf(outfilename, sizeof(outfilename), "%s/%05d.fits", basedir, stepvalue); writeimage(image, outfilename); } // write solution data if ((hf) && (udata.rank == 0)) { output2_add(hf, stepvalue, image->data); } } } // measure end time double end = gettime(); // we are now done, rank 0 displays the result if (udata.rank == 0) { printf("%.6f",end - start); } // close the netcdf file if ((udata.rank == 0) && (hf)) { output_close(hf); } // cleanup MPI MPI_Finalize(); // cleanup the memory we have allocated free(udata.ranges); udata.ranges = NULL; free_u(&udata); return EXIT_SUCCESS; }