xref: /haiku/src/servers/app/drawing/PatternHandler.h (revision e7c8829c5d8e5d34a2a1e111f1c06aceff256013)
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 			void				MakeOpCopyColorCache();
135 	inline	const rgb_color*	OpCopyColorCache() const
136 									{ return fOpCopyColorCache; }
137 
138  private:
139 			Pattern				fPattern;
140 			rgb_color			fHighColor;
141 			rgb_color			fLowColor;
142 
143 			uint16				fXOffset;
144 			uint16				fYOffset;
145 
146 			rgb_color			fOpCopyColorCache[256];
147 			uint64				fColorsWhenCached;
148 };
149 
150 /*!
151 	\brief Obtains the color in the pattern at the specified coordinates
152 	\param x X coordinate to get the color for
153 	\param y Y coordinate to get the color for
154 	\return Color for the coordinates
155 */
156 inline rgb_color
157 PatternHandler::ColorAt(int x, int y) const
158 {
159 	return IsHighColor(x, y) ? fHighColor : fLowColor;
160 }
161 
162 /*!
163 	\brief Obtains the value of the pattern at the specified coordinates
164 	\param pt Coordinates to get the value for
165 	\return Value for the coordinates - true if high, false if low.
166 */
167 
168 inline bool
169 PatternHandler::IsHighColor(int x, int y) const
170 {
171 	x -= fXOffset;
172 	y -= fYOffset;
173 	const int8* ptr = fPattern.GetInt8();
174 	int32 value = ptr[y & 7] & (1 << (7 - (x & 7)) );
175 
176 	return value != 0;
177 }
178 
179 #endif
180