forked from keendreams/keen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
id_vw_conv.c
389 lines (328 loc) · 10.7 KB
/
id_vw_conv.c
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* Keen Dreams (SDL2/Steam Port) Source Code
* Copyright (C) 2012 David Gow <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// ID_VW_CONV: This contains code (that I originally wrote for Omnispeak) for
// converting EGA planar data to VGA data or RGBA.
#include "id_heads.h"
#define max(a,b) (((a) < (b))?(b):(a))
#define min(a,b) (((a) < (b))?(a):(b))
const uint8_t VW_EGAPalette[16][3] = {
0x00, 0x00, 0x00, // Black
0x00, 0x00, 0xaa, // Blue
0x00, 0xaa, 0x00, // Green
0x00, 0xaa, 0xaa, // Cyan
0xaa, 0x00, 0x00, // Red
0xaa, 0x00, 0xaa, // Magenta
0xaa, 0x55, 0x00, // Brown
0xaa, 0xaa, 0xaa, // Light Grey
0x55, 0x55, 0x55, // Dark Grey
0x55, 0x55, 0xff, // Light Blue
0x55, 0xff, 0x55, // Light Green
0x55, 0xff, 0xff, // Light Cyan
0xff, 0x55, 0x55, // Light Red
0xff, 0x55, 0xff, // Light Magenta
0xff, 0xff, 0x55, // Yellow
0xff, 0xff, 0xff, // White
};
uint8_t VW_CurrentRGBPalette[16][3] = {0};
void VW_UnmaskedToPAL8(void *src,void *dest, int x, int y, int pitch, int w, int h)
{
uint8_t *dstptr = (uint8_t*)dest;
uint8_t *srcptr_b = (uint8_t*)src;
uint8_t *srcptr_g = srcptr_b + (w/8)*h;
uint8_t *srcptr_r = srcptr_g + (w/8)*h;
uint8_t *srcptr_i = srcptr_r + (w/8)*h;
for(int sy = 0; sy < h; ++sy)
{
for(int sx = 0; sx < w; ++sx)
{
int plane_off = (sy * w + sx) >> 3;
int plane_bit = 1<<(7-((sy * w + sx) & 7));
int pixel = ((srcptr_i[plane_off] & plane_bit)?8:0) |
((srcptr_r[plane_off] & plane_bit)?4:0) |
((srcptr_g[plane_off] & plane_bit)?2:0) |
((srcptr_b[plane_off] & plane_bit)?1:0);
dstptr[(sy+y)*pitch+(sx+x)] = pixel;
}
}
}
void VW_UnmaskedSubRectToPAL8(void *src,void *dest, int x, int y, int pitch, int w, int h, int planesize, int planepitch)
{
uint8_t *dstptr = (uint8_t*)dest;
uint8_t *srcptr_b = (uint8_t*)src;
uint8_t *srcptr_g = srcptr_b + planesize;
uint8_t *srcptr_r = srcptr_g + planesize;
uint8_t *srcptr_i = srcptr_r + planesize;
for(int sy = 0; sy < h; ++sy)
{
for(int sx = 0; sx < w; ++sx)
{
int plane_off = (sy * planepitch*8 + sx) >> 3;
int plane_bit = 1<<(7-((sy * planepitch*8 + sx) & 7));
int pixel = ((srcptr_i[plane_off] & plane_bit)?8:0) |
((srcptr_r[plane_off] & plane_bit)?4:0) |
((srcptr_g[plane_off] & plane_bit)?2:0) |
((srcptr_b[plane_off] & plane_bit)?1:0);
dstptr[(sy+y)*pitch+(sx+x)] = pixel;
}
}
}
// This is used for the mouse cursor, if we're doing HW cursors.
void VW_PAL8ScaleToRGBA(void *src,void *dest, int xscale, int yscale, int pitch, int w, int h)
{
uint8_t *dstptr = (uint8_t*)dest;
uint8_t *srcptr = (uint8_t*)src;
for(int sy = 0; sy < h; ++sy)
{
for(int sx = 0; sx < w; ++sx)
{
int pixel = (srcptr[sy*w+sx]);
// ARGB LE output
for(int dy = 0; dy < yscale; ++dy)
{
for(int dx = 0; dx < xscale; ++dx)
{
size_t pixcoord = (sy*yscale+dy)*pitch+(sx*xscale+dx)*4;
dstptr[pixcoord+0] = (pixel == 255)?0:VW_CurrentRGBPalette[pixel][2];
dstptr[pixcoord+1] = (pixel == 255)?0:VW_CurrentRGBPalette[pixel][1];
dstptr[pixcoord+2] = (pixel == 255)?0:VW_CurrentRGBPalette[pixel][0];
dstptr[pixcoord+3] = (pixel == 255)?0x00:0xFF;
}
}
}
}
}
void VW_MaskedScaleToRGBA(void *src,void *dest, int xscale, int yscale, int pitch, int w, int h)
{
uint8_t *dstptr = (uint8_t*)dest;
uint8_t *srcptr_a = (uint8_t*)src;
uint8_t *srcptr_b = srcptr_a + (w/8)*h;
uint8_t *srcptr_g = srcptr_b + (w/8)*h;
uint8_t *srcptr_r = srcptr_g + (w/8)*h;
uint8_t *srcptr_i = srcptr_r + (w/8)*h;
for(int sy = 0; sy < h; ++sy)
{
for(int sx = 0; sx < w; ++sx)
{
int plane_off = (sy * w + sx) >> 3;
int plane_bit = 1<<(7-((sy * w + sx) & 7));
int pixel = ((srcptr_i[plane_off] & plane_bit)?8:0) |
((srcptr_r[plane_off] & plane_bit)?4:0) |
((srcptr_g[plane_off] & plane_bit)?2:0) |
((srcptr_b[plane_off] & plane_bit)?1:0);
// ARGB LE output
for(int dy = 0; dy < yscale; ++dy)
{
for(int dx = 0; dx < xscale; ++dx)
{
size_t pixcoord = (sy*yscale+dy)*pitch+(sx*xscale+dx)*4;
dstptr[pixcoord+0] = VW_CurrentRGBPalette[pixel][2];
dstptr[pixcoord+1] = VW_CurrentRGBPalette[pixel][1];
dstptr[pixcoord+2] = VW_CurrentRGBPalette[pixel][0];
dstptr[pixcoord+3] = ((srcptr_a[plane_off] & plane_bit)?0x00:0xFF);
}
}
}
}
}
void VW_MaskedToPAL8(void *src,void *dest, int x, int y, int pitch, int w, int h)
{
uint8_t *dstptr = (uint8_t*)dest;
uint8_t *srcptr_a = (uint8_t*)src;
uint8_t *srcptr_b = srcptr_a + (w/8)*h;
uint8_t *srcptr_g = srcptr_b + (w/8)*h;
uint8_t *srcptr_r = srcptr_g + (w/8)*h;
uint8_t *srcptr_i = srcptr_r + (w/8)*h;
for(int sy = 0; sy < h; ++sy)
{
for(int sx = 0; sx < w; ++sx)
{
int plane_off = (sy * w + sx) >> 3;
int plane_bit = 1<<(7-((sy * w + sx) & 7));
int pixel = ((srcptr_i[plane_off] & plane_bit)?8:0) |
((srcptr_r[plane_off] & plane_bit)?4:0) |
((srcptr_g[plane_off] & plane_bit)?2:0) |
((srcptr_b[plane_off] & plane_bit)?1:0);
if(!(srcptr_a[plane_off] & plane_bit))
dstptr[(sy+y)*pitch+(sx+x)] = pixel;
else
dstptr[(sy+y)*pitch+(sx+x)] = 255;
}
}
}
void VW_MaskedBlitToPAL8(void *src,void *dest, int x, int y, int pitch, int w, int h)
{
uint8_t *dstptr = (uint8_t*)dest;
uint8_t *srcptr_a = (uint8_t*)src;
uint8_t *srcptr_b = srcptr_a + (w/8)*h;
uint8_t *srcptr_g = srcptr_b + (w/8)*h;
uint8_t *srcptr_r = srcptr_g + (w/8)*h;
uint8_t *srcptr_i = srcptr_r + (w/8)*h;
for(int sy = 0; sy < h; ++sy)
{
for(int sx = 0; sx < w; ++sx)
{
int plane_off = (sy * w + sx) >> 3;
int plane_bit = 1<<(7-((sy * w + sx) & 7));
if ((srcptr_a[plane_off] & plane_bit) != 0) {
continue;
}
int pixel = ((srcptr_i[plane_off] & plane_bit)?8:0) |
((srcptr_r[plane_off] & plane_bit)?4:0) |
((srcptr_g[plane_off] & plane_bit)?2:0) |
((srcptr_b[plane_off] & plane_bit)?1:0);
dstptr[(sy+y)*pitch+(sx+x)] = pixel;
}
}
}
void VW_MaskedBlitWPlaneSizeToPAL8(void *src,void *dest, int x, int y, int pitch, int w, int h, int planesize)
{
uint8_t *dstptr = (uint8_t*)dest;
uint8_t *srcptr_a = (uint8_t*)src;
uint8_t *srcptr_b = srcptr_a + planesize;
uint8_t *srcptr_g = srcptr_b + planesize;
uint8_t *srcptr_r = srcptr_g + planesize;
uint8_t *srcptr_i = srcptr_r + planesize;
for(int sy = 0; sy < h; ++sy)
{
for(int sx = 0; sx < w; ++sx)
{
int plane_off = (sy * w + sx) >> 3;
int plane_bit = 1<<(7-((sy * w + sx) & 7));
if ((srcptr_a[plane_off] & plane_bit) != 0) {
continue;
}
int pixel = ((srcptr_i[plane_off] & plane_bit)?8:0) |
((srcptr_r[plane_off] & plane_bit)?4:0) |
((srcptr_g[plane_off] & plane_bit)?2:0) |
((srcptr_b[plane_off] & plane_bit)?1:0);
dstptr[(sy+y)*pitch+(sx+x)] = pixel;
}
}
}
void VW_MaskedBlitClipToPAL8(void *src,void *dest, int x, int y, int pitch, int w, int h, int dw, int dh)
{
uint8_t *dstptr = (uint8_t*)dest;
uint8_t *srcptr_a = (uint8_t*)src;
uint8_t *srcptr_b = srcptr_a + (w/8)*h;
uint8_t *srcptr_g = srcptr_b + (w/8)*h;
uint8_t *srcptr_r = srcptr_g + (w/8)*h;
uint8_t *srcptr_i = srcptr_r + (w/8)*h;
int initialX = max(-x,0);
int initialY = max(-y,0);
int finalW = min(max(dw-x,0), w);
int finalH = min(max(dh-y,0), h);
for(int sy = initialY; sy < finalH; ++sy)
{
for(int sx = initialX; sx < finalW; ++sx)
{
int plane_off = (sy * w + sx) >> 3;
int plane_bit = 1<<(7-((sy * w + sx) & 7));
if ((srcptr_a[plane_off] & plane_bit) != 0) {
continue;
}
int pixel = ((srcptr_i[plane_off] & plane_bit)?8:0) |
((srcptr_r[plane_off] & plane_bit)?4:0) |
((srcptr_g[plane_off] & plane_bit)?2:0) |
((srcptr_b[plane_off] & plane_bit)?1:0);
dstptr[(sy+y)*pitch+(sx+x)] = pixel;
}
}
}
void VW_1bppToPAL8(void *src,void *dest, int x, int y, int pitch, int w, int h, int colour)
{
uint8_t *dstptr = (uint8_t*)dest;
uint8_t *srcptr= (uint8_t*)src;
for(int sy = 0; sy < h; ++sy)
{
for(int sx = 0; sx < w; ++sx)
{
int plane_off = (sy * w + sx) >> 3;
int plane_bit = 1<<(7-((sy * w + sx) & 7));
int pixel = ((srcptr[plane_off] & plane_bit)?colour:colour&0xF0);
dstptr[(sy+y)*pitch+(sx+x)] = pixel;
}
}
}
void VW_1bppXorWithPAL8(void *src,void *dest, int x, int y, int pitch, int w, int h, int colour)
{
uint8_t *dstptr = (uint8_t*)dest;
uint8_t *srcptr= (uint8_t*)src;
int spitch = ((w + 7)/8)*8;
for(int sy = 0; sy < h; ++sy)
{
for(int sx = 0; sx < w; ++sx)
{
int plane_off = (sy * spitch + sx) >> 3;
int plane_bit = 1<<(7-((sy * spitch + sx) & 7));
if (!(srcptr[plane_off] & plane_bit)) continue;
dstptr[(sy+y)*pitch+(sx+x)] ^= colour;
}
}
}
void VW_1bppBlitToPAL8(void *src,void *dest, int x, int y, int pitch, int w, int h, int colour)
{
uint8_t *dstptr = (uint8_t*)dest;
uint8_t *srcptr= (uint8_t*)src;
int spitch = ((w + 7)/8)*8;
for(int sy = 0; sy < h; ++sy)
{
for(int sx = 0; sx < w; ++sx)
{
int plane_off = (sy * spitch + sx) >> 3;
int plane_bit = 1<<(7-((sy * spitch + sx) & 7));
if (!(srcptr[plane_off] & plane_bit)) continue;
dstptr[(sy+y)*pitch+(sx+x)] = colour;
}
}
}
void VW_1bppInvBlitToPAL8(void *src,void *dest, int x, int y, int pitch, int w, int h, int colour)
{
uint8_t *dstptr = (uint8_t*)dest;
uint8_t *srcptr= (uint8_t*)src;
int spitch = ((w + 7)/8)*8;
for(int sy = 0; sy < h; ++sy)
{
for(int sx = 0; sx < w; ++sx)
{
int plane_off = (sy * spitch + sx) >> 3;
int plane_bit = 1<<(7-((sy * spitch + sx) & 7));
if ((srcptr[plane_off] & plane_bit)) continue;
dstptr[(sy+y)*pitch+(sx+x)] = colour;
}
}
}
void VW_1bppInvBlitClipToPAL8(void *src,void *dest, int x, int y, int pitch, int w, int h, int dw, int dh, int colour)
{
uint8_t *dstptr = (uint8_t*)dest;
uint8_t *srcptr = (uint8_t*)src;
int initialX = max(-x,0);
int initialY = max(-y,0);
int finalW = min(max(dw-x,0), w);
int finalH = min(max(dh-y,0), h);
int spitch = ((w + 7)/8)*8;
for(int sy = initialY; sy < finalH; ++sy)
{
for(int sx = initialX; sx < finalW; ++sx)
{
int plane_off = (sy * spitch + sx) >> 3;
int plane_bit = 1<<(7-((sy * spitch + sx) & 7));
if ((srcptr[plane_off] & plane_bit)) continue;
dstptr[(sy+y)*pitch+(sx+x)] = colour;
}
}
}