我不知道为什么这很难做到,但我无法让 LibTiff.Net 2.3 正确设置合理值......多年来,我一直在 tiff 标签号 282(XRESOLUTION)中使用像“200/1”这样的值) 和 283(Y 分辨率)。但是当使用 LibTiff.Net 库时,似乎不可能得到这样的结果。我总是得到像“419430400/2097152”这样的东西。有谁知道我该如何解决这个问题?
关于我的问题的注释:
这是 libtiff 库(.Net 之前),它看起来像第一个 else if 确实占了 200/1 之类的东西。
TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
{
static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray";
uint32* m;
float* na;
uint32* nb;
uint32 nc;
int o;
assert(sizeof(uint32)==4);
m=_TIFFmalloc(count*2*sizeof(uint32));
if (m==NULL)
{
TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
return(0);
}
for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
{
if (*na<=0.0)
{
nb[0]=0;
nb[1]=1;
}
else if (*na==(float)(uint32)(*na))
{
nb[0]=(uint32)(*na);
nb[1]=1;
}
else if (*na<1.0)
{
nb[0]=(uint32)((*na)*0xFFFFFFFF);
nb[1]=0xFFFFFFFF;
}
else
{
nb[0]=0xFFFFFFFF;
nb[1]=(uint32)(0xFFFFFFFF/(*na));
}
}
if (tif->tif_flags&TIFF_SWAB)
TIFFSwabArrayOfLong(m,count*2);
o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,count,count*8,&m[0]);
_TIFFfree(m);
return(o);
}
这是新的 .Net 版本...
private bool writeRationalArray(ref TiffDirEntry dir, float[] v)
{
int[] t = new int [2 * dir.tdir_count];
for (int i = 0; i < dir.tdir_count; i++)
{
int sign = 1;
float fv = v[i];
if (fv < 0)
{
if (dir.tdir_type == TiffType.RATIONAL)
{
WarningExt(this, m_clientdata, m_name,
"\"{0}\": Information lost writing value ({1:G}) as (unsigned) RATIONAL",
FieldWithTag(dir.tdir_tag).Name, fv);
fv = 0;
}
else
{
fv = -fv;
sign = -1;
}
}
int den = 1;
if (fv > 0)
{
while (fv < (1L << (31 - 3)) && den < (1L << (31 - 3)))
{
fv *= 1 << 3;
den *= 1 << 3;
}
}
t[2 * i + 0] = (int)(sign * (fv + 0.5));
t[2 * i + 1] = den;
}
return writeData(ref dir, t, 2 * dir.tdir_count);
}