From 76788817faaeb3c8a4fd58b1fc6c670257228001 Mon Sep 17 00:00:00 2001
From: Nicola Vigano <nicola.vigano@esrf.fr>
Date: Mon, 26 Feb 2018 00:41:00 +0100
Subject: [PATCH] Figures-extras: added image inset generation

Signed-off-by: Nicola Vigano <nicola.vigano@esrf.fr>
---
 zUtil_Drawing/gtFigureAddExtras.m    | 48 ++++++++++++++++++++--------
 zUtil_Drawing/gtFigureCreateExtras.m | 18 +++++++++--
 2 files changed, 49 insertions(+), 17 deletions(-)

diff --git a/zUtil_Drawing/gtFigureAddExtras.m b/zUtil_Drawing/gtFigureAddExtras.m
index 2275a781..01db4e2b 100644
--- a/zUtil_Drawing/gtFigureAddExtras.m
+++ b/zUtil_Drawing/gtFigureAddExtras.m
@@ -28,8 +28,10 @@ function apply_extras(ax, im_props, extras)
             add_arrow(ax, extras)
         case 'segment'
             add_segment(ax, extras)
-        case 'inset'
-            add_inset(ax, extras)
+        case 'inset_zoom'
+            add_inset(ax, extras, 'zoom')
+        case 'inset_image'
+            add_inset(ax, extras, 'image')
     end
 end
 
@@ -147,25 +149,46 @@ function add_segment(ax, ex_props)
         'LineWidth', ex_props.line_width, 'LineStyle', ex_props.line_style)
 end
 
-function add_inset(ax, ex_props)
+function add_inset(ax, ex_props, type)
 
-    cdata = permute(getimage(ax), [2 1 3]);
+    switch (lower(type))
+        case 'zoom'
+            cdata = permute(getimage(ax), [2 1 3]);
 
-    shifts = min(ex_props.region(3:4) ./ ex_props.position(3:4) - 0.5, 0);
-    region_start = ex_props.region(1:2) + shifts;
-    region_end = ex_props.region(1:2) + ex_props.region(3:4) - 1 - shifts;
-    yy = linspace(region_start(1), region_end(1), ex_props.position(3));
-    xx = linspace(region_start(2), region_end(2), ex_props.position(4));
+            method_type = 'nearest';
+            oversampling = [1, 1];
+
+            shifts = min(ex_props.region(3:4) ./ ex_props.position(3:4) - 0.5, 0);
+            region_start = ex_props.region(1:2) + shifts;
+            region_end = ex_props.region(1:2) + ex_props.region(3:4) - 1 - shifts;
+        case 'image'
+            cdata = im2double(permute(ex_props.image, [2 1 3]));
+            region = [1, 1, size(cdata, 1), size(cdata, 2)];
+
+            method_type = 'nearest';
+            oversampling = ceil(region(3:4) ./ ex_props.position(3:4));
+
+            shifts = min(region(3:4) ./ (ex_props.position(3:4) .* oversampling) - 0.5, 0);
+            region_start = region(1:2) + shifts;
+            region_end = region(1:2) + region(3:4) - 1 - shifts;
+        otherwise
+            error('gtFigureAddExtras:wrong_argument', ...
+                'No known inset type: %s', type)
+    end
+    yy = linspace(region_start(1), region_end(1), ex_props.position(3) * oversampling(1));
+    xx = linspace(region_start(2), region_end(2), ex_props.position(4) * oversampling(2));
     if (numel(size(cdata)) == 3)
         zz = [1 2 3];
         [xx, yy, zz] = ndgrid(xx, yy, zz);
 
-        inset_data = interp3(cdata, xx, yy, zz, 'nearest');
+        inset_data = interp3(cdata, xx, yy, zz, method_type, 0);
     else
         [xx, yy] = ndgrid(xx, yy);
 
-        inset_data = interp2(cdata, xx, yy, 'nearest');
+        inset_data = interp2(cdata, xx, yy, method_type, 0);
     end
+    inset_data = reshape(inset_data, oversampling(2), ex_props.position(4), oversampling(1), ex_props.position(3), []);
+    inset_data = squeeze(sum(sum(inset_data, 1), 3)) / prod(oversampling);
 
     hold(ax, 'on')
     % watch out for the inversion of coordinates xx and yy here
@@ -175,8 +198,5 @@ function add_inset(ax, ex_props)
     hold(ax, 'off')
 
     add_box(ax, ex_props);
-    ex_props_region = ex_props;
-    ex_props_region.position = ex_props_region.region;
-    add_box(ax, ex_props_region);
 end
 
diff --git a/zUtil_Drawing/gtFigureCreateExtras.m b/zUtil_Drawing/gtFigureCreateExtras.m
index 9ac356cf..e3544d3c 100644
--- a/zUtil_Drawing/gtFigureCreateExtras.m
+++ b/zUtil_Drawing/gtFigureCreateExtras.m
@@ -12,8 +12,10 @@ function extras = gtFigureCreateExtras(type, varargin)
             extras = create_arrow(varargin{:});
         case 'segment'
             extras = create_segment(varargin{:});
-        case 'inset'
-            extras = create_inset(varargin{:});
+        case 'inset_zoom'
+            extras = create_inset_zoom(varargin{:});
+        case 'inset_image'
+            extras = create_inset_image(varargin{:});
     end
     extras.type = type;
 end
@@ -84,7 +86,7 @@ function extras = create_segment(varargin)
     extras = parse_pv_pairs(extras, varargin);
 end
 
-function extras = create_inset(varargin)
+function extras = create_inset_zoom(varargin)
     extras = struct( ...
         'line_width', 1, ...
         'line_style', '-', ...
@@ -94,3 +96,13 @@ function extras = create_inset(varargin)
     extras = parse_pv_pairs(extras, varargin);
 end
 
+function extras = create_inset_image(varargin)
+    extras = struct( ...
+        'line_width', 1, ...
+        'line_style', '-', ...
+        'image', [], ...
+        'position', [1 1 10 10], ...
+        'color', [1 1 1] );
+    extras = parse_pv_pairs(extras, varargin);
+end
+
-- 
GitLab