| | 1 | | using System.Runtime.InteropServices; |
| | 2 | | using static SDL2.SDL_surface; |
| | 3 | | using static Imagini.ErrorHandler; |
| | 4 | | using System; |
| | 5 | |
|
| | 6 | | namespace Imagini.Drawing |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Contains pixel-related utility functions. |
| | 10 | | /// </summary> |
| | 11 | | public static class Pixels |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Copies a block of pixels of one format to another format. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="width">The width of the block to copy, in pixels</param> |
| | 17 | | /// <param name="height">The height of the block to copy, in pixels</param> |
| | 18 | | /// <param name="srcStride">The stride (pitch) of the block to copy</param> |
| | 19 | | /// <param name="dstStride">The stride (pitch) of the destination pixels</param> |
| | 20 | | /// <param name="src">Source pixel data</param> |
| | 21 | | /// <param name="dst">Destination pixel data</param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public static void Convert<T1, T2>(int width, int height, int srcStride, int dstStride, |
| | 24 | | ref T1[] src, ref T2[] dst) |
| | 25 | | where T1 : IColor |
| | 26 | | where T2 : IColor |
| | 27 | | { |
| 1 | 28 | | var srcFormat = src[0].Format; |
| 1 | 29 | | var dstFormat = dst[0].Format; |
| 1 | 30 | | var srcHandle = GCHandle.Alloc(src, GCHandleType.Pinned); |
| 1 | 31 | | var dstHandle = GCHandle.Alloc(dst, GCHandleType.Pinned); |
| | 32 | | try |
| | 33 | | { |
| 0 | 34 | | Convert(width, height, srcStride, dstStride, |
| 0 | 35 | | srcFormat, dstFormat, |
| 0 | 36 | | srcHandle.AddrOfPinnedObject(), dstHandle.AddrOfPinnedObject()); |
| 1 | 37 | | } |
| | 38 | | finally |
| | 39 | | { |
| 1 | 40 | | srcHandle.Free(); |
| 1 | 41 | | dstHandle.Free(); |
| 1 | 42 | | } |
| 1 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Copies a block of pixels of one format to another format. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="width">The width of the block to copy, in pixels</param> |
| | 49 | | /// <param name="height">The height of the block to copy, in pixels</param> |
| | 50 | | /// <param name="srcStride">The stride (pitch) of the block to copy</param> |
| | 51 | | /// <param name="dstStride">The stride (pitch) of the destination pixels</param> |
| | 52 | | /// <param name="srcFormat">Source pixel format</param> |
| | 53 | | /// <param name="dstFormat">Destination pixel format</param> |
| | 54 | | /// <param name="src">Source pixel data</param> |
| | 55 | | /// <param name="dst">Destination pixel data</param> |
| | 56 | | public static void Convert(int width, int height, int srcStride, int dstStride, |
| | 57 | | PixelFormat srcFormat, PixelFormat dstFormat, IntPtr src, IntPtr dst) |
| | 58 | | { |
| 0 | 59 | | Try(() => SDL_ConvertPixels(width, height, |
| 0 | 60 | | (uint)srcFormat, src, srcStride, |
| 0 | 61 | | (uint)dstFormat, dst, dstStride), |
| 0 | 62 | | "SDL_ConvertPixels"); |
| 19 | 63 | | } |
| | 64 | | } |
| | 65 | | } |