1
/////////////////////////////////////////////////////////////////////////////////
3
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
4
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
5
// See license-pdn.txt for full licensing and attribution details. //
6
/////////////////////////////////////////////////////////////////////////////////
14
/// Adapts a Surface class so it can be used as a two dimensional boolean array.
15
/// Elements are stored compactly, such that each pixel stores 32 boolean values.
16
/// However, the usable width is the same as that of the adapted surface.
17
/// (in other words, a surface that is 100 pixels wide can still only store 100
20
public sealed class BitVector2DSurfaceAdapter
23
private ImageSurface surface;
25
public BitVector2DSurfaceAdapter (ImageSurface surface)
29
throw new ArgumentNullException("surface");
32
this.surface = surface;
47
return surface.Height;
55
return (Width == 0) || (Height == 0);
59
public void Clear (bool newValue)
62
uint val = newValue ? 0xffffffff : 0;
64
for (int y = 0; y < Height; ++y) {
65
ColorBgra* row = surface.GetRowAddressUnchecked (y);
67
int w = (this.Width + 31) / 32;
78
public bool Get(int x, int y)
80
if (x < 0 || x >= this.Width)
82
throw new ArgumentOutOfRangeException("x");
85
if (y < 0 || y >= this.Height)
87
throw new ArgumentOutOfRangeException("y");
90
return GetUnchecked(x, y);
93
public unsafe bool GetUnchecked(int x, int y)
97
uint mask = surface.GetPointAddressUnchecked(cx, y)->Bgra;
98
return 0 != (mask & (1 << sx));
101
public void Set(int x, int y, bool newValue)
103
if (x < 0 || x >= this.Width)
105
throw new ArgumentOutOfRangeException("x");
108
if (y < 0 || y >= this.Height)
110
throw new ArgumentOutOfRangeException("y");
113
SetUnchecked(x, y, newValue);
116
public void Set(Point pt, bool newValue)
118
Set(pt.X, pt.Y, newValue);
121
public void Set(Rectangle rect, bool newValue)
123
for (int y = (int)rect.Y; y < rect.GetBottom (); ++y)
125
for (int x = (int)rect.X; x < rect.GetRight (); ++x)
132
public void Set(Scanline scan, bool newValue)
136
while (x < scan.X + scan.Length)
138
Set(x, scan.Y, newValue);
143
//public void Set(PdnRegion region, bool newValue)
145
// foreach (Rectangle rect in region.GetRegionScansReadOnlyInt())
147
// Set(rect, newValue);
151
public unsafe void SetUnchecked(int x, int y, bool newValue)
155
ColorBgra *ptr = surface.GetPointAddressUnchecked(cx, y);
156
uint mask = ptr->Bgra;
157
uint slice = ((uint)1 << sx);
162
newMask = mask | slice;
166
newMask = mask & ~slice;
172
public void Invert(int x, int y)
174
Set(x, y, !Get(x, y));
177
public void Invert(Point pt)
182
public void Invert(Rectangle rect)
184
for (int y = (int)rect.Y; y < rect.GetBottom (); ++y)
186
for (int x = (int)rect.X; x < rect.GetRight (); ++x)
193
public void Invert(Scanline scan)
197
while (x < scan.X + scan.Length)
204
//public void Invert(PdnRegion region)
206
// foreach (Rectangle rect in region.GetRegionScansReadOnlyInt())
212
public bool this[Point pt]
216
return this[pt.X, pt.Y];
221
this[pt.X, pt.Y] = value;
225
public bool this[int x, int y]
238
public BitVector2DSurfaceAdapter Clone()
240
ImageSurface clonedSurface = this.surface.Clone();
241
return new BitVector2DSurfaceAdapter(clonedSurface);
244
object ICloneable.Clone()