# HG changeset patch
# User Markus Mützel <markus.muetzel@gmx.de>
# Date 1628968927 -10800
#      Sat Aug 14 22:22:07 2021 +0300
# Node ID 77e4d12c6671e6f52c7f5c7596a2d7f5ce377b92
# Parent  de7d55650ad741377c8f073638cbc1fd440f91d6
Don't use function dim_vector::to_jit (bug #61030)

copy of https://hg.octave.org/mxe-octave/file/93d20ff5d6a6/src/of-image-1-no-jit.patch by Markus Mützel, pushed by Avinoam

Affected files:
src/connectivity.h
src/imreconstruct.cc
src/bwconncomp.cc

diff -r de7d55650ad7 -r 77e4d12c6671 src/bwconncomp.cc
--- a/src/bwconncomp.cc	Wed Jul 28 09:47:07 2021 +0200
+++ b/src/bwconncomp.cc	Sat Aug 14 22:22:07 2021 +0300
@@ -81,13 +81,11 @@
 
   const dim_vector original_size = BW.dims ();
   const dim_vector padded_size = BW_pad.dims ();
-  const octave_idx_type* o = original_size.to_jit ();
-  const octave_idx_type* p = padded_size.to_jit ();
   const octave_idx_type ndims_m1 = BW_pad.ndims () -1;
 
   std::vector<bool> dim_padded (BW_pad.ndims (), true);
   for (octave_idx_type i = 0; i < BW_pad.ndims (); i++)
-    if (p[i] == o[i])
+    if (padded_size.OCT_DIM_VECTOR_XELEM (i) == original_size.OCT_DIM_VECTOR_XELEM (i))
       dim_padded[i] = false;
 
   for (std::vector<octave_idx_type>& conn_comp : all_components)
@@ -100,21 +98,21 @@
             {
               if (dim_padded[d])
                 {
-                  ind  += mult * (offset % p[d] - 1);
-                  mult *= p[d] - 2;
-                  offset /= p[d];
+                  ind  += mult * (offset % padded_size.OCT_DIM_VECTOR_XELEM (d) - 1);
+                  mult *= padded_size.OCT_DIM_VECTOR_XELEM (d) - 2;
+                  offset /= padded_size.OCT_DIM_VECTOR_XELEM (d);
                 }
               else
                 {
-                  ind  += mult * (offset % p[d]);
-                  mult *= p[d];
-                  offset /= p[d];
+                  ind  += mult * (offset % padded_size.OCT_DIM_VECTOR_XELEM (d));
+                  mult *= padded_size.OCT_DIM_VECTOR_XELEM (d);
+                  offset /= padded_size.OCT_DIM_VECTOR_XELEM (d);
                 }
             }
           if (dim_padded[ndims_m1])
-            ind  += mult * (offset % p[ndims_m1] - 1);
+            ind  += mult * (offset % padded_size.OCT_DIM_VECTOR_XELEM (ndims_m1) - 1);
           else
-            ind  += mult * (offset % p[ndims_m1]);
+            ind  += mult * (offset % padded_size.OCT_DIM_VECTOR_XELEM (ndims_m1));
 
           offset = ind;
         }
diff -r de7d55650ad7 -r 77e4d12c6671 src/configure.ac
--- a/src/configure.ac	Wed Jul 28 09:47:07 2021 +0200
+++ b/src/configure.ac	Sat Aug 14 22:22:07 2021 +0300
@@ -161,6 +161,22 @@
   ])
 fi
 
+## Check if "dim_vector" class has "xelem" member function.
+AC_CACHE_CHECK([whether dim_vector has xelem method],
+   [octave_image_cv_dim_vector_xelem],
+   [AC_COMPILE_IFELSE(
+     [AC_LANG_PROGRAM([#include <octave/dim-vector.h>],
+                      [dim_vector foo = dim_vector (1, 1);
+                       octave_idx_type bar = foo.xelem (0);])],
+     [octave_image_cv_dim_vector_xelem=yes],
+     [octave_image_cv_dim_vector_xelem=no])]
+)
+
+if test "$octave_image_cv_dim_vector_xelem" = yes; then
+  AC_DEFINE([HAVE_OCTAVE_DIM_VECTOR_XELEM], [1],
+            [Define if dim_vector has member xelem.])
+fi
+
 ## Some functions have been moving between Octave core and the Image
 ## package so make sure we are not shadowing anything on this
 ## installation.
diff -r de7d55650ad7 -r 77e4d12c6671 src/connectivity.h
--- a/src/connectivity.h	Wed Jul 28 09:47:07 2021 +0200
+++ b/src/connectivity.h	Sat Aug 14 22:22:07 2021 +0300
@@ -29,6 +29,12 @@
 
 #include <octave/ov.h>
 
+#if defined (HAVE_OCTAVE_DIM_VECTOR_XELEM)
+#  define OCT_DIM_VECTOR_XELEM xelem
+#else
+#  define OCT_DIM_VECTOR_XELEM elem
+#endif
+
 namespace octave
 {
   namespace image
@@ -153,8 +159,7 @@
   const Array<octave_idx_type> lengths = padding_lengths (size, padded_size);
   const octave_idx_type* lengths_v = lengths.fortran_vec ();
 
-  const octave_idx_type* strides_v = size.to_jit ();
-  const octave_idx_type row_stride = strides_v[0];
+  const octave_idx_type row_stride = size.OCT_DIM_VECTOR_XELEM (0);
 
   std::function<void(const octave_idx_type)> fill;
   fill = [&] (const octave_idx_type dim) -> void
@@ -165,7 +170,7 @@
     if (dim == 0)
       im_v += row_stride;
     else
-      for (octave_idx_type i = 0; i < strides_v[dim]; i++)
+      for (octave_idx_type i = 0; i < size.OCT_DIM_VECTOR_XELEM (dim); i++)
         fill (dim -1);
 
     for (octave_idx_type i = 0; i < lengths_v[dim]; i++, im_v++)
diff -r de7d55650ad7 -r 77e4d12c6671 src/imreconstruct.cc
--- a/src/imreconstruct.cc	Wed Jul 28 09:47:07 2021 +0200
+++ b/src/imreconstruct.cc	Sat Aug 14 22:22:07 2021 +0300
@@ -110,10 +110,6 @@
 
   const octave_idx_type n_neighbours = raster_neighbours.numel ();
 
-  // We probably should not be using this but converting to Array
-  // just to have fortran_vec seems a bit too much.
-  const octave_idx_type* s = original_size.to_jit ();
-
   std::function<void(const octave_idx_type)> scan;
   scan = [&] (const octave_idx_type dim) -> void
   {
@@ -122,7 +118,7 @@
 
     if (dim == 0)
       {
-        for (octave_idx_type k = 0; k < s[0]; k++, J++, I++)
+        for (octave_idx_type k = 0; k < original_size.OCT_DIM_VECTOR_XELEM (0); k++, J++, I++)
           {
             for (octave_idx_type i = 0; i < n_neighbours; i++)
               if (*J < J[neighbours[i]])
@@ -133,7 +129,7 @@
           }
       }
     else
-      for (octave_idx_type i = 0; i < s[dim]; i++)
+      for (octave_idx_type i = 0; i < original_size.OCT_DIM_VECTOR_XELEM (dim); i++)
         scan (dim-1);
 
     J += pads[dim];
@@ -161,10 +157,6 @@
 
   const octave_idx_type n_neighbours = antiraster_neighbours.numel ();
 
-  // We probably should not be using this but converting to Array
-  // just to have fortran_vec seems a bit too much.
-  const octave_idx_type* s = original_size.to_jit ();
-
   J += padded_marker.numel () -1;
   I += padded_marker.numel () -1;
 
@@ -178,7 +170,7 @@
 
     if (dim == 0)
       {
-        for (octave_idx_type k = 0; k < s[0]; k++, J--, I--, ind--)
+        for (octave_idx_type k = 0; k < original_size.OCT_DIM_VECTOR_XELEM (0); k++, J--, I--, ind--)
           {
             for (octave_idx_type i = 0; i < n_neighbours; i++)
               if (*J < J[neighbours[i]])
@@ -193,7 +185,7 @@
           }
       }
     else
-      for (octave_idx_type i = 0; i < s[dim]; i++)
+      for (octave_idx_type i = 0; i < original_size.OCT_DIM_VECTOR_XELEM (dim); i++)
         scan (dim-1);
 
     J   -= pads[dim];
