如何使用LibTiff.Net 2.3库编写200/1的合理标签

问题描述 投票:2回答:2

我不知道为什么这么难做但我无法让LibTiff.Net 2.3正确设置一个合理的值...多年来我总是在tiff标签号282(XRESOLUTION)中使用像“200/1”这样的值)和283(YRESOLUTION)。但是当使用LibTiff.Net库时,似乎无法获得结果。我总是得到像“419430400/2097152”这样的东西。有谁知道如何解决这个问题?

关于我的问题的注释:

这是libtiff库(pre .Net),它看起来像第一个,如果它像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);
}
c# tags tiff libtiff.net
2个回答
5
投票

如果这对你有帮助,一定要给我1UP。谢谢!

好的,所以我决定对图书馆进行编辑以考虑减少的分数。下面是我在.Net库中更改的代码,以使其工作。它的工作!

我希望Bobrovsky在他的下一个版本中包括这个。我相信其他人会感激我的其他人! ;)

如果你有点不确定如何编辑库这里是我尽可能详细的步骤...

1)下载位于here的库源。

2)打开项目文件。我使用LibTiff.NET_NoSilverlight.sln解决方案文件来打开项目。

3)展开LibTiff项目节点。

4)展开Internals节点。

5)找到Tiff_DirWrite.cs类文件并打开它。

6)在该类文件中,我找到了一个名为writeRationalArray的函数,这就是它的样子......

/// <summary>
/// Setup a directory entry of an array of RATIONAL or SRATIONAL and
/// write the associated indirect values.
/// </summary>
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);
}

7)编辑writeRationalArray函数,如下所示......

/// <summary>
/// Setup a directory entry of an array of RATIONAL or SRATIONAL and
/// write the associated indirect values.
/// </summary>
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;

        //Reduce the fraction
        int a = t[2 * i + 0];
        int b = t[2 * i + 1];
        while (b > 0) { int rem = a % b; a = b; b = rem; }
        for (int ind = 0; ind < 2; ind++) { t[2 * i + ind] /= a; }
    }

    return writeData(ref dir, t, 2 * dir.tdir_count);
}

我所做的就是添加三行代码来减少最后的分数。


3
投票

使用LibTiff.Net时,您应该设置如下的理性值:

image.SetField(TiffTag.XRESOLUTION, 150.0);
image.SetField(TiffTag.YRESOLUTION, 150.0);

库将在TIFF中将一个有理值写为一对整数值(被除数和除数)。你不需要帮助它。

© www.soinside.com 2019 - 2024. All rights reserved.