Added minimal support for tiff encoder parameters and test for issue #2161
authorAndrey Kamaev <andrey.kamaev@itseez.com>
Tue, 29 Jan 2013 16:13:09 +0000 (20:13 +0400)
committerAndrey Kamaev <andrey.kamaev@itseez.com>
Tue, 29 Jan 2013 20:07:30 +0000 (00:07 +0400)
modules/highgui/src/grfmt_tiff.cpp
modules/highgui/test/test_grfmt.cpp

index d2321ce..07fe644 100644 (file)
@@ -402,7 +402,18 @@ void  TiffEncoder::writeTag( WLByteStream& strm, TiffTag tag,
 }
 
 #ifdef HAVE_TIFF
-bool  TiffEncoder::writeLibTiff( const Mat& img, const vector<int>& /*params*/)
+
+static void readParam(const vector<int>& params, int key, int& value)
+{
+    for(size_t i = 0; i + 1 < params.size(); i += 2)
+        if(params[i] == key)
+        {
+            value = params[i+1];
+            break;
+        }
+}
+
+bool  TiffEncoder::writeLibTiff( const Mat& img, const vector<int>& params)
 {
     int channels = img.channels();
     int width = img.cols, height = img.rows;
@@ -429,7 +440,9 @@ bool  TiffEncoder::writeLibTiff( const Mat& img, const vector<int>& /*params*/)
 
     const int bitsPerByte = 8;
     size_t fileStep = (width * channels * bitsPerChannel) / bitsPerByte;
+
     int rowsPerStrip = (int)((1 << 13)/fileStep);
+    readParam(params, TIFFTAG_ROWSPERSTRIP, rowsPerStrip);
 
     if( rowsPerStrip < 1 )
         rowsPerStrip = 1;
@@ -450,6 +463,9 @@ bool  TiffEncoder::writeLibTiff( const Mat& img, const vector<int>& /*params*/)
     int   compression  = COMPRESSION_LZW;
     int   predictor    = PREDICTOR_HORIZONTAL;
 
+    readParam(params, TIFFTAG_COMPRESSION, compression);
+    readParam(params, TIFFTAG_PREDICTOR, predictor);
+
     int   colorspace = channels > 1 ? PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK;
 
     if ( !TIFFSetField(pTiffHandle, TIFFTAG_IMAGEWIDTH, width)
index 7226ebf..28d30c9 100644 (file)
@@ -291,3 +291,22 @@ TEST(Highgui_Jpeg, encode_empty)
     ASSERT_THROW(cv::imencode(".jpg", img, jpegImg), cv::Exception);
 }
 #endif
+
+
+#ifdef HAVE_TIFF
+#include "tiff.h"
+TEST(Highgui_Tiff, decode_tile16384x16384)
+{
+    // see issue #2161
+    cv::Mat big(16384, 16384, CV_8UC1, cv::Scalar::all(0));
+    string file = cv::tempfile(".tiff");
+    std::vector<int> params;
+    params.push_back(TIFFTAG_ROWSPERSTRIP);
+    params.push_back(big.rows);
+    cv::imwrite(file, big, params);
+    big.release();
+
+    EXPECT_NO_THROW(cv::imread(file));
+    remove(file.c_str());
+}
+#endif