2
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
3
// of this distribution and at http://opencv.org/license.html
4
4
#include " test_precomp.hpp"
5
+ #include " test_common.hpp"
5
6
6
7
namespace opencv_test { namespace {
7
8
@@ -145,45 +146,6 @@ TEST(Imgcodecs_Image, read_write_bmp)
145
146
typedef string Ext;
146
147
typedef testing::TestWithParam<Ext> Imgcodecs_Image;
147
148
148
- TEST_P (Imgcodecs_Image, read_write)
149
- {
150
- const string ext = this ->GetParam ();
151
- const string full_name = cv::tempfile (ext.c_str ());
152
- const string _name = TS::ptr ()->get_data_path () + " ../cv/shared/baboon.png" ;
153
- const double thresDbell = 32 ;
154
-
155
- Mat image = imread (_name);
156
- image.convertTo (image, CV_8UC3);
157
- ASSERT_FALSE (image.empty ());
158
-
159
- imwrite (full_name, image);
160
- Mat loaded = imread (full_name);
161
- ASSERT_FALSE (loaded.empty ());
162
-
163
- double psnr = cvtest::PSNR (loaded, image);
164
- EXPECT_GT (psnr, thresDbell);
165
-
166
- vector<uchar> from_file;
167
- FILE *f = fopen (full_name.c_str (), " rb" );
168
- fseek (f, 0 , SEEK_END);
169
- long len = ftell (f);
170
- from_file.resize ((size_t )len);
171
- fseek (f, 0 , SEEK_SET);
172
- from_file.resize (fread (&from_file[0 ], 1 , from_file.size (), f));
173
- fclose (f);
174
- vector<uchar> buf;
175
- imencode (" ." + ext, image, buf);
176
- ASSERT_EQ (buf, from_file);
177
-
178
- Mat buf_loaded = imdecode (Mat (buf), 1 );
179
- ASSERT_FALSE (buf_loaded.empty ());
180
-
181
- psnr = cvtest::PSNR (buf_loaded, image);
182
- EXPECT_GT (psnr, thresDbell);
183
-
184
- EXPECT_EQ (0 , remove (full_name.c_str ()));
185
- }
186
-
187
149
const string exts[] = {
188
150
#ifdef HAVE_PNG
189
151
" png" ,
@@ -209,6 +171,90 @@ const string exts[] = {
209
171
#endif
210
172
};
211
173
174
+ static
175
+ void test_image_io (const Mat& image, const std::string& fname, const std::string& ext, int imreadFlag, double psnrThreshold)
176
+ {
177
+ vector<uchar> buf;
178
+ ASSERT_NO_THROW (imencode (" ." + ext, image, buf));
179
+
180
+ ASSERT_NO_THROW (imwrite (fname, image));
181
+
182
+ FILE *f = fopen (fname.c_str (), " rb" );
183
+ fseek (f, 0 , SEEK_END);
184
+ long len = ftell (f);
185
+ cout << " File size: " << len << " bytes" << endl;
186
+ EXPECT_GT (len, 1024 ) << " File is small. Test or implementation is broken" ;
187
+ fseek (f, 0 , SEEK_SET);
188
+ vector<uchar> file_buf ((size_t )len);
189
+ EXPECT_EQ (len, (long )fread (&file_buf[0 ], 1 , (size_t )len, f));
190
+ fclose (f); f = NULL ;
191
+
192
+ EXPECT_EQ (buf, file_buf) << " imwrite() / imencode() calls must provide the same output (bit-exact)" ;
193
+
194
+ Mat buf_loaded = imdecode (Mat (buf), imreadFlag);
195
+ EXPECT_FALSE (buf_loaded.empty ());
196
+
197
+ Mat loaded = imread (fname, imreadFlag);
198
+ EXPECT_FALSE (loaded.empty ());
199
+
200
+ EXPECT_EQ (0 , cv::norm (loaded, buf_loaded, NORM_INF)) << " imread() and imdecode() calls must provide the same result (bit-exact)" ;
201
+
202
+ double psnr = cvtest::PSNR (loaded, image);
203
+ EXPECT_GT (psnr, psnrThreshold);
204
+
205
+ // not necessary due bitexact check above
206
+ // double buf_psnr = cvtest::PSNR(buf_loaded, image);
207
+ // EXPECT_GT(buf_psnr, psnrThreshold);
208
+
209
+ #if 0 // debug
210
+ if (psnr <= psnrThreshold /*|| buf_psnr <= thresDbell*/)
211
+ {
212
+ cout << "File: " << fname << endl;
213
+ imshow("origin", image);
214
+ imshow("imread", loaded);
215
+ imshow("imdecode", buf_loaded);
216
+ waitKey();
217
+ }
218
+ #endif
219
+ }
220
+
221
+ TEST_P (Imgcodecs_Image, read_write_BGR)
222
+ {
223
+ const string ext = this ->GetParam ();
224
+ const string fname = cv::tempfile (ext.c_str ());
225
+
226
+ double psnrThreshold = 100 ;
227
+ if (ext == " jpg" )
228
+ psnrThreshold = 32 ;
229
+
230
+ Mat image = generateTestImageBGR ();
231
+ EXPECT_NO_THROW (test_image_io (image, fname, ext, IMREAD_COLOR, psnrThreshold));
232
+
233
+ EXPECT_EQ (0 , remove (fname.c_str ()));
234
+ }
235
+
236
+ TEST_P (Imgcodecs_Image, read_write_GRAYSCALE)
237
+ {
238
+ const string ext = this ->GetParam ();
239
+
240
+ if (false
241
+ || ext == " ppm" // grayscale is not implemented
242
+ || ext == " ras" // broken (black result)
243
+ )
244
+ throw SkipTestException (" GRAYSCALE mode is not supported" );
245
+
246
+ const string fname = cv::tempfile (ext.c_str ());
247
+
248
+ double psnrThreshold = 100 ;
249
+ if (ext == " jpg" )
250
+ psnrThreshold = 40 ;
251
+
252
+ Mat image = generateTestImageGrayscale ();
253
+ EXPECT_NO_THROW (test_image_io (image, fname, ext, IMREAD_GRAYSCALE, psnrThreshold));
254
+
255
+ EXPECT_EQ (0 , remove (fname.c_str ()));
256
+ }
257
+
212
258
INSTANTIATE_TEST_CASE_P (imgcodecs, Imgcodecs_Image, testing::ValuesIn(exts));
213
259
214
260
TEST (Imgcodecs_Image, regression_9376)
0 commit comments