[giv] Added support for pixel grid, make auto-reload immune to failures, overlay export with marks off



commit fa2d98171d4e35069a5aeaf8a5f9c04481f1e6ce
Author: Dov Grobgeld <dov grobgeld gmail com>
Date:   Sun Oct 30 00:27:20 2011 +0200

    Added support for pixel grid, make auto-reload immune to failures, overlay export with marks off
    
    2011-10-30  Dov Grobgeld  <dov grobgeld gmail com>
    
            * giv-win.gob, giv-widget.gob : Added support for pixel grid.
    
    2011-10-29  Dov Grobgeld  <dov grobgeld gmail com>
    
            * giv-win.gob : On auto-reload make image read failures silently ignored.
    
            * giv-win.gob : Fixed bug that overlay was exported even if mark view was off.

 ChangeLog          |   12 +++++++++-
 src/GivPainter.h   |    8 +++++++
 src/giv-widget.gob |   50 ++++++++++++++++++++++++++++++++++++++++++++
 src/giv-win.gob    |   59 ++++++++++++++++++++++++++++++++++++++++++---------
 src/menu-top.xml   |    1 +
 5 files changed, 118 insertions(+), 12 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 403e96c..26a381d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,16 @@
+2011-10-30  Dov Grobgeld  <dov grobgeld gmail com>
+
+        * giv-win.gob, giv-widget.gob : Added support for pixel grid.
+
+2011-10-29  Dov Grobgeld  <dov grobgeld gmail com>
+
+        * giv-win.gob : On auto-reload make image read failures silently ignored.
+
+        * giv-win.gob : Fixed bug that overlay was exported even if mark view was off.
+
 2011-09-25  Dov Grobgeld  <dov grobgeld gmail com>
 
-        * GivRenderer.gob : Temporary fix for clipping error. 
+        * GivRenderer.gob : Temporary fix for clipping error.
 
         * giv-win.gob, giv-parser.gob : Added tool for counting marks in rectangle.
 
diff --git a/src/GivPainter.h b/src/GivPainter.h
index e8c34d7..cfd165a 100644
--- a/src/GivPainter.h
+++ b/src/GivPainter.h
@@ -20,6 +20,14 @@ class GivPainter {
                            double green,
                            double blue,
                            double alpha=1.0) = 0;
+    void set_giv_color(GivColor giv_color)
+    {
+        static const double s = 1.0/0xffff;
+        set_color(giv_color.red * s,
+                  giv_color.green * s,
+                  giv_color.blue * s,
+                  giv_color.alpha * s);
+    }
     virtual int set_line_width(double line_width) = 0;
     virtual int set_text_size(double text_size) = 0;
     virtual int set_font(const char* font) = 0;
diff --git a/src/giv-widget.gob b/src/giv-widget.gob
index 434bb07..6466d51 100644
--- a/src/giv-widget.gob
+++ b/src/giv-widget.gob
@@ -43,6 +43,7 @@ typedef void (*giv_widget_file_reference_callback_t)(const char *filename,
 #include "GivRenderer.h"
 #include "colormaps.h"
 #include "giv-data.h"
+#include "math.h"
 
 using namespace std;
 
@@ -91,6 +92,7 @@ class Giv:Widget from Gtk:Image:Viewer
     private bool do_view_cross_hair = FALSE;
     private bool do_show_balloon = FALSE;
     private bool do_no_transparency = FALSE;
+    private bool do_show_grid = FALSE;
     private gint last_cx = 0;
     private gint last_cy = 0;
     private gint last_move_x = 0;
@@ -101,6 +103,8 @@ class Giv:Widget from Gtk:Image:Viewer
     private GivPseudoColor colormap = GIV_PSEUDO_COLOR_OFF;
     private giv_widget_file_reference_callback_t file_reference_cb;
     private gpointer file_reference_user_data;
+    private int pixel_grid_min_zoom = 5;
+    private GivColor pixel_grid_color;
    
     public GtkWidget *
     new (GdkPixbuf *pixbuf)
@@ -130,6 +134,12 @@ class Giv:Widget from Gtk:Image:Viewer
                                  GTK_STATE_NORMAL, &color);
         }
 
+        // Default grid color. This should be made configurable
+        selfp->pixel_grid_color.red = 0xffff;
+        selfp->pixel_grid_color.green = 0;
+        selfp->pixel_grid_color.blue = 0;
+        selfp->pixel_grid_color.alpha = 0x8000;
+
         gtk_widget_set_app_paintable (selfp->w_balloon_window, TRUE);
         gtk_window_set_resizable (GTK_WINDOW(selfp->w_balloon_window), FALSE);
         selfp->w_balloon_label = gtk_label_new("Balloon");
@@ -342,6 +352,13 @@ class Giv:Widget from Gtk:Image:Viewer
     }
 
     public
+    void set_show_grid(self,
+                          bool do_show_grid)
+    {
+      selfp->do_show_grid = do_show_grid;
+    }
+
+    public
     void set_colormap(self,
                       GivPseudoColor pseudo_color)
     {
@@ -388,6 +405,10 @@ class Giv:Widget from Gtk:Image:Viewer
             giv_widget_set_show_marks(self, !selfp->do_view_marks);
             gtk_image_viewer_redraw(GTK_IMAGE_VIEWER(self), true);
             return 1;
+        case 'g':
+            giv_widget_set_show_grid(self, !selfp->do_show_grid);
+            gtk_image_viewer_redraw(GTK_IMAGE_VIEWER(self), true);
+            return 1;
         case 'x':
             giv_widget_set_show_cross_hair(self, !selfp->do_view_cross_hair);
             gtk_image_viewer_redraw(GTK_IMAGE_VIEWER(self), true);
@@ -589,6 +610,35 @@ cb_image_annotate(GivWidget *self,
         }
     }
 
+    // Draw the grid
+    if (selfp->do_show_grid && scale_x > selfp->pixel_grid_min_zoom) {
+        // img bounding box of image currently shown
+        int x0, x1, y0, y1;
+        // px = ix * scale_x - shift_x
+        // ix = (px+shift_x)/scale_x
+        x0 = (int)floor(1.0*shift_x/scale_x);
+        y0 = (int)floor(1.0*shift_y/scale_y);
+        x1 = (int)ceil((width+shift_x)/scale_x);
+        y1 = (int)ceil((height+shift_y)/scale_y);
+        double color_scale = 1.0/0xffff;
+
+        painter.set_giv_color(selfp->pixel_grid_color);
+        painter.set_line_width(1);
+        for (int i=x0; i<=x1; i++) {
+            double px = i*scale_x - shift_x;
+
+            painter.add_line_segment(px,0, px,height);
+            painter.stroke();
+        }
+        for (int i=y0; i<=y1; i++) {
+            double py = i*scale_y - shift_y;
+
+            painter.add_line_segment(0,py, width,py);
+            painter.stroke();
+        }
+        painter.fill();
+    }
+
     // Create the label image necessary for the balloon popup. The
     // balloon image is always the size of the total displayed area.
     // It would be faster to store and scroll the label image. But
diff --git a/src/giv-win.gob b/src/giv-win.gob
index e6ff0e4..a1b6785 100644
--- a/src/giv-win.gob
+++ b/src/giv-win.gob
@@ -100,6 +100,7 @@ static void cb_view_hflip (GtkAction *action, gpointer data);
 static void cb_view_vflip (GtkAction *action, gpointer data);
 static void cb_view_colormap (GtkAction *action, gpointer data);
 static void cb_view_cross_hair (GtkAction *action, gpointer data);
+static void cb_view_pixel_grid (GtkAction *action, gpointer data);
 static void cb_view_auto_resize (GtkAction *action, gpointer data);
 static void cb_view_auto_contrast (GtkAction *action, gpointer data);
 static void cb_view_auto_reload (GtkAction *action, gpointer data);
@@ -371,6 +372,11 @@ static GtkToggleActionEntry toggle_entries[] =
     "View Cross Hair", NULL,    
     "View Cross hair",
     G_CALLBACK (cb_view_cross_hair), FALSE },
+  { "ViewPixelGrid",
+    NULL,
+    "View Pixel Grid", NULL,    
+    "View Pixel Grid",
+    G_CALLBACK (cb_view_pixel_grid), FALSE },
   { "AutoResize",
     NULL,
     "_Auto Resize", NULL,    
@@ -487,6 +493,11 @@ class Giv:Win from Gtk:Window
     private bool do_square_aspect_ratio = true;
     private bool do_auto_fit_marks = TRUE;
     private bool do_auto_reload = FALSE;
+    private bool do_draw_pixel_grid = TRUE;
+    // If the image is triggered to be loaded by auto reload then the
+    // a failure to load is silently ignored. This is handled by the
+    // auto_relod_trigger flag;
+    private bool auto_reload_trigger = FALSE; 
     private bool do_auto_contrast = TRUE;
     private bool do_no_transparency = FALSE;
     private gdouble last_move_x=-1;
@@ -971,13 +982,23 @@ class Giv:Win from Gtk:Window
         }
 
         if (error) {
-            app_error(self,
-                      "Failed to load image %s: %s\n",
-                      filename, error->message);
+            // Silently ignore image failures on auto reload
+            if (selfp->do_auto_reload && selfp->auto_reload_trigger) {
+                // Force a new test
+                selfp->cached_mtime = 0;
+                return;
+            }
+            else {
+                app_error(self,
+                          "Failed to load image %s: %s\n",
+                          filename, error->message);
+                return;
+            }
             g_error_free(error);
-            return;
         }
         
+        selfp->auto_reload_trigger = false;
+        
         if (selfp->img_org)
             giv_image_unref(selfp->img_org);
         if (selfp->img_display) {
@@ -1465,6 +1486,7 @@ static gboolean auto_reload_function(gpointer data)
     stat(fn, &st);
     if (st.st_mtime != selfp->cached_mtime) {
         selfp->cached_mtime = st.st_mtime;
+        selfp->auto_reload_trigger = TRUE;
         giv_win_set_current_file(self, selfp->filename_list_index);
     }
 
@@ -2036,6 +2058,19 @@ static void cb_view_cross_hair (GtkAction *action, gpointer data)
     giv_win_redraw(self);
 }
 
+static void cb_view_pixel_grid (GtkAction *action, gpointer data)
+{
+    GivWin *self = GIV_WIN(data);
+    GtkWidget *menu_widget = gtk_ui_manager_get_widget(selfp->menu_manager,
+                                                       "/menubar/ViewMenu/CrossHair");
+
+    gboolean old_val = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu_widget));
+    giv_widget_set_show_grid(GIV_WIDGET(selfp->w_imgv), !old_val);
+    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_widget), !old_val);
+
+    giv_win_redraw(self);
+}
+
 static void cb_view_auto_resize (GtkAction *action, gpointer data)
 {
     GivWin *self = GIV_WIN(data);
@@ -2739,13 +2774,15 @@ void draw_to_cairo(cairo_t *cr,
         gdk_pixbuf_unref(cimg);
     }
 
-    GivPainterCairo painter(cr);
-    GivRenderer renderer(datasets, painter,
-                         scale_x, scale_y,
-                         shift_x, shift_y,
-                         cnv_width, cnv_height);
-    renderer.set_do_no_transparency(selfp->do_no_transparency);
-    renderer.paint();
+    if (selfp->do_view_marks) {
+        GivPainterCairo painter(cr);
+        GivRenderer renderer(datasets, painter,
+                             scale_x, scale_y,
+                             shift_x, shift_y,
+                             cnv_width, cnv_height);
+        renderer.set_do_no_transparency(selfp->do_no_transparency);
+        renderer.paint();
+    }
 #endif
 }
 
diff --git a/src/menu-top.xml b/src/menu-top.xml
index 4e80f68..0273c16 100644
--- a/src/menu-top.xml
+++ b/src/menu-top.xml
@@ -17,6 +17,7 @@
       <menuitem name="HorizontalFlip" action="ViewHorizontalFlip" />
       <menuitem name="VerticalFlip" action="ViewVerticalFlip" />
       <menuitem name="CrossHair" action="ViewCrossHair" />
+      <menuitem name="Pixel-grid" action="ViewPixelGrid" />
       <menuitem name="ColorMapBar" action="ViewColorMap" />
       <menuitem name="AutoResize" action="AutoResize" />
       <menuitem name="AutoContrast" action="AutoContrast" />



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]