20130313
[gdash.git] / src / gfx / pixbuf.hpp
blob2e5b4213ccd003c86f8b3ce6e006cf679af007c6
1 /*
2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #ifndef GD_PIXBUF
17 #define GD_PIXBUF
19 #include <glib.h>
20 #include <vector>
22 #include "config.h"
24 class GdColor;
26 /// @defgroup Graphics
28 /// @ingroup Graphics
29 /// A class which represents a 32-bit RGBA image in memory.
30 class Pixbuf {
31 public:
33 /* these masks are always for RGBA ordering in memory.
34 that is what gdk-pixbuf uses, and also what sdl uses.
35 no BGR, no ABGR.
37 #if G_BYTE_ORDER == G_BIG_ENDIAN
38 static const guint32 rmask = 0xff000000;
39 static const guint32 gmask = 0x00ff0000;
40 static const guint32 bmask = 0x0000ff00;
41 static const guint32 amask = 0x000000ff;
43 static const guint32 rshift = 24;
44 static const guint32 gshift = 16;
45 static const guint32 bshift = 8;
46 static const guint32 ashift = 0;
47 #else
48 static const guint32 rmask = 0x000000ff;
49 static const guint32 gmask = 0x0000ff00;
50 static const guint32 bmask = 0x00ff0000;
51 static const guint32 amask = 0xff000000;
53 static const guint32 rshift = 0;
54 static const guint32 gshift = 8;
55 static const guint32 bshift = 16;
56 static const guint32 ashift = 24;
57 #endif
59 /// Creates a guint32 value, which can be raw-written to a pixbuf memory area.
60 static guint32 rgba_pixel_from_color(const GdColor& col, unsigned a);
62 /// Get the width of the pixbuf in pixels.
63 virtual int get_width() const=0;
65 /// Get the height of the pixbuf in pixels.
66 virtual int get_height() const=0;
68 /// @brief Blit pixbuf to another pixbuf (for an area).
69 /// @param dest The destination pixbuf.
70 /// @param x Upper left coordinate of source area to copy.
71 /// @param y Upper left coordinate of source area to copy.
72 /// @param w Width of area to copy.
73 /// @param h Height of area to copy.
74 /// @param dx Destination coordinate, upper left corner.
75 /// @param dy Destination coordinate, upper left corner.
76 virtual void blit_full(int x, int y, int w, int h, Pixbuf &dest, int dx, int dy) const=0;
78 /// @brief Blit pixbuf to another pixbuf (for an area).
79 /// @param dest The destination pixbuf.
80 /// @param x Upper left coordinate of source area to copy.
81 /// @param y Upper left coordinate of source area to copy.
82 /// @param w Width of area to copy.
83 /// @param h Height of area to copy.
84 /// @param dx Destination coordinate, upper left corner.
85 /// @param dy Destination coordinate, upper left corner.
86 /// The purpose of this function is to give an overloaded name for blit_full,
87 /// without requiring derived classes to say "using Pixbuf::blit".
88 void blit(int x, int y, int w, int h, Pixbuf &dest, int dx, int dy) const {
89 blit_full(x, y, w, h, dest, dx, dy);
92 /// @brief Blit the whole pixbuf to another pixbuf.
93 /// @param dest The destination pixbuf.
94 /// @param dx Destination coordinate, upper left corner.
95 /// @param dy Destination coordinate, upper left corner.
96 void blit(Pixbuf& dest, int dx, int dy) const {
97 blit_full(0, 0, get_width(), get_height(), dest, dx, dy);
100 /// @brief Copy pixbuf to another pixbuf (copy area). No alpha-blending, no nothing, just copies data.
101 /// @param dest The destination pixbuf.
102 /// @param x Upper left coordinate of source area to copy.
103 /// @param y Upper left coordinate of source area to copy.
104 /// @param w Width of area to copy.
105 /// @param h Height of area to copy.
106 /// @param dx Destination coordinate, upper left corner.
107 /// @param dy Destination coordinate, upper left corner.
108 virtual void copy_full(int x, int y, int w, int h, Pixbuf &dest, int dx, int dy) const=0;
110 /// @brief Copy pixbuf to another pixbuf (copy area). No alpha-blending, no nothing, just copies data.
111 /// @param dest The destination pixbuf.
112 /// @param x Upper left coordinate of source area to copy.
113 /// @param y Upper left coordinate of source area to copy.
114 /// @param w Width of area to copy.
115 /// @param h Height of area to copy.
116 /// @param dx Destination coordinate, upper left corner.
117 /// @param dy Destination coordinate, upper left corner.
118 /// The purpose of this function is to give an overloaded name for blit_full,
119 /// without requiring derived classes to say "using Pixbuf::blit".
120 void copy(int x, int y, int w, int h, Pixbuf &dest, int dx, int dy) const {
121 copy_full(x, y, w, h, dest, dx, dy);
124 /// @brief Copy pixbuf to another pixbuf (copy area). No alpha-blending, no nothing, just copies data.
125 /// @param dest The destination pixbuf.
126 /// @param dx Destination coordinate, upper left corner.
127 /// @param dy Destination coordinate, upper left corner.
128 void copy(Pixbuf& dest, int dx, int dy) const {
129 copy_full(0, 0, get_width(), get_height(), dest, dx, dy);
132 /// @brief Check if the pixbuf has an alpha channel.
133 /// @return true, if it has one.
134 virtual bool has_alpha() const=0;
136 /// @brief Fill the given area of the pixbuf with the specified color.
137 /// No blending takes place!
138 virtual void fill_rect(int x, int y, int w, int h, const GdColor& c)=0;
139 void fill(const GdColor &c) {
140 fill_rect(0, 0, get_width(), get_height(), c);
143 virtual unsigned char *get_pixels() const=0;
144 virtual int get_pitch() const=0;
146 /// Get pointer to row number n, cast to a 32-bit unsigned integer.
147 /// Using this, one can: pixbuf.get_row(y)[x]=0xRRGGBBAA or 0xAABBGGRR;
148 guint32* get_row(int y) { return reinterpret_cast<guint32*>(get_pixels()+y*get_pitch()); }
149 const guint32* get_row(int y) const { return reinterpret_cast<guint32*>(get_pixels()+y*get_pitch()); }
150 /// Generic get/putpixel.
151 guint32& operator()(int x, int y) { return get_row(y)[x]; }
152 /// Generic getpixel.
153 guint32 const& operator()(int x, int y) const { return get_row(y)[x]; }
155 /// Lock a surface - enable only the current thread to access the pixbuf.
156 virtual void lock() const {}
158 /// Unlock the surface.
159 virtual void unlock() const {}
161 virtual ~Pixbuf() {}
163 static std::vector<unsigned char> c64_gfx_data_from_pixbuf(Pixbuf const& image);
166 #endif