Fixed refresh of image area, it was not completely refreshed
[colorize.git] / gui / image.c
index 70f5cb7..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,11 +78,65 @@ void create_image(const char *filename)
 
 no_mark:
        /* create pixbuf */
-       create_or_reset_pixbuf();
+       create_or_reset_pixbuf(resize);
 }
 
-void create_or_reset_pixbuf(void)
+void create_or_reset_pixbuf(int resize)
 {
+       int w, h, dw = 0, dh = 0;
+       GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(main_window));
+       GdkRectangle max;
+
+       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-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) {
+                               if (img_scale_x > 4) {
+                                       int aspect = img_scale_y / img_scale_x;
+                                       img_scale_x -= 4;
+                                       img_scale_y = img_scale_x * aspect;
+                                       goto try_smaller;
+                               }
+                               w = max.width;
+                       }
+                       dw = w - main_window->allocation.width;
+               }
+               if (h < img_height*img_scale_y/16) {
+                       h = main_window->allocation.height - h + img_height*img_scale_y/16;
+                       if (h > max.height) {
+                               if (img_scale_x > 4) {
+                                       int aspect = img_scale_y / img_scale_x;
+                                       img_scale_x -= 4;
+                                       img_scale_y = img_scale_x * aspect;
+                                       goto try_smaller;
+                               }
+                               h = max.height;
+                       }
+                       dh = h - main_window->allocation.height;
+               }
+//             printf("%d %d\n", gdk_screen_get_width(screen), gdk_screen_get_height(screen));
+               if (dw || dh)
+                       gtk_window_resize(GTK_WINDOW(main_window), main_window->allocation.width+dw, main_window->allocation.height+dh);
+       }
+
        if (img_pixbuf) {
                g_object_unref(img_pixbuf);
                img_pixbuf = NULL;
@@ -90,7 +144,6 @@ void create_or_reset_pixbuf(void)
        img_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, img_width*img_scale_x/16, img_height*img_scale_y/16);
 
        gtk_drawing_area_size(GTK_DRAWING_AREA(img_drawing_area), img_width*img_scale_x/16, img_height*img_scale_y/16);
-//     gtk_widget_set_size_request(drawing_area, img_width, img_height);
 
        /* label */
        timeline_show_name(timeline_selected);
@@ -126,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];
@@ -224,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;
@@ -258,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)) {
@@ -275,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;
+                                               }
                                        }
                                }
                        }