Mercurial > octave-libtiff
diff scripts/io/Tiff.m @ 31170:72a159bc5a4c
Tiff: added readRGBAImage method to read image using the RGBA interface
* __tiff__.cc(F__tiff_reag_rgba_image__): implemented logic for reading images
using LibTIFF's RGBA interface.
* Tiff.m: added method readRGBAImage and added unit tests for the new method.
author | magedrifaat <magedrifaat@gmail.com> |
---|---|
date | Sat, 13 Aug 2022 17:36:12 +0200 |
parents | ae41e14bf5c7 |
children | 8bf3fa6b6977 |
line wrap: on
line diff
--- a/scripts/io/Tiff.m Sat Aug 13 02:45:51 2022 +0200 +++ b/scripts/io/Tiff.m Sat Aug 13 17:36:12 2022 +0200 @@ -253,6 +253,13 @@ tileData = __tiff_read_encoded_tile__ (t.tiff_handle, tileNumber); endfunction + function [RGB, alpha] = readRGBAImage (t) + if (t.closed) + error ("Image file was closed"); + endif + [RGB, alpha] = __tiff_read_rgba_image__ (t.tiff_handle); + endfunction + function write (t, imageData) if (t.closed) error ("Image file was closed"); @@ -1450,3 +1457,40 @@ %! img.close(); %! endfunction %! file_wrapper (@test_fn); + +## test readRGBAImage +%!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 +%! )); +%! data = uint8 (reshape (1:300, [10, 10, 3])); +%! write (img, data); +%! [rgb, alpha] = readRGBAImage (img); +%! assert (rgb, data); +%! assert (alpha, uint8 (repmat ([255], [10, 10]))); +%! endfunction +%! file_wrapper (@test_fn); + +## test readRGBAImage 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, +%! "ExtraSamples", 1 +%! )); +%! data = uint8 (randi ([0,255], [10, 10, 4])); +%! write (img, data); +%! [rgb, alpha] = readRGBAImage (img); +%! assert (rgb, data(:,:,1:3)); +%! assert (alpha, data(:,:,4)); +%! endfunction +%! file_wrapper (@test_fn);