diff scripts/io/Tiff.m @ 31171:8bf3fa6b6977

Tiff: added readRGBAStrip and readRGBATile methods * __tiff__.cc: added internal functions for reading strips and tiles using LibTIFF's RGBA interface. * Tiff.m: added readRGBAStrip and readRGBATile methods to the Tiff class and added unit tests for the ne methods.
author magedrifaat <magedrifaat@gmail.com>
date Sun, 14 Aug 2022 02:40:03 +0200
parents 72a159bc5a4c
children 3f5f1404af8a
line wrap: on
line diff
--- a/scripts/io/Tiff.m	Sat Aug 13 17:36:12 2022 +0200
+++ b/scripts/io/Tiff.m	Sun Aug 14 02:40:03 2022 +0200
@@ -260,6 +260,20 @@
       [RGB, alpha] = __tiff_read_rgba_image__ (t.tiff_handle);
     endfunction
 
+    function [RGB, alpha] = readRGBAStrip (t, row)
+      if (t.closed)
+        error ("Image file was closed");
+      endif
+      [RGB, alpha] = __tiff_read_rgba_strip__ (t.tiff_handle, row);
+    endfunction
+
+    function [RGB, alpha] = readRGBATile (t, row, col)
+      if (t.closed)
+        error ("Image file was closed");
+      endif
+      [RGB, alpha] = __tiff_read_rgba_tile__ (t.tiff_handle, row, col);
+    endfunction
+
     function write (t, imageData)
       if (t.closed)
         error ("Image file was closed");
@@ -1034,7 +1048,7 @@
 %!    writeEncodedStrip (img, 1, uint8 (reshape (1:20, [2, 10])));
 %!    assert (computeStrip (img, 1, 2), 6);
 %!    assert (computeStrip (img, 100, 1), 5);
-%!    img = Tiff ("test.tif", "w");
+%!    img = Tiff (filename, "w");
 %!    setTag (img, "TileWidth", 16);
 %!    setTag (img, "TileLength", 16);
 %!    fail ("computeStrip (img, 1, 1)", "The image is tiled not stripped");
@@ -1062,7 +1076,7 @@
 %!    writeEncodedTile (img, 1, uint8 (reshape (1:256, [16, 16])));
 %!    assert (computeTile (img, [1, 1], 2), 5);
 %!    assert (computeTile (img, [100, 100], 1), 4);
-%!    img = Tiff ("test.tif", "w");
+%!    img = Tiff (filename, "w");
 %!    fail ("computeTile (img, 1, 1)", "The image is stripped not tiled");
 %!  endfunction
 %!  file_wrapper (@test_fn);
@@ -1494,3 +1508,119 @@
 %!    assert (alpha, data(:,:,4));
 %!  endfunction
 %!  file_wrapper (@test_fn);
+
+## test readRGBAStrip
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag(img, struct (
+%!      "ImageLength", 10, "ImageWidth", 10,
+%!      "BitsPerSample", 8, "SamplesPerPixel", 3,
+%!      "PhotometricInterpretation", 2,
+%!      "PlanarConfiguration", 1,
+%!      "RowsPerStrip", 3
+%!    ));
+%!    data = uint8 (randi ([0,255], [10, 10, 3]));
+%!    write (img, data);
+%!    [rgb, alpha] = readRGBAStrip (img, 1);
+%!    assert (rgb, data(1:3,:,:));
+%!    assert (alpha, uint8 (repmat ([255], [3, 10])));
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test readRGBAStrip boundary strip
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag(img, struct (
+%!      "ImageLength", 10, "ImageWidth", 10,
+%!      "BitsPerSample", 8, "SamplesPerPixel", 3,
+%!      "PhotometricInterpretation", 2,
+%!      "PlanarConfiguration", 1,
+%!      "RowsPerStrip", 3
+%!    ));
+%!    data = uint8 (randi ([0,255], [10, 10, 3]));
+%!    write (img, data);
+%!    [rgb, alpha] = readRGBAStrip (img, 10);
+%!    assert (rgb, data(10,:,:));
+%!    assert (alpha, uint8 (repmat ([255], [1, 10])));
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test readRGBAStrip with alpha
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag(img, struct (
+%!      "ImageLength", 10, "ImageWidth", 10,
+%!      "BitsPerSample", 8, "SamplesPerPixel", 4,
+%!      "PhotometricInterpretation", 2,
+%!      "PlanarConfiguration", 1,
+%!      "RowsPerStrip", 3,
+%!      "ExtraSamples", 1
+%!    ));
+%!    data = uint8 (randi ([0,255], [10, 10, 4]));
+%!    write (img, data);
+%!    [rgb, alpha] = readRGBAStrip (img, 1);
+%!    assert (rgb, data(1:3,:, 1:3));
+%!    assert (alpha, data (1:3, :, 4));
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test readRGBATile
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag(img, struct (
+%!      "ImageLength", 40, "ImageWidth", 40,
+%!      "BitsPerSample", 8, "SamplesPerPixel", 3,
+%!      "PhotometricInterpretation", 2,
+%!      "PlanarConfiguration", 1,
+%!      "TileLength", 16, "TileWidth", 32
+%!    ));
+%!    data = uint8 (randi ([0,255], [40, 40, 3]));
+%!    write (img, data);
+%!    [rgb, alpha] = readRGBATile (img, 1, 1);
+%!    assert (rgb, data(1:16,1:32,:));
+%!    assert (alpha, uint8 (repmat ([255], [16, 32])));
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test readRGBATile boundary tile
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag(img, struct (
+%!      "ImageLength", 40, "ImageWidth", 40,
+%!      "BitsPerSample", 8, "SamplesPerPixel", 3,
+%!      "PhotometricInterpretation", 2,
+%!      "PlanarConfiguration", 1,
+%!      "TileLength", 16, "TileWidth", 32
+%!    ));
+%!    data = uint8 (randi ([0,255], [40, 40, 3]));
+%!    write (img, data);
+%!    [rgb, alpha] = readRGBATile (img, 40, 40);
+%!    assert (rgb, data(33:end,33:end,:));
+%!    assert (alpha, uint8 (repmat ([255], [8, 8])));
+%!  endfunction
+%!  file_wrapper (@test_fn);
+
+## test readRGBATile ith alpha
+%!testif HAVE_TIFF
+%!  function test_fn (filename)
+%!    img = Tiff (filename, "w");
+%!    setTag(img, struct (
+%!      "ImageLength", 40, "ImageWidth", 40,
+%!      "BitsPerSample", 8, "SamplesPerPixel", 4,
+%!      "PhotometricInterpretation", 2,
+%!      "PlanarConfiguration", 1,
+%!      "TileLength", 16, "TileWidth", 32,
+%!      "ExtraSamples", 1
+%!    ));
+%!    data = uint8 (randi ([0,255], [40, 40, 4]));
+%!    write (img, data);
+%!    [rgb, alpha] = readRGBATile (img, 1, 1);
+%!    assert (rgb, data(1:16,1:32,1:3));
+%!    assert (alpha, data(1:16,1:32,4));
+%!  endfunction
+%!  file_wrapper (@test_fn);