nareshmlx/opencv-pr huggingface dataset:
**File:** modules/ts/src/ts_perf.cpp
**Change Type:** modified
**Context:** PR #4064: Adding test support for WINRT - Adding test support for Windows RT. Some unused highgui #include's were removed ([sample](https://github.com...
**Review Line:** 672

**Code Changes:**
```diff
@@ -185,7 +185,11 @@ void Regression::init(const std::string& testSuitName, const std::string& ext)
         return;
     }
 
+#ifndef WINRT
     const char *data_path_dir = getenv("OPENCV_TEST_DATA_PATH");
+#else
+    const char *data_path_dir = OPENCV_TEST_DATA_PATH;
+#endif
     const char *path_separator = "/";
 
     if (data_path_dir)
@@ -814,7 +818,12 @@ void TestBase::Init(const std::vector<std::string> & availableImpls,
     param_force_samples = args.get<unsigned int>("perf_force_samples");
     param_write_sanity  = args.has("perf_write_sanity");
```

Review comment:
Please find detailed description of reasons of these updates [there](https://github.com/Itseez/opencv/pull/4064#issuecomment-107431788) I can suggest placing of full function body into `#ifdef` blocks there for now. This supposes some duplication of code however separates WinRT and non-WinRT code to make this part more maintainable.
**File:** modules/ts/src/ts.cpp
**Change Type:** modified
**Context:** PR #4064: Adding test support for WINRT - Adding test support for Windows RT. Some unused highgui #include's were removed ([sample](https://github.com...
**Review Line:** 692

**Code Changes:**
```diff
@@ -684,7 +688,11 @@ void parseCustomOptions(int argc, char **argv)
 
     test_ipp_check = parser.get<bool>("test_ipp_check");
     if (!test_ipp_check)
+#ifndef WINRT
         test_ipp_check = getenv("OPENCV_IPP_CHECK") != NULL;
+#else
+        test_ipp_check = false;
+#endif
 }
 
```

Review comment:
Currently we are disabling APPCONTAINER on the postbuild step, so file system becomes available w/o standard WinRT restrictions. However using of OPENCV_TEST_DATA_PATH environment variable seems to be more common as no additional command line arguments and actions required if you already have OpenCV test environment configured on your machine. Could you please explain what do you mean under using of `--env MY_ENV_VAR=MY_ENV_VALUE` command line argument. Is it updating of GTest code to support `--env` (that would be likely undocumented in official GTest docs) and starting of every test executable with this key?
**File:** modules/ts/src/ts_perf.cpp
**Change Type:** modified
**Context:** PR #4064: Adding test support for WINRT - Adding test support for Windows RT. Some unused highgui #include's were removed ([sample](https://github.com...
**Review Line:** 672

**Code Changes:**
```diff
@@ -185,7 +185,11 @@ void Regression::init(const std::string& testSuitName, const std::string& ext)
         return;
     }
 
+#ifndef WINRT
     const char *data_path_dir = getenv("OPENCV_TEST_DATA_PATH");
+#else
+    const char *data_path_dir = OPENCV_TEST_DATA_PATH;
+#endif
     const char *path_separator = "/";
 
     if (data_path_dir)
@@ -814,7 +818,12 @@ void TestBase::Init(const std::vector<std::string> & availableImpls,
     param_force_samples = args.get<unsigned int>("perf_force_samples");
     param_write_sanity  = args.has("perf_write_sanity");
```

Review comment:
I mean something like this: https://github.com/alalek/opencv/commit/winrt_fstream_emulate (`ifstream`/`ostream` emulation via `fopen` and `stringstream`)
**File:** modules/ts/src/ts_gtest.cpp
**Change Type:** modified
**Context:** PR #4064: Adding test support for WINRT - Adding test support for Windows RT. Some unused highgui #include's were removed ([sample](https://github.com...
**Review Line:** 4057

**Code Changes:**
```diff
@@ -4054,7 +4054,7 @@ enum GTestColor {
   COLOR_YELLOW
 };
 
-#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE
+#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE && !defined WINRT
 
 // Returns the character attribute for the given color.
 WORD GetColorAttribute(GTestColor color) {
@@ -4122,7 +4122,7 @@ static void ColoredPrintf(GTestColor color, const char* fmt, ...) {
   va_list args;
```

Review comment:
Actually GTest 1.7.0 is the last released version and OpenCV uses it. OpenCV uses some patches for Google Test to: - fix build: eliminate warnings about incorrect handling of definitions / make some functions `static`; - add some functionality (filter by parameter value); - etc; I created update for Google Test: https://github.com/Itseez/opencv/pull/4157 . If it helps, we can try to merge it.
**File:** modules/ts/include/opencv2/ts.hpp
**Change Type:** modified
**Context:** PR #4064: Adding test support for WINRT - Adding test support for Windows RT. Some unused highgui #include's were removed ([sample](https://github.com...

**Code Changes:**
```diff
@@ -588,3 +588,102 @@ int main(int argc, char **argv) \
 #endif
 
 #include "opencv2/ts/ts_perf.hpp"
+
+#ifdef WINRT
+#ifndef __FSTREAM_EMULATED__
+#define __FSTREAM_EMULATED__
+#include <stdlib.h>
+#include <fstream>
```

Review comment:
Please use here: `mode = ios_base::in`. This will allow to remove the first constructor with duplicated code.
**File:** modules/core/src/copy.cpp
**Change Type:** modified
**Context:** PR #4159: Vector processing issue - Problem is in layout mismatch of `src` and `dst` for some cases: ``` OutputArray _dst(dst1); { _dst.create(src.row...

**Code Changes:**
```diff
@@ -285,6 +285,11 @@ void Mat::copyTo( OutputArray _dst ) const
 
         if( rows > 0 && cols > 0 )
         {
+            // For some cases (with vector) dst.size != src.size, so force to column-based form
+            // It prevents memory corruption in case of column-based src
+            if (_dst.isVector())
+                dst = dst.reshape(0, (int)dst.total());
+
             const uchar* sptr = data;
```

Review comment:
such call of reshape gives valid results as long as dst is 1xN before the call. I suggest to change it to more robust <pre> dst = dst.reshape(0, dst.cols*dst.rows); </pre>
**File:** modules/videoio/src/cap_mjpeg_encoder.cpp
**Change Type:** modified
**Context:** PR #4164: Parallel mjpeg encoder implementation based on previous single thread implementation.
**Review Line:** 1383

**Code Changes:**
```diff
+    {
+        if( colorspace == COLORSPACE_YUV444P && y_limit == 16 && x_limit == 16 )
+        {
+            for( i = 0; i < y_limit; i += 2, pix_data += step*2, Y_data += Y_step*2, UV_data += UV_step )
+            {
+#ifdef WITH_NEON
+                {
+                    uint16x8_t masklo = vdupq_n_u16(255);
+                    uint16x8_t lane = vld1q_u16((unsigned short*)(pix_data+v_plane_ofs));
+                    uint16x8_t t1 = vaddq_u16(vshrq_n_u16(lane, 8), vandq_u16(lane, masklo));
+                    lane = vld1q_u16((unsigned short*)(pix_data + v_plane_ofs + step));
```

Review comment:
why not universal intr. from HAL?
**File:** modules/python/src2/cv2.cpp
**Change Type:** modified
**Context:** PR #4117: BUG: ndarray to Mat conversion with NPY_RELAXED_STRIDES set - Add logic to avoid nonsense strides when dimension size is 1 and NumPy is buil...
**Review Line:** 353

**Code Changes:**
```diff
+    for ( int i = ndims - 1; i >= 0; --i )
     {
         size[i] = (int)_sizes[i];
-        step[i] = (size_t)_strides[i];
+        if ( size[i] > 1 )
+        {
+            step[i] = (size_t)_strides[i];
+            default_step = step[i] * size[i];
+        }
+        else
+        {
```

Review comment:
First branch seems identical to second one (just explicit copy instead of recalculating what is known to be right)? Assuming we know numpy said the array is contiguous.
**File:** modules/python/src2/cv2.cpp
**Change Type:** modified
**Context:** PR #4117: BUG: ndarray to Mat conversion with NPY_RELAXED_STRIDES set - Add logic to avoid nonsense strides when dimension size is 1 and NumPy is buil...
**Review Line:** 320

**Code Changes:**
```diff
         //  c) flipped arrays, where some of _strides[] elements are negative
-        if( (i == ndims-1 && (size_t)_strides[i] != elemsize) ||
-            (i < ndims-1 && _strides[i] < _strides[i+1]) )
+        // the _sizes[i] > 1 is needed to avoid spurious copies when NPY_RELAXED_STRIDES is set
+        if( (i == ndims-1 && _sizes[i] > 1 && (size_t)_strides[i] != elemsize) ||
+            (i < ndims-1 && _sizes[i] > 1 && _strides[i] < _strides[i+1]) )
             needcopy = true;
     }
 
@@ -343,10 +344,21 @@ static bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo info)
         _strides = PyArray_STRIDES(oarr);
```

Review comment:
Empty arrays may drop through here if opencv has those, not that there is much of a copy needed in that case ;).
**File:** modules/python/src2/cv2.cpp
**Change Type:** modified
**Context:** PR #4117: BUG: ndarray to Mat conversion with NPY_RELAXED_STRIDES set - Add logic to avoid nonsense strides when dimension size is 1 and NumPy is buil...
**Review Line:** 353

**Code Changes:**
```diff
+    for ( int i = ndims - 1; i >= 0; --i )
     {
         size[i] = (int)_sizes[i];
-        step[i] = (size_t)_strides[i];
+        if ( size[i] > 1 )
+        {
+            step[i] = (size_t)_strides[i];
+            default_step = step[i] * size[i];
+        }
+        else
+        {
```

Review comment:
Side Remark: I personally always think, if you know that your arrays are contiguous, just don't use stride information at all. To choke on the mangled up strides, the function has to use the strides, while at the same time assuming contiguity, either one is fine, but both is not great habit IMO.
**File:** modules/python/src2/cv2.cpp
**Change Type:** modified
**Context:** PR #4117: BUG: ndarray to Mat conversion with NPY_RELAXED_STRIDES set - Add logic to avoid nonsense strides when dimension size is 1 and NumPy is buil...
**Review Line:** 353

**Code Changes:**
```diff
+    for ( int i = ndims - 1; i >= 0; --i )
     {
         size[i] = (int)_sizes[i];
-        step[i] = (size_t)_strides[i];
+        if ( size[i] > 1 )
+        {
+            step[i] = (size_t)_strides[i];
+            default_step = step[i] * size[i];
+        }
+        else
+        {
```

Review comment:
I don't know if that is another error in OpenCV, but the current `needcopy` logic doesn't exclude a non-contiguous array, as it only requires non-increasing strides, so for something like: ``` >>> a = np.empty((10, 10, 3), dtype=np.int32) >>> a = a[::2, ::10] >>> a.strides (240, 120, 4) >>> a.shape (5, 1, 3) ``` I don't think you would have a copy made, so the original array strides have to be kept unchanged.
**File:** modules/imgproc/include/opencv2/imgproc.hpp
**Change Type:** modified
**Context:** PR #4139: spatialGradient (hal accelerated) - ``` void spatialGradient( InputArray src, OutputArray dx, OutputArray dy, int ksize ); ``` Equivalent in...
**Review Line:** 1393

**Code Changes:**
```diff
+ */
+
+CV_EXPORTS_W void spatialGradient( InputArray src, OutputArray dx,
+                                   OutputArray dy, int ksize = 3,
+                                   int borderType = BORDER_DEFAULT );
+
 /** @brief Calculates the first x- or y- image derivative using Scharr operator.
 
 The function computes the first x- or y- spatial image derivative using the Scharr operator. The
```

Review comment:
probably, there should also be parameter borderMode, and we will have to support at least BORDER_REPLICATE and BORDER_REFLECT_101 (=BORDER_DEFAULT)
**File:** modules/imgproc/src/spatialgradient.cpp
**Change Type:** added
**Context:** PR #4139: spatialGradient (hal accelerated) - ``` void spatialGradient( InputArray src, OutputArray dx, OutputArray dy, int ksize ); ``` Equivalent in...

**Code Changes:**
```diff
@@ -0,0 +1,329 @@
+/*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.
+//
+//
```

Review comment:
spatialGradient is important function, so we need to support non-continuous images as well. use image.ptr<datatype>(y) to get the pointer to y-th row
**File:** modules/imgproc/src/spatialgradient.cpp
**Change Type:** added
**Context:** PR #4139: spatialGradient (hal accelerated) - ``` void spatialGradient( InputArray src, OutputArray dx, OutputArray dy, int ksize ); ``` Equivalent in...

**Code Changes:**
```diff
@@ -0,0 +1,329 @@
+/*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.
+//
+//
```

Review comment:
same thing, non-continuous dx and dy should be supported
**File:** modules/imgproc/src/spatialgradient.cpp
**Change Type:** added
**Context:** PR #4139: spatialGradient (hal accelerated) - ``` void spatialGradient( InputArray src, OutputArray dx, OutputArray dy, int ksize ); ``` Equivalent in...

**Code Changes:**
```diff
@@ -0,0 +1,329 @@
+/*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.
+//
+//
```

Review comment:
if you store pointers to (y-1)-th, y-th and y-th row in local variables and take into account the border extrapolation in the case of 0-th and (H-1)-th row, then the loop can be from 0 to H and you will not need separate code to process the top and bottom rows
**File:** modules/imgproc/src/spatialgradient.cpp
**Change Type:** added
**Context:** PR #4139: spatialGradient (hal accelerated) - ``` void spatialGradient( InputArray src, OutputArray dx, OutputArray dy, int ksize ); ``` Equivalent in...

**Code Changes:**
```diff
@@ -0,0 +1,329 @@
+/*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.
+//
+//
```

Review comment:
please, move the left-most and the right-most pixel processing here, so that you have a single pass through the image
**File:** modules/imgproc/src/spatialgradient.cpp
**Change Type:** added
**Context:** PR #4139: spatialGradient (hal accelerated) - ``` void spatialGradient( InputArray src, OutputArray dx, OutputArray dy, int ksize ); ``` Equivalent in...

**Code Changes:**
```diff
@@ -0,0 +1,329 @@
+/*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.
+//
+//
```

Review comment:
instead of a separate C branch use the tail processing as the C branch
**File:** modules/imgproc/src/spatialgradient.cpp
**Change Type:** added
**Context:** PR #4139: spatialGradient (hal accelerated) - ``` void spatialGradient( InputArray src, OutputArray dx, OutputArray dy, int ksize ); ``` Equivalent in...

**Code Changes:**
```diff
@@ -0,0 +1,329 @@
+/*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.
+//
+//
```

Review comment:
replace v_two*a with a+a
**File:** modules/imgproc/src/spatialgradient.cpp
**Change Type:** added
**Context:** PR #4139: spatialGradient (hal accelerated) - ``` void spatialGradient( InputArray src, OutputArray dx, OutputArray dy, int ksize ); ``` Equivalent in...

**Code Changes:**
```diff
@@ -0,0 +1,329 @@
+/*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.
+//
+//
```

Review comment:
put left and the right border processing out of the inner loop. it's very big overhead. Do the vectorized loop from 1 to W - 16.
**File:** modules/imgproc/src/spatialgradient.cpp
**Change Type:** added
**Context:** PR #4139: spatialGradient (hal accelerated) - ``` void spatialGradient( InputArray src, OutputArray dx, OutputArray dy, int ksize ); ``` Equivalent in...

**Code Changes:**
```diff
@@ -0,0 +1,329 @@
+/*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.
+//
+//
```

Review comment:
do not use array for pointers. instead, use compute pointers on-fly, it's very little overhead
**File:** modules/imgproc/src/spatialgradient.cpp
**Change Type:** added
**Context:** PR #4139: spatialGradient (hal accelerated) - ``` void spatialGradient( InputArray src, OutputArray dx, OutputArray dy, int ksize ); ``` Equivalent in...

**Code Changes:**
```diff
@@ -0,0 +1,329 @@
+/*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.
+//
+//
```

Review comment:
move these variables inside the inner loop
**File:** modules/imgproc/src/spatialgradient.cpp
**Change Type:** added
**Context:** PR #4139: spatialGradient (hal accelerated) - ``` void spatialGradient( InputArray src, OutputArray dx, OutputArray dy, int ksize ); ``` Equivalent in...

**Code Changes:**
```diff
@@ -0,0 +1,329 @@
+/*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.
+//
+//
```

Review comment:
It's not much overhead, but since the border handling etc is done in both the SSE and non-SSE code, this would be somewhat duplicated. Would that still be okay? The vectors for caching pointers is only as long as the no. of rows so I thought it wouldn't have high impact.
**File:** modules/imgproc/src/spatialgradient.cpp
**Change Type:** added
**Context:** PR #4139: spatialGradient (hal accelerated) - ``` void spatialGradient( InputArray src, OutputArray dx, OutputArray dy, int ksize ); ``` Equivalent in...

**Code Changes:**
```diff
@@ -0,0 +1,329 @@
+/*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.
+//
+//
```

Review comment:
It's no longer an issue, I moved the pointer calls into the loops without impact in performance.
**File:** modules/core/include/opencv2/core/mat.inl.hpp
**Change Type:** modified
**Context:** PR #4094: Mat push back MatExpr bug 4340 - Mat::push_back does not handle MatExpr inputs as desribed in [Bug #4340](http://code.opencv.org/issues/4340...
**Review Line:** 1099

**Code Changes:**
```diff
@@ -1096,6 +1096,12 @@ void Mat::push_back(const Mat_<_Tp>& m)
     push_back((const Mat&)m);
 }
 
+template<> inline
+void Mat::push_back(const MatExpr& expr)
+{
+    push_back(static_cast<Mat>(expr));
+}
+
```

Review comment:
I believe, template&lt;&gt; is not needed here
**File:** modules/core/include/opencv2/core/mat.inl.hpp
**Change Type:** modified
**Context:** PR #4094: Mat push back MatExpr bug 4340 - Mat::push_back does not handle MatExpr inputs as desribed in [Bug #4340](http://code.opencv.org/issues/4340...
**Review Line:** 1099

**Code Changes:**
```diff
@@ -1096,6 +1096,12 @@ void Mat::push_back(const Mat_<_Tp>& m)
     push_back((const Mat&)m);
 }
 
+template<> inline
+void Mat::push_back(const MatExpr& expr)
+{
+    push_back(static_cast<Mat>(expr));
+}
+
```

Review comment:
It's needed in order to define it as a specialization of this method of the cv::Mat class: template<typename _Tp> void push_back(const _Tp& elem); Omitting template<> without adding an explicit overload in the class definition results in the compilation error: prototype for 'void cv::Mat::push_back(const cv::MatExpr&)' does not match any in class 'cv::Mat' candidates are... So we either add a specialization to the template overload of push_back() or add a new overload.
**File:** modules/core/include/opencv2/core/mat.inl.hpp
**Change Type:** modified
**Context:** PR #4094: Mat push back MatExpr bug 4340 - Mat::push_back does not handle MatExpr inputs as desribed in [Bug #4340](http://code.opencv.org/issues/4340...

**Code Changes:**
```diff
@@ -1096,6 +1096,12 @@ void Mat::push_back(const Mat_<_Tp>& m)
     push_back((const Mat&)m);
 }
 
+template<> inline
+void Mat::push_back(const MatExpr& expr)
+{
+    push_back(static_cast<Mat>(expr));
+}
+
```

Review comment:
when you cast a reference to another reference, it's like you cast pointer to another pointer. Use (Mat)expr instead of (const Mat&)expr. In order to verify it, in the test below make the c 2x2 instead of 2x1 (or use another expression, not the transposition). Now it looks like the function works incorrectly in most cases
**File:** modules/core/include/opencv2/core/mat.inl.hpp
**Change Type:** modified
**Context:** PR #4094: Mat push back MatExpr bug 4340 - Mat::push_back does not handle MatExpr inputs as desribed in [Bug #4340](http://code.opencv.org/issues/4340...

**Code Changes:**
```diff
@@ -1096,6 +1096,12 @@ void Mat::push_back(const Mat_<_Tp>& m)
     push_back((const Mat&)m);
 }
 
+template<> inline
+void Mat::push_back(const MatExpr& expr)
+{
+    push_back(static_cast<Mat>(expr));
+}
+
```

Review comment:
Done. Appreciate the feedback. Do you think this should also be done for "(const Mat&)m" in the other push_back overload (line 1096 of the same file)? The cast there is from a const Mat_<_Tp>& to const Mat&.
**File:** modules/core/include/opencv2/core/mat.inl.hpp
**Change Type:** modified
**Context:** PR #4094: Mat push back MatExpr bug 4340 - Mat::push_back does not handle MatExpr inputs as desribed in [Bug #4340](http://code.opencv.org/issues/4340...

**Code Changes:**
```diff
@@ -1096,6 +1096,12 @@ void Mat::push_back(const Mat_<_Tp>& m)
     push_back((const Mat&)m);
 }
 
+template<> inline
+void Mat::push_back(const MatExpr& expr)
+{
+    push_back(static_cast<Mat>(expr));
+}
+
```

Review comment:
nope. Mat_&lt;_Tp&gt; and Mat are binary identical, so the reference cast works just fine (and it's definitely faster)