fixed build errors and warnings on Windows
[profile/ivi/opencv.git] / modules / imgproc / test / test_imgwarp_strict.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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "test_precomp.hpp"
43
44 #include <cmath>
45 #include <vector>
46 #include <iostream>
47
48 using namespace cv;
49 using namespace std;
50
51 void __wrap_printf_func(const char* fmt, ...)
52 {
53     va_list args;
54     va_start(args, fmt);
55     char buffer[256];
56     vsprintf (buffer, fmt, args);
57     cvtest::TS::ptr()->printf(cvtest::TS::SUMMARY, buffer);
58     va_end(args);
59 }
60
61 #define PRINT_TO_LOG __wrap_printf_func
62 #define SHOW_IMAGE
63 #undef SHOW_IMAGE
64
65 ////////////////////////////////////////////////////////////////////////////////////////////////////////
66 // ImageWarpBaseTest
67 ////////////////////////////////////////////////////////////////////////////////////////////////////////
68
69 class CV_ImageWarpBaseTest :
70     public cvtest::BaseTest
71 {
72 public:
73     enum
74     {
75         cell_size = 10
76     };
77
78     CV_ImageWarpBaseTest();
79
80     virtual void run(int);
81
82     virtual ~CV_ImageWarpBaseTest();
83
84 protected:
85     virtual void generate_test_data();
86
87     virtual void run_func() = 0;
88     virtual void run_reference_func() = 0;
89     virtual void validate_results() const = 0;
90
91     Size randSize(RNG& rng) const;
92     
93     const char* interpolation_to_string(int inter_type) const;
94
95     int interpolation;
96     Mat src;
97     Mat dst;
98 };
99
100 CV_ImageWarpBaseTest::CV_ImageWarpBaseTest() :
101     BaseTest(), interpolation(-1),
102     src(), dst()
103 {
104     test_case_count = 40;
105     ts->set_failed_test_info(cvtest::TS::OK);
106 }
107
108 CV_ImageWarpBaseTest::~CV_ImageWarpBaseTest()
109 {
110 }
111
112 const char* CV_ImageWarpBaseTest::interpolation_to_string(int inter) const
113 {
114     if (inter == INTER_NEAREST)
115         return "INTER_NEAREST";
116     if (inter == INTER_LINEAR)
117         return "INTER_LINEAR";
118     if (inter == INTER_AREA)
119         return "INTER_AREA";
120     if (inter == INTER_CUBIC)
121         return "INTER_CUBIC";
122     if (inter == INTER_LANCZOS4)
123         return "INTER_LANCZOS4";
124     if (inter == INTER_LANCZOS4 + 1)
125         return "INTER_AREA_FAST";
126     return "Unsupported/Unkown interpolation type";
127 }
128
129 void interpolateLinear(float x, float* coeffs)
130 {
131     coeffs[0] = 1.f - x;
132     coeffs[1] = x;
133 }
134
135 void interpolateCubic(float x, float* coeffs)
136 {
137     const float A = -0.75f;
138
139     coeffs[0] = ((A*(x + 1) - 5*A)*(x + 1) + 8*A)*(x + 1) - 4*A;
140     coeffs[1] = ((A + 2)*x - (A + 3))*x*x + 1;
141     coeffs[2] = ((A + 2)*(1 - x) - (A + 3))*(1 - x)*(1 - x) + 1;
142     coeffs[3] = 1.f - coeffs[0] - coeffs[1] - coeffs[2];
143 }
144
145 void interpolateLanczos4(float x, float* coeffs)
146 {
147     static const double s45 = 0.70710678118654752440084436210485;
148     static const double cs[][2]=
149     {{1, 0}, {-s45, -s45}, {0, 1}, {s45, -s45}, {-1, 0}, {s45, s45}, {0, -1}, {-s45, s45}};
150
151     if( x < FLT_EPSILON )
152     {
153         for( int i = 0; i < 8; i++ )
154             coeffs[i] = 0;
155         coeffs[3] = 1;
156         return;
157     }
158
159     float sum = 0;
160     double y0=-(x+3)*CV_PI*0.25, s0 = sin(y0), c0=cos(y0);
161     for(int i = 0; i < 8; i++ )
162     {
163         double y = -(x+3-i)*CV_PI*0.25;
164         coeffs[i] = (float)((cs[i][0]*s0 + cs[i][1]*c0)/(y*y));
165         sum += coeffs[i];
166     }
167
168     sum = 1.f/sum;
169     for(int i = 0; i < 8; i++ )
170         coeffs[i] *= sum;
171 }
172
173 typedef void (*interpolate_method)(float x, float* coeffs);
174 interpolate_method inter_array[] = { &interpolateLinear, &interpolateCubic, &interpolateLanczos4 };
175
176 Size CV_ImageWarpBaseTest::randSize(RNG& rng) const
177 {
178     Size size;
179     size.width = saturate_cast<int>(std::exp(rng.uniform(0.0, 7.0)));
180     size.height = saturate_cast<int>(std::exp(rng.uniform(0.0, 7.0)));
181
182     return size;
183 }
184
185 void CV_ImageWarpBaseTest::generate_test_data()
186 {
187     RNG& rng = ts->get_rng();
188
189     Size ssize = randSize(rng);
190
191     int depth = CV_8S;
192     while (depth == CV_8S || depth == CV_32S)
193         depth = rng.uniform(0, CV_64F);
194
195     int cn = rng.uniform(1, 4);
196     while (cn == 2)
197         cn = rng.uniform(1, 4);
198     interpolation = rng.uniform(0, CV_INTER_LANCZOS4 + 1);
199     interpolation = INTER_NEAREST;
200
201     src.create(ssize, CV_MAKE_TYPE(depth, cn));
202
203     // generating the src matrix
204     int x, y;
205     if (cvtest::randInt(rng) % 2)
206     {
207         for (y = 0; y < ssize.height; y += cell_size)
208             for (x = 0; x < ssize.width; x += cell_size)
209                 rectangle(src, Point(x, y), Point(x + std::min<int>(cell_size, ssize.width - x), y +
210                         std::min<int>(cell_size, ssize.height - y)), Scalar::all((x + y) % 2 ? 255: 0), CV_FILLED);
211     }
212     else
213     {
214         src = Scalar::all(255);
215         for (y = cell_size; y < src.rows; y += cell_size)
216             line(src, Point2i(0, y), Point2i(src.cols, y), Scalar::all(0), 1);
217         for (x = cell_size; x < src.cols; x += cell_size)
218             line(src, Point2i(x, 0), Point2i(x, src.rows), Scalar::all(0), 1);
219     }
220 }
221
222 void CV_ImageWarpBaseTest::run(int)
223 {
224     for (int i = 0; i < test_case_count; ++i)
225     {
226         generate_test_data();
227         run_func();
228         run_reference_func();
229         if (ts->get_err_code() < 0)
230             break;
231         validate_results();
232         if (ts->get_err_code() < 0)
233             break;
234         ts->update_context(this, i, true);
235     }
236     ts->set_gtest_status();
237 }
238
239 ////////////////////////////////////////////////////////////////////////////////////////////////////////
240 // Resize
241 ////////////////////////////////////////////////////////////////////////////////////////////////////////
242
243 class CV_Resize_Test :
244     public CV_ImageWarpBaseTest
245 {
246 public:
247     CV_Resize_Test();
248     virtual ~CV_Resize_Test();
249
250 protected:
251     virtual void generate_test_data();
252
253     virtual void run_func();
254     virtual void run_reference_func();
255     virtual void validate_results() const;
256
257 private:
258     double scale_x;
259     double scale_y;
260     bool area_fast;
261     Mat reference_dst;
262
263     void resize_generic();
264     void resize_area();
265     double getWeight(double a, double b, int x);
266     
267     typedef std::vector<std::pair<int, double> > dim;
268     void generate_buffer(double scale, dim& _dim);
269     void resize_1d(const Mat& _src, Mat& _dst, int dy, const dim& _dim);
270 };
271
272 CV_Resize_Test::CV_Resize_Test() :
273     CV_ImageWarpBaseTest(), scale_x(), scale_y(),
274     area_fast(false), reference_dst()
275 {
276 }
277
278 CV_Resize_Test::~CV_Resize_Test()
279 {
280 }
281
282 void CV_Resize_Test::generate_test_data()
283 {
284     CV_ImageWarpBaseTest::generate_test_data();
285     RNG& rng = ts->get_rng();
286     Size dsize, ssize = src.size();
287     
288     if (interpolation == INTER_AREA)
289     {
290         area_fast = rng.uniform(0., 1.) > 0.5;
291         if (area_fast)
292         {
293             scale_x = rng.uniform(2, 5);
294             scale_y = rng.uniform(2, 5);
295         }
296         else
297         {
298             scale_x = rng.uniform(1.0, 3.0);
299             scale_y = rng.uniform(1.0, 3.0);
300         }
301     }
302     else
303     {
304         scale_x = rng.uniform(0.4, 4.0);
305         scale_y = rng.uniform(0.4, 4.0);
306     }
307     dsize.width = saturate_cast<int>((ssize.width + scale_x - 1) / scale_x);
308     dsize.height = saturate_cast<int>((ssize.height + scale_y - 1) / scale_y);
309
310     dst = Mat::zeros(dsize, src.type());
311     reference_dst = Mat::zeros(dst.size(), CV_MAKE_TYPE(CV_32F, dst.channels()));
312
313     scale_x = src.cols / static_cast<double>(dst.cols);
314     scale_y = src.rows / static_cast<double>(dst.rows);
315     
316     if (interpolation == INTER_AREA && (scale_x < 1.0 || scale_y < 1.0))
317         interpolation = INTER_LINEAR;
318     
319     area_fast = interpolation == INTER_AREA &&
320         fabs(scale_x - cvRound(scale_x)) < FLT_EPSILON &&
321         fabs(scale_y - cvRound(scale_y)) < FLT_EPSILON;
322     if (area_fast)
323     {
324         scale_x = cvRound(scale_x);
325         scale_y = cvRound(scale_y);
326     }
327 }
328
329 void CV_Resize_Test::run_func()
330 {
331     cv::resize(src, dst, dst.size(), 0, 0, interpolation);
332 }
333
334 void CV_Resize_Test::run_reference_func()
335 {
336     if (src.depth() != CV_32F)
337     {
338         Mat tmp;
339         src.convertTo(tmp, CV_32F);
340         src = tmp;
341     }
342     if (interpolation == INTER_AREA)
343         resize_area();
344     else
345         resize_generic();
346 }
347
348 double CV_Resize_Test::getWeight(double a, double b, int x)
349 {
350     float w = std::min(x + 1., b) - std::max(x + 0., a);
351     CV_Assert(w >= 0);
352     return w;
353 }
354
355 void CV_Resize_Test::resize_area()
356 {
357     Size ssize = src.size(), dsize = reference_dst.size();
358     CV_Assert(ssize.area() > 0 && dsize.area() > 0); 
359     int cn = src.channels();
360
361     CV_Assert(scale_x >= 1.0 && scale_y >= 1.0); 
362     
363     double fsy0 = 0, fsy1 = scale_y;
364     for (int dy = 0; dy < dsize.height; ++dy)
365     {
366         float* yD = reference_dst.ptr<float>(dy);
367         int isy0 = cvFloor(fsy0), isy1 = std::min(cvFloor(fsy1), ssize.height - 1);
368         CV_Assert(isy1 <= ssize.height && isy0 < ssize.height);
369        
370         float fsx0 = 0, fsx1 = scale_x;
371
372         for (int dx = 0; dx < dsize.width; ++dx)
373         {
374             float* xyD = yD + cn * dx;
375             int isx0 = cvFloor(fsx0), isx1 = std::min(ssize.width - 1, cvFloor(fsx1));
376             
377             CV_Assert(isx1 <= ssize.width);
378             CV_Assert(isx0 < ssize.width);
379             
380             // for each pixel of dst
381             for (int r = 0; r < cn; ++r)
382             {
383                 xyD[r] = 0.0f;
384                 double area = 0.0;
385                 for (int sy = isy0; sy <= isy1; ++sy)
386                 {
387                     const float* yS = src.ptr<float>(sy);
388                     for (int sx = isx0; sx <= isx1; ++sx)
389                     {
390                         double wy = getWeight(fsy0, fsy1, sy);
391                         double wx = getWeight(fsx0, fsx1, sx);
392                         double w = wx * wy;
393                         xyD[r] += yS[sx * cn + r] * w;
394                         area += w;
395                     }
396                 }
397                 
398                 CV_Assert(area != 0);
399                 // norming pixel
400                 xyD[r] /= area;
401             }
402             fsx1 = std::min<double>((fsx0 = fsx1) + scale_x, ssize.width);
403         }
404         fsy1 = std::min<double>((fsy0 = fsy1) + scale_y, ssize.height);
405     }
406 }
407
408 // for interpolation type : INTER_LINEAR, INTER_LINEAR, INTER_CUBIC, INTER_LANCZOS4
409 void CV_Resize_Test::resize_1d(const Mat& _src, Mat& _dst, int dy, const dim& _dim)
410 {
411     Size dsize = _dst.size(); 
412     int cn = _dst.channels();
413     float* yD = _dst.ptr<float>(dy);
414     
415     if (interpolation == INTER_NEAREST)
416     {
417         const float* yS = _src.ptr<float>(dy);
418         for (int dx = 0; dx < dsize.width; ++dx)
419         {
420             int isx = _dim[dx].first;
421             const float* xyS = yS + isx * cn; 
422             float* xyD = yD + dx * cn; 
423             
424             for (int r = 0; r < cn; ++r)
425                 xyD[r] = xyS[r];
426         }
427     }
428     else if (interpolation == INTER_LINEAR || interpolation == INTER_CUBIC || interpolation == INTER_LANCZOS4)
429     {
430         interpolate_method inter_func = inter_array[interpolation - (interpolation == INTER_LANCZOS4 ? 2 : 1)];
431         int elemsize = _src.elemSize();
432         
433         int ofs = 0, ksize = 2;
434         if (interpolation == INTER_CUBIC)
435             ofs = 1, ksize = 4;
436         else if (interpolation == INTER_LANCZOS4)
437             ofs = 3, ksize = 8;
438         cv::AutoBuffer<float> _w(ksize);
439         float* w = _w;
440         
441         Mat _extended_src_row(1, _src.cols + ksize * 2, _src.type());
442         uchar* srow = _src.data + dy * _src.step;
443         memcpy(_extended_src_row.data + elemsize * ksize, srow, _src.step);
444         for (int k = 0; k < ksize; ++k)
445         {
446             memcpy(_extended_src_row.data + k * elemsize, srow, elemsize);
447             memcpy(_extended_src_row.data + (ksize + k) * elemsize + _src.step, srow + _src.step - elemsize, elemsize);
448         }
449         
450         for (int dx = 0; dx < dsize.width; ++dx)
451         {
452             int isx = _dim[dx].first;
453             double fsx = _dim[dx].second;
454
455             float *xyD = yD + dx * cn;
456             const float* xyS = _extended_src_row.ptr<float>(0) + (isx + ksize - ofs) * cn;
457
458             inter_func(fsx, w);
459
460             for (int r = 0; r < cn; ++r)
461             {
462                 xyD[r] = 0;
463                 for (int k = 0; k < ksize; ++k)
464                     xyD[r] += w[k] * xyS[k * cn + r];
465                 xyD[r] = xyD[r];
466             }
467         }
468     }
469     else
470         CV_Assert(0);
471 }
472
473 void CV_Resize_Test::generate_buffer(double scale, dim& _dim)
474 {
475     int length = _dim.size();
476     for (int dx = 0; dx < length; ++dx)
477     {            
478         double fsx = scale * (dx + 0.5f) - 0.5f;
479         int isx = cvFloor(fsx);
480         _dim[dx] = std::make_pair(isx, fsx - isx);
481     }
482 }
483
484 void CV_Resize_Test::resize_generic()
485 {
486     Size dsize = reference_dst.size(), ssize = src.size();
487     CV_Assert(dsize.area() > 0 && ssize.area() > 0);
488     
489     dim dims[] = { dim(dsize.width), dim(dsize.height) };
490     if (interpolation == INTER_NEAREST)
491     {
492         for (int dx = 0; dx < dsize.width; ++dx)
493             dims[0][dx].first = std::min(cvFloor(dx * scale_x), ssize.width - 1); 
494         for (int dy = 0; dy < dsize.height; ++dy)
495             dims[1][dy].first = std::min(cvFloor(dy * scale_y), ssize.height - 1);
496     }
497     else
498     {
499         generate_buffer(scale_x, dims[0]);
500         generate_buffer(scale_y, dims[1]);
501     }
502     
503     Mat tmp(ssize.height, dsize.width, reference_dst.type());
504     for (int dy = 0; dy < tmp.rows; ++dy)
505         resize_1d(src, tmp, dy, dims[0]);
506
507     transpose(tmp, tmp);
508     transpose(reference_dst, reference_dst);
509     
510     for (int dy = 0; dy < tmp.rows; ++dy)
511         resize_1d(tmp, reference_dst, dy, dims[1]);
512     transpose(reference_dst, reference_dst);
513 }
514
515 void CV_Resize_Test::validate_results() const
516 {
517     Mat _dst = dst;
518     if (dst.depth() != reference_dst.depth())
519     {
520         Mat tmp;
521         dst.convertTo(tmp, reference_dst.depth());
522         _dst = tmp;
523     }
524
525     Size dsize = dst.size();
526     int cn = _dst.channels();
527     dsize.width *= cn;
528     float t = 1.0f;
529     if (interpolation == INTER_CUBIC)
530         t = 1.0f;
531     else if (interpolation == INTER_LANCZOS4)
532         t = 1.0f;
533     else if (interpolation == INTER_NEAREST)
534         t = 1.0f;
535     else if (interpolation == INTER_AREA)
536         t = 2.0f;
537     
538     for (int dy = 0; dy < dsize.height; ++dy)
539     {
540         const float* rD = reference_dst.ptr<float>(dy);
541         const float* D = _dst.ptr<float>(dy);
542
543         for (int dx = 0; dx < dsize.width; ++dx)
544             if (fabs(rD[dx] - D[dx]) > t)
545             {
546                 PRINT_TO_LOG("\nNorm of the difference: %lf\n", norm(reference_dst, _dst, NORM_INF));
547                 PRINT_TO_LOG("Error in (dx, dy): (%d, %d)\n", dx / cn + 1, dy + 1);
548                 PRINT_TO_LOG("Tuple (rD, D): (%f, %f)\n", rD[dx], D[dx]);
549                 PRINT_TO_LOG("Dsize: (%d, %d)\n", dsize.width / cn, dsize.height);
550                 PRINT_TO_LOG("Ssize: (%d, %d)\n", src.cols, src.rows);
551                 PRINT_TO_LOG("Interpolation: %s\n",
552                     interpolation_to_string(area_fast ? INTER_LANCZOS4 + 1 : interpolation));
553                 PRINT_TO_LOG("Scale (x, y): (%lf, %lf)\n", scale_x, scale_y);
554                 PRINT_TO_LOG("Elemsize: %d\n", src.elemSize());
555                 PRINT_TO_LOG("Channels: %d\n", cn);
556
557 #ifdef SHOW_IMAGE
558                 const std::string w1("Resize (run func)"), w2("Reference Resize"), w3("Src image"), w4("Diff");
559                 namedWindow(w1, CV_WINDOW_KEEPRATIO);
560                 namedWindow(w2, CV_WINDOW_KEEPRATIO);
561                 namedWindow(w3, CV_WINDOW_KEEPRATIO);
562                 namedWindow(w4, CV_WINDOW_KEEPRATIO);
563                 
564                 Mat diff;
565                 absdiff(reference_dst, _dst, diff);
566                 
567                 imshow(w1, dst);
568                 imshow(w2, reference_dst);
569                 imshow(w3, src);
570                 imshow(w4, diff);
571                 
572                 waitKey();
573 #endif
574                 /*
575                 const int radius = 3;
576                 int rmin = MAX(dy - radius, 0), rmax = MIN(dy + radius, dsize.height);
577                 int cmin = MAX(dx - radius, 0), cmax = MIN(dx + radius, dsize.width);
578                 
579                 cout << "opencv result:\n" << dst(Range(rmin, rmax), Range(cmin, cmax)) << endl;
580                 cout << "reference result:\n" << reference_dst(Range(rmin, rmax), Range(cmin, cmax)) << endl;
581                 */
582                 
583                 ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
584                 return;
585             }
586     }
587 }
588
589 ////////////////////////////////////////////////////////////////////////////////////////////////////////
590 // remap
591 ////////////////////////////////////////////////////////////////////////////////////////////////////////
592
593 class CV_Remap_Test :
594     public CV_ImageWarpBaseTest
595 {
596 public:
597     CV_Remap_Test();
598
599     virtual ~CV_Remap_Test();
600
601 private:
602     typedef void (CV_Remap_Test::*remap_func)(const Mat&, Mat&);
603
604 protected:
605     virtual void generate_test_data();
606     virtual void prepare_test_data_for_reference_func();
607
608     virtual void run_func();
609     virtual void run_reference_func();
610     virtual void validate_results() const;
611
612     Mat mapx, mapy;
613     int borderType;
614     Scalar borderValue;
615
616     remap_func funcs[2];
617
618     Mat dilate_src;
619     Mat erode_src;
620     Mat dilate_dst;
621     Mat erode_dst;
622
623 private:
624     void remap_nearest(const Mat&, Mat&);
625     void remap_generic(const Mat&, Mat&);
626
627     void convert_maps();
628     const char* borderType_to_string() const;
629 };
630
631 CV_Remap_Test::CV_Remap_Test() :
632     CV_ImageWarpBaseTest(), mapx(), mapy(),
633     borderType(), borderValue(), dilate_src(), erode_src(),
634     dilate_dst(), erode_dst()
635 {
636     funcs[0] = &CV_Remap_Test::remap_nearest;
637     funcs[1] = &CV_Remap_Test::remap_generic;
638 }
639
640 CV_Remap_Test::~CV_Remap_Test()
641 {
642 }
643
644 const char* CV_Remap_Test::borderType_to_string() const
645 {
646     if (borderType == BORDER_CONSTANT)
647         return "BORDER_CONSTANT";
648     if (borderType == BORDER_REPLICATE)
649         return "BORDER_REPLICATE";
650     if (borderType == BORDER_REFLECT)
651         return "BORDER_REFLECT";
652     return "Unsupported/Unkown border type";
653 }
654
655 void CV_Remap_Test::generate_test_data()
656 {
657     CV_ImageWarpBaseTest::generate_test_data();
658
659     RNG& rng = ts->get_rng();
660     borderType = rng.uniform(1, BORDER_WRAP);
661     borderValue = Scalar::all(rng.uniform(0, 255));
662
663     Size dsize = randSize(rng);
664     dst.create(dsize, src.type());
665
666     // generating the mapx, mapy matrix
667     static const int mapx_types[] = { CV_16SC2, CV_32FC1, CV_32FC2 };
668     mapx.create(dst.size(), mapx_types[rng.uniform(0, sizeof(mapx_types) / sizeof(int))]);
669     mapy.release();
670
671     const int n = std::min(std::min(src.cols, src.rows) / 10 + 1, 2);
672     float _n = 0; //static_cast<float>(-n);
673
674 //     mapy.release();
675 //     mapx.create(dst.size(), CV_32FC2);
676 //     for (int y = 0; y < dsize.height; ++y)
677 //     {
678 //         float* yM = mapx.ptr<float>(y);
679 //         for (int x = 0; x < dsize.width; ++x)
680 //         {
681 //             float* xyM = yM + (x << 1);
682 //             xyM[0] = x;
683 //             xyM[1] = y;
684 //         }
685 //     }
686 //     return;
687      
688     switch (mapx.type())
689     {
690         case CV_16SC2:
691         {
692             MatIterator_<Vec2s> begin_x = mapx.begin<Vec2s>(), end_x = mapx.end<Vec2s>();
693             for ( ; begin_x != end_x; ++begin_x)
694             {
695                 begin_x[0] = rng.uniform(static_cast<int>(_n), std::max(src.cols + n - 1, 0));
696                 begin_x[1] = rng.uniform(static_cast<int>(_n), std::max(src.rows + n - 1, 0));
697             }
698
699             if (interpolation != INTER_NEAREST)
700             {
701                 static const int mapy_types[] = { CV_16UC1, CV_16SC1 };
702                 mapy.create(dst.size(), mapy_types[rng.uniform(0, sizeof(mapy_types) / sizeof(int))]);
703
704                 switch (mapy.type())
705                 {
706                     case CV_16UC1:
707                     {
708                         MatIterator_<ushort> begin_y = mapy.begin<ushort>(), end_y = mapy.end<ushort>();
709                         for ( ; begin_y != end_y; ++begin_y)
710                             begin_y[0] = (ushort)rng.uniform(0, 1024);
711                     }
712                     break;
713
714                     case CV_16SC1:
715                     {
716                         MatIterator_<short> begin_y = mapy.begin<short>(), end_y = mapy.end<short>();
717                         for ( ; begin_y != end_y; ++begin_y)
718                             begin_y[0] = (short)rng.uniform(0, 1024);
719                     }
720                     break;
721                 }
722             }
723         }
724         break;
725
726         case CV_32FC1:
727         {
728             mapy.create(dst.size(), CV_32FC1);
729             float fscols = static_cast<float>(std::max(src.cols - 1 + n, 0)),
730                     fsrows = static_cast<float>(std::max(src.rows - 1 + n, 0));
731             MatIterator_<float> begin_x = mapx.begin<float>(), end_x = mapx.end<float>();
732             MatIterator_<float> begin_y = mapy.begin<float>();
733             for ( ; begin_x != end_x; ++begin_x, ++begin_y)
734             {
735                 begin_x[0] = rng.uniform(_n, fscols);
736                 begin_y[0] = rng.uniform(_n, fsrows);
737             }
738         }
739         break;
740
741         case CV_32FC2:
742         {
743             MatIterator_<Vec2f> begin_x = mapx.begin<Vec2f>(), end_x = mapx.end<Vec2f>();
744             float fscols = static_cast<float>(std::max(src.cols - 1 + n, 0)),
745                     fsrows = static_cast<float>(std::max(src.rows - 1 + n, 0));
746             for ( ; begin_x != end_x; ++begin_x)
747             {
748                 begin_x[0] = rng.uniform(_n, fscols);
749                 begin_x[1] = rng.uniform(_n, fsrows);
750             }
751         }
752         break;
753     }
754 }
755
756 void CV_Remap_Test::run_func()
757 {
758     remap(src, dst, mapx, mapy, interpolation, borderType, borderValue);
759 }
760
761 void CV_Remap_Test::convert_maps()
762 {
763     if (mapx.type() == mapy.type() && mapx.type() == CV_32FC1)
764     {
765         Mat maps[] = { mapx, mapy };
766         Mat dst_map;
767         merge(maps, sizeof(maps) / sizeof(Mat), dst_map);
768         mapx = dst_map;
769     }
770     else if (interpolation == INTER_NEAREST && mapx.type() == CV_16SC2)
771     {
772         Mat tmp_mapx;
773         mapx.convertTo(tmp_mapx, CV_32F);
774         mapx = tmp_mapx;
775         mapy.release();
776         return;
777     }
778     else if (mapx.type() != CV_32FC2)
779     {
780         Mat out_mapy;
781         convertMaps(mapx.clone(), mapy, mapx, out_mapy, CV_32FC2, interpolation == INTER_NEAREST);
782     }
783
784     mapy.release();
785 }
786
787 void CV_Remap_Test::prepare_test_data_for_reference_func()
788 {
789     convert_maps();
790
791     if (src.depth() != CV_32F)
792     {
793         Mat _src;
794         src.convertTo(_src, CV_32F);
795         src = _src;
796     }
797         
798 /*
799     const int ksize = 3;
800     Mat kernel = getStructuringElement(CV_MOP_ERODE, Size(ksize, ksize));
801     Mat mask(src.size(), CV_8UC1, Scalar::all(255)), dst_mask;
802     cv::erode(src, erode_src, kernel);
803     cv::erode(mask, dst_mask, kernel, Point(-1, -1), 1, BORDER_CONSTANT, Scalar::all(0));
804     bitwise_not(dst_mask, mask);
805     src.copyTo(erode_src, mask);
806     dst_mask.release();
807
808     mask = Scalar::all(0);
809     kernel = getStructuringElement(CV_MOP_DILATE, kernel.size());
810     cv::dilate(src, dilate_src, kernel);
811     cv::dilate(mask, dst_mask, kernel, Point(-1, -1), 1, BORDER_CONSTANT, Scalar::all(255));
812     src.copyTo(dilate_src, dst_mask);
813     dst_mask.release();
814 */
815
816         dilate_src = src;
817     erode_src = src;
818
819     dilate_dst = Mat::zeros(dst.size(), dilate_src.type());
820     erode_dst = Mat::zeros(dst.size(), erode_src.type());
821 }
822
823 void CV_Remap_Test::run_reference_func()
824 {
825     prepare_test_data_for_reference_func();
826
827     if (interpolation == INTER_AREA)
828         interpolation = INTER_LINEAR;
829     CV_Assert(interpolation != INTER_AREA);
830
831     int index = interpolation == INTER_NEAREST ? 0 : 1;
832     (this->*funcs[index])(erode_src, erode_dst);
833     (this->*funcs[index])(dilate_src, dilate_dst);
834 }
835
836 void CV_Remap_Test::remap_nearest(const Mat& _src, Mat& _dst)
837 {
838     CV_Assert(_src.depth() == CV_32F && _dst.type() == _src.type());
839     CV_Assert(mapx.type() == CV_32FC2);
840
841     Size ssize = _src.size(), dsize = _dst.size();
842     CV_Assert(ssize.area() > 0 && dsize.area() > 0);
843     int cn = _src.channels();
844
845     for (int dy = 0; dy < dsize.height; ++dy)
846     {
847         const float* yM = mapx.ptr<float>(dy);
848         float* yD = _dst.ptr<float>(dy);
849         
850         for (int dx = 0; dx < dsize.width; ++dx)
851         {
852             float* xyD = yD + cn * dx;
853             int sx = cvRound(yM[dx * 2]), sy = cvRound(yM[dx * 2 + 1]);
854
855             if (sx >= 0 && sx < ssize.width && sy >= 0 && sy < ssize.height)
856             {
857                 const float *S = _src.ptr<float>(sy) + sx * cn;
858
859                 for (int r = 0; r < cn; ++r)
860                     xyD[r] = S[r];
861             }
862             else if (borderType != BORDER_TRANSPARENT)
863             {
864                 if (borderType == BORDER_CONSTANT)
865                     for (int r = 0; r < cn; ++r)
866                         xyD[r] = (float)borderValue[r];
867                 else
868                 {
869                     sx = borderInterpolate(sx, ssize.width, borderType);
870                     sy = borderInterpolate(sy, ssize.height, borderType);
871                     CV_Assert(sx >= 0 && sy >= 0 && sx < ssize.width && sy < ssize.height);
872
873                     const float *S = _src.ptr<float>(sy) + sx * cn;
874
875                     for (int r = 0; r < cn; ++r)
876                         xyD[r] = S[r];
877                 }
878             }
879         }
880     }
881 }
882
883 void CV_Remap_Test::remap_generic(const Mat& _src, Mat& _dst)
884 {
885     int ksize = 2;
886     if (interpolation == INTER_CUBIC)
887         ksize = 4;
888     else if (interpolation == INTER_LANCZOS4)
889         ksize = 8;
890     int ofs = (ksize / 2) - 1;
891     
892     CV_Assert(_src.depth() == CV_32F && _dst.type() == _src.type());
893     CV_Assert(mapx.type() == CV_32FC2);
894
895     Size ssize = _src.size(), dsize = _dst.size();
896     int cn = _src.channels(), width1 = std::max(ssize.width - ksize / 2, 0), height1 = std::max(ssize.height - ksize / 2, 0);
897
898     float ix[8], w[16];
899     interpolate_method inter_func = inter_array[interpolation - (interpolation == INTER_LANCZOS4 ? 2 : 1)];
900
901     for (int dy = 0; dy < dsize.height; ++dy)
902     {
903         const float* yM = mapx.ptr<float>(dy);
904         float* yD = _dst.ptr<float>(dy);
905         
906         for (int dx = 0; dx < dsize.width; ++dx)
907         {
908             float* xyD = yD + dx * cn;
909             float sx = yM[dx * 2], sy = yM[dx * 2 + 1];
910             int isx = cvFloor(sx), isy = cvFloor(sy);
911
912             float fsx = sx - isx, fsy = sy - isy;
913             inter_func(fsx, w);
914             inter_func(fsy, w + ksize);
915
916             if (isx >= ofs && isx < width1 && isy >= ofs && isy < height1)
917             {
918                 for (int r = 0; r < cn; ++r)
919                 {
920                     for (int y = 0; y < ksize; ++y)
921                     {
922                         const float* xyS = _src.ptr<float>(isy + y - ofs) + isx * cn;
923
924                         ix[y] = 0;
925                         for (int i = 0; i < ksize; ++i)
926                             ix[y] += w[i] * xyS[i * cn + r];
927                     }
928
929                     xyD[r] = 0;
930                     for (int i = 0; i < ksize; ++i)
931                         xyD[r] += w[ksize + i] * ix[i];
932                 }
933             }
934             else if (borderType != BORDER_TRANSPARENT)
935             {
936                 if (borderType == BORDER_CONSTANT &&
937                         (isx >= ssize.width || isx + ksize <= 0 ||
938                         isy >= ssize.height || isy + ksize <= 0))
939                     for (int r = 0; r < cn; ++r)
940                         xyD[r] = (float)borderValue[r];
941                 else
942                 {
943                     int ar_x[8], ar_y[8];
944                   
945                     for(int k = 0; k < ksize; k++ )
946                     {
947                         ar_x[k] = borderInterpolate(isx + k - ofs, ssize.width, borderType) * cn;
948                         ar_y[k] = borderInterpolate(isy + k - ofs, ssize.height, borderType);
949                         
950                         CV_Assert(ar_x[k] < ssize.width * cn && ar_y[k] < ssize.height);
951                     }
952
953                     for (int r = 0; r < cn; r++)
954                     {
955 //                         if (interpolation == INTER_LINEAR)
956                         {
957                             xyD[r] = 0;
958                             for (int i = 0; i < ksize; ++i)
959                             {
960                                 ix[i] = 0;
961                                 if (ar_y[i] >= 0)
962                                 {
963                                     const float* yS = _src.ptr<float>(ar_y[i]);
964                                     for (int j = 0; j < ksize; ++j)
965                                         if (ar_x[j] >= 0)
966                                         {
967                                             CV_Assert(ar_x[j] < ssize.width * cn);
968                                             ix[i] += yS[ar_x[j] + r] * w[j];
969                                         }
970                                         else
971                                             ix[i] += borderValue[r] * w[j];
972                                 }
973                                 else
974                                     for (int j = 0; j < ksize; ++j)
975                                         ix[i] += borderValue[r] * w[j];
976                             }
977                             for (int i = 0; i < ksize; ++i)
978                                 xyD[r] += w[ksize + i] * ix[i];
979                         }
980 //                         else
981 //                         {
982 //                             int ONE = 1;
983 //                             if (src.elemSize() == 4)
984 //                                 ONE <<= 15;
985 //                         
986 //                             float cv = borderValue[r], sum = cv * ONE;
987 //                             for (int i = 0; i < ksize; ++i)
988 //                             {
989 //                                 int yi = ar_y[i];
990 //                                 if (yi < 0)
991 //                                     continue;
992 //                                 const float* S1 = _src.ptr<float>(ar_y[i]);
993 //                                 for (int j = 0; j < ksize; ++j)
994 //                                     if( ar_x[j] >= 0 )
995 //                                         sum += (S1[ar_x[j] + r] - cv) * w[j];
996 //                             }
997 //                             xyD[r] = sum;
998 //                         }
999                     }
1000                 }
1001             }
1002         }
1003     }
1004 }
1005
1006 void CV_Remap_Test::validate_results() const
1007 {
1008     Mat _dst;
1009     dst.convertTo(_dst, CV_32F);
1010     
1011     Size dsize = _dst.size(), ssize = src.size();
1012     dsize.width *= _dst.channels();
1013
1014     CV_Assert(_dst.size() == erode_dst.size() && dilate_dst.size() == _dst.size());
1015     CV_Assert(dilate_dst.type() == _dst.type() && _dst.type() == erode_dst.type());
1016     
1017     for (int y = 0; y < dsize.height; ++y)
1018     {
1019         const float* D = _dst.ptr<float>(y);
1020         const float* dD = dilate_dst.ptr<float>(y);
1021         const float* eD = erode_dst.ptr<float>(y);
1022         dD = eD;
1023         
1024         float t = 1.0f;
1025         for (int x = 0; x < dsize.width; ++x)
1026             if ( !(eD[x] - t <= D[x] && D[x] <= dD[x] + t) )
1027             {
1028                 PRINT_TO_LOG("\nnorm(erode_dst, dst): %lf\n", norm(erode_dst, _dst, NORM_INF));
1029                 PRINT_TO_LOG("norm(dst, dilate_dst): %lf\n", norm(_dst, dilate_dst, NORM_INF));
1030                 PRINT_TO_LOG("(dx, dy): (%d, %d)\n", x / _dst.channels() + 1, 1 + y);
1031                 PRINT_TO_LOG("Values = (%f, %f, %f)\n", eD[x], D[x], dD[x]);
1032                 PRINT_TO_LOG("Interpolation: %s\n",
1033                     interpolation_to_string((interpolation | CV_WARP_INVERSE_MAP) ^ CV_WARP_INVERSE_MAP));
1034                 PRINT_TO_LOG("Ssize: (%d, %d)\n", ssize.width, ssize.height);
1035                 PRINT_TO_LOG("Dsize: (%d, %d)\n", _dst.cols, dsize.height);
1036                 PRINT_TO_LOG("BorderType: %s\n", borderType_to_string());
1037                 PRINT_TO_LOG("BorderValue: (%f, %f, %f, %f)\n",
1038                     borderValue[0], borderValue[1], borderValue[2], borderValue[3]);
1039                 
1040 #ifdef _SHOW_IMAGE
1041                 
1042                 std::string w1("Dst"), w2("Erode dst"), w3("Dilate dst"), w4("Diff erode"), w5("Diff dilate");
1043                 
1044                 cv::namedWindow(w1, CV_WINDOW_AUTOSIZE);
1045                 cv::namedWindow(w2, CV_WINDOW_AUTOSIZE);
1046                 cv::namedWindow(w3, CV_WINDOW_AUTOSIZE);
1047                 cv::namedWindow(w4, CV_WINDOW_AUTOSIZE);
1048                 cv::namedWindow(w5, CV_WINDOW_AUTOSIZE);
1049                 
1050                 Mat diff_dilate, diff_erode;
1051                 absdiff(_dst, erode_dst, diff_erode);
1052                 absdiff(_dst, dilate_dst, diff_dilate);
1053
1054                 cv::imshow(w1, dst / 255.);
1055                 cv::imshow(w2, erode_dst / 255.);
1056                 cv::imshow(w3, dilate_dst / 255.);
1057                 cv::imshow(w4, erode_dst / 255.);
1058                 cv::imshow(w5, dilate_dst / 255.);
1059
1060                 cv::waitKey();
1061                 
1062 #endif
1063                 
1064                 /*
1065                  const int radius = 3;
1066                  int rmin = MAX(y - radius, 0), rmax = MIN(y + radius, dsize.height);
1067                  int cmin = MAX(x - radius, 0), cmax = MIN(x + radius, dsize.width);
1068                  
1069                  cout << "src:\n" << src(Range(rmin, rmax), Range(cmin, cmax)) << endl;
1070                  cout << "opencv result:\n" << dst(Range(rmin, rmax), Range(cmin, cmax)) << endl << std::endl;
1071                  cout << "erode src:\n" << erode_src(Range(rmin, rmax), Range(cmin, cmax)) << endl;
1072                  cout << "erode result:\n" << dilate_dst(Range(rmin, rmax), Range(cmin, cmax)) << endl << std::endl;
1073                  cout << "dilate src:\n" << dilate_src(Range(rmin, rmax), Range(cmin, cmax)) << endl;
1074                  cout << "dilate result:\n" << dilate_dst(Range(rmin, rmax), Range(cmin, cmax)) << endl << std::endl;
1075                  */
1076
1077                 ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
1078                 return;
1079             }
1080     }
1081 }
1082
1083 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1084 // warpAffine
1085 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1086
1087 class CV_WarpAffine_Test :
1088     public CV_Remap_Test
1089 {
1090 public:
1091     CV_WarpAffine_Test();
1092
1093     virtual ~CV_WarpAffine_Test();
1094
1095 protected:
1096     virtual void generate_test_data();
1097
1098     virtual void run_func();
1099     virtual void run_reference_func();
1100
1101     Mat M;
1102 private:
1103     void warpAffine(const Mat&, Mat&);
1104 };
1105
1106 CV_WarpAffine_Test::CV_WarpAffine_Test() :
1107     CV_Remap_Test()
1108 {
1109 }
1110
1111 CV_WarpAffine_Test::~CV_WarpAffine_Test()
1112 {
1113 }
1114
1115 void CV_WarpAffine_Test::generate_test_data()
1116 {
1117     CV_Remap_Test::generate_test_data();
1118     CV_Remap_Test::prepare_test_data_for_reference_func();
1119     
1120     if (src.depth() != CV_32F)
1121     {
1122         Mat tmp;
1123         src.convertTo(tmp, CV_32F);
1124         src = tmp;
1125     }
1126
1127     RNG& rng = ts->get_rng();
1128
1129     // generating the M 2x3 matrix
1130     static const int depths[] = { CV_32FC1, CV_64FC1 };
1131
1132     // generating 2d matrix
1133     M = getRotationMatrix2D(Point2f(src.cols / 2.f, src.rows / 2.f),
1134         rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f));
1135     int depth = depths[rng.uniform(0, sizeof(depths) / sizeof(depths[0]))];
1136     if (M.depth() != depth)
1137     {
1138         Mat tmp;
1139         M.convertTo(tmp, depth);
1140         M = tmp;
1141     }
1142     
1143     // warp_matrix is inverse
1144     if (rng.uniform(0., 1.) > 0)
1145         interpolation |= CV_WARP_INVERSE_MAP;
1146 }
1147
1148 void CV_WarpAffine_Test::run_func()
1149 {
1150     cv::warpAffine(src, dst, M, dst.size(), interpolation, borderType, borderValue);
1151 }
1152
1153 void CV_WarpAffine_Test::run_reference_func()
1154 {
1155     CV_Remap_Test::prepare_test_data_for_reference_func();
1156
1157     warpAffine(erode_src, erode_dst);
1158     warpAffine(dilate_src, dilate_dst);
1159 }
1160
1161 void CV_WarpAffine_Test::warpAffine(const Mat& _src, Mat& _dst)
1162 {
1163     Size dsize = _dst.size();
1164
1165     CV_Assert(_src.size().area() > 0 && dsize.area() > 0);
1166     CV_Assert(_src.type() == _dst.type());
1167
1168     Mat tM;
1169     M.convertTo(tM, CV_64F);
1170     
1171     int inter = interpolation & INTER_MAX;
1172     if (inter == INTER_AREA)
1173         inter = INTER_LINEAR;
1174
1175     mapx.create(dsize, CV_16SC2);
1176     if (inter != INTER_NEAREST)
1177         mapy.create(dsize, CV_16SC1);
1178         
1179     if (!(interpolation & CV_WARP_INVERSE_MAP))
1180         invertAffineTransform(tM.clone(), tM);
1181     
1182     const int AB_BITS = MAX(10, (int)INTER_BITS);
1183     const int AB_SCALE = 1 << AB_BITS;  
1184     int round_delta = (inter == INTER_NEAREST) ? AB_SCALE / 2 : (AB_SCALE / INTER_TAB_SIZE / 2);
1185     
1186     const double* data_tM = tM.ptr<double>(0);
1187     for (int dy = 0; dy < dsize.height; ++dy)
1188     {
1189         short* yM = mapx.ptr<short>(dy);
1190         for (int dx = 0; dx < dsize.width; ++dx, yM += 2)
1191         {   
1192             int v1 = saturate_cast<int>(saturate_cast<int>(data_tM[0] * dx * AB_SCALE) + 
1193                     saturate_cast<int>((data_tM[1] * dy + data_tM[2]) * AB_SCALE) + round_delta), 
1194                    v2 = saturate_cast<int>(saturate_cast<int>(data_tM[3] * dx * AB_SCALE) + 
1195                     saturate_cast<int>((data_tM[4] * dy + data_tM[5]) * AB_SCALE) + round_delta);
1196             v1 >>= AB_BITS - INTER_BITS;
1197             v2 >>= AB_BITS - INTER_BITS;
1198
1199             yM[0] = saturate_cast<short>(v1 >> INTER_BITS);
1200             yM[1] = saturate_cast<short>(v2 >> INTER_BITS);
1201             
1202             if (inter != INTER_NEAREST)
1203                 mapy.ptr<short>(dy)[dx] = ((v2 & (INTER_TAB_SIZE - 1)) * INTER_TAB_SIZE + (v1 & (INTER_TAB_SIZE - 1)));
1204         }
1205     }
1206     
1207     cv::remap(_src, _dst, mapx, mapy, inter, borderType, borderValue);
1208 }
1209
1210 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1211 // warpPerspective
1212 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1213
1214 class CV_WarpPerspective_Test :
1215     public CV_WarpAffine_Test
1216 {
1217 public:
1218     CV_WarpPerspective_Test();
1219
1220     virtual ~CV_WarpPerspective_Test();
1221
1222 protected:
1223     virtual void generate_test_data();
1224
1225     virtual void run_func();
1226     virtual void run_reference_func();
1227
1228 private:
1229     void warpPerspective(const Mat&, Mat&);
1230 };
1231
1232 CV_WarpPerspective_Test::CV_WarpPerspective_Test() :
1233     CV_WarpAffine_Test()
1234 {
1235 }
1236
1237 CV_WarpPerspective_Test::~CV_WarpPerspective_Test()
1238 {
1239 }
1240
1241 void CV_WarpPerspective_Test::generate_test_data()
1242 {
1243     CV_Remap_Test::generate_test_data();
1244
1245     // generating the M 3x3 matrix
1246     RNG& rng = ts->get_rng();
1247
1248     Point2f sp[] = { Point(0, 0), Point(src.cols, 0), Point(0, src.rows), Point(src.cols, src.rows) };
1249     Point2f dp[] = { Point(rng.uniform(0, src.cols), rng.uniform(0, src.rows)),
1250         Point(rng.uniform(0, src.cols), rng.uniform(0, src.rows)),
1251         Point(rng.uniform(0, src.cols), rng.uniform(0, src.rows)),
1252         Point(rng.uniform(0, src.cols), rng.uniform(0, src.rows)) };
1253     M = getPerspectiveTransform(sp, dp);
1254
1255     static const int depths[] = { CV_32F, CV_64F };
1256     int depth = depths[rng.uniform(0, 2)];
1257     M.clone().convertTo(M, depth);
1258 }
1259
1260 void CV_WarpPerspective_Test::run_func()
1261 {
1262     cv::warpPerspective(src, dst, M, dst.size(), interpolation, borderType, borderValue);
1263 }
1264
1265 void CV_WarpPerspective_Test::run_reference_func()
1266 {
1267     CV_Remap_Test::prepare_test_data_for_reference_func();
1268
1269     warpPerspective(erode_src, erode_dst);
1270     warpPerspective(dilate_src, dilate_dst);
1271 }
1272
1273 void CV_WarpPerspective_Test::warpPerspective(const Mat& _src, Mat& _dst)
1274 {
1275     Size ssize = _src.size(), dsize = _dst.size();
1276
1277     CV_Assert(ssize.area() > 0 && dsize.area() > 0);
1278     CV_Assert(_src.type() == _dst.type());
1279
1280         if (M.depth() != CV_64F)
1281         {
1282                 Mat tmp;
1283                 M.convertTo(tmp, CV_64F);
1284                 M = tmp;
1285         }
1286         
1287     if (!(interpolation & CV_WARP_INVERSE_MAP))
1288     {
1289         Mat tmp;
1290         invert(M, tmp);
1291         M = tmp;
1292     }
1293     
1294     int inter = interpolation & INTER_MAX;
1295     if (inter == INTER_AREA)
1296         inter = INTER_LINEAR;
1297     
1298     mapx.create(dsize, CV_16SC2);
1299     if (inter != INTER_NEAREST)
1300         mapy.create(dsize, CV_16SC1);
1301
1302     double* tM = M.ptr<double>(0);
1303     for (int dy = 0; dy < dsize.height; ++dy)
1304     {
1305         short* yMx = mapx.ptr<short>(dy);
1306         
1307         for (int dx = 0; dx < dsize.width; ++dx, yMx += 2)
1308         {
1309             double den = tM[6] * dx + tM[7] * dy + tM[8];
1310             den = den ? 1.0 / den : 0.0;
1311             
1312             if (inter == INTER_NEAREST)
1313             {
1314                 yMx[0] = saturate_cast<short>((tM[0] * dx + tM[1] * dy + tM[2]) * den);
1315                 yMx[1] = saturate_cast<short>((tM[3] * dx + tM[4] * dy + tM[5]) * den);
1316                 continue;
1317             }
1318             
1319             den *= INTER_TAB_SIZE;
1320             int v0 = saturate_cast<int>((tM[0] * dx + tM[1] * dy + tM[2]) * den);
1321             int v1 = saturate_cast<int>((tM[3] * dx + tM[4] * dy + tM[5]) * den);
1322             
1323             yMx[0] = saturate_cast<short>(v0 >> INTER_BITS);
1324             yMx[1] = saturate_cast<short>(v1 >> INTER_BITS);
1325             mapy.ptr<short>(dy)[dx] = saturate_cast<short>((v1 & (INTER_TAB_SIZE - 1)) * 
1326                     INTER_TAB_SIZE + (v0 & (INTER_TAB_SIZE - 1)));
1327         }
1328     }
1329     
1330     cv::remap(_src, _dst, mapx, mapy, inter, borderType, borderValue);
1331 }
1332
1333 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1334 // Tests
1335 ////////////////////////////////////////////////////////////////////////////////////////////////////////
1336
1337 TEST(Imgproc_Resize_Test, accuracy) { CV_Resize_Test test; test.safe_run(); }
1338 // TEST(Imgproc_Remap_Test, accuracy) { CV_Remap_Test test; test.safe_run(); }
1339 TEST(Imgproc_WarpAffine_Test, accuracy) { CV_WarpAffine_Test test; test.safe_run(); }
1340 TEST(Imgproc_WarpPerspective_Test, accuracy) { CV_WarpPerspective_Test test; test.safe_run(); }