-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathImage.cs
More file actions
422 lines (377 loc) · 17.9 KB
/
Image.cs
File metadata and controls
422 lines (377 loc) · 17.9 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using SFML.System;
namespace SFML.Graphics
{
////////////////////////////////////////////////////////////
/// <summary>
/// Image is the low-level class for loading and
/// manipulating images
/// </summary>
////////////////////////////////////////////////////////////
public class Image : ObjectBase
{
////////////////////////////////////////////////////////////
/// <summary>
/// Construct the image with black color
/// </summary>
/// <param name="width">Image width</param>
/// <param name="height">Image height</param>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
public Image(uint width, uint height) : this(width, height, Color.Black) { }
////////////////////////////////////////////////////////////
/// <summary>
/// Construct the image from a single color
/// </summary>
/// <param name="width">Image width</param>
/// <param name="height">Image height</param>
/// <param name="color">Color to fill the image with</param>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
public Image(uint width, uint height, Color color) : base(sfImage_createFromColor(width, height, color))
{
if (CPointer == IntPtr.Zero)
{
throw new LoadingFailedException("image");
}
}
////////////////////////////////////////////////////////////
/// <summary>
/// Construct the image from a file
/// </summary>
/// <param name="filename">Path of the image file to load</param>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
public Image(string filename) : base(sfImage_createFromFile(filename))
{
if (CPointer == IntPtr.Zero)
{
throw new LoadingFailedException("image", filename);
}
}
////////////////////////////////////////////////////////////
/// <summary>
/// Construct the image from a file in a stream
/// </summary>
/// <param name="stream">Stream containing the file contents</param>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
public Image(Stream stream) :
base(IntPtr.Zero)
{
using (var adaptor = new SFML.System.StreamAdaptor(stream))
{
CPointer = sfImage_createFromStream(adaptor.InputStreamPtr);
}
if (CPointer == IntPtr.Zero)
{
throw new LoadingFailedException("image");
}
}
////////////////////////////////////////////////////////////
/// <summary>
/// Construct the image from a file in memory
/// </summary>
/// <param name="bytes">Byte array containing the file contents</param>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
public Image(byte[] bytes) :
base(IntPtr.Zero)
{
GCHandle pin = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try
{
CPointer = sfImage_createFromMemory(pin.AddrOfPinnedObject(), Convert.ToUInt64(bytes.Length));
}
finally
{
pin.Free();
}
if (CPointer == IntPtr.Zero)
{
throw new LoadingFailedException("image");
}
}
////////////////////////////////////////////////////////////
/// <summary>
/// Construct the image directly from an array of pixels
/// </summary>
/// <param name="pixels">2 dimensions array containing the pixels</param>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
public Image(Color[,] pixels) :
base(IntPtr.Zero)
{
uint Width = (uint)pixels.GetLength(0);
uint Height = (uint)pixels.GetLength(1);
// Transpose the array (.Net gives dimensions in reverse order of what SFML expects)
Color[,] transposed = new Color[Height, Width];
for (int x = 0; x < Width; ++x)
{
for (int y = 0; y < Height; ++y)
{
transposed[y, x] = pixels[x, y];
}
}
unsafe
{
fixed (Color* PixelsPtr = transposed)
{
CPointer = sfImage_createFromPixels(Width, Height, (byte*)PixelsPtr);
}
}
if (CPointer == IntPtr.Zero)
{
throw new LoadingFailedException("image");
}
}
////////////////////////////////////////////////////////////
/// <summary>
/// Construct the image directly from an array of pixels
/// </summary>
/// <param name="width">Image width</param>
/// <param name="height">Image height</param>
/// <param name="pixels">array containing the pixels</param>
/// <exception cref="LoadingFailedException" />
////////////////////////////////////////////////////////////
public Image(uint width, uint height, byte[] pixels) :
base(IntPtr.Zero)
{
unsafe
{
fixed (byte* PixelsPtr = pixels)
{
CPointer = sfImage_createFromPixels(width, height, PixelsPtr);
}
}
if (CPointer == IntPtr.Zero)
{
throw new LoadingFailedException("image");
}
}
////////////////////////////////////////////////////////////
/// <summary>
/// Construct the image from another image
/// </summary>
/// <param name="copy">Image to copy</param>
////////////////////////////////////////////////////////////
public Image(Image copy) :
base(sfImage_copy(copy.CPointer))
{
}
////////////////////////////////////////////////////////////
/// <summary>
/// Save the contents of the image to a file
/// </summary>
/// <param name="filename">Path of the file to save (overwritten if already exist)</param>
/// <returns>True if saving was successful</returns>
////////////////////////////////////////////////////////////
public bool SaveToFile(string filename) => sfImage_saveToFile(CPointer, filename);
////////////////////////////////////////////////////////////
/// <summary>
/// Create a transparency mask from a specified colorkey
/// </summary>
/// <param name="color">Color to become transparent</param>
////////////////////////////////////////////////////////////
public void CreateMaskFromColor(Color color)
{
CreateMaskFromColor(color, 0);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Create a transparency mask from a specified colorkey
/// </summary>
/// <param name="color">Color to become transparent</param>
/// <param name="alpha">Alpha value to use for transparent pixels</param>
////////////////////////////////////////////////////////////
public void CreateMaskFromColor(Color color, byte alpha)
{
sfImage_createMaskFromColor(CPointer, color, alpha);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Copy pixels from another image onto this one.
/// This function does a slow pixel copy and should only
/// be used at initialization time
/// </summary>
/// <param name="source">Source image to copy</param>
/// <param name="destX">X coordinate of the destination position</param>
/// <param name="destY">Y coordinate of the destination position</param>
////////////////////////////////////////////////////////////
public void Copy(Image source, uint destX, uint destY)
{
Copy(source, destX, destY, new IntRect(0, 0, 0, 0));
}
////////////////////////////////////////////////////////////
/// <summary>
/// Copy pixels from another image onto this one.
/// This function does a slow pixel copy and should only
/// be used at initialization time
/// </summary>
/// <param name="source">Source image to copy</param>
/// <param name="destX">X coordinate of the destination position</param>
/// <param name="destY">Y coordinate of the destination position</param>
/// <param name="sourceRect">Sub-rectangle of the source image to copy</param>
////////////////////////////////////////////////////////////
public void Copy(Image source, uint destX, uint destY, IntRect sourceRect)
{
Copy(source, destX, destY, sourceRect, false);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Copy pixels from another image onto this one.
/// This function does a slow pixel copy and should only
/// be used at initialization time
/// </summary>
/// <param name="source">Source image to copy</param>
/// <param name="destX">X coordinate of the destination position</param>
/// <param name="destY">Y coordinate of the destination position</param>
/// <param name="sourceRect">Sub-rectangle of the source image to copy</param>
/// <param name="applyAlpha">Should the copy take in account the source transparency?</param>
////////////////////////////////////////////////////////////
public void Copy(Image source, uint destX, uint destY, IntRect sourceRect, bool applyAlpha)
{
sfImage_copyImage(CPointer, source.CPointer, destX, destY, sourceRect, applyAlpha);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Get a pixel from the image
/// </summary>
/// <param name="x">X coordinate of pixel in the image</param>
/// <param name="y">Y coordinate of pixel in the image</param>
/// <returns>Color of pixel (x, y)</returns>
////////////////////////////////////////////////////////////
public Color GetPixel(uint x, uint y) => sfImage_getPixel(CPointer, x, y);
////////////////////////////////////////////////////////////
/// <summary>
/// Change the color of a pixel
/// </summary>
/// <param name="x">X coordinate of pixel in the image</param>
/// <param name="y">Y coordinate of pixel in the image</param>
/// <param name="color">New color for pixel (x, y)</param>
////////////////////////////////////////////////////////////
public void SetPixel(uint x, uint y, Color color)
{
sfImage_setPixel(CPointer, x, y, color);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Get a copy of the array of pixels (RGBA 8 bits integers components)
/// Array size is Width x Height x 4
/// </summary>
/// <returns>Array of pixels</returns>
////////////////////////////////////////////////////////////
public byte[] Pixels
{
get
{
Vector2u size = Size;
byte[] PixelsPtr = new byte[size.X * size.Y * 4];
Marshal.Copy(sfImage_getPixelsPtr(CPointer), PixelsPtr, 0, PixelsPtr.Length);
return PixelsPtr;
}
}
////////////////////////////////////////////////////////////
/// <summary>
/// Copy the array of pixels (RGBA 8 bits integers
/// components) to the provided buffer.
/// Buffer size must be at least Width x Height x 4
/// </summary>
/// <param name="buffer">Buffer to copy to</param>
////////////////////////////////////////////////////////////
public void CopyPixels(byte[] buffer)
{
Vector2u size = Size;
if (buffer.Length < size.X * size.Y * 4)
{
throw new ArgumentException("Buffer must have a size of at least Width * Height * 4 bytes", nameof(buffer));
}
Marshal.Copy(sfImage_getPixelsPtr(CPointer), buffer, 0, (int)(size.X * size.Y * 4));
}
////////////////////////////////////////////////////////////
/// <summary>
/// Size of the image, in pixels
/// </summary>
////////////////////////////////////////////////////////////
public Vector2u Size => sfImage_getSize(CPointer);
////////////////////////////////////////////////////////////
/// <summary>
/// Flip the image horizontally
/// </summary>
////////////////////////////////////////////////////////////
public void FlipHorizontally()
{
sfImage_flipHorizontally(CPointer);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Flip the image vertically
/// </summary>
////////////////////////////////////////////////////////////
public void FlipVertically()
{
sfImage_flipVertically(CPointer);
}
////////////////////////////////////////////////////////////
/// <summary>
/// Provide a string describing the object
/// </summary>
/// <returns>String description of the object</returns>
////////////////////////////////////////////////////////////
public override string ToString() => $"[Image] Size({Size})";
////////////////////////////////////////////////////////////
/// <summary>
/// Internal constructor
/// </summary>
/// <param name="cPointer">Pointer to the object in C library</param>
////////////////////////////////////////////////////////////
internal Image(IntPtr cPointer) : base(cPointer) { }
////////////////////////////////////////////////////////////
/// <summary>
/// Handle the destruction of the object
/// </summary>
/// <param name="disposing">Is the GC disposing the object, or is it an explicit call ?</param>
////////////////////////////////////////////////////////////
protected override void Destroy(bool disposing)
{
sfImage_destroy(CPointer);
}
#region Imports
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfImage_createFromColor(uint Width, uint Height, Color Col);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
unsafe static extern IntPtr sfImage_createFromPixels(uint Width, uint Height, byte* Pixels);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfImage_createFromFile(string Filename);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
unsafe static extern IntPtr sfImage_createFromStream(IntPtr stream);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
unsafe static extern IntPtr sfImage_createFromMemory(IntPtr data, ulong size);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfImage_copy(IntPtr Image);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern void sfImage_destroy(IntPtr CPointer);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern bool sfImage_saveToFile(IntPtr CPointer, string Filename);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern void sfImage_createMaskFromColor(IntPtr CPointer, Color Col, byte Alpha);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern void sfImage_copyImage(IntPtr CPointer, IntPtr Source, uint DestX, uint DestY, IntRect SourceRect, bool applyAlpha);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern void sfImage_setPixel(IntPtr CPointer, uint X, uint Y, Color Col);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern Color sfImage_getPixel(IntPtr CPointer, uint X, uint Y);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfImage_getPixelsPtr(IntPtr CPointer);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern Vector2u sfImage_getSize(IntPtr CPointer);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern uint sfImage_flipHorizontally(IntPtr CPointer);
[DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern uint sfImage_flipVertically(IntPtr CPointer);
#endregion
}
}