changeset 31185:a1145ac2ce9b

Tiff: populated TagID from the C++ map to avoid having two copies * __tiff__.cc (F__tiff_make_tagid__): implemented internal function as initializer for TagID. * Tiff.m: changed the initialization for TagID to use the internal function.
author magedrifaat <magedrifaat@gmail.com>
date Thu, 18 Aug 2022 17:23:43 +0200
parents 86f91ea7a642
children 90eccc78d958
files libinterp/corefcn/__tiff__.cc scripts/io/Tiff.m
diffstat 2 files changed, 13 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/__tiff__.cc	Thu Aug 18 16:39:56 2022 +0200
+++ b/libinterp/corefcn/__tiff__.cc	Thu Aug 18 17:23:43 2022 +0200
@@ -93,12 +93,13 @@
                                    &bits_per_sample))
         error ("Failed to read the BitsPerSample tag");
       
-      // TODO(maged): this doesn't really work for writing as LibTIFF will
-      // refuse to write unless the tag is set
       if (! TIFFGetField (tif, TIFFTAG_PLANARCONFIG,
                           &planar_configuration))
         // LibTIFF has a bug where it incorrectly returns 0 as a default
         // value for PlanarConfiguration although the default value is 1
+        // This doesn't completely solve the issue as LibTIFF will refuse
+        // to write data to images of the tag is not set.
+        // see: https://www.asmail.be/msg0054918184.html
         planar_configuration = 1;
       
       is_tiled = TIFFIsTiled(tif);
@@ -107,7 +108,6 @@
 
   // A map of tag names supported by matlab, there are some differences
   // than LibTIFF's names (e.g. Photometric vs PhotometricInerpretation)
-  // TODO(maged): use this to initialize TagID to avoid having two copies
   static const std::map<std::string, ttag_t> tag_name_map = {
     {"SubFileType", 254},
     {"ImageWidth", 256},
@@ -1073,6 +1073,7 @@
       {
         // Workaround for a bug in LibTIFF where it incorrectly returns
         // zero as the default value for PlanaConfiguration
+        // See: https://www.asmail.be/msg0054918184.html
         if (! TIFFGetField(tif, tag_id, data_ptr))
           *reinterpret_cast<uint16_t *> (data_ptr) = 1;
       }
@@ -3471,4 +3472,12 @@
 #endif
   }
 
+  DEFUN (__tiff_make_tagid__, , ,
+         "Create the TagID structure from the internal map")
+  {
+    std::map<std::string, octave_value> tag_ov_map;
+    for (auto it = tag_name_map.cbegin (); it != tag_name_map.cend (); it++)
+      tag_ov_map[it->first] = octave_value (it->second);
+    return octave_value_list(octave_scalar_map (tag_ov_map));
+  }
 }
--- a/scripts/io/Tiff.m	Thu Aug 18 16:39:56 2022 +0200
+++ b/scripts/io/Tiff.m	Thu Aug 18 17:23:43 2022 +0200
@@ -2,77 +2,7 @@
 
 classdef Tiff
   properties (Constant = true)
-    TagID = struct (
-      "SubFileType", 254,
-      "ImageWidth", 256,
-      "ImageLength", 257,
-      "BitsPerSample", 258,
-      "Compression", 259,
-      "Photometric", 262,
-      "Thresholding", 263,
-      "FillOrder", 266,
-      "DocumentName", 269,
-      "ImageDescription", 270,
-      "Make", 271,
-      "Model", 272,
-      "StripOffsets", 273,
-      "Orientation", 274,
-      "SamplesPerPixel", 277,
-      "RowsPerStrip", 278,
-      "StripByteCounts", 279,
-      "MinSampleValue", 280,
-      "MaxSampleValue", 281,
-      "XResolution", 282,
-      "YResolution", 283,
-      "PlanarConfiguration", 284,
-      "PageName", 285,
-      "XPosition", 286,
-      "YPosition", 287,
-      "GrayResponseUnit", 290,
-      'GrayResponseCurve', 291,
-      "Group3Options", 292,
-      "Group4Options", 293,
-      "ResolutionUnit", 296,
-      "PageNumber", 297,
-      "TransferFunction", 301,
-      "Software", 305,
-      "DateTime", 306,
-      "Artist", 315,
-      "HostComputer", 316,
-      "WhitePoint", 318,
-      "PrimaryChromaticities", 319,
-      "ColorMap", 320,
-      "HalfToneHints", 321,
-      "TileWidth", 322,
-      "TileLength", 323,
-      "TileOffsets", 324,
-      "TileByteCounts", 325,
-      "SubIFD", 330,
-      "InkSet", 332,
-      "InkNames", 333,
-      "NumberOfInks", 334,
-      "DotRange", 336,
-      "TargetPrinter", 337,
-      "ExtraSamples", 338,
-      "SampleFormat", 339,
-      "SMinSampleValue", 340,
-      "SMaxSampleValue", 341,
-      "YCbCrCoefficients", 529,
-      "YCbCrSubSampling", 530,
-      "YCbCrPositioning", 531,
-      "ReferenceBlackWhite", 532,
-      "XMP", 700,
-      "ImageDepth", 32997,
-      "Copyright", 33432,
-      "RichTIFFIPTC", 33723,
-      "Photoshop", 34377,
-      "ICCProfile", 34675,
-      "SToNits", 37439,
-      "JPEGQuality", 65537,
-      "JPEGColorMode", 65538,
-      "ZipQuality", 65557,
-      "SGILogDataFmt", 65560
-    );
+    TagID = __tiff_make_tagid__ ();
 
     Compression = struct (
       "None", 1,