changeset 31137:233130c0b1f6

Tiff writeEncodedStrip: changed strip dimension check behavior to mimic matlab * __tiff__.cc(write_strip): changed the way ring strip dimensions are handled by using resize instead of aborting to mimic matlab behavior. * Tiff.m: rewrote the tests to match the neww behavior.
author magedrifaat <magedrifaat@gmail.com>
date Wed, 27 Jul 2022 00:37:50 +0200
parents be1a983981b2
children 68762676dab1
files libinterp/dldfcn/__tiff__.cc scripts/io/Tiff.m
diffstat 2 files changed, 29 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/dldfcn/__tiff__.cc	Wed Jul 27 00:18:57 2022 +0200
+++ b/libinterp/dldfcn/__tiff__.cc	Wed Jul 27 00:37:50 2022 +0200
@@ -892,7 +892,7 @@
       rows_in_strip = image_data->height;
     
     uint32_t strip_count = TIFFNumberOfStrips (tif);
-    uint32_t expected_numel;
+    dim_vector strip_dimensions;
 
     // Calculate the expected number of elements in the strip data array
     // All strips have equal number of rows excpet strips at the bottom
@@ -903,8 +903,8 @@
         // of the image can have less number of rows
         if (strip_no == strip_count)
           rows_in_strip = image_data->height - rows_in_strip * (strip_no - 1);
-        expected_numel = rows_in_strip * image_data->width
-                        * image_data->samples_per_pixel;
+        strip_dimensions = dim_vector (rows_in_strip, image_data->width,
+                                       image_data->samples_per_pixel);
       }
     else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
       {
@@ -918,30 +918,14 @@
             rows_in_strip = image_data->height - rows_in_strip
                                                  * ((strip_no - 1)
                                                     % (strips_per_channel));
-        expected_numel = rows_in_strip * image_data->width;
+        strip_dimensions = dim_vector (rows_in_strip, image_data->width, 1);
       }
     else
       error ("Planar configuration not supported");
-
-    // TODO(maged): check matlab behavior
-    if (strip_data.numel () != expected_numel)
-      error ("Size of strip data is different from the expected size of the strip");
+    
+    strip_data.resize (strip_dimensions);
 
-    
-    // Put the strip in a consistent shape for the subsequent permutation
-    if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
-      {
-        strip_data
-          = strip_data.reshape (dim_vector (rows_in_strip, image_data->width,
-                                            image_data->samples_per_pixel));
-      }
-    else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
-      {
-        strip_data = strip_data.reshape (dim_vector (rows_in_strip, 
-                                                     image_data->width, 1));
-      }
-
-    //TODO(maged): add suppot for 1-bit and 4-bit images
+    //TODO(maged): add suppot for 1-bit images
 
     // Permute the dimesions of the strip to match the expected memory
     // arrangement of LibTIFF (channel x width x height)
@@ -955,7 +939,7 @@
     strip_no--;
     // Can't rely in LibTIFF's TIFFStripSize because boundary strips
     // can be smaller in size
-    tsize_t strip_size = expected_numel * image_data->bits_per_sample / 8;
+    tsize_t strip_size = strip_data.numel () * image_data->bits_per_sample / 8;
     if (TIFFWriteEncodedStrip (tif, strip_no,
                                strip_data.fortran_vec (), strip_size) == -1)
       error ("Failed to write strip data to image");
--- a/scripts/io/Tiff.m	Wed Jul 27 00:18:57 2022 +0200
+++ b/scripts/io/Tiff.m	Wed Jul 27 00:37:50 2022 +0200
@@ -173,14 +173,19 @@
 %!    unlink (filename);
 %!  end_unwind_protect
 
-## test failure of wrong size of row
+## 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));
-%!    fail ("writeEncodedStrip (a, 1, data)", "Size of strip data is different from the expected size of the strip");
+%!    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);
@@ -204,14 +209,19 @@
 %!    unlink (filename);
 %!  end_unwind_protect
 
-## test failure wrong height of strip
+## 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));
-%!    fail ("writeEncodedStrip (a, 1, data)", "Size of strip data is different from the expected size of the strip");
+%!    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);
@@ -237,7 +247,7 @@
 %!    unlink (filename);
 %!  end_unwind_protect
 
-## test failure wrong number of channels
+## test wrong number of channels
 %!testif HAVE_TIFF
 %!  filename = [tempname() ".tif"];
 %!  unwind_protect
@@ -246,7 +256,12 @@
 %!    setTag(a, "PlanarConfiguration", 1);
 %!    setTag(a, "PhotometricInterpretation", 2);
 %!    data = uint8 (randi (intmax ("uint8"), 10, 10, 4));
-%!    fail ("writeEncodedStrip (a, 1, data)", "Size of strip data is different from the expected size of the strip");
+%!    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);
@@ -274,21 +289,6 @@
 %!    unlink (filename);
 %!  end_unwind_protect
 
-## test failure of strip size in separate planes image
-%!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));
-%!    fail ("writeEncodedStrip (a, 1, data)", "Size of strip data is different from the expected size of the strip");
-%!    a.close ();
-%!  unwind_protect_cleanup
-%!    unlink (filename);
-%!  end_unwind_protect
-
 ## test 16-bit grayscale image
 %!testif HAVE_TIFF
 %!  filename = [tempname() ".tif"];