xref: /haiku/src/servers/app/drawing/PatternHandler.h (revision a381c8a06378de22ff08adf4282b4e3f7e50d250)
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(void) {}
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(void) const
38 									{ return fPattern.type8; }
39 
40 	inline	uint64				GetInt64(void) const
41 									{ return fPattern.type64; }
42 
43 	inline	void				Set(const int8* p)
44 									{ fPattern.type64 = *((const uint64*)p); }
45 
46 	inline	void				Set(const uint64& p)
47 									{ fPattern.type64 = p; }
48 
49 			Pattern&			operator=(const Pattern& from)
50 									{ fPattern.type64 = from.fPattern.type64; return *this; }
51 
52 			Pattern&			operator=(const int64 &from)
53 									{ fPattern.type64 = from; return *this; }
54 
55 			Pattern&			operator=(const pattern &from)
56 									{ memcpy(&fPattern.type64, &from, sizeof(pattern)); return *this; }
57 
58 			bool				operator==(const Pattern& other) const
59 									{ return fPattern.type64 == other.fPattern.type64; }
60 
61 			bool				operator==(const pattern& other) const
62 									{ return fPattern.type64 == *(uint64*)other.data; }
63 
64  private:
65 
66 	typedef union
67 	{
68 		uint64	type64;
69 		int8	type8[8];
70 	} pattern_union;
71 
72 			pattern_union		fPattern;
73 };
74 
75 extern const Pattern kSolidHigh;
76 extern const Pattern kSolidLow;
77 extern const Pattern kMixedColors;
78 
79 /*!
80 	\brief Class for easy calculation and use of patterns
81 
82 	PatternHandlers are designed specifically for DisplayDriver subclasses.
83 	Pattern support can be easily added by setting the pattern to use via
84 	SetTarget, and then merely retrieving the value for the coordinates
85 	specified.
86 */
87 class PatternHandler {
88  public:
89 								PatternHandler(void);
90 								PatternHandler(const int8* p);
91 								PatternHandler(const uint64& p);
92 								PatternHandler(const Pattern& p);
93 								PatternHandler(const PatternHandler& other);
94 	virtual						~PatternHandler(void);
95 
96 			void				SetPattern(const int8* p);
97 			void				SetPattern(const uint64& p);
98 			void				SetPattern(const Pattern& p);
99 			void				SetPattern(const pattern& p);
100 
101 			void				SetColors(const rgb_color& high,
102 									const rgb_color& low);
103 			void				SetHighColor(const rgb_color& color);
104 			void				SetLowColor(const rgb_color& color);
105 
106 			rgb_color			HighColor() const
107 									{ return fHighColor; }
108 			rgb_color			LowColor() const
109 									{ return fLowColor; }
110 
111 			rgb_color			ColorAt(const BPoint& pt) const;
112 			rgb_color			ColorAt(float x, float y) const;
113 	inline	rgb_color			ColorAt(int x, int y) const;
114 
115 			bool				IsHighColor(const BPoint& pt) const;
116 	inline	bool				IsHighColor(int x, int y) const;
117 	inline	bool				IsSolidHigh() const
118 									{ return fPattern == B_SOLID_HIGH; }
119 	inline	bool				IsSolidLow() const
120 									{ return fPattern == B_SOLID_LOW; }
121 	inline	bool				IsSolid() const
122 									{ return IsSolidHigh() || IsSolidLow(); }
123 
124 			const pattern*		GetR5Pattern(void) const
125 									{ return (const pattern*)fPattern.GetInt8(); }
126 			const Pattern&		GetPattern(void) const
127 									{ return fPattern; }
128 
129 			void				SetOffsets(int32 x, int32 y);
130 
131 			void				MakeOpCopyColorCache();
132 	inline	const rgb_color*	OpCopyColorCache() const
133 									{ return fOpCopyColorCache; }
134 
135  private:
136 			Pattern				fPattern;
137 			rgb_color			fHighColor;
138 			rgb_color			fLowColor;
139 
140 			uint16				fXOffset;
141 			uint16				fYOffset;
142 
143 			rgb_color			fOpCopyColorCache[256];
144 			uint64				fColorsWhenCached;
145 };
146 
147 /*!
148 	\brief Obtains the color in the pattern at the specified coordinates
149 	\param x X coordinate to get the color for
150 	\param y Y coordinate to get the color for
151 	\return Color for the coordinates
152 */
153 inline rgb_color
154 PatternHandler::ColorAt(int x, int y) const
155 {
156 	return IsHighColor(x, y) ? fHighColor : fLowColor;
157 }
158 
159 /*!
160 	\brief Obtains the value of the pattern at the specified coordinates
161 	\param pt Coordinates to get the value for
162 	\return Value for the coordinates - true if high, false if low.
163 */
164 
165 inline bool
166 PatternHandler::IsHighColor(int x, int y) const
167 {
168 	x -= fXOffset;
169 	y -= fYOffset;
170 	const int8* ptr = fPattern.GetInt8();
171 	int32 value = ptr[y & 7] & (1 << (7 - (x & 7)) );
172 
173 	return value != 0;
174 }
175 
176 #endif
177