diff libinterp/corefcn/__tiff__.cc @ 31170:72a159bc5a4c

Tiff: added readRGBAImage method to read image using the RGBA interface * __tiff__.cc(F__tiff_reag_rgba_image__): implemented logic for reading images using LibTIFF's RGBA interface. * Tiff.m: added method readRGBAImage and added unit tests for the new method.
author magedrifaat <magedrifaat@gmail.com>
date Sat, 13 Aug 2022 17:36:12 +0200
parents 27ed758c1688
children 8bf3fa6b6977
line wrap: on
line diff
--- a/libinterp/corefcn/__tiff__.cc	Sat Aug 13 02:45:51 2022 +0200
+++ b/libinterp/corefcn/__tiff__.cc	Sat Aug 13 17:36:12 2022 +0200
@@ -2272,7 +2272,7 @@
     int nargin = args.length ();
 
     if (nargin != 2)
-      error ("rong number of arguments");
+      error ("Wrong number of arguments");
     
     TIFF *tif = (TIFF *)(args(0).uint64_value ());
 
@@ -2307,7 +2307,7 @@
     int nargin = args.length ();
 
     if (nargin != 2)
-      error ("rong number of arguments");
+      error ("Wrong number of arguments");
     
     TIFF *tif = (TIFF *)(args(0).uint64_value ());
 
@@ -2335,6 +2335,50 @@
 #endif
   }
 
+  DEFUN (__tiff_read_rgba_image__, args, ,
+             "Read the image data in rgba mode")
+  {
+#if defined (HAVE_TIFF)
+    int nargin = args.length ();
+
+    if (nargin != 1)
+      error ("Wrong number of arguments");
+    
+    TIFF *tif = (TIFF *)(args(0).uint64_value ());
+
+    tiff_image_data image_data (tif);
+
+    dim_vector img_dims (4, image_data.width, image_data.height);
+    uint8NDArray img (img_dims);
+    uint32_t *img_ptr = reinterpret_cast <uint32_t *> (img.fortran_vec ());
+    // Matlab (R2022a) uses a top-left orientation ignoring the tag if present
+    if (! TIFFReadRGBAImageOriented (tif, image_data.width, image_data.height,
+                                     img_ptr, 1))
+      error ("Failed to read image");
+    
+    Array<octave_idx_type> perm (dim_vector (3, 1));
+    perm(0) = 2;
+    perm(1) = 1;
+    perm(2) = 0;
+    img = img.permute (perm);
+
+    Array<idx_vector> idx (dim_vector (3, 1));
+    idx(0) = idx_vector (':');
+    idx(1) = idx_vector (':');
+    idx(2) = idx_vector (0, 3);
+    uint8NDArray rgb = uint8NDArray (img.index (idx));
+    idx(2) = idx_vector (3);
+    uint8NDArray alpha = uint8NDArray (img.index (idx));
+    
+    octave_value_list retval (2);
+    retval(0) = octave_value (rgb);
+    retval(1) = octave_value (alpha);
+    return retval;
+#else
+    err_disabled_feature ("readEncodedTile", "Tiff");
+#endif
+  }
+
   DEFUN (__tiff_write__, args, ,
              "Write the image data to the current IFD")
   {