changeset 31139:3431a15b2c75

Tiff.m: refactored tests to use functions to reduce code repetition.
author magedrifaat <magedrifaat@gmail.com>
date Thu, 28 Jul 2022 20:50:41 +0200
parents 68762676dab1
children 5f70efad6e2c
files libinterp/dldfcn/__tiff__.cc scripts/io/Tiff.m
diffstat 2 files changed, 166 insertions(+), 223 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/dldfcn/__tiff__.cc	Wed Jul 27 00:54:48 2022 +0200
+++ b/libinterp/dldfcn/__tiff__.cc	Thu Jul 28 20:50:41 2022 +0200
@@ -6,6 +6,7 @@
 #include <iostream>
 
 // Workaround to not have to include fcntl which doesn't exist on windows
+// TODO(maged): see what octave does for this (fcntl)
 #ifndef O_RDONLY
 #define O_RDONLY 0
 #endif
@@ -874,6 +875,8 @@
   void
   set_field_data (TIFF *tif, const TIFFField *fip, octave_value tag_ov)
   {
+    // TODO(maged): prevent calling here for read-only images
+    
     // TODO(maged): complete the implementation of this function
     uint32_t tag_id = TIFFFieldTag (fip);
     uint32_t tag_data = tag_ov.double_value ();
@@ -928,6 +931,7 @@
     else
       error ("Planar configuration not supported");
     
+    // TODO(maged): give a warning for a larger inpput strip
     strip_data.resize (strip_dimensions);
 
     //TODO(maged): add suppot for 1-bit images
@@ -1006,6 +1010,7 @@
     if (nargin == 0)
       error ("No handle provided\n");
     
+    // TODO: mark object as closed to prevent segfault/ check matlab behavior
     TIFF *tif = (TIFF *)(args (0).uint64_value ());
     TIFFClose (tif);
 
@@ -1185,6 +1190,7 @@
     
     TIFF *tif = (TIFF *)(args (0).uint64_value ());
 
+    // TODO(maged): check on windows
     if (TIFFGetMode (tif) == O_RDONLY)
       error ("Can't write data to a file opened in read-only mode");
 
@@ -1237,8 +1243,6 @@
           error ("Only uint8 data is allowed for uint images with bit depth of 8");
         break;
       case 16:
-        // TODO(maged): what is the behavior if the input matrix has
-        // negative numbers?
         if (args(2).is_uint16_type ())
           write_strip<uint16NDArray> (tif, strip_no,
                                       args(2).uint16_array_value (),
--- a/scripts/io/Tiff.m	Wed Jul 27 00:54:48 2022 +0200
+++ b/scripts/io/Tiff.m	Thu Jul 28 20:50:41 2022 +0200
@@ -125,273 +125,212 @@
 %!  endif
 %!endfunction
 
-## test one-pixel grayscale image
-%!testif HAVE_TIFF
+%!function file_wrapper (fn)
 %!  filename = [tempname() ".tif"];
 %!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 1, 1, 1, 8);
-%!    data = uint8 (randi (intmax ("uint8"), 1, 1));
-%!    writeEncodedStrip (a, 1, data);
-%!    a.close ();
-%!    a = Tiff (filename, "r");
-%!    data2 = a.read ();
-%!    assert (size (data2), [1, 1]);
-%!    assert (data2, data);
-%!    a.close ();
+%!    fn (filename);
 %!  unwind_protect_cleanup
 %!    unlink (filename);
 %!  end_unwind_protect
+%!endfunction
+
+%!function verify_data (filename, data, ex_size)
+%!    img = Tiff (filename, "r");
+%!    data2 = img.read ();
+%!    assert (size (data2), ex_size);
+%!    assert (data2, resize (data, size (data2)));
+%!    img.close ();
+%!endfunction
+
+## test one-pixel grayscale image
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 1, 1, 1, 8);
+%!    data = uint8 (randi (intmax ("uint8"), 1, 1));
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close ();
+%!    verify_data (filename, data, [1, 1]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
 ## test failure to write to image without dimensions
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
 %!    data = uint8 (randi (intmax ("uint8"), 1, 1));
-%!    fail ("writeEncodedStrip (a, 1, data)", "Failed to read image width");
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
+%!    fail ("writeEncodedStrip (img, 1, data)", "Failed to read image width");
+%!    img.close ();
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
 ## test one row grayscale image
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 1, 10, 1, 8);
-%!    data = uint8 (randi (intmax ("uint8"), 1, 10));
-%!    writeEncodedStrip (a, 1, data);
-%!    a.close ();
-%!    a = Tiff (filename, "r");
-%!    data2 = a.read ();
-%!    assert (size (data2), [1, 10]);
-%!    assert (data2, data);
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 1, 10, 1, 8);
+%!    data = uint8 (reshape (1:10, [1, 10]));
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close ();
+%!    verify_data (filename, data, [1, 10]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
 ## test wrong size of row
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 1, 10, 1, 8);
-%!    data = uint8 (randi (intmax ("uint8"), 1, 9));
-%!    writeEncodedStrip (a, 1, data);
-%!    a.close ();
-%!    a = Tiff (filename, "r");
-%!    data2 = a.read ();
-%!    assert (size (data2), [1, 10]);
-%!    assert (data2, resize (data, size (data2)));
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 1, 10, 1, 8);
+%!    data = uint8 (reshape (1:9, [1, 9]));
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close ();
+%!    verify_data (filename, data, [1, 10]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
 ## test one strip grayscale image
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 10, 10, 1, 8);
-%!    data = uint8 (randi (intmax ("uint8"), 10, 10));
-%!    writeEncodedStrip (a, 1, data);
-%!    a.close ();
-%!    a = Tiff (filename, "r");
-%!    data2 = a.read ();
-%!    assert (size (data2), [10, 10]);
-%!    assert (data2, data);
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 10, 10, 1, 8);
+%!    data = uint8 (reshape (1:100, [10, 10]));
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close ();
+%!    verify_data (filename, data, [10, 10]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
 ## test wrong height of strip
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 10, 10, 1, 8);
-%!    data = uint8 (randi (intmax ("uint8"), 11, 10));
-%!    writeEncodedStrip (a, 1, data);
-%!    a.close ();
-%!    a = Tiff (filename, "r");
-%!    data2 = a.read ();
-%!    assert (size (data2), [10, 10]);
-%!    assert (data2, resize (data, size (data2)));
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 10, 10, 1, 8);
+%!    data = uint8 (reshape (1:110, [11, 10]));
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close ();
+%!    verify_data (filename, data, [10, 10]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
 ## test one strip RGB image chunky planes (RGBRGBRGB)
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 10, 10, 3, 8);
-%!    setTag(a, "PlanarConfiguration", 1);
-%!    setTag(a, "PhotometricInterpretation", 2);
-%!    data = uint8 (randi (intmax ("uint8"), 10, 10, 3));
-%!    writeEncodedStrip (a, 1, data);
-%!    a.close ();
-%!    a = Tiff (filename, "r");
-%!    data2 = a.read ();
-%!    assert (size (data2), [10, 10, 3]);
-%!    assert (data2, data);
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 10, 10, 3, 8);
+%!    setTag(img, "PlanarConfiguration", 1);
+%!    setTag(img, "PhotometricInterpretation", 2);
+%!    data = uint8 (reshape (1:300, [10, 10, 3]));
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close ();
+%!    verify_data (filename, data, [10, 10, 3]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
 ## test wrong number of channels
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 10, 10, 3, 8);
-%!    setTag(a, "PlanarConfiguration", 1);
-%!    setTag(a, "PhotometricInterpretation", 2);
-%!    data = uint8 (randi (intmax ("uint8"), 10, 10, 4));
-%!    writeEncodedStrip (a, 1, data);
-%!    a.close ();
-%!    a = Tiff (filename, "r");
-%!    data2 = a.read ();
-%!    assert (size (data2), [10, 10, 3]);
-%!    assert (data2, resize (data, size (data2)));
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 10, 10, 3, 8);
+%!    setTag(img, "PlanarConfiguration", 1);
+%!    setTag(img, "PhotometricInterpretation", 2);
+%!    data = uint8 (reshape (1:400, [10, 10, 4]));
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close ();
+%!    verify_data (filename, data, [10, 10, 3]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
 ## test one strip RGB image separate planes (RRRGGGBBB)
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 10, 10, 3, 8);
-%!    setTag(a, "PlanarConfiguration", 2);
-%!    setTag(a, "PhotometricInterpretation", 2);
-%!    data = uint8 (randi (intmax ("uint8"), 10, 10, 3));
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 10, 10, 3, 8);
+%!    setTag(img, "PlanarConfiguration", 2);
+%!    setTag(img, "PhotometricInterpretation", 2);
+%!    data = uint8 (reshape (1:300, [10, 10, 3]));
 %!    for i = 1:3
-%!      writeEncodedStrip (a, i, data(:,:,i));
+%!      writeEncodedStrip (img, i, data(:,:,i));
 %!    endfor
-%!    a.close ();
-%!    a = Tiff (filename, "r");
-%!    data2 = a.read ();
-%!    assert (size (data2), [10, 10, 3]);
-%!    assert (data2, data);
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
+%!    img.close ();
+%!    verify_data (filename, data, [10, 10, 3]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
 ## test 16-bit grayscale image
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 20, 20, 1, 16);
-%!    data = uint16 (randi (intmax ("uint16"), 20, 20));
-%!    writeEncodedStrip (a, 1, data);
-%!    a.close ();
-%!    a = Tiff (filename, "r");
-%!    data2 = a.read ();
-%!    assert (size (data2), [20, 20]);
-%!    assert (data2, data);
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 20, 20, 1, 16);
+%!    data = uint16 (reshape (1:400, [20, 20]));
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close ();
+%!    verify_data (filename, data, [20, 20]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
 ## test 32-bit grayscale image
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 20, 20, 1, 32);
-%!    data = uint32 (randi (intmax ("uint32"), 20, 20));
-%!    writeEncodedStrip (a, 1, data);
-%!    a.close ();
-%!    a = Tiff (filename, "r");
-%!    data2 = a.read ();
-%!    assert (size (data2), [20, 20]);
-%!    assert (data2, data);
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 20, 20, 1, 32);
+%!    data = uint32 (reshape (1:400, [20, 20]));
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close ();
+%!    verify_data (filename, data, [20, 20]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
 ## test 16-bit RGB image
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 20, 20, 3, 16);
-%!    setTag(a, "PlanarConfiguration", 1);
-%!    setTag(a, "PhotometricInterpretation", 2);
-%!    data = uint16 (randi (intmax ("uint16"), 20, 20, 3));
-%!    writeEncodedStrip (a, 1, data);
-%!    a.close ();
-%!    a = Tiff (filename, "r");
-%!    data2 = a.read ();
-%!    assert (size (data2), [20, 20, 3]);
-%!    assert (data2, data);
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 20, 20, 3, 16);
+%!    setTag(img, "PlanarConfiguration", 1);
+%!    setTag(img, "PhotometricInterpretation", 2);
+%!    data = uint16 (reshape (1:1200, [20, 20, 3]));
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close ();
+%!    verify_data (filename, data, [20, 20, 3]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
 ## test 32-bit RGB image
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 20, 20, 3, 32);
-%!    setTag(a, "PlanarConfiguration", 1);
-%!    setTag(a, "PhotometricInterpretation", 2);
-%!    data = uint32 (randi (intmax ("uint32"), 20, 20, 3));
-%!    writeEncodedStrip (a, 1, data);
-%!    a.close ();
-%!    a = Tiff (filename, "r");
-%!    data2 = a.read ();
-%!    assert (size (data2), [20, 20, 3]);
-%!    assert (data2, data);
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 20, 20, 3, 32);
+%!    setTag(img, "PlanarConfiguration", 1);
+%!    setTag(img, "PhotometricInterpretation", 2);
+%!    data = uint32 (reshape (1:1200, [20, 20, 3]));
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close ();
+%!    verify_data (filename, data, [20, 20, 3]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
 ## test failure data-type and bit-depth mismatch
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 20, 20, 3, 16);
-%!    setTag(a, "PlanarConfiguration", 1);
-%!    setTag(a, "PhotometricInterpretation", 2);
-%!    data = uint8 (randi (intmax ("uint8"), 20, 20, 3));
-%!    fail ("writeEncodedStrip (a, 1, data)", "Only uint16 data is allowed for uint images with bit depth of 16");
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 20, 20, 3, 16);
+%!    setTag(img, "PlanarConfiguration", 1);
+%!    setTag(img, "PhotometricInterpretation", 2);
+%!    data = uint8 (reshape (1:1200, [20, 20, 3]));
+%!    fail ("writeEncodedStrip (img, 1, data)", "Only uint16 data is allowed for uint images with bit depth of 16");
+%!    img.close ();
+%!  endfunction
+%!  file_wrapper (@test_fn);
 
-## test failure writing to a read-only file
+## test failure writing to img read-only file
 %!testif HAVE_TIFF
-%!  filename = [tempname() ".tif"];
-%!  unwind_protect
-%!    a = Tiff (filename, "w");
-%!    set_configs (a, 1, 1, 1, 8);
-%!    data = uint8 (randi (intmax ("uint8"), 1, 1));
-%!    writeEncodedStrip (a, 1, data);
-%!    a.close();
-%!    a = Tiff (filename, "r");
-%!    fail ("writeEncodedStrip(a, 1, [1])", "Can't write data to a file opened in read-only mode");
-%!    a.close();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
\ No newline at end of file
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    set_configs (img, 1, 1, 1, 8);
+%!    data = uint8 (1);
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close();
+%!    img = Tiff (filename, "r");
+%!    fail ("writeEncodedStrip(img, 1, [1])", "Can't write data to a file opened in read-only mode");
+%!    img.close();
+%!  endfunction
+%!  file_wrapper (@test_fn);
\ No newline at end of file