fixed many warnings from GCC 4.6.1
[profile/ivi/opencv.git] / modules / highgui / test / test_grfmt.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #include "test_precomp.hpp"
44 #include "opencv2/highgui/highgui.hpp"
45
46 using namespace cv;
47 using namespace std;
48
49
50 class CV_GrfmtWriteBigImageTest : public cvtest::BaseTest
51 {
52 public:
53     void run(int)
54     {
55         try
56         {
57             ts->printf(cvtest::TS::LOG, "start  reading big image\n");
58             Mat img = imread(string(ts->get_data_path()) + "readwrite/read.png");
59             ts->printf(cvtest::TS::LOG, "finish reading big image\n");
60             if (img.empty()) ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
61             ts->printf(cvtest::TS::LOG, "start  writing big image\n");
62             imwrite(string(ts->get_data_path()) + "readwrite/write.png", img);
63             ts->printf(cvtest::TS::LOG, "finish writing big image\n");
64         }
65         catch(...)
66         {
67             ts->set_failed_test_info(cvtest::TS::FAIL_EXCEPTION);
68         }
69         ts->set_failed_test_info(cvtest::TS::OK);
70     }
71 };
72
73 string ext_from_int(int ext)
74 {
75     if (ext == 0) return ".png";
76     if (ext == 1) return ".bmp";
77     if (ext == 2) return ".pgm";
78     if (ext == 3) return ".tiff";
79     return "";
80 }
81
82 class CV_GrfmtWriteSequenceImageTest : public cvtest::BaseTest
83 {
84 public:
85     void run(int)
86     {
87         try
88         {
89             const int img_r = 640;
90             const int img_c = 480;
91
92             for (int k = 1; k <= 5; ++k)
93             {
94                 for (int ext = 0; ext < 4; ++ext) // 0 - png, 1 - bmp, 2 - pgm, 3 - tiff
95                     for (int num_channels = 1; num_channels <= 3; num_channels+=2)
96                     {
97                         ts->printf(ts->LOG, "image type depth:%d   channels:%d   ext: %s\n", CV_8U, num_channels, ext_from_int(ext).c_str());
98                         Mat img(img_r * k, img_c * k, CV_MAKETYPE(CV_8U, num_channels), Scalar::all(0));
99                         circle(img, Point2i((img_c * k) / 2, (img_r * k) / 2), cv::min((img_r * k), (img_c * k)) / 4 , Scalar::all(255));
100                         ts->printf(ts->LOG, "writing      image : %s\n", string(string(ts->get_data_path()) + "readwrite/test" + ext_from_int(ext)).c_str());
101                         imwrite(string(ts->get_data_path()) + "readwrite/test" + ext_from_int(ext), img);
102                         ts->printf(ts->LOG, "reading test image : %s\n", string(string(ts->get_data_path()) + "readwrite/test" + ext_from_int(ext)).c_str());
103
104                         Mat img_test = imread(string(ts->get_data_path()) + "readwrite/test" + ext_from_int(ext), CV_LOAD_IMAGE_UNCHANGED);
105
106                         if (img_test.empty()) ts->set_failed_test_info(ts->FAIL_MISMATCH);
107
108                         CV_Assert(img.size() == img_test.size());
109                         CV_Assert(img.type() == img_test.type());
110
111                         double n = norm(img, img_test);
112                         if ( n > 1.0)
113                         {
114                             ts->printf(ts->LOG, "norm = %f \n", n);
115                             ts->set_failed_test_info(ts->FAIL_MISMATCH);
116                         }
117                     }
118
119                 for (int num_channels = 1; num_channels <= 3; num_channels+=2)
120                 {
121                     // jpeg
122                     ts->printf(ts->LOG, "image type depth:%d   channels:%d   ext: %s\n", CV_8U, num_channels, ".jpg");
123                     Mat img(img_r * k, img_c * k, CV_MAKETYPE(CV_8U, num_channels), Scalar::all(0));
124                     circle(img, Point2i((img_c * k) / 2, (img_r * k) / 2), cv::min((img_r * k), (img_c * k)) / 4 , Scalar::all(255));
125                     string filename = string(ts->get_data_path() + "readwrite/test_" + char(k + 48) + "_c" + char(num_channels + 48) + "_.jpg");
126                     imwrite(filename, img);
127                     img = imread(filename, CV_LOAD_IMAGE_UNCHANGED);
128
129                     filename = string(ts->get_data_path() + "readwrite/test_" + char(k + 48) + "_c" + char(num_channels + 48) + ".jpg");
130                     ts->printf(ts->LOG, "reading test image : %s\n", filename.c_str());
131                     Mat img_test = imread(filename, CV_LOAD_IMAGE_UNCHANGED);
132
133                     if (img_test.empty()) ts->set_failed_test_info(ts->FAIL_MISMATCH);
134
135                     CV_Assert(img.size() == img_test.size());
136                     CV_Assert(img.type() == img_test.type());
137
138                     double n = norm(img, img_test);
139                     if ( n > 1.0)
140                     {
141                         ts->printf(ts->LOG, "norm = %f \n", n);
142                         ts->set_failed_test_info(ts->FAIL_MISMATCH);
143                     }
144                 }
145
146                 for (int num_channels = 1; num_channels <= 3; num_channels+=2)
147                 {
148                     // tiff
149                     ts->printf(ts->LOG, "image type depth:%d   channels:%d   ext: %s\n", CV_16U, num_channels, ".tiff");
150                     Mat img(img_r * k, img_c * k, CV_MAKETYPE(CV_16U, num_channels), Scalar::all(0));
151                     circle(img, Point2i((img_c * k) / 2, (img_r * k) / 2), cv::min((img_r * k), (img_c * k)) / 4 , Scalar::all(255));
152                     string filename = string(ts->get_data_path() + "readwrite/test.tiff");
153                     imwrite(filename, img);
154                     ts->printf(ts->LOG, "reading test image : %s\n", filename.c_str());
155                     Mat img_test = imread(filename, CV_LOAD_IMAGE_UNCHANGED);
156
157                     if (img_test.empty()) ts->set_failed_test_info(ts->FAIL_MISMATCH);
158
159                     CV_Assert(img.size() == img_test.size());
160
161                     ts->printf(ts->LOG, "img      : %d ; %d \n", img.channels(), img.depth());
162                     ts->printf(ts->LOG, "img_test : %d ; %d \n", img_test.channels(), img_test.depth());
163
164                     CV_Assert(img.type() == img_test.type());
165
166
167                     double n = norm(img, img_test);
168                     if ( n > 1.0)
169                     {
170                         ts->printf(ts->LOG, "norm = %f \n", n);
171                         ts->set_failed_test_info(ts->FAIL_MISMATCH);
172                     }
173                 }
174             }
175         }
176         catch(const cv::Exception & e)
177         {
178             ts->printf(ts->LOG, "Exception: %s\n" , e.what());
179             ts->set_failed_test_info(ts->FAIL_MISMATCH);
180         }
181     }
182 };
183
184 TEST(Highgui_Grfmt_WriteBigImage,         regression) { CV_GrfmtWriteBigImageTest      test; test.safe_run(); }
185 TEST(Highgui_Grfmt_WriteSequenceImage,    regression) { CV_GrfmtWriteSequenceImageTest test; test.safe_run(); }