changeset 31183:6ab628dfe2a0

__tif__.cc: added support for BiLevel Images with reversed bit ordering.
author magedrifaat <magedrifaat@gmail.com>
date Thu, 18 Aug 2022 15:59:33 +0200
parents 4c8b8c400a3b
children 86f91ea7a642
files libinterp/corefcn/__tiff__.cc
diffstat 1 files changed, 24 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/__tiff__.cc	Thu Aug 18 02:49:47 2022 +0200
+++ b/libinterp/corefcn/__tiff__.cc	Thu Aug 18 15:59:33 2022 +0200
@@ -327,7 +327,9 @@
           {
             for (uint32_t col = 0; col < image_data->width; col++)
             {
-              uint8_t shift = 7 - col % 8;
+              uint8_t shift = col % 8;
+                if (TIFFIsMSB2LSB (tif))
+                  shift = 7 - shift;
               strip_fvec[col] = (strip_buf[col / 8] >> shift) & 0x1;
             }
             strip_fvec += image_data->width;
@@ -409,7 +411,9 @@
           {
             for (uint32_t col = 0; col < tile_width; col++)
               {
-                uint8_t shift = 7 - col % 8;
+                uint8_t shift = col % 8;
+                if (TIFFIsMSB2LSB (tif))
+                  shift = 7 - shift;
                 tile_fvec[col] = (tile_buf [col / 8] >> shift) & 0x1;
               }  
             tile_fvec += tile_width;
@@ -646,8 +650,9 @@
             // to their respective bytes without overwriting the read data
             for (int64_t pixel = strip_size - 1; pixel >= 0; pixel--)
               {
-                // TODO(maged): is it necessary to check FillOrder?
-                uint8_t bit_number = 7 - pixel % 8;
+                uint8_t bit_number = pixel % 8;
+                if (TIFFIsMSB2LSB (tif))
+                  bit_number = 7 - bit_number;
                 uint8_t * img_u8 = reinterpret_cast<uint8_t *> (img_fvec);
                 img_fvec[pixel] = (img_u8[pixel / 8] >> bit_number) & 0x01;
               }
@@ -786,8 +791,9 @@
             // to their respective bytes without overwriting the read data
             for (int64_t pixel = tile_size - 1; pixel >= 0; pixel--)
               {
-                // TODO(maged): is it necessary to check FillOrder?
-                uint8_t bit_number = 7 - pixel % 8;
+                uint8_t bit_number = pixel % 8;
+                if (TIFFIsMSB2LSB (tif))
+                  bit_number = 7 - bit_number;
                 uint8_t * img_u8 = reinterpret_cast<uint8_t *> (img_fvec);
                 img_fvec[pixel]= (img_u8[pixel / 8] >> bit_number) & 0x01;
               }
@@ -1765,7 +1771,9 @@
           {
             for (uint32_t col = 0; col < image_data->width; col++)
             {
-              uint8_t shift = 7 - col % 8;
+              uint8_t shift = col % 8;
+                if (TIFFIsMSB2LSB (tif))
+                  shift = 7 - shift;
               strip_buf[row * padded_width + col / 8] |= data_u8[col] << shift;
             }
             data_u8 += image_data->width;
@@ -1853,7 +1861,9 @@
           {
             for (uint32_t col = 0; col < tile_width; col++)
             {
-              uint8_t shift = 7 - col % 8;
+              uint8_t shift = col % 8;
+                if (TIFFIsMSB2LSB (tif))
+                  shift = 7 - shift;
               tile_buf[row * tile_width/8 + col/8] |= data_u8[col] << shift;
             }
             data_u8 += tile_width;
@@ -2109,7 +2119,9 @@
               {
                 for (uint32_t col = 0; col < image_data->width; col++)
                 {
-                  uint8_t shift = 7 - col % 8;
+                  uint8_t shift = col % 8;
+                  if (TIFFIsMSB2LSB (tif))
+                    shift = 7 - shift;
                   strip_buf[row * padded_width + col / 8]
                     |= pixel_fvec[col] << shift;
                 }
@@ -2219,7 +2231,9 @@
               {
                 for (uint32_t col = 0; col < tile_width; col++)
                 {
-                  uint8_t shift = 7 - col % 8;
+                  uint8_t shift = col % 8;
+                  if (TIFFIsMSB2LSB (tif))
+                    shift = 7 - shift;
                   tile_buf[row * tile_width / 8 + col / 8]
                     |= pixel_fvec[col] << shift;
                 }
@@ -2564,8 +2578,6 @@
           error ("Too few arguments provided");
 
         const TIFFField *fip;
-        // TODO(maged): matlab actually checks for its own strings not LibTIFF's
-        // Make it work in both ways
         if (args(1).is_string ())
           {
             std::string tag_name = args(1).string_value ();