/
image.d
181 lines (144 loc) · 5.53 KB
/
image.d
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
module stdc.graphic.image;
import stdccolor = stdc.graphic.color;
import std.traits : isIntegral;
import std.math : floor;
signature ImageBase {
alias Color;
alias IndexType=size_t;
static assert(is(Color : stdccolor.Color), "An image Color must match the stdc.graphic.color Color definition.");
static assert(isIntegral!IndexType, "An image IndexType must be an integer");
@property @nogc nothrow @safe {
IndexType width();
IndexType height() { return 1; }
}
}
signature SharedImageBase {
alias Color;
alias IndexType;
static assert(is(Color : stdccolor.Color), "An image Color must match the stdc.graphic.color Color definition.");
static assert(isIntegral!IndexType, "An image IndexType must be an integer");
@property @nogc nothrow @safe shared {
IndexType width();
IndexType height() { return 1; }
}
}
signature UniformImage : ImageBase {
Color opIndex(IndexType i) @nogc @safe
default(IndexedImage) {
return IndexedImage(this)[cast(IndexType)(i % width), cast(IndexType)floor(i / width)];
}
void opIndexAssign(Color v, IndexType i) @nogc @safe
default(IndexedImage) {
IndexedImage(this)[cast(IndexType)(i % width), cast(IndexType)floor(i / width)] = v;
}
void resize(IndexType newLength) @safe
default(IndexedImage) {
IndexedImage(this).resize(cast(IndexType)(newLength % width), cast(IndexType)floor(newLength / width));
}
}
signature SharedUniformImage : SharedImageBase {
Color opIndex(IndexType i) @nogc @safe
default(SharedIndexedImage) {
return SharedIndexedImage(this)[cast(IndexType)(i % width), cast(IndexType)floor(i / width)];
}
void opIndexAssign(Color v, IndexType i) @nogc @safe
default(SharedIndexedImage) {
SharedIndexedImage(this)[cast(IndexType)(i % width), cast(IndexType)floor(i / width)] = v;
}
void resize(IndexType newLength) @safe
default(SharedIndexedImage) {
SharedIndexedImage(this).resize(cast(IndexType)(newLength % width), cast(IndexType)floor(newLength / width));
}
}
signature IndexedImage : ImageBase {
Color opIndex(IndexType x, IndexType y) @nogc @safe
default(UniformImage) {
return UniformImage(this)[y*width+x];
}
void opIndexAssign(Color v, IndexType x, IndexType y) @nogc @safe
default(UniformImage) {
UniformImage(this)[y*width+x] = v;
}
void resize(IndexType newWidth, IndexType newHeight) @safe
default(UniformImage) {
UniformImage(this).resize(newHeight*width+newWidth);
}
}
signature SharedIndexedImage : SharedImageBase {
Color opIndex(IndexType x, IndexType y) @nogc @safe
default(SharedUniformImage) {
return SharedUniformImage(this)[y*width+x];
}
void opIndexAssign(Color v, IndexType x, IndexType y) @nogc @safe
default(SharedUniformImage) {
SharedUniformImage(this)[y*width+x] = v;
}
void resize(IndexType newWidth, IndexType newHeight) @safe
default(SharedUniformImage) {
SharedUniformImage(this).resize(newHeight*width+newWidth);
}
}
signature Image : UniformImage, IndexedImage {}
signature SharedImage : SharedUniformImage, SharedIndexedImage {}
struct SwapableImage(NewColor, IImage:Image) {
alias Color = NewColor;
static assert(is(NewColor : stdccolor.Color), "A SwapableImage Color must match the stdc.graphic.color Color definition.");
IImage source;
@property @nogc nothrow @safe {
IndexType width() { return source.width; }
IndexType height() { return source.height; }
}
static if (is(IImage : IndexedImage)) {
Color opIndex(IndexType x, IndexType y) @nogc @safe {
return cast(Color)source[x, y];
}
void opIndexAssign(Color v, IndexType x, IndexType y) @nogc @safe {
source[x, y] = cast(IImage.Color)stdccolor.Color(v);
}
void resize(IndexType newWidth, IndexType newHeight) @safe {
source.resize(newWidth, newHeight);
}
}
static if (is(IImage : UniformImage)) {
Color opIndex(IndexType i) @nogc @safe {
return cast(Color)source[i];
}
void opIndexAssign(Color v, IndexType i) @nogc @safe {
source[i] = cast(IImage.Color)stdccolor.Color(v);
}
void resize(IndexType newLength) @safe {
source.resize(newLength);
}
}
}
struct SharedSwapableImage(NewColor, IImage:SharedImage) {
alias Color = NewColor;
static assert(is(NewColor : stdccolor.Color), "A SwapableImage Color must match the stdc.graphic.color Color definition.");
shared(IImage) source;
@property @nogc nothrow @safe shared {
IndexType width() { return source.width; }
IndexType height() { return source.height; }
}
static if (is(IImage : SharedIndexedImage)) {
Color opIndex(IndexType x, IndexType y) @nogc @safe shared {
return cast(Color)source[x, y];
}
void opIndexAssign(Color v, IndexType x, IndexType y) @nogc @safe shared {
source[x, y] = cast(IImage.Color)stdccolor.Color(v);
}
void resize(IndexType newWidth, IndexType newHeight) @safe shared {
source.resize(newWidth, newHeight);
}
}
static if (is(IImage : SharedUniformImage)) {
Color opIndex(IndexType i) @nogc @safe shared {
return cast(Color)source[i];
}
void opIndexAssign(Color v, IndexType i) @nogc @safe shared {
source[i] = cast(IImage.Color)stdccolor.Color(v);
}
void resize(IndexType newLength) @safe shared {
source.resize(newLength);
}
}
}