olena-2.0-14-gbab624b New GIMP image wrapper.

--- .../src => milena/mln/core/image}/gimp-image.hh | 466 +++++++++----------- 1 files changed, 205 insertions(+), 261 deletions(-) copy {plugin-gimp/src => milena/mln/core/image}/gimp-image.hh (64%) diff --git a/plugin-gimp/src/gimp-image.hh b/milena/mln/core/image/gimp-image.hh similarity index 64% copy from plugin-gimp/src/gimp-image.hh copy to milena/mln/core/image/gimp-image.hh index be0fd4f..112dc16 100644 --- a/plugin-gimp/src/gimp-image.hh +++ b/milena/mln/core/image/gimp-image.hh @@ -1,4 +1,4 @@ -// Copyright (C) 2007, 2011 EPITA Research and Development Laboratory +// Copyright (C) 2011 EPITA Research and Development Laboratory // // This file is part of the Milena Library. This library is free // software; you can redistribute it and/or modify it under the terms @@ -25,8 +25,13 @@ // reasons why the executable file might be covered by the GNU General // Public License. -#ifndef __IMAGEGIMP_HH__ -# define __IMAGEGIMP_HH__ +#ifndef MLN_CORE_IMAGE_GIMP_IMAGE_HH +# define MLN_CORE_IMAGE_GIMP_IMAGE_HH + +/// \file +/// +/// GIMP image wrapper. +/// \todo Handle mln::duplicate(gimp_image) # include <libgimp/gimp.h> # include <libgimp/gimpui.h> @@ -40,50 +45,95 @@ # include <mln/value/int_u8.hh> # include <mln/literal/all.hh> +# include <mln/core/trait/pixter.hh> +# include <mln/core/dpoints_pixter.hh> +# include <mln/core/pixter2d.hh> +# include <mln/core/w_window.hh> + + # include <iostream> # include <sstream> namespace mln { - // Fwd decl. + // Forward declaration. template <GimpImageType TI> struct gimp_image; - template <typename T1, typename T2> - struct my_shell - : public Proxy< my_shell<T1,T2> >, - public mln::internal::proxy_impl< T1, my_shell<T1,T2> > + + namespace trait { - // Equiv - typedef T1 equiv; + // pixter - T1 subj_() + template <GimpImageType TI> + struct fwd_pixter< gimp_image<TI> > { - return val_; - } + typedef fwd_pixter2d< gimp_image<TI> > ret; + }; - my_shell(GimpPixelRgn* rgn, const point2d& p) - : rgn_(rgn), p_(p) + + template <GimpImageType TI> + struct fwd_pixter< const gimp_image<TI> > { - gimp_pixel_rgn_get_pixel(rgn_, - (guchar *) &val_, - p_.col(), p_.row()); - } + typedef fwd_pixter2d< const gimp_image<TI> > ret; + }; + - my_shell<T1, T2>& operator=(const T1& nval) + template <GimpImageType TI> + struct bkd_pixter< gimp_image<TI> > { - val_ = nval; - gimp_pixel_rgn_set_pixel(rgn_, - (guchar *) &val_, - p_.col(), p_.row()); - } + typedef bkd_pixter2d< gimp_image<TI> > ret; + }; - T1 val_; - GimpPixelRgn* rgn_; - point2d p_; + template <GimpImageType TI> + struct bkd_pixter< const gimp_image<TI> > + { + typedef bkd_pixter2d< const gimp_image<TI> > ret; + }; + + // qixter + + template <typename W, GimpImageType TI> + struct fwd_qixter< gimp_image<TI>, W > + { + typedef dpoints_fwd_pixter< gimp_image<TI> > ret; + }; + + template <typename W, GimpImageType TI> + struct fwd_qixter< const gimp_image<TI>, W > + { + typedef dpoints_fwd_pixter< const gimp_image<TI> > ret; + }; + + template <typename W, GimpImageType TI> + struct bkd_qixter< gimp_image<TI>, W > + { + typedef mln::internal::fixme ret; + }; + + // nixter + + template <typename N, GimpImageType TI> + struct fwd_nixter< gimp_image<TI>, N > + { + typedef dpoints_fwd_pixter< gimp_image<TI> > ret; + }; + + template <typename N, GimpImageType TI> + struct fwd_nixter< const gimp_image<TI>, N > + { + typedef dpoints_fwd_pixter< const gimp_image<TI> > ret; + }; + + template <typename N, GimpImageType TI> + struct bkd_nixter< gimp_image<TI>, N > + { + typedef mln::internal::fixme ret; + }; + + } // end of namespace mln::trait - }; namespace internal { @@ -115,19 +165,19 @@ namespace mln /// Constructor. /// { - data(GimpPixelRgn* rgn); + data(GimpDrawable* drawable); /// } /// Destructor. ~data(); /// Gimp region. - GimpPixelRgn* rgn_; + GimpDrawable* drawable_; - guchar *rdata_; + GimpPixelRgn rgn_; - // Columns pointers - //guchar** array_; + T *rdata_; + T** array_; /// theoretical box box2d b_; @@ -143,13 +193,13 @@ namespace mln { // misc typedef trait::image::category::primary category; - typedef trait::image::speed::fast speed; + typedef trait::image::speed::fastest speed; typedef trait::image::size::regular size; // value typedef trait::image::vw_io::none vw_io; typedef trait::image::vw_set::none vw_set; - typedef trait::image::value_access::computed value_access; + typedef trait::image::value_access::direct value_access; typedef trait::image::value_storage::one_block value_storage; typedef trait::image::value_browsing::site_wise_only value_browsing; typedef trait::image::value_alignment::with_grid value_alignment; @@ -171,8 +221,11 @@ namespace mln - /*! \brief Basic 2D image class. - * + /*! \brief GIMP 2D image class. + + \tparam GimpImageType Can take the following values : + GIMP_GRAY_IMAGE and GIMP_RGB_IMAGE. + */ template <GimpImageType TI> struct gimp_image @@ -197,10 +250,12 @@ namespace mln typedef T value; /// Return type of read-only access. - typedef my_shell<T,guchar> rvalue; + //typedef my_shell<T,guchar> rvalue; + typedef const T& rvalue; /// Return type of read-write access. - typedef my_shell<T,guchar> lvalue; + //typedef my_shell<T,guchar> lvalue; + typedef T& lvalue; /// Skeleton. @@ -214,8 +269,8 @@ namespace mln /// Constructor without argument. gimp_image(); - /// Constructor with a gimp region. - gimp_image(GimpPixelRgn* rgn); + /// Constructor with a gimp drawable. + gimp_image(GimpDrawable* drawable); /// Constructor with a box2d. gimp_image(box2d box); @@ -223,11 +278,8 @@ namespace mln /// Constructor with with the numbers of rows and columns. gimp_image(int nrows, int ncols); - /// Initialise an image from a gimp region. - void init_(GimpPixelRgn* rgn); - - // /// Initialise an image from a box2d. - // void init_(box2d box); + /// Initialise an image from a gimp drawable. + void init_(GimpDrawable* drawable); /// Test if \p p is valid. bool has(const point2d& p) const; @@ -248,9 +300,8 @@ namespace mln // As a fastest image: // ------------------- - // // Give the index of a point. - // unsigned index_of_point(const point& p) const; - + // Give the index of a point. + using super_::index_of_point; /// Give the border thickness. unsigned border() const; @@ -268,22 +319,19 @@ namespace mln int delta_index(const dpoint2d& dp) const; // /// Give the point corresponding to the index \p i. - // point2d point_at_index(unsigned i) const; + point2d point_at_index(unsigned i) const; /// Give a hook to the value buffer. - const guchar* buffer() const; + const T* buffer() const; /// Give a hook to the value buffer. - guchar* buffer(); + T* buffer(); /// Gimp Image method - /// Give a hook to the GimpPixelRgn. - GimpPixelRgn* gimp_region(); - /// Give a hook to the GimpDrawable. - GimpDrawable* gimp_drawable(); + GimpDrawable* gimp_drawable_(); }; @@ -293,60 +341,76 @@ namespace mln { template <GimpImageType TI> - data< gimp_image<TI> >::data(GimpPixelRgn* rgn) - : rgn_(rgn) + data< gimp_image<TI> >::data(GimpDrawable* drawable) + : drawable_(drawable) { - mln_assertion(rgn != NULL); - - rgn_ = rgn; - - rdata_ = rgn->data; - mln_assertion(rdata_ != 0); - - // unsigned - // nr = b_.height(), - // nc = b_.width(); - // array_ = new guchar*[nc]; - // guchar* buf = rgn_->data; - // for (unsigned i = 0; i < nc; ++i) - // { - // array_[i] = buf; - // buf += nr * sizeof(T); - // } - - b_ = make::box2d(rgn->y, rgn->x, - rgn->y + rgn->h - 1, - rgn->x + rgn->w - 1); + mln_precondition(drawable != NULL); + + gint x1, y1, x2, y2; + gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2); + + gimp_pixel_rgn_init(&rgn_, drawable, x1, y1, x2 - x1, y2 - y1, TRUE, TRUE); + + rdata_ = new T[(x2 - x1) * (y2 - y1)]; + gimp_pixel_rgn_get_rect(&rgn_, (guchar *)rdata_, x1, y1, (x2 - x1), (y2 - y1)); + + b_ = make::box2d(y1, x1, y2 - 1, x2 - 1); + array_ = new T*[b_.height()]; + T* buf = rdata_; + for (unsigned i = 0; i < b_.height(); ++i) + { + array_[i] = buf; + buf += b_.width(); + } + } template <GimpImageType TI> data< gimp_image<TI> >::~data() { - // FIXME : Flush! + gint x1, y1, x2, y2; + gimp_drawable_mask_bounds (drawable_->drawable_id, &x1, &y1, &x2, &y2); + + gimp_pixel_rgn_set_rect(&rgn_, (guchar *)rdata_, + x1, y1, + (x2 - x1), (y2 - y1)); + + gimp_drawable_flush (drawable_); + gimp_drawable_merge_shadow(drawable_->drawable_id, true); + + gimp_drawable_update (drawable_->drawable_id, 0, 0, + drawable_->width, drawable_->height); + + gimp_displays_flush(); + gimp_drawable_detach (drawable_); + + delete[] rdata_; + delete[] array_; } } // end of namespace mln::internal - // gimp_image<TI> + // gimp_image<TI> + template <GimpImageType TI> gimp_image<TI>::gimp_image() { } template <GimpImageType TI> - gimp_image<TI>::gimp_image(GimpPixelRgn* rgn) + gimp_image<TI>::gimp_image(GimpDrawable* drawable) { - init_(rgn); - gimp_tile_cache_ntiles(3 * (this->data_->rgn_->w / gimp_tile_width())); + init_(drawable); + gimp_tile_cache_ntiles(3 * (drawable->width / gimp_tile_width())); } template <GimpImageType TI> void - gimp_image<TI>::init_(GimpPixelRgn* rgn) + gimp_image<TI>::init_(GimpDrawable* drawable) { - this->data_ = new internal::data< gimp_image<TI> >(rgn); + this->data_ = new internal::data< gimp_image<TI> >(drawable); } template <GimpImageType TI> @@ -357,19 +421,6 @@ namespace mln return this->data_->b_; } - // template <GimpImageType TI> - // unsigned - // gimp_image<TI>::index_of_point(const point& p) const - // { - // mln_precondition(this->is_valid()); - // mln_precondition(this->has(p)); - - // unsigned i = (this->operator()(p).address_() - this->buffer()) / sizeof(T); - // mln_postcondition(p == this->point_at_index(i)); - // return i; - // } - - template <GimpImageType TI> unsigned gimp_image<TI>::border() const @@ -385,23 +436,23 @@ namespace mln return this->data_->b_.nsites(); } - // template <GimpImageType TI> - // inline - // typename gimp_image<TI>::rvalue - // gimp_image<TI>::element(unsigned i) const - // { - // mln_precondition(i < nelements()); - // return rvalue(this->data_->rdata_ + 3 * i); - // } - - // template <GimpImageType TI> - // inline - // typename gimp_image<TI>::lvalue - // gimp_image<TI>::element(unsigned i) - // { - // mln_precondition(i < nelements()); - // return lvalue(this->data_->rdata_ + 3 * i); - // } + template <GimpImageType TI> + inline + typename gimp_image<TI>::rvalue + gimp_image<TI>::element(unsigned i) const + { + mln_precondition(i < nelements()); + return *(this->data_->rdata_ + this->data_->drawable_->bpp * i); + } + + template <GimpImageType TI> + inline + typename gimp_image<TI>::lvalue + gimp_image<TI>::element(unsigned i) + { + mln_precondition(i < nelements()); + return *(this->data_->rdata_ + this->data_->drawable_->bpp * i); + } template <GimpImageType TI> @@ -417,14 +468,7 @@ namespace mln gimp_image<TI>::operator()(const point& p) const { mln_precondition(this->has(p)); - // return rvalue(&this->data_->array_[p.col()][p.row() * 3]); - // return rvalue(this->data_->rdata_ + 3 * (p.row() * this->data_->b_.width() + p.col())); - // static T c; - // gimp_pixel_rgn_get_pixel(this->data_->rgn_, - // (guchar *) &c, - // p.col(), p.row()); - // return c; - return rvalue(this->data_->rgn_, p); + return this->data_->array_[p.row()][p.col()]; } template <GimpImageType TI> @@ -432,157 +476,57 @@ namespace mln gimp_image<TI>::operator()(const point& p) { mln_precondition(this->has(p)); - return rvalue(this->data_->rgn_, p); -// return lvalue(&this->data_->array_[p.col()][p.row() * 3]); - //return rvalue(this->data_->rdata_ + 3 * (p.row() * this->data_->b_.width() + p.col())); + return this->data_->array_[p.row()][p.col()]; } - // template <GimpImageType TI> - // inline - // int - // gimp_image<TI>::delta_index(const dpoint2d& dp) const - // { - // mln_precondition(this->is_valid()); - // int o = dp[0] * this->data_->b_.width() * 3 + dp[1] * 3; - // return o; - // } - - // template <GimpImageType TI> - // inline - // point2d - // gimp_image<TI>::point_at_index(unsigned i) const - // { - // mln_precondition(i < nelements()); - // def::coord - // row = static_cast<def::coord>(i / this->data_->b_.height() + this->data_->b_.min_col()), - // col = static_cast<def::coord>(i % this->data_->b_.height() + this->data_->b_.min_row()); - // point2d p = point2d(row, col); - // mln_postcondition(this->operator()(p).address_() == this->data_->rdata_ + sizeof(T) * i); - // return p; - // } - - // template <GimpImageType TI> - // const guchar* - // gimp_image<TI>::buffer() const - // { - // return this->data_->rdata_; - // } - - - // template <GimpImageType TI> - // guchar* - // gimp_image<TI>::buffer() - // { - // return this->data_->rdata_; - // } - - template <GimpImageType TI> - GimpPixelRgn* - gimp_image<TI>::gimp_region() + inline + int + gimp_image<TI>::delta_index(const dpoint2d& dp) const { - return this->data_->rgn_; + mln_precondition(this->is_valid()); + int o = (dp[0] * this->data_->b_.width() + dp[1]) * this->data_->drawable_->bpp; + return o; } template <GimpImageType TI> - GimpDrawable* - gimp_image<TI>::gimp_drawable() + inline + point2d + gimp_image<TI>::point_at_index(unsigned i) const { - return this->data_->rgn_->drawable; + mln_precondition(i < nelements()); + def::coord + row = static_cast<def::coord>(i / this->data_->b_.width() + this->data_->b_.min_row()), + col = static_cast<def::coord>(i % this->data_->b_.width() + this->data_->b_.min_col()); + point2d p = point2d(row, col); + mln_postcondition(& this->operator()(p) == this->data_->rdata_ + i); + return p; } - -# endif // ! MLN_INCLUDE_ONLY - -} // end of namespace mln - - - -# include <mln/core/trait/pixter.hh> -# include <mln/core/dpoints_pixter.hh> -# include <mln/core/pixter2d.hh> -# include <mln/core/w_window.hh> - - -namespace mln -{ - - namespace trait + template <GimpImageType TI> + const typename gimp_image<TI>::T* + gimp_image<TI>::buffer() const { + return this->data_->rdata_; + } - // pixter - - - template <GimpImageType TI> - struct fwd_pixter< gimp_image<TI> > - { - typedef fwd_pixter2d< gimp_image<TI> > ret; - }; - - - template <GimpImageType TI> - struct fwd_pixter< const gimp_image<TI> > - { - typedef fwd_pixter2d< const gimp_image<TI> > ret; - }; - - - template <GimpImageType TI> - struct bkd_pixter< gimp_image<TI> > - { - typedef bkd_pixter2d< gimp_image<TI> > ret; - }; - - - template <GimpImageType TI> - struct bkd_pixter< const gimp_image<TI> > - { - typedef bkd_pixter2d< const gimp_image<TI> > ret; - }; - - // qixter - - template <typename W, GimpImageType TI> - struct fwd_qixter< gimp_image<TI>, W > - { - typedef dpoints_fwd_pixter< gimp_image<TI> > ret; - }; - - template <typename W, GimpImageType TI> - struct fwd_qixter< const gimp_image<TI>, W > - { - typedef dpoints_fwd_pixter< const gimp_image<TI> > ret; - }; - - template <typename W, GimpImageType TI> - struct bkd_qixter< gimp_image<TI>, W > - { - typedef mln::internal::fixme ret; - }; - - // nixter - - template <typename N, GimpImageType TI> - struct fwd_nixter< gimp_image<TI>, N > - { - typedef dpoints_fwd_pixter< gimp_image<TI> > ret; - }; + template <GimpImageType TI> + typename gimp_image<TI>::T* + gimp_image<TI>::buffer() + { + return this->data_->rdata_; + } - template <typename N, GimpImageType TI> - struct fwd_nixter< const gimp_image<TI>, N > - { - typedef dpoints_fwd_pixter< const gimp_image<TI> > ret; - }; + template <GimpImageType TI> + GimpDrawable* + gimp_image<TI>::gimp_drawable_() + { + return this->data_->drawable_; + } - template <typename N, GimpImageType TI> - struct bkd_nixter< gimp_image<TI>, N > - { - typedef mln::internal::fixme ret; - }; - } // end of namespace mln::trait +# endif // ! MLN_INCLUDE_ONLY } // end of namespace mln - -#endif // ! __GIMP_IMAGE_HH__ +#endif // ! MLN_CORE_IMAGE_GIMP_IMAGE_HH -- 1.7.2.5
participants (1)
-
Guillaume Lazzara