test_grfmt.cpp 35.6 KB
Newer Older
A
Andrey Kamaev 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "test_precomp.hpp"

45
#include <fstream>
46
#include <sstream>
47
#include <iostream>
48

A
Andrey Kamaev 已提交
49 50 51
using namespace cv;
using namespace std;

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
static
bool mats_equal(const Mat& lhs, const Mat& rhs)
{
    if (lhs.channels() != rhs.channels() ||
        lhs.depth() != rhs.depth() ||
        lhs.size().height != rhs.size().height ||
        lhs.size().width != rhs.size().width)
    {
        return false;
    }

    Mat diff = (lhs != rhs);
    const Scalar s = sum(diff);
    for (int i = 0; i < s.channels; ++i)
    {
        if (s[i] != 0)
        {
            return false;
        }
    }

    return true;
}

static
bool imread_compare(const string& filepath, int flags = IMREAD_COLOR)
{
    vector<Mat> pages;
    if (!imreadmulti(filepath, pages, flags) ||
        pages.empty())
    {
        return false;
    }

    const Mat single = imread(filepath, flags);
    return mats_equal(single, pages[0]);
}

TEST(Imgcodecs_imread, regression)
{
    const char* const filenames[] =
    {
94 95
#ifdef HAVE_JASPER
        "Rome.jp2",
M
mrquorr 已提交
96 97 98 99 100 101
#endif
#ifdef HAVE_GDCM
        "int16-mono1.dcm",
        "uint8-mono2.dcm",
        "uint16-mono2.dcm",
        "uint8-rgb.dcm",
102
#endif
103 104 105 106 107 108 109 110 111 112 113 114
        "color_palette_alpha.png",
        "multipage.tif",
        "rle.hdr",
        "ordinary.bmp",
        "rle8.bmp",
        "test_1_c1.jpg"
    };

    const string folder = string(cvtest::TS::ptr()->get_data_path()) + "/readwrite/";

    for (size_t i = 0; i < sizeof(filenames) / sizeof(filenames[0]); ++i)
    {
115 116 117 118 119 120
        const string path = folder + string(filenames[i]);
        ASSERT_TRUE(imread_compare(path, IMREAD_UNCHANGED));
        ASSERT_TRUE(imread_compare(path, IMREAD_GRAYSCALE));
        ASSERT_TRUE(imread_compare(path, IMREAD_COLOR));
        ASSERT_TRUE(imread_compare(path, IMREAD_ANYDEPTH));
        ASSERT_TRUE(imread_compare(path, IMREAD_ANYCOLOR));
M
mrquorr 已提交
121 122
        const string ext = path.substr( path.length() - 3 );
        if ( ext != "hdr" && ext != "dcm" )
123
        {
M
mrquorr 已提交
124
            // GDAL does not support hdr nor dcm
125 126
            ASSERT_TRUE(imread_compare(path, IMREAD_LOAD_GDAL));
        }
127 128
    }
}
A
Andrey Kamaev 已提交
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
template<class T>
string to_string(T i)
{
    stringstream ss;
    string s;
    ss << i;
    s = ss.str();

    return s;
}


/**
 * Test for check whether reading exif orientation tag was processed successfully or not
 * The test info is the set of 8 images named testExifRotate_{1 to 8}.jpg
 * The test image is the square 10x10 points divided by four sub-squares:
 * (R corresponds to Red, G to Green, B to Blue, W to white)
 * ---------             ---------
 * | R | G |             | G | R |
 * |-------| - (tag 1)   |-------| - (tag 2)
 * | B | W |             | W | B |
 * ---------             ---------
 *
 * ---------             ---------
 * | W | B |             | B | W |
 * |-------| - (tag 3)   |-------| - (tag 4)
 * | G | R |             | R | G |
 * ---------             ---------
 *
 * ---------             ---------
 * | R | B |             | G | W |
 * |-------| - (tag 5)   |-------| - (tag 6)
 * | G | W |             | R | B |
 * ---------             ---------
 *
 * ---------             ---------
 * | W | G |             | B | R |
 * |-------| - (tag 7)   |-------| - (tag 8)
 * | B | R |             | W | G |
 * ---------             ---------
 *
 *
 * Every image contains exif field with orientation tag (0x112)
 * After reading each image the corresponding matrix must be read as
 * ---------
 * | R | G |
 * |-------|
 * | B | W |
 * ---------
 *
 */
class CV_GrfmtJpegExifOrientationTest : public cvtest::BaseTest
{
public:
    void run(int)
    {
        try
        {
            for( int i = 1; i <= 8; ++i)
            {
                string fileName = "readwrite/testExifOrientation_" + to_string(i) + ".jpg";
                m_img = imread(string(ts->get_data_path()) + fileName);
                if( !m_img.data )
                {
                    ts->set_failed_test_info(cvtest::TS::FAIL_MISSING_TEST_DATA);
                }
                ts->printf(cvtest::TS::LOG, "start  reading image\t%s\n", fileName.c_str());
                if( !checkOrientation() )
                {
                    ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
                }
            }

        }
        catch(...)
        {
            ts->set_failed_test_info(cvtest::TS::FAIL_EXCEPTION);
        }
    }
private:
    bool checkOrientation();
    Mat m_img;
};


bool CV_GrfmtJpegExifOrientationTest::checkOrientation()
{
    Vec3b vec;
    int red = 0;
    int green = 0;
    int blue = 0;

    const int colorThresholdHigh = 250;
    const int colorThresholdLow = 5;

    //Checking the first quadrant (with supposed red)
    vec = m_img.at<Vec3b>(2, 2); //some point inside the square
    red   = vec.val[2];
    green = vec.val[1];
    blue  = vec.val[0];

    ts->printf(cvtest::TS::LOG, "RED QUADRANT:\n");
    ts->printf(cvtest::TS::LOG, "Red calculated:\t\t%d\n", red);
    ts->printf(cvtest::TS::LOG, "Green calculated:\t%d\n", green);
    ts->printf(cvtest::TS::LOG, "Blue calculated:\t%d\n", blue);
    if( red < colorThresholdHigh ) return false;
    if( blue > colorThresholdLow ) return false;
    if( green > colorThresholdLow ) return false;

    //Checking the second quadrant (with supposed green)
    vec = m_img.at<Vec3b>(2, 7);  //some point inside the square
    red   = vec.val[2];
    green = vec.val[1];
    blue  = vec.val[0];
    ts->printf(cvtest::TS::LOG, "GREEN QUADRANT:\n");
    ts->printf(cvtest::TS::LOG, "Red calculated:\t\t%d\n", red);
    ts->printf(cvtest::TS::LOG, "Green calculated:\t%d\n", green);
    ts->printf(cvtest::TS::LOG, "Blue calculated:\t%d\n", blue);
    if( green < colorThresholdHigh ) return false;
    if( red > colorThresholdLow ) return false;
    if( blue > colorThresholdLow ) return false;

    //Checking the third quadrant (with supposed blue)
    vec = m_img.at<Vec3b>(7, 2);  //some point inside the square
    red   = vec.val[2];
    green = vec.val[1];
    blue  = vec.val[0];
    ts->printf(cvtest::TS::LOG, "BLUE QUADRANT:\n");
    ts->printf(cvtest::TS::LOG, "Red calculated:\t\t%d\n", red);
    ts->printf(cvtest::TS::LOG, "Green calculated:\t%d\n", green);
    ts->printf(cvtest::TS::LOG, "Blue calculated:\t%d\n", blue);
    if( blue < colorThresholdHigh ) return false;
    if( red > colorThresholdLow ) return false;
    if( green > colorThresholdLow ) return false;

    return true;
}

TEST(Imgcodecs_jpeg_exif, setOrientation)
{
    CV_GrfmtJpegExifOrientationTest test;
    test.safe_run();
}

274 275 276 277 278
#ifdef HAVE_JASPER
TEST(Imgcodecs_jasper, regression)
{
    const string folder = string(cvtest::TS::ptr()->get_data_path()) + "/readwrite/";

279
    ASSERT_TRUE(imread_compare(folder + "Bretagne2.jp2", IMREAD_COLOR));
280
    ASSERT_TRUE(imread_compare(folder + "Bretagne2.jp2", IMREAD_GRAYSCALE));
281 282
    ASSERT_TRUE(imread_compare(folder + "Grey.jp2", IMREAD_COLOR));
    ASSERT_TRUE(imread_compare(folder + "Grey.jp2", IMREAD_GRAYSCALE));
283 284 285
}
#endif

A
Andrey Kamaev 已提交
286 287 288 289 290 291 292 293 294 295 296 297
class CV_GrfmtWriteBigImageTest : public cvtest::BaseTest
{
public:
    void run(int)
    {
        try
        {
            ts->printf(cvtest::TS::LOG, "start  reading big image\n");
            Mat img = imread(string(ts->get_data_path()) + "readwrite/read.png");
            ts->printf(cvtest::TS::LOG, "finish reading big image\n");
            if (img.empty()) ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
            ts->printf(cvtest::TS::LOG, "start  writing big image\n");
298
            imwrite(cv::tempfile(".png"), img);
A
Andrey Kamaev 已提交
299 300 301 302 303 304 305 306 307 308 309 310
            ts->printf(cvtest::TS::LOG, "finish writing big image\n");
        }
        catch(...)
        {
            ts->set_failed_test_info(cvtest::TS::FAIL_EXCEPTION);
        }
        ts->set_failed_test_info(cvtest::TS::OK);
    }
};

string ext_from_int(int ext)
{
311
#ifdef HAVE_PNG
A
Andrey Kamaev 已提交
312
    if (ext == 0) return ".png";
313
#endif
A
Andrey Kamaev 已提交
314 315
    if (ext == 1) return ".bmp";
    if (ext == 2) return ".pgm";
316
#ifdef HAVE_TIFF
A
Andrey Kamaev 已提交
317
    if (ext == 3) return ".tiff";
318
#endif
319
    if (ext == 4) return ".pam";
A
Andrey Kamaev 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    return "";
}

class CV_GrfmtWriteSequenceImageTest : public cvtest::BaseTest
{
public:
    void run(int)
    {
        try
        {
            const int img_r = 640;
            const int img_c = 480;

            for (int k = 1; k <= 5; ++k)
            {
335
                for (int ext = 0; ext < 5; ++ext) // 0 - png, 1 - bmp, 2 - pgm, 3 - tiff
336 337 338
                {
                    if(ext_from_int(ext).empty())
                        continue;
339
                    for (int num_channels = 1; num_channels <= 4; num_channels++)
A
Andrey Kamaev 已提交
340
                    {
K
kobigurk 已提交
341 342
                        if (num_channels == 2) continue;
                        if (num_channels == 4 && ext!=3 /*TIFF*/) continue;
343

A
Andrey Kamaev 已提交
344 345
                        ts->printf(ts->LOG, "image type depth:%d   channels:%d   ext: %s\n", CV_8U, num_channels, ext_from_int(ext).c_str());
                        Mat img(img_r * k, img_c * k, CV_MAKETYPE(CV_8U, num_channels), Scalar::all(0));
346
                        circle(img, Point2i((img_c * k) / 2, (img_r * k) / 2), std::min((img_r * k), (img_c * k)) / 4 , Scalar::all(255));
A
Andrey Kamaev 已提交
347

348 349 350 351 352
                        string img_path = cv::tempfile(ext_from_int(ext).c_str());
                        ts->printf(ts->LOG, "writing      image : %s\n", img_path.c_str());
                        imwrite(img_path, img);

                        ts->printf(ts->LOG, "reading test image : %s\n", img_path.c_str());
353
                        Mat img_test = imread(img_path, IMREAD_UNCHANGED);
A
Andrey Kamaev 已提交
354 355 356 357 358

                        if (img_test.empty()) ts->set_failed_test_info(ts->FAIL_MISMATCH);

                        CV_Assert(img.size() == img_test.size());
                        CV_Assert(img.type() == img_test.type());
359
                        CV_Assert(num_channels == img_test.channels());
A
Andrey Kamaev 已提交
360

361
                        double n = cvtest::norm(img, img_test, NORM_L2);
A
Andrey Kamaev 已提交
362 363 364 365 366 367
                        if ( n > 1.0)
                        {
                            ts->printf(ts->LOG, "norm = %f \n", n);
                            ts->set_failed_test_info(ts->FAIL_MISMATCH);
                        }
                    }
368
                }
A
Andrey Kamaev 已提交
369

370
#ifdef HAVE_JPEG
A
Andrey Kamaev 已提交
371 372 373 374 375
                for (int num_channels = 1; num_channels <= 3; num_channels+=2)
                {
                    // jpeg
                    ts->printf(ts->LOG, "image type depth:%d   channels:%d   ext: %s\n", CV_8U, num_channels, ".jpg");
                    Mat img(img_r * k, img_c * k, CV_MAKETYPE(CV_8U, num_channels), Scalar::all(0));
376
                    circle(img, Point2i((img_c * k) / 2, (img_r * k) / 2), std::min((img_r * k), (img_c * k)) / 4 , Scalar::all(255));
377 378

                    string filename = cv::tempfile(".jpg");
A
Andrey Kamaev 已提交
379 380
                    imwrite(filename, img);
                    ts->printf(ts->LOG, "reading test image : %s\n", filename.c_str());
381
                    Mat img_test = imread(filename, IMREAD_UNCHANGED);
A
Andrey Kamaev 已提交
382 383 384 385 386 387

                    if (img_test.empty()) ts->set_failed_test_info(ts->FAIL_MISMATCH);

                    CV_Assert(img.size() == img_test.size());
                    CV_Assert(img.type() == img_test.type());

M
Maksim Shabunin 已提交
388 389 390 391 392
                    // JPEG format does not provide 100% accuracy
                    // using fuzzy image comparison
                    double n = cvtest::norm(img, img_test, NORM_L1);
                    double expected = 0.05 * img.size().area();
                    if ( n > expected)
A
Andrey Kamaev 已提交
393
                    {
M
Maksim Shabunin 已提交
394
                        ts->printf(ts->LOG, "norm = %f > expected = %f \n", n, expected);
A
Andrey Kamaev 已提交
395 396 397
                        ts->set_failed_test_info(ts->FAIL_MISMATCH);
                    }
                }
398
#endif
A
Andrey Kamaev 已提交
399

400
#ifdef HAVE_TIFF
401
                for (int num_channels = 1; num_channels <= 4; num_channels++)
A
Andrey Kamaev 已提交
402
                {
403
                    if (num_channels == 2) continue;
A
Andrey Kamaev 已提交
404 405 406
                    // tiff
                    ts->printf(ts->LOG, "image type depth:%d   channels:%d   ext: %s\n", CV_16U, num_channels, ".tiff");
                    Mat img(img_r * k, img_c * k, CV_MAKETYPE(CV_16U, num_channels), Scalar::all(0));
407
                    circle(img, Point2i((img_c * k) / 2, (img_r * k) / 2), std::min((img_r * k), (img_c * k)) / 4 , Scalar::all(255));
408 409

                    string filename = cv::tempfile(".tiff");
A
Andrey Kamaev 已提交
410 411
                    imwrite(filename, img);
                    ts->printf(ts->LOG, "reading test image : %s\n", filename.c_str());
412
                    Mat img_test = imread(filename, IMREAD_UNCHANGED);
A
Andrey Kamaev 已提交
413 414 415 416 417 418 419 420 421 422 423

                    if (img_test.empty()) ts->set_failed_test_info(ts->FAIL_MISMATCH);

                    CV_Assert(img.size() == img_test.size());

                    ts->printf(ts->LOG, "img      : %d ; %d \n", img.channels(), img.depth());
                    ts->printf(ts->LOG, "img_test : %d ; %d \n", img_test.channels(), img_test.depth());

                    CV_Assert(img.type() == img_test.type());


424
                    double n = cvtest::norm(img, img_test, NORM_L2);
A
Andrey Kamaev 已提交
425 426 427 428 429 430
                    if ( n > 1.0)
                    {
                        ts->printf(ts->LOG, "norm = %f \n", n);
                        ts->set_failed_test_info(ts->FAIL_MISMATCH);
                    }
                }
431
#endif
A
Andrey Kamaev 已提交
432 433 434 435 436 437 438 439 440 441
            }
        }
        catch(const cv::Exception & e)
        {
            ts->printf(ts->LOG, "Exception: %s\n" , e.what());
            ts->set_failed_test_info(ts->FAIL_MISMATCH);
        }
    }
};

A
Alexander Shishkov 已提交
442 443 444 445 446 447 448 449 450
class CV_GrfmtReadBMPRLE8Test : public cvtest::BaseTest
{
public:
    void run(int)
    {
        try
        {
            Mat rle = imread(string(ts->get_data_path()) + "readwrite/rle8.bmp");
            Mat bmp = imread(string(ts->get_data_path()) + "readwrite/ordinary.bmp");
451
            if (cvtest::norm(rle-bmp, NORM_L2)>1.e-10)
A
Alexander Shishkov 已提交
452 453 454 455 456 457 458 459 460 461
                ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
        }
        catch(...)
        {
            ts->set_failed_test_info(cvtest::TS::FAIL_EXCEPTION);
        }
        ts->set_failed_test_info(cvtest::TS::OK);
    }
};

462

V
Vadim Pisarevsky 已提交
463
#ifdef HAVE_PNG
464
TEST(Imgcodecs_Image, write_big) { CV_GrfmtWriteBigImageTest test; test.safe_run(); }
V
Vadim Pisarevsky 已提交
465 466
#endif

467
TEST(Imgcodecs_Image, write_imageseq) { CV_GrfmtWriteSequenceImageTest test; test.safe_run(); }
V
Vadim Pisarevsky 已提交
468

469
TEST(Imgcodecs_Image, read_bmp_rle8) { CV_GrfmtReadBMPRLE8Test test; test.safe_run(); }
A
Alexander Shishkov 已提交
470

471 472 473 474 475 476 477 478 479 480 481 482
#ifdef HAVE_PNG
class CV_GrfmtPNGEncodeTest : public cvtest::BaseTest
{
public:
    void run(int)
    {
        try
        {
            vector<uchar> buff;
            Mat im = Mat::zeros(1000,1000, CV_8U);
            //randu(im, 0, 256);
            vector<int> param;
483
            param.push_back(IMWRITE_PNG_COMPRESSION);
484 485 486 487
            param.push_back(3); //default(3) 0-9.
            cv::imencode(".png" ,im ,buff, param);

            // hangs
488
            Mat im2 = imdecode(buff,IMREAD_ANYDEPTH);
489 490 491 492 493 494 495 496 497
        }
        catch(...)
        {
            ts->set_failed_test_info(cvtest::TS::FAIL_EXCEPTION);
        }
        ts->set_failed_test_info(cvtest::TS::OK);
    }
};

498
TEST(Imgcodecs_Image, encode_png) { CV_GrfmtPNGEncodeTest test; test.safe_run(); }
499

500
TEST(Imgcodecs_ImreadVSCvtColor, regression)
501 502 503
{
    cvtest::TS& ts = *cvtest::TS::ptr();

V
Vadim Pisarevsky 已提交
504
    const int MAX_MEAN_DIFF = 1;
505 506
    const int MAX_ABS_DIFF = 10;

V
Vadim Pisarevsky 已提交
507
    string imgName = string(ts.get_data_path()) + "/../cv/shared/lena.png";
508 509 510 511 512 513 514 515 516 517 518 519
    Mat original_image = imread(imgName);
    Mat gray_by_codec = imread(imgName, 0);
    Mat gray_by_cvt;

    cvtColor(original_image, gray_by_cvt, CV_BGR2GRAY);

    Mat diff;
    absdiff(gray_by_codec, gray_by_cvt, diff);

    double actual_avg_diff = (double)mean(diff)[0];
    double actual_maxval, actual_minval;
    minMaxLoc(diff, &actual_minval, &actual_maxval);
520
    //printf("actual avg = %g, actual maxdiff = %g, npixels = %d\n", actual_avg_diff, actual_maxval, (int)diff.total());
521 522 523 524

    EXPECT_LT(actual_avg_diff, MAX_MEAN_DIFF);
    EXPECT_LT(actual_maxval, MAX_ABS_DIFF);
}
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

//Test OpenCV issue 3075 is solved
class CV_GrfmtReadPNGColorPaletteWithAlphaTest : public cvtest::BaseTest
{
public:
    void run(int)
    {
        try
        {
            // First Test : Read PNG with alpha, imread flag -1
            Mat img = imread(string(ts->get_data_path()) + "readwrite/color_palette_alpha.png",-1);
            if (img.empty()) ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);

            ASSERT_TRUE(img.channels() == 4);

540
            unsigned char* img_data = img.ptr();
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559

            // Verification first pixel is red in BGRA
            ASSERT_TRUE(img_data[0] == 0x00);
            ASSERT_TRUE(img_data[1] == 0x00);
            ASSERT_TRUE(img_data[2] == 0xFF);
            ASSERT_TRUE(img_data[3] == 0xFF);

            // Verification second pixel is red in BGRA
            ASSERT_TRUE(img_data[4] == 0x00);
            ASSERT_TRUE(img_data[5] == 0x00);
            ASSERT_TRUE(img_data[6] == 0xFF);
            ASSERT_TRUE(img_data[7] == 0xFF);

            // Second Test : Read PNG without alpha, imread flag -1
            img = imread(string(ts->get_data_path()) + "readwrite/color_palette_no_alpha.png",-1);
            if (img.empty()) ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);

            ASSERT_TRUE(img.channels() == 3);

560
            img_data = img.ptr();
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

            // Verification first pixel is red in BGR
            ASSERT_TRUE(img_data[0] == 0x00);
            ASSERT_TRUE(img_data[1] == 0x00);
            ASSERT_TRUE(img_data[2] == 0xFF);

            // Verification second pixel is red in BGR
            ASSERT_TRUE(img_data[3] == 0x00);
            ASSERT_TRUE(img_data[4] == 0x00);
            ASSERT_TRUE(img_data[5] == 0xFF);

            // Third Test : Read PNG with alpha, imread flag 1
            img = imread(string(ts->get_data_path()) + "readwrite/color_palette_alpha.png",1);
            if (img.empty()) ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);

            ASSERT_TRUE(img.channels() == 3);

578
            img_data = img.ptr();
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595

            // Verification first pixel is red in BGR
            ASSERT_TRUE(img_data[0] == 0x00);
            ASSERT_TRUE(img_data[1] == 0x00);
            ASSERT_TRUE(img_data[2] == 0xFF);

            // Verification second pixel is red in BGR
            ASSERT_TRUE(img_data[3] == 0x00);
            ASSERT_TRUE(img_data[4] == 0x00);
            ASSERT_TRUE(img_data[5] == 0xFF);

            // Fourth Test : Read PNG without alpha, imread flag 1
            img = imread(string(ts->get_data_path()) + "readwrite/color_palette_no_alpha.png",1);
            if (img.empty()) ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);

            ASSERT_TRUE(img.channels() == 3);

596
            img_data = img.ptr();
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

            // Verification first pixel is red in BGR
            ASSERT_TRUE(img_data[0] == 0x00);
            ASSERT_TRUE(img_data[1] == 0x00);
            ASSERT_TRUE(img_data[2] == 0xFF);

            // Verification second pixel is red in BGR
            ASSERT_TRUE(img_data[3] == 0x00);
            ASSERT_TRUE(img_data[4] == 0x00);
            ASSERT_TRUE(img_data[5] == 0xFF);
        }
        catch(...)
        {
            ts->set_failed_test_info(cvtest::TS::FAIL_EXCEPTION);
    }
        ts->set_failed_test_info(cvtest::TS::OK);
    }
};

616
TEST(Imgcodecs_Image, read_png_color_palette_with_alpha) { CV_GrfmtReadPNGColorPaletteWithAlphaTest test; test.safe_run(); }
617 618
#endif

619
#ifdef HAVE_JPEG
620
TEST(Imgcodecs_Jpeg, encode_empty)
621 622 623 624 625 626
{
    cv::Mat img;
    std::vector<uchar> jpegImg;

    ASSERT_THROW(cv::imencode(".jpg", img, jpegImg), cv::Exception);
}
N
Nicolas Gryman 已提交
627

628
TEST(Imgcodecs_Jpeg, encode_decode_progressive_jpeg)
N
Nicolas Gryman 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
{
    cvtest::TS& ts = *cvtest::TS::ptr();
    string input = string(ts.get_data_path()) + "../cv/shared/lena.png";
    cv::Mat img = cv::imread(input);
    ASSERT_FALSE(img.empty());

    std::vector<int> params;
    params.push_back(IMWRITE_JPEG_PROGRESSIVE);
    params.push_back(1);

    string output_progressive = cv::tempfile(".jpg");
    EXPECT_NO_THROW(cv::imwrite(output_progressive, img, params));
    cv::Mat img_jpg_progressive = cv::imread(output_progressive);

    string output_normal = cv::tempfile(".jpg");
    EXPECT_NO_THROW(cv::imwrite(output_normal, img));
    cv::Mat img_jpg_normal = cv::imread(output_normal);

647
    EXPECT_EQ(0, cvtest::norm(img_jpg_progressive, img_jpg_normal, NORM_INF));
N
Nicolas Gryman 已提交
648 649 650

    remove(output_progressive.c_str());
}
651

652
TEST(Imgcodecs_Jpeg, encode_decode_optimize_jpeg)
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
{
    cvtest::TS& ts = *cvtest::TS::ptr();
    string input = string(ts.get_data_path()) + "../cv/shared/lena.png";
    cv::Mat img = cv::imread(input);
    ASSERT_FALSE(img.empty());

    std::vector<int> params;
    params.push_back(IMWRITE_JPEG_OPTIMIZE);
    params.push_back(1);

    string output_optimized = cv::tempfile(".jpg");
    EXPECT_NO_THROW(cv::imwrite(output_optimized, img, params));
    cv::Mat img_jpg_optimized = cv::imread(output_optimized);

    string output_normal = cv::tempfile(".jpg");
    EXPECT_NO_THROW(cv::imwrite(output_normal, img));
    cv::Mat img_jpg_normal = cv::imread(output_normal);

671
    EXPECT_EQ(0, cvtest::norm(img_jpg_optimized, img_jpg_normal, NORM_INF));
672 673 674

    remove(output_optimized.c_str());
}
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

TEST(Imgcodecs_Jpeg, encode_decode_rst_jpeg)
{
    cvtest::TS& ts = *cvtest::TS::ptr();
    string input = string(ts.get_data_path()) + "../cv/shared/lena.png";
    cv::Mat img = cv::imread(input);
    ASSERT_FALSE(img.empty());

    std::vector<int> params;
    params.push_back(IMWRITE_JPEG_RST_INTERVAL);
    params.push_back(1);

    string output_rst = cv::tempfile(".jpg");
    EXPECT_NO_THROW(cv::imwrite(output_rst, img, params));
    cv::Mat img_jpg_rst = cv::imread(output_rst);

    string output_normal = cv::tempfile(".jpg");
    EXPECT_NO_THROW(cv::imwrite(output_normal, img));
    cv::Mat img_jpg_normal = cv::imread(output_normal);

    EXPECT_EQ(0, cvtest::norm(img_jpg_rst, img_jpg_normal, NORM_INF));

S
Sergey Nikulov 已提交
697
    remove(output_rst.c_str());
698 699
}

700
#endif
701 702 703


#ifdef HAVE_TIFF
704 705 706 707

// these defines are used to resolve conflict between tiff.h and opencv2/core/types_c.h
#define uint64 uint64_hack_
#define int64 int64_hack_
708
#include "tiff.h"
709

710 711 712
#ifdef ANDROID
// Test disabled as it uses a lot of memory.
// It is killed with SIGKILL by out of memory killer.
713
TEST(Imgcodecs_Tiff, DISABLED_decode_tile16384x16384)
714
#else
715
TEST(Imgcodecs_Tiff, decode_tile16384x16384)
716
#endif
717 718 719
{
    // see issue #2161
    cv::Mat big(16384, 16384, CV_8UC1, cv::Scalar::all(0));
720 721 722
    string file3 = cv::tempfile(".tiff");
    string file4 = cv::tempfile(".tiff");

723 724 725
    std::vector<int> params;
    params.push_back(TIFFTAG_ROWSPERSTRIP);
    params.push_back(big.rows);
726 727
    cv::imwrite(file4, big, params);
    cv::imwrite(file3, big.colRange(0, big.cols - 1), params);
728 729
    big.release();

730 731
    try
    {
732 733
        cv::imread(file3, IMREAD_UNCHANGED);
        EXPECT_NO_THROW(cv::imread(file4, IMREAD_UNCHANGED));
734 735 736 737 738 739 740 741
    }
    catch(const std::bad_alloc&)
    {
        // have no enough memory
    }

    remove(file3.c_str());
    remove(file4.c_str());
742
}
743

744
TEST(Imgcodecs_Tiff, write_read_16bit_big_little_endian)
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
{
    // see issue #2601 "16-bit Grayscale TIFF Load Failures Due to Buffer Underflow and Endianness"

    // Setup data for two minimal 16-bit grayscale TIFF files in both endian formats
    uchar tiff_sample_data[2][86] = { {
        // Little endian
        0x49, 0x49, 0x2a, 0x00, 0x0c, 0x00, 0x00, 0x00, 0xad, 0xde, 0xef, 0xbe, 0x06, 0x00, 0x00, 0x01,
        0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x00, 0x01, 0x00,
        0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00,
        0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x01,
        0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x17, 0x01, 0x04, 0x00, 0x01, 0x00,
        0x00, 0x00, 0x04, 0x00, 0x00, 0x00 }, {
        // Big endian
        0x4d, 0x4d, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x0c, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x06, 0x01, 0x00,
        0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x01, 0x01, 0x00, 0x03, 0x00, 0x00,
        0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10,
        0x00, 0x00, 0x01, 0x06, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x11,
        0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x01, 0x17, 0x00, 0x04, 0x00, 0x00,
        0x00, 0x01, 0x00, 0x00, 0x00, 0x04 }
        };

    // Test imread() for both a little endian TIFF and big endian TIFF
    for (int i = 0; i < 2; i++)
    {
        string filename = cv::tempfile(".tiff");

        // Write sample TIFF file
        FILE* fp = fopen(filename.c_str(), "wb");
        ASSERT_TRUE(fp != NULL);
        ASSERT_EQ((size_t)1, fwrite(tiff_sample_data, 86, 1, fp));
        fclose(fp);

777
        Mat img = imread(filename, IMREAD_UNCHANGED);
778 779 780 781 782 783 784 785 786 787 788 789 790

        EXPECT_EQ(1, img.rows);
        EXPECT_EQ(2, img.cols);
        EXPECT_EQ(CV_16U, img.type());
        EXPECT_EQ(sizeof(ushort), img.elemSize());
        EXPECT_EQ(1, img.channels());
        EXPECT_EQ(0xDEAD, img.at<ushort>(0,0));
        EXPECT_EQ(0xBEEF, img.at<ushort>(0,1));

        remove(filename.c_str());
    }
}

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
class CV_GrfmtReadTifTiledWithNotFullTiles: public cvtest::BaseTest
{
public:
    void run(int)
    {
        try
        {
            /* see issue #3472 - dealing with tiled images where the tile size is
             * not a multiple of image size.
             * The tiled images were created with 'convert' from ImageMagick,
             * using the command 'convert <input> -define tiff:tile-geometry=128x128 -depth [8|16] <output>
             * Note that the conversion to 16 bits expands the range from 0-255 to 0-255*255,
             * so the test converts back but rounding errors cause small differences.
             */
            cv::Mat img = imread(string(ts->get_data_path()) + "readwrite/non_tiled.tif",-1);
            if (img.empty()) ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
            ASSERT_TRUE(img.channels() == 3);
            cv::Mat tiled8 = imread(string(ts->get_data_path()) + "readwrite/tiled_8.tif", -1);
            if (tiled8.empty()) ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
            ASSERT_PRED_FORMAT2(cvtest::MatComparator(0, 0), img, tiled8);

            cv::Mat tiled16 = imread(string(ts->get_data_path()) + "readwrite/tiled_16.tif", -1);
            if (tiled16.empty()) ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
            ASSERT_TRUE(tiled16.elemSize() == 6);
            tiled16.convertTo(tiled8, CV_8UC3, 1./256.);
            ASSERT_PRED_FORMAT2(cvtest::MatComparator(2, 0), img, tiled8);
            // What about 32, 64 bit?
        }
        catch(...)
        {
            ts->set_failed_test_info(cvtest::TS::FAIL_EXCEPTION);
        }
        ts->set_failed_test_info(cvtest::TS::OK);
    }
};

827
TEST(Imgcodecs_Tiff, decode_tile_remainder)
828 829 830 831
{
    CV_GrfmtReadTifTiledWithNotFullTiles test; test.safe_run();
}

832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
TEST(Imgcodecs_Tiff, decode_infinite_rowsperstrip)
{
    const uchar sample_data[142] = {
        0x49, 0x49, 0x2a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x56, 0x54,
        0x56, 0x5a, 0x59, 0x55, 0x5a, 0x00, 0x0a, 0x00, 0x00, 0x01,
        0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
        0x01, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x07, 0x00,
        0x00, 0x00, 0x02, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
        0x08, 0x00, 0x00, 0x00, 0x03, 0x01, 0x03, 0x00, 0x01, 0x00,
        0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00,
        0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x01,
        0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
        0x15, 0x01, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
        0x00, 0x00, 0x16, 0x01, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00,
        0xff, 0xff, 0xff, 0xff, 0x17, 0x01, 0x04, 0x00, 0x01, 0x00,
        0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x1c, 0x01, 0x03, 0x00,
        0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00
    };

    const string filename = cv::tempfile(".tiff");
    std::ofstream outfile(filename.c_str(), std::ofstream::binary);
    outfile.write(reinterpret_cast<const char *>(sample_data), sizeof sample_data);
    outfile.close();

    EXPECT_NO_THROW(cv::imread(filename, IMREAD_UNCHANGED));

    remove(filename.c_str());
}

862 863 864 865 866 867 868 869 870 871 872
class CV_GrfmtReadTifMultiPage : public cvtest::BaseTest
{
private:
    void compare(int flags)
    {
        const string folder = string(cvtest::TS::ptr()->get_data_path()) + "/readwrite/";
        const int page_count = 6;

        vector<Mat> pages;
        bool res = imreadmulti(folder + "multipage.tif", pages, flags);
        ASSERT_TRUE(res == true);
873
        ASSERT_EQ(static_cast<size_t>(page_count), pages.size());
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892

        for (int i = 0; i < page_count; i++)
        {
            char buffer[256];
            sprintf(buffer, "%smultipage_p%d.tif", folder.c_str(), i + 1);
            const string filepath(buffer);
            const Mat page = imread(filepath, flags);
            ASSERT_TRUE(mats_equal(page, pages[i]));
        }
    }

public:
    void run(int)
    {
        compare(IMREAD_UNCHANGED);
        compare(IMREAD_GRAYSCALE);
        compare(IMREAD_COLOR);
        compare(IMREAD_ANYDEPTH);
        compare(IMREAD_ANYCOLOR);
M
Maksim Shabunin 已提交
893
        // compare(IMREAD_LOAD_GDAL); // GDAL does not support multi-page TIFFs
894 895 896 897 898 899 900 901
    }
};

TEST(Imgcodecs_Tiff, decode_multipage)
{
    CV_GrfmtReadTifMultiPage test; test.safe_run();
}

902 903 904 905 906 907 908 909 910 911 912 913 914
TEST(Imgcodecs_Tiff, imdecode_no_exception_temporary_file_removed)
{
    cvtest::TS& ts = *cvtest::TS::ptr();
    string input = string(ts.get_data_path()) + "../cv/shared/lena.png";
    cv::Mat img = cv::imread(input);
    ASSERT_FALSE(img.empty());

    std::vector<uchar> buf;
    EXPECT_NO_THROW(cv::imencode(".tiff", img, buf));

    EXPECT_NO_THROW(cv::imdecode(buf, IMREAD_UNCHANGED));
}

915
#endif
A
AoD314 已提交
916 917 918

#ifdef HAVE_WEBP

919
TEST(Imgcodecs_WebP, encode_decode_lossless_webp)
A
AoD314 已提交
920 921
{
    cvtest::TS& ts = *cvtest::TS::ptr();
922
    string input = string(ts.get_data_path()) + "../cv/shared/lena.png";
A
AoD314 已提交
923 924 925
    cv::Mat img = cv::imread(input);
    ASSERT_FALSE(img.empty());

926
    string output = cv::tempfile(".webp");
A
AoD314 已提交
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
    EXPECT_NO_THROW(cv::imwrite(output, img)); // lossless

    cv::Mat img_webp = cv::imread(output);

    std::vector<unsigned char> buf;

    FILE * wfile = NULL;

    wfile = fopen(output.c_str(), "rb");
    if (wfile != NULL)
    {
        fseek(wfile, 0, SEEK_END);
        size_t wfile_size = ftell(wfile);
        fseek(wfile, 0, SEEK_SET);

        buf.resize(wfile_size);

        size_t data_size = fread(&buf[0], 1, wfile_size, wfile);

        if(wfile)
        {
            fclose(wfile);
        }

        if (data_size != wfile_size)
        {
            EXPECT_TRUE(false);
        }
    }

A
AoD314 已提交
957 958
    remove(output.c_str());

959
    cv::Mat decode = cv::imdecode(buf, IMREAD_COLOR);
A
AoD314 已提交
960
    ASSERT_FALSE(decode.empty());
961
    EXPECT_TRUE(cvtest::norm(decode, img_webp, NORM_INF) == 0);
A
AoD314 已提交
962 963 964

    ASSERT_FALSE(img_webp.empty());

965
    EXPECT_TRUE(cvtest::norm(img, img_webp, NORM_INF) == 0);
A
AoD314 已提交
966 967
}

968
TEST(Imgcodecs_WebP, encode_decode_lossy_webp)
A
AoD314 已提交
969 970
{
    cvtest::TS& ts = *cvtest::TS::ptr();
971
    std::string input = std::string(ts.get_data_path()) + "../cv/shared/lena.png";
A
AoD314 已提交
972 973 974
    cv::Mat img = cv::imread(input);
    ASSERT_FALSE(img.empty());

975
    for(int q = 100; q>=0; q-=20)
A
AoD314 已提交
976 977
    {
        std::vector<int> params;
978
        params.push_back(IMWRITE_WEBP_QUALITY);
A
AoD314 已提交
979 980 981 982 983
        params.push_back(q);
        string output = cv::tempfile(".webp");

        EXPECT_NO_THROW(cv::imwrite(output, img, params));
        cv::Mat img_webp = cv::imread(output);
A
AoD314 已提交
984
        remove(output.c_str());
A
AoD314 已提交
985
        EXPECT_FALSE(img_webp.empty());
986 987 988
        EXPECT_EQ(3,   img_webp.channels());
        EXPECT_EQ(512, img_webp.cols);
        EXPECT_EQ(512, img_webp.rows);
A
AoD314 已提交
989 990 991
    }
}

992
TEST(Imgcodecs_WebP, encode_decode_with_alpha_webp)
993 994
{
    cvtest::TS& ts = *cvtest::TS::ptr();
995
    std::string input = std::string(ts.get_data_path()) + "../cv/shared/lena.png";
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
    cv::Mat img = cv::imread(input);
    ASSERT_FALSE(img.empty());

    std::vector<cv::Mat> imgs;
    cv::split(img, imgs);
    imgs.push_back(cv::Mat(imgs[0]));
    imgs[imgs.size() - 1] = cv::Scalar::all(128);
    cv::merge(imgs, img);

    string output = cv::tempfile(".webp");

    EXPECT_NO_THROW(cv::imwrite(output, img));
    cv::Mat img_webp = cv::imread(output);
    remove(output.c_str());
    EXPECT_FALSE(img_webp.empty());
1011 1012 1013
    EXPECT_EQ(4,   img_webp.channels());
    EXPECT_EQ(512, img_webp.cols);
    EXPECT_EQ(512, img_webp.rows);
1014 1015
}

A
AoD314 已提交
1016
#endif
F
Fedor Morozov 已提交
1017

1018
TEST(Imgcodecs_Hdr, regression)
F
Fedor Morozov 已提交
1019
{
1020
    string folder = string(cvtest::TS::ptr()->get_data_path()) + "/readwrite/";
1021 1022 1023 1024 1025 1026 1027 1028 1029
    string name_rle = folder + "rle.hdr";
    string name_no_rle = folder + "no_rle.hdr";
    Mat img_rle = imread(name_rle, -1);
    ASSERT_FALSE(img_rle.empty()) << "Could not open " << name_rle;
    Mat img_no_rle = imread(name_no_rle, -1);
    ASSERT_FALSE(img_no_rle.empty()) << "Could not open " << name_no_rle;

    double min = 0.0, max = 1.0;
    minMaxLoc(abs(img_rle - img_no_rle), &min, &max);
1030
    ASSERT_FALSE(max > DBL_EPSILON);
1031 1032 1033 1034 1035 1036 1037 1038
    string tmp_file_name = tempfile(".hdr");
    vector<int>param(1);
    for(int i = 0; i < 2; i++) {
        param[0] = i;
        imwrite(tmp_file_name, img_rle, param);
        Mat written_img = imread(tmp_file_name, -1);
        ASSERT_FALSE(written_img.empty()) << "Could not open " << tmp_file_name;
        minMaxLoc(abs(img_rle - written_img), &min, &max);
1039
        ASSERT_FALSE(max > DBL_EPSILON);
1040
    }
F
Fedor Morozov 已提交
1041
}
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

TEST(Imgcodecs_Pam, readwrite)
{
    string folder = string(cvtest::TS::ptr()->get_data_path()) + "readwrite/";
    string filepath = folder + "lena.pam";

    cv::Mat img = cv::imread(filepath);
    ASSERT_FALSE(img.empty());

    std::vector<int> params;
    params.push_back(IMWRITE_PAM_TUPLETYPE);
    params.push_back(IMWRITE_PAM_FORMAT_RGB);

    string writefile = cv::tempfile(".pam");
    EXPECT_NO_THROW(cv::imwrite(writefile, img, params));
    cv::Mat reread = cv::imread(writefile);

    string writefile_no_param = cv::tempfile(".pam");
    EXPECT_NO_THROW(cv::imwrite(writefile_no_param, img));
    cv::Mat reread_no_param = cv::imread(writefile_no_param);

    EXPECT_EQ(0, cvtest::norm(reread, reread_no_param, NORM_INF));
    EXPECT_EQ(0, cvtest::norm(img, reread, NORM_INF));
}