forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRageSurface.cpp
277 lines (236 loc) · 6.59 KB
/
RageSurface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "global.h"
#include "RageSurface.h"
#include "RageUtil.h"
#include <limits>
int32_t RageSurfacePalette::FindColor( const RageSurfaceColor &color ) const
{
for( int i = 0; i < ncolors; ++i )
if( colors[i] == color )
return i;
return -1;
}
/* XXX: untested */
int32_t RageSurfacePalette::FindClosestColor( const RageSurfaceColor &color ) const
{
int iBest = -1;
int iBestDist = std::numeric_limits<int>::max();
for( int i = 0; i < ncolors; ++i )
{
if( colors[i] == color )
return i;
int iDist = abs( colors[i].r - color.r ) +
abs( colors[i].g - color.g ) +
abs( colors[i].b - color.b ) +
abs( colors[i].a - color.a );
if( iDist < iBestDist )
{
iBestDist = iDist ;
iBest = i;
}
}
return iBest;
}
RageSurfaceFormat::RageSurfaceFormat():
Rmask(Mask[0]), Gmask(Mask[1]), Bmask(Mask[2]), Amask(Mask[3]),
Rshift(Shift[0]), Gshift(Shift[1]), Bshift(Shift[2]), Ashift(Shift[3])
{
palette = nullptr;
}
RageSurfaceFormat::RageSurfaceFormat( const RageSurfaceFormat &cpy ):
Rmask(Mask[0]), Gmask(Mask[1]), Bmask(Mask[2]), Amask(Mask[3]),
Rshift(Shift[0]), Gshift(Shift[1]), Bshift(Shift[2]), Ashift(Shift[3])
{
memcpy( this, &cpy, sizeof(RageSurfaceFormat) );
if( palette )
palette = new RageSurfacePalette( *palette );
}
RageSurfaceFormat::~RageSurfaceFormat()
{
delete palette;
}
void RageSurfaceFormat::GetRGB( uint32_t val, uint8_t *r, uint8_t *g, uint8_t *b ) const
{
if( BytesPerPixel == 1 )
{
ASSERT( palette != nullptr );
*r = palette->colors[val].r;
*g = palette->colors[val].g;
*b = palette->colors[val].b;
} else {
*r = int8_t( (val & Mask[0]) >> Shift[0] << Loss[0] );
*g = int8_t( (val & Mask[1]) >> Shift[1] << Loss[1] );
*b = int8_t( (val & Mask[2]) >> Shift[2] << Loss[2] );
}
}
bool RageSurfaceFormat::MapRGBA( uint8_t r, uint8_t g, uint8_t b, uint8_t a, uint32_t &val ) const
{
if( BytesPerPixel == 1 )
{
RageSurfaceColor c( r, g, b, a );
int32_t n = palette->FindColor( c );
if( n == -1 )
return false;
val = (uint32_t) n;
} else {
val =
(r >> Loss[0] << Shift[0]) |
(g >> Loss[1] << Shift[1]) |
(b >> Loss[2] << Shift[2]) |
(a >> Loss[3] << Shift[3]);
}
return true;
}
bool RageSurfaceFormat::operator== ( const RageSurfaceFormat &rhs ) const
{
if( !Equivalent(rhs) )
return false;
if( BytesPerPixel == 1 )
if( memcmp( palette, rhs.palette, sizeof(RageSurfaceFormat) ) )
return false;
return true;
}
bool RageSurfaceFormat::Equivalent( const RageSurfaceFormat &rhs ) const
{
#define COMP(a) if( a != rhs.a ) return false;
COMP( BytesPerPixel );
COMP( Rmask );
COMP( Gmask );
COMP( Bmask );
COMP( Amask );
return true;
}
RageSurface::RageSurface()
{
format = &fmt;
pixels = nullptr;
pixels_owned = true;
}
RageSurface::RageSurface( const RageSurface &cpy )
{
format = &fmt;
w = cpy.w;
h = cpy.h;
pitch = cpy.pitch;
flags = cpy.flags;
pixels_owned = true;
if( cpy.pixels )
{
pixels = new uint8_t[ pitch*h ];
memcpy( pixels, cpy.pixels, pitch*h );
}
else
pixels = nullptr;
}
RageSurface::~RageSurface()
{
if( pixels_owned )
delete [] pixels;
}
static int GetShiftFromMask( uint32_t mask )
{
if( !mask )
return 0;
int iShift = 0;
while( (mask & 1) == 0 )
{
mask >>= 1;
++iShift;
}
return iShift;
}
static int GetBitsFromMask( uint32_t mask )
{
if( !mask )
return 0;
mask >>= GetShiftFromMask(mask);
int iBits = 0;
while( (mask & 1) == 1 )
{
mask >>= 1;
++iBits;
}
return iBits;
}
void SetupFormat( RageSurfaceFormat &fmt,
int width, int height, int BitsPerPixel, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask )
{
fmt.BitsPerPixel = BitsPerPixel;
fmt.BytesPerPixel = BitsPerPixel/8;
if( fmt.BytesPerPixel == 1 )
{
ZERO( fmt.Mask );
ZERO( fmt.Shift );
// Loss for paletted textures is zero; the actual palette entries are 8-bit.
ZERO( fmt.Loss );
fmt.palette = new RageSurfacePalette;
fmt.palette->ncolors = 256;
}
else
{
fmt.Mask[0] = Rmask;
fmt.Mask[1] = Gmask;
fmt.Mask[2] = Bmask;
fmt.Mask[3] = Amask;
fmt.Shift[0] = GetShiftFromMask( Rmask );
fmt.Shift[1] = GetShiftFromMask( Gmask );
fmt.Shift[2] = GetShiftFromMask( Bmask );
fmt.Shift[3] = GetShiftFromMask( Amask );
fmt.Loss[0] = (uint8_t) (8-GetBitsFromMask( Rmask ));
fmt.Loss[1] = (uint8_t) (8-GetBitsFromMask( Gmask ));
fmt.Loss[2] = (uint8_t) (8-GetBitsFromMask( Bmask ));
fmt.Loss[3] = (uint8_t) (8-GetBitsFromMask( Amask ));
}
}
RageSurface *CreateSurface( int width, int height, int BitsPerPixel, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask )
{
RageSurface *pImg = new RageSurface;
SetupFormat( pImg->fmt, width, height, BitsPerPixel, Rmask, Gmask, Bmask, Amask );
pImg->w = width;
pImg->h = height;
pImg->flags = 0;
pImg->pitch = width*BitsPerPixel/8;
pImg->pixels = new uint8_t[ pImg->pitch*height ];
/*
if( BitsPerPixel == 8 )
{
pImg->fmt.palette = new RageSurfacePalette;
}
*/
return pImg;
}
RageSurface *CreateSurfaceFrom( int width, int height, int BitsPerPixel, uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask, uint8_t *pPixels, uint32_t pitch )
{
RageSurface *pImg = new RageSurface;
SetupFormat( pImg->fmt, width, height, BitsPerPixel, Rmask, Gmask, Bmask, Amask );
pImg->w = width;
pImg->h = height;
pImg->flags = 0;
pImg->pitch = pitch;
pImg->pixels = pPixels;
pImg->pixels_owned = false;
return pImg;
}
/*
* (c) 2001-2004 Glenn Maynard, Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/