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