1 
2 //          Copyright 2018 - 2021 Michael D. Parker
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6 
7 module bindbc.sdl.bind.sdlpixels;
8 
9 import bindbc.sdl.config;
10 import bindbc.sdl.bind.sdlstdinc : SDL_FOURCC, SDL_bool;
11 
12 enum SDL_ALPHA_OPAQUE = 255;
13 enum SDL_ALPHA_TRANSPARENT = 0;
14 
15 enum SDL_PixelType {
16     SDL_PIXELTYPE_UNKNOWN,
17     SDL_PIXELTYPE_INDEX1,
18     SDL_PIXELTYPE_INDEX4,
19     SDL_PIXELTYPE_INDEX8,
20     SDL_PIXELTYPE_PACKED8,
21     SDL_PIXELTYPE_PACKED16,
22     SDL_PIXELTYPE_PACKED32,
23     SDL_PIXELTYPE_ARRAYU8,
24     SDL_PIXELTYPE_ARRAYU16,
25     SDL_PIXELTYPE_ARRAYU32,
26     SDL_PIXELTYPE_ARRAYF16,
27     SDL_PIXELTYPE_ARRAYF32
28 }
29 mixin(expandEnum!SDL_PixelType);
30 
31 enum SDL_BitmapOrder {
32     SDL_BITMAPORDER_NONE,
33     SDL_BITMAPORDER_4321,
34     SDL_BITMAPORDER_1234
35 }
36 mixin(expandEnum!SDL_BitmapOrder);
37 
38 enum SDL_PackedOrder {
39     SDL_PACKEDORDER_NONE,
40     SDL_PACKEDORDER_XRGB,
41     SDL_PACKEDORDER_RGBX,
42     SDL_PACKEDORDER_ARGB,
43     SDL_PACKEDORDER_RGBA,
44     SDL_PACKEDORDER_XBGR,
45     SDL_PACKEDORDER_BGRX,
46     SDL_PACKEDORDER_ABGR,
47     SDL_PACKEDORDER_BGRA
48 }
49 mixin(expandEnum!SDL_PackedOrder);
50 
51 enum SDL_ArrayOrder {
52     SDL_ARRAYORDER_NONE,
53     SDL_ARRAYORDER_RGB,
54     SDL_ARRAYORDER_RGBA,
55     SDL_ARRAYORDER_ARGB,
56     SDL_ARRAYORDER_BGR,
57     SDL_ARRAYORDER_BGRA,
58     SDL_ARRAYORDER_ABGR
59 }
60 mixin(expandEnum!SDL_ArrayOrder);
61 
62 enum SDL_PackedLayout {
63     SDL_PACKEDLAYOUT_NONE,
64     SDL_PACKEDLAYOUT_332,
65     SDL_PACKEDLAYOUT_4444,
66     SDL_PACKEDLAYOUT_1555,
67     SDL_PACKEDLAYOUT_5551,
68     SDL_PACKEDLAYOUT_565,
69     SDL_PACKEDLAYOUT_8888,
70     SDL_PACKEDLAYOUT_2101010,
71     SDL_PACKEDLAYOUT_1010102
72 }
73 mixin(expandEnum!SDL_PackedLayout);
74 
75 alias SDL_DEFINE_PIXELFOURCC = SDL_FOURCC;
76 
77 enum uint SDL_DEFINE_PIXELFORMAT(int type, int order, int layout, int bits, int bytes) =
78     (1 << 28) | (type << 24) | (order << 20) | (layout << 16) | (bits << 8) | (bytes << 0);
79 
80 enum uint SDL_PIXELFLAG(uint x) = (x >> 28) & 0x0F;
81 enum uint SDL_PIXELTYPE(uint x) = (x >> 24) & 0x0F;
82 enum uint SDL_PIXELORDER(uint x) = (x >> 20) & 0x0F;
83 enum uint SDL_PIXELLAYOUT(uint x) = (x >> 16) & 0x0F;
84 enum uint SDL_BITSPERPIXEL(uint x) = (x >> 8) & 0xFF;
85 
86 template SDL_BYTESPERPIXEL(uint x) {
87     static if(SDL_ISPIXELFORMAT_FOURCC!x) {
88         static if(x == SDL_PIXELFORMAT_YUY2 || x == SDL_PIXELFORMAT_UYVY || x == SDL_PIXELFORMAT_YVYU)
89             enum SDL_BYTESPERPIXEL = 2;
90         else enum SDL_BYTESPERPIXEL = 1;
91     }
92     else enum SDL_BYTESPERPIXEL = (x >> 0) & 0xFF;
93 }
94 
95 template SDL_ISPIXELFORMAT_INDEXED(uint format) {
96     static if(SDL_ISPIXELFORMAT_FOURCC!format) {
97         enum SDL_ISPIXELFORMAT_INDEXED = SDL_PIXELTYPE!format == SDL_PIXELTYPE_INDEX1 || SDL_PIXELTYPE!format == SDL_PIXELTYPE_INDEX4 ||
98             SDL_PIXELTYPE!format == SDL_PIXELTYPE_INDEX8;
99     }
100     else enum SDL_ISPIXELFORMAT_INDEXED = false;
101 }
102 
103 template SDL_ISPIXELFORMAT_PACKED(uint format) {
104     static if(SDL_ISPIXELFORMAT_FOURCC!format) {
105         enum SDL_ISPIXELFORMAT_PACKED = SDL_PIXELTYPE!format == SDL_PIXELTYPE_PACKED8 || SDL_PIXELTYPE!format == SDL_PIXELTYPE_PACKED16 ||
106             SDL_PIXELTYPE!format == SDL_PIXELTYPE_PACKED32;
107     }
108     else enum SDL_ISPIXELFORMAT_PACKED = false;
109 }
110 
111 static if(sdlSupport >= SDLSupport.sdl204) {
112     template SDL_ISPIXELFORMAT_ARRAY(uint format) {
113         static if(SDL_ISPIXELFORMAT_FOURCC!format) {
114             enum SDL_ISPIXELFORMAT_ARRAY = SDL_PIXELTYPE!format == SDL_PIXELTYPE_ARRAYU8 || SDL_PIXELTYPE!format == SDL_PIXELTYPE_ARRAYU16 ||
115                 SDL_PIXELTYPE!format == SDL_PIXELTYPE_ARRAYU32 || SDL_PIXELTYPE!format == SDL_PIXELTYPE_ARRAYF16 ||
116                 SDL_PIXELTYPE!format == SDL_PIXELTYPE_ARRAYF32;
117         }
118         else enum SDL_ISPIXELFORMAT_ARRAY = false;
119     }
120 }
121 
122 template SDL_ISPIXELFORMAT_ALPHA(uint format) {
123     static if(SDL_ISPIXELFORMAT_PACKED!format) {
124         enum SDL_ISPIXELFORMAT_ALPHA = (SDL_PIXELORDER!format == SDL_PACKEDORDER_ARGB || SDL_PIXELORDER!format == SDL_PACKEDORDER_RGBA ||
125                 SDL_PIXELORDER!format == SDL_PACKEDORDER_ABGR || SDL_PIXELORDER!format == SDL_PACKEDORDER_BGRA);
126     }
127     else static if(sdlSupport >= SDLSupport.sdl204 && SDL_ISPIXELFORMAT_ARRAY!format) {
128         enum SDL_ISPIXELFORMAT_ALPHA = (SDL_PIXELORDER!format == SDL_ARRAYORDER_ARGB || SDL_PIXELORDER!format == SDL_ARRAYORDER_RGBA ||
129                 SDL_PIXELORDER!format == SDL_ARRAYORDER_ABGR || SDL_PIXELORDER!format == SDL_ARRAYORDER_BGRA);
130     }
131     else enum SDL_ISPIXELFORMAT_ALPHA = false;
132 }
133 
134 enum SDL_ISPIXELFORMAT_FOURCC(uint format) = format && !(format & 0x80000000);
135 
136 enum SDL_PIXELFORMAT_UNKNOWN        = 0;
137 enum SDL_PIXELFORMAT_INDEX1LSB      = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_INDEX1, SDL_BITMAPORDER_4321, 0, 1, 0);
138 enum SDL_PIXELFORMAT_INDEX1MSB      = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_INDEX1, SDL_BITMAPORDER_1234, 0, 1, 0);
139 enum SDL_PIXELFORMAT_INDEX4LSB      = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_INDEX4, SDL_BITMAPORDER_4321, 0, 4, 0);
140 enum SDL_PIXELFORMAT_INDEX4MSB      = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_INDEX4, SDL_BITMAPORDER_1234, 0, 4, 0);
141 enum SDL_PIXELFORMAT_INDEX8         = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_INDEX8, 0, 0, 8, 1);
142 enum SDL_PIXELFORMAT_RGB332         = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED8, SDL_PACKEDORDER_XRGB, SDL_PACKEDLAYOUT_332, 8, 1);
143 enum SDL_PIXELFORMAT_XRGB444        = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_XRGB, SDL_PACKEDLAYOUT_4444, 12, 2);
144 enum SDL_PIXELFORMAT_RGB444         = SDL_PIXELFORMAT_XRGB444;
145 enum SDL_PIXELFORMAT_XBGR444        = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_XBGR, SDL_PACKEDLAYOUT_4444, 12, 2);
146 enum SDL_PIXELFORMAT_BGR444         = SDL_PIXELFORMAT_XBGR444;
147 enum SDL_PIXELFORMAT_XRGB1555       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_XRGB, SDL_PACKEDLAYOUT_1555, 15, 2);
148 enum SDL_PIXELFORMAT_RGB555         = SDL_PIXELFORMAT_XRGB1555;
149 enum SDL_PIXELFORMAT_XBGR1555       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_XBGR, SDL_PACKEDLAYOUT_1555, 15, 2);
150 enum SDL_PIXELFORMAT_BGR555         = SDL_PIXELFORMAT_XBGR1555;
151 enum SDL_PIXELFORMAT_ARGB4444       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_ARGB, SDL_PACKEDLAYOUT_4444, 16, 2);
152 enum SDL_PIXELFORMAT_RGBA4444       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_RGBA, SDL_PACKEDLAYOUT_4444, 16, 2);
153 enum SDL_PIXELFORMAT_ABGR4444       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_ABGR, SDL_PACKEDLAYOUT_4444, 16, 2);
154 enum SDL_PIXELFORMAT_BGRA4444       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_BGRA, SDL_PACKEDLAYOUT_4444, 16, 2);
155 enum SDL_PIXELFORMAT_ARGB1555       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_ARGB, SDL_PACKEDLAYOUT_1555, 16, 2);
156 enum SDL_PIXELFORMAT_RGBA5551       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_RGBA, SDL_PACKEDLAYOUT_5551, 16, 2);
157 enum SDL_PIXELFORMAT_ABGR1555       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_ABGR, SDL_PACKEDLAYOUT_1555, 16, 2);
158 enum SDL_PIXELFORMAT_BGRA5551       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_BGRA, SDL_PACKEDLAYOUT_5551, 16, 2);
159 enum SDL_PIXELFORMAT_RGB565         = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_XRGB, SDL_PACKEDLAYOUT_565, 16, 2);
160 enum SDL_PIXELFORMAT_BGR565         = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED16, SDL_PACKEDORDER_XBGR, SDL_PACKEDLAYOUT_565, 16, 2);
161 enum SDL_PIXELFORMAT_RGB24          = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_ARRAYU8, SDL_ARRAYORDER_RGB, 0, 24, 3);
162 enum SDL_PIXELFORMAT_BGR24          = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_ARRAYU8, SDL_ARRAYORDER_BGR, 0, 24, 3);
163 enum SDL_PIXELFORMAT_XRGB888        = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_XRGB, SDL_PACKEDLAYOUT_8888, 24, 4);
164 enum SDL_PIXELFORMAT_RGB888         = SDL_PIXELFORMAT_XRGB888;
165 enum SDL_PIXELFORMAT_RGBX8888       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_RGBX, SDL_PACKEDLAYOUT_8888, 24, 4);
166 enum SDL_PIXELFORMAT_XBGR888        = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_XBGR, SDL_PACKEDLAYOUT_8888, 24, 4);
167 enum SDL_PIXELFORMAT_BGR888         = SDL_PIXELFORMAT_XBGR888;
168 enum SDL_PIXELFORMAT_BGRX8888       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_BGRX, SDL_PACKEDLAYOUT_8888, 24, 4);
169 enum SDL_PIXELFORMAT_ARGB8888       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_ARGB, SDL_PACKEDLAYOUT_8888, 32, 4);
170 enum SDL_PIXELFORMAT_RGBA8888       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_RGBA, SDL_PACKEDLAYOUT_8888, 32, 4);
171 enum SDL_PIXELFORMAT_ABGR8888       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_ABGR, SDL_PACKEDLAYOUT_8888, 32, 4);
172 enum SDL_PIXELFORMAT_BGRA8888       = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_BGRA, SDL_PACKEDLAYOUT_8888, 32, 4);
173 enum SDL_PIXELFORMAT_ARGB2101010    = SDL_DEFINE_PIXELFORMAT!(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_ARGB, SDL_PACKEDLAYOUT_2101010, 32, 4);
174 
175 enum SDL_PIXELFORMAT_YV12           = SDL_DEFINE_PIXELFOURCC!('Y', 'V', '1', '2');
176 enum SDL_PIXELFORMAT_IYUV           = SDL_DEFINE_PIXELFOURCC!('I', 'Y', 'U', 'V');
177 enum SDL_PIXELFORMAT_YUY2           = SDL_DEFINE_PIXELFOURCC!('Y', 'U', 'Y', '2');
178 enum SDL_PIXELFORMAT_UYVY           = SDL_DEFINE_PIXELFOURCC!('U', 'Y', 'V', 'Y');
179 enum SDL_PIXELFORMAT_YVYU           = SDL_DEFINE_PIXELFOURCC!('Y', 'V', 'Y', 'U');
180 
181 static if(sdlSupport >= SDLSupport.sdl204) {
182     enum SDL_PIXELFORMAT_NV12       = SDL_DEFINE_PIXELFOURCC!('N', 'V', '1', '2');
183     enum SDL_PIXELFORMAT_NV21       = SDL_DEFINE_PIXELFOURCC!('N', 'V', '2', '1');
184 }
185 
186 static if(sdlSupport >= SDLSupport.sdl208) {
187     enum SDL_PIXELFORMAT_EXTERNAL_OES   = SDL_DEFINE_PIXELFOURCC!('O', 'E', 'S', ' ');
188 }
189 
190 static assert(SDL_PIXELFORMAT_BGRX8888 == 0x16661804);
191 
192 // Added in SDL 2.0.5, but doesn't hurt to make available for every version.
193 version(BigEndian) {
194     alias SDL_PIXELFORMAT_RGBA32 = SDL_PIXELFORMAT_RGBA8888;
195     alias SDL_PIXELFORMAT_ARGB32 = SDL_PIXELFORMAT_ARGB8888;
196     alias SDL_PIXELFORMAT_BGRA32 = SDL_PIXELFORMAT_BGRA8888;
197     alias SDL_PIXELFORMAT_ABGR32 = SDL_PIXELFORMAT_ABGR8888;
198 }
199 else {
200     alias SDL_PIXELFORMAT_RGBA32 = SDL_PIXELFORMAT_ABGR8888;
201     alias SDL_PIXELFORMAT_ARGB32 = SDL_PIXELFORMAT_BGRA8888;
202     alias SDL_PIXELFORMAT_BGRA32 = SDL_PIXELFORMAT_ARGB8888;
203     alias SDL_PIXELFORMAT_ABGR32 = SDL_PIXELFORMAT_RGBA8888;
204 }
205 
206 struct SDL_Color {
207     ubyte r;
208     ubyte g;
209     ubyte b;
210     ubyte a;
211 }
212 alias SDL_Colour = SDL_Color;
213 
214 struct SDL_Palette {
215     int ncolors;
216     SDL_Color* colors;
217     uint version_;    // NOTE: original was named 'version'
218     int refcount;
219 }
220 
221 struct SDL_PixelFormat {
222     uint format;
223     SDL_Palette *palette;
224     ubyte BitsPerPixel;
225     ubyte BytesPerPixel;
226     ubyte[2] padding;
227     uint Rmask;
228     uint Gmask;
229     uint Bmask;
230     uint Amask;
231     ubyte Rloss;
232     ubyte Gloss;
233     ubyte Bloss;
234     ubyte Aloss;
235     ubyte Rshift;
236     ubyte Gshift;
237     ubyte Bshift;
238     ubyte Ashift;
239     int refcount;
240     SDL_PixelFormat* next;
241 }
242 
243 static if(staticBinding) {
244   extern(C) @nogc nothrow {
245       const(char)* SDL_GetPixelFormatName(uint format);
246       SDL_bool SDL_PixelFormatEnumToMasks(uint format, int* bpp, uint* Rmask, uint* Gmask, uint* Bmask, uint* Amask);
247       uint SDL_MasksToPixelFormatEnum(int bpp, uint Rmask, uint Gmask, uint Bmask, uint Amask);
248       SDL_PixelFormat* SDL_AllocFormat(uint pixel_format);
249       void SDL_FreeFormat(SDL_PixelFormat* format);
250       SDL_Palette* SDL_AllocPalette(int ncolors);
251       int SDL_SetPixelFormatPalette(SDL_PixelFormat* format,SDL_Palette* palette);
252       int SDL_SetPaletteColors(SDL_Palette* palette, const(SDL_Color)* colors, int firstcolor, int ncolors);
253       void SDL_FreePalette(SDL_Palette* palette);
254       uint SDL_MapRGB(const(SDL_PixelFormat)* format, ubyte r, ubyte g, ubyte b);
255       uint SDL_MapRGBA(const(SDL_PixelFormat)* format, ubyte r, ubyte g, ubyte b, ubyte a);
256       void SDL_GetRGB(uint pixel, const(SDL_PixelFormat)* format, ubyte* r, ubyte* g, ubyte* b);
257       void SDL_GetRGBA(uint pixel, const(SDL_PixelFormat)* format, ubyte* r, ubyte* g, ubyte* b, ubyte* a);
258       void SDL_CalculateGammaRamp(float gamma, ushort* ramp);
259   }
260 }
261 else {
262   extern(C) @nogc nothrow {
263       alias pSDL_GetPixelFormatName = const(char)* function(uint pixel_format);
264       alias pSDL_PixelFormatEnumToMasks = SDL_bool function(uint format, int* bpp, uint* Rmask, uint* Gmask, uint* Bmask, uint* Amask);
265       alias pSDL_MasksToPixelFormatEnum = uint function(int bpp, uint Rmask, uint Gmask, uint Bmask, uint Amask);
266       alias pSDL_AllocFormat = SDL_PixelFormat* function(uint format);
267       alias pSDL_FreeFormat = void function(SDL_PixelFormat* format);
268       alias pSDL_AllocPalette = SDL_Palette* function(int ncolors);
269       alias pSDL_SetPixelFormatPalette = int function(SDL_PixelFormat* format,SDL_Palette* palette);
270       alias pSDL_SetPaletteColors = int function(SDL_Palette* palette, const(SDL_Color)* colors, int firstcolor, int ncolors);
271       alias pSDL_FreePalette = void function(SDL_Palette* palette);
272       alias pSDL_MapRGB = uint function(const(SDL_PixelFormat)* format, ubyte r, ubyte g, ubyte b);
273       alias pSDL_MapRGBA = uint function(const(SDL_PixelFormat)* format, ubyte r, ubyte g, ubyte b, ubyte a);
274       alias pSDL_GetRGB = void function(uint pixel, const(SDL_PixelFormat)* format, ubyte* r, ubyte* g, ubyte* b);
275       alias pSDL_GetRGBA = void function(uint pixel, const(SDL_PixelFormat)* format, ubyte* r, ubyte* g, ubyte* b, ubyte* a);
276       alias pSDL_CalculateGammaRamp = void function(float gamma, ushort* ramp);
277   }
278 
279   __gshared {
280       pSDL_GetPixelFormatName SDL_GetPixelFormatName;
281       pSDL_PixelFormatEnumToMasks SDL_PixelFormatEnumToMasks;
282       pSDL_MasksToPixelFormatEnum SDL_MasksToPixelFormatEnum;
283       pSDL_AllocFormat SDL_AllocFormat;
284       pSDL_FreeFormat SDL_FreeFormat;
285       pSDL_AllocPalette SDL_AllocPalette;
286       pSDL_SetPixelFormatPalette SDL_SetPixelFormatPalette;
287       pSDL_SetPaletteColors SDL_SetPaletteColors;
288       pSDL_FreePalette SDL_FreePalette;
289       pSDL_MapRGB SDL_MapRGB;
290       pSDL_MapRGBA SDL_MapRGBA;
291       pSDL_GetRGB SDL_GetRGB;
292       pSDL_GetRGBA SDL_GetRGBA;
293       pSDL_CalculateGammaRamp SDL_CalculateGammaRamp;
294   }
295 }