c++ - 尝试将 "ExtraSamples"标记应用于要写入的 TIFF 文件时出错

标签 c++ file-io tiff libtiff

我有一个程序可以拍摄图像并将其写入 TIFF 文件。图像可以是灰度(8 位)、带 alpha channel 的灰度(16 位)、RGB(24 位)或 ARGB(32 位)。我在没有 alpha channel 的情况下写出图像没有任何问题,但是对于带有 alpha 的图像,当我尝试设置额外的样本标签时,我被发送到 TIFFSetErrorHandler 设置的 TIFF 错误处理例程。传入的消息是 <filename>: Bad value 1 for "ExtraSamples"在 _TIFFVSetField 模块中。下面是一些示例代码:

#include "tiff.h"
#include "tiffio.h"
#include "xtiffio.h"
//Other includes

class MyTIFFWriter
{
public:
    MyTIFFWriter(void);

    ~MyTIFFWriter(void);

    bool writeFile(MyImage* outputImage);
    bool openFile(std::string filename);
    void closeFile();

private:
    TIFF* m_tif;
};

//...

bool MyTIFFWriter::writeFile(MyImage* outputImage)
{
    // check that we have data and that the tiff is ready for writing
    if (outputImage->getHeight() == 0 || outputImage->getWidth() == 0 || !m_tif)
        return false;

    TIFFSetField(m_tif, TIFFTAG_IMAGEWIDTH, outputImage->getWidth());
    TIFFSetField(m_tif, TIFFTAG_IMAGELENGTH, outputImage->getHeight());
    TIFFSetField(m_tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
    TIFFSetField(m_tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);

    if (outputImage->getColourMode() == MyImage::ARGB)
    {
        TIFFSetField(m_tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
        TIFFSetField(m_tif, TIFFTAG_BITSPERSAMPLE, outputImage->getBitDepth() / 4);
        TIFFSetField(m_tif, TIFFTAG_SAMPLESPERPIXEL, 4);
        TIFFSetField(m_tif, TIFFTAG_EXTRASAMPLES, EXTRASAMPLE_ASSOCALPHA);  //problem exists here
    } else if (/*other mode*/)
        //apply other mode settings

    //...

    return (TIFFWriteEncodedStrip(m_tif, 0, outputImage->getImgDataAsCharPtr(), 
        outputImage->getWidth() * outputImage->getHeight() *
        (outputImage->getBitDepth() / 8)) != -1);
}

据我所知,标签从未写入文件。幸运的是,GIMP 仍然可以识别附加 channel 是 alpha,但是其他一些需要读取这些 TIFF 的程序就没那么慷慨了。我是否缺少必须在 TIFFTAG_EXTRASAMPLES 之前设置的标签?我是否遗漏了其他需要存在的标签?任何帮助将不胜感激。

最佳答案

我找到了解决方案。 Extra_Samples 字段不是 uint16,而是首先是一个计数(它是 uint16),然后是该大小的数组(uint16 类型)。因此,调用应如下所示:

uint16 out[1];
out[0] = EXTRASAMPLE_ASSOCALPHA;

TIFFSetField( outImage, TIFFTAG_EXTRASAMPLES, 1, &out );

这样做的原因是允许多于一个额外的样本。

希望这对您有所帮助!

干杯, 卡斯帕

关于c++ - 尝试将 "ExtraSamples"标记应用于要写入的 TIFF 文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3451806/

相关文章:

python - 多页 tiff 调整 python 大小

c++ - 从函数 : by value VS by pointer 返回大对象

c++ - 使用 OCILIB 调用带有输出变量的 Oracle 过程

java - 尝试将文件移动到不同位置(在项目文件夹中)

python - 将 Matplotlib 图另存为 TIFF

c - 我怎样才能简单地在 libtiff 中加载灰度 tiff 并获得像素强度数组?

c++ - CMake 无法检测 gcc 内置函数,如 sqrt、pow、exp 等

architecture - 新闻源架构

c - C 中的文件处理和图形 ADT

c++ - 当一个单独的应用程序写入日志文件时从日志文件中读取数据