xref: /haiku/src/servers/app/drawing/PatternHandler.h (revision 52f7c9389475e19fc21487b38064b4390eeb6fea)
1 /*
2  * Copyright (c) 2001-2007, Haiku, Inc.
3  * Distributed under the terms of the MIT license.
4  *
5  * Author:	DarkWyrm <bpmagic@columbus.rr.com>
6  *			Stephan Aßmus <superstippi@gmx.de>
7  */
8 #ifndef PATTERNHANDLER_H
9 #define PATTERNHANDLER_H
10 
11 
12 #include <stdio.h>
13 #include <string.h>
14 
15 #include <GraphicsDefs.h>
16 
17 class BPoint;
18 
19 
20 class Pattern {
21  public:
22 
23 								Pattern() {}
24 
25 								Pattern(const uint64& p)
26 									{ fPattern.type64 = p; }
27 
28 								Pattern(const int8* p)
29 									{ fPattern.type64 = *((const uint64*)p); }
30 
31 								Pattern(const Pattern& src)
32 									{ fPattern.type64 = src.fPattern.type64; }
33 
34 								Pattern(const pattern& src)
35 									{ fPattern.type64 = *(uint64*)src.data; }
36 
37 	inline	const int8*			GetInt8() const
38 									{ return fPattern.type8; }
39 
40 	inline	uint64				GetInt64() const
41 									{ return fPattern.type64; }
42 
43 	inline	const ::pattern&	GetPattern() const
44 									{ return *(const ::pattern*)&fPattern.type64; }
45 
46 	inline	void				Set(const int8* p)
47 									{ fPattern.type64 = *((const uint64*)p); }
48 
49 	inline	void				Set(const uint64& p)
50 									{ fPattern.type64 = p; }
51 
52 			Pattern&			operator=(const Pattern& from)
53 									{ fPattern.type64 = from.fPattern.type64; return *this; }
54 
55 			Pattern&			operator=(const int64 &from)
56 									{ fPattern.type64 = from; return *this; }
57 
58 			Pattern&			operator=(const pattern &from)
59 									{ memcpy(&fPattern.type64, &from, sizeof(pattern)); return *this; }
60 
61 			bool				operator==(const Pattern& other) const
62 									{ return fPattern.type64 == other.fPattern.type64; }
63 
64 			bool				operator==(const pattern& other) const
65 									{ return fPattern.type64 == *(uint64*)other.data; }
66 
67  private:
68 
69 	typedef union
70 	{
71 		uint64	type64;
72 		int8	type8[8];
73 	} pattern_union;
74 
75 			pattern_union		fPattern;
76 };
77 
78 extern const Pattern kSolidHigh;
79 extern const Pattern kSolidLow;
80 extern const Pattern kMixedColors;
81 
82 /*!
83 	\brief Class for easy calculation and use of patterns
84 
85 	PatternHandlers are designed specifically for DisplayDriver subclasses.
86 	Pattern support can be easily added by setting the pattern to use via
87 	SetTarget, and then merely retrieving the value for the coordinates
88 	specified.
89 */
90 class PatternHandler {
91  public:
92 								PatternHandler(void);
93 								PatternHandler(const int8* p);
94 								PatternHandler(const uint64& p);
95 								PatternHandler(const Pattern& p);
96 								PatternHandler(const PatternHandler& other);
97 	virtual						~PatternHandler(void);
98 
99 			void				SetPattern(const int8* p);
100 			void				SetPattern(const uint64& p);
101 			void				SetPattern(const Pattern& p);
102 			void				SetPattern(const pattern& p);
103 
104 			void				SetColors(const rgb_color& high,
105 									const rgb_color& low);
106 			void				SetHighColor(const rgb_color& color);
107 			void				SetLowColor(const rgb_color& color);
108 
109 			rgb_color			HighColor() const
110 									{ return fHighColor; }
111 			rgb_color			LowColor() const
112 									{ return fLowColor; }
113 
114 			rgb_color			ColorAt(const BPoint& pt) const;
115 			rgb_color			ColorAt(float x, float y) const;
116 	inline	rgb_color			ColorAt(int x, int y) const;
117 
118 			bool				IsHighColor(const BPoint& pt) const;
119 	inline	bool				IsHighColor(int x, int y) const;
120 	inline	bool				IsSolidHigh() const
121 									{ return fPattern == B_SOLID_HIGH; }
122 	inline	bool				IsSolidLow() const
123 									{ return fPattern == B_SOLID_LOW; }
124 	inline	bool				IsSolid() const
125 									{ return IsSolidHigh() || IsSolidLow(); }
126 
127 			const pattern*		GetR5Pattern(void) const
128 									{ return (const pattern*)fPattern.GetInt8(); }
129 			const Pattern&		GetPattern(void) const
130 									{ return fPattern; }
131 
132 			void				SetOffsets(int32 x, int32 y);
133 
134  private:
135 			Pattern				fPattern;
136 			rgb_color			fHighColor;
137 			rgb_color			fLowColor;
138 
139 			uint16				fXOffset;
140 			uint16				fYOffset;
141 };
142 
143 /*!
144 	\brief Obtains the color in the pattern at the specified coordinates
145 	\param x X coordinate to get the color for
146 	\param y Y coordinate to get the color for
147 	\return Color for the coordinates
148 */
149 inline rgb_color
150 PatternHandler::ColorAt(int x, int y) const
151 {
152 	return IsHighColor(x, y) ? fHighColor : fLowColor;
153 }
154 
155 /*!
156 	\brief Obtains the value of the pattern at the specified coordinates
157 	\param pt Coordinates to get the value for
158 	\return Value for the coordinates - true if high, false if low.
159 */
160 
161 inline bool
162 PatternHandler::IsHighColor(int x, int y) const
163 {
164 	x -= fXOffset;
165 	y -= fYOffset;
166 	const int8* ptr = fPattern.GetInt8();
167 	int32 value = ptr[y & 7] & (1 << (7 - (x & 7)) );
168 
169 	return value != 0;
170 }
171 
172 #endif
173