1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
44 /**************************************************************************************\
46 \**************************************************************************************/
49 cvSampleLine( const void* img, CvPoint pt1, CvPoint pt2,
50 void* _buffer, int connectivity )
54 CV_FUNCNAME( "cvSampleLine" );
58 int i, coi = 0, pix_size;
59 CvMat stub, *mat = (CvMat*)img;
60 CvLineIterator iterator;
61 uchar* buffer = (uchar*)_buffer;
63 CV_CALL( mat = cvGetMat( mat, &stub, &coi ));
66 CV_ERROR( CV_BadCOI, "" );
69 CV_ERROR( CV_StsNullPtr, "" );
71 CV_CALL( count = cvInitLineIterator( mat, pt1, pt2, &iterator, connectivity ));
73 pix_size = CV_ELEM_SIZE(mat->type);
74 for( i = 0; i < count; i++ )
76 for( int j = 0; j < pix_size; j++ )
77 buffer[j] = iterator.ptr[j];
79 CV_NEXT_LINE_POINT( iterator );
89 icvAdjustRect( const void* srcptr, int src_step, int pix_size,
90 CvSize src_size, CvSize win_size,
91 CvPoint ip, CvRect* pRect )
94 const char* src = (const char*)srcptr;
104 if( rect.x > win_size.width )
105 rect.x = win_size.width;
108 if( ip.x + win_size.width < src_size.width )
109 rect.width = win_size.width;
112 rect.width = src_size.width - ip.x - 1;
115 src += rect.width*pix_size;
118 assert( rect.width <= win_size.width );
123 src += ip.y * src_step;
129 if( ip.y + win_size.height < src_size.height )
130 rect.height = win_size.height;
133 rect.height = src_size.height - ip.y - 1;
134 if( rect.height < 0 )
136 src += rect.height*src_step;
142 return src - rect.x*pix_size;
146 #define ICV_DEF_GET_RECT_SUB_PIX_FUNC( flavor, srctype, dsttype, worktype, \
147 cast_macro, scale_macro, cast_macro2 )\
148 CvStatus CV_STDCALL icvGetRectSubPix_##flavor##_C1R \
149 ( const srctype* src, int src_step, CvSize src_size, \
150 dsttype* dst, int dst_step, CvSize win_size, CvPoint2D32f center ) \
153 worktype a11, a12, a21, a22, b1, b2; \
157 center.x -= (win_size.width-1)*0.5f; \
158 center.y -= (win_size.height-1)*0.5f; \
160 ip.x = cvFloor( center.x ); \
161 ip.y = cvFloor( center.y ); \
163 a = center.x - ip.x; \
164 b = center.y - ip.y; \
165 a11 = scale_macro((1.f-a)*(1.f-b)); \
166 a12 = scale_macro(a*(1.f-b)); \
167 a21 = scale_macro((1.f-a)*b); \
168 a22 = scale_macro(a*b); \
169 b1 = scale_macro(1.f - b); \
170 b2 = scale_macro(b); \
172 src_step /= sizeof(src[0]); \
173 dst_step /= sizeof(dst[0]); \
175 if( 0 <= ip.x && ip.x + win_size.width < src_size.width && \
176 0 <= ip.y && ip.y + win_size.height < src_size.height ) \
178 /* extracted rectangle is totally inside the image */ \
179 src += ip.y * src_step + ip.x; \
181 for( i = 0; i < win_size.height; i++, src += src_step, \
184 for( j = 0; j <= win_size.width - 2; j += 2 ) \
186 worktype s0 = cast_macro(src[j])*a11 + \
187 cast_macro(src[j+1])*a12 + \
188 cast_macro(src[j+src_step])*a21 + \
189 cast_macro(src[j+src_step+1])*a22; \
190 worktype s1 = cast_macro(src[j+1])*a11 + \
191 cast_macro(src[j+2])*a12 + \
192 cast_macro(src[j+src_step+1])*a21 + \
193 cast_macro(src[j+src_step+2])*a22; \
195 dst[j] = (dsttype)cast_macro2(s0); \
196 dst[j+1] = (dsttype)cast_macro2(s1); \
199 for( ; j < win_size.width; j++ ) \
201 worktype s0 = cast_macro(src[j])*a11 + \
202 cast_macro(src[j+1])*a12 + \
203 cast_macro(src[j+src_step])*a21 + \
204 cast_macro(src[j+src_step+1])*a22; \
206 dst[j] = (dsttype)cast_macro2(s0); \
214 src = (const srctype*)icvAdjustRect( src, src_step*sizeof(*src), \
215 sizeof(*src), src_size, win_size,ip, &r); \
217 for( i = 0; i < win_size.height; i++, dst += dst_step ) \
219 const srctype *src2 = src + src_step; \
221 if( i < r.y || i >= r.height ) \
224 for( j = 0; j < r.x; j++ ) \
226 worktype s0 = cast_macro(src[r.x])*b1 + \
227 cast_macro(src2[r.x])*b2; \
229 dst[j] = (dsttype)cast_macro2(s0); \
232 for( ; j < r.width; j++ ) \
234 worktype s0 = cast_macro(src[j])*a11 + \
235 cast_macro(src[j+1])*a12 + \
236 cast_macro(src2[j])*a21 + \
237 cast_macro(src2[j+1])*a22; \
239 dst[j] = (dsttype)cast_macro2(s0); \
242 for( ; j < win_size.width; j++ ) \
244 worktype s0 = cast_macro(src[r.width])*b1 + \
245 cast_macro(src2[r.width])*b2; \
247 dst[j] = (dsttype)cast_macro2(s0); \
259 #define ICV_DEF_GET_RECT_SUB_PIX_FUNC_C3( flavor, srctype, dsttype, worktype, \
260 cast_macro, scale_macro, mul_macro )\
261 static CvStatus CV_STDCALL icvGetRectSubPix_##flavor##_C3R \
262 ( const srctype* src, int src_step, CvSize src_size, \
263 dsttype* dst, int dst_step, CvSize win_size, CvPoint2D32f center ) \
269 center.x -= (win_size.width-1)*0.5f; \
270 center.y -= (win_size.height-1)*0.5f; \
272 ip.x = cvFloor( center.x ); \
273 ip.y = cvFloor( center.y ); \
275 a = scale_macro( center.x - ip.x ); \
276 b = scale_macro( center.y - ip.y ); \
278 src_step /= sizeof( src[0] ); \
279 dst_step /= sizeof( dst[0] ); \
281 if( 0 <= ip.x && ip.x + win_size.width < src_size.width && \
282 0 <= ip.y && ip.y + win_size.height < src_size.height ) \
284 /* extracted rectangle is totally inside the image */ \
285 src += ip.y * src_step + ip.x*3; \
287 for( i = 0; i < win_size.height; i++, src += src_step, \
290 for( j = 0; j < win_size.width; j++ ) \
292 worktype s0 = cast_macro(src[j*3]); \
293 worktype s1 = cast_macro(src[j*3 + src_step]); \
294 s0 += mul_macro( a, (cast_macro(src[j*3+3]) - s0)); \
295 s1 += mul_macro( a, (cast_macro(src[j*3+3+src_step]) - s1));\
296 dst[j*3] = (dsttype)(s0 + mul_macro( b, (s1 - s0))); \
298 s0 = cast_macro(src[j*3+1]); \
299 s1 = cast_macro(src[j*3+1 + src_step]); \
300 s0 += mul_macro( a, (cast_macro(src[j*3+4]) - s0)); \
301 s1 += mul_macro( a, (cast_macro(src[j*3+4+src_step]) - s1));\
302 dst[j*3+1] = (dsttype)(s0 + mul_macro( b, (s1 - s0))); \
304 s0 = cast_macro(src[j*3+2]); \
305 s1 = cast_macro(src[j*3+2 + src_step]); \
306 s0 += mul_macro( a, (cast_macro(src[j*3+5]) - s0)); \
307 s1 += mul_macro( a, (cast_macro(src[j*3+5+src_step]) - s1));\
308 dst[j*3+2] = (dsttype)(s0 + mul_macro( b, (s1 - s0))); \
316 src = (const srctype*)icvAdjustRect( src, src_step*sizeof(*src), \
317 sizeof(*src)*3, src_size, win_size, ip, &r ); \
319 for( i = 0; i < win_size.height; i++, dst += dst_step ) \
321 const srctype *src2 = src + src_step; \
323 if( i < r.y || i >= r.height ) \
326 for( j = 0; j < r.x; j++ ) \
328 worktype s0 = cast_macro(src[r.x*3]); \
329 worktype s1 = cast_macro(src2[r.x*3]); \
330 dst[j*3] = (dsttype)(s0 + mul_macro( b, (s1 - s0))); \
332 s0 = cast_macro(src[r.x*3+1]); \
333 s1 = cast_macro(src2[r.x*3+1]); \
334 dst[j*3+1] = (dsttype)(s0 + mul_macro( b, (s1 - s0))); \
336 s0 = cast_macro(src[r.x*3+2]); \
337 s1 = cast_macro(src2[r.x*3+2]); \
338 dst[j*3+2] = (dsttype)(s0 + mul_macro( b, (s1 - s0))); \
341 for( ; j < r.width; j++ ) \
343 worktype s0 = cast_macro(src[j*3]); \
344 worktype s1 = cast_macro(src2[j*3]); \
345 s0 += mul_macro( a, (cast_macro(src[j*3 + 3]) - s0)); \
346 s1 += mul_macro( a, (cast_macro(src2[j*3 + 3]) - s1)); \
347 dst[j*3] = (dsttype)(s0 + mul_macro( b, (s1 - s0))); \
349 s0 = cast_macro(src[j*3+1]); \
350 s1 = cast_macro(src2[j*3+1]); \
351 s0 += mul_macro( a, (cast_macro(src[j*3 + 4]) - s0)); \
352 s1 += mul_macro( a, (cast_macro(src2[j*3 + 4]) - s1)); \
353 dst[j*3+1] = (dsttype)(s0 + mul_macro( b, (s1 - s0))); \
355 s0 = cast_macro(src[j*3+2]); \
356 s1 = cast_macro(src2[j*3+2]); \
357 s0 += mul_macro( a, (cast_macro(src[j*3 + 5]) - s0)); \
358 s1 += mul_macro( a, (cast_macro(src2[j*3 + 5]) - s1)); \
359 dst[j*3+2] = (dsttype)(s0 + mul_macro( b, (s1 - s0))); \
362 for( ; j < win_size.width; j++ ) \
364 worktype s0 = cast_macro(src[r.width*3]); \
365 worktype s1 = cast_macro(src2[r.width*3]); \
366 dst[j*3] = (dsttype)(s0 + mul_macro( b, (s1 - s0))); \
368 s0 = cast_macro(src[r.width*3+1]); \
369 s1 = cast_macro(src2[r.width*3+1]); \
370 dst[j*3+1] = (dsttype)(s0 + mul_macro( b, (s1 - s0))); \
372 s0 = cast_macro(src[r.width*3+2]); \
373 s1 = cast_macro(src2[r.width*3+2]); \
374 dst[j*3+2] = (dsttype)(s0 + mul_macro( b, (s1 - s0))); \
387 CvStatus CV_STDCALL icvGetRectSubPix_8u32f_C1R
388 ( const uchar* src, int src_step, CvSize src_size,
389 float* dst, int dst_step, CvSize win_size, CvPoint2D32f center )
392 float a12, a22, b1, b2;
397 center.x -= (win_size.width-1)*0.5f;
398 center.y -= (win_size.height-1)*0.5f;
400 ip.x = cvFloor( center.x );
401 ip.y = cvFloor( center.y );
403 if( win_size.width <= 0 || win_size.height <= 0 )
404 return CV_BADRANGE_ERR;
415 src_step /= sizeof(src[0]);
416 dst_step /= sizeof(dst[0]);
418 if( 0 <= ip.x && ip.x + win_size.width < src_size.width &&
419 0 <= ip.y && ip.y + win_size.height < src_size.height )
421 // extracted rectangle is totally inside the image
422 src += ip.y * src_step + ip.x;
425 if( icvCopySubpix_8u32f_C1R_p &&
426 icvCopySubpix_8u32f_C1R_p( src, src_step, dst,
427 dst_step*sizeof(dst[0]), win_size, a, b ) >= 0 )
431 for( ; win_size.height--; src += src_step, dst += dst_step )
433 float prev = (1 - a)*(b1*CV_8TO32F(src[0]) + b2*CV_8TO32F(src[src_step]));
434 for( j = 0; j < win_size.width; j++ )
436 float t = a12*CV_8TO32F(src[j+1]) + a22*CV_8TO32F(src[j+1+src_step]);
446 src = (const uchar*)icvAdjustRect( src, src_step*sizeof(*src),
447 sizeof(*src), src_size, win_size,ip, &r);
449 for( i = 0; i < win_size.height; i++, dst += dst_step )
451 const uchar *src2 = src + src_step;
453 if( i < r.y || i >= r.height )
456 for( j = 0; j < r.x; j++ )
458 float s0 = CV_8TO32F(src[r.x])*b1 +
459 CV_8TO32F(src2[r.x])*b2;
461 dst[j] = (float)(s0);
466 float prev = (1 - a)*(b1*CV_8TO32F(src[j]) + b2*CV_8TO32F(src2[j]));
468 for( ; j < r.width; j++ )
470 float t = a12*CV_8TO32F(src[j+1]) + a22*CV_8TO32F(src2[j+1]);
476 for( ; j < win_size.width; j++ )
478 float s0 = CV_8TO32F(src[r.width])*b1 +
479 CV_8TO32F(src2[r.width])*b2;
481 dst[j] = (float)(s0);
495 #define ICV_SCALE(x) cvRound((x)*(1 << ICV_SHIFT))
496 #define ICV_MUL_SCALE(x,y) (((x)*(y) + (1 << (ICV_SHIFT-1))) >> ICV_SHIFT)
497 #define ICV_DESCALE(x) (((x)+(1 << (ICV_SHIFT-1))) >> ICV_SHIFT)
499 /*icvCopySubpix_8u_C1R_t icvCopySubpix_8u_C1R_p = 0;
500 icvCopySubpix_8u32f_C1R_t icvCopySubpix_8u32f_C1R_p = 0;
501 icvCopySubpix_32f_C1R_t icvCopySubpix_32f_C1R_p = 0;*/
503 ICV_DEF_GET_RECT_SUB_PIX_FUNC( 8u, uchar, uchar, int, CV_NOP, ICV_SCALE, ICV_DESCALE )
504 //ICV_DEF_GET_RECT_SUB_PIX_FUNC( 8u32f, uchar, float, float, CV_8TO32F, CV_NOP, CV_NOP )
505 ICV_DEF_GET_RECT_SUB_PIX_FUNC( 32f, float, float, float, CV_NOP, CV_NOP, CV_NOP )
507 ICV_DEF_GET_RECT_SUB_PIX_FUNC_C3( 8u, uchar, uchar, int, CV_NOP, ICV_SCALE, ICV_MUL_SCALE )
508 ICV_DEF_GET_RECT_SUB_PIX_FUNC_C3( 8u32f, uchar, float, float, CV_8TO32F, CV_NOP, CV_MUL )
509 ICV_DEF_GET_RECT_SUB_PIX_FUNC_C3( 32f, float, float, float, CV_NOP, CV_NOP, CV_MUL )
512 #define ICV_DEF_INIT_SUBPIX_TAB( FUNCNAME, FLAG ) \
513 static void icvInit##FUNCNAME##FLAG##Table( CvFuncTable* tab ) \
515 tab->fn_2d[CV_8U] = (void*)icv##FUNCNAME##_8u_##FLAG; \
516 tab->fn_2d[CV_32F] = (void*)icv##FUNCNAME##_32f_##FLAG; \
518 tab->fn_2d[1] = (void*)icv##FUNCNAME##_8u32f_##FLAG; \
522 ICV_DEF_INIT_SUBPIX_TAB( GetRectSubPix, C1R )
523 ICV_DEF_INIT_SUBPIX_TAB( GetRectSubPix, C3R )
525 typedef CvStatus (CV_STDCALL *CvGetRectSubPixFunc)( const void* src, int src_step,
526 CvSize src_size, void* dst,
527 int dst_step, CvSize win_size,
528 CvPoint2D32f center );
531 cvGetRectSubPix( const void* srcarr, void* dstarr, CvPoint2D32f center )
533 static CvFuncTable gr_tab[2];
534 static int inittab = 0;
535 CV_FUNCNAME( "cvGetRectSubPix" );
539 CvMat srcstub, *src = (CvMat*)srcarr;
540 CvMat dststub, *dst = (CvMat*)dstarr;
541 CvSize src_size, dst_size;
542 CvGetRectSubPixFunc func;
543 int cn, src_step, dst_step;
547 icvInitGetRectSubPixC1RTable( gr_tab + 0 );
548 icvInitGetRectSubPixC3RTable( gr_tab + 1 );
553 CV_CALL( src = cvGetMat( src, &srcstub ));
556 CV_CALL( dst = cvGetMat( dst, &dststub ));
558 cn = CV_MAT_CN( src->type );
560 if( (cn != 1 && cn != 3) || !CV_ARE_CNS_EQ( src, dst ))
561 CV_ERROR( CV_StsUnsupportedFormat, "" );
563 src_size = cvGetMatSize( src );
564 dst_size = cvGetMatSize( dst );
565 src_step = src->step ? src->step : CV_STUB_STEP;
566 dst_step = dst->step ? dst->step : CV_STUB_STEP;
568 //if( dst_size.width > src_size.width || dst_size.height > src_size.height )
569 // CV_ERROR( CV_StsBadSize, "destination ROI must be smaller than source ROI" );
571 if( CV_ARE_DEPTHS_EQ( src, dst ))
573 func = (CvGetRectSubPixFunc)(gr_tab[cn != 1].fn_2d[CV_MAT_DEPTH(src->type)]);
577 if( CV_MAT_DEPTH( src->type ) != CV_8U || CV_MAT_DEPTH( dst->type ) != CV_32F )
578 CV_ERROR( CV_StsUnsupportedFormat, "" );
580 func = (CvGetRectSubPixFunc)(gr_tab[cn != 1].fn_2d[1]);
584 CV_ERROR( CV_StsUnsupportedFormat, "" );
586 IPPI_CALL( func( src->data.ptr, src_step, src_size,
587 dst->data.ptr, dst_step, dst_size, center ));
593 #define ICV_32F8U(x) ((uchar)cvRound(x))
595 #define ICV_DEF_GET_QUADRANGLE_SUB_PIX_FUNC( flavor, srctype, dsttype, \
596 worktype, cast_macro, cvt ) \
597 CvStatus CV_STDCALL \
598 icvGetQuadrangleSubPix_##flavor##_C1R \
599 ( const srctype * src, int src_step, CvSize src_size, \
600 dsttype *dst, int dst_step, CvSize win_size, const float *matrix ) \
603 double dx = (win_size.width - 1)*0.5; \
604 double dy = (win_size.height - 1)*0.5; \
605 double A11 = matrix[0], A12 = matrix[1], A13 = matrix[2]-A11*dx-A12*dy; \
606 double A21 = matrix[3], A22 = matrix[4], A23 = matrix[5]-A21*dx-A22*dy; \
608 src_step /= sizeof(srctype); \
609 dst_step /= sizeof(dsttype); \
611 for( y = 0; y < win_size.height; y++, dst += dst_step ) \
613 double xs = A12*y + A13; \
614 double ys = A22*y + A23; \
615 double xe = A11*(win_size.width-1) + A12*y + A13; \
616 double ye = A21*(win_size.width-1) + A22*y + A23; \
618 if( (unsigned)(cvFloor(xs)-1) < (unsigned)(src_size.width - 3) && \
619 (unsigned)(cvFloor(ys)-1) < (unsigned)(src_size.height - 3) && \
620 (unsigned)(cvFloor(xe)-1) < (unsigned)(src_size.width - 3) && \
621 (unsigned)(cvFloor(ye)-1) < (unsigned)(src_size.height - 3)) \
623 for( x = 0; x < win_size.width; x++ ) \
625 int ixs = cvFloor( xs ); \
626 int iys = cvFloor( ys ); \
627 const srctype *ptr = src + src_step*iys + ixs; \
628 double a = xs - ixs, b = ys - iys, a1 = 1.f - a; \
629 worktype p0 = cvt(ptr[0])*a1 + cvt(ptr[1])*a; \
630 worktype p1 = cvt(ptr[src_step])*a1 + cvt(ptr[src_step+1])*a;\
634 dst[x] = cast_macro(p0 + b * (p1 - p0)); \
639 for( x = 0; x < win_size.width; x++ ) \
641 int ixs = cvFloor( xs ), iys = cvFloor( ys ); \
642 double a = xs - ixs, b = ys - iys, a1 = 1.f - a; \
643 const srctype *ptr0, *ptr1; \
645 xs += A11; ys += A21; \
647 if( (unsigned)iys < (unsigned)(src_size.height-1) ) \
648 ptr0 = src + src_step*iys, ptr1 = ptr0 + src_step; \
650 ptr0 = ptr1 = src + (iys < 0 ? 0 : src_size.height-1)*src_step; \
652 if( (unsigned)ixs < (unsigned)(src_size.width-1) ) \
654 p0 = cvt(ptr0[ixs])*a1 + cvt(ptr0[ixs+1])*a; \
655 p1 = cvt(ptr1[ixs])*a1 + cvt(ptr1[ixs+1])*a; \
659 ixs = ixs < 0 ? 0 : src_size.width - 1; \
660 p0 = cvt(ptr0[ixs]); p1 = cvt(ptr1[ixs]); \
662 dst[x] = cast_macro(p0 + b * (p1 - p0)); \
671 #define ICV_DEF_GET_QUADRANGLE_SUB_PIX_FUNC_C3( flavor, srctype, dsttype, \
672 worktype, cast_macro, cvt ) \
673 static CvStatus CV_STDCALL \
674 icvGetQuadrangleSubPix_##flavor##_C3R \
675 ( const srctype * src, int src_step, CvSize src_size, \
676 dsttype *dst, int dst_step, CvSize win_size, const float *matrix ) \
679 double dx = (win_size.width - 1)*0.5; \
680 double dy = (win_size.height - 1)*0.5; \
681 double A11 = matrix[0], A12 = matrix[1], A13 = matrix[2]-A11*dx-A12*dy; \
682 double A21 = matrix[3], A22 = matrix[4], A23 = matrix[5]-A21*dx-A22*dy; \
684 src_step /= sizeof(srctype); \
685 dst_step /= sizeof(dsttype); \
687 for( y = 0; y < win_size.height; y++, dst += dst_step ) \
689 double xs = A12*y + A13; \
690 double ys = A22*y + A23; \
691 double xe = A11*(win_size.width-1) + A12*y + A13; \
692 double ye = A21*(win_size.width-1) + A22*y + A23; \
694 if( (unsigned)(cvFloor(xs)-1) < (unsigned)(src_size.width - 3) && \
695 (unsigned)(cvFloor(ys)-1) < (unsigned)(src_size.height - 3) && \
696 (unsigned)(cvFloor(xe)-1) < (unsigned)(src_size.width - 3) && \
697 (unsigned)(cvFloor(ye)-1) < (unsigned)(src_size.height - 3)) \
699 for( x = 0; x < win_size.width; x++ ) \
701 int ixs = cvFloor( xs ); \
702 int iys = cvFloor( ys ); \
703 const srctype *ptr = src + src_step*iys + ixs*3; \
704 double a = xs - ixs, b = ys - iys, a1 = 1.f - a; \
709 p0 = cvt(ptr[0])*a1 + cvt(ptr[3])*a; \
710 p1 = cvt(ptr[src_step])*a1 + cvt(ptr[src_step+3])*a; \
711 dst[x*3] = cast_macro(p0 + b * (p1 - p0)); \
713 p0 = cvt(ptr[1])*a1 + cvt(ptr[4])*a; \
714 p1 = cvt(ptr[src_step+1])*a1 + cvt(ptr[src_step+4])*a; \
715 dst[x*3+1] = cast_macro(p0 + b * (p1 - p0)); \
717 p0 = cvt(ptr[2])*a1 + cvt(ptr[5])*a; \
718 p1 = cvt(ptr[src_step+2])*a1 + cvt(ptr[src_step+5])*a; \
719 dst[x*3+2] = cast_macro(p0 + b * (p1 - p0)); \
724 for( x = 0; x < win_size.width; x++ ) \
726 int ixs = cvFloor(xs), iys = cvFloor(ys); \
727 double a = xs - ixs, b = ys - iys; \
728 const srctype *ptr0, *ptr1; \
729 xs += A11; ys += A21; \
731 if( (unsigned)iys < (unsigned)(src_size.height-1) ) \
732 ptr0 = src + src_step*iys, ptr1 = ptr0 + src_step; \
734 ptr0 = ptr1 = src + (iys < 0 ? 0 : src_size.height-1)*src_step; \
736 if( (unsigned)ixs < (unsigned)(src_size.width - 1) ) \
738 double a1 = 1.f - a; \
740 ptr0 += ixs*3; ptr1 += ixs*3; \
741 p0 = cvt(ptr0[0])*a1 + cvt(ptr0[3])*a; \
742 p1 = cvt(ptr1[0])*a1 + cvt(ptr1[3])*a; \
743 dst[x*3] = cast_macro(p0 + b * (p1 - p0)); \
745 p0 = cvt(ptr0[1])*a1 + cvt(ptr0[4])*a; \
746 p1 = cvt(ptr1[1])*a1 + cvt(ptr1[4])*a; \
747 dst[x*3+1] = cast_macro(p0 + b * (p1 - p0)); \
749 p0 = cvt(ptr0[2])*a1 + cvt(ptr0[5])*a; \
750 p1 = cvt(ptr1[2])*a1 + cvt(ptr1[5])*a; \
751 dst[x*3+2] = cast_macro(p0 + b * (p1 - p0)); \
755 double b1 = 1.f - b; \
756 ixs = ixs < 0 ? 0 : src_size.width - 1; \
757 ptr0 += ixs*3; ptr1 += ixs*3; \
759 dst[x*3] = cast_macro(cvt(ptr0[0])*b1 + cvt(ptr1[0])*b);\
760 dst[x*3+1]=cast_macro(cvt(ptr0[1])*b1 + cvt(ptr1[1])*b);\
761 dst[x*3+2]=cast_macro(cvt(ptr0[2])*b1 + cvt(ptr1[2])*b);\
771 /*#define srctype uchar
772 #define dsttype uchar
773 #define worktype float
774 #define cvt CV_8TO32F
775 #define cast_macro ICV_32F8U
783 ICV_DEF_GET_QUADRANGLE_SUB_PIX_FUNC( 8u, uchar, uchar, double, ICV_32F8U, CV_8TO32F )
784 ICV_DEF_GET_QUADRANGLE_SUB_PIX_FUNC( 32f, float, float, double, CV_CAST_32F, CV_NOP )
785 ICV_DEF_GET_QUADRANGLE_SUB_PIX_FUNC( 8u32f, uchar, float, double, CV_CAST_32F, CV_8TO32F )
787 ICV_DEF_GET_QUADRANGLE_SUB_PIX_FUNC_C3( 8u, uchar, uchar, double, ICV_32F8U, CV_8TO32F )
788 ICV_DEF_GET_QUADRANGLE_SUB_PIX_FUNC_C3( 32f, float, float, double, CV_CAST_32F, CV_NOP )
789 ICV_DEF_GET_QUADRANGLE_SUB_PIX_FUNC_C3( 8u32f, uchar, float, double, CV_CAST_32F, CV_8TO32F )
791 ICV_DEF_INIT_SUBPIX_TAB( GetQuadrangleSubPix, C1R )
792 ICV_DEF_INIT_SUBPIX_TAB( GetQuadrangleSubPix, C3R )
794 typedef CvStatus (CV_STDCALL *CvGetQuadrangleSubPixFunc)(
795 const void* src, int src_step,
796 CvSize src_size, void* dst,
797 int dst_step, CvSize win_size,
798 const float* matrix );
801 cvGetQuadrangleSubPix( const void* srcarr, void* dstarr, const CvMat* mat )
803 static CvFuncTable gq_tab[2];
804 static int inittab = 0;
805 CV_FUNCNAME( "cvGetQuadrangleSubPix" );
809 CvMat srcstub, *src = (CvMat*)srcarr;
810 CvMat dststub, *dst = (CvMat*)dstarr;
811 CvSize src_size, dst_size;
812 CvGetQuadrangleSubPixFunc func;
818 icvInitGetQuadrangleSubPixC1RTable( gq_tab + 0 );
819 icvInitGetQuadrangleSubPixC3RTable( gq_tab + 1 );
824 CV_CALL( src = cvGetMat( src, &srcstub ));
827 CV_CALL( dst = cvGetMat( dst, &dststub ));
830 CV_ERROR( CV_StsBadArg, "map matrix is not valid" );
832 cn = CV_MAT_CN( src->type );
834 if( (cn != 1 && cn != 3) || !CV_ARE_CNS_EQ( src, dst ))
835 CV_ERROR( CV_StsUnsupportedFormat, "" );
837 src_size = cvGetMatSize( src );
838 dst_size = cvGetMatSize( dst );
840 /*if( dst_size.width > src_size.width || dst_size.height > src_size.height )
841 CV_ERROR( CV_StsBadSize, "destination ROI must not be larger than source ROI" );*/
843 if( mat->rows != 2 || mat->cols != 3 )
844 CV_ERROR( CV_StsBadArg,
845 "Transformation matrix must be 2x3" );
847 if( CV_MAT_TYPE( mat->type ) == CV_32FC1 )
849 for( k = 0; k < 3; k++ )
851 m[k] = mat->data.fl[k];
852 m[3 + k] = ((float*)(mat->data.ptr + mat->step))[k];
855 else if( CV_MAT_TYPE( mat->type ) == CV_64FC1 )
857 for( k = 0; k < 3; k++ )
859 m[k] = (float)mat->data.db[k];
860 m[3 + k] = (float)((double*)(mat->data.ptr + mat->step))[k];
864 CV_ERROR( CV_StsUnsupportedFormat,
865 "The transformation matrix should have 32fC1 or 64fC1 type" );
867 if( CV_ARE_DEPTHS_EQ( src, dst ))
869 func = (CvGetQuadrangleSubPixFunc)(gq_tab[cn != 1].fn_2d[CV_MAT_DEPTH(src->type)]);
873 if( CV_MAT_DEPTH( src->type ) != CV_8U || CV_MAT_DEPTH( dst->type ) != CV_32F )
874 CV_ERROR( CV_StsUnsupportedFormat, "" );
876 func = (CvGetQuadrangleSubPixFunc)(gq_tab[cn != 1].fn_2d[1]);
880 CV_ERROR( CV_StsUnsupportedFormat, "" );
882 IPPI_CALL( func( src->data.ptr, src->step, src_size,
883 dst->data.ptr, dst->step, dst_size, m ));
889 void cv::getRectSubPix( const Mat& image, Size patchSize, Point2f center,
890 Mat& patch, int patchType )
892 patch.create(patchSize, patchType < 0 ? image.type() :
893 CV_MAKETYPE(CV_MAT_DEPTH(patchType),image.channels()));
894 CvMat _image = image, _patch = patch;
895 cvGetRectSubPix(&_image, &_patch, center);