changeset 31166:b8b6cc05c8ea

Tiff setTag: added support for tags with array values.
author magedrifaat <magedrifaat@gmail.com>
date Fri, 12 Aug 2022 01:23:07 +0200
parents 48d46f7a640b
children f91cd5ceaae6
files libinterp/corefcn/__tiff__.cc
diffstat 1 files changed, 74 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/__tiff__.cc	Fri Aug 12 00:04:37 2022 +0200
+++ b/libinterp/corefcn/__tiff__.cc	Fri Aug 12 01:23:07 2022 +0200
@@ -1197,6 +1197,9 @@
         // TODO(maged): check if matlab errors for long data type/range
         TIFFSetField (tif, tag_id, tag_ov.uint8_scalar_value ());
         break;
+      case TIFF_ASCII:
+        TIFFSetField (tif, tag_id, tag_ov.string_value ().c_str ());
+        break;
       case TIFF_SHORT:
         TIFFSetField (tif, tag_id, tag_ov.uint16_scalar_value ());
         break;
@@ -1243,6 +1246,65 @@
   set_array_field_data (TIFF *tif, const TIFFField *fip,
                         octave_value tag_ov, uint32_t count)
   {
+    uint32_t tag_id = TIFFFieldTag (fip);
+    TIFFDataType tag_datatype = TIFFFieldDataType (fip);
+
+    // TODO(maged): validate count?
+    octave_unused_parameter (count);
+
+    void *data_ptr;
+
+    switch (tag_datatype)
+      {
+      case TIFF_BYTE:
+      case TIFF_UNDEFINED:
+        // TODO(maged): check if matlab errors for long data type/dimensions
+        // TODO(maged): should we resize/reshape?
+        data_ptr = tag_ov.uint8_array_value ().fortran_vec ();
+        break;
+      case TIFF_SHORT:
+        data_ptr = tag_ov.uint16_array_value ().fortran_vec ();
+        break;
+      case TIFF_LONG:
+        data_ptr = tag_ov.uint32_array_value ().fortran_vec ();
+        break;
+      case TIFF_LONG8:
+        data_ptr = tag_ov.uint64_array_value ().fortran_vec ();
+        break;
+      case TIFF_RATIONAL:
+        data_ptr = tag_ov.float_array_value ().fortran_vec ();
+        break;
+      case TIFF_SBYTE:
+        data_ptr = tag_ov.int8_array_value ().fortran_vec ();
+        break;
+      case TIFF_SSHORT:
+        data_ptr = tag_ov.int16_array_value ().fortran_vec ();
+        break;
+      case TIFF_SLONG:
+        data_ptr = tag_ov.int32_array_value ().fortran_vec ();
+        break;
+      case TIFF_SLONG8:
+        data_ptr = tag_ov.int64_array_value ().fortran_vec ();
+        break;
+      case TIFF_FLOAT:
+        data_ptr = tag_ov.float_array_value ().fortran_vec ();
+        break;
+      case TIFF_DOUBLE:
+        data_ptr = tag_ov.array_value ().fortran_vec ();
+        break;
+      case TIFF_SRATIONAL:
+        data_ptr = tag_ov.float_array_value ().fortran_vec ();
+        break;
+      case TIFF_IFD:
+      case TIFF_IFD8:
+        error ("Unimplemented IFFD data type");
+        break;
+      default:
+        error ("Unsupported tag data type");
+      }
+
+    if (! TIFFSetField (tif, tag_id, data_ptr))
+      error ("Failed to set tag");
   }
 
   void
@@ -1290,10 +1352,18 @@
           if (bits_per_sample > 16)
             error ("Too high bit depth for a palette image");
 
+          // TODO(maged): what is matlab behavior for wrong input dimensions
           uint32_t count = 1 << bits_per_sample;
-          // uint16_t *red, *green, *blue;
-          // validate_tiff_get_field (TIFFGetField (tif, TIFFTAG_COLORMAP,
-          //                                        &red, &green, &blue));
+          NDArray array_data = tag_ov.array_value ();
+          array_data *= UINT16_MAX;
+          uint16NDArray u16_array = uint16NDArray (array_data);
+          uint16_t *data_ptr
+            = reinterpret_cast<uint16_t *> (u16_array.fortran_vec ());
+          uint16_t *red = data_ptr;
+          uint16_t *green = red + count;
+          uint16_t *blue = green + count;
+          if (! TIFFSetField (tif, tag_id, red, green, blue))
+            error ("Failed to set tag");
           break;
         }
       case TIFFTAG_TRANSFERFUNCTION:
@@ -1367,10 +1437,7 @@
       default:
         set_scalar_field_data (tif, fip, tag_ov);
     }
-    uint32_t tag_data = tag_ov.double_value ();
-
-    if (! TIFFSetField(tif, tag_id, tag_data))
-      error ("Failed to set tag value");
+    
   }
   
   template <typename T>