vfs: check userland buffers before reading them.
[haiku.git] / src / servers / app / drawing / PatternHandler.cpp
blob78c9b43fd396b12e0ee86f528981e0d44867b3f2
1 /*
2 * Copyright (c) 2001-2007, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
5 * Author: DarkWyrm <bpmagic@columbus.rr.com>
6 * Stephan Aßmus <superstippi@gmx.de>
7 */
9 #include "PatternHandler.h"
11 #include <stdio.h>
12 #include <string.h>
14 #include <Point.h>
16 const Pattern kSolidHigh(0xFFFFFFFFFFFFFFFFLL);
17 const Pattern kSolidLow((uint64)0);
18 const Pattern kMixedColors(0xAAAAAAAAAAAAAAAALL);
20 const rgb_color kBlack = (rgb_color){ 0, 0, 0, 255 };
21 const rgb_color kWhite = (rgb_color){ 255, 255, 255, 255 };
23 /*!
24 \brief Void constructor
26 The pattern is set to B_SOLID_HIGH, high color is set to black, and
27 low color is set to white.
29 PatternHandler::PatternHandler(void)
30 : fPattern(kSolidHigh),
31 fHighColor(kBlack),
32 fLowColor(kWhite),
33 fXOffset(0),
34 fYOffset(0),
35 fColorsWhenCached(ULONGLONG_MAX)
37 memset(fOpCopyColorCache, 255, 256 * sizeof(rgb_color));
40 /*!
41 \brief Constructor initializes to given pattern
42 \param pat Pattern to use.
44 This initializes to the given pattern or B_SOLID_HIGH if the pattern
45 is NULL. High color is set to black, and low color is set to white.
47 PatternHandler::PatternHandler(const int8* pat)
48 : fPattern(pat ? Pattern(pat) : Pattern(kSolidHigh)),
49 fHighColor(kBlack),
50 fLowColor(kWhite),
51 fXOffset(0),
52 fYOffset(0),
53 fColorsWhenCached(ULONGLONG_MAX)
55 memset(fOpCopyColorCache, 255, 256 * sizeof(rgb_color));
58 /*!
59 \brief Constructor initializes to given pattern
60 \param pat Pattern to use.
62 This initializes to the given pattern or B_SOLID_HIGH if the pattern
63 is NULL. High color is set to black, and low color is set to white.
65 PatternHandler::PatternHandler(const uint64& pat)
66 : fPattern(pat),
67 fHighColor(kBlack),
68 fLowColor(kWhite),
69 fXOffset(0),
70 fYOffset(0),
71 fColorsWhenCached(ULONGLONG_MAX)
73 memset(fOpCopyColorCache, 255, 256 * sizeof(rgb_color));
76 /*!
77 \brief Constructor initializes to given pattern
78 \param pat Pattern to use.
80 This initializes to the given Pattern.
81 High color is set to black, and low color is set to white.
83 PatternHandler::PatternHandler(const Pattern& pat)
84 : fPattern(pat),
85 fHighColor(kBlack),
86 fLowColor(kWhite),
87 fXOffset(0),
88 fYOffset(0),
89 fColorsWhenCached(ULONGLONG_MAX)
91 memset(fOpCopyColorCache, 255, 256 * sizeof(rgb_color));
94 /*!
95 \brief Constructor initializes to given PatternHandler
96 \param other PatternHandler to copy.
98 Copy constructor.
100 PatternHandler::PatternHandler(const PatternHandler& other)
101 : fPattern(other.fPattern),
102 fHighColor(other.fHighColor),
103 fLowColor(other.fLowColor),
104 fXOffset(other.fXOffset),
105 fYOffset(other.fYOffset),
106 fColorsWhenCached(ULONGLONG_MAX)
108 memset(fOpCopyColorCache, 255, 256 * sizeof(rgb_color));
111 //! Destructor does nothing
112 PatternHandler::~PatternHandler(void)
117 \brief Sets the pattern for the handler to the one given
118 \param pat Pattern to use.
120 This initializes to the given pattern or B_SOLID_HIGH if the pattern
121 is NULL.
123 void
124 PatternHandler::SetPattern(const int8* pat)
126 if (pat)
127 fPattern.Set(pat);
128 else
129 fPattern = kSolidHigh;
133 \brief Sets the pattern for the handler to the one given
134 \param pat Pattern to use.
136 void
137 PatternHandler::SetPattern(const uint64& pat)
139 fPattern = pat;
143 \brief Sets the pattern for the handler to the one given
144 \param pat Pattern to use.
146 void
147 PatternHandler::SetPattern(const Pattern& pat)
149 fPattern = pat;
153 \brief Sets the pattern for the handler to the one given
154 \param pat R5 style pattern to use.
156 void
157 PatternHandler::SetPattern(const pattern& pat)
159 fPattern = pat;
163 \brief Set the colors for the pattern to use
164 \param high High color for the handler
165 \param low Low color for the handler
167 void
168 PatternHandler::SetColors(const rgb_color& high, const rgb_color& low)
170 fHighColor = high;
171 fLowColor = low;
175 \brief Set the high color for the pattern to use
176 \param color High color for the handler
178 void
179 PatternHandler::SetHighColor(const rgb_color& color)
181 fHighColor = color;
185 \brief Set the low color for the pattern to use
186 \param color Low color for the handler
188 void
189 PatternHandler::SetLowColor(const rgb_color& color)
191 fLowColor = color;
195 \brief Obtains the color in the pattern at the specified coordinates
196 \param pt Coordinates to get the color for
197 \return Color for the coordinates
199 rgb_color
200 PatternHandler::ColorAt(const BPoint &pt) const
202 return ColorAt(pt.x, pt.y);
206 \brief Obtains the color in the pattern at the specified coordinates
207 \param x X coordinate to get the color for
208 \param y Y coordinate to get the color for
209 \return Color for the coordinates
211 rgb_color
212 PatternHandler::ColorAt(float x, float y) const
214 return ColorAt(int(x), int(y));
218 \brief Obtains the value of the pattern at the specified coordinates
219 \param pt Coordinates to get the value for
220 \return Value for the coordinates - true if high, false if low.
222 bool
223 PatternHandler::IsHighColor(const BPoint &pt) const
225 return IsHighColor((int)pt.x, (int)pt.y);
229 \brief Transfers the scrolling offset of a BView to affect the pattern.
230 \param x Positive or negative horizontal offset
231 \param y Positive or negative vertical offset
233 void
234 PatternHandler::SetOffsets(int32 x, int32 y)
236 fXOffset = x & 7;
237 fYOffset = y & 7;
241 void
242 PatternHandler::MakeOpCopyColorCache()
244 uint64 t = *(uint32*)&fHighColor;
245 uint64 colors = (t << 32) | *(uint32*)&fLowColor;
247 if (fColorsWhenCached == colors)
248 return;
250 fColorsWhenCached = colors;
252 // ramp from low color to high color
253 uint8 rA = fLowColor.red;
254 uint8 gA = fLowColor.green;
255 uint8 bA = fLowColor.blue;
257 uint8 rB = fHighColor.red;
258 uint8 gB = fHighColor.green;
259 uint8 bB = fHighColor.blue;
261 for (int32 i = 0; i < 256; i++) {
262 // NOTE: rgb is twisted around, since this is
263 // only used as uint32 in the end
264 fOpCopyColorCache[i].red = (((bB - bA) * i) + (bA << 8)) >> 8;
265 fOpCopyColorCache[i].green = (((gB - gA) * i) + (gA << 8)) >> 8;
266 fOpCopyColorCache[i].blue = (((rB - rA) * i) + (rA << 8)) >> 8;