/* set pixels that have at least mask pixels around it set (including the * examined pixel itself) to black (foreground), all other pixels to white * (background) */ Imlib_Image set_pixels_filter(Imlib_Image *source_image, double thresh, luminance_t lt, int mask) { Imlib_Image new_image; /* construct filtered image here */ Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int x,y,i,j; /* iteration variables */ int set_pixel; /* should pixel be set or not? */ Imlib_Color color; int lum; /* luminance value of pixel */ /* save pointer to current image */ current_image = imlib_context_get_image(); /* create a new image */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); new_image = imlib_clone_image(); /* check for every pixel if it should be set in filtered image */ for(x=0; x<width; x++) { for(y=0; y<height; y++) { set_pixel=0; for(i=x-1; i<=x+1; i++) { for(j=y-1; j<=y+1; j++) { if(i>=0 && i<width && j>=0 && j<height) { /* i,j inside image? */ imlib_image_query_pixel(i, j, &color); lum = get_lum(&color, lt); if(is_pixel_set(lum, thresh)) { set_pixel++; } } } } /* set pixel if at least mask pixels around it are set */ if(set_pixel >= mask) { draw_fg_pixel(new_image, x, y); } else { draw_bg_pixel(new_image, x, y); } } } /* restore image from before function call */ imlib_context_set_image(current_image); /* return filtered image */ return new_image; }
/* compute dynamic threshold value from the rectangle (x,y),(x+w,y+h) of * source_image */ double get_threshold(Imlib_Image *source_image, double fraction, luminance_t lt, int x, int y, int w, int h) { Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int xi,yi; /* iteration variables */ Imlib_Color color; int lum; /* luminance of pixel */ double minval=(double)MAXRGB, maxval=0.0; /* save pointer to current image */ current_image = imlib_context_get_image(); /* get image dimensions */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); /* special value -1 for width or height means image width/height */ if(w == -1) w = width; if(h == -1) h = width; /* assure valid coordinates */ if(x+w > width) x = width-w; if(y+h > height) y = height-h; if(x<0) x=0; if(y<0) y=0; /* find the threshold value to differentiate between dark and light */ for(xi=0; (xi<w) && (xi<width); xi++) { for(yi=0; (yi<h) && (yi<height); yi++) { imlib_image_query_pixel(x+xi, y+yi, &color); lum = get_lum(&color, lt); if(lum < minval) minval = lum; if(lum > maxval) maxval = lum; } } /* restore image from before function call */ imlib_context_set_image(current_image); return (minval + fraction * (maxval - minval)) * 100 / MAXRGB; }
/* get maximum luminance value */ double get_maxval(Imlib_Image *source_image, int x, int y, int w, int h, luminance_t lt) { Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int xi,yi; /* iteration variables */ Imlib_Color color; /* Imlib2 RGBA color structure */ int lum = 0; int maxval = 0; /* save pointer to current image */ current_image = imlib_context_get_image(); /* get image dimensions */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); /* special value -1 for width or height means image width/height */ if(w == -1) w = width; if(h == -1) h = width; /* assure valid coordinates */ if(x+w > width) x = width-w; if(y+h > height) y = height-h; if(x<0) x=0; if(y<0) y=0; /* find the minimum value in the image */ for(xi=0; (xi<w) && (xi<width); xi++) { for(yi=0; (yi<h) && (yi<height); yi++) { imlib_image_query_pixel(xi, yi, &color); lum = clip(get_lum(&color, lt),0,255); if(lum > maxval) maxval = lum; } } /* restore image from before function call */ imlib_context_set_image(current_image); return maxval; }
int main(int ac, char **av) { t_lum *tab_lum; t_pos test; t_win mlxwin; ac != 4 ? exit (1) : 0; (mlxwin.mlx = mlx_init()) == NULL ? exit (1) : 0; (mlxwin.win = mlx_new_window(mlxwin.mlx, 1000, 1000, "RTV1")) == NULL ?\ exit (1) : 0; (mlxwin.img = mlx_new_image(mlxwin.mlx, 1000, 1000)) == NULL ? exit (1) : 0; get_eye(&mlxwin, av[1]); mlxwin.tab_obj = get_obj(av[2]); mlxwin.tab_lum = get_lum(av[3]); remplis_image(&mlxwin); mlx_key_hook(mlxwin.win, gere_key, &mlxwin); mlx_expose_hook(mlxwin.win, gere_expose, &mlxwin); mlx_loop(mlxwin.mlx); return (0); }
void get_light(t_scene *s, t_intersect *inter, double *col) { int i; int m; double max; double total; i = m = -1; while (++i < 3) col[i] = s->spec.bg_color.argb[i] / 255.0 * s->spec.ambiant; max = total = 0; while (++m < s->nb_light) { get_lum(col, s, inter, m); max = MAX(s->lights[m].light.power, max); total += s->lights[m].light.power; } i = -1; while (++i < 3) col[i] /= total / max; }
/* ww and wh are the width and height of the rectangle used to find the * threshold value */ Imlib_Image dynamic_threshold(Imlib_Image *source_image,double t,luminance_t lt, int ww, int wh) { Imlib_Image new_image; /* construct filtered image here */ Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int x,y; /* iteration variables */ Imlib_Color color; int lum; double thresh; /* save pointer to current image */ current_image = imlib_context_get_image(); /* create a new image */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); new_image = imlib_clone_image(); /* check for every pixel if it should be set in filtered image */ for(x=0; x<width; x++) { for(y=0; y<height; y++) { imlib_image_query_pixel(x, y, &color); lum = get_lum(&color, lt); thresh = get_threshold(source_image, t/100.0, lt, x-ww/2, y-ww/2, ww, wh); if(is_pixel_set(lum, thresh)) { draw_fg_pixel(new_image, x, y); } else { draw_bg_pixel(new_image, x, y); } } } /* restore image from before function call */ imlib_context_set_image(current_image); /* return filtered image */ return new_image; }
/* turn image to grayscale */ Imlib_Image grayscale(Imlib_Image *source_image, luminance_t lt) { Imlib_Image new_image; /* construct grayscale image here */ Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int x,y; /* iteration variables */ Imlib_Color color; /* Imlib2 color structure */ int lum=0; /* save pointer to current image */ current_image = imlib_context_get_image(); /* create a new image */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); new_image = imlib_clone_image(); /* transform image to grayscale */ for(x=0; x<width; x++) { for(y=0; y<height; y++) { imlib_context_set_image(*source_image); imlib_image_query_pixel(x, y, &color); imlib_context_set_image(new_image); lum = clip(get_lum(&color, lt),0,255); imlib_context_set_color(lum, lum, lum, color.alpha); imlib_image_draw_pixel(x, y, 0); } } /* restore image from before function call */ imlib_context_set_image(current_image); /* return filtered image */ return new_image; }
/* determine threshold by an iterative method */ double iterative_threshold(Imlib_Image *source_image, double thresh, luminance_t lt, int x, int y, int w, int h) { Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int xi,yi; /* iteration variables */ Imlib_Color color; int lum; /* luminance of pixel */ unsigned int size_white, size_black; /* size of black and white groups */ unsigned long int sum_white, sum_black; /* sum of black and white groups */ unsigned int avg_white, avg_black; /* average values of black and white */ double old_thresh; /* old threshold computed by last iteration step */ double new_thresh; /* new threshold computed by current iteration step */ int thresh_lum; /* luminance value of threshold */ /* normalize threshold (was given as a percentage) */ new_thresh = thresh / 100.0; /* save pointer to current image */ current_image = imlib_context_get_image(); /* get image dimensions */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); /* special value -1 for width or height means image width/height */ if(w == -1) w = width; if(h == -1) h = width; /* assure valid coordinates */ if(x+w > width) x = width-w; if(y+h > height) y = height-h; if(x<0) x=0; if(y<0) y=0; /* find the threshold value to differentiate between dark and light */ do { thresh_lum = MAXRGB * new_thresh; old_thresh = new_thresh; size_black = sum_black = size_white = sum_white = 0; for(xi=0; (xi<w) && (xi<width); xi++) { for(yi=0; (yi<h) && (yi<height); yi++) { imlib_image_query_pixel(xi, yi, &color); lum = get_lum(&color, lt); if(lum <= thresh_lum) { size_black++; sum_black += lum; } else { size_white++; sum_white += lum; } } } if(!size_white) { fprintf(stderr, "iterative_threshold(): error: no white pixels\n"); imlib_context_set_image(current_image); return thresh; } if(!size_black) { fprintf(stderr, "iterative_threshold(): error: no black pixels\n"); imlib_context_set_image(current_image); return thresh; } avg_white = sum_white / size_white; avg_black = sum_black / size_black; new_thresh = (avg_white + avg_black) / (2.0 * MAXRGB); /*fprintf(stderr, "iterative_threshold(): new_thresh = %f\n", new_thresh);*/ } while(fabs(new_thresh - old_thresh) > EPSILON); /* restore image from before function call */ imlib_context_set_image(current_image); return new_thresh * 100; }
/* gray stretching, i.e. lum<t1 => lum=0, lum>t2 => lum=100, * else lum=((lum-t1)*MAXRGB)/(t2-t1) */ Imlib_Image gray_stretch(Imlib_Image *source_image, double t1, double t2, luminance_t lt) { Imlib_Image new_image; /* construct filtered image here */ Imlib_Image current_image; /* save image pointer */ int height, width; /* image dimensions */ int x,y; /* iteration variables */ Imlib_Color color; int lum; /* luminance value of pixel */ /* do nothing if t1>=t2 */ if(t1 >= t2) { fprintf(stderr, "error: gray_stretch(): t1=%.2f >= t2=%.2f\n", t1, t2); exit(99); } /* check if 0 < t1,t2 < MAXRGB */ if(t1 <= 0.0) { fprintf(stderr, "error: gray_stretch(): t1=%.2f <= 0.0\n", t1); exit(99); } if(t2 >= MAXRGB) { fprintf(stderr, "error: gray_stretch(): t2=%.2f >= %d.0\n", t2, MAXRGB); exit(99); } /* save pointer to current image */ current_image = imlib_context_get_image(); /* create a new image */ imlib_context_set_image(*source_image); height = imlib_image_get_height(); width = imlib_image_get_width(); new_image = imlib_clone_image(); /* gray stretch image */ for(x=0; x<width; x++) { for(y=0; y<height; y++) { imlib_image_query_pixel(x, y, &color); lum = get_lum(&color, lt); imlib_context_set_image(new_image); if(lum<=t1) { imlib_context_set_color(0, 0, 0, color.alpha); } else if(lum>=t2) { imlib_context_set_color(MAXRGB, MAXRGB, MAXRGB, color.alpha); } else { imlib_context_set_color(clip(((lum-t1)*255)/(t2-t1),0,255), clip(((lum-t1)*255)/(t2-t1),0,255), clip(((lum-t1)*255)/(t2-t1),0,255), color.alpha); } imlib_image_draw_pixel(x, y, 0); imlib_context_set_image(*source_image); } } /* restore image from before function call */ imlib_context_set_image(current_image); /* return filtered image */ return new_image; }