Main Page | Class Hierarchy | Class List | Directories | File List

Image2D.h

00001 #ifndef __IMAGE2D__
00002 #define __IMAGE2D__
00003 
00004 #include <OpenGL.h>
00005 #include "TextureData.h"
00006 #include "ColorTypes.h"
00007 #include "Texture.h"
00008 #include "Image1D.h"
00009 #include <QImage>
00010 
00011 namespace apig {
00012 
00021     class AbstractImage2D : public TextureData {
00022         public:
00023             AbstractImage2D(int w=0, int h=0) : w(w), h(h) {}
00024             virtual ~AbstractImage2D() {}
00025     
00026             virtual bool loaded() const = 0;    // true ssi l'image contient des données
00027             virtual void destroy() = 0;         // libère la mémoire éventuellement occupée par l'image
00028             
00029             virtual QImage toQImage() const = 0;
00030             virtual void initialize(QImage image) = 0;      // initialise l'image à partir de <image>
00031             
00032             void save(QString fileName) const;              // sauvegarde l'image dans un format donné par l'extension du nom de fichier
00033     
00034             int width()  const { return w; }
00035             int height() const { return h; }
00036             bool contains(int i, int j)     const { return (i >= 0) && (i < w) && (j >= 0) && (j < h); }
00037             bool contains(float x, float y) const { return (x >= 0) && (x < w) && (y >= 0) && (y < h); }
00038             
00039             virtual GLenum textureMode() const { return GL_TEXTURE_2D; }
00040             virtual GLint defaultTexFormat() const = 0;
00041             virtual void loadToGPU(GLint texFormat) const { loadTexture2D(texFormat, GL_TEXTURE_2D); }
00042             virtual void loadTexture2D(GLenum texFormat, GLenum target = GL_TEXTURE_2D) const = 0;
00043     
00044             enum WrapMode { CLAMP_TO_EDGE, CLAMP_TO_BORDER, REPEAT, MIRRORED_REPEAT };
00045             
00046         protected:
00047             int w, h;
00048         };
00049     
00051     
00056     template<class Color>
00057     class Image2D : public AbstractImage2D {
00058         public:
00059             Image2D(int w=0, int h=0, Color* data=NULL);
00060             Image2D(QImage image);
00061             Image2D(QString fileName);
00062             virtual ~Image2D() {}
00063     
00064             virtual bool loaded() const { return data != NULL; }
00065             virtual void destroy();
00066             void clear(Color c = Color());
00067             
00068             virtual QImage toQImage() const;
00069             virtual void initialize(QImage image);      // initialise l'image à partir de <image>
00070             Image2D<Color> clone() const;               // duplique l'image en une nouvelle image
00071             Image2D<Color> subImage(int i, int j, int w, int h) const;  // précondition : i >= 0 && i+w <= width() && j >= 0 && j+h <= height()
00072             Image2D<Color> supImage(int i, int j, int w, int h) const;  // précondition : i >= 0 && i+width() <= w && j >= 0 && j+height() <= h
00073             Image2D<Color> boundingPowerOfTwo() const;  // retourne l'image englobant l'image courante, ayant des dimensions puissances de 2
00074             
00075             Image1D<Color> line(int j) const { return Image1D<Color>(w, data + j*w); }     // retourne l'Image1D référençant la ligne <j>
00076             
00077             virtual GLint defaultTexFormat() const { return Color::TEX_FORMAT; }
00078             virtual void loadTexture2D(GLenum texFormat, GLenum target = GL_TEXTURE_2D) const;
00079     
00080             // lecture/écriture du buffer de couleurs OpenGL :
00081             static Image2D<Color> getColorBuffer();
00082             static Image2D<Color> getDepthBuffer();         // ne marche correctement que si le format <Color> a une seule composante
00083             void readColorBuffer(int i=0, int j=0);         // précondition : loaded() == true et le buffer contient le viewport (i,j,w,h)
00084             void readDepthBuffer(int i=0, int j=0);         // de même mais le format <Color> doit avoir une seule composante
00085             void drawColorBuffer(int i=0, int j=0) const;   // point (i,j) en unités de pixels, origine en bas à gauche
00086             static Color readPixelColor(int i, int j);
00087             
00088             // acquisition d'une texture 2D :
00089             static Image2D<Color> getTexture(const Texture *tex);
00090             void readTexture(const Texture *tex);           // précondition : l'image a la même taille que la texture
00091             
00092             // chargment direct d'une texture :
00093             static Texture createTex2D(QString fileName, GLint internalFormat = GL_RGBA, GLenum interpMode = GL_NEAREST, GLenum wrapMode = GL_CLAMP);
00094     
00095             // définition de l'échantillonnage aux bords de l'image :
00096             void setBorderColor(Color border);
00097             void setWrapMode(WrapMode wrapMode);
00098             void setupBorder(WrapMode wrapMode, Color border);
00099             
00100             // fonctions d'acces aux pixels :
00101             //-------------------------------
00102             inline       Color& texel(int i, int j)       { return data[i + j*w]; }    // précondition : this->contains(i,j)
00103             inline const Color& texel(int i, int j) const { return data[i + j*w]; }    // précondition : this->contains(i,j)
00104             inline       Color& operator()(int i, int j)       { return texel(i,j); }
00105             inline const Color& operator()(int i, int j) const { return texel(i,j); }
00106             Color sample(int i, int j) const;               // si !this->contains(i,j), le résultat dépend de <borderColor> et de <wrapMode>
00107             Color interp(float x, float y) const;           // interpolation bilinéaire : image mappée sur [0,w[*[0,h[
00108             inline Color operator()(float x, float y) const { return interp(x,y); }
00109             inline const Color* mem() const { return data; }
00110             inline       Color* mem()       { return data; }
00111             
00112         private:
00113             Color *data;
00114             Color borderColor;
00115             WrapMode wrapMode;
00116         };
00117     
00118     typedef Image2D<UByte4> Image2DUByte4;
00119     typedef Image2D<Float1> Image2DFloat;
00120     typedef Image2D<Float2> Image2DFloat2;
00121     typedef Image2D<Float3> Image2DFloat3;
00122     typedef Image2D<Float4> Image2DFloat4;
00123     
00124     }
00125 
00126 #include "Image2D_impl.h"
00127 
00128 #endif
00129 

Generated on Fri Nov 14 20:49:47 2008 for Api Graphics by  doxygen 1.4.4