00001 #ifndef __IMAGE1D__
00002 #define __IMAGE1D__
00003
00004 #include <OpenGL.h>
00005 #include "TextureData.h"
00006 #include "ColorTypes.h"
00007 #include "Texture.h"
00008 #include <QImage>
00009
00010 namespace apig {
00011
00013 class AbstractImage1D : public TextureData {
00014 public:
00015 AbstractImage1D(int w=0) : w(w) {}
00016 virtual ~AbstractImage1D() {}
00017
00018 virtual bool loaded() const = 0;
00019 virtual void destroy() = 0;
00020
00021 virtual QImage toQImage() const = 0;
00022 virtual void initialize(QImage image) = 0;
00023
00024 void save(QString fileName) const;
00025
00026 int width() const { return w; }
00027 int height() const { return h; }
00028 bool contains(int i) const { return (i >= 0) && (i < w); }
00029 bool contains(float x) const { return (x >= 0) && (x < w); }
00030
00031 virtual GLint defaultTexFormat() const = 0;
00032 virtual GLenum textureMode() const { return GL_TEXTURE_1D; }
00033 virtual void loadToGPU(GLint texFormat) const { loadTexture1D(texFormat, GL_TEXTURE_1D); }
00034 virtual void loadTexture1D(GLint texFormat, GLenum target = GL_TEXTURE_1D) const = 0;
00035
00036 enum WrapMode { CLAMP_TO_EDGE, CLAMP_TO_BORDER, REPEAT, MIRRORED_REPEAT };
00037
00038 protected:
00039 int w, h;
00040 };
00041
00043
00048 template<class Color>
00049 class Image1D : public AbstractImage1D {
00050 public:
00051 Image1D(int w=0, Color* data=NULL, WrapMode wrap=CLAMP_TO_EDGE, Color border=Color());
00052 Image1D(QImage image);
00053 Image1D(QString fileName);
00054 virtual ~Image1D() {}
00055
00056 virtual bool loaded() const { return data != NULL; }
00057 virtual void destroy();
00058
00059 virtual QImage toQImage() const;
00060 virtual void initialize(QImage image);
00061 Image1D<Color> clone() const;
00062 void copy(const Image1D<Color> &image);
00063
00064 virtual GLint defaultTexFormat() const { return Color::TEX_FORMAT; }
00065 virtual void loadTexture1D(GLint texFormat, GLenum target = GL_TEXTURE_1D) const;
00066
00067
00068 static Image1D<Color> readTexture(Texture *tex);
00069
00070
00071 void setBorderColor(Color border);
00072 void setWrapMode(WrapMode wrapMode);
00073 void setupBorder(WrapMode wrapMode, Color border);
00074
00075
00076
00077 inline Color& texel(int i) { return data[i]; }
00078 inline const Color& texel(int i) const { return data[i]; }
00079 inline Color& operator()(int i) { return texel(i); }
00080 inline const Color& operator()(int i) const { return texel(i); }
00081 Color sample(int i) const;
00082 Color interp(float x) const;
00083 Color interpUnit(float x) const { return interp(x * w); }
00084 Color operator()(float x) const { return interp(x); }
00085 inline const Color* mem() const { return data; }
00086 inline Color* mem() { return data; }
00087
00088 private:
00089 Color *data;
00090 WrapMode wrapMode;
00091 Color borderColor;
00092 };
00093
00094 typedef Image1D<UByte4> Image1DUByte4;
00095 typedef Image1D<Float1> Image1DFloat;
00096 typedef Image1D<Float2> Image1DFloat2;
00097 typedef Image1D<Float3> Image1DFloat3;
00098 typedef Image1D<Float4> Image1DFloat4;
00099
00100 }
00101
00102 #include "Image1D_impl.h"
00103
00104 #endif
00105