Ejemplo n.º 1
0
/* dst = src */
void Mat::copyTo( OutputArray _dst ) const
{
    int dtype = _dst.type();
    if( _dst.fixedType() && dtype != type() )
    {
        CV_Assert( channels() == CV_MAT_CN(dtype) );
        convertTo( _dst, dtype );
        return;
    }

    if( empty() )
    {
        _dst.release();
        return;
    }

    if( dims <= 2 )
    {
        _dst.create( rows, cols, type() );
        Mat dst = _dst.getMat();
        if( data == dst.data )
            return;

        if( rows > 0 && cols > 0 )
        {
            const uchar* sptr = data;
            uchar* dptr = dst.data;

            // to handle the copying 1xn matrix => nx1 std vector.
            Size sz = size() == dst.size() ?
                getContinuousSize(*this, dst) :
                getContinuousSize(*this);
            size_t len = sz.width*elemSize();

            for( ; sz.height--; sptr += step, dptr += dst.step )
                memcpy( dptr, sptr, len );
        }
        return;
    }

    _dst.create( dims, size, type() );
    Mat dst = _dst.getMat();
    if( data == dst.data )
        return;

    if( total() != 0 )
    {
        const Mat* arrays[] = { this, &dst };
        uchar* ptrs[2];
        NAryMatIterator it(arrays, ptrs, 2);
        size_t sz = it.size*elemSize();

        for( size_t i = 0; i < it.nplanes; i++, ++it )
            memcpy(ptrs[1], ptrs[0], sz);
    }
}
Ejemplo n.º 2
0
Mat& Mat::operator = (const Scalar& s)
{
    const Mat* arrays[] = { this };
    uchar* ptr;
    NAryMatIterator it(arrays, &ptr, 1);
    size_t size = it.size*elemSize();
    
    if( s[0] == 0 && s[1] == 0 && s[2] == 0 && s[3] == 0 )
    {
        for( size_t i = 0; i < it.nplanes; i++, ++it )
            memset( ptr, 0, size );
    }
    else
    {
        if( it.nplanes > 0 )
        {
            double scalar[12];
            scalarToRawData(s, scalar, type(), 12);
            size_t blockSize = 12*elemSize1();
            
            for( size_t j = 0; j < size; j += blockSize )
            {
                size_t sz = MIN(blockSize, size - j);
                memcpy( ptr + j, scalar, sz );
            }
        }
        
        for( size_t i = 1; i < it.nplanes; i++ )
        {
            ++it;
            memcpy( ptr, data, size );
        }
    }
    return *this;
}
Ejemplo n.º 3
0
UMat UMat::diag(int d) const
{
    CV_Assert( dims <= 2 );
    UMat m = *this;
    size_t esz = elemSize();
    int len;

    if( d >= 0 )
    {
        len = std::min(cols - d, rows);
        m.offset += esz*d;
    }
    else
    {
        len = std::min(rows + d, cols);
        m.offset -= step[0]*d;
    }
    CV_DbgAssert( len > 0 );

    m.size[0] = m.rows = len;
    m.size[1] = m.cols = 1;
    m.step[0] += (len > 1 ? esz : 0);

    if( m.rows > 1 )
        m.flags &= ~CONTINUOUS_FLAG;
    else
        m.flags |= CONTINUOUS_FLAG;

    if( size() != Size(1,1) )
        m.flags |= SUBMATRIX_FLAG;

    return m;
}
Ejemplo n.º 4
0
/*! \relates XsArray
	\brief Initializes the XsArray with space for \a count items and copies them from \a src 
	\details This function initializes the object reserving \a count items in the buffer. \a count may
	be 0. If \a src is not 0, \a count items from \a src will be copied.
	\param descriptor The descriptor of the data in the list
	\param count The number of items to reserve space for. When \a src is not NULL, thisArray is also the number of items copied from \a src
	\param src A pointer to an array of objects to copy, may be NULL, ignored when \a count is 0
*/
void XsArray_construct(void* thisPtr, XsArrayDescriptor const* const descriptor, XsSize count, void const* src)
{
	XsArray* thisArray = (XsArray*) thisPtr;
	*((XsArrayDescriptor const**) &thisArray->m_descriptor) = descriptor;
	*((XsSize*) &thisArray->m_size) = count;

	if (thisArray->m_size)
	{
		// init to size
		*((void**) &thisArray->m_data) = malloc(thisArray->m_size*elemSize(thisArray));
		INC_ALLOC();

		// init the configurations
		if (src)
		{
			XsSize i;
			assert(thisArray->m_descriptor->itemCopyConstruct);
			for (i=0; i<thisArray->m_size; ++i)
				thisArray->m_descriptor->itemCopyConstruct(elemAt(thisArray->m_data, i), elemAt(src, i));
		}
		else if (thisArray->m_descriptor->itemConstruct)
		{
			XsSize i;
			for (i=0; i<thisArray->m_size; ++i)
				thisArray->m_descriptor->itemConstruct(elemAt(thisArray->m_data, i));
		}
	}
	else
		*((void**) &thisArray->m_data) = 0;
	*((XsSize*) &thisArray->m_reserved) = thisArray->m_size;
	*((int*) &thisArray->m_flags) = XSDF_Managed;
}
Ejemplo n.º 5
0
int EMat::CopyElement(uchar *fptr, uchar *tptr) {
    int increment = 0;
    for (unsigned int k = 0; k < elemSize(); k++) {
        *tptr = *fptr;
        tptr++;
        fptr++;
        increment++;
    }
    return increment;
}
Ejemplo n.º 6
0
void Mat::copyTo( Mat& dst, const Mat& mask ) const
{
    if( !mask.data )
    {
        copyTo(dst);
        return;
    }

    uchar* data0 = dst.data;
    dst.create( size(), type() );
    if( dst.data != data0 ) // do not leave dst uninitialized
        dst = Scalar(0);
    getCopyMaskFunc((int)elemSize())(*this, dst, mask);
}
Ejemplo n.º 7
0
 inline oclMat::oclMat(const oclMat &m, const Rect &roi)
     : flags(m.flags), rows(roi.height), cols(roi.width),
       step(m.step), data(m.data), refcount(m.refcount),
       datastart(m.datastart), dataend(m.dataend), clCxt(m.clCxt), offset(m.offset), wholerows(m.wholerows), wholecols(m.wholecols)
 {
     flags &= roi.width < m.cols ? ~Mat::CONTINUOUS_FLAG : -1;
     offset += roi.y * step + roi.x * elemSize();
     CV_Assert( 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.wholecols &&
                0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.wholerows );
     if( refcount )
         CV_XADD(refcount, 1);
     if( rows <= 0 || cols <= 0 )
         rows = cols = 0;
 }
Ejemplo n.º 8
0
Mat& Mat::setTo(const Scalar& s, const Mat& mask)
{
    if( !mask.data )
        *this = s;
    else
    {
        CV_Assert( channels() <= 4 );
        SetMaskFunc func = setMaskFuncTab[elemSize()];
        CV_Assert( func != 0 );
        double buf[4];
        scalarToRawData(s, buf, type(), 0);
        func(buf, *this, mask);
    }
    return *this;
}
Ejemplo n.º 9
0
 inline oclMat &oclMat::adjustROI( int dtop, int dbottom, int dleft, int dright )
 {
     Size wholeSize;
     Point ofs;
     size_t esz = elemSize();
     locateROI( wholeSize, ofs );
     int row1 = std::max(ofs.y - dtop, 0), row2 = std::min(ofs.y + rows + dbottom, wholeSize.height);
     int col1 = std::max(ofs.x - dleft, 0), col2 = std::min(ofs.x + cols + dright, wholeSize.width);
     offset += (row1 - ofs.y) * step + (col1 - ofs.x) * esz;
     rows = row2 - row1;
     cols = col2 - col1;
     if( esz * cols == step || rows == 1 )
         flags |= Mat::CONTINUOUS_FLAG;
     else
         flags &= ~Mat::CONTINUOUS_FLAG;
     return *this;
 }
Ejemplo n.º 10
0
UMat& UMat::adjustROI( int dtop, int dbottom, int dleft, int dright )
{
    CV_Assert( dims <= 2 && step[0] > 0 );
    Size wholeSize; Point ofs;
    size_t esz = elemSize();
    locateROI( wholeSize, ofs );
    int row1 = std::max(ofs.y - dtop, 0), row2 = std::min(ofs.y + rows + dbottom, wholeSize.height);
    int col1 = std::max(ofs.x - dleft, 0), col2 = std::min(ofs.x + cols + dright, wholeSize.width);
    offset += (row1 - ofs.y)*step + (col1 - ofs.x)*esz;
    rows = row2 - row1; cols = col2 - col1;
    size.p[0] = rows; size.p[1] = cols;
    if( esz*cols == step[0] || rows == 1 )
        flags |= CONTINUOUS_FLAG;
    else
        flags &= ~CONTINUOUS_FLAG;
    return *this;
}
Ejemplo n.º 11
0
Mat& Mat::operator = (const Scalar& s)
{
    Size sz = size();
    uchar* dst = data;

    sz.width *= (int)elemSize();
    if( isContinuous() )
    {
        sz.width *= sz.height;
        sz.height = 1;
    }
    
    if( s[0] == 0 && s[1] == 0 && s[2] == 0 && s[3] == 0 )
    {
        for( ; sz.height--; dst += step )
            memset( dst, 0, sz.width );
    }
    else
    {
        int t = type(), esz1 = (int)elemSize1();
        double scalar[12];
        scalarToRawData(s, scalar, t, 12);
        int copy_len = 12*esz1;
        uchar* dst_limit = dst + sz.width;
        
        if( sz.height-- )
        {
            while( dst + copy_len <= dst_limit )
            {
                memcpy( dst, scalar, copy_len );
                dst += copy_len;
            }
            memcpy( dst, scalar, dst_limit - dst );
        }

        if( sz.height )
        {
            dst = dst_limit - sz.width + step;
            for( ; sz.height--; dst += step )
                memcpy( dst, data, sz.width );
        }
    }
    return *this;
}
Ejemplo n.º 12
0
cv::Mat EMat::shift(cv::Mat dst, int amount, bool row) {
    cv::Mat newmat(rows, cols, type());

    if (row) {
        assert(amount < cols);

        for (int i = 0; i < rows; i++) {
            uchar *fptr = ptr(i) + (amount * elemSize());
            uchar *tptr = newmat.ptr(i);
            for (int j = 0; j < cols; j++) {
                int increment = CopyElement(fptr, tptr);
                fptr += increment;
                tptr += increment;
            }

            // Start over our from pointer
            if (++amount > cols)
                fptr = ptr(i);
        }
    } else {
        assert(amount < rows);

        int offset = amount;
        for (int i = 0; i < rows; i++) {
            // wrap our rows
            if (offset + i >= rows)
                offset = -i;

            uchar *fptr = ptr(i);
            uchar *tptr = newmat.ptr(i + offset);

            for (int j = 0; j < cols; j++) {
                int increment = CopyElement(fptr, tptr);
                fptr += increment;
                tptr += increment;
            }
        }
    }

    // Add column code here later on (it's a bit messier)

    dst = newmat;
    return dst;
}
Ejemplo n.º 13
0
void UMat::copyTo(OutputArray _dst) const
{
    int dtype = _dst.type();
    if( _dst.fixedType() && dtype != type() )
    {
        CV_Assert( channels() == CV_MAT_CN(dtype) );
        convertTo( _dst, dtype );
        return;
    }

    if( empty() )
    {
        _dst.release();
        return;
    }

    size_t i, sz[CV_MAX_DIM], srcofs[CV_MAX_DIM], dstofs[CV_MAX_DIM], esz = elemSize();
    for( i = 0; i < (size_t)dims; i++ )
        sz[i] = size.p[i];
    sz[dims-1] *= esz;
    ndoffset(srcofs);
    srcofs[dims-1] *= esz;

    _dst.create( dims, size.p, type() );
    if( _dst.isUMat() )
    {
        UMat dst = _dst.getUMat();
        if( u == dst.u && dst.offset == offset )
            return;

        if (u->currAllocator == dst.u->currAllocator)
        {
            dst.ndoffset(dstofs);
            dstofs[dims-1] *= esz;
            u->currAllocator->copy(u, dst.u, dims, sz, srcofs, step.p, dstofs, dst.step.p, false);
            return;
        }
    }

    Mat dst = _dst.getMat();
    u->currAllocator->download(u, dst.data, dims, sz, srcofs, step.p, dst.step.p);
}
Ejemplo n.º 14
0
UMat::UMat(const UMat& m, const Range& _rowRange, const Range& _colRange)
    : flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), u(0), offset(0), size(&rows)
{
    CV_Assert( m.dims >= 2 );
    if( m.dims > 2 )
    {
        AutoBuffer<Range> rs(m.dims);
        rs[0] = _rowRange;
        rs[1] = _colRange;
        for( int i = 2; i < m.dims; i++ )
            rs[i] = Range::all();
        *this = m(rs);
        return;
    }

    *this = m;
    if( _rowRange != Range::all() && _rowRange != Range(0,rows) )
    {
        CV_Assert( 0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows );
        rows = _rowRange.size();
        offset += step*_rowRange.start;
        flags |= SUBMATRIX_FLAG;
    }

    if( _colRange != Range::all() && _colRange != Range(0,cols) )
    {
        CV_Assert( 0 <= _colRange.start && _colRange.start <= _colRange.end && _colRange.end <= m.cols );
        cols = _colRange.size();
        offset += _colRange.start*elemSize();
        flags &= cols < m.cols ? ~CONTINUOUS_FLAG : -1;
        flags |= SUBMATRIX_FLAG;
    }

    if( rows == 1 )
        flags |= CONTINUOUS_FLAG;

    if( rows <= 0 || cols <= 0 )
    {
        release();
        rows = cols = 0;
    }
}
Ejemplo n.º 15
0
void UMat::locateROI( Size& wholeSize, Point& ofs ) const
{
    CV_Assert( dims <= 2 && step[0] > 0 );
    size_t esz = elemSize(), minstep;
    ptrdiff_t delta1 = (ptrdiff_t)offset, delta2 = (ptrdiff_t)u->size;

    if( delta1 == 0 )
        ofs.x = ofs.y = 0;
    else
    {
        ofs.y = (int)(delta1/step[0]);
        ofs.x = (int)((delta1 - step[0]*ofs.y)/esz);
        CV_DbgAssert( offset == (size_t)(ofs.y*step[0] + ofs.x*esz) );
    }
    minstep = (ofs.x + cols)*esz;
    wholeSize.height = (int)((delta2 - minstep)/step[0] + 1);
    wholeSize.height = std::max(wholeSize.height, ofs.y + rows);
    wholeSize.width = (int)((delta2 - step*(wholeSize.height-1))/esz);
    wholeSize.width = std::max(wholeSize.width, ofs.x + cols);
}
Ejemplo n.º 16
0
/* dst = src */
void Mat::copyTo( Mat& dst ) const
{
    if( data == dst.data )
        return;

    dst.create( rows, cols, type() );
    Size sz = size();
    const uchar* sptr = data;
    uchar* dptr = dst.data;

    sz.width *= (int)elemSize();
    if( isContinuous() && dst.isContinuous() )
    {
        sz.width *= sz.height;
        sz.height = 1;
    }

    for( ; sz.height--; sptr += step, dptr += dst.step )
        memcpy( dptr, sptr, sz.width );
}
Ejemplo n.º 17
0
 inline void oclMat::locateROI( Size &wholeSize, Point &ofs ) const
 {
     size_t esz = elemSize();//, minstep;
     //ptrdiff_t delta1 = offset;//, delta2 = dataend - datastart;
     CV_DbgAssert( step > 0 );
     if( offset == 0 )
         ofs.x = ofs.y = 0;
     else
     {
         ofs.y = (int)(offset / step);
         ofs.x = (int)((offset - step * ofs.y) / esz);
         //CV_DbgAssert( data == datastart + ofs.y*step + ofs.x*esz );
     }
     //minstep = (ofs.x + cols)*esz;
     //wholeSize.height = (int)((delta2 - minstep)/step + 1);
     //wholeSize.height = std::max(wholeSize.height, ofs.y + rows);
     //wholeSize.width = (int)((delta2 - step*(wholeSize.height-1))/esz);
     //wholeSize.width = std::max(wholeSize.width, ofs.x + cols);
     wholeSize.height = wholerows;
     wholeSize.width = wholecols;
 }
Ejemplo n.º 18
0
/*! \relates XsArray
	\brief Reserves space for \a count items
	\details This function reserves space for exactly \a count items unless \a count is less than the 
	current list size. The function will retain the current data in the list.
	\param count The number of items to reserve space for
	\sa XsArray_assign
*/
void XsArray_reserve(void* thisPtr, XsSize count)
{
	XsArray* thisArray = (XsArray*) thisPtr;
	XsArray tmp = { 0, thisArray->m_size, 0, XSDF_Managed, thisArray->m_descriptor };
	XsSize i;

	if (count < thisArray->m_size)
		count = thisArray->m_size;

	if (count == thisArray->m_reserved)
		return;

	if (!(thisArray->m_flags & XSDF_Managed))
	{
		// attempting thisArray on an unmanaged list is ignored silently
		return;
	}

	if (!count)
	{
		XsArray_destruct(thisArray);
		return;
	}

	*((XsSize*) &tmp.m_reserved) = count;

	// init to size
	*((void**) &tmp.m_data) = malloc(tmp.m_reserved*elemSize(thisArray));
	INC_ALLOC();

	if (thisArray->m_descriptor->itemConstruct)
		for (i=0; i<tmp.m_reserved; ++i)
			thisArray->m_descriptor->itemConstruct(elemAt(tmp.m_data, i));
	
	for (i=0; i<thisArray->m_size; ++i)
		thisArray->m_descriptor->itemSwap(elemAt(thisArray->m_data, i), elemAt(tmp.m_data, i));

	XsArray_destruct(thisArray);
	XsArray_swap(thisArray, &tmp);
}
Ejemplo n.º 19
0
void Mat::copyTo( OutputArray _dst, InputArray _mask ) const
{
    Mat mask = _mask.getMat();
    if( !mask.data )
    {
        copyTo(_dst);
        return;
    }

    int cn = channels(), mcn = mask.channels();
    CV_Assert( mask.depth() == CV_8U && (mcn == 1 || mcn == cn) );
    bool colorMask = mcn > 1;

    size_t esz = colorMask ? elemSize1() : elemSize();
    BinaryFunc copymask = getCopyMaskFunc(esz);

    uchar* data0 = _dst.getMat().data;
    _dst.create( dims, size, type() );
    Mat dst = _dst.getMat();

    if( dst.data != data0 ) // do not leave dst uninitialized
        dst = Scalar(0);

    if( dims <= 2 )
    {
        CV_Assert( size() == mask.size() );
        Size sz = getContinuousSize(*this, dst, mask, mcn);
        copymask(data, step, mask.data, mask.step, dst.data, dst.step, sz, &esz);
        return;
    }

    const Mat* arrays[] = { this, &dst, &mask, 0 };
    uchar* ptrs[3];
    NAryMatIterator it(arrays, ptrs);
    Size sz((int)(it.size*mcn), 1);

    for( size_t i = 0; i < it.nplanes; i++, ++it )
        copymask(ptrs[0], 0, ptrs[2], 0, ptrs[1], 0, sz, &esz);
}
Ejemplo n.º 20
0
Mat& Mat::setTo(InputArray _value, InputArray _mask)
{
    if( !data )
        return *this;
    
    Mat value = _value.getMat(), mask = _mask.getMat();
    
    CV_Assert( checkScalar(value, type(), _value.kind(), _InputArray::MAT ));
    CV_Assert( mask.empty() || mask.type() == CV_8U );
    
    size_t esz = elemSize();
    BinaryFunc copymask = getCopyMaskFunc(esz);
    
    const Mat* arrays[] = { this, !mask.empty() ? &mask : 0, 0 };
    uchar* ptrs[2]={0,0};
    NAryMatIterator it(arrays, ptrs);
    int total = (int)it.size, blockSize0 = std::min(total, (int)((BLOCK_SIZE + esz-1)/esz));
    AutoBuffer<uchar> _scbuf(blockSize0*esz + 32);
    uchar* scbuf = alignPtr((uchar*)_scbuf, (int)sizeof(double));
    convertAndUnrollScalar( value, type(), scbuf, blockSize0 );
    
    for( size_t i = 0; i < it.nplanes; i++, ++it )
    {
        for( int j = 0; j < total; j += blockSize0 )
        {
            Size sz(std::min(blockSize0, total - j), 1);
            size_t blockSize = sz.width*esz;
            if( ptrs[1] )
            {
                copymask(scbuf, 0, ptrs[1], 0, ptrs[0], 0, sz, &esz);
                ptrs[1] += sz.width;
            }
            else
                memcpy(ptrs[0], scbuf, blockSize);
            ptrs[0] += blockSize;
        }
    }
    return *this;
}
        inline oclMat::oclMat(const oclMat &m, const Range &rRange, const Range &cRange)
        {
            flags = m.flags;
            step = m.step;
            refcount = m.refcount;
            data = m.data;
            datastart = m.datastart;
            dataend = m.dataend;
            clCxt = m.clCxt;
            wholerows = m.wholerows;
            wholecols = m.wholecols;
            offset = m.offset;
            if( rRange == Range::all() )
                rows = m.rows;
            else
            {
                CV_Assert( 0 <= rRange.start && rRange.start <= rRange.end && rRange.end <= m.rows );
                rows = rRange.size();
                offset += step * rRange.start;
            }

            if( cRange == Range::all() )
                cols = m.cols;
            else
            {
                CV_Assert( 0 <= cRange.start && cRange.start <= cRange.end && cRange.end <= m.cols );
                cols = cRange.size();
                offset += cRange.start * elemSize();
                flags &= cols < m.cols ? ~Mat::CONTINUOUS_FLAG : -1;
            }

            if( rows == 1 )
                flags |= Mat::CONTINUOUS_FLAG;

            if( refcount )
                CV_XADD(refcount, 1);
            if( rows <= 0 || cols <= 0 )
                rows = cols = 0;
        }
Ejemplo n.º 22
0
bool Assign<std::string>::do_assign(std::string& dest, int type, const void* data)
{
  char buf[32];
  if (isSpecial(type))
  {
    if (isString(type))
    {
      dest.assign((const char*)data,size(type));
      return true;
    }
    switch (type)
    {
    case Null:
      dest.clear();
      return true;
    case Enum:
      snprintf(buf,sizeof(buf),"%d",*(const int*)data);
      dest = buf;
      return true;
    case ID:
      snprintf(buf,sizeof(buf),"0x%016llx",*(const long long*)data);
      dest = buf;
      return true;
    }
  }
  else if (isMatrix(type))
  {
    std::string str;
    dest = "{";
    for (int y=0;y<ny(type);y++)
    {
      if (y) dest += ", ";
      dest += '{';
      for (int x=0;x<nx(type);x++)
      {
	if (x) dest += ", ";
	do_assign(str, toSingle(type), data);
	data = ((const char*)data) + elemSize(type);
	dest += str;
      }
      dest += '}';
    }
    dest += '}';
  }
  else if (isVector(type))
  {
    std::string str;
    dest = "{";
    for (int x=0;x<nx(type);x++)
    {
      if (x) dest += ", ";
      do_assign(str, toSingle(type), data);
      data = ((const char*)data) + elemSize(type);
      dest += str;
    }
    dest += '}';
  }
  else
  {
    if (!isSingle(type)) type = toSingle(type);
    switch (toSingle(type))
    {
    case Bool:
      dest = (*(const unsigned char*)data)?"true":"false";
      return true;
    case Byte:
      snprintf(buf,sizeof(buf),"%u",(unsigned)*(const unsigned char*)data);
      dest = buf;
      return true;
    case Short:
      snprintf(buf,sizeof(buf),"%d",(int)*(const short*)data);
      dest = buf;
      return true;
    case Int:
      snprintf(buf,sizeof(buf),"%d",*(const int*)data);
      dest = buf;
      return true;
    case QWord:
      snprintf(buf,sizeof(buf),"%lld",*(const long long*)data);
      dest = buf;
      return true;
    case Float:
      snprintf(buf,sizeof(buf),"%f",(double)*(const float*)data);
      dest = buf;
      return true;
    case Double:
      snprintf(buf,sizeof(buf),"%f",*(const double*)data);
      dest = buf;
      return true;
    case LongDouble:
      snprintf(buf,sizeof(buf),"%Lf",*(const long double*)data);
      dest = buf;
      return true;
    }
  }
  // Failure
  // In this implementation: only Type::Delete is not supported
  return false;
}
Ejemplo n.º 23
0
/* dst = src */
void Mat::copyTo( OutputArray _dst ) const
{
    int dtype = _dst.type();
    if( _dst.fixedType() && dtype != type() )
    {
        CV_Assert( channels() == CV_MAT_CN(dtype) );
        convertTo( _dst, dtype );
        return;
    }

    if( empty() )
    {
        _dst.release();
        return;
    }

    if( _dst.isUMat() )
    {
        _dst.create( dims, size.p, type() );
        UMat dst = _dst.getUMat();

        size_t i, sz[CV_MAX_DIM], dstofs[CV_MAX_DIM], esz = elemSize();
        for( i = 0; i < (size_t)dims; i++ )
            sz[i] = size.p[i];
        sz[dims-1] *= esz;
        dst.ndoffset(dstofs);
        dstofs[dims-1] *= esz;
        dst.u->currAllocator->upload(dst.u, data, dims, sz, dstofs, dst.step.p, step.p);
        return;
    }

    if( dims <= 2 )
    {
        _dst.create( rows, cols, type() );
        Mat dst = _dst.getMat();
        if( data == dst.data )
            return;

        if( rows > 0 && cols > 0 )
        {
            const uchar* sptr = data;
            uchar* dptr = dst.data;

            Size sz = getContinuousSize(*this, dst);
            size_t len = sz.width*elemSize();

            for( ; sz.height--; sptr += step, dptr += dst.step )
                memcpy( dptr, sptr, len );
        }
        return;
    }

    _dst.create( dims, size, type() );
    Mat dst = _dst.getMat();
    if( data == dst.data )
        return;

    if( total() != 0 )
    {
        const Mat* arrays[] = { this, &dst };
        uchar* ptrs[2];
        NAryMatIterator it(arrays, ptrs, 2);
        size_t sz = it.size*elemSize();

        for( size_t i = 0; i < it.nplanes; i++, ++it )
            memcpy(ptrs[1], ptrs[0], sz);
    }
}
Ejemplo n.º 24
0
cv::Mat EMat::padarray(int x_padsize, int y_padsize, pad_method method, pad_dir dir) {
    // Create a new matrix of the appropriate size and initialize it
    cv::Mat new_mat;
    if ((dir == pre) || (dir == post))
        new_mat.create(rows + y_padsize, cols + x_padsize, type());
    else
        new_mat.create(rows + (y_padsize * 2), cols + (x_padsize * 2), type());

    int padmult = 1;
    if (dir == both)
        padmult = 2;
    assert((new_mat.rows == (rows + (y_padsize * padmult))) && (new_mat.cols + (x_padsize * padmult)));

    int dsize = elemSize();
    // Mirror our matrix
    if (method == symmetric) {
        // for each line, we have a reverse, followed by a forward, etc.  We just have to find a good starting point

        // First find a direction:
        int start_x_dir, x_dir, y_dir;
        int start_x, cur_x, cur_y;

        // Now find our starting position in our source matrix
        if ((x_padsize / cols) % 2) // If we have an odd multiple (plus some), go Forward
        {
            start_x_dir = 1;
            start_x = cols - (x_padsize % cols) - 1;
        } else {
            start_x_dir = -1;
            start_x = (x_padsize % cols) - 1;
        }

        if ((y_padsize / rows) % 2) {
            y_dir = 1;
            cur_y = rows - (y_padsize % rows) - 1;
        } else {
            y_dir = -1;
            cur_y = (y_padsize % rows) - 1;
        }

        // Loop through every matrix of the old matrix
        uchar *fptr, *tptr;
        for (int i = 0; i < new_mat.rows; i++) {
            // Always reset initial conditions for x values
            cur_x = start_x;
            x_dir = start_x_dir;

            // New row for the to matrix
            tptr = new_mat.ptr(i);

            fptr = ptr(cur_y) + (cur_x * dsize);

            for (int j = 0; j < new_mat.cols; j++) {
                // Copy the element and increment applicable pointers
                CopyElement(fptr, tptr);
                tptr += dsize;

                // Handle incrementing from row ptr and reversing directions if necessary
                cur_x += x_dir;
                if ((cur_x == cols) || (cur_x < 0)) {
                    x_dir = x_dir * (-1); // reverse direction
                    cur_x += x_dir; // Go back into safe areas
                }
                fptr = ptr(cur_y) + (cur_x * dsize);
            }

            // Handle incrementing from matrix column and reversing if necessary
            cur_y += y_dir;
            if ((cur_y == rows) || (cur_y < 0)) {
                y_dir = y_dir * (-1); // reverse direction
                cur_y += y_dir; // Go back into safe areas
            }
        }
    } else if (method == circular)
        assert(0); // Not coded yet
    else if (method == replicate)
        assert(0); // Not coded yet
    else
        assert(0); // Probably should raise an exception here

    *this = new_mat;
    return new_mat;
}
Ejemplo n.º 25
0
__host__ size_t GpuMat_<T>::stepT() const
{
    return step / elemSize();
}