31 explicit StreamAdaptor( std::istream & stream_in ) :
33 m_pos0(stream_in.tellg())
36 void skip(
unsigned int n )
38 m_s.seekg( n , std::ios_base::cur ) ;
46 if( !good() )
return 0U ;
47 return static_cast<unsigned int>(m_s.get()) ;
51 if( !good() )
return 0U ;
52 return static_cast<unsigned int>(m_s.peek()) ;
56 return m_s.tellg() - m_pos0 ;
59 std::streampos m_pos0 ;
64 DataStream(
const unsigned char * p ,
size_t n ) :
70 void skip(
unsigned int n )
80 if( m_p >= m_end )
return 0U ;
85 if( m_p >= m_end )
return 0U ;
90 return m_p - m_begin ;
92 const unsigned char * m_begin ;
93 const unsigned char * m_p ;
94 const unsigned char * m_end ;
98 unsigned int get2( T & in )
100 unsigned int hi = in.get() ;
101 unsigned int lo = in.get() ;
102 return (hi<<8) + lo ;
105 template <
typename T>
106 void read( T & in ,
int & dx ,
int & dy ,
int & channels )
112 const unsigned int sof0 = 0xC0 ;
113 const unsigned int sof1 = 0xC1 ;
114 const unsigned int sof2 = 0xC2 ;
115 const unsigned int sof9 = 0xC9 ;
116 const unsigned int sof10 = 0xCA ;
120 if( in.get() != 0xff )
break ;
121 unsigned int type = in.get() ;
122 unsigned int length = in.peek() == 0xff ? 0 : get2(in) ;
123 if( type == sof0 || type == sof1 || type == sof2 || type == sof9 || type == sof10 )
126 unsigned int height = get2(in) ;
127 unsigned int width = get2(in) ;
128 unsigned int components = in.get() ;
129 dx =
static_cast<int>( width ) ;
130 dy =
static_cast<int>( height ) ;
131 channels =
static_cast<int>( components ) ;
136 in.skip( length-2U ) ;
138 G_DEBUG(
"gjpeg::read: no sof chunk: offset=" << in.offset() <<
" skip=" << length ) ;
147 StreamAdaptor s( stream_in ) ;
148 read( s , m_dx , m_dy , m_channels ) ;
153 DataStream s( p , n ) ;
154 read( s , m_dx , m_dy , m_channels ) ;
159 DataStream s( reinterpret_cast<const unsigned char *>(p) , n ) ;
160 read( s , m_dx , m_dy , m_channels ) ;
165 return m_dx > 0 && m_dy > 0 && m_channels > 0 ;
170 return valid() ? m_dx : 0 ;
175 return valid() ? m_dy : 0 ;
180 return valid() ? m_channels : 0 ;
int dx() const
Returns the image width.
int dy() const
Returns the image height.
int channels() const
Returns the number of channels (eg. 3).
bool valid() const
Returns true if constructed successfully.
JpegInfo(const unsigned char *p, size_t)
Constructor.