| 2697 | |
| 2698 | |
| 2699 | CV_IMPL CvMat* |
| 2700 | cvReshape( const CvArr* array, CvMat* header, |
| 2701 | int new_cn, int new_rows ) |
| 2702 | { |
| 2703 | CvMat* result = 0; |
| 2704 | CvMat *mat = (CvMat*)array; |
| 2705 | int total_width, new_width; |
| 2706 | |
| 2707 | if( !header ) |
| 2708 | CV_Error( CV_StsNullPtr, "" ); |
| 2709 | |
| 2710 | if( !CV_IS_MAT( mat )) |
| 2711 | { |
| 2712 | int coi = 0; |
| 2713 | mat = cvGetMat( mat, header, &coi, 1 ); |
| 2714 | if( coi ) |
| 2715 | CV_Error( CV_BadCOI, "COI is not supported" ); |
| 2716 | } |
| 2717 | |
| 2718 | if( new_cn == 0 ) |
| 2719 | new_cn = CV_MAT_CN(mat->type); |
| 2720 | else if( (unsigned)(new_cn - 1) > 3 ) |
| 2721 | CV_Error( CV_BadNumChannels, "" ); |
| 2722 | |
| 2723 | if( mat != header ) |
| 2724 | { |
| 2725 | int hdr_refcount = header->hdr_refcount; |
| 2726 | *header = *mat; |
| 2727 | header->refcount = 0; |
| 2728 | header->hdr_refcount = hdr_refcount; |
| 2729 | } |
| 2730 | |
| 2731 | total_width = mat->cols * CV_MAT_CN( mat->type ); |
| 2732 | |
| 2733 | if( (new_cn > total_width || total_width % new_cn != 0) && new_rows == 0 ) |
| 2734 | new_rows = mat->rows * total_width / new_cn; |
| 2735 | |
| 2736 | if( new_rows == 0 || new_rows == mat->rows ) |
| 2737 | { |
| 2738 | header->rows = mat->rows; |
| 2739 | header->step = mat->step; |
| 2740 | } |
| 2741 | else |
| 2742 | { |
| 2743 | int total_size = total_width * mat->rows; |
| 2744 | if( !CV_IS_MAT_CONT( mat->type )) |
| 2745 | CV_Error( CV_BadStep, |
| 2746 | "The matrix is not continuous, thus its number of rows can not be changed" ); |
| 2747 | |
| 2748 | if( (unsigned)new_rows > (unsigned)total_size ) |
| 2749 | CV_Error( CV_StsOutOfRange, "Bad new number of rows" ); |
| 2750 | |
| 2751 | total_width = total_size / new_rows; |
| 2752 | |
| 2753 | if( total_width * new_rows != total_size ) |
| 2754 | CV_Error( CV_StsBadArg, "The total number of matrix elements " |
| 2755 | "is not divisible by the new number of rows" ); |
| 2756 |
no test coverage detected