changeset 31164:3155aa74c62e

Tiff write: modified handling incorrect dimensions to match matlab.
author magedrifaat <magedrifaat@gmail.com>
date Wed, 10 Aug 2022 23:55:33 +0200
parents d701c6a4cda1
children 48d46f7a640b
files libinterp/corefcn/__tiff__.cc
diffstat 1 files changed, 15 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/__tiff__.cc	Wed Aug 10 20:59:51 2022 +0200
+++ b/libinterp/corefcn/__tiff__.cc	Wed Aug 10 23:55:33 2022 +0200
@@ -1179,9 +1179,7 @@
 
   void
   set_field_data (TIFF *tif, const TIFFField *fip, octave_value tag_ov)
-  {
-    // TODO(maged): prevent calling here for read-only images
-    
+  { 
     // TODO(maged): complete the implementation of this function
     uint32_t tag_id = TIFFFieldTag (fip);
     uint32_t tag_data = tag_ov.double_value ();
@@ -1503,7 +1501,7 @@
   void
   write_stripped_image (TIFF *tif, T pixel_data, tiff_image_data *image_data)
   {
-    // TODO(maged): remove this? ASSUMES pixel data dimensions are already validated
+    // ASSUMES pixel data dimensions are already validated
 
     typedef typename T::element_type P;
 
@@ -1590,7 +1588,7 @@
   void
   write_tiled_image (TIFF *tif, T pixel_data, tiff_image_data *image_data)
   {
-    // TODO(maged): remove this? ASSUMES pixel data dimensions are already validated
+    // ASSUMES pixel data dimensions are already validated
 
     uint32_t tile_width, tile_height;
     if (! TIFFGetField (tif, TIFFTAG_TILEWIDTH, &tile_width))
@@ -1710,14 +1708,21 @@
   {
     // TODO(maged): matlab sets the remaining to zero for less width and height
     // and issues a warning for larger widths but not for larger height? (we should error)
+    // and doesnt issue a warning for larger width for tiled images and produces zeros
     // and errors for less or more number of channels
-    if (image_data->height != pixel_data.dim1 ()
-        || image_data->width != pixel_data.dim2 ()
-        || pixel_data.ndims () > 3
-        || (image_data->samples_per_pixel > 1 && pixel_data.ndims () < 3)
+    if ((image_data->samples_per_pixel > 1 && pixel_data.ndims () < 3)
         || (pixel_data.ndims () > 2
             && image_data->samples_per_pixel != pixel_data.dim3 ()))
-      error ("Dimensions of the input don't match image dimenions");
+      error ("Incorrect number of channels, expected %u",
+             image_data->samples_per_pixel);
+    
+    if (pixel_data.dim1 () > image_data->height)
+      warning ("Input has more rows than the image length, data will be truncated");
+    if (pixel_data.dim2 () > image_data->width)
+      warning ("Input has more columns than the image width, data will be truncated");
+    
+    pixel_data.resize (dim_vector (image_data->height, image_data->width,
+                                   image_data->samples_per_pixel));
     
     if (image_data->is_tiled)
       write_tiled_image<T> (tif, pixel_data, image_data);