changeset 31125:3b775b939de4

Tiff read: Added support for floating-point images * __tif__.cc(F__tiff_read__): handled the SampleFormat tag to support reading floating-point images.
author magedrifaat <magedrifaat@gmail.com>
date Fri, 22 Jul 2022 05:07:00 +0200
parents e8d1cc309bc9
children 7851c5b9c950
files libinterp/dldfcn/__tiff__.cc
diffstat 1 files changed, 29 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/dldfcn/__tiff__.cc	Thu Jul 21 22:44:01 2022 +0200
+++ b/libinterp/dldfcn/__tiff__.cc	Fri Jul 22 05:07:00 2022 +0200
@@ -145,7 +145,8 @@
               {
                 // TODO(maged): is it necessary to check FillOrder?
                 uint8_t bit_number = 7 - pixel % 8;
-                img_fvec[pixel] = (img_fvec[pixel / 8] >> bit_number) & 0x01;
+                img_fvec[pixel]
+                  = (((uint8_t *)img_fvec)[pixel / 8] >> bit_number) & 0x01;
               }
             break;
           }
@@ -167,7 +168,8 @@
             for (int64_t pixel = strip_size - 1; pixel >= 0; pixel--)
               {
                 uint8_t shift = pixel % 2 == 0? 4: 0;
-                img_fvec[pixel] = (img_fvec[pixel / 2] >> shift) & 0x0F;
+                img_fvec[pixel]
+                  = (((uint8_t *)img_fvec)[pixel / 2] >> shift) & 0x0F;
               }
             break;
           }
@@ -283,7 +285,8 @@
               {
                 // TODO(maged): is it necessary to check FillOrder?
                 uint8_t bit_number = 7 - pixel % 8;
-                img_fvec[pixel] = (img_fvec[pixel / 8] >> bit_number) & 0x01;
+                img_fvec[pixel]
+                  = (((uint8_t *)img_fvec)[pixel / 8] >> bit_number) & 0x01;
               }
             break;
           }
@@ -305,7 +308,8 @@
             for (int64_t pixel = tile_size - 1; pixel >= 0; pixel--)
               {
                 uint8_t shift = pixel % 2 == 0? 4: 0;
-                img_fvec[pixel] = (img_fvec[pixel / 2] >> shift) & 0x0F;
+                img_fvec[pixel]
+                  = (((uint8_t *)img_fvec)[pixel / 2] >> shift) & 0x0F;
               }
             break;
           }
@@ -1034,6 +1038,18 @@
     // data to avoid repeating the same calls to TIFFGetField in the different
     // functions as each call is a possible point of failure
     tiff_image_data image_data (tif);
+
+    uint16_t sample_format;
+    if (! TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLEFORMAT, &sample_format))
+      error ("Failed to obtain a value for sample format");
+
+    if (sample_format == 3)
+      {
+        if (false && image_data.bits_per_sample != 32 && image_data.bits_per_sample != 64)
+          error ("Floating point images are only supported for bit depths of 32 and 64");
+      }
+    else if (sample_format != 1 && sample_format != 4)
+      error ("Unsupported sample format");
     
     octave_value_list retval;
     switch (image_data.bits_per_sample)
@@ -1049,10 +1065,17 @@
         retval(0) = read_image<uint16NDArray> (tif, &image_data);
         break;
       case 32:
-        retval(0) = read_image<uint32NDArray> (tif, &image_data);
+        // TODO(maged): do we need to check for Min and MaxSampleValue
+        if (sample_format == 3)
+          retval(0) = read_image<FloatNDArray> (tif, &image_data);
+        else
+          retval(0) = read_image<uint32NDArray> (tif, &image_data);
         break;
       case 64:
-        retval(0) = read_image<uint64NDArray> (tif, &image_data);
+        if (sample_format == 3)
+          retval(0) = read_image<NDArray> (tif, &image_data);
+        else
+          retval(0) = read_image<uint64NDArray> (tif, &image_data);
         break;
       default:
         error ("Unsupported bit depth");