void process(struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, void *i, void *o, const dt_iop_roi_t *roi_in, const dt_iop_roi_t *roi_out) { dt_iop_shrecovery_data_t *d = (dt_iop_shrecovery_data_t *)piece->data; const float scale = 1.0 + d->strength; const int ch = piece->colors; /* #ifdef _OPENMP #pragma omp parallel for default(none) shared(roi_out,i,o) schedule(static) #endif */ float *in = (float*)i, *out = (float*)o; float *W = (float*)malloc(2*sizeof(float)*roi_out->height*roi_out->width); float t1, t2, t3, mx; create_image_weight(roi_out->width, roi_out->height, ch, in, W, scale, d->mu, d->sigma); gauss_image_weight(d->size_limit, roi_out->width, roi_out->height, W); float *im1 = (float*)malloc(6*sizeof(float)*roi_out->height*roi_out->width); float *im2 = (float*)malloc(6*sizeof(float)*roi_out->height*roi_out->width); const int LENGTH = roi_out->width*roi_out->height; for(int i = 0; i < LENGTH; i++) { im1[i*3] = in[i*ch]; im1[i*3+1] = in[i*ch+1]; im1[i*3+2] = in[i*ch+2]; t1 = in[i*ch]*scale; t2 = in[i*ch+1]*scale; t3 = in[i*ch+2]*scale; mx = 1.0; mx = max(mx,t1); mx = max(mx,t2); mx = max(mx,t3); im2[i*3] = t1/mx; im2[i*3+1] = t2/mx; im2[i*3+2] = t3/mx; } laplace_image(d->size_limit, roi_out->width, roi_out->height, im1); laplace_image(d->size_limit, roi_out->width, roi_out->height, im2); weighted_image(d->size_limit, roi_out->width, roi_out->height, im1, im2, W); for(int i = 0; i < LENGTH; i++) { out[i*ch] = im1[3*i]; out[i*ch + 1] = im1[3*i+1]; out[i*ch + 2] = im1[3*i+2]; } free(W); free(im1); free(im2); }
static void laplace_image(const int size_limit, int width, int height, float *im) { float *imn = im + 3*width*height; int w = width/2, h = height/2, i1, j1; float *l1, *l2, *l3, *l4, *l5; int st1, st2, st3, st4, st5; for(int i = 0; i < h; i++) { l1 = im + max(2*i-2,0)*width*3; l2 = im + max(2*i-1,0)*width*3; l3 = im + 2*i*width*3; l4 = im + min(2*i+1,height-1)*width*3; l5 = im + min(2*i+2,height-1)*width*3; for(int j = 0; j < w; j++) { st1 = max(2*j-2,0)*3; st2 = max(2*j-1,0)*3; st3 = 6*j; st4 = min(2*j+1,width-1)*3; st5 = min(2*j+2,width-1)*3; imn[3*(i*w+j)] = 0.0625*(l2[st2]+l2[st4]+l4[st2]+l4[st4])+ 0.0125*(l1[st2]+l1[st4]+l2[st1]+l2[st5]+l4[st1]+l4[st5]+l5[st2]+l5[st4])+ 0.1*(l2[st3]+l3[st2]+l3[st4]+l4[st3])+ 0.16*(l3[st3])+ 0.0025*(l1[st1]+l1[st5]+l5[st1]+l5[st5])+ 0.02*(l1[st3]+l3[st1]+l3[st5]+l5[st3]); imn[3*(i*w+j)+1] = 0.0625*(l2[st2+1]+l2[st4+1]+l4[st2+1]+l4[st4+1])+ 0.0125*(l1[st2+1]+l1[st4+1]+l2[st1+1]+l2[st5+1]+l4[st1+1]+l4[st5+1]+l5[st2+1]+l5[st4+1])+ 0.1*(l2[st3+1]+l3[st2+1]+l3[st4+1]+l4[st3+1])+ 0.16*(l3[st3+1])+ 0.0025*(l1[st1+1]+l1[st5+1]+l5[st1+1]+l5[st5+1])+ 0.02*(l1[st3+1]+l3[st1+1]+l3[st5+1]+l5[st3+1]); imn[3*(i*w+j)+2] = 0.0625*(l2[st2+2]+l2[st4+2]+l4[st2+2]+l4[st4+2])+ 0.0125*(l1[st2+2]+l1[st4+2]+l2[st1+2]+l2[st5+2]+l4[st1+2]+l4[st5+2]+l5[st2+2]+l5[st4+2])+ 0.1*(l2[st3+2]+l3[st2+2]+l3[st4+2]+l4[st3+2])+ 0.16*(l3[st3+2])+ 0.0025*(l1[st1+2]+l1[st5+2]+l5[st1+2]+l5[st5+2])+ 0.02*(l1[st3+2]+l3[st1+2]+l3[st5+2]+l5[st3+2]); } } for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { for(int m = -2; m < 3; m++) { for(int n = -2; n < 3; n++) { if((i-m)%2) continue; if((j-n)%2) continue; i1 = min(max((i-m)/2,0),h-1); j1 = min(max((j-n)/2,0),w-1); im[3*(i*width+j)] -= 4*_w_[n+2]*_w_[m+2]*imn[3*(i1*w+j1)]; im[3*(i*width+j)+1] -= 4*_w_[n+2]*_w_[m+2]*imn[3*(i1*w+j1)+1]; im[3*(i*width+j)+2] -= 4*_w_[n+2]*_w_[m+2]*imn[3*(i1*w+j1)+2]; } } } } if((w/2>=size_limit)&&(h/2>=size_limit)) { laplace_image(size_limit, w,h,imn); } }
/*----------------------------------------------------------------------------*/ int main(int argc, char* argv[]) { int loop, test = 0, error = 0, command = COMMAND_NONE; int gray = 0, view = 1, help = 0; char *psave = 0x0, *pname = 0x0, *pdata = 0x0; my1Image currimage, tempimage, *pimage; my1IFrame currframe, tempframe; /* print tool info */ printf("\n%s - %s (%s)\n",MY1APP_PROGNAME,MY1APP_PROGINFO,MY1APP_PROGVERS); printf(" => by [email protected]\n\n"); /* check program arguments */ if(argc>1) { for(loop=1;loop<argc;loop++) { if(argv[loop][0]=='-') /* options! */ { if(!strcmp(argv[loop],"--save")) { loop++; if(loop<argc) psave = argv[loop]; else printf("Cannot get save file name - NOT saving!\n"); } else if(!strcmp(argv[loop],"--cdata")) { loop++; if(loop<argc) pdata = argv[loop]; else printf("Cannot get C data file name - NOT writing!\n"); } else if(!strcmp(argv[loop],"--gray")) { gray = 1; } else if(!strcmp(argv[loop],"--hide")) { view = 0; } else if(!strcmp(argv[loop],"--help")) { help = 1; } else { printf("Unknown option '%s'!\n",argv[loop]); } } else /* not an option? */ { /* first non-option must be file name! */ if(!pname) { pname = argv[loop]; continue; } /* then check for command! */ if(!strcmp(argv[loop],"laplace1")) { command = COMMAND_LAPLACE1; gray = 1; } else if(!strcmp(argv[loop],"sobelx")) { command = COMMAND_SOBELX; gray = 1; } else if(!strcmp(argv[loop],"sobely")) { command = COMMAND_SOBELY; gray = 1; } else if(!strcmp(argv[loop],"sobelall")) { command = COMMAND_SOBELALL; gray = 1; } else if(!strcmp(argv[loop],"laplace2")) { command = COMMAND_LAPLACE2; gray = 1; } else if(!strcmp(argv[loop],"gauss")) { command = COMMAND_GAUSS; gray = 1; } else { printf("Unknown parameter %s!\n",argv[loop]); continue; } /* warn if overriding previous command! */ if(command) { printf("Warning! Command '%s' overrides '%s'!\n", argv[loop],argv[test]); } test = loop; } } } /* check if user requested help */ if(help) { about(); return 0; } /** check input filename */ if(!pname) { printf("No filename given! Aborting!\n"); return ERROR_GENERAL; } /* initialize image & frame*/ initimage(&currimage); initimage(&tempimage); initframe(&currframe); initframe(&tempframe); /* try to open file */ if((error=load_image(&currimage,pname))<0) { return error; } /* display basic info */ printf("Input image: %s\n",pname); print_image_info(&currimage); /* convert grayscale if requested/required */ if(gray) grayscale_image(&currimage); /* process command */ switch(command) { case COMMAND_LAPLACE1: { createimage(&tempimage,currimage.height,currimage.width); laplace_image(&currimage,&tempimage); break; } case COMMAND_SOBELX: { createimage(&tempimage,currimage.height,currimage.width); sobel_x_image(&currimage,&tempimage); break; } case COMMAND_SOBELY: { createimage(&tempimage,currimage.height,currimage.width); sobel_y_image(&currimage,&tempimage); break; } case COMMAND_SOBELALL: { createimage(&tempimage,currimage.height,currimage.width); sobel_image(&currimage,&tempimage,0x0); break; } case COMMAND_LAPLACE2: { createimage(&tempimage,currimage.height,currimage.width); createframe(&currframe,currimage.height,currimage.width); createframe(&tempframe,currimage.height,currimage.width); image2frame(&currimage,&currframe,0); laplace_frame(&currframe,&tempframe); frame2image(&tempframe,&tempimage,1); break; } case COMMAND_GAUSS: { createimage(&tempimage,currimage.height,currimage.width); createframe(&currframe,currimage.height,currimage.width); createframe(&tempframe,currimage.height,currimage.width); image2frame(&currimage,&currframe,0); gauss_frame(&currframe,&tempframe,1.0,0x0); frame2image(&tempframe,&tempimage,1); break; } } if(!tempimage.length) pimage = &currimage; else pimage = &tempimage; printf("Check image:\n"); print_image_info(pimage); /** save results if requested */ if(psave) { printf("Saving image data to %s...\n",psave); error=save_image(pimage,psave); view = 0; } if(pdata) { printf("Saving C data to %s...\n",pdata); error=cdata_image(pimage,pdata); } /* view image if no request to hide! .. and NOT saving! */ if(view) view_image(pimage); /* cleanup */ freeframe(&currframe); freeframe(&tempframe); freeimage(&currimage); freeimage(&tempimage); return error; }