/*! * \file yof_image_iterator.hpp * \brief 画像イテレータ * * \author Kenta Yoshimura * * Copyright(C) 2003-2004 Kenta Yoshimura All Rights Reserved. * * $Id$ */ #ifndef YOF_IMAGE_ITERATOR #define YOF_IMAGE_ITERATOR #if !defined(_MSC_VER) | _MSC_VER > 1000 #pragma once #pragma inline_recursion (on) #pragma inline_depth (255) #pragma auto_inline (on) #endif #include "yof_types.hpp" #include #include #include namespace yoffy { template< typename T > class image_iterator_base : public iterator< std::random_access_iterator_tag, T > { public: typedef image_iterator_base< T > self; typedef iterator< std::random_access_iterator_tag, T > super; typedef typename super::iterator_category iterator_category; typedef typename super::value_type value_type; typedef typename super::difference_type difference_type; typedef typename super::pointer pointer; typedef typename super::reference reference; typedef value_type template_value_type; typedef self owner_iterator; typedef const pointer const_pointer; typedef const value_type & const_reference; //typedef size_t size_type; typedef unsigned size_type; enum { pixels_of_value = 1 }; protected: pointer cursor; public: image_iterator_base() : cursor( NULL ) {} //! ポインタから iterator を作成 image_iterator_base( pointer ptr ) : cursor( ptr ) {} protected: void bump_up() { ++cursor; } void bump_down() { --cursor; } difference_type diff( const self & src ) const { return cursor - src.cursor; } void increment( difference_type n ) { cursor += n; } void decrement( difference_type n ) { cursor -= n; } public: bool operator <( const self & src ) const { return cursor < src.cursor; } bool operator >( const self & src ) const { return cursor > src.cursor; } bool operator <=( const self & src ) const { return cursor <= src.cursor; } bool operator >=( const self & src ) const { return cursor >= src.cursor; } bool operator ==( const self & src ) const { return cursor == src.cursor; } bool operator !=( const self & src ) const { return cursor != src.cursor; } reference at( difference_type n ) { return cursor[ n ]; } const_reference const_at( difference_type n ) const { return cursor[ n ]; } reference value() { return *cursor; } const_reference const_value() const { return *cursor; } pointer get() { return cursor; } const_pointer const_get() const { return cursor; } }; template<> class image_iterator_base< bool > : public iterator< std::random_access_iterator_tag, uint8_t > { public: typedef image_iterator_base< bool > self; typedef iterator< std::random_access_iterator_tag, uint8_t > super; typedef super::pointer pointer; typedef bool template_value_type; typedef self owner_iterator; typedef const pointer const_pointer; //typedef size_t size_type; typedef unsigned size_type; enum { pixels_of_value = 8 }; class ref { private: value_type & m_value; value_type m_mask; public: ref( value_type & value, value_type mask ) : m_value( value ), m_mask( mask ) {} operator value_type() const { return (m_value & m_mask) ? 1 : 0; } ref & operator =( bool src ) { if ( src ) { m_value |= m_mask; } else { m_value &= ~m_mask; } return *this; } }; typedef ref reference; typedef const ref const_reference; protected: pointer cursor; size_type position; public: image_iterator_base() : cursor( NULL ), position( 0 ) {} //! ポインタから iterator の作成 image_iterator_base( pointer ptr, size_type inPosition = 0 ) : cursor( ptr ) , position( inPosition ) {} protected: void bump_up() { ++position; } void bump_down() { --position; } difference_type diff( const self & src ) const { return cursor - src.cursor + ((position - src.position) / 8); } void increment( difference_type n ) { position += size_type(n); } void decrement( difference_type n ) { position -= size_type(n); } public: bool operator <( const self & src ) const { return (ptrdiff_t(cursor) << 3) + position < (ptrdiff_t(src.cursor) << 3) + position; } bool operator >( const self & src ) const { return (ptrdiff_t(cursor) << 3) + position > (ptrdiff_t(src.cursor) << 3) + position; } bool operator <=( const self & src ) const { return (ptrdiff_t(cursor) << 3) + position <= (ptrdiff_t(src.cursor) << 3) + position; } bool operator >=( const self & src ) const { return (ptrdiff_t(cursor) << 3) + position >= (ptrdiff_t(src.cursor) << 3) + position; } bool operator ==( const self & src ) const { return (ptrdiff_t(cursor) << 3) + position == (ptrdiff_t(src.cursor) << 3) + position; } bool operator !=( const self & src ) const { return (ptrdiff_t(cursor) << 3) + position == (ptrdiff_t(src.cursor) << 3) + position; } reference at( difference_type n ) { size_type p = position + n; value_type mask; // mask = (value_type)0x80 >> (p % 8); // return ref( cursor[ p / 8 ], mask ); mask = (value_type)0x80 >> (value_type)(p & 0x07); return ref( cursor[ p >> 3 ], mask ); } const_reference const_at( difference_type n ) const { size_type p = position + n; value_type mask; // mask = (value_type)0x80 >> (p % 8); // return ref( cursor[ p / 8 ], mask ); mask = (value_type)0x80 >> (value_type)(p & 0x07); return ref( cursor[ p >> 3 ], mask ); } reference value() { value_type mask; // mask = (value_type)0x80 >> (position % 8); // return ref( cursor[ position / 8 ], mask ); mask = (value_type)0x80 >> (value_type)(position & 0x07); return ref( cursor[ position >> 3 ], mask ); } const_reference const_value() const { value_type mask; // mask = (value_type)0x80 >> (position % 8); // return ref( cursor[ position / 8 ], mask ); mask = (value_type)0x80 >> (value_type)(position & 0x07); return ref( cursor[ position >> 3 ], mask ); } //! ポインタの取得 pointer get() { return cursor + (position >> 3); } //! ポインタの取得 const_pointer const_get() const { return cursor + (position >> 3); } }; /*! yoffy::image 用の iterator * * *p++ = value; のような形で 1 次元に連続アクセスする場合に使用します。 */ template< class BASE_ITERATOR > class image_iterator : public BASE_ITERATOR { public: typedef image_iterator< BASE_ITERATOR > self; typedef BASE_ITERATOR super; typedef typename super::iterator_category iterator_category; typedef typename super::value_type value_type; typedef typename super::difference_type difference_type; typedef typename super::pointer pointer; typedef typename super::const_pointer const_pointer; typedef typename super::reference reference; typedef typename super::const_reference const_reference; typedef typename super::template_value_type template_value_type; typedef typename super::owner_iterator owner_iterator; typedef typename super::size_type size_type; image_iterator() {} //! ポインタから iterator を作成 image_iterator( pointer ptr ) : super( ptr ) {} image_iterator( super & ptr ) : super( ptr ) {} self & operator ++() { super::bump_up(); return *this; } self operator ++( int ) { self tmp = *this; super::bump_up(); return tmp; } self & operator --() { super::bump_down(); return *this; } self operator --( int ) { self tmp = *this; super::bump_down(); return tmp; } self operator +( difference_type n ) const { self tmp = *this; tmp.increment( n ); return tmp; } self operator -( difference_type n ) const { self tmp = *this; tmp.decrement( n ); return tmp; } difference_type operator -( const self & src ) const { return diff( src ); } self & operator +=( difference_type n ) { increment( n ); return *this; } self & operator -=( difference_type n ) { decrement( n ); return *this; } reference operator []( difference_type n ) { return at( n ); } const_reference operator []( difference_type n ) const { return const_at( n ); } reference operator *() { return super::value(); } const_reference operator *() const { return super::const_value(); } pointer operator ->() { return super::get(); } const_pointer operator ->() const { return super::const_get(); } }; template< class BASE_ITERATOR > class const_image_iterator : public BASE_ITERATOR { public: typedef const_image_iterator< BASE_ITERATOR > self; typedef BASE_ITERATOR super; typedef typename super::iterator_category iterator_category; typedef typename super::value_type value_type; typedef typename super::difference_type difference_type; typedef typename super::pointer pointer; typedef typename super::const_pointer const_pointer; typedef typename super::reference reference; typedef typename super::const_reference const_reference; typedef typename super::template_value_type template_value_type; typedef typename super::owner_iterator owner_iterator; typedef typename super::size_type size_type; const_image_iterator() {} //! ポインタから iterator を作成 const_image_iterator( pointer ptr ) : super( ptr ) {} //! clipimage_iterator と互換性を取るためのコンストラクタ const_image_iterator( self ptr, size_type origwidth, size_type left, size_type top, size_type clipwidth ) : super( ptr, origwidth, left, top, clipwidth ) {} const_image_iterator( const image_iterator< BASE_ITERATOR > & src ) : super( src ) {} const_image_iterator( const BASE_ITERATOR & src ) : super( src ) {} self & operator ++() { super::bump_up(); return *this; } self operator ++( int ) { self tmp = *this; super::bump_up(); return tmp; } self & operator --() { super::bump_down(); return *this; } self operator --( int ) { self tmp = *this; super::bump_down(); return tmp; } self operator +( difference_type n ) const { self tmp = *this; tmp.increment( n ); return tmp; } self operator -( difference_type n ) const { self tmp = *this; tmp.decrement( n ); return tmp; } difference_type operator -( const self & src ) const { return diff( src ); } self & operator +=( difference_type n ) { increment( n ); return *this; } self & operator -=( difference_type n ) { decrement( n ); return *this; } const_reference operator []( difference_type n ) const { return super::const_at( n ); } const_reference operator *() const { return super::const_value(); } const_pointer operator ->() const { return super::const_get(); } }; #define yof_image_iterator(type) yoffy::image_iterator< yoffy::image_iterator_base< type > > #define yof_const_image_iterator(type) yoffy::const_image_iterator< yoffy::image_iterator_base< type > > typedef yof_image_iterator( bool ) imageb_iterator; typedef yof_image_iterator( int8_t ) images8_iterator; typedef yof_image_iterator( int16_t ) images16_iterator; typedef yof_image_iterator( int32_t ) images32_iterator; typedef yof_image_iterator( int64_t ) images64_iterator; typedef yof_image_iterator( uint8_t ) imageu8_iterator; typedef yof_image_iterator( uint16_t ) imageu16_iterator; typedef yof_image_iterator( uint32_t ) imageu32_iterator; typedef yof_image_iterator( uint64_t ) imageu64_iterator; typedef yof_image_iterator( float ) imagef32_iterator; typedef yof_image_iterator( double ) imagef64_iterator; typedef yof_image_iterator( std::complex< float > ) imagec64_iterator; typedef yof_image_iterator( std::complex< double > ) imagec128_iterator; typedef yof_const_image_iterator( bool ) const_imageb_iterator; typedef yof_const_image_iterator( int8_t ) const_images8_iterator; typedef yof_const_image_iterator( int16_t ) const_images16_iterator; typedef yof_const_image_iterator( int32_t ) const_images32_iterator; typedef yof_const_image_iterator( int64_t ) const_images64_iterator; typedef yof_const_image_iterator( uint8_t ) const_imageu8_iterator; typedef yof_const_image_iterator( uint16_t ) const_imageu16_iterator; typedef yof_const_image_iterator( uint32_t ) const_imageu32_iterator; typedef yof_const_image_iterator( uint64_t ) const_imageu64_iterator; typedef yof_const_image_iterator( float ) const_imagef32_iterator; typedef yof_const_image_iterator( double ) const_imagef64_iterator; typedef yof_const_image_iterator( std::complex< float > ) const_imagec64_iterator; typedef yof_const_image_iterator( std::complex< double > ) const_imagec128_iterator; } // namespace yoffy #endif // #ifndef YOF_IMAGE_ITERATOR