Fixed refresh of image area, it was not completely refreshed
[colorize.git] / gui / image.c
index 1590279..a8f18ce 100644 (file)
@@ -18,7 +18,7 @@ GdkPixbuf *img_pixbuf = NULL;
 int img_width, img_height;
 int copy_width, copy_height;
 char img_name[256];
-unsigned char *img_grey_buffer = NULL;
+unsigned short *img_grey_buffer = NULL;
 unsigned char *img_mark_buffer = NULL, *img_mark_buffer_undo[UNDO_MAX], *img_mark_buffer_copy;
 int button_down_x_undo[UNDO_MAX], button_down_y_undo[UNDO_MAX];
 int undo_current, undo_num;
@@ -27,7 +27,7 @@ int anything_modified;
 extern int button_down_x, button_down_y;
 
 /* load image and create pixbuf */
-void create_image(const char *filename)
+void create_image(const char *filename, int resize)
 {
        int rc;
        static char imgfile[256];
@@ -78,7 +78,7 @@ void create_image(const char *filename)
 
 no_mark:
        /* create pixbuf */
-       create_or_reset_pixbuf(1);
+       create_or_reset_pixbuf(resize);
 }
 
 void create_or_reset_pixbuf(int resize)
@@ -87,21 +87,25 @@ void create_or_reset_pixbuf(int resize)
        GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(main_window));
        GdkRectangle max;
 
-       /* calculate the maximum windows size, so that the border will not exceed the screen size */
-       gdk_window_get_frame_extents(gtk_widget_get_window(main_window), &max);
-       max.width -= main_window->allocation.width;
-       max.height -= main_window->allocation.height;
-       if (max.width < 0)
-               max.width = 0;
-       if (max.height < 0)
-               max.height = 0;
-       max.width = gdk_screen_get_width(screen)-max.width;
-       max.height = gdk_screen_get_height(screen)-max.height;
-
        if (resize && screen) {
+               /* process GTK window event to have correct size */
+               while (gtk_events_pending())
+                       gtk_main_iteration();
+
+               /* calculate the maximum windows size, so that the border will not exceed the screen size */
+               gdk_window_get_frame_extents(gtk_widget_get_window(main_window), &max);
+               max.width -= main_window->allocation.width;
+               max.height -= main_window->allocation.height;
+               if (max.width < 0)
+                       max.width = 0;
+               if (max.height < 0)
+                       max.height = 0;
+               max.width = gdk_screen_get_width(screen)-max.width;
+               max.height = gdk_screen_get_height(screen)-max.height;
+
 try_smaller:
-               w = img_scroll->allocation.width-20;
-               h = img_scroll->allocation.height-20;
+               w = img_scroll->allocation.width-25;
+               h = img_scroll->allocation.height-25;
                if (w < img_width*img_scale_x/16) {
                        w = main_window->allocation.width - w + img_width*img_scale_x/16;
                        if (w > max.width) {
@@ -175,12 +179,13 @@ void draw_image(int x, int y, int w, int h)
        int window_width, window_height, x_offset, y_offset;
        gdk_drawable_get_size (draw, &window_width, &window_height);
        double _r, _g, _b, _y, _u, _v, u_palette, v_palette;
+       int cr, cg, cb;
        int _c, preview_asis = 0;
 
        if (w < 0)
                w = window_width;
        if (h < 0)
-               h = window_width;
+               h = window_height;
 
        guchar *data;
        unsigned char compose[w*3];
@@ -273,9 +278,9 @@ void draw_image(int x, int y, int w, int h)
                        for (j = 0; j < w; j++) {
                                if (preview && !rendered && !flowview) {
                                        /* apply selected color from palette to all pixles */
-                                       _r = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3] / 255.0F;
-                                       _g = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+1] / 255.0F;
-                                       _b = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+2] / 255.0F;
+                                       _r = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3] / 65535.0F;
+                                       _g = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+1] / 65535.0F;
+                                       _b = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+2] / 65535.0F;
                                        rgb2yuv_pixle(_r, _g, _b, &_y, &_u, &_v);
                                        _y = (_y - 0.5) * mark_palette[mark_selected].contrast + 0.5;
                                        _y += mark_palette[mark_selected].bright;
@@ -307,9 +312,9 @@ void draw_image(int x, int y, int w, int h)
                                        compose[j*3+2] = _c;
 
                                } else {
-                                       compose[j*3] = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3];
-                                       compose[j*3+1] = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+1];
-                                       compose[j*3+2] = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+2];
+                                       compose[j*3] = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3] >> 8;
+                                       compose[j*3+1] = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+1] >> 8;
+                                       compose[j*3+2] = img_grey_buffer[((j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y))*3+2] >> 8;
                                }
                                c = img_mark_buffer[(j+x)*16/img_scale_x+img_width*((i+y)*16/img_scale_y)];
                                if (c > 0 && !flowview && !(rendered && preview)) {
@@ -324,9 +329,17 @@ void draw_image(int x, int y, int w, int h)
                                                        compose[j*3+2] = 128;
                                                }
                                        } else {
-                                               compose[j*3] = mark_palette[c-1].r;
-                                               compose[j*3+1] = mark_palette[c-1].g;
-                                               compose[j*3+2] = mark_palette[c-1].b;
+                                               cr = mark_palette[c-1].r;
+                                               cg = mark_palette[c-1].g;
+                                               cb = mark_palette[c-1].b;
+                                               if (cr == 255 && cg == 255 && cb == 255) {
+                                                       compose[j*3] = compose[j*3+1] = compose[j*3+2] = ((((i+y)>>3)&1) == (((j+x)>>3)&1)) ? 255 : 192;
+                                               } else
+                                               {
+                                                       compose[j*3] = cr;
+                                                       compose[j*3+1] = cg;
+                                                       compose[j*3+2] = cb;
+                                               }
                                        }
                                }
                        }