2010-08-10 6 views
2

이미지를 가져 와서 TIFF 파일에 쓰는 프로그램이 있습니다. 이미지는 그레이 스케일 (8 비트), 알파 채널 (16 비트), RGB (24 비트) 또는 ARGB (32 비트)의 그레이 스케일 일 수 있습니다. 알파 채널없이 이미지를 쓰는 데 아무런 문제가 없지만 알파가있는 이미지의 경우 추가 샘플 태그를 설정하려고하면 TIFFS 오류 처리 루틴에 TIFFSetErrorHandler가 설정되어 전송됩니다. 전달 된 메시지는 _TIFFVSetField 모듈에 <filename>: Bad value 1 for "ExtraSamples"입니다. 아래 샘플 코드 중 일부는 다음과 같습니다."ExtraSamples"태그를 쓸 TIFF 파일에 적용하려고 할 때 오류가 발생합니다.

#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); 
} 

필자가 볼 수있는 한 태그는 파일에 기록되지 않습니다. 운좋게도 김프는 추가 채널이 알파임을 인식하지만이 TIFF를 읽어야하는 다른 프로그램은 그렇게 관대하지 않습니다. TIFFTAG_EXTRASAMPLES 전에 설정해야하는 태그가 누락 되었습니까? 거기에 있어야하는 다른 태그가 누락 되었습니까? 어떤 도움을 주시면 감사하겠습니다.

답변

4

해결책을 찾았습니다. Extra_Samples 필드는 uint16이 아니라 처음에는 count (uint16)이고 그 다음 uint16 유형의 배열입니다. 이 호출은 따라서 다음과 같아야합니다

그 이유는 하나 개 이상의 추가 샘플이 허용되는 것입니다
uint16 out[1]; 
out[0] = EXTRASAMPLE_ASSOCALPHA; 

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

.

희망이 도움이됩니다.

건배, 카스파

+0

신난다는 sooooo를 많이 감사합니다. – MBraedley

+0

누군가가 여기에 약간의 참조를 제공했는지는 알 수 없습니다. 이 경우 소스 다이빙은 극적입니다. 한 함수에서 다른 함수로 va_args 목록을 전달한 다음 잘못된 포인터를 segfault합니다. 배열은 더 간결하게 작성할 수 있습니다 :'short extras [] = {EXTRASAMPLE_ASSOCALPHA};'그리고 마지막 인수로'extras'를 전달하십시오. –

+0

100 % 확실하지는 않지만 "uint16, uint16 *"및 "count & types array"[http://www.microsoft.com/libtiff.org/man/TIFFSetField.3t.html]이 나와 있습니다. –

관련 문제