image.cpp

Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2008 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien_jorge@yahoo.fr
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 /* The png.h file must be included before any other file that includes setjmp.h
00039    (as jpeg.hpp). */
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 } // image::scanline::begin()
00057 
00058 /*----------------------------------------------------------------------------*/
00062 claw::graphic::image::scanline::iterator claw::graphic::image::scanline::end()
00063 {
00064   return super::end();
00065 } // image::scanline::end()
00066 
00067 /*----------------------------------------------------------------------------*/
00071 claw::graphic::image::scanline::const_iterator
00072 claw::graphic::image::scanline::begin() const
00073 {
00074   return super::begin();
00075 } // image::scanline::begin()
00076 
00077 /*----------------------------------------------------------------------------*/
00081 claw::graphic::image::scanline::const_iterator
00082 claw::graphic::image::scanline::end() const
00083 {
00084   return super::end();
00085 } // image::scanline::end()
00086 
00087 /*----------------------------------------------------------------------------*/
00091 claw::graphic::image::scanline::size_type
00092 claw::graphic::image::scanline::size() const
00093 {
00094   return super::size();
00095 } // image::scanline::size()
00096 
00097 
00098 
00099 
00100 /*----------------------------------------------------------------------------*/
00105 claw::graphic::image::image()
00106 {
00107 
00108 } // image::image() [default constructor]
00109 
00110 /*----------------------------------------------------------------------------*/
00117 claw::graphic::image::image( unsigned int w, unsigned int h )
00118 {
00119   set_size(w, h);
00120 } // image::image() [constructor]
00121 
00122 /*----------------------------------------------------------------------------*/
00127 claw::graphic::image::image( std::istream& f )
00128 {
00129   load(f);
00130 } // image::image() [constructor]
00131 
00132 /*----------------------------------------------------------------------------*/
00136 claw::graphic::image::~image()
00137 {
00138   // nothing to do
00139 } // image::~image()
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 } // image::width()
00152 
00153 /*----------------------------------------------------------------------------*/
00157 unsigned int claw::graphic::image::height() const
00158 {
00159   return m_data.size();
00160 } // image::height()
00161 
00162 /*----------------------------------------------------------------------------*/
00166 claw::graphic::image::iterator claw::graphic::image::begin()
00167 {
00168   return iterator(*this);
00169 } // image::begin()
00170 
00171 /*----------------------------------------------------------------------------*/
00175 claw::graphic::image::iterator claw::graphic::image::end()
00176 {
00177   return iterator(*this, width(), height()-1);
00178 } // image::end()
00179 
00180 /*----------------------------------------------------------------------------*/
00184 claw::graphic::image::const_iterator claw::graphic::image::begin() const
00185 {
00186   return const_iterator(*this);
00187 } // image::begin()
00188 
00189 /*----------------------------------------------------------------------------*/
00193 claw::graphic::image::const_iterator claw::graphic::image::end() const
00194 {
00195   return const_iterator(*this, width(), height());
00196 } // image::end()
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 } // image::partial_copy()
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 } // image::flip()
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 } // image::set_size()
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 } // image::load()

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1