| | 1 | | using System; |
| | 2 | | using static SDL2.SDL_error; |
| | 3 | | using static SDL2.SDL_pixels; |
| | 4 | | using static Imagini.ErrorHandler; |
| | 5 | | using System.Runtime.InteropServices; |
| | 6 | |
|
| | 7 | | namespace Imagini.Drawing |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Contains pixel format information. |
| | 11 | | /// </summary> |
| | 12 | | public sealed class PixelFormatInfo : Resource, IDisposable |
| | 13 | | { |
| | 14 | | internal IntPtr Handle; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Gets the pixel format. |
| | 18 | | /// </summary> |
| 286 | 19 | | public PixelFormat Format { get; private set; } |
| | 20 | | /// <summary> |
| | 21 | | /// Returns bits per pixel. |
| | 22 | | /// </summary> |
| 118 | 23 | | public int BitsPerPixel { get; private set; } |
| | 24 | | /// <summary> |
| | 25 | | /// Returns bytes per pixel. |
| | 26 | | /// </summary> |
| 94 | 27 | | public int BytesPerPixel { get; private set; } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// A bit mask representing the location of the red component of the pixel. |
| | 31 | | /// </summary> |
| 116 | 32 | | public int MaskR { get; private set; } |
| | 33 | | /// <summary> |
| | 34 | | /// A bit mask representing the location of the green component of the pixel. |
| | 35 | | /// </summary> |
| 116 | 36 | | public int MaskG { get; private set; } |
| | 37 | | /// <summary> |
| | 38 | | /// A bit mask representing the location of the blue component of the pixel. |
| | 39 | | /// </summary> |
| 116 | 40 | | public int MaskB { get; private set; } |
| | 41 | | /// <summary> |
| | 42 | | /// A bit mask representing the location of the alpha component of the |
| | 43 | | /// pixel or 0 if the pixel format doesn't have any alpha information. |
| | 44 | | /// </summary> |
| 116 | 45 | | public int MaskA { get; private set; } |
| | 46 | |
|
| | 47 | | private Palette _palette; |
| | 48 | | /// <summary> |
| | 49 | | /// Gets or sets the palette used by this pixel format. Returns null |
| | 50 | | /// if the palette is not present. |
| | 51 | | /// </summary> |
| | 52 | | public Palette Palette |
| | 53 | | { |
| | 54 | | get |
| | 55 | | { |
| 6 | 56 | | return _palette; |
| | 57 | | } |
| | 58 | | set |
| | 59 | | { |
| 1 | 60 | | if (!Format.IsIndexed()) |
| 0 | 61 | | throw new ImaginiException("Only indexed formats can have palettes"); |
| 0 | 62 | | Try(() => |
| 1 | 63 | | SDL_SetPixelFormatPalette(Handle, value.Handle), |
| 0 | 64 | | "SDL_SetPixelFormatPalette"); |
| 1 | 65 | | if (_palette != null) |
| 0 | 66 | | Unregister(_palette); |
| 1 | 67 | | _palette = value; |
| 1 | 68 | | Register(_palette); |
| 1 | 69 | | } |
| | 70 | | } |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Creates a new PixelFormatInfo with the specified pixel format. |
| | 74 | | /// </summary> |
| 36 | 75 | | public PixelFormatInfo(PixelFormat format) |
| | 76 | | { |
| 36 | 77 | | Handle = SDL_AllocFormat((uint)format); |
| 36 | 78 | | if (Handle == IntPtr.Zero) |
| 0 | 79 | | throw new ImaginiException($"Could not create a pixel format: {SDL_GetError()}"); |
| 36 | 80 | | FromSDL(Marshal.PtrToStructure<SDL_PixelFormat>(Handle)); |
| 36 | 81 | | } |
| | 82 | |
|
| 42 | 83 | | internal PixelFormatInfo(IntPtr handle) |
| | 84 | | { |
| 42 | 85 | | Handle = handle; |
| 42 | 86 | | FromSDL(Marshal.PtrToStructure<SDL_PixelFormat>(Handle)); |
| 42 | 87 | | } |
| | 88 | |
|
| | 89 | | private void FromSDL(SDL_PixelFormat fmt) |
| | 90 | | { |
| 78 | 91 | | if (fmt.palette != IntPtr.Zero) |
| 1 | 92 | | _palette = new Palette(fmt.palette); |
| 78 | 93 | | Format = (PixelFormat)fmt.format; |
| 78 | 94 | | BitsPerPixel = Format.GetBitsPerPixel(); |
| 78 | 95 | | BytesPerPixel = Format.GetBytesPerPixel(); |
| | 96 | | unchecked |
| | 97 | | { |
| 78 | 98 | | MaskR = (int)fmt.Rmask; |
| 78 | 99 | | MaskG = (int)fmt.Gmask; |
| 78 | 100 | | MaskB = (int)fmt.Bmask; |
| 78 | 101 | | MaskA = (int)fmt.Amask; |
| | 102 | | } |
| 78 | 103 | | } |
| | 104 | |
|
| | 105 | | internal override void Destroy() |
| | 106 | | { |
| 35 | 107 | | if (IsDisposed) return; |
| 35 | 108 | | base.Destroy(); |
| 35 | 109 | | SDL_FreeFormat(Handle); |
| 35 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Disposes the object. |
| | 114 | | /// </summary> |
| 35 | 115 | | public void Dispose() => Destroy(); |
| | 116 | |
|
| 1 | 117 | | static PixelFormatInfo() => Lifecycle.TryInitialize(); |
| | 118 | | } |
| | 119 | | } |