From e31ff001042ea2321c3d67266a8d240dc836ecd2 Mon Sep 17 00:00:00 2001 From: Pierre Chatelier Date: Thu, 7 Aug 2025 16:19:55 +0200 Subject: [PATCH] Merge pull request #27599 from chacha21:drawing_stack_alloc optimize some drawing with stack allocation #27599 Some drawings can try a stack allocation instead of a std::vector ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [X] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/imgproc/src/drawing.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/imgproc/src/drawing.cpp b/modules/imgproc/src/drawing.cpp index d0368cfa95..e3158750c5 100644 --- a/modules/imgproc/src/drawing.cpp +++ b/modules/imgproc/src/drawing.cpp @@ -2034,9 +2034,12 @@ void fillPoly( InputOutputArray _img, const Point** pts, const int* npts, int nc edges.reserve( total + 1 ); for (i = 0; i < ncontours; i++) { - if (npts[i] > 0 && pts[i]) + const Point* currentContour = pts[i]; + const int currentContourLength = npts[i]; + if ( (currentContourLength > 0) && currentContour ) { - std::vector _pts(pts[i], pts[i] + npts[i]); + AutoBuffer _pts(currentContourLength); + std::copy(currentContour, currentContour+currentContourLength, _pts.data()); CollectPolyEdges(img, _pts.data(), npts[i], edges, buf, line_type, shift, offset); } } @@ -2063,8 +2066,11 @@ void polylines( InputOutputArray _img, const Point* const* pts, const int* npts, for( int i = 0; i < ncontours; i++ ) { - std::vector _pts(pts[i], pts[i]+npts[i]); - PolyLine( img, _pts.data(), npts[i], isClosed, buf, thickness, line_type, shift ); + const Point* currentContour = pts[i]; + const int currentContourLength = npts[i]; + AutoBuffer _pts(currentContourLength); + std::copy(currentContour, currentContour+currentContourLength, _pts.data()); + PolyLine( img, _pts.data(), currentContourLength, isClosed, buf, thickness, line_type, shift ); } }