image.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #include <claw/image.hpp>
00031 #include <claw/exception.hpp>
00032 #include <claw/bitmap.hpp>
00033 #include <claw/pcx.hpp>
00034 #include <claw/targa.hpp>
00035 #include <claw/xbm.hpp>
00036
00037 #ifdef CLAW_PNG_SUPPORT
00038
00039
00040 #include <claw/png.hpp>
00041 #endif // CLAW_PNG_SUPPORT
00042
00043 #ifdef CLAW_JPEG_SUPPORT
00044 #include <claw/jpeg.hpp>
00045 #endif // CLAW_JPEG_SUPPORT
00046
00047 #include <algorithm>
00048
00049
00053 claw::graphic::image::scanline::iterator claw::graphic::image::scanline::begin()
00054 {
00055 return super::begin();
00056 }
00057
00058
00062 claw::graphic::image::scanline::iterator claw::graphic::image::scanline::end()
00063 {
00064 return super::end();
00065 }
00066
00067
00071 claw::graphic::image::scanline::const_iterator
00072 claw::graphic::image::scanline::begin() const
00073 {
00074 return super::begin();
00075 }
00076
00077
00081 claw::graphic::image::scanline::const_iterator
00082 claw::graphic::image::scanline::end() const
00083 {
00084 return super::end();
00085 }
00086
00087
00091 claw::graphic::image::scanline::size_type
00092 claw::graphic::image::scanline::size() const
00093 {
00094 return super::size();
00095 }
00096
00097
00098
00099
00100
00105 claw::graphic::image::image()
00106 {
00107
00108 }
00109
00110
00117 claw::graphic::image::image( unsigned int w, unsigned int h )
00118 {
00119 set_size(w, h);
00120 }
00121
00122
00127 claw::graphic::image::image( std::istream& f )
00128 {
00129 load(f);
00130 }
00131
00132
00136 claw::graphic::image::~image()
00137 {
00138
00139 }
00140
00141
00145 unsigned int claw::graphic::image::width() const
00146 {
00147 if ( m_data.empty() )
00148 return 0;
00149 else
00150 return m_data[0].size();
00151 }
00152
00153
00157 unsigned int claw::graphic::image::height() const
00158 {
00159 return m_data.size();
00160 }
00161
00162
00166 claw::graphic::image::iterator claw::graphic::image::begin()
00167 {
00168 return iterator(*this);
00169 }
00170
00171
00175 claw::graphic::image::iterator claw::graphic::image::end()
00176 {
00177 return iterator(*this, width(), height()-1);
00178 }
00179
00180
00184 claw::graphic::image::const_iterator claw::graphic::image::begin() const
00185 {
00186 return const_iterator(*this);
00187 }
00188
00189
00193 claw::graphic::image::const_iterator claw::graphic::image::end() const
00194 {
00195 return const_iterator(*this, width(), height());
00196 }
00197
00198
00204 void claw::graphic::image::partial_copy
00205 (const image& that, const claw::math::coordinate_2d<int>& pos )
00206 {
00207 claw::math::rectangle<int> my_box(0, 0, width(), height());
00208 claw::math::rectangle<int> his_box(pos.x, pos.y, that.width(), that.height());
00209
00210 if ( my_box.intersects( his_box ) )
00211 {
00212 claw::math::rectangle<int> intersection;
00213 unsigned int that_y = pos.y < 0 ? -pos.y : 0;
00214 unsigned int that_x = pos.x < 0 ? -pos.x : 0;
00215
00216 intersection = my_box.intersection( his_box );
00217
00218 for (int y=0; y!=intersection.height; ++y)
00219 {
00220 scanline::const_iterator first = that[y + that_y].begin() + that_x;
00221 scanline::const_iterator last = first + intersection.width;
00222 scanline::iterator dest = (*this)[y + intersection.position.y].begin()
00223 + intersection.position.x;
00224
00225 std::copy( first, last, dest );
00226 }
00227 }
00228 }
00229
00230
00234 void claw::graphic::image::flip()
00235 {
00236 for (unsigned int y=0; y!=height()/2; ++y)
00237 std::swap( m_data[y], m_data[height()-y-1] );
00238 }
00239
00240
00247 void claw::graphic::image::set_size( unsigned int w, unsigned int h )
00248 {
00249 if (w == 0)
00250 m_data.clear();
00251 else
00252 {
00253 m_data.resize(h);
00254
00255 for (unsigned int y=0; y!=height(); ++y)
00256 m_data[y].resize(w);
00257 }
00258 }
00259
00260
00265 void claw::graphic::image::load( std::istream& f )
00266 {
00267 bool ok = false;
00268
00269 #ifdef CLAW_JPEG_SUPPORT
00270 if (!ok)
00271 try { jpeg::reader( *this, f ); ok = true; }
00272 catch( ... ) { }
00273 #endif // CLAW_JPEG_SUPPORT
00274
00275 #ifdef CLAW_PNG_SUPPORT
00276 if (!ok)
00277 try { png::reader( *this, f ); ok = true; }
00278 catch( ... ) { }
00279 #endif // CLAW_PNG_SUPPORT
00280
00281 if (!ok)
00282 try { bitmap::reader( *this, f ); ok = true; }
00283 catch( ... ) { }
00284
00285 if (!ok)
00286 try { targa::reader( *this, f ); ok = true; }
00287 catch( ... ) { }
00288
00289 if (!ok)
00290 try { pcx::reader( *this, f ); ok = true; }
00291 catch( ... ) { }
00292
00293 if (!ok)
00294 try { xbm::reader( *this, f ); ok = true; }
00295 catch( ... ) { }
00296
00297 if (!ok)
00298 throw claw::bad_format( "image::load: file format isn't supported." );
00299 }