Commit c197a46e authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

removed rarely used fixed_size parameter from AutoBuffer type, added optional…

removed rarely used fixed_size parameter from AutoBuffer type, added optional AutoBuffer* but to cvarrToMat in order to speedup CvSeq->Mat conversion; finished conversion of convex hull and related functions to C++
parent 457fa521
...@@ -109,6 +109,8 @@ template<typename _Tp> class CV_EXPORTS MatIterator_; ...@@ -109,6 +109,8 @@ template<typename _Tp> class CV_EXPORTS MatIterator_;
template<typename _Tp> class CV_EXPORTS MatConstIterator_; template<typename _Tp> class CV_EXPORTS MatConstIterator_;
template<typename _Tp> class CV_EXPORTS MatCommaInitializer_; template<typename _Tp> class CV_EXPORTS MatCommaInitializer_;
template<typename _Tp> class CV_EXPORTS AutoBuffer;
CV_EXPORTS string format( const char* fmt, ... ); CV_EXPORTS string format( const char* fmt, ... );
CV_EXPORTS string tempfile( const char* suffix CV_DEFAULT(0)); CV_EXPORTS string tempfile( const char* suffix CV_DEFAULT(0));
...@@ -2061,7 +2063,8 @@ CV_EXPORTS void swap(Mat& a, Mat& b); ...@@ -2061,7 +2063,8 @@ CV_EXPORTS void swap(Mat& a, Mat& b);
//! converts array (CvMat or IplImage) to cv::Mat //! converts array (CvMat or IplImage) to cv::Mat
CV_EXPORTS Mat cvarrToMat(const CvArr* arr, bool copyData=false, CV_EXPORTS Mat cvarrToMat(const CvArr* arr, bool copyData=false,
bool allowND=true, int coiMode=0); bool allowND=true, int coiMode=0,
AutoBuffer<double>* buf=0);
//! extracts Channel of Interest from CvMat or IplImage and makes cv::Mat out of it. //! extracts Channel of Interest from CvMat or IplImage and makes cv::Mat out of it.
CV_EXPORTS void extractImageCOI(const CvArr* arr, OutputArray coiimg, int coi=-1); CV_EXPORTS void extractImageCOI(const CvArr* arr, OutputArray coiimg, int coi=-1);
//! inserts single-channel cv::Mat into a multi-channel CvMat or IplImage //! inserts single-channel cv::Mat into a multi-channel CvMat or IplImage
...@@ -3081,7 +3084,7 @@ public: ...@@ -3081,7 +3084,7 @@ public:
\code \code
void my_func(const cv::Mat& m) void my_func(const cv::Mat& m)
{ {
cv::AutoBuffer<float, 1000> buf; // create automatic buffer containing 1000 floats cv::AutoBuffer<float> buf; // create automatic buffer containing 1000 floats
buf.allocate(m.rows); // if m.rows <= 1000, the pre-allocated buffer is used, buf.allocate(m.rows); // if m.rows <= 1000, the pre-allocated buffer is used,
// otherwise the buffer of "m.rows" floats will be allocated // otherwise the buffer of "m.rows" floats will be allocated
...@@ -3090,16 +3093,22 @@ public: ...@@ -3090,16 +3093,22 @@ public:
} }
\endcode \endcode
*/ */
template<typename _Tp, size_t fixed_size=4096/sizeof(_Tp)+8> class CV_EXPORTS AutoBuffer template<typename _Tp> class CV_EXPORTS AutoBuffer
{ {
public: public:
typedef _Tp value_type; typedef _Tp value_type;
enum { buffer_padding = (int)((16 + sizeof(_Tp) - 1)/sizeof(_Tp)) }; enum { fixed_size = 1024/sizeof(_Tp)+8, buffer_padding = (int)((16 + sizeof(_Tp) - 1)/sizeof(_Tp)) };
//! the default contructor //! the default contructor
AutoBuffer(); AutoBuffer();
//! constructor taking the real buffer size //! constructor taking the real buffer size
AutoBuffer(size_t _size); AutoBuffer(size_t _size);
//! the copy constructor
AutoBuffer(const AutoBuffer<_Tp>& buf);
//! the assignment operator
AutoBuffer<_Tp>& operator = (const AutoBuffer<_Tp>& buf);
//! destructor. calls deallocate() //! destructor. calls deallocate()
~AutoBuffer(); ~AutoBuffer();
...@@ -4318,7 +4327,6 @@ public: ...@@ -4318,7 +4327,6 @@ public:
int index; int index;
}; };
class CV_EXPORTS Algorithm; class CV_EXPORTS Algorithm;
class CV_EXPORTS AlgorithmInfo; class CV_EXPORTS AlgorithmInfo;
struct CV_EXPORTS AlgorithmInfoData; struct CV_EXPORTS AlgorithmInfoData;
......
...@@ -2534,23 +2534,46 @@ inline Point LineIterator::pos() const ...@@ -2534,23 +2534,46 @@ inline Point LineIterator::pos() const
/////////////////////////////// AutoBuffer //////////////////////////////////////// /////////////////////////////// AutoBuffer ////////////////////////////////////////
template<typename _Tp, size_t fixed_size> inline AutoBuffer<_Tp, fixed_size>::AutoBuffer() template<typename _Tp> inline AutoBuffer<_Tp>::AutoBuffer()
{ {
ptr = buf; ptr = buf;
sz = fixed_size; sz = fixed_size;
} }
template<typename _Tp, size_t fixed_size> inline AutoBuffer<_Tp, fixed_size>::AutoBuffer(size_t _size) template<typename _Tp> inline AutoBuffer<_Tp>::AutoBuffer(size_t _size)
{ {
ptr = buf; ptr = buf;
sz = fixed_size; sz = fixed_size;
allocate(_size); allocate(_size);
} }
template<typename _Tp, size_t fixed_size> inline AutoBuffer<_Tp, fixed_size>::~AutoBuffer() template<typename _Tp>
inline AutoBuffer<_Tp>::AutoBuffer(const AutoBuffer<_Tp>& abuf )
{
ptr = buf;
sz = fixed_size;
allocate(abuf.size);
for( size_t i = 0; i < sz; i++ )
ptr[i] = abuf.ptr[i];
}
template<typename _Tp>
inline AutoBuffer<_Tp>& AutoBuffer<_Tp>::operator = (const AutoBuffer<_Tp>& abuf )
{
if( this != &abuf )
{
deallocate();
allocate(abuf.size);
for( size_t i = 0; i < sz; i++ )
ptr[i] = abuf.ptr[i];
}
return *this;
}
template<typename _Tp> inline AutoBuffer<_Tp>::~AutoBuffer()
{ deallocate(); } { deallocate(); }
template<typename _Tp, size_t fixed_size> inline void AutoBuffer<_Tp, fixed_size>::allocate(size_t _size) template<typename _Tp> inline void AutoBuffer<_Tp>::allocate(size_t _size)
{ {
if(_size <= sz) if(_size <= sz)
{ {
...@@ -2565,17 +2588,17 @@ template<typename _Tp, size_t fixed_size> inline void AutoBuffer<_Tp, fixed_size ...@@ -2565,17 +2588,17 @@ template<typename _Tp, size_t fixed_size> inline void AutoBuffer<_Tp, fixed_size
} }
} }
template<typename _Tp, size_t fixed_size> inline void AutoBuffer<_Tp, fixed_size>::deallocate() template<typename _Tp> inline void AutoBuffer<_Tp>::deallocate()
{ {
if( ptr != buf ) if( ptr != buf )
{ {
delete[] ptr; delete[] ptr;
ptr = buf; ptr = buf;
sz = 0; sz = fixed_size;
} }
} }
template<typename _Tp, size_t fixed_size> inline void AutoBuffer<_Tp, fixed_size>::resize(size_t _size) template<typename _Tp> inline void AutoBuffer<_Tp>::resize(size_t _size)
{ {
if(_size <= sz) if(_size <= sz)
{ {
...@@ -2598,13 +2621,13 @@ template<typename _Tp, size_t fixed_size> inline void AutoBuffer<_Tp, fixed_size ...@@ -2598,13 +2621,13 @@ template<typename _Tp, size_t fixed_size> inline void AutoBuffer<_Tp, fixed_size
delete[] prevptr; delete[] prevptr;
} }
template<typename _Tp, size_t fixed_size> inline size_t AutoBuffer<_Tp, fixed_size>::size() const template<typename _Tp> inline size_t AutoBuffer<_Tp>::size() const
{ return sz; } { return sz; }
template<typename _Tp, size_t fixed_size> inline AutoBuffer<_Tp, fixed_size>::operator _Tp* () template<typename _Tp> inline AutoBuffer<_Tp>::operator _Tp* ()
{ return ptr; } { return ptr; }
template<typename _Tp, size_t fixed_size> inline AutoBuffer<_Tp, fixed_size>::operator const _Tp* () const template<typename _Tp> inline AutoBuffer<_Tp>::operator const _Tp* () const
{ return ptr; } { return ptr; }
......
...@@ -1314,7 +1314,7 @@ cvMixChannels( const CvArr** src, int src_count, ...@@ -1314,7 +1314,7 @@ cvMixChannels( const CvArr** src, int src_count,
CvArr** dst, int dst_count, CvArr** dst, int dst_count,
const int* from_to, int pair_count ) const int* from_to, int pair_count )
{ {
cv::AutoBuffer<cv::Mat, 32> buf(src_count + dst_count); cv::AutoBuffer<cv::Mat> buf(src_count + dst_count);
int i; int i;
for( i = 0; i < src_count; i++ ) for( i = 0; i < src_count; i++ )
......
...@@ -669,7 +669,7 @@ void Mat::push_back(const Mat& elems) ...@@ -669,7 +669,7 @@ void Mat::push_back(const Mat& elems)
Mat cvarrToMat(const CvArr* arr, bool copyData, Mat cvarrToMat(const CvArr* arr, bool copyData,
bool /*allowND*/, int coiMode) bool /*allowND*/, int coiMode, AutoBuffer<double>* abuf )
{ {
if( !arr ) if( !arr )
return Mat(); return Mat();
...@@ -687,10 +687,21 @@ Mat cvarrToMat(const CvArr* arr, bool copyData, ...@@ -687,10 +687,21 @@ Mat cvarrToMat(const CvArr* arr, bool copyData,
if( CV_IS_SEQ(arr) ) if( CV_IS_SEQ(arr) )
{ {
CvSeq* seq = (CvSeq*)arr; CvSeq* seq = (CvSeq*)arr;
CV_Assert(seq->total > 0 && CV_ELEM_SIZE(seq->flags) == seq->elem_size); int total = seq->total, type = CV_MAT_TYPE(seq->flags), esz = seq->elem_size;
if( total == 0 )
return Mat();
CV_Assert(total > 0 && CV_ELEM_SIZE(seq->flags) == esz);
if(!copyData && seq->first->next == seq->first) if(!copyData && seq->first->next == seq->first)
return Mat(seq->total, 1, CV_MAT_TYPE(seq->flags), seq->first->data); return Mat(total, 1, type, seq->first->data);
Mat buf(seq->total, 1, CV_MAT_TYPE(seq->flags)); if( abuf )
{
abuf->allocate(((size_t)total*esz + sizeof(double)-1)/sizeof(double));
double* bufdata = *abuf;
cvCvtSeqToArray(seq, bufdata, CV_WHOLE_SEQ);
return Mat(total, 1, type, bufdata);
}
Mat buf(total, 1, type);
cvCvtSeqToArray(seq, buf.data, CV_WHOLE_SEQ); cvCvtSeqToArray(seq, buf.data, CV_WHOLE_SEQ);
return buf; return buf;
} }
......
...@@ -92,7 +92,7 @@ namespace ...@@ -92,7 +92,7 @@ namespace
} }
void download(double** hptrs) void download(double** hptrs)
{ {
AutoBuffer<double, 2 * sizeof(double)> hbuf(count); AutoBuffer<double> hbuf(count);
cudaSafeCall( cudaMemcpy((void*)hbuf, pdev, count * sizeof(double), cudaMemcpyDeviceToHost) ); cudaSafeCall( cudaMemcpy((void*)hbuf, pdev, count * sizeof(double), cudaMemcpyDeviceToHost) );
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
*hptrs[i] = hbuf[i]; *hptrs[i] = hbuf[i];
......
...@@ -202,9 +202,9 @@ bool PxMDecoder::readData( Mat& img ) ...@@ -202,9 +202,9 @@ bool PxMDecoder::readData( Mat& img )
if( m_offset < 0 || !m_strm.isOpened()) if( m_offset < 0 || !m_strm.isOpened())
return false; return false;
AutoBuffer<uchar,1024> _src(src_pitch + 32); AutoBuffer<uchar> _src(src_pitch + 32);
uchar* src = _src; uchar* src = _src;
AutoBuffer<uchar,1024> _gray_palette; AutoBuffer<uchar> _gray_palette;
uchar* gray_palette = _gray_palette; uchar* gray_palette = _gray_palette;
// create LUT for converting colors // create LUT for converting colors
......
...@@ -469,7 +469,7 @@ bool TiffEncoder::writeLibTiff( const Mat& img, const vector<int>& /*params*/) ...@@ -469,7 +469,7 @@ bool TiffEncoder::writeLibTiff( const Mat& img, const vector<int>& /*params*/)
// row buffer, because TIFFWriteScanline modifies the original data! // row buffer, because TIFFWriteScanline modifies the original data!
size_t scanlineSize = TIFFScanlineSize(pTiffHandle); size_t scanlineSize = TIFFScanlineSize(pTiffHandle);
AutoBuffer<uchar,1024> _buffer(scanlineSize+32); AutoBuffer<uchar> _buffer(scanlineSize+32);
uchar* buffer = _buffer; uchar* buffer = _buffer;
if (!buffer) if (!buffer)
{ {
...@@ -577,9 +577,9 @@ bool TiffEncoder::write( const Mat& img, const vector<int>& /*params*/) ...@@ -577,9 +577,9 @@ bool TiffEncoder::write( const Mat& img, const vector<int>& /*params*/)
#endif*/ #endif*/
int directoryOffset = 0; int directoryOffset = 0;
AutoBuffer<int,1024> stripOffsets(stripCount); AutoBuffer<int> stripOffsets(stripCount);
AutoBuffer<short,1024> stripCounts(stripCount); AutoBuffer<short> stripCounts(stripCount);
AutoBuffer<uchar,1024> _buffer(fileStep+32); AutoBuffer<uchar> _buffer(fileStep+32);
uchar* buffer = _buffer; uchar* buffer = _buffer;
int stripOffsetsOffset = 0; int stripOffsetsOffset = 0;
int stripCountsOffset = 0; int stripCountsOffset = 0;
......
This diff is collapsed.
...@@ -395,8 +395,8 @@ FarnebackUpdateFlow_GaussianBlur( const Mat& _R0, const Mat& _R1, ...@@ -395,8 +395,8 @@ FarnebackUpdateFlow_GaussianBlur( const Mat& _R0, const Mat& _R1,
double sigma = m*0.3, s = 1; double sigma = m*0.3, s = 1;
AutoBuffer<float> _vsum((width+m*2+2)*5 + 16), _hsum(width*5 + 16); AutoBuffer<float> _vsum((width+m*2+2)*5 + 16), _hsum(width*5 + 16);
AutoBuffer<float, 4096> _kernel((m+1)*5 + 16); AutoBuffer<float> _kernel((m+1)*5 + 16);
AutoBuffer<float*, 1024> _srow(m*2+1); AutoBuffer<float*> _srow(m*2+1);
float *vsum = alignPtr((float*)_vsum + (m+1)*5, 16), *hsum = alignPtr((float*)_hsum, 16); float *vsum = alignPtr((float*)_vsum + (m+1)*5, 16), *hsum = alignPtr((float*)_hsum, 16);
float* kernel = (float*)_kernel; float* kernel = (float*)_kernel;
const float** srow = (const float**)&_srow[0]; const float** srow = (const float**)&_srow[0];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment