changeset 31182:4c8b8c400a3b

__tiff__.cc (F__set_tag__, F__get_tag__): check for TagID names as well.
author magedrifaat <magedrifaat@gmail.com>
date Thu, 18 Aug 2022 02:49:47 +0200
parents 8a4ef572077d
children 6ab628dfe2a0
files libinterp/corefcn/__tiff__.cc
diffstat 1 files changed, 93 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/__tiff__.cc	Thu Aug 18 00:25:42 2022 +0200
+++ b/libinterp/corefcn/__tiff__.cc	Thu Aug 18 02:49:47 2022 +0200
@@ -105,6 +105,93 @@
     }
   };
 
+  // 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},
+    {"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", 6556}
+  };
+
+  const TIFFField *get_fip_with_name (TIFF *tif, std::string& tag_name)
+  {
+    const TIFFField *fip = TIFFFieldWithName (tif, tag_name.c_str ());
+    
+    // If the tag name is not found, fallback to the names defined
+    // in our name map.
+    if (! fip && tag_name_map.find (tag_name) != tag_name_map.cend ())
+      fip = TIFFFieldWithTag (tif, tag_name_map.at (tag_name));
+    
+    return fip;
+  }
+
   void
   check_readonly (TIFF *tif)
   {
@@ -2322,7 +2409,6 @@
 
     if (nargin == 0 || nargin > 2)
       {
-        // TODO(maged): return invalid object instead??
         error ("No filename supplied\n");
       }
 
@@ -2414,21 +2500,18 @@
 
     TIFF *tif = tiff_handle->get_file ();
 
-    uint32_t tag_id;
     const TIFFField *fip;
     
     if (args(1).is_string ())
       {
-        std::string tagName = args(1).string_value ();
-        fip = TIFFFieldWithName (tif, tagName.c_str ());
+        std::string tag_name = args(1).string_value ();
+        fip = get_fip_with_name (tif, tag_name);
         if (! fip)
           error ("Tiff tag not found");
-        
-        tag_id = TIFFFieldTag (fip);
       }
     else
       {
-        tag_id = args(1).int_value ();
+        ttag_t tag_id = args(1).int_value ();
         fip = TIFFFieldWithTag (tif, tag_id);
         
         if (! fip)
@@ -2469,7 +2552,7 @@
         for (octave_idx_type i = 0; i < keys.numel (); i++)
           {
             std::string key = keys[i];
-            const TIFFField *fip =  TIFFFieldWithName (tif, key.c_str ());
+            const TIFFField *fip =  get_fip_with_name (tif, key);
             if (! fip)
               error ("Tag %s not found", key.c_str ());
             set_field_data (tif, fip, tags.contents (key));
@@ -2485,8 +2568,8 @@
         // Make it work in both ways
         if (args(1).is_string ())
           {
-            std::string tagName = args(1).string_value ();
-            fip = TIFFFieldWithName (tif, tagName.c_str ());
+            std::string tag_name = args(1).string_value ();
+            fip = get_fip_with_name (tif, tag_name);
             if (! fip)
               error ("Tiff tag not found");
           }