--- /dev/null
+/*M///////////////////////////////////////////////////////////////////////////////////////\r
+//\r
+// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
+//\r
+// By downloading, copying, installing or using the software you agree to this license.\r
+// If you do not agree to this license, do not download, install,\r
+// copy or use the software.\r
+//\r
+//\r
+// License Agreement\r
+// For Open Source Computer Vision Library\r
+//\r
+// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
+// Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
+// Third party copyrights are property of their respective owners.\r
+//\r
+// Redistribution and use in source and binary forms, with or without modification,\r
+// are permitted provided that the following conditions are met:\r
+//\r
+// * Redistribution's of source code must retain the above copyright notice,\r
+// this list of conditions and the following disclaimer.\r
+//\r
+// * Redistribution's in binary form must reproduce the above copyright notice,\r
+// this list of conditions and the following disclaimer in the documentation\r
+// and/or other materials provided with the distribution.\r
+//\r
+// * The name of the copyright holders may not be used to endorse or promote products\r
+// derived from this software without specific prior written permission.\r
+//\r
+// This software is provided by the copyright holders and contributors "as is" and\r
+// any express or implied warranties, including, but not limited to, the implied\r
+// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
+// In no event shall the Intel Corporation or contributors be liable for any direct,\r
+// indirect, incidental, special, exemplary, or consequential damages\r
+// (including, but not limited to, procurement of substitute goods or services;\r
+// loss of use, data, or profits; or business interruption) however caused\r
+// and on any theory of liability, whether in contract, strict liability,\r
+// or tort (including negligence or otherwise) arising in any way out of\r
+// the use of this software, even if advised of the possibility of such damage.\r
+//\r
+//M*/\r
+\r
+/****************************************************************************************\\r
+ A part of the file implements TIFF reader on base of libtiff library\r
+ (see otherlibs/_graphics/readme.txt for copyright notice)\r
+\****************************************************************************************/\r
+\r
+#include "_highgui.h"\r
+#include "grfmt_tiff.h"\r
+\r
+namespace cv\r
+{\r
+static const char fmtSignTiffII[] = "II\x2a\x00";\r
+static const char fmtSignTiffMM[] = "MM\x00\x2a";\r
+\r
+#ifdef HAVE_TIFF\r
+\r
+#include "tiff.h"\r
+#include "tiffio.h"\r
+\r
+static int grfmt_tiff_err_handler_init = 0;\r
+static void GrFmtSilentTIFFErrorHandler( const char*, const char*, va_list ) {}\r
+\r
+TiffDecoder::TiffDecoder()\r
+{\r
+ m_tif = 0;\r
+ if( !grfmt_tiff_err_handler_init )\r
+ {\r
+ grfmt_tiff_err_handler_init = 1;\r
+\r
+ TIFFSetErrorHandler( GrFmtSilentTIFFErrorHandler );\r
+ TIFFSetWarningHandler( GrFmtSilentTIFFErrorHandler );\r
+ }\r
+}\r
+\r
+\r
+void TiffDecoder::close()\r
+{\r
+ if( m_tif )\r
+ {\r
+ TIFF* tif = (TIFF*)m_tif;\r
+ TIFFClose( tif );\r
+ m_tif = 0;\r
+ }\r
+}\r
+\r
+TiffDecoder::~TiffDecoder()\r
+{\r
+ close();\r
+}\r
+\r
+size_t TiffDecoder::signatureLength() const\r
+{\r
+ return 4;\r
+}\r
+\r
+bool TiffDecoder::checkSignature( const string& signature ) const\r
+{\r
+ return signature.size() >= 4 &&\r
+ (memcmp(signature.c_str(), fmtSignTiffII, 4) == 0 ||\r
+ memcmp(signature.c_str(), fmtSignTiffMM, 4) == 0);\r
+}\r
+\r
+ImageDecoder TiffDecoder::newDecoder() const\r
+{\r
+ return new TiffDecoder;\r
+}\r
+\r
+bool TiffDecoder::readHeader()\r
+{\r
+ char errmsg[1024];\r
+ bool result = false;\r
+\r
+ close();\r
+ TIFF* tif = TIFFOpen( m_filename.c_str(), "rb" );\r
+\r
+ if( tif )\r
+ {\r
+ int width = 0, height = 0, photometric = 0, compression = 0;\r
+ m_tif = tif;\r
+\r
+ if( TIFFRGBAImageOK( tif, errmsg ) &&\r
+ TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &width ) &&\r
+ TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &height ) &&\r
+ TIFFGetField( tif, TIFFTAG_PHOTOMETRIC, &photometric ) &&\r
+ (!TIFFGetField( tif, TIFFTAG_COMPRESSION, &compression ) ||\r
+ (compression != COMPRESSION_LZW &&\r
+ compression != COMPRESSION_OJPEG)))\r
+ {\r
+ m_width = width;\r
+ m_height = height;\r
+ m_type = photometric > 1 ? CV_8UC3 : CV_8UC1;\r
+\r
+ result = true;\r
+ }\r
+ }\r
+\r
+ if( !result )\r
+ close();\r
+\r
+ return result;\r
+}\r
+\r
+\r
+bool TiffDecoder::readData( Mat& img )\r
+{\r
+ bool result = false;\r
+ bool color = img.channels() > 1;\r
+ uchar* data = img.data;\r
+ int step = img.step;\r
+\r
+ if( m_tif && m_width && m_height )\r
+ {\r
+ TIFF* tif = (TIFF*)m_tif;\r
+ int tile_width0 = m_width, tile_height0 = 0;\r
+ int x, y, i;\r
+ int is_tiled = TIFFIsTiled(tif);\r
+\r
+ if( (!is_tiled &&\r
+ TIFFGetField( tif, TIFFTAG_ROWSPERSTRIP, &tile_height0 )) ||\r
+ (is_tiled &&\r
+ TIFFGetField( tif, TIFFTAG_TILEWIDTH, &tile_width0 ) &&\r
+ TIFFGetField( tif, TIFFTAG_TILELENGTH, &tile_height0 )))\r
+ {\r
+ if( tile_width0 <= 0 )\r
+ tile_width0 = m_width;\r
+\r
+ if( tile_height0 <= 0 )\r
+ tile_height0 = m_height;\r
+\r
+ AutoBuffer<uchar> _buffer(tile_height0*tile_width0*4);\r
+ uchar* buffer = _buffer;\r
+\r
+ for( y = 0; y < m_height; y += tile_height0, data += step*tile_height0 )\r
+ {\r
+ int tile_height = tile_height0;\r
+\r
+ if( y + tile_height > m_height )\r
+ tile_height = m_height - y;\r
+\r
+ for( x = 0; x < m_width; x += tile_width0 )\r
+ {\r
+ int tile_width = tile_width0, ok;\r
+\r
+ if( x + tile_width > m_width )\r
+ tile_width = m_width - x;\r
+\r
+ if( !is_tiled )\r
+ ok = TIFFReadRGBAStrip( tif, y, (uint32*)buffer );\r
+ else\r
+ ok = TIFFReadRGBATile( tif, x, y, (uint32*)buffer );\r
+\r
+ if( !ok )\r
+ {\r
+ close();\r
+ return false;\r
+ }\r
+\r
+ for( i = 0; i < tile_height; i++ )\r
+ if( color )\r
+ icvCvt_BGRA2BGR_8u_C4C3R( buffer + i*tile_width*4, 0,\r
+ data + x*3 + step*(tile_height - i - 1), 0,\r
+ cvSize(tile_width,1), 2 );\r
+ else\r
+ icvCvt_BGRA2Gray_8u_C4C1R( buffer + i*tile_width*4, 0,\r
+ data + x + step*(tile_height - i - 1), 0,\r
+ cvSize(tile_width,1), 2 );\r
+ }\r
+ }\r
+\r
+ result = true;\r
+ }\r
+ }\r
+\r
+ close();\r
+ return result;\r
+}\r
+\r
+#endif\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////////\r
+\r
+TiffEncoder::TiffEncoder()\r
+{\r
+ m_description = "TIFF Files (*.tiff;*.tif)";\r
+ m_buf_supported = true;\r
+}\r
+\r
+TiffEncoder::~TiffEncoder()\r
+{\r
+}\r
+\r
+ImageEncoder TiffEncoder::newEncoder() const\r
+{\r
+ return new TiffEncoder;\r
+}\r
+\r
+void TiffEncoder::writeTag( WLByteStream& strm, TiffTag tag,\r
+ TiffFieldType fieldType,\r
+ int count, int value )\r
+{\r
+ strm.putWord( tag );\r
+ strm.putWord( fieldType );\r
+ strm.putDWord( count );\r
+ strm.putDWord( value );\r
+}\r
+\r
+\r
+bool TiffEncoder::write( const Mat& img, const vector<int>& )\r
+{\r
+ int channels = img.channels();\r
+ int width = img.cols, height = img.rows;\r
+ int fileStep = width*channels;\r
+ WLByteStream strm;\r
+\r
+ if( m_buf )\r
+ {\r
+ if( !strm.open(*m_buf) )\r
+ return false;\r
+ }\r
+ else if( !strm.open(m_filename) )\r
+ return false;\r
+\r
+ int rowsPerStrip = (1 << 13)/fileStep;\r
+\r
+ if( rowsPerStrip < 1 )\r
+ rowsPerStrip = 1;\r
+\r
+ if( rowsPerStrip > height )\r
+ rowsPerStrip = height;\r
+\r
+ int i, stripCount = (height + rowsPerStrip - 1) / rowsPerStrip;\r
+\r
+ if( m_buf )\r
+ m_buf->reserve( alignSize(stripCount*8 + fileStep*height + 256, 256) );\r
+\r
+/*#if defined _DEBUG || !defined WIN32\r
+ int uncompressedRowSize = rowsPerStrip * fileStep;\r
+#endif*/\r
+ int directoryOffset = 0;\r
+\r
+ AutoBuffer<int,1024> stripOffsets(stripCount);\r
+ AutoBuffer<short,1024> stripCounts(stripCount);\r
+ AutoBuffer<uchar,1024> _buffer(fileStep+32);\r
+ uchar* buffer = _buffer;\r
+ int stripOffsetsOffset = 0;\r
+ int stripCountsOffset = 0;\r
+ int bitsPerSample = 8; // TODO support 16 bit\r
+ int y = 0;\r
+\r
+ strm.putBytes( fmtSignTiffII, 4 );\r
+ strm.putDWord( directoryOffset );\r
+\r
+ // write an image data first (the most reasonable way\r
+ // for compressed images)\r
+ for( i = 0; i < stripCount; i++ )\r
+ {\r
+ int limit = y + rowsPerStrip;\r
+\r
+ if( limit > height )\r
+ limit = height;\r
+\r
+ stripOffsets[i] = strm.getPos();\r
+\r
+ for( ; y < limit; y++ )\r
+ {\r
+ if( channels == 3 )\r
+ icvCvt_BGR2RGB_8u_C3R( img.data + img.step*y, 0, buffer, 0, cvSize(width,1) );\r
+ else if( channels == 4 )\r
+ icvCvt_BGRA2RGBA_8u_C4R( img.data + img.step*y, 0, buffer, 0, cvSize(width,1) );\r
+\r
+ strm.putBytes( channels > 1 ? buffer : img.data + img.step*y, fileStep );\r
+ }\r
+\r
+ stripCounts[i] = (short)(strm.getPos() - stripOffsets[i]);\r
+ /*assert( stripCounts[i] == uncompressedRowSize ||\r
+ stripCounts[i] < uncompressedRowSize &&\r
+ i == stripCount - 1);*/\r
+ }\r
+\r
+ if( stripCount > 2 )\r
+ {\r
+ stripOffsetsOffset = strm.getPos();\r
+ for( i = 0; i < stripCount; i++ )\r
+ strm.putDWord( stripOffsets[i] );\r
+\r
+ stripCountsOffset = strm.getPos();\r
+ for( i = 0; i < stripCount; i++ )\r
+ strm.putWord( stripCounts[i] );\r
+ }\r
+ else if(stripCount == 2)\r
+ {\r
+ stripOffsetsOffset = strm.getPos();\r
+ for (i = 0; i < stripCount; i++)\r
+ {\r
+ strm.putDWord (stripOffsets [i]);\r
+ }\r
+ stripCountsOffset = stripCounts [0] + (stripCounts [1] << 16);\r
+ }\r
+ else\r
+ {\r
+ stripOffsetsOffset = stripOffsets[0];\r
+ stripCountsOffset = stripCounts[0];\r
+ }\r
+\r
+ if( channels > 1 )\r
+ {\r
+ bitsPerSample = strm.getPos();\r
+ strm.putWord(8);\r
+ strm.putWord(8);\r
+ strm.putWord(8);\r
+ if( channels == 4 )\r
+ strm.putWord(8);\r
+ }\r
+\r
+ directoryOffset = strm.getPos();\r
+\r
+ // write header\r
+ strm.putWord( 9 );\r
+\r
+ /* warning: specification 5.0 of Tiff want to have tags in\r
+ ascending order. This is a non-fatal error, but this cause\r
+ warning with some tools. So, keep this in ascending order */\r
+\r
+ writeTag( strm, TIFF_TAG_WIDTH, TIFF_TYPE_LONG, 1, width );\r
+ writeTag( strm, TIFF_TAG_HEIGHT, TIFF_TYPE_LONG, 1, height );\r
+ writeTag( strm, TIFF_TAG_BITS_PER_SAMPLE,\r
+ TIFF_TYPE_SHORT, channels, bitsPerSample );\r
+ writeTag( strm, TIFF_TAG_COMPRESSION, TIFF_TYPE_LONG, 1, TIFF_UNCOMP );\r
+ writeTag( strm, TIFF_TAG_PHOTOMETRIC, TIFF_TYPE_SHORT, 1, channels > 1 ? 2 : 1 );\r
+\r
+ writeTag( strm, TIFF_TAG_STRIP_OFFSETS, TIFF_TYPE_LONG,\r
+ stripCount, stripOffsetsOffset );\r
+\r
+ writeTag( strm, TIFF_TAG_SAMPLES_PER_PIXEL, TIFF_TYPE_SHORT, 1, channels );\r
+ writeTag( strm, TIFF_TAG_ROWS_PER_STRIP, TIFF_TYPE_LONG, 1, rowsPerStrip );\r
+\r
+ writeTag( strm, TIFF_TAG_STRIP_COUNTS,\r
+ stripCount > 1 ? TIFF_TYPE_SHORT : TIFF_TYPE_LONG,\r
+ stripCount, stripCountsOffset );\r
+\r
+ strm.putDWord(0);\r
+ strm.close();\r
+\r
+ if( m_buf )\r
+ {\r
+ (*m_buf)[4] = (uchar)directoryOffset;\r
+ (*m_buf)[5] = (uchar)(directoryOffset >> 8);\r
+ (*m_buf)[6] = (uchar)(directoryOffset >> 16);\r
+ (*m_buf)[7] = (uchar)(directoryOffset >> 24);\r
+ }\r
+ else\r
+ {\r
+ // write directory offset\r
+ FILE* f = fopen( m_filename.c_str(), "r+b" );\r
+ buffer[0] = (uchar)directoryOffset;\r
+ buffer[1] = (uchar)(directoryOffset >> 8);\r
+ buffer[2] = (uchar)(directoryOffset >> 16);\r
+ buffer[3] = (uchar)(directoryOffset >> 24);\r
+\r
+ fseek( f, 4, SEEK_SET );\r
+ fwrite( buffer, 1, 4, f );\r
+ fclose(f);\r
+ }\r
+\r
+ return true;\r
+}\r
+\r
+}\r