-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBCnImage.cs
121 lines (105 loc) · 3.14 KB
/
BCnImage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BCn
{
[Serializable]
public class BCnImage<T>
where T : struct
{
private int width, height;
private T[] blocks;
/// <summary>
/// Gets the image's width in pixels.
/// </summary>
public int Width { get { return width; } }
/// <summary>
/// Gets the image's height in pixels.
/// </summary>
public int Height { get { return height; } }
/// <summary>
/// Gets the image's width in terms of horizontal blocks.
/// </summary>
/// <remarks>
/// Note that the image size is rounded up to the nearest block when
/// computing this value.
/// </remarks>
public int WidthInBlocks { get { return (width + blockWidth - 1) / blockWidth; } }
/// <summary>
/// Gets the image's height in terms of vertical blocks.
/// </summary>
/// <remarks>
/// Note that the image size is rounded up to the nearest block when
/// computing this value.
/// </remarks>
public int HeightInBlocks { get { return (height + blockHeight - 1) / blockHeight; } }
/// <summary>
/// Gets the number of blocks in the image.
/// </summary>
public int BlockCount { get { return blocks.Length; } }
/// <summary>
/// Initializes a new BCnImage.
/// </summary>
/// <param name="width">The image's width in pixels.</param>
/// <param name="height">The image's height in pixels.</param>
public BCnImage( int width, int height )
{
if( width <= 0 )
throw new ArgumentOutOfRangeException( "width" );
if( height <= 0 )
throw new ArgumentOutOfRangeException( "height" );
this.width = width;
this.height = height;
blocks = new T[WidthInBlocks * HeightInBlocks];
}
/// <summary>
/// Gets the underlying storage for the image's blocks.
/// </summary>
public T[] GetBlockStorage()
{
return blocks;
}
/// <summary>
/// Gets or sets an individual block.
/// </summary>
/// <param name="xBlock">The block's x coordinate.</param>
/// <param name="yBlock">The block's y coordinate.</param>
/// <remarks>
/// The coordinates are in blocks, not pixels.
/// </remarks>
public T this[int xBlock, int yBlock]
{
get
{
if( xBlock < 0 || xBlock >= WidthInBlocks )
throw new ArgumentOutOfRangeException( "xBlock" );
if( yBlock < 0 || yBlock >= HeightInBlocks )
throw new ArgumentOutOfRangeException( "yBlock" );
return blocks[yBlock * WidthInBlocks + xBlock];
}
set
{
if( xBlock < 0 || xBlock >= WidthInBlocks )
throw new ArgumentOutOfRangeException( "xBlock" );
if( yBlock < 0 || yBlock >= HeightInBlocks )
throw new ArgumentOutOfRangeException( "yBlock" );
blocks[yBlock * WidthInBlocks + xBlock] = value;
}
}
private static readonly int blockWidth, blockHeight;
/// <summary>
/// Gets the width of a single block in pixels.
/// </summary>
public static int BlockWidth { get { return blockWidth; } }
/// <summary>
/// Gets the height of a single block in pixels.
/// </summary>
public static int BlockHeight { get { return blockHeight; } }
static BCnImage()
{
//handle other cases here as they arise
blockWidth = blockHeight = 4;
}
}
}