changeset 31151:6fb54834aa93

Tiff: added numberOfTiles and getVersion methods * Tiff.m: added numberOfTiles and getVersion methods and added a test for numberOfTiles. * __tiff__.cc: added internal functions implementing numberOfTiles and getVersion.
author magedrifaat <magedrifaat@gmail.com>
date Mon, 01 Aug 2022 00:56:19 +0200
parents 6bede2d6f273
children 2244617f4da5
files libinterp/dldfcn/__tiff__.cc scripts/io/Tiff.m
diffstat 2 files changed, 61 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/dldfcn/__tiff__.cc	Mon Aug 01 00:34:34 2022 +0200
+++ b/libinterp/dldfcn/__tiff__.cc	Mon Aug 01 00:56:19 2022 +0200
@@ -1552,6 +1552,27 @@
 #endif
   }
 
+  DEFUN_DLD (__tiff_number_of_tiles__, args, ,
+             "Get the number of tiles in the image")
+  {
+#if defined (HAVE_TIFF)
+    int nargin = args.length ();
+
+    if (nargin == 0)
+      error ("No handle provided\n");
+    
+    TIFF *tif = (TIFF *)(args(0).uint64_value ());
+    // What is the behavior in matlab?
+    if (! TIFFIsTiled (tif))
+      error ("The image is stripped not tiled");
+    
+    double tile_count = static_cast<double> (TIFFNumberOfTiles (tif));
+    return octave_value_list (octave_value (tile_count));
+#else
+    err_disabled_feature ("numberOfTiles", "Tiff");
+#endif
+  }
+
   DEFUN_DLD (__tiff_compute_strip__, args, ,
              "Get the strip index containing the given row")
   {
@@ -1659,4 +1680,15 @@
 #endif
   }
 
+  DEFUN_DLD (__tiff_version__, , ,
+             "Get the version stamp of LibTIFF")
+  {
+#if defined (HAVE_TIFF)
+    std::string version = std::string (TIFFGetVersion ());
+    return octave_value_list (octave_value (version));
+#else
+    err_disabled_feature ("getVersion", "Tiff");
+#endif
+  }
+
 }
--- a/scripts/io/Tiff.m	Mon Aug 01 00:34:34 2022 +0200
+++ b/scripts/io/Tiff.m	Mon Aug 01 00:56:19 2022 +0200
@@ -144,6 +144,13 @@
       numStrips = __tiff_number_of_strips__ (t.tiff_handle);
     endfunction
 
+    function numTiles = numberOfTiles (t)
+      if (t.closed)
+        error ("Image file was closed");
+      endif
+      numTiles = __tiff_number_of_tiles__ (t.tiff_handle);
+    endfunction
+
     function stripNumber = computeStrip (t, varargin)
       if (t.closed)
         error ("Image file was closed");
@@ -160,6 +167,12 @@
 
     % TODO(maged): add documentation and make print_usage work
   endmethods
+
+  methods (Static = true)
+    function versionString = getVersion()
+      versionString = __tiff_version__ ();
+    endfunction
+  endmethods
 endclassdef
 
 %!function file_wrapper (fn)
@@ -811,6 +824,22 @@
 %!  endfunction
 %!  file_wrapper (@test_fn);
 
+## test numberOfTiles returns the correct value
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, struct ("ImageWidth", 20, "ImageLength", 20,
+%!                         "TileLength", 16, "TileWidth", 16));
+%!    assert (numberOfTiles (img), 4);
+%!    setTag (img, "TileLength", 32);
+%!    assert (numberOfTiles (img), 2);
+%!    setTag (img, "TileWidth", 32);
+%!    assert (numberOfTiles (img), 1);
+%!    img = Tiff (filename, "w");
+%!    fail ("numberOfTiles (img)", "The image is stripped not tiled");
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
 ## test computeStrip returns the correct value
 %!testif HAVE_TIFF
 %!  function test_fn (filename)
@@ -861,4 +890,3 @@
 %!    fail ("computeTile (img, 1, 1)", "The image is stripped not tiled");
 %!  endfunction
 %!  file_wrapper (@test_fn);
-