Lines 30-35
Link Here
|
30 |
* Directory Write Support Routines. |
30 |
* Directory Write Support Routines. |
31 |
*/ |
31 |
*/ |
32 |
#include "tiffiop.h" |
32 |
#include "tiffiop.h" |
|
|
33 |
#include <float.h> |
33 |
|
34 |
|
34 |
#ifdef HAVE_IEEEFP |
35 |
#ifdef HAVE_IEEEFP |
35 |
#define TIFFCvtNativeToIEEEFloat(tif, n, fp) |
36 |
#define TIFFCvtNativeToIEEEFloat(tif, n, fp) |
Lines 939-944
Link Here
|
939 |
return(0); |
940 |
return(0); |
940 |
} |
941 |
} |
941 |
|
942 |
|
|
|
943 |
static float TIFFClampDoubleToFloat( double val ) |
944 |
{ |
945 |
if( val > FLT_MAX ) |
946 |
return FLT_MAX; |
947 |
if( val < -FLT_MAX ) |
948 |
return -FLT_MAX; |
949 |
return (float)val; |
950 |
} |
951 |
|
952 |
static int8 TIFFClampDoubleToInt8( double val ) |
953 |
{ |
954 |
if( val > 127 ) |
955 |
return 127; |
956 |
if( val < -128 || val != val ) |
957 |
return -128; |
958 |
return (int8)val; |
959 |
} |
960 |
|
961 |
static int16 TIFFClampDoubleToInt16( double val ) |
962 |
{ |
963 |
if( val > 32767 ) |
964 |
return 32767; |
965 |
if( val < -32768 || val != val ) |
966 |
return -32768; |
967 |
return (int16)val; |
968 |
} |
969 |
|
970 |
static int32 TIFFClampDoubleToInt32( double val ) |
971 |
{ |
972 |
if( val > 0x7FFFFFFF ) |
973 |
return 0x7FFFFFFF; |
974 |
if( val < -0x7FFFFFFF-1 || val != val ) |
975 |
return -0x7FFFFFFF-1; |
976 |
return (int32)val; |
977 |
} |
978 |
|
979 |
static uint8 TIFFClampDoubleToUInt8( double val ) |
980 |
{ |
981 |
if( val < 0 ) |
982 |
return 0; |
983 |
if( val > 255 || val != val ) |
984 |
return 255; |
985 |
return (uint8)val; |
986 |
} |
987 |
|
988 |
static uint16 TIFFClampDoubleToUInt16( double val ) |
989 |
{ |
990 |
if( val < 0 ) |
991 |
return 0; |
992 |
if( val > 65535 || val != val ) |
993 |
return 65535; |
994 |
return (uint16)val; |
995 |
} |
996 |
|
997 |
static uint32 TIFFClampDoubleToUInt32( double val ) |
998 |
{ |
999 |
if( val < 0 ) |
1000 |
return 0; |
1001 |
if( val > 0xFFFFFFFFU || val != val ) |
1002 |
return 0xFFFFFFFFU; |
1003 |
return (uint32)val; |
1004 |
} |
1005 |
|
942 |
static int |
1006 |
static int |
943 |
TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value) |
1007 |
TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value) |
944 |
{ |
1008 |
{ |
Lines 959-965
Link Here
|
959 |
if (tif->tif_dir.td_bitspersample<=32) |
1023 |
if (tif->tif_dir.td_bitspersample<=32) |
960 |
{ |
1024 |
{ |
961 |
for (i = 0; i < count; ++i) |
1025 |
for (i = 0; i < count; ++i) |
962 |
((float*)conv)[i] = (float)value[i]; |
1026 |
((float*)conv)[i] = TIFFClampDoubleToFloat(value[i]); |
963 |
ok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv); |
1027 |
ok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv); |
964 |
} |
1028 |
} |
965 |
else |
1029 |
else |
Lines 971-989
Link Here
|
971 |
if (tif->tif_dir.td_bitspersample<=8) |
1035 |
if (tif->tif_dir.td_bitspersample<=8) |
972 |
{ |
1036 |
{ |
973 |
for (i = 0; i < count; ++i) |
1037 |
for (i = 0; i < count; ++i) |
974 |
((int8*)conv)[i] = (int8)value[i]; |
1038 |
((int8*)conv)[i] = TIFFClampDoubleToInt8(value[i]); |
975 |
ok = TIFFWriteDirectoryTagSbyteArray(tif,ndir,dir,tag,count,(int8*)conv); |
1039 |
ok = TIFFWriteDirectoryTagSbyteArray(tif,ndir,dir,tag,count,(int8*)conv); |
976 |
} |
1040 |
} |
977 |
else if (tif->tif_dir.td_bitspersample<=16) |
1041 |
else if (tif->tif_dir.td_bitspersample<=16) |
978 |
{ |
1042 |
{ |
979 |
for (i = 0; i < count; ++i) |
1043 |
for (i = 0; i < count; ++i) |
980 |
((int16*)conv)[i] = (int16)value[i]; |
1044 |
((int16*)conv)[i] = TIFFClampDoubleToInt16(value[i]); |
981 |
ok = TIFFWriteDirectoryTagSshortArray(tif,ndir,dir,tag,count,(int16*)conv); |
1045 |
ok = TIFFWriteDirectoryTagSshortArray(tif,ndir,dir,tag,count,(int16*)conv); |
982 |
} |
1046 |
} |
983 |
else |
1047 |
else |
984 |
{ |
1048 |
{ |
985 |
for (i = 0; i < count; ++i) |
1049 |
for (i = 0; i < count; ++i) |
986 |
((int32*)conv)[i] = (int32)value[i]; |
1050 |
((int32*)conv)[i] = TIFFClampDoubleToInt32(value[i]); |
987 |
ok = TIFFWriteDirectoryTagSlongArray(tif,ndir,dir,tag,count,(int32*)conv); |
1051 |
ok = TIFFWriteDirectoryTagSlongArray(tif,ndir,dir,tag,count,(int32*)conv); |
988 |
} |
1052 |
} |
989 |
break; |
1053 |
break; |
Lines 991-1009
Link Here
|
991 |
if (tif->tif_dir.td_bitspersample<=8) |
1055 |
if (tif->tif_dir.td_bitspersample<=8) |
992 |
{ |
1056 |
{ |
993 |
for (i = 0; i < count; ++i) |
1057 |
for (i = 0; i < count; ++i) |
994 |
((uint8*)conv)[i] = (uint8)value[i]; |
1058 |
((uint8*)conv)[i] = TIFFClampDoubleToUInt8(value[i]); |
995 |
ok = TIFFWriteDirectoryTagByteArray(tif,ndir,dir,tag,count,(uint8*)conv); |
1059 |
ok = TIFFWriteDirectoryTagByteArray(tif,ndir,dir,tag,count,(uint8*)conv); |
996 |
} |
1060 |
} |
997 |
else if (tif->tif_dir.td_bitspersample<=16) |
1061 |
else if (tif->tif_dir.td_bitspersample<=16) |
998 |
{ |
1062 |
{ |
999 |
for (i = 0; i < count; ++i) |
1063 |
for (i = 0; i < count; ++i) |
1000 |
((uint16*)conv)[i] = (uint16)value[i]; |
1064 |
((uint16*)conv)[i] = TIFFClampDoubleToUInt16(value[i]); |
1001 |
ok = TIFFWriteDirectoryTagShortArray(tif,ndir,dir,tag,count,(uint16*)conv); |
1065 |
ok = TIFFWriteDirectoryTagShortArray(tif,ndir,dir,tag,count,(uint16*)conv); |
1002 |
} |
1066 |
} |
1003 |
else |
1067 |
else |
1004 |
{ |
1068 |
{ |
1005 |
for (i = 0; i < count; ++i) |
1069 |
for (i = 0; i < count; ++i) |
1006 |
((uint32*)conv)[i] = (uint32)value[i]; |
1070 |
((uint32*)conv)[i] = TIFFClampDoubleToUInt32(value[i]); |
1007 |
ok = TIFFWriteDirectoryTagLongArray(tif,ndir,dir,tag,count,(uint32*)conv); |
1071 |
ok = TIFFWriteDirectoryTagLongArray(tif,ndir,dir,tag,count,(uint32*)conv); |
1008 |
} |
1072 |
} |
1009 |
break; |
1073 |
break; |
Lines 2094-2108
Link Here
|
2094 |
static int |
2158 |
static int |
2095 |
TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value) |
2159 |
TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value) |
2096 |
{ |
2160 |
{ |
|
|
2161 |
static const char module[] = "TIFFWriteDirectoryTagCheckedRational"; |
2097 |
uint32 m[2]; |
2162 |
uint32 m[2]; |
2098 |
assert(value>=0.0); |
|
|
2099 |
assert(sizeof(uint32)==4); |
2163 |
assert(sizeof(uint32)==4); |
2100 |
if (value<=0.0) |
2164 |
if( value < 0 ) |
|
|
2165 |
{ |
2166 |
TIFFErrorExt(tif->tif_clientdata,module,"Negative value is illegal"); |
2167 |
return 0; |
2168 |
} |
2169 |
else if( value != value ) |
2170 |
{ |
2171 |
TIFFErrorExt(tif->tif_clientdata,module,"Not-a-number value is illegal"); |
2172 |
return 0; |
2173 |
} |
2174 |
else if (value==0.0) |
2101 |
{ |
2175 |
{ |
2102 |
m[0]=0; |
2176 |
m[0]=0; |
2103 |
m[1]=1; |
2177 |
m[1]=1; |
2104 |
} |
2178 |
} |
2105 |
else if (value==(double)(uint32)value) |
2179 |
else if (value <= 0xFFFFFFFFU && value==(double)(uint32)value) |
2106 |
{ |
2180 |
{ |
2107 |
m[0]=(uint32)value; |
2181 |
m[0]=(uint32)value; |
2108 |
m[1]=1; |
2182 |
m[1]=1; |
Lines 2143-2154
Link Here
|
2143 |
} |
2217 |
} |
2144 |
for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++) |
2218 |
for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++) |
2145 |
{ |
2219 |
{ |
2146 |
if (*na<=0.0) |
2220 |
if (*na<=0.0 || *na != *na) |
2147 |
{ |
2221 |
{ |
2148 |
nb[0]=0; |
2222 |
nb[0]=0; |
2149 |
nb[1]=1; |
2223 |
nb[1]=1; |
2150 |
} |
2224 |
} |
2151 |
else if (*na==(float)(uint32)(*na)) |
2225 |
else if (*na >= 0 && *na <= (float)0xFFFFFFFFU && |
|
|
2226 |
*na==(float)(uint32)(*na)) |
2152 |
{ |
2227 |
{ |
2153 |
nb[0]=(uint32)(*na); |
2228 |
nb[0]=(uint32)(*na); |
2154 |
nb[1]=1; |
2229 |
nb[1]=1; |