changeset 31127:0d9633ee715e

Tiff: fixed a bug where the default value of some tags was ignored * __tif__.cc: modified the call to TIFFGetField for some tags to make use of the default value of the tag.
author magedrifaat <magedrifaat@gmail.com>
date Sat, 23 Jul 2022 23:06:43 +0200
parents 7851c5b9c950
children 524cb3106432
files libinterp/dldfcn/__tiff__.cc
diffstat 1 files changed, 23 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/dldfcn/__tiff__.cc	Sat Jul 23 21:34:47 2022 +0200
+++ b/libinterp/dldfcn/__tiff__.cc	Sat Jul 23 23:06:43 2022 +0200
@@ -40,13 +40,16 @@
       if (! TIFFGetField (tif, TIFFTAG_IMAGELENGTH, &height))
         error ("Failed to read image height");
       
-      if (! TIFFGetField (tif, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel))
+      if (! TIFFGetFieldDefaulted (tif, TIFFTAG_SAMPLESPERPIXEL,
+                                   &samples_per_pixel))
         error ("Failed to read the SamplesPerPixel tag");
 
-      if (! TIFFGetField (tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample))
+      if (! TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE,
+                                   &bits_per_sample))
         error ("Failed to read the BitsPerSample tag");
       
-      if (! TIFFGetField (tif, TIFFTAG_PLANARCONFIG, &planar_configuration))
+      if (! TIFFGetFieldDefaulted (tif, TIFFTAG_PLANARCONFIG,
+                                   &planar_configuration))
         error ("Failed to read the PlanarConfiguration tag");
       
       is_tiled = TIFFIsTiled(tif);
@@ -643,10 +646,10 @@
     if (TIFFFieldPassCount (fip))
       error ("Unsupported tag");
     
-    // TODO(maged): test this function vs actual data type size
     int type_size = TIFFDataWidth (TIFFFieldDataType (fip));
     // TODO(maged): use shared pointer instead of malloc
     void *data = _TIFFmalloc (type_size);
+    // TODO(maged): check if this should be GetFieldDefaulted instead
     validate_tiff_get_field (TIFFGetField (tif, tag_id, data), data);
     octave_value tag_data_ov = interpret_tag_data (data, 1,
                                                    TIFFFieldDataType (fip));
@@ -691,7 +694,8 @@
       case TIFFTAG_GRAYRESPONSECURVE:
         {
           uint16_t bits_per_sample;
-          if (! TIFFGetField (tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample))
+          if (! TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE,
+                                            &bits_per_sample))
             error ("Failed to obtain the bit depth");
           
           tag_data_ov = get_array_field_data (tif, fip, 1<<bits_per_sample);
@@ -700,7 +704,8 @@
       case TIFFTAG_COLORMAP:
         {
           uint16_t bits_per_sample;
-          if (! TIFFGetField (tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample))
+          if (! TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE,
+                                       &bits_per_sample))
             error ("Failed to obtain the bit depth");
                 
           if (bits_per_sample > 24)
@@ -743,11 +748,13 @@
       case TIFFTAG_TRANSFERFUNCTION:
         {
           uint16_t samples_per_pixel;
-          if (! TIFFGetField (tif, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel))
+          if (! TIFFGetFieldDefaulted (tif, TIFFTAG_SAMPLESPERPIXEL,
+                                       &samples_per_pixel))
             error ("Failed to obtain the number of samples per pixel");
 
           uint16_t bits_per_sample;
-          if (! TIFFGetField (tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample))
+          if (! TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE,
+                                       &bits_per_sample))
             error ("Failed to obtain the number of samples per pixel");
 
           uint32_t count = 1 << bits_per_sample;
@@ -872,9 +879,14 @@
     T strip_data = T (strip_data_ov.array_value ());
     
     uint32_t rows_in_strip;
-    if (! TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rows_in_strip))
+    if (! TIFFGetFieldDefaulted (tif, TIFFTAG_ROWSPERSTRIP, &rows_in_strip))
       error ("Failed to obtain the RowsPerStrip tag");
     
+    // The tag has a default value of UINT32_MAX which means the entire
+    // image, but we need to cap it to the height for later calculations
+    if (rows_in_strip > image_data->height)
+      rows_in_strip = image_data->height;
+    
     uint32_t strip_count = TIFFNumberOfStrips (tif);
     uint32_t expected_numel;
 
@@ -924,6 +936,8 @@
                                                      image_data->width, 1));
       }
 
+    //TODO(maged): add suppot for 1-bit and 4-bit images
+
     // Permute the dimesions of the strip to match the expected memory
     // arrangement of LibTIFF (channel x width x height)
     Array<octave_idx_type> perm (dim_vector (3, 1));