changeset 31143:a68f2dadafee

Tiff.m: added all remaining tests for implemented features.
author magedrifaat <magedrifaat@gmail.com>
date Sun, 31 Jul 2022 00:13:04 +0200
parents 97e7ee3b27b7
children 8ba9f2326ee9
files libinterp/dldfcn/__tiff__.cc scripts/io/Tiff.m
diffstat 2 files changed, 206 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/dldfcn/__tiff__.cc	Fri Jul 29 01:16:02 2022 +0200
+++ b/libinterp/dldfcn/__tiff__.cc	Sun Jul 31 00:13:04 2022 +0200
@@ -725,8 +725,10 @@
           if (! TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE,
                                        &bits_per_sample))
             error ("Failed to obtain the bit depth");
-                
-          if (bits_per_sample > 24)
+          
+          // According to the format specification, this field should
+          // be 8 or 16 only.
+          if (bits_per_sample > 16)
             error ("Too high bit depth for a palette image");
 
           uint32_t count = 1 << bits_per_sample;
@@ -1207,7 +1209,7 @@
     tiff_image_data image_data (tif);
 
     if (image_data.is_tiled)
-      error ("Can't rite strips to a tiled image");
+      error ("Can't write strips to a tiled image");
 
     uint32_t strip_no = args (1).uint32_scalar_value ();
     if (strip_no < 1 || strip_no > TIFFNumberOfStrips (tif))
--- a/scripts/io/Tiff.m	Fri Jul 29 01:16:02 2022 +0200
+++ b/scripts/io/Tiff.m	Sun Jul 31 00:13:04 2022 +0200
@@ -140,10 +140,61 @@
 %!    img = Tiff (filename, "r");
 %!    data2 = img.read ();
 %!    assert (size (data2), ex_size);
-%!    assert (data2, resize (data, size (data2)));
+%!    assert (data2, resize (data, size (data2)), -1e5);
 %!    img.close ();
 %!endfunction
 
+## test setTag and getTag for scalar tags
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, "ImageWidth", 2);
+%!    val = getTag (img, "ImageWidth");
+%!    assert (val, 2);
+%!    assert (class (val), "double");
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test failure setTag and getTag unknown tag
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    fail ("getTag (img, \"ImageWidt\")", "Tiff tag not found");
+%!    fail ("getTag (img, 999999)", "Tiff tag not found");
+%!    fail ("setTag (img, \"ImageWidt\", 2)", "Tiff tag not found");
+%!    fail ("setTag (img, 999999, 2)", "Tiff tag not found");
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test setTag structure input
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, struct ("ImageLength", 2, "ImageWidth", 2));
+%!    assert (getTag (img, "ImageLength"), 2);
+%!    assert (getTag (img, "ImageWidth"), 2);
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test failure setTag structure unknown tag
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, "ImageLength", 1);
+%!    setTag (img, "ImageWidth", 1);
+%!    fail ("setTag (img, struct (\"ImageLength\", 2, \"a\", 1, \"ImageWidth\", 2))",
+%!          "Tag a not found");
+%!    assert (getTag (img, "ImageLength"), 2);
+%!    assert (getTag (img, "ImageWidth"), 1);
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test failure invalid open mode/ invalid filename
+%!testif HAVE_TIFF
+%!  fail ("Tiff (\"test.tif\", \"rh\")",
+%!        "Invalid mode for openning Tiff file: rh");
+%!  fail ("Tiff ([tempname() \".tif\"], \"r\")", "Failed to open Tiff file");
+
 ## test one-pixel grayscale image
 %!testif HAVE_TIFF
 %!  function test_fn (filename)
@@ -322,6 +373,114 @@
 %!  endfunction
 %!  file_wrapper (@test_fn);
 
+## test failure unsupported bit-depth
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, struct ("ImageLength", 20, "ImageWidth", 20,
+%!                         "BitsPerSample", 24, "SamplesPerPixel", 1));
+%!    data = uint16 (reshape (1:400, [20, 20]));
+%!    fail ("writeEncodedStrip (img, 1, data)", "Unsupported bit depth");
+%!    img.close ();
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test multi-strip grayscale image
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, struct ("ImageLength", 20, "ImageWidth", 20,
+%!                         "BitsPerSample", 16, "SamplesPerPixel", 1,
+%!                         "RowsPerStrip", 2));
+%!    data = uint16 (reshape (1:400, [20, 20]));
+%!    for row = 1:2:20
+%!      writeEncodedStrip (img, row / 2, data(row: row + 1, :));
+%!    endfor
+%!    img.close ();
+%!    verify_data (filename, data, [20, 20]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test multi-strip RGB image
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, struct ("ImageLength", 20, "ImageWidth", 20,
+%!                         "BitsPerSample", 16, "SamplesPerPixel", 3,
+%!                         "RowsPerStrip", 2, "PlanarConfiguration", 1,
+%!                         "PhotometricInterpretation", 2));
+%!    data = uint16 (reshape (1:1200, [20, 20, 3]));
+%!    for row = 1:2:20
+%!      writeEncodedStrip (img, row / 2, data(row: row + 1, :, :));
+%!    endfor
+%!    img.close ();
+%!    verify_data (filename, data, [20, 20, 3]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test multi-strip RGB separate planes image
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, struct ("ImageLength", 20, "ImageWidth", 20,
+%!                         "BitsPerSample", 16, "SamplesPerPixel", 3,
+%!                         "RowsPerStrip", 2, "PlanarConfiguration", 2,
+%!                         "PhotometricInterpretation", 2));
+%!    data = uint16 (reshape (1:1200, [20, 20, 3]));
+%!    strip = 1;
+%!    for sample = 1:3
+%!      for row = 1:2:20
+%!        writeEncodedStrip (img, strip, data(row: row + 1, :, sample));
+%!        strip = strip + 1;
+%!      endfor
+%!    endfor
+%!    img.close ();
+%!    verify_data (filename, data, [20, 20, 3]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test single-precision image
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, struct ("ImageLength", 20, "ImageWidth", 20,
+%!                         "BitsPerSample", 32, "SamplesPerPixel", 1,
+%!                         "SampleFormat", 3));
+%!    data = single (reshape (1:400, [20, 20]));
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close ();
+%!    verify_data (filename, data, [20, 20]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test double-precision image
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, struct ("ImageLength", 20, "ImageWidth", 20,
+%!                         "BitsPerSample", 64, "SamplesPerPixel", 1,
+%!                         "SampleFormat", 3));
+%!    data = double (reshape (1:400, [20, 20]));
+%!    writeEncodedStrip (img, 1, data);
+%!    img.close ();
+%!    verify_data (filename, data, [20, 20]);
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test failure unsupported floating-point bit depth
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, struct ("ImageLength", 20, "ImageWidth", 20,
+%!                         "BitsPerSample", 16, "SamplesPerPixel", 1,
+%!                         "SampleFormat", 3));
+%!    data = double (reshape (1:400, [20, 20]));
+%!    fail ("writeEncodedStrip (img, 1, data)",
+%!          "Floating point images are only supported for bit depths of 32 and 64");
+%!    img.close ();
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
 ## test failure data-type and bit-depth mismatch
 %!testif HAVE_TIFF
 %!  function test_fn (filename)
@@ -349,4 +508,45 @@
 %!    fail ("writeEncodedStrip(img, 1, [1])", "Can't write data to a file opened in read-only mode");
 %!    img.close();
 %!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test failure unsupported sample format
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, struct ("ImageLength", 20, "ImageWidth", 20,
+%!                         "BitsPerSample", 16, "SamplesPerPixel", 1,
+%!                         "SampleFormat", 5));
+%!    data = double (reshape (1:400, [20, 20]));
+%!    fail ("writeEncodedStrip (img, 1, data)", "Unsupported sample format");
+%!    img.close ();
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test failure wrong strip number
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, struct ("ImageLength", 20, "ImageWidth", 20,
+%!                         "BitsPerSample", 16, "SamplesPerPixel", 1,
+%!                         "RowsPerStrip", 5));
+%!    strip_data = uint16 (reshape (1:100, [5, 20]));
+%!    fail ("writeEncodedStrip (img, 5, strip_data)",
+%!          "Strip number out of range");
+%!    img.close ();
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test failure writing strips to tiled image
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag (img, struct ("ImageLength", 20, "ImageWidth", 20,
+%!                         "BitsPerSample", 16, "SamplesPerPixel", 1,
+%!                         "TileLength", 16, "TileWidth", 16));
+%!    data = uint8 (reshape (1:400, [20, 20]));
+%!    fail ("writeEncodedStrip (img, 1, data)",
+%!          "Can't write strips to a tiled image");
+%!    img.close ();
+%!  endfunction
 %!  file_wrapper (@test_fn);
\ No newline at end of file