00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00033 #ifndef _BITMAP_H
00034 #define _BITMAP_H
00035
00036 #include <stdlib.h>
00037 #include <string.h>
00038
00039 #ifndef MIN
00040 # define MAX(a, b) ((a) > (b) ? (a) : (b))
00041 # define MIN(a, b) ((a) < (b) ? (a) : (b))
00042 #endif
00043
00049 class Bitmap {
00050 public:
00052 enum ColorSpace { CS_RGB, CS_RGBA, CS_BGR, CS_ABGR, CS_YV12, CS_YV12A };
00053
00055 Bitmap(int width = 0, int height = 0):
00056 _width(height), _height(height), _buf(NULL) {
00057 }
00058 virtual ~Bitmap() { free(_buf); }
00059
00061 int w() const { return _width; }
00063 int h() const { return _height; }
00065 int get_space() const { return _width * _height * _bpp / 8; }
00068 int get_space(int width, int height) const {
00069 return width * height * _bpp / 8;
00070 }
00075 unsigned char *get_buf() const { return _buf; }
00076
00080 virtual Bitmap *create() const = 0;
00084 virtual Bitmap *clone() const;
00085
00090 virtual void set_size(int width, int height);
00091
00093 virtual void clear();
00094
00096 virtual enum ColorSpace get_color_space() const = 0;
00099 virtual void convert(Bitmap &target) const;
00103
00104 virtual Bitmap *unify_csp(Bitmap &bitmap) const;
00105
00111 void resize(int left = 0, int right = 0, int top = 0, int bottom = 0);
00116 Bitmap *resize_copy(int left=0, int right=0, int top=0, int bottom=0) const;
00117
00128 virtual void blend(const Bitmap &source,
00129 int xoff = 0, int yoff = 0, int alpha = 0);
00130
00139 virtual void put(const Bitmap &source, int xoff = 0, int yoff = 0);
00140
00141 protected:
00142 void alloc(int width, int height);
00143 virtual void resize_into(int left, int right, int top, int bottom,
00144 Bitmap *target) const = 0;
00145
00146 virtual int channel_count() const { return 3; }
00147 virtual int get_pixel_alpha(int pixel) const = 0;
00148 virtual void set_pixel_alpha(int pixel, int value) = 0;
00149 virtual int get_pixel_val(int pixel, int channel) const = 0;
00150 virtual void set_pixel_val(int pixel, int channel, int value) = 0;
00151
00152 int _bpp;
00153
00154 private:
00155 int _width;
00156 int _height;
00157 unsigned char *_buf;
00158 };
00159
00165 class Packed: public Bitmap {
00166 protected:
00167 virtual void resize_into(int left, int right, int top, int bottom,
00168 Bitmap *target) const;
00169
00170 virtual int get_pixel_val(int pixel, int channel) const {
00171 return get_buf()[pixel*_bpp/8 + channel]; }
00172 virtual void set_pixel_val(int pixel, int channel, int value) {
00173 get_buf()[pixel*_bpp/8 + channel] = value; }
00174 };
00175
00179 class RGB: public Packed {
00180 public:
00181 RGB(int width = 0, int height = 0) { _bpp = 24; set_size(width, height); }
00182 virtual enum ColorSpace get_color_space() const { return CS_RGB; }
00183 virtual Bitmap *create() const { return new RGB; }
00184
00185 protected:
00186 virtual int get_pixel_alpha(int pixel) const { return 255; }
00187 virtual void set_pixel_alpha(int pixel, int value) {}
00188 };
00189
00194 class RGBA: public Packed {
00195 public:
00196 RGBA(int width = 0, int height = 0) { _bpp = 32; set_size(width, height); }
00197 virtual enum ColorSpace get_color_space() const { return CS_RGBA; }
00198 virtual Bitmap *create() const { return new RGBA; }
00199 virtual void blend(const Bitmap &source,
00200 int xoff = 0, int yoff = 0, int alpha = 0);
00201 virtual void put(const Bitmap &source, int xoff = 0, int yoff = 0);
00202
00203 protected:
00204 virtual int get_pixel_alpha(int pixel) const {
00205 return get_buf()[pixel*4+3]; }
00206 virtual void set_pixel_alpha(int pixel, int value) {
00207 get_buf()[pixel*4+3] = value; }
00208 };
00209
00214 class ABGR: public Packed {
00215 public:
00216 ABGR(int width = 0, int height = 0) { _bpp = 32; set_size(width, height); }
00217 virtual enum ColorSpace get_color_space() const { return CS_ABGR; }
00218 virtual Bitmap *create() const { return new ABGR; }
00219
00220 protected:
00221 virtual int get_pixel_alpha(int pixel) const { return get_buf()[pixel*4+0];}
00222 virtual void set_pixel_alpha(int pixel, int value) {
00223 get_buf()[pixel*4+0] = value; };
00224 };
00225
00226 #endif