added 16-bit support to TiffEncoder (restore after #4919)

This commit is contained in:
Vladislav Vinogradov
2011-04-25 10:46:06 +00:00
parent 3b9138c4a0
commit dad986a6cb
4 changed files with 53 additions and 9 deletions

View File

@@ -306,6 +306,11 @@ ImageEncoder TiffEncoder::newEncoder() const
return new TiffEncoder;
}
bool TiffEncoder::isFormatSupported( int depth ) const
{
return depth == CV_8U || depth == CV_16U;
}
void TiffEncoder::writeTag( WLByteStream& strm, TiffTag tag,
TiffFieldType fieldType,
int count, int value )
@@ -321,7 +326,14 @@ bool TiffEncoder::write( const Mat& img, const vector<int>& )
{
int channels = img.channels();
int width = img.cols, height = img.rows;
int fileStep = width*channels;
int depth = img.depth();
if (depth != CV_8U && depth != CV_16U)
return false;
int bytesPerChannel = depth == CV_8U ? 1 : 2;
int fileStep = width * channels * bytesPerChannel;
WLByteStream strm;
if( m_buf )
@@ -356,7 +368,7 @@ bool TiffEncoder::write( const Mat& img, const vector<int>& )
uchar* buffer = _buffer;
int stripOffsetsOffset = 0;
int stripCountsOffset = 0;
int bitsPerSample = 8; // TODO support 16 bit
int bitsPerSample = 8 * bytesPerChannel;
int y = 0;
strm.putBytes( fmtSignTiffII, 4 );
@@ -376,9 +388,15 @@ bool TiffEncoder::write( const Mat& img, const vector<int>& )
for( ; y < limit; y++ )
{
if( channels == 3 )
icvCvt_BGR2RGB_8u_C3R( img.data + img.step*y, 0, buffer, 0, cvSize(width,1) );
if (depth == CV_8U)
icvCvt_BGR2RGB_8u_C3R( img.data + img.step*y, 0, buffer, 0, cvSize(width,1) );
else
icvCvt_BGR2RGB_16u_C3R( (const ushort*)(img.data + img.step*y), 0, (ushort*)buffer, 0, cvSize(width,1) );
else if( channels == 4 )
icvCvt_BGRA2RGBA_8u_C4R( img.data + img.step*y, 0, buffer, 0, cvSize(width,1) );
if (depth == CV_8U)
icvCvt_BGRA2RGBA_8u_C4R( img.data + img.step*y, 0, buffer, 0, cvSize(width,1) );
else
icvCvt_BGRA2RGBA_16u_C4R( (const ushort*)(img.data + img.step*y), 0, (ushort*)buffer, 0, cvSize(width,1) );
strm.putBytes( channels > 1 ? buffer : img.data + img.step*y, fileStep );
}
@@ -416,12 +434,13 @@ bool TiffEncoder::write( const Mat& img, const vector<int>& )
if( channels > 1 )
{
bitsPerSample = strm.getPos();
strm.putWord(8);
strm.putWord(8);
strm.putWord(8);
int bitsPerSamplePos = strm.getPos();
strm.putWord(bitsPerSample);
strm.putWord(bitsPerSample);
strm.putWord(bitsPerSample);
if( channels == 4 )
strm.putWord(8);
strm.putWord(bitsPerSample);
bitsPerSample = bitsPerSamplePos;
}
directoryOffset = strm.getPos();