changeset 31210:e83a62df599b

__magick_read__: appended alpha to the rest of the channels for Tiff images * libinterp/corefcn/__magick_read__.cc (read_images, read_indexed_images): added logic to append the alpha channel to the rest of the channels and return an empty alpha for TIFF images for matlab compatibility.
author magedrifaat <magedrifaat@gmail.com>
date Tue, 06 Sep 2022 20:57:50 +0200
parents bbb41a5f377a
children edbd725ee76c
files libinterp/corefcn/__magick_read__.cc
diffstat 1 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/__magick_read__.cc	Sun Sep 04 15:57:47 2022 +0200
+++ b/libinterp/corefcn/__magick_read__.cc	Tue Sep 06 20:57:50 2022 +0200
@@ -263,6 +263,21 @@
   octave_idx_type m_col_out;
 };
 
+template <typename T>
+static octave_value
+append_alpha (octave_value channels, octave_value alpha)
+{
+  // ASSUMES the dimensions of channels and alpha are already validated
+  typedef typename T::element_type P;
+
+  OCTAVE_LOCAL_BUFFER (Array<P>, array_list, 2);
+  array_list[0] = octave_value_extract<T> (channels);
+  array_list[1] = octave_value_extract<T> (alpha);
+  
+  // Concatenate the two arrays on the 3rd dimensions
+  return octave_value (Array<P>::cat (2, 2, array_list));
+}
+
 static octave_value_list
 read_maps (Magick::Image& img)
 {
@@ -374,6 +389,15 @@
         }
     }
 
+  // For TIFF images, matlab doesn't return a separate alpha channel but
+  // appends tha alpha channel to the rest of the channels in the first
+  // return value, and returns an empty alpha channel
+  if (imvec[0].magick () == "TIFF" && ! retval(2).isempty ())
+  {
+    retval (0) = append_alpha<T> (retval (0), retval (2));
+    retval (2) = Matrix ();
+  }
+
   return retval;
 }
 
@@ -757,6 +781,14 @@
 
   retval(0) = img;
 
+  // For TIFF images, matlab doesn't return a separate alpha channel but
+  // appends tha alpha channel to the rest of the channels in the first
+  // return value, and returns an empty alpha channel
+  if (imvec[0].magick () == "TIFF" && ! retval(2).isempty ())
+  {
+    retval (0) = append_alpha<T> (retval (0), retval (2));
+    retval (2) = Matrix ();
+  }
   return retval;
 }