/ *   
 
   *   P R O J E C T :   N y A R T o o l k i t C S ( E x t e n s i o n ) 
 
   *   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
   *   T h e   N y A R T o o l k i t C S   i s   C #   e d i t i o n   A R T o o l K i t   c l a s s   l i b r a r y . 
 
   *   C o p y r i g h t   ( C ) 2 0 0 8 - 2 0 0 9   R y o   I i z u k a 
 
   * 
 
   *   T h i s   p r o g r a m   i s   f r e e   s o f t w a r e :   y o u   c a n   r e d i s t r i b u t e   i t   a n d / o r   m o d i f y 
 
   *   i t   u n d e r   t h e   t e r m s   o f   t h e   G N U   G e n e r a l   P u b l i c   L i c e n s e   a s   p u b l i s h e d   b y 
 
   *   t h e   F r e e   S o f t w a r e   F o u n d a t i o n ,   e i t h e r   v e r s i o n   3   o f   t h e   L i c e n s e ,   o r 
 
   *   ( a t   y o u r   o p t i o n )   a n y   l a t e r   v e r s i o n . 
 
   *   
 
   *   T h i s   p r o g r a m   i s   d i s t r i b u t e d   i n   t h e   h o p e   t h a t   i t   w i l l   b e   u s e f u l , 
 
   *   b u t   W I T H O U T   A N Y   W A R R A N T Y ;   w i t h o u t   e v e n   t h e   i m p l i e d   w a r r a n t y   o f 
 
   *   M E R C H A N T A B I L I T Y   o r   F I T N E S S   F O R   A   P A R T I C U L A R   P U R P O S E .     S e e   t h e 
 
   *   G N U   G e n e r a l   P u b l i c   L i c e n s e   f o r   m o r e   d e t a i l s . 
 
   * 
 
   *   Y o u   s h o u l d   h a v e   r e c e i v e d   a   c o p y   o f   t h e   G N U   G e n e r a l   P u b l i c   L i c e n s e 
 
   *   a l o n g   w i t h   t h i s   p r o g r a m .     I f   n o t ,   s e e   < h t t p : / / w w w . g n u . o r g / l i c e n s e s / > . 
 
   *   
 
   *   F o r   f u r t h e r   i n f o r m a t i o n   p l e a s e   c o n t a c t . 
 
   * 	 h t t p : / / n y a t l a . j p / n y a t o o l k i t / 
 
   * 	 < a i r m a i l ( a t ) e b o n y . p l a l a . o r . j p >   o r   < n y a t l a ( a t ) n y a t l a . j p > 
 
   *   
 
   * / 
 
 u s i n g   S y s t e m ; 
 
 u s i n g   S y s t e m . D i a g n o s t i c s ; 
 
 u s i n g   j p . n y a t l a . n y a r t o o l k i t . c s . c o r e ; 
 
 
 
 n a m e s p a c e   j p . n y a t l a . n y a r t o o l k i t . c s . r p f 
 
 { 
 
         / * * 
 
           *   0000000000k0[Y000yrkj0;u }0000Kbk0cOW0~0Y00
 
           * 
 
           * / 
 
         p u b l i c   c l a s s   N y A R V e c t o r R e a d e r _ I N T 1 D _ G R A Y _ 8   :   N y A R V e c t o r R e a d e r _ B a s e 
 
         { 
 
 	         / * * 
 
 	           *   
 
 	           *   @ p a r a m   i _ r e f _ r a s t e r 
 
 	           *   W,g;uP
 
 	           *   @ p a r a m   i _ r e f _ r a s t e r _ d i s t o r t i o n 
 
 	           *   jk0d000000( n u l l n04XTjk0d0weu) 
 
 	           *   @ p a r a m   i _ r e f _ r o b _ r a s t e r 
 
 	           *   000c"}(un0R O B ;uP
 
 	           *   @ p a r a m   
 
 	           * / 
 
 	         p u b l i c   N y A R V e c t o r R e a d e r _ I N T 1 D _ G R A Y _ 8 ( N y A R G r a y s c a l e R a s t e r   i _ r e f _ r a s t e r , I N y A R C a m e r a D i s t o r t i o n F a c t o r   i _ r e f _ r a s t e r _ d i s t o r t i o n , N y A R G r a y s c a l e R a s t e r   i _ r e f _ r o b _ r a s t e r ) 
 
                         : b a s e ( ) 
 
 
 
 	         { 
 
 	 	         D e b u g . A s s e r t   ( i _ r e f _ r a s t e r . g e t B u f f e r T y p e ( )   = =   N y A R B u f f e r T y p e . I N T 1 D _ G R A Y _ 8 ) ; 
 
 	 	         D e b u g . A s s e r t   ( i _ r e f _ r o b _ r a s t e r . g e t B u f f e r T y p e ( )   = =   N y A R B u f f e r T y p e . I N T 1 D _ G R A Y _ 8 ) ; 
 
 	 	         t h i s . i n i t I n s t a n c e ( i _ r e f _ r a s t e r ,   i _ r e f _ r a s t e r _ d i s t o r t i o n ,   i _ r e f _ r o b _ r a s t e r , n e w   N y A R C o n t o u r P i c k u p ( ) ) ; 
 
 	         } 
 
 
 
 	         / * * 
 
 	           *   ;u }n04 яPn0;u }00000S_W0~0Y00  S_Sj0{Vo00R a s t e r n01 000QtPg0Y00  0   , - 1 ,   0   0 ,   0 ,   0   0   ,   x , 
 
 	           *   0  0+   - 1 ,   y , + 1   0   , + 1 ,   0   0 ,   0 ,   0 
 
 	           *   
 
 	           *   @ p a r a m   i _ r a s t e r 
 
 	           *   @ p a r a m   x 
 
 	           *   @ p a r a m   y 
 
 	           *   @ p a r a m   o _ v 
 
 	           * / 
 
         / * 	 *gO(uk0d0M00000000
 
 	         p u b l i c   v o i d   g e t P i x e l V e c t o r 4 ( i n t   x ,   i n t   y ,   N y A R I n t P o i n t 2 d   o _ v ) 
 
 	         { 
 
 	 	         a s s e r t   ( ( x   >   0 )   & &   ( y   >   0 )   & &   ( x )   <   t h i s . _ r e f _ b a s e _ r a s t e r . g e t W i d t h ( )   & &   ( y )   <   t h i s . _ r e f _ b a s e _ r a s t e r . g e t H e i g h t ( ) ) ; 
 
 	 	         i n t [ ]   b u f   =   ( i n t [ ] ) ( t h i s . _ r e f _ b a s e _ r a s t e r . g e t B u f f e r ( ) ) ; 
 
 	 	         i n t   w   =   t h i s . _ r e f _ b a s e _ r a s t e r . g e t W i d t h ( ) ; 
 
 	 	         i n t   i d x   =   w   *   y   +   x ; 
 
 	 	         o _ v . x   =   ( b u f [ i d x   +   1 ]   -   b u f [ i d x   -   1 ] )   > >   1 ; 
 
 	 	         o _ v . y   =   ( b u f [ i d x   +   w ]   -   b u f [ i d x   -   w ] )   > >   1 ; 
 
 	 	         / / jk0܈cki0F0Y00n0
 
 	         } 
 
         * / 
 
 	         / * * 
 
 	           *   ;u }n08 яP;u }00000S_W0~0Y00  S_Sj0{Vo00R a s t e r n01 000QtPg0Y00
 
 	           *     - 1 , - 2 , - 1  0 0 0 0- 1 ,  00 , + 1 
 
 	           *       0 ,   y ,  00   +  0 0- 2 ,   x , + 2 
 
 	           *     + 1 , + 2 , + 1    0 0 0 0- 1 ,   0 , + 1 
 
 	           *   
 
 	           *   @ p a r a m   i _ r a s t e r 
 
 	           *   @ p a r a m   x 
 
 	           *   @ p a r a m   y 
 
 	           *   @ p a r a m   o _ v 
 
 	           * / 
 
         / *     *gO(uk0d0M00000000
 
 	         p u b l i c   v o i d   g e t P i x e l V e c t o r 8 ( i n t   x ,   i n t   y ,   N y A R I n t P o i n t 2 d   o _ v )   { 
 
 	 	         a s s e r t   ( ( x   >   0 )   & &   ( y   >   0 )   & &   ( x )   <   t h i s . _ r e f _ b a s e _ r a s t e r . g e t W i d t h ( )   & &   ( y )   <   t h i s . _ r e f _ b a s e _ r a s t e r . g e t H e i g h t ( ) ) ; 
 
 	 	         i n t [ ]   b u f   =   ( i n t [ ] ) t h i s . _ r e f _ b a s e _ r a s t e r . g e t B u f f e r ( ) ; 
 
 	 	         i n t   s w   =   t h i s . _ r e f _ b a s e _ r a s t e r . g e t W i d t h ( ) ; 
 
 	 	         i n t   i d x _ 0   =   s w   *   y   +   x ; 
 
 	 	         i n t   i d x _ p 1   =   i d x _ 0   +   s w ; 
 
 	 	         i n t   i d x _ m 1   =   i d x _ 0   -   s w ; 
 
 	 	         i n t   b   =   b u f [ i d x _ m 1   -   1 ] ; 
 
 	 	         i n t   d   =   b u f [ i d x _ m 1   +   1 ] ; 
 
 	 	         i n t   h   =   b u f [ i d x _ p 1   -   1 ] ; 
 
 	 	         i n t   f   =   b u f [ i d x _ p 1   +   1 ] ; 
 
 	 	         o _ v . x   =   ( ( b u f [ i d x _ 0   +   1 ]   -   b u f [ i d x _ 0   -   1 ] )   > >   1 ) 
 
 	 	 	 	         +   ( ( d   -   b   +   f   -   h )   > >   2 ) ; 
 
 	 	         o _ v . y   =   ( ( b u f [ i d x _ p 1 ]   -   b u f [ i d x _ m 1 ] )   > >   1 )   +   ( ( f   -   d   +   h   -   b )   > >   2 ) ; 
 
 	 	         / / jk0܈cki0F0Y00n0
 
 	         } 
 
         * / 
 
                 p r i v a t e   N y A R D o u b l e P o i n t 2 d   _ _ t m p   =   n e w   N y A R D o u b l e P o i n t 2 d ( ) ; 
 
 
 
 	         / * * 
 
 	           *   R E C T {VQn0;u }0000n0T$Ph000000n0000-N_0S_W0~0Y00  3 2 0 * 2 4 0 n04XT0
 
 	           *   R E C T n0{Vo0( x > = 0   & &   x < 3 1 9   x + w > = 0   & &   x + w < 3 1 9 ) , ( y > = 0   & &   y < 2 3 9   x + w > = 0   & &   x + w < 3 1 9 ) h0j00~0Y00
 
 	           *   @ p a r a m   i x 
 
 	           *   0000S_0LF0MOn0-[W0~0Y00
 
 	           *   @ p a r a m   i y 
 
 	           *   0000S_0LF0MOn0-[W0~0Y00
 
 	           *   @ p a r a m   i w 
 
 	           *   0000S_0LF0{V0-[W0~0Y00
 
 	           *   @ p a r a m   i h 
 
 	           *   0000S_0LF0{V0-[W0~0Y00
 
 	           *   @ p a r a m   o _ p o s v e c 
 
 	           *   000-N_h000000ԏW0~0Y00
 
 	           *   @ r e t u r n 
 
 	           *   0000n07_^0ԏW0~0Y007_^$Po00]R$Pn0NWNn0Tg0Y00
 
 	           * / 
 
                 p u b l i c   o v e r r i d e   i n t   g e t A r e a V e c t o r 3 3 ( i n t   i x ,   i n t   i y ,   i n t   i w ,   i n t   i h ,   N y A R V e c L i n e a r 2 d   o _ p o s v e c ) 
 
 	         { 
 
 	 	         D e b u g . A s s e r t   ( i h   > =   3   & &   i w   > =   3 ) ; 
 
 	 	         D e b u g . A s s e r t   ( ( i x   > =   0 )   & &   ( i y   > =   0 )   & &   ( i x   +   i w )   < =   t h i s . _ r e f _ b a s e _ r a s t e r . g e t W i d t h ( )   & &   ( i y   +   i h )   < =   t h i s . _ r e f _ b a s e _ r a s t e r . g e t H e i g h t ( ) ) ; 
 
 	 	         i n t [ ]   b u f   = ( i n t [ ] ) t h i s . _ r e f _ b a s e _ r a s t e r . g e t B u f f e r ( ) ; 
 
 	 	         i n t   s t r i d e   = t h i s . _ r e f _ b a s e _ r a s t e r . g e t W i d t h ( ) ; 
 
 	 	         / /   x = ( | V x | * X n ) / n , y = ( | V y | * Y n ) / n 
 
 	 	         / /   x = ( V x ) ^ 2 / ( V x + V y ) ^ 2 , y = ( V y ) ^ 2 / ( V x + V y ) ^ 2 
 
 	 	         i n t   s u m _ x ,   s u m _ y ,   s u m _ w x ,   s u m _ w y ,   s u m _ v x ,   s u m _ v y ; 
 
 	 	         s u m _ x   =   s u m _ y   =   s u m _ w x   =   s u m _ w y   =   s u m _ v x   =   s u m _ v y   =   0 ; 
 
 	 	         i n t   l w = i w   -   3 ; 
 
 	 	         i n t   v x ,   v y ; 
 
 	 	         f o r   ( i n t   i   =   i h   -   3 ;   i   > =   0 ;   i - - )   { 
 
 	 	 	         i n t   i d x _ 0   =   s t r i d e   *   ( i   +   1   +   i y )   +   ( i w   -   3   +   1   +   i x ) ; 
 
 	 	 	         f o r   ( i n t   i 2   =   l w ;   i 2   > =   0 ;   i 2 - - ) { 
 
 	 	 	 	         / /   1 000Rn000000{
 
 	 	 	 	         i n t   i d x _ p 1   =   i d x _ 0   +   s t r i d e ; 
 
 	 	 	 	         i n t   i d x _ m 1   =   i d x _ 0   -   s t r i d e ; 
 
 	 	 	 	         i n t   b   =   b u f [ i d x _ m 1   -   1 ] ; 
 
 	 	 	 	         i n t   d   =   b u f [ i d x _ m 1   +   1 ] ; 
 
 	 	 	 	         i n t   h   =   b u f [ i d x _ p 1   -   1 ] ; 
 
 	 	 	 	         i n t   f   =   b u f [ i d x _ p 1   +   1 ] ; 
 
 	 	 	 	         v x   =   ( ( b u f [ i d x _ 0   +   1 ]   -   b u f [ i d x _ 0   -   1 ] )   > >   1 ) +   ( ( d   -   b   +   f   -   h )   > >   2 ) ; 
 
 	 	 	 	         v y   =   ( ( b u f [ i d x _ p 1 ]   -   b u f [ i d x _ m 1 ] )   > >   1 ) +   ( ( f   -   d   +   h   -   b )   > >   2 ) ; 
 
 	 	 	 	         i d x _ 0 - - ; 
 
 
 
 	 	 	 	         / /   R͑o0v e c t o r n0v}[$P
 
 	 	 	 	         i n t   w x   =   v x   *   v x ; 
 
 	 	 	 	         i n t   w y   =   v y   *   v y ; 
 
 	 	 	 	         s u m _ w x   + =   w x ;   / / R͑$P
 
 	 	 	 	         s u m _ w y   + =   w y ;   / / R͑$P
 
 	 	 	 	         s u m _ v x   + =   w x   *   v x ;   / / R͑* 0000n0Mz
 
 	 	 	 	         s u m _ v y   + =   w y   *   v y ;   / / R͑* 0000n0Mz
 
 	 	 	 	         s u m _ x   + =   w x   *   ( i 2   +   1 ) ; / / MOn
 
 	 	 	 	         s u m _ y   + =   w y   *   ( i   +   1 ) ;   / / 
 
 	 	 	         } 
 
 	 	         } 
 
 	 	         / / x , d x , y , d y n0{
 
 	 	         d o u b l e   x x , y y ; 
 
 	 	         i f   ( s u m _ w x   = =   0 )   { 
 
 	 	 	         x x   =   i x   +   ( i w   > >   1 ) ; 
 
 	 	 	         o _ p o s v e c . d x   =   0 ; 
 
 	 	         }   e l s e   { 
 
 	 	 	         x x   =   i x + ( d o u b l e )   s u m _ x   /   s u m _ w x ; 
 
 	 	 	         o _ p o s v e c . d x   =   ( d o u b l e )   s u m _ v x   /   s u m _ w x ; 
 
 	 	         } 
 
 	 	         i f   ( s u m _ w y   = =   0 )   { 
 
 	 	 	         y y   =   i y   +   ( i h   > >   1 ) ; 
 
 	 	 	         o _ p o s v e c . d y   =   0 ; 
 
 	 	         }   e l s e   { 
 
 	 	 	         y y   =   i y + ( d o u b l e )   s u m _ y   /   s u m _ w y ; 
 
 	 	 	         o _ p o s v e c . d y   =   ( d o u b l e )   s u m _ v y   /   s u m _ w y ; 
 
 	 	         } 
 
 	 	         / / _j00jk00d
 
 	 	         i f ( t h i s . _ f a c t o r ! = n u l l ) { 
 
                                 t h i s . _ f a c t o r . o b s e r v 2 I d e a l ( x x ,   y y ,   t h i s . _ _ t m p ) ; 
 
                                 o _ p o s v e c . x   =   t h i s . _ _ t m p . x ; 
 
                                 o _ p o s v e c . y   =   t h i s . _ _ t m p . y ; 
 
 	 	         } e l s e { 
 
 	 	 	         o _ p o s v e c . x = x x ; 
 
 	 	 	         o _ p o s v e c . y = y y ; 
 
 	 	         } 
 
 	 	         / / R͑s^GWn0Rk0ԏtS
 
 	 	         r e t u r n   s u m _ w x + s u m _ w y ; 
 
 	         } 
 
 	         p u b l i c   o v e r r i d e   i n t   g e t A r e a V e c t o r 2 2 ( i n t   i x ,   i n t   i y ,   i n t   i w ,   i n t   i h , N y A R V e c L i n e a r 2 d   o _ p o s v e c ) 
 
 	         { 
 
 	 	         D e b u g . A s s e r t   ( i h   > =   3   & &   i w   > =   3 ) ; 
 
 	 	         D e b u g . A s s e r t   ( ( i x   > =   0 )   & &   ( i y   > =   0 )   & &   ( i x   +   i w )   < =   t h i s . _ r e f _ b a s e _ r a s t e r . g e t W i d t h ( )   & &   ( i y   +   i h )   < =   t h i s . _ r e f _ b a s e _ r a s t e r . g e t H e i g h t ( ) ) ; 
 
 	 	         i n t [ ]   b u f   = ( i n t [ ] ) t h i s . _ r e f _ b a s e _ r a s t e r . g e t B u f f e r ( ) ; 
 
 	 	         i n t   s t r i d e   = t h i s . _ r e f _ b a s e _ r a s t e r . g e t W i d t h ( ) ; 
 
 	 	         i n t   s u m _ x ,   s u m _ y ,   s u m _ w x ,   s u m _ w y ,   s u m _ v x ,   s u m _ v y ; 
 
 	 	         s u m _ x   =   s u m _ y   =   s u m _ w x   =   s u m _ w y   =   s u m _ v x   =   s u m _ v y   =   0 ; 
 
 	 	         i n t   v x ,   v y ; 
 
 	 	         i n t   l l = i w - 1 ; 
 
 	 	         f o r   ( i n t   i   =   0 ;   i < i h - 1 ;   i + + )   { 
 
 	 	 	         i n t   i d x _ 0   =   s t r i d e   *   ( i + i y )   +   i x + 1 ; 
 
 	 	 	         i n t   a = b u f [ i d x _ 0 - 1 ] ; 
 
 	 	 	         i n t   b = b u f [ i d x _ 0 ] ; 
 
 	 	 	         i n t   c = b u f [ i d x _ 0 + s t r i d e - 1 ] ; 
 
 	 	 	         i n t   d = b u f [ i d x _ 0 + s t r i d e ] ; 
 
 	 	 	         f o r   ( i n t   i 2   =   0 ;   i 2 < l l ;   i 2 + + ) { 
 
 	 	 	 	         / /   1 000Rn000000{
 
 	 	 	 	         v x = ( b - a + d - c ) > > 2 ; 
 
 	 	 	 	         v y = ( c - a + d - b ) > > 2 ; 
 
 	 	 	 	         i d x _ 0 + + ; 
 
 	 	 	 	         a = b ; 
 
 	 	 	 	         c = d ; 
 
 	 	 	 	         b = b u f [ i d x _ 0 ] ; 
 
 	 	 	 	         d = b u f [ i d x _ 0 + s t r i d e ] ; 
 
 
 
 	 	 	 	         / /   R͑o0v e c t o r n0v}[$P
 
 	 	 	 	         i n t   w x   =   v x   *   v x ; 
 
 	 	 	 	         s u m _ w x   + =   w x ;   / / R͑$P
 
 	 	 	 	         s u m _ v x   + =   w x   *   v x ;   / / R͑* 0000n0Mz
 
 	 	 	 	         s u m _ x   + =   w x   *   i 2 ; / / MOn
 
 
 
 	 	 	 	         i n t   w y   =   v y   *   v y ; 
 
 	 	 	 	         s u m _ w y   + =   w y ;   / / R͑$P
 
 	 	 	 	         s u m _ v y   + =   w y   *   v y ;   / / R͑* 0000n0Mz
 
 	 	 	 	         s u m _ y   + =   w y   *   i ;   / / 
 
 	 	 	         } 
 
 	 	         } 
 
 	 	         / / x , d x , y , d y n0{
 
 	 	         d o u b l e   x x , y y ; 
 
 	 	         i f   ( s u m _ w x   = =   0 )   { 
 
 	 	 	         x x   =   i x   +   ( i w   > >   1 ) ; 
 
 	 	 	         o _ p o s v e c . d x   =   0 ; 
 
 	 	         }   e l s e   { 
 
 	 	 	         x x   =   i x + ( d o u b l e )   s u m _ x   /   s u m _ w x ; 
 
 	 	 	         o _ p o s v e c . d x   =   ( d o u b l e )   s u m _ v x   /   s u m _ w x ; 
 
 	 	         } 
 
 	 	         i f   ( s u m _ w y   = =   0 )   { 
 
 	 	 	         y y   =   i y   +   ( i h   > >   1 ) ; 
 
 	 	 	         o _ p o s v e c . d y   =   0 ; 
 
 	 	         }   e l s e   { 
 
 	 	 	         y y   =   i y + ( d o u b l e )   s u m _ y   /   s u m _ w y ; 
 
 	 	 	         o _ p o s v e c . d y   =   ( d o u b l e )   s u m _ v y   /   s u m _ w y ; 
 
 	 	         } 
 
 	 	         / / _j00jk00d
 
 	 	         i f ( t h i s . _ f a c t o r ! = n u l l ) { 
 
                                 t h i s . _ f a c t o r . o b s e r v 2 I d e a l ( x x ,   y y ,   t h i s . _ _ t m p ) ; 
 
                                 o _ p o s v e c . x   =   t h i s . _ _ t m p . x ; 
 
                                 o _ p o s v e c . y   =   t h i s . _ _ t m p . y ; 
 
                         } 
 
                         e l s e 
 
                         { 
 
 	 	 	         o _ p o s v e c . x = x x ; 
 
 	 	 	         o _ p o s v e c . y = y y ; 
 
 	 	         } 
 
 	 	         / / R͑s^GWn0Rk0ԏtS
 
 	 	         r e t u r n   s u m _ w x + s u m _ w y ; 
 
 	         } 
 
         } 
 
 } 