|
Boost : |
From: Andreou Giannis (gandreou_at_[hidden])
Date: 2004-02-24 14:03:02
During the last years, i have been working with images
and i had to have a library for image processing.
Besides other libraries that i have used, i created some
classes/functions of my own that helped us a 'glue' and
allowed me to program in my (very simple - and i hope clear)
programming style.
Allthough i cannot provide the boost library with a full
image-processing library (with image loading-saving facilities),
i would like to submit a library containing source code that includes
a templated image class.
The source code is very simple and depends on nothing but
the allocating function ('new' is used, but another could be used, easilly).
(an appropriate header would be image/image.h"):
This class takes into concideration the facts that:
1) most platforms use an alignment for the image lines.
the 'align' template parameter takes care of that.
Possibly, the result of lineSize should be stored as a variable
inside the class.
This is the reason char* is used instead of the actual pixel
type, in allocation. I know that this limits the acceptable pixel types
to simple types:(
2) accessing the image data is done in a per-line basis.
(The operator [] returns a pointer to a line of the image.)
3) Other image libraries may exist and be used with this library.
It is possible to initialize the class with external data and
later 'unlockData', so that it is not deleted.
The use of the class is very simple:
One writes a[y][x], where a is a TImage<...> instance
to access pixel at (x, y).
Issues:
1) The boost naming conventions are not followed,
but this can be done easilly...
2) Not using iterators:
A set of begin(), end() e.t.c iterators, in the manner of vector
can be used here, for the lines of the image, or even the pixels in it.
Definitions of pixel types and converting functions are included.
I would like to include also:
A toolkit for 2D graphics, that works well with the above
class allowing the user to 'draw' on top of this image.
This consists mainly of a 'graphics' class.
However, to do that, a set of definitions must allready be there for
basic 2d geometry, such as points, polygons, e.t.c.
About a million people must have defined structs/classes/functions for
that purpose.
I have some code that can be used as a basis for points/polygons e.t.c.,
but if someone else allready has something ready for this purpose,
i am willing to make MY source code compatible to his/her.
If the people of boost are interested in any of the above ideas,
i will commit a (more boost-compliant) version of this library.
Andreou Ioannis
Unversity of Piraeus
Greece
P.S. A section of the TImage (to be image::image) class follows:
template<class PixT, int align> class TImage{
char* _pixels;
int w, h;
public:
typedef PixT pixel_type;
inline TImage(int w1, int h1){
w=w1;
h=h1;
int lSize=lineSize();
_pixels=new char[lSize*h];
}
inline TImage(){
w=0; h=0; _pixels=0;
}
inline TImage(int w1, int h1, char* p){ //must unlock later!!!
w=w1; h=h1; _pixels=p;
}
inline ~TImage(){
if(_pixels) delete [] _pixels;
}
inline int pixelSize()const{
return sizeof(pixel_type);
}
inline int alignment()const{
return align;
}
inline int width()const{
return w;
}
inline int height()const{
return h;
}
inline int size()const{ //in pixels
return w*h;
}
inline void setSize(int w1, int h1){
if(w==w1 && h==h1) return;
if(_pixels) delete _pixels;
w=w1;
h=h1;
_pixels=new char[totalSize()];
}
inline void setData(int width, int height, char* p){
if(_pixels) delete _pixels;
w=width;
h=height;
_pixels=p;
}
inline void unlockData(){ //let data go, w/o delete
w=0;
h=0;
_pixels=0;
}
...
...
inline pixel_type* operator[](int i)const{
return (pixel_type*)( _pixels+(i*lineSize()) );
}
....
};
...
template<typename DstIm, typename SrcIm, typename Func>
void im_convert(DstIm& dst, SrcIm& src, Func& func){
int w=src.width();
int h=src.height();
dst.setSize(w, h);
for(int i=0; i<h; i++){
for(int j=0; j<w; j++){
func((dst)[i][j], (src)[i][j]);
}
}
}
...
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk