[LIBTIFF]
[reactos.git] / reactos / dll / 3rdparty / libtiff / tif_dir.c
index 702b512..782b136 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_dir.c,v 1.127 2016-10-25 21:35:15 erouault Exp $ */
+/* $Id: tif_dir.c,v 1.130 2017-05-17 21:54:05 erouault Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -32,6 +32,7 @@
  */
 
 #include <precomp.h>
+#include <float.h>
 
 /*
  * These are used in the backwards compatibility code...
@@ -155,6 +156,15 @@ bad:
        return (0);
 }
 
+static float TIFFClampDoubleToFloat( double val )
+{
+    if( val > FLT_MAX )
+        return FLT_MAX;
+    if( val < -FLT_MAX )
+        return -FLT_MAX;
+    return (float)val;
+}
+
 static int
 _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
 {
@@ -313,13 +323,13 @@ _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
         dblval = va_arg(ap, double);
         if( dblval < 0 )
             goto badvaluedouble;
-               td->td_xresolution = (float) dblval;
+               td->td_xresolution = TIFFClampDoubleToFloat( dblval );
                break;
        case TIFFTAG_YRESOLUTION:
         dblval = va_arg(ap, double);
         if( dblval < 0 )
             goto badvaluedouble;
-               td->td_yresolution = (float) dblval;
+               td->td_yresolution = TIFFClampDoubleToFloat( dblval );
                break;
        case TIFFTAG_PLANARCONFIG:
                v = (uint16) va_arg(ap, uint16_vap);
@@ -328,10 +338,10 @@ _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
                td->td_planarconfig = (uint16) v;
                break;
        case TIFFTAG_XPOSITION:
-               td->td_xposition = (float) va_arg(ap, double);
+               td->td_xposition = TIFFClampDoubleToFloat( va_arg(ap, double) );
                break;
        case TIFFTAG_YPOSITION:
-               td->td_yposition = (float) va_arg(ap, double);
+               td->td_yposition = TIFFClampDoubleToFloat( va_arg(ap, double) );
                break;
        case TIFFTAG_RESOLUTIONUNIT:
                v = (uint16) va_arg(ap, uint16_vap);
@@ -677,7 +687,7 @@ _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
                                case TIFF_SRATIONAL:
                                case TIFF_FLOAT:
                                        {
-                                               float v2 = (float)va_arg(ap, double);
+                                               float v2 = TIFFClampDoubleToFloat(va_arg(ap, double));
                                                _TIFFmemcpy(val, &v2, tv_size);
                                        }
                                        break;
@@ -855,6 +865,32 @@ _TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
        if( fip == NULL ) /* cannot happen since TIFFGetField() already checks it */
            return 0;
        
+        if( tag == TIFFTAG_NUMBEROFINKS )
+        {
+            int i;
+            for (i = 0; i < td->td_customValueCount; i++) {
+                uint16 val;
+                TIFFTagValue *tv = td->td_customValues + i;
+                if (tv->info->field_tag != tag)
+                    continue;
+                val = *(uint16 *)tv->value;
+                /* Truncate to SamplesPerPixel, since the */
+                /* setting code for INKNAMES assume that there are SamplesPerPixel */
+                /* inknames. */
+                /* Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2599 */
+                if( val > td->td_samplesperpixel )
+                {
+                    TIFFWarningExt(tif->tif_clientdata,"_TIFFVGetField",
+                                   "Truncating NumberOfInks from %u to %u",
+                                   val, td->td_samplesperpixel);
+                    val = td->td_samplesperpixel;
+                }
+                *va_arg(ap, uint16*) = val;
+                return 1;
+            }
+            return 0;
+        }
+
        /*
         * We want to force the custom code to be used for custom
         * fields even if the tag happens to match a well known