Classes | Public Types | Public Member Functions | Private Attributes

claw::graphic::image Class Reference

A class to deal with images. More...

#include <image.hpp>

Inheritance diagram for claw::graphic::image:
claw::graphic::bitmap claw::graphic::gif claw::graphic::gif::frame claw::graphic::jpeg claw::graphic::pcx claw::graphic::png claw::graphic::targa claw::graphic::xbm

List of all members.

Classes

class  base_iterator
 Base class for iterators on an image. More...
class  scanline
 One line in the image. More...

Public Types

typedef rgba_pixel pixel_type
typedef base_iterator< image,
pixel_type
iterator
typedef base_iterator< const
image, const pixel_type
const_iterator

Public Member Functions

 image ()
 Constructor. Creates an image without datas.
 image (unsigned int w, unsigned int h)
 Constructor. Creates an empty image.
 image (std::istream &f)
 Constructor. Reads an image from an input stream.
void swap (image &that)
 Swap the content of two images.
unsigned int width () const
 Gets image's width.
unsigned int height () const
 Gets image's height.
scanlineoperator[] (unsigned int i)
 Gets a line of the image.
const scanlineoperator[] (unsigned int i) const
 Gets a line of the image.
iterator begin ()
 Get an iterator pointing on the first pixel.
iterator end ()
 Get an iterator pointing just past the last pixel.
const_iterator begin () const
 Get an iterator pointing on the first pixel.
const_iterator end () const
 Get an iterator pointing just past the last pixel.
void merge (const image &that)
 Merge an image on the current image.
void merge (const image &that, const math::coordinate_2d< int > &pos)
 Merge an image on the current image.
void partial_copy (const image &that, const math::coordinate_2d< int > &pos)
 Copy an image on the current image.
void flip ()
 Set the image upside down.
void fill (const math::rectangle< int > r, const pixel_type &c)
 Fill an area of the image with a given color.
void set_size (unsigned int w, unsigned int h)
 Set a new size to the image.
void load (std::istream &f)
 Read the image from a stream.

Private Attributes

std::vector< scanlinem_data
 Data of the picture.

Detailed Description

A class to deal with images.

Author:
Julien Jorge

Definition at line 48 of file image.hpp.


Member Typedef Documentation

Definition at line 172 of file image.hpp.

Definition at line 171 of file image.hpp.

Definition at line 51 of file image.hpp.


Constructor & Destructor Documentation

claw::graphic::image::image (  )

Constructor. Creates an image without datas.

Postcondition:
width() == height() == 0

Definition at line 106 of file image.cpp.

{

} // image::image() [default constructor]
claw::graphic::image::image ( unsigned int  w,
unsigned int  h 
)

Constructor. Creates an empty image.

Parameters:
wImage's width.
hImage's height.
Precondition:
w > 0 and h > 0

Definition at line 128 of file image.cpp.

References set_size().

{
  set_size(w, h);
} // image::image() [constructor]
claw::graphic::image::image ( std::istream &  f )

Constructor. Reads an image from an input stream.

Parameters:
fThe stream to read from.

Definition at line 116 of file image.cpp.

References load().

{
  load(f);
} // image::image() [constructor]

Member Function Documentation

claw::graphic::image::iterator claw::graphic::image::begin (  )
claw::graphic::image::const_iterator claw::graphic::image::begin (  ) const

Get an iterator pointing on the first pixel.

Definition at line 186 of file image.cpp.

{
  return const_iterator(*this);
} // image::begin()
claw::graphic::image::const_iterator claw::graphic::image::end (  ) const

Get an iterator pointing just past the last pixel.

Definition at line 195 of file image.cpp.

References height(), and width().

{
  return const_iterator(*this, width(), height());
} // image::end()
claw::graphic::image::iterator claw::graphic::image::end (  )
void claw::graphic::image::fill ( const math::rectangle< int >  r,
const pixel_type c 
)

Fill an area of the image with a given color.

Parameters:
rThe area to fill.
cThe color to fill with.

Definition at line 313 of file image.cpp.

References claw::graphic::rgba_pixel::components, height(), claw::math::rectangle< T >::intersection(), claw::math::rectangle< T >::intersects(), and width().

Referenced by claw::graphic::gif::reader::fill_background(), claw::graphic::gif::reader::make_frames(), claw::graphic::gif::reader::read_frame_data(), and claw::graphic::pcx::writer::write_header().

{
  math::rectangle<int> my_box(0, 0, width(), height());

  if ( my_box.intersects( r ) )
    {
      const math::rectangle<int> intersection( my_box.intersection( r ) );
      const double max_comp
        ( std::numeric_limits<rgba_pixel::component_type>::max() );

      for (int y=0; y!=intersection.height; ++y)
        {
          scanline::iterator first =
            (*this)[intersection.position.y + y].begin()
            + intersection.position.x;
          const scanline::iterator last = first + intersection.width;

          for( ; first!=last; ++first )
            {
              const double src_alpha(c.components.alpha);

              double red =
                (double)first->components.red
                + src_alpha * (double)c.components.red / max_comp;
              double green =
                (double)first->components.green
                  + src_alpha * (double)c.components.green / max_comp;
              double blue =
                (double)first->components.blue
                + src_alpha * (double)c.components.blue / max_comp;
              double alpha = (double)first->components.alpha
                + (max_comp - src_alpha) / max_comp;

              first->components.red = std::min(red, max_comp);
              first->components.green = std::min(green, max_comp);
              first->components.blue = std::min(blue, max_comp);
              first->components.alpha = std::min(alpha, max_comp);
            }
        }
    }
} // image::fill()
void claw::graphic::image::flip (  )

Set the image upside down.

Definition at line 300 of file image.cpp.

References height(), m_data, and std::swap().

{
  for (unsigned int y=0; y!=height()/2; ++y)
    std::swap( m_data[y], m_data[height()-y-1] );
} // image::flip()
unsigned int claw::graphic::image::height (  ) const
void claw::graphic::image::load ( std::istream &  f )

Read the image from a stream.

Parameters:
fThe stream to read from.

Definition at line 380 of file image.cpp.

Referenced by image(), and claw::graphic::gif::reader::reader().

{
  bool ok = false;

#ifdef CLAW_JPEG_SUPPORT
  if (!ok)
    try { jpeg::reader( *this, f ); ok = true; }
    catch( ... ) { }
#endif // CLAW_JPEG_SUPPORT

#ifdef CLAW_PNG_SUPPORT
  if (!ok)
    try { png::reader( *this, f ); ok = true; }
    catch( ... ) { }
#endif // CLAW_PNG_SUPPORT

  if (!ok)
    try { bitmap::reader( *this, f ); ok = true; }
    catch( ... ) { }

  if (!ok)
    try { targa::reader( *this, f ); ok = true; }
    catch( ... ) { }

  if (!ok)
    try { gif::reader( *this, f ); ok = true; }
    catch( ... ) { }

  if (!ok)
    try { pcx::reader( *this, f ); ok = true; }
    catch( ... ) { }

  if (!ok)
    try { xbm::reader( *this, f ); ok = true; }
    catch( ... ) { }

  if (!ok)
    throw claw::bad_format( "image::load: file format isn't supported." );
} // image::load()
void claw::graphic::image::merge ( const image that )

Merge an image on the current image.

Parameters:
thatThe image to merge.

Definition at line 205 of file image.cpp.

Referenced by claw::graphic::gif::reader::make_frames().

{
  merge( that, math::coordinate_2d<int>(0, 0) );
} // image::merge()
void claw::graphic::image::merge ( const image that,
const math::coordinate_2d< int > &  pos 
)

Merge an image on the current image.

Parameters:
thatThe image to merge.
posThe position of the top left corner.

Definition at line 217 of file image.cpp.

References begin(), claw::math::rectangle< T >::height, height(), claw::math::rectangle< T >::intersection(), claw::math::rectangle< T >::intersects(), claw::math::rectangle< T >::position, claw::math::rectangle< T >::width, width(), claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.

{
  math::rectangle<int> my_box(0, 0, width(), height());
  math::rectangle<int> his_box(pos.x, pos.y, that.width(), that.height());

  if ( my_box.intersects( his_box ) )
    {
      math::rectangle<int> intersection;
      unsigned int that_y = pos.y < 0 ? -pos.y : 0;
      unsigned int that_x = pos.x < 0 ? -pos.x : 0;
      const double max_comp
        ( std::numeric_limits<rgba_pixel::component_type>::max() );

      intersection = my_box.intersection( his_box );

      for (int y=0; y!=intersection.height; ++y)
        {
          scanline::const_iterator first = that[y + that_y].begin() + that_x;
          scanline::const_iterator last = first + intersection.width;
          scanline::iterator dest = (*this)[y + intersection.position.y].begin()
            + intersection.position.x;

          for( ; first!=last; ++first, ++dest )
            {
              const double src_alpha(first->components.alpha);
              const double dest_alpha(dest->components.alpha);

              double red =
                (double)first->components.red
                + dest_alpha * (double)dest->components.red / max_comp;
              double green =
                (double)first->components.green
                + dest_alpha * (double)dest->components.green / max_comp;
              double blue =
                (double)first->components.blue
                  + dest_alpha * (double)dest->components.blue / max_comp;
              double alpha = src_alpha + (max_comp - dest_alpha) / max_comp;

              dest->components.red = std::min(red, max_comp);
              dest->components.green = std::min(green, max_comp);
              dest->components.blue = std::min(blue, max_comp);
              dest->components.alpha = std::min(alpha, max_comp);
            }
        }
    }
} // image::merge()
claw::graphic::image::scanline & claw::graphic::image::operator[] ( unsigned int  i ) [inline]

Gets a line of the image.

Definition at line 429 of file image.ipp.

References m_data.

Referenced by claw::graphic::image::scanline::operator[]().

{
  return m_data[i];
} // image::operator[]()
const claw::graphic::image::scanline & claw::graphic::image::operator[] ( unsigned int  i ) const [inline]

Gets a line of the image.

Definition at line 439 of file image.ipp.

References m_data.

{
  return m_data[i];
} // image::operator[]() [const]
void claw::graphic::image::partial_copy ( const image that,
const math::coordinate_2d< int > &  pos 
)

Copy an image on the current image.

Parameters:
thatThe image to copy.
posThe position of the top left corner.

Definition at line 271 of file image.cpp.

References begin(), claw::math::rectangle< T >::height, height(), claw::math::rectangle< T >::intersection(), claw::math::rectangle< T >::intersects(), claw::math::rectangle< T >::position, claw::math::rectangle< T >::width, width(), claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.

{
  math::rectangle<int> my_box(0, 0, width(), height());
  math::rectangle<int> his_box(pos.x, pos.y, that.width(), that.height());

  if ( my_box.intersects( his_box ) )
    {
      math::rectangle<int> intersection;
      unsigned int that_y = pos.y < 0 ? -pos.y : 0;
      unsigned int that_x = pos.x < 0 ? -pos.x : 0;

      intersection = my_box.intersection( his_box );

      for (int y=0; y!=intersection.height; ++y)
        {
          scanline::const_iterator first = that[y + that_y].begin() + that_x;
          scanline::const_iterator last = first + intersection.width;
          scanline::iterator dest = (*this)[y + intersection.position.y].begin()
            + intersection.position.x;

          std::copy( first, last, dest );
        }
    }
} // image::partial_copy()
void claw::graphic::image::set_size ( unsigned int  w,
unsigned int  h 
)

Set a new size to the image.

Remarks:
Image's data won't be lost. If a dimension is set larger than its current value, extra pixels won't be initialized.
Precondition:
(w!=0) && (h!=0)

Definition at line 362 of file image.cpp.

References height(), and m_data.

Referenced by image(), and claw::graphic::gif::reader::read_frame_data().

{
  if (w == 0)
    m_data.clear();
  else
    {
      m_data.resize(h);
  
      for (unsigned int y=0; y!=height(); ++y)
        m_data[y].resize(w);
    }
} // image::set_size()
void claw::graphic::image::swap ( image that )

Swap the content of two images.

Parameters:
thatThe image to swap with.

Definition at line 138 of file image.cpp.

References m_data.

Referenced by std::swap().

{
  std::swap(m_data, that.m_data);
} // image::swap()
unsigned int claw::graphic::image::width (  ) const

Member Data Documentation

std::vector<scanline> claw::graphic::image::m_data [private]

Data of the picture.

Definition at line 208 of file image.hpp.

Referenced by flip(), height(), operator[](), set_size(), swap(), and width().


The documentation for this class was generated from the following files: