Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/octoshock/video/surface.h
2 views
1
#ifndef __MDFN_SURFACE_H
2
#define __MDFN_SURFACE_H
3
4
struct MDFN_Rect
5
{
6
int32 x, y, w, h;
7
};
8
9
enum
10
{
11
MDFN_COLORSPACE_RGB = 0,
12
MDFN_COLORSPACE_YCbCr = 1,
13
//MDFN_COLORSPACE_YUV = 2, // TODO, maybe.
14
};
15
16
struct MDFN_PaletteEntry
17
{
18
uint8 r, g, b;
19
};
20
21
class MDFN_PixelFormat
22
{
23
public:
24
25
MDFN_PixelFormat();
26
MDFN_PixelFormat(const unsigned int p_colorspace, const uint8 p_rs, const uint8 p_gs, const uint8 p_bs, const uint8 p_as);
27
28
unsigned int bpp; // 32 only for now(16 and 8 wip)
29
unsigned int colorspace;
30
31
union
32
{
33
uint8 Rshift; // Bit position of the lowest bit of the red component
34
uint8 Yshift;
35
};
36
37
union
38
{
39
uint8 Gshift; // [...] green component
40
uint8 Ushift;
41
uint8 Cbshift;
42
};
43
44
union
45
{
46
uint8 Bshift; // [...] blue component
47
uint8 Vshift;
48
uint8 Crshift;
49
};
50
51
uint8 Ashift; // [...] alpha component.
52
53
// For 16bpp, WIP
54
uint8 Rprec;
55
uint8 Gprec;
56
uint8 Bprec;
57
uint8 Aprec;
58
59
// Creates a color value for the surface corresponding to the 8-bit R/G/B/A color passed.
60
INLINE uint32 MakeColor(uint8 r, uint8 g, uint8 b, uint8 a = 0) const
61
{
62
if(colorspace == MDFN_COLORSPACE_YCbCr)
63
{
64
uint32 y, u, v;
65
66
y = 16 + ((r * 16842 + g * 33030 + b * 6422) >> 16);
67
u = 128 + ((r * -9699 + g * -19071 + b * 28770) >> 16);
68
v = 128 + ((r * 28770 + g * -24117 + b * -4653) >> 16);
69
70
return((y << Yshift) | (u << Ushift) | (v << Vshift) | (a << Ashift));
71
}
72
else
73
{
74
if(bpp == 16)
75
{
76
uint32 ret = 0;
77
/*
78
ret |= std::min(((r * ((1 << Rprec) - 1) + 127) / 255), 255) << Rshift;
79
ret |= std::min(((g * ((1 << Gprec) - 1) + 127) / 255), 255) << Gshift;
80
ret |= std::min(((b * ((1 << Bprec) - 1) + 127) / 255), 255) << Bshift;
81
ret |= std::min(((a * ((1 << Aprec) - 1) + 127) / 255), 255) << Ashift;
82
*/
83
ret |= ((r * ((1 << Rprec) - 1) + 127) / 255) << Rshift;
84
ret |= ((g * ((1 << Gprec) - 1) + 127) / 255) << Gshift;
85
ret |= ((b * ((1 << Bprec) - 1) + 127) / 255) << Bshift;
86
ret |= ((a * ((1 << Aprec) - 1) + 127) / 255) << Ashift;
87
return(ret);
88
}
89
else
90
return((r << Rshift) | (g << Gshift) | (b << Bshift) | (a << Ashift));
91
}
92
}
93
94
INLINE MDFN_PaletteEntry MakePColor(uint8 r, uint8 g, uint8 b) const
95
{
96
MDFN_PaletteEntry ret;
97
98
ret.r = ((r * ((1 << Rprec) - 1) + 127) / 255) << Rshift;
99
ret.g = ((g * ((1 << Gprec) - 1) + 127) / 255) << Gshift;
100
ret.b = ((b * ((1 << Bprec) - 1) + 127) / 255) << Bshift;
101
102
return ret;
103
}
104
105
INLINE void DecodePColor(const MDFN_PaletteEntry& pe, uint8 &r, uint8 &g, uint8 &b) const
106
{
107
r = ((pe.r >> Rshift) & ((1 << Rprec) - 1)) * 255 / ((1 << Rprec) - 1);
108
g = ((pe.g >> Gshift) & ((1 << Gprec) - 1)) * 255 / ((1 << Gprec) - 1);
109
b = ((pe.b >> Bshift) & ((1 << Bprec) - 1)) * 255 / ((1 << Bprec) - 1);
110
}
111
112
// Gets the R/G/B/A values for the passed 32-bit surface pixel value
113
INLINE void DecodeColor(uint32 value, int &r, int &g, int &b, int &a) const
114
{
115
if(colorspace == MDFN_COLORSPACE_YCbCr)
116
{
117
int32 y = (value >> Yshift) & 0xFF;
118
int32 cb = (value >> Cbshift) & 0xFF;
119
int32 cr = (value >> Crshift) & 0xFF;
120
121
int32 r_tmp, g_tmp, b_tmp;
122
123
r_tmp = g_tmp = b_tmp = 76284 * (y - 16);
124
125
r_tmp = r_tmp + 104595 * (cr - 128);
126
g_tmp = g_tmp - 53281 * (cr - 128) - 25690 * (cb - 128);
127
b_tmp = b_tmp + 132186 * (cb - 128);
128
129
r_tmp >>= 16;
130
g_tmp >>= 16;
131
b_tmp >>= 16;
132
133
if(r_tmp < 0) r_tmp = 0;
134
if(r_tmp > 255) r_tmp = 255;
135
136
if(g_tmp < 0) g_tmp = 0;
137
if(g_tmp > 255) g_tmp = 255;
138
139
if(b_tmp < 0) b_tmp = 0;
140
if(b_tmp > 255) b_tmp = 255;
141
142
r = r_tmp;
143
g = g_tmp;
144
b = b_tmp;
145
146
a = (value >> Ashift) & 0xFF;
147
}
148
else
149
{
150
if(bpp == 16)
151
{
152
r = ((value >> Rshift) & ((1 << Rprec) - 1)) * 255 / ((1 << Rprec) - 1);
153
g = ((value >> Gshift) & ((1 << Gprec) - 1)) * 255 / ((1 << Gprec) - 1);
154
b = ((value >> Bshift) & ((1 << Bprec) - 1)) * 255 / ((1 << Bprec) - 1);
155
a = ((value >> Ashift) & ((1 << Aprec) - 1)) * 255 / ((1 << Aprec) - 1);
156
}
157
else
158
{
159
r = (value >> Rshift) & 0xFF;
160
g = (value >> Gshift) & 0xFF;
161
b = (value >> Bshift) & 0xFF;
162
a = (value >> Ashift) & 0xFF;
163
}
164
}
165
}
166
167
INLINE void DecodeColor(uint32 value, int &r, int &g, int &b) const
168
{
169
int dummy_a;
170
171
DecodeColor(value, r, g, b, dummy_a);
172
}
173
}; // MDFN_PixelFormat;
174
175
// Supports 32-bit RGBA
176
// 16-bit is WIP
177
// 8-bit is even WIPier.
178
class MDFN_Surface //typedef struct
179
{
180
public:
181
182
MDFN_Surface();
183
MDFN_Surface(void *const p_pixels, const uint32 p_width, const uint32 p_height, const uint32 p_pitchinpix, const MDFN_PixelFormat &nf, const bool alloc_init_pixels = true);
184
185
~MDFN_Surface();
186
187
uint8 *pixels8;
188
uint16 *pixels16;
189
uint32 *pixels;
190
191
template<typename T>
192
T* pix(void)
193
{
194
if(sizeof(T) == 1)
195
return (T*)pixels8;
196
else if(sizeof(T) == 2)
197
return (T*)pixels16;
198
else if(sizeof(T) == 4)
199
return (T*)pixels;
200
else
201
return NULL;
202
}
203
204
MDFN_PaletteEntry *palette;
205
206
bool pixels_is_external;
207
208
// w, h, and pitch32 should always be > 0
209
int32 w;
210
int32 h;
211
212
union
213
{
214
int32 pitch32; // In pixels, not in bytes.
215
int32 pitchinpix; // New name, new code should use this.
216
};
217
218
MDFN_PixelFormat format;
219
220
void Fill(uint8 r, uint8 g, uint8 b, uint8 a);
221
void SetFormat(const MDFN_PixelFormat &new_format, bool convert);
222
223
// Creates a 32-bit value for the surface corresponding to the R/G/B/A color passed.
224
INLINE uint32 MakeColor(uint8 r, uint8 g, uint8 b, uint8 a = 0) const
225
{
226
return(format.MakeColor(r, g, b, a));
227
}
228
229
// Gets the R/G/B/A values for the passed 32-bit surface pixel value
230
INLINE void DecodeColor(uint32 value, int &r, int &g, int &b, int &a) const
231
{
232
format.DecodeColor(value, r, g, b, a);
233
}
234
235
INLINE void DecodeColor(uint32 value, int &r, int &g, int &b) const
236
{
237
int dummy_a;
238
239
DecodeColor(value, r, g, b, dummy_a);
240
}
241
private:
242
void Init(void *const p_pixels, const uint32 p_width, const uint32 p_height, const uint32 p_pitchinpix, const MDFN_PixelFormat &nf, const bool alloc_init_pixels);
243
};
244
245
#endif
246
247