<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0cm;
        margin-right:0cm;
        margin-bottom:0cm;
        margin-left:36.0pt;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
span.EmailStyle18
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:42.5pt 42.5pt 42.5pt 70.85pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="UK" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">Hello everyone, <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">It seems that I found bug in RGBA tiff image handling.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">I’m trying to load RGBA tiff image and save it using following code:
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"> typedef itk::Image<uint16_t> ImageType;<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> auto reader = itk::ImageFileReader<ImageType>::New();<o:p></o:p></p>
<p class="MsoNormal"> reader->SetFileName("D://tmp//<span lang="EN-US">some</span>.tif");<o:p></o:p></p>
<p class="MsoNormal"> reader->SetDebug(true);<o:p></o:p></p>
<p class="MsoNormal"> reader->Update();<o:p></o:p></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"> auto writer = itk::ImageFileWriter<ImageType>::New();<o:p></o:p></p>
<p class="MsoNormal"> writer->SetFileName("D://tmp//tifcheck.tif");<o:p></o:p></p>
<p class="MsoNormal"> writer->SetInput(reader->GetOutput());<o:p></o:p></p>
<p class="MsoNormal"> writer->Update();<span lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">but resulting tifcheck.tif image is just black rectangle.
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">During bug investigation I’ve found that itkTiffIO handles alpha value in a strange way:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">1) itkTIFFImageIO.cxx: int TIFFImageIO::EvaluateImageAt(void *out, void *in)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> …<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> case TIFFImageIO::RGB_:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> ….<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> red = *( source );<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> green = *( source + 1 );<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> blue = *( source + 2 );<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> *( image ) = red;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> *( image + 1 ) = green;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> *( image + 2 ) = blue;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> if ( m_InternalImage->m_SamplesPerPixel == 4 )<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> {<o:p></o:p></span></p>
<p class="MsoNormal"><b><span lang="EN-US"> alpha = *( source + 3 ); // 255 in my example<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US"> *( image + 3 ) = 255 - alpha; // 0<o:p></o:p></span></b></p>
<p class="MsoNormal"><span lang="EN-US"> }<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">2) After reading it ImageReader converts rgba values to CIE luminance(itkConvertPixelBuffer.txx):<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">::ConvertRGBAToGray(InputPixelType* inputData,
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> OutputPixelType* outputData , size_t size)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">….<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">// this is an ugly implementation of the simple equation<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> // greval = (.2125 * red + .7154 * green + .0721 * blue) / alpha<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> //<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> double tempval =<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> ((2125.0 * static_cast< double >( * inputData)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> + 7154.0 * static_cast< double >( *( inputData + 1 ))<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> + 0721.0 * static_cast< double >( *( inputData + 2 ))) / 10000.0)<o:p></o:p></span></p>
<p class="MsoNormal"><b><span lang="EN-US"> * static_cast< double >(*( inputData + 3)) /<span style="color:#1F497D">/</span> 0!!<o:p></o:p></span></b></p>
<p class="MsoNormal"><span lang="EN-US"> / maxAlpha;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> inputData += 4;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> OutputComponentType val = static_cast< OutputComponentType >( tempval );<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> OutputConvertTraits::SetNthComponent(0, *outputData++, val);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">So, as the result, I get fully black picture.
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">After patching TiffIO class, by changing <b>
255 – alpha </b>to <b>alpha; </b> code example listed above works fine for me. <o:p>
</o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">So here is my question: is it really bug, or I’m doing something wrong?<o:p></o:p></span></p>
</div>
</body>
</html>