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;
00027 virtual void destroy() = 0;
00028
00029 virtual QImage toQImage() const = 0;
00030 virtual void initialize(QImage image) = 0;
00031
00032 void save(QString fileName) const;
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);
00070 Image2D<Color> clone() const;
00071 Image2D<Color> subImage(int i, int j, int w, int h) const;
00072 Image2D<Color> supImage(int i, int j, int w, int h) const;
00073 Image2D<Color> boundingPowerOfTwo() const;
00074
00075 Image1D<Color> line(int j) const { return Image1D<Color>(w, data + j*w); }
00076
00077 virtual GLint defaultTexFormat() const { return Color::TEX_FORMAT; }
00078 virtual void loadTexture2D(GLenum texFormat, GLenum target = GL_TEXTURE_2D) const;
00079
00080
00081 static Image2D<Color> getColorBuffer();
00082 static Image2D<Color> getDepthBuffer();
00083 void readColorBuffer(int i=0, int j=0);
00084 void readDepthBuffer(int i=0, int j=0);
00085 void drawColorBuffer(int i=0, int j=0) const;
00086 static Color readPixelColor(int i, int j);
00087
00088
00089 static Image2D<Color> getTexture(const Texture *tex);
00090 void readTexture(const Texture *tex);
00091
00092
00093 static Texture createTex2D(QString fileName, GLint internalFormat = GL_RGBA, GLenum interpMode = GL_NEAREST, GLenum wrapMode = GL_CLAMP);
00094
00095
00096 void setBorderColor(Color border);
00097 void setWrapMode(WrapMode wrapMode);
00098 void setupBorder(WrapMode wrapMode, Color border);
00099
00100
00101
00102 inline Color& texel(int i, int j) { return data[i + j*w]; }
00103 inline const Color& texel(int i, int j) const { return data[i + j*w]; }
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;
00107 Color interp(float x, float y) const;
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