Returns pointer to specified element of array (linear index is used)
| 1634 | |
| 1635 | // Returns pointer to specified element of array (linear index is used) |
| 1636 | CV_IMPL uchar* |
| 1637 | cvPtr1D( const CvArr* arr, int idx, int* _type ) |
| 1638 | { |
| 1639 | uchar* ptr = 0; |
| 1640 | if( CV_IS_MAT( arr )) |
| 1641 | { |
| 1642 | CvMat* mat = (CvMat*)arr; |
| 1643 | |
| 1644 | int type = CV_MAT_TYPE(mat->type); |
| 1645 | int pix_size = CV_ELEM_SIZE(type); |
| 1646 | |
| 1647 | if( _type ) |
| 1648 | *_type = type; |
| 1649 | |
| 1650 | // the first part is mul-free sufficient check |
| 1651 | // that the index is within the matrix |
| 1652 | if( (unsigned)idx >= (unsigned)(mat->rows + mat->cols - 1) && |
| 1653 | (unsigned)idx >= (unsigned)(mat->rows*mat->cols)) |
| 1654 | CV_Error( CV_StsOutOfRange, "index is out of range" ); |
| 1655 | |
| 1656 | if( CV_IS_MAT_CONT(mat->type)) |
| 1657 | { |
| 1658 | ptr = mat->data.ptr + (size_t)idx*pix_size; |
| 1659 | } |
| 1660 | else |
| 1661 | { |
| 1662 | int row, col; |
| 1663 | if( mat->cols == 1 ) |
| 1664 | row = idx, col = 0; |
| 1665 | else |
| 1666 | row = idx/mat->cols, col = idx - row*mat->cols; |
| 1667 | ptr = mat->data.ptr + (size_t)row*mat->step + col*pix_size; |
| 1668 | } |
| 1669 | } |
| 1670 | else if( CV_IS_IMAGE_HDR( arr )) |
| 1671 | { |
| 1672 | IplImage* img = (IplImage*)arr; |
| 1673 | int width = !img->roi ? img->width : img->roi->width; |
| 1674 | int y = idx/width, x = idx - y*width; |
| 1675 | |
| 1676 | ptr = cvPtr2D( arr, y, x, _type ); |
| 1677 | } |
| 1678 | else if( CV_IS_MATND( arr )) |
| 1679 | { |
| 1680 | CvMatND* mat = (CvMatND*)arr; |
| 1681 | int j, type = CV_MAT_TYPE(mat->type); |
| 1682 | size_t size = mat->dim[0].size; |
| 1683 | |
| 1684 | if( _type ) |
| 1685 | *_type = type; |
| 1686 | |
| 1687 | for( j = 1; j < mat->dims; j++ ) |
| 1688 | size *= mat->dim[j].size; |
| 1689 | |
| 1690 | if((unsigned)idx >= (unsigned)size ) |
| 1691 | CV_Error( CV_StsOutOfRange, "index is out of range" ); |
| 1692 | |
| 1693 | if( CV_IS_MAT_CONT(mat->type)) |
no test coverage detected