xref: /haiku/src/servers/app/drawing/Painter/drawing_modes/PixelFormat.h (revision a5061ecec55353a5f394759473f1fd6df04890da)
1 /*
2  * Copyright 2005, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2008, Andrej Spielmann <andrej.spielmann@seh.ox.ac.uk>
4  * All rights reserved. Distributed under the terms of the MIT License.
5  *
6  * Copyright 2002-2004 Maxim Shemanarev (http://www.antigrain.com)
7  *
8  * A class implementing the AGG "pixel format" interface which maintains
9  * a PatternHandler and pointers to blending functions implementing the
10  * different BeOS "drawing_modes".
11  *
12  */
13 
14 #ifndef PIXEL_FORMAT_H
15 #define PIXEL_FORMAT_H
16 
17 #include <agg_basics.h>
18 #include <agg_color_rgba.h>
19 #include <agg_rendering_buffer.h>
20 #include <agg_pixfmt_rgba.h>
21 
22 #include <GraphicsDefs.h>
23 
24 #include "AggCompOpAdapter.h"
25 
26 class PatternHandler;
27 
28 class PixelFormat {
29  public:
30 	typedef agg::rgba8						color_type;
31 	typedef agg::rendering_buffer			agg_buffer;
32 	typedef agg::rendering_buffer::row_data	row_data;
33 	typedef agg::order_bgra					order_type;
34 
35 	typedef agg::comp_op_rgba_src_in<color_type, order_type>	comp_src_in;
36 	typedef agg::comp_op_rgba_src_out<color_type, order_type>	comp_src_out;
37 	typedef agg::comp_op_rgba_src_atop<color_type, order_type>	comp_src_atop;
38 	typedef agg::comp_op_rgba_dst_over<color_type, order_type>	comp_dst_over;
39 	typedef agg::comp_op_rgba_dst_in<color_type, order_type>	comp_dst_in;
40 	typedef agg::comp_op_rgba_dst_out<color_type, order_type>	comp_dst_out;
41 	typedef agg::comp_op_rgba_dst_atop<color_type, order_type>	comp_dst_atop;
42 	typedef agg::comp_op_rgba_xor<color_type, order_type>		comp_xor;
43 	typedef agg::comp_op_rgba_clear<color_type, order_type>		comp_clear;
44 	typedef agg::comp_op_rgba_difference<color_type, order_type>
45 		comp_difference;
46 	typedef agg::comp_op_rgba_lighten<color_type, order_type>	comp_lighten;
47 	typedef agg::comp_op_rgba_darken<color_type, order_type>	comp_darken;
48 
49 	typedef AggCompOpAdapter<comp_src_in, agg_buffer>		alpha_src_in;
50 	typedef AggCompOpAdapter<comp_src_out, agg_buffer>		alpha_src_out;
51 	typedef AggCompOpAdapter<comp_src_atop, agg_buffer>		alpha_src_atop;
52 	typedef AggCompOpAdapter<comp_dst_over, agg_buffer>		alpha_dst_over;
53 	typedef AggCompOpAdapter<comp_dst_in, agg_buffer>		alpha_dst_in;
54 	typedef AggCompOpAdapter<comp_dst_out, agg_buffer>		alpha_dst_out;
55 	typedef AggCompOpAdapter<comp_dst_atop, agg_buffer>		alpha_dst_atop;
56 	typedef AggCompOpAdapter<comp_xor, agg_buffer>			alpha_xor;
57 	typedef AggCompOpAdapter<comp_clear, agg_buffer>		alpha_clear;
58 	typedef AggCompOpAdapter<comp_difference, agg_buffer>	alpha_difference;
59 	typedef AggCompOpAdapter<comp_lighten, agg_buffer>		alpha_lighten;
60 	typedef AggCompOpAdapter<comp_darken, agg_buffer>		alpha_darken;
61 
62 	enum base_scale_e
63 	{
64 		base_shift = color_type::base_shift,
65 		base_scale = color_type::base_scale,
66 		base_mask  = color_type::base_mask,
67 		pix_width  = 4,
68 	};
69 
70 	typedef void (*blend_pixel_f)(int x, int y, const color_type& c,
71 								  uint8 cover,
72 								  agg_buffer* buffer,
73 								  const PatternHandler* pattern);
74 
75 	typedef void (*blend_line)(int x, int y, unsigned len,
76 							   const color_type& c, uint8 cover,
77 							   agg_buffer* buffer,
78 							   const PatternHandler* pattern);
79 
80 	typedef void (*blend_solid_span)(int x, int y, unsigned len,
81 									 const color_type& c,
82 									 const uint8* covers,
83 									 agg_buffer* buffer,
84 									 const PatternHandler* pattern);
85 
86 	typedef void (*blend_color_span)(int x, int y, unsigned len,
87 									 const color_type* colors,
88 									 const uint8* covers,
89 									 uint8 cover,
90 									 agg_buffer* buffer,
91 									 const PatternHandler* pattern);
92 
93 			// PixelFormat class
94 								PixelFormat(agg::rendering_buffer& buffer,
95 											const PatternHandler* handler);
96 
97 								~PixelFormat();
98 
99 
100 			void				SetDrawingMode(drawing_mode mode,
101 											   source_alpha alphaSrcMode,
102 											   alpha_function alphaFncMode,
103 											   bool text);
104 
105 			// AGG "pixel format" interface
106 	inline	unsigned			width() const
107 									{ return fBuffer->width(); }
108 	inline	unsigned			height() const
109 									{ return fBuffer->height(); }
110 	inline	int					stride() const
111 									{ return fBuffer->stride(); }
112 
113 	inline	uint8*				row_ptr(int y)
114 									{ return fBuffer->row_ptr(y); }
115 	inline	const uint8*		row_ptr(int y) const
116 									{ return fBuffer->row_ptr(y); }
117 	inline	row_data			row(int y) const
118 									{ return fBuffer->row(y); }
119 
120 	inline	uint8*				pix_ptr(int x, int y);
121 	inline	const uint8*		pix_ptr(int x, int y) const;
122 
123 	inline	static	void		make_pix(uint8* p, const color_type& c);
124 //	inline	color_type			pixel(int x, int y) const;
125 
126 	inline	void				blend_pixel(int x, int y,
127 											const color_type& c,
128 											uint8 cover);
129 
130 
131 	inline	void				blend_hline(int x, int y,
132 											unsigned len,
133 											const color_type& c,
134 											uint8 cover);
135 
136 	inline	void				blend_vline(int x, int y,
137 											unsigned len,
138 											const color_type& c,
139 											uint8 cover);
140 
141 	inline	void				blend_solid_hspan(int x, int y,
142 												  unsigned len,
143 												  const color_type& c,
144 												  const uint8* covers);
145 
146 	inline	void				blend_solid_hspan_subpix(int x, int y,
147 												  unsigned len,
148 												  const color_type& c,
149 												  const uint8* covers);
150 
151 	inline	void				blend_solid_vspan(int x, int y,
152 												  unsigned len,
153 												  const color_type& c,
154 												  const uint8* covers);
155 
156 	inline	void				blend_color_hspan(int x, int y,
157 												  unsigned len,
158 												  const color_type* colors,
159 												  const uint8* covers,
160 												  uint8 cover);
161 
162 	inline	void				blend_color_vspan(int x, int y,
163 												  unsigned len,
164 												  const color_type* colors,
165 												  const uint8* covers,
166 												  uint8 cover);
167 
168  private:
169 	agg::rendering_buffer*		fBuffer;
170 	const PatternHandler*		fPatternHandler;
171 
172 	blend_pixel_f				fBlendPixel;
173 	blend_line					fBlendHLine;
174 	blend_line					fBlendVLine;
175 	blend_solid_span			fBlendSolidHSpan;
176 	blend_solid_span            fBlendSolidHSpanSubpix;
177 	blend_solid_span			fBlendSolidVSpan;
178 	blend_color_span			fBlendColorHSpan;
179 	blend_color_span			fBlendColorVSpan;
180 
181 	template<typename T>
182 	void SetAggCompOpAdapter()
183 	{
184 		fBlendPixel = T::blend_pixel;
185 		fBlendHLine = T::blend_hline;
186 		fBlendSolidHSpanSubpix = T::blend_solid_hspan_subpix;
187 		fBlendSolidHSpan = T::blend_solid_hspan;
188 		fBlendSolidVSpan = T::blend_solid_vspan;
189 		fBlendColorHSpan = T::blend_color_hspan;
190 	}
191 };
192 
193 // inlined functions
194 
195 // pix_ptr
196 inline uint8*
197 PixelFormat::pix_ptr(int x, int y)
198 {
199 	return fBuffer->row_ptr(y) + x * pix_width;
200 }
201 
202 // pix_ptr
203 inline const uint8*
204 PixelFormat::pix_ptr(int x, int y) const
205 {
206 	return fBuffer->row_ptr(y) + x * pix_width;
207 }
208 
209 // make_pix
210 inline void
211 PixelFormat::make_pix(uint8* p, const color_type& c)
212 {
213 	p[0] = c.b;
214 	p[1] = c.g;
215 	p[2] = c.r;
216 	p[3] = c.a;
217 }
218 
219 //// pixel
220 //inline color_type
221 //PixelFormat::pixel(int x, int y) const
222 //{
223 //	const uint8* p = (const uint8*)fBuffer->row_ptr(y);
224 //	if (p) {
225 //		p += x << 2;
226 //		return color_type(p[2], p[1], p[0], p[3]);
227 //	}
228 //	return color_type::no_color();
229 //}
230 
231 // blend_pixel
232 inline void
233 PixelFormat::blend_pixel(int x, int y, const color_type& c, uint8 cover)
234 {
235 	fBlendPixel(x, y, c, cover, fBuffer, fPatternHandler);
236 }
237 
238 // blend_hline
239 inline void
240 PixelFormat::blend_hline(int x, int y, unsigned len,
241 						 const color_type& c, uint8 cover)
242 {
243 	fBlendHLine(x, y, len, c, cover, fBuffer, fPatternHandler);
244 }
245 
246 // blend_vline
247 inline void
248 PixelFormat::blend_vline(int x, int y, unsigned len,
249 						 const color_type& c, uint8 cover)
250 {
251 	fBlendVLine(x, y, len, c, cover, fBuffer, fPatternHandler);
252 }
253 
254 // blend_solid_hspan
255 inline void
256 PixelFormat::blend_solid_hspan(int x, int y, unsigned len,
257 							   const color_type& c, const uint8* covers)
258 {
259 	fBlendSolidHSpan(x, y, len, c, covers, fBuffer, fPatternHandler);
260 }
261 
262 // blend_solid_hspan_subpix
263 inline void
264 PixelFormat::blend_solid_hspan_subpix(int x, int y, unsigned len,
265 							   const color_type& c, const uint8* covers)
266 {
267 	fBlendSolidHSpanSubpix(x, y, len, c, covers, fBuffer, fPatternHandler);
268 }
269 
270 // blend_solid_vspan
271 inline void
272 PixelFormat::blend_solid_vspan(int x, int y, unsigned len,
273 							   const color_type& c, const uint8* covers)
274 {
275 	fBlendSolidVSpan(x, y, len, c, covers, fBuffer, fPatternHandler);
276 }
277 
278 // blend_color_hspan
279 inline void
280 PixelFormat::blend_color_hspan(int x, int y, unsigned len,
281 							   const color_type* colors,
282 							   const uint8* covers,
283 							   uint8 cover)
284 {
285 	fBlendColorHSpan(x, y, len, colors, covers, cover,
286 					 fBuffer, fPatternHandler);
287 }
288 
289 // blend_color_vspan
290 inline void
291 PixelFormat::blend_color_vspan(int x, int y, unsigned len,
292 							   const color_type* colors,
293 							   const uint8* covers,
294 							   uint8 cover)
295 {
296 	fBlendColorVSpan(x, y, len, colors, covers, cover,
297 					 fBuffer, fPatternHandler);
298 }
299 
300 #endif // PIXEL_FORMAT_H
301 
302