Mercurial > octave-libtiff
changeset 31140:5f70efad6e2c
Tiff setTag: added support for setting multiple tags at once using structs
* __tiff__.cc(F_tiff_set_tag__): implemented support for handling struct
of multiple tags.
* Tiff.m: modified tests to use the struct method for better readability.
author | magedrifaat <magedrifaat@gmail.com> |
---|---|
date | Thu, 28 Jul 2022 23:32:23 +0200 |
parents | 3431a15b2c75 |
children | af4519cbfcae |
files | libinterp/dldfcn/__tiff__.cc scripts/io/Tiff.m |
diffstat | 2 files changed, 66 insertions(+), 54 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/dldfcn/__tiff__.cc Thu Jul 28 20:50:41 2022 +0200 +++ b/libinterp/dldfcn/__tiff__.cc Thu Jul 28 23:32:23 2022 +0200 @@ -1038,8 +1038,7 @@ uint32_t tag_id; const TIFFField *fip; - if (args (1).type_name () == "string" - || args (1).type_name () == "sq_string") + if (args (1).is_string ()) { std::string tagName = args (1).string_value (); fip = TIFFFieldWithName (tif, tagName.c_str ()); @@ -1075,16 +1074,32 @@ TIFF *tif = (TIFF *)(args (0).uint64_value ()); - // TODO(maged): does matlab allow calling this function for images - // opened for reading? - if (args (1).type_name () == "struct") - error ("setTag with struct is not yet supported"); + // TODO(maged): should all tags be verified first? + if (args (1).isstruct ()) + { + octave_scalar_map tags = args (1).scalar_map_value (); + // Verify existance of all tags first + for (auto tag_it = tags.cbegin (); tag_it != tags.cend (); tag_it++) + { + std::string key = tags.key (tag_it); + if (! TIFFFieldWithName (tif, key.c_str ())) + error ("Tag %s not found", key.c_str ()); + } + + for (auto tag_it = tags.cbegin (); tag_it != tags.cend (); tag_it++) + { + std::string key = tags.key (tag_it); + const TIFFField *fip = TIFFFieldWithName (tif, key.c_str ()); + set_field_data (tif, fip, tags.contents (tag_it)); + } + } else { + if (nargin < 3) + error ("Too few arguments provided"); + const TIFFField *fip; - - if (args (1).type_name () == "string" - || args(1).type_name () == "sq_string") + if (args (1).is_string ()) { std::string tagName = args (1).string_value (); fip = TIFFFieldWithName (tif, tagName.c_str ()); @@ -1099,9 +1114,6 @@ error ("Tiff tag not found"); } - if (nargin < 3) - error ("Too few arguments provided"); - set_field_data (tif, fip, args (2)); }
--- a/scripts/io/Tiff.m Thu Jul 28 20:50:41 2022 +0200 +++ b/scripts/io/Tiff.m Thu Jul 28 23:32:23 2022 +0200 @@ -95,8 +95,8 @@ tag = __tiff_get_tag__ (t.tiff_handle, tag_name); endfunction - function setTag (t, tag_name, tag_value) - __tiff_set_tag__ (t.tiff_handle, tag_name, tag_value); + function setTag (t, varargin) + __tiff_set_tag__ (t.tiff_handle, varargin{:}); endfunction function argout = read (t) @@ -111,20 +111,6 @@ endmethods endclassdef -%!function set_configs (img, height, width, channels=-1, bit_depth=-1, rows_per_strip=-1) -%! setTag (img, "ImageLength", height); -%! setTag (img, "ImageWidth", width); -%! if channels != -1 -%! setTag (img, "SamplesPerPixel", channels); -%! endif -%! if bit_depth != -1 -%! setTag (img, "BitsPerSample", bit_depth); -%! endif -%! if rows_per_strip != -1 -%! setTag(img, "RowsPerStrip", rows_per_strip); -%! endif -%!endfunction - %!function file_wrapper (fn) %! filename = [tempname() ".tif"]; %! unwind_protect @@ -146,7 +132,8 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 1, 1, 1, 8); +%! setTag (img, struct ("ImageLength", 1, "ImageWidth", 1, +%! "BitsPerSample", 8, "SamplesPerPixel", 1)); %! data = uint8 (randi (intmax ("uint8"), 1, 1)); %! writeEncodedStrip (img, 1, data); %! img.close (); @@ -168,7 +155,8 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 1, 10, 1, 8); +%! setTag (img, struct ("ImageLength", 1, "ImageWidth", 10, +%! "BitsPerSample", 8, "SamplesPerPixel", 1)); %! data = uint8 (reshape (1:10, [1, 10])); %! writeEncodedStrip (img, 1, data); %! img.close (); @@ -180,7 +168,8 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 1, 10, 1, 8); +%! setTag (img, struct ("ImageLength", 1, "ImageWidth", 10, +%! "BitsPerSample", 8, "SamplesPerPixel", 1)); %! data = uint8 (reshape (1:9, [1, 9])); %! writeEncodedStrip (img, 1, data); %! img.close (); @@ -192,7 +181,8 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 10, 10, 1, 8); +%! setTag (img, struct ("ImageLength", 10, "ImageWidth", 10, +%! "BitsPerSample", 8, "SamplesPerPixel", 1)); %! data = uint8 (reshape (1:100, [10, 10])); %! writeEncodedStrip (img, 1, data); %! img.close (); @@ -204,7 +194,8 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 10, 10, 1, 8); +%! setTag (img, struct ("ImageLength", 10, "ImageWidth", 10, +%! "BitsPerSample", 8, "SamplesPerPixel", 1)); %! data = uint8 (reshape (1:110, [11, 10])); %! writeEncodedStrip (img, 1, data); %! img.close (); @@ -216,9 +207,10 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 10, 10, 3, 8); -%! setTag(img, "PlanarConfiguration", 1); -%! setTag(img, "PhotometricInterpretation", 2); +%! setTag (img, struct ("ImageLength", 10, "ImageWidth", 10, +%! "BitsPerSample", 8, "SamplesPerPixel", 3, +%! "PlanarConfiguration", 1, +%! "PhotometricInterpretation", 2)); %! data = uint8 (reshape (1:300, [10, 10, 3])); %! writeEncodedStrip (img, 1, data); %! img.close (); @@ -230,9 +222,10 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 10, 10, 3, 8); -%! setTag(img, "PlanarConfiguration", 1); -%! setTag(img, "PhotometricInterpretation", 2); +%! setTag (img, struct ("ImageLength", 10, "ImageWidth", 10, +%! "BitsPerSample", 8, "SamplesPerPixel", 3, +%! "PlanarConfiguration", 1, +%! "PhotometricInterpretation", 2)); %! data = uint8 (reshape (1:400, [10, 10, 4])); %! writeEncodedStrip (img, 1, data); %! img.close (); @@ -244,9 +237,10 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 10, 10, 3, 8); -%! setTag(img, "PlanarConfiguration", 2); -%! setTag(img, "PhotometricInterpretation", 2); +%! setTag (img, struct ("ImageLength", 10, "ImageWidth", 10, +%! "BitsPerSample", 8, "SamplesPerPixel", 3, +%! "PlanarConfiguration", 2, +%! "PhotometricInterpretation", 2)); %! data = uint8 (reshape (1:300, [10, 10, 3])); %! for i = 1:3 %! writeEncodedStrip (img, i, data(:,:,i)); @@ -260,7 +254,8 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 20, 20, 1, 16); +%! setTag (img, struct ("ImageLength", 20, "ImageWidth", 20, +%! "BitsPerSample", 16, "SamplesPerPixel", 1)); %! data = uint16 (reshape (1:400, [20, 20])); %! writeEncodedStrip (img, 1, data); %! img.close (); @@ -272,7 +267,8 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 20, 20, 1, 32); +%! setTag (img, struct ("ImageLength", 20, "ImageWidth", 20, +%! "BitsPerSample", 32, "SamplesPerPixel", 1)); %! data = uint32 (reshape (1:400, [20, 20])); %! writeEncodedStrip (img, 1, data); %! img.close (); @@ -284,9 +280,10 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 20, 20, 3, 16); -%! setTag(img, "PlanarConfiguration", 1); -%! setTag(img, "PhotometricInterpretation", 2); +%! setTag (img, struct ("ImageLength", 20, "ImageWidth", 20, +%! "BitsPerSample", 16, "SamplesPerPixel", 3, +%! "PlanarConfiguration", 1, +%! "PhotometricInterpretation", 2)); %! data = uint16 (reshape (1:1200, [20, 20, 3])); %! writeEncodedStrip (img, 1, data); %! img.close (); @@ -298,9 +295,10 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 20, 20, 3, 32); -%! setTag(img, "PlanarConfiguration", 1); -%! setTag(img, "PhotometricInterpretation", 2); +%! setTag (img, struct ("ImageLength", 20, "ImageWidth", 20, +%! "BitsPerSample", 32, "SamplesPerPixel", 3, +%! "PlanarConfiguration", 1, +%! "PhotometricInterpretation", 2)); %! data = uint32 (reshape (1:1200, [20, 20, 3])); %! writeEncodedStrip (img, 1, data); %! img.close (); @@ -312,9 +310,10 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 20, 20, 3, 16); -%! setTag(img, "PlanarConfiguration", 1); -%! setTag(img, "PhotometricInterpretation", 2); +%! setTag (img, struct ("ImageLength", 20, "ImageWidth", 20, +%! "BitsPerSample", 16, "SamplesPerPixel", 3, +%! "PlanarConfiguration", 1, +%! "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 (); @@ -325,7 +324,8 @@ %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); -%! set_configs (img, 1, 1, 1, 8); +%! setTag (img, struct ("ImageLength", 1, "ImageWidth", 1, +%! "BitsPerSample", 8, "SamplesPerPixel", 1)); %! data = uint8 (1); %! writeEncodedStrip (img, 1, data); %! img.close();