Debian Codegraph @

src/video_scale.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2010 The OpenTyrian Development Team
4 *
5 * Scale2x, Scale3x
6 * Copyright (C) 2001, 2002, 2003, 2004 Andrea Mazzoleni
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22 #include "video_scale.h"
23
24 #include "palette.h"
25 #include "video.h"
26
27 #include <assert.h>
28 #include <string.h>
29
30 static void nn_32(SDL_Surface *src_surface, SDL_Texture *dst_texture);
31 static void nn_16(SDL_Surface *src_surface, SDL_Texture *dst_texture);
32
33 static void scale2x_32(SDL_Surface *src_surface, SDL_Texture *dst_texture);
34 static void scale2x_16(SDL_Surface *src_surface, SDL_Texture *dst_texture);
35 static void scale3x_32(SDL_Surface *src_surface, SDL_Texture *dst_texture);
36 static void scale3x_16(SDL_Surface *src_surface, SDL_Texture *dst_texture);
37
38 void hq2x_32(SDL_Surface *src_surface, SDL_Texture *dst_texture);
39 void hq3x_32(SDL_Surface *src_surface, SDL_Texture *dst_texture);
40 void hq4x_32(SDL_Surface *src_surface, SDL_Texture *dst_texture);
41
42 uint scaler;
43
44 const struct Scalers scalers[] =
45 {
46 { 1 * vga_width, 1 * vga_height, nn_16, nn_32, "None" },
47 { 2 * vga_width, 2 * vga_height, nn_16, nn_32, "2x" },
48 { 2 * vga_width, 2 * vga_height, scale2x_16, scale2x_32, "Scale2x" },
49 { 2 * vga_width, 2 * vga_height, NULL, hq2x_32, "hq2x" },
50 { 3 * vga_width, 3 * vga_height, nn_16, nn_32, "3x" },
51 { 3 * vga_width, 3 * vga_height, scale3x_16, scale3x_32, "Scale3x" },
52 { 3 * vga_width, 3 * vga_height, NULL, hq3x_32, "hq3x" },
53 { 4 * vga_width, 4 * vga_height, nn_16, nn_32, "4x" },
54 { 4 * vga_width, 4 * vga_height, NULL, hq4x_32, "hq4x" },
55 };
56 const uint scalers_count = COUNTOF(scalers);
57
58 void set_scaler_by_name(const char *name)
59 {
60 for (uint i = 0; i < scalers_count; ++i)
61 {
62 if (strcmp(name, scalers[i].name) == 0)
63 {
64 scaler = i;
65 break;
66 }
67 }
68 }
69
70 void nn_32(SDL_Surface *src_surface, SDL_Texture *dst_texture)
71 {
72 Uint8 *src = src_surface->pixels, *src_temp;
73 Uint8 *dst, *dst_temp;
74
75 int src_pitch = src_surface->pitch;
76 int dst_pitch;
77
78 const int dst_Bpp = 4; // dst_surface->format->BytesPerPixel
79 int dst_width, dst_height;
80 SDL_QueryTexture(dst_texture, NULL, NULL, &dst_width, &dst_height);
81
82 const int height = vga_height, // src_surface->h
83 width = vga_width, // src_surface->w
84 scale = dst_width / width;
85 assert(scale == dst_height / height);
86
87 void* tmp_ptr;
88 SDL_LockTexture(dst_texture, NULL, &tmp_ptr, &dst_pitch);
89 dst = tmp_ptr;
90
91 for (int y = height; y > 0; y--)
92 {
93 src_temp = src;
94 dst_temp = dst;
95
96 for (int x = width; x > 0; x--)
97 {
98 for (int z = scale; z > 0; z--)
99 {
100 *(Uint32 *)dst = rgb_palette[*src];
101 dst += dst_Bpp;
102 }
103 src++;
104 }
105
106 src = src_temp + src_pitch;
107 dst = dst_temp + dst_pitch;
108
109 for (int z = scale; z > 1; z--)
110 {
111 memcpy(dst, dst_temp, dst_width * dst_Bpp);
112 dst += dst_pitch;
113 }
114 }
115
116 SDL_UnlockTexture(dst_texture);
117 }
118
119 void nn_16(SDL_Surface *src_surface, SDL_Texture *dst_texture)
120 {
121 Uint8 *src = src_surface->pixels, *src_temp;
122 Uint8 *dst, *dst_temp;
123
124 int src_pitch = src_surface->pitch;
125 int dst_pitch;
126
127 const int dst_Bpp = 2; // dst_surface->format->BytesPerPixel
128 int dst_width, dst_height;
129 SDL_QueryTexture(dst_texture, NULL, NULL, &dst_width, &dst_height);
130
131 const int height = vga_height, // src_surface->h
132 width = vga_width, // src_surface->w
133 scale = dst_width / width;
134 assert(scale == dst_height / height);
135
136 void* tmp_ptr;
137 SDL_LockTexture(dst_texture, NULL, &tmp_ptr, &dst_pitch);
138 dst = tmp_ptr;
139
140 for (int y = height; y > 0; y--)
141 {
142 src_temp = src;
143 dst_temp = dst;
144
145 for (int x = width; x > 0; x--)
146 {
147 for (int z = scale; z > 0; z--)
148 {
149 *(Uint16 *)dst = rgb_palette[*src];
150 dst += dst_Bpp;
151 }
152 src++;
153 }
154
155 src = src_temp + src_pitch;
156 dst = dst_temp + dst_pitch;
157
158 for (int z = scale; z > 1; z--)
159 {
160 memcpy(dst, dst_temp, dst_width * dst_Bpp);
161 dst += dst_pitch;
162 }
163 }
164
165 SDL_UnlockTexture(dst_texture);
166 }
167
168 void scale2x_32(SDL_Surface *src_surface, SDL_Texture *dst_texture)
169 {
170 Uint8 *src = src_surface->pixels, *src_temp;
171 Uint8 *dst, *dst_temp;
172
173 int src_pitch = src_surface->pitch;
174 int dst_pitch;
175
176 const int dst_Bpp = 4, // dst_surface->format->BytesPerPixel
177 height = vga_height, // src_surface->h
178 width = vga_width; // src_surface->w
179
180 void* tmp_ptr;
181 SDL_LockTexture(dst_texture, NULL, &tmp_ptr, &dst_pitch);
182 dst = tmp_ptr;
183
184 int prevline, nextline;
185
186 Uint32 E0, E1, E2, E3, B, D, E, F, H;
187 for (int y = 0; y < height; y++)
188 {
189 src_temp = src;
190 dst_temp = dst;
191
192 prevline = (y > 0) ? -src_pitch : 0;
193 nextline = (y < height - 1) ? src_pitch : 0;
194
195 for (int x = 0; x < width; x++)
196 {
197 B = rgb_palette[*(src + prevline)];
198 D = rgb_palette[*(x > 0 ? src - 1 : src)];
199 E = rgb_palette[*src];
200 F = rgb_palette[*(x < width - 1 ? src + 1 : src)];
201 H = rgb_palette[*(src + nextline)];
202
203 if (B != H && D != F)
204 {
205 E0 = D == B ? D : E;
206 E1 = B == F ? F : E;
207 E2 = D == H ? D : E;
208 E3 = H == F ? F : E;
209 }
210 else
211 {
212 E0 = E1 = E2 = E3 = E;
213 }
214
215 *(Uint32 *)dst = E0;
216 *(Uint32 *)(dst + dst_Bpp) = E1;
217 *(Uint32 *)(dst + dst_pitch) = E2;
218 *(Uint32 *)(dst + dst_pitch + dst_Bpp) = E3;
219
220 src++;
221 dst += 2 * dst_Bpp;
222 }
223
224 src = src_temp + src_pitch;
225 dst = dst_temp + 2 * dst_pitch;
226 }
227
228 SDL_UnlockTexture(dst_texture);
229 }
230
231 void scale2x_16(SDL_Surface *src_surface, SDL_Texture *dst_texture)
232 {
233 Uint8 *src = src_surface->pixels, *src_temp;
234 Uint8 *dst, *dst_temp;
235
236 int src_pitch = src_surface->pitch;
237 int dst_pitch;
238
239 const int dst_Bpp = 2, // dst_surface->format->BytesPerPixel
240 height = vga_height, // src_surface->h
241 width = vga_width; // src_surface->w
242
243 void* tmp_ptr;
244 SDL_LockTexture(dst_texture, NULL, &tmp_ptr, &dst_pitch);
245 dst = tmp_ptr;
246
247 int prevline, nextline;
248
249 Uint16 E0, E1, E2, E3, B, D, E, F, H;
250 for (int y = 0; y < height; y++)
251 {
252 src_temp = src;
253 dst_temp = dst;
254
255 prevline = (y > 0) ? -src_pitch : 0;
256 nextline = (y < height - 1) ? src_pitch : 0;
257
258 for (int x = 0; x < width; x++)
259 {
260 B = rgb_palette[*(src + prevline)];
261 D = rgb_palette[*(x > 0 ? src - 1 : src)];
262 E = rgb_palette[*src];
263 F = rgb_palette[*(x < width - 1 ? src + 1 : src)];
264 H = rgb_palette[*(src + nextline)];
265
266 if (B != H && D != F)
267 {
268 E0 = D == B ? D : E;
269 E1 = B == F ? F : E;
270 E2 = D == H ? D : E;
271 E3 = H == F ? F : E;
272 }
273 else
274 {
275 E0 = E1 = E2 = E3 = E;
276 }
277
278 *(Uint16 *)dst = E0;
279 *(Uint16 *)(dst + dst_Bpp) = E1;
280 *(Uint16 *)(dst + dst_pitch) = E2;
281 *(Uint16 *)(dst + dst_pitch + dst_Bpp) = E3;
282
283 src++;
284 dst += 2 * dst_Bpp;
285 }
286
287 src = src_temp + src_pitch;
288 dst = dst_temp + 2 * dst_pitch;
289 }
290
291 SDL_UnlockTexture(dst_texture);
292 }
293
294 void scale3x_32(SDL_Surface *src_surface, SDL_Texture *dst_texture)
295 {
296 Uint8 *src = src_surface->pixels, *src_temp;
297 Uint8 *dst, *dst_temp;
298
299 int src_pitch = src_surface->pitch;
300 int dst_pitch;
301
302 const int dst_Bpp = 4, // dst_surface->format->BytesPerPixel
303 height = vga_height, // src_surface->h
304 width = vga_width; // src_surface->w
305
306 void* tmp_ptr;
307 SDL_LockTexture(dst_texture, NULL, &tmp_ptr, &dst_pitch);
308 dst = tmp_ptr;
309
310 int prevline, nextline;
311
312 Uint32 E0, E1, E2, E3, E4, E5, E6, E7, E8, A, B, C, D, E, F, G, H, I;
313 for (int y = 0; y < height; y++)
314 {
315 src_temp = src;
316 dst_temp = dst;
317
318 prevline = (y > 0) ? -src_pitch : 0;
319 nextline = (y < height - 1) ? src_pitch : 0;
320
321 for (int x = 0; x < width; x++)
322 {
323 A = rgb_palette[*(src + prevline - (x > 0 ? 1 : 0))];
324 B = rgb_palette[*(src + prevline)];
325 C = rgb_palette[*(src + prevline + (x < width - 1 ? 1 : 0))];
326 D = rgb_palette[*(src - (x > 0 ? 1 : 0))];
327 E = rgb_palette[*src];
328 F = rgb_palette[*(src + (x < width - 1 ? 1 : 0))];
329 G = rgb_palette[*(src + nextline - (x > 0 ? 1 : 0))];
330 H = rgb_palette[*(src + nextline)];
331 I = rgb_palette[*(src + nextline + (x < width - 1 ? 1 : 0))];
332
333 if (B != H && D != F)
334 {
335 E0 = D == B ? D : E;
336 E1 = (D == B && E != C) || (B == F && E != A) ? B : E;
337 E2 = B == F ? F : E;
338 E3 = (D == B && E != G) || (D == H && E != A) ? D : E;
339 E4 = E;
340 E5 = (B == F && E != I) || (H == F && E != C) ? F : E;
341 E6 = D == H ? D : E;
342 E7 = (D == H && E != I) || (H == F && E != G) ? H : E;
343 E8 = H == F ? F : E;
344 }
345 else
346 {
347 E0 = E1 = E2 = E3 = E4 = E5 = E6 = E7 = E8 = E;
348 }
349
350 *(Uint32 *)dst = E0;
351 *(Uint32 *)(dst + dst_Bpp) = E1;
352 *(Uint32 *)(dst + 2 * dst_Bpp) = E2;
353 *(Uint32 *)(dst + dst_pitch) = E3;
354 *(Uint32 *)(dst + dst_pitch + dst_Bpp) = E4;
355 *(Uint32 *)(dst + dst_pitch + 2 * dst_Bpp) = E5;
356 *(Uint32 *)(dst + 2 * dst_pitch) = E6;
357 *(Uint32 *)(dst + 2 * dst_pitch + dst_Bpp) = E7;
358 *(Uint32 *)(dst + 2 * dst_pitch + 2 * dst_Bpp) = E8;
359
360 src++;
361 dst += 3 * dst_Bpp;
362 }
363
364 src = src_temp + src_pitch;
365 dst = dst_temp + 3 * dst_pitch;
366 }
367
368 SDL_UnlockTexture(dst_texture);
369 }
370
371 void scale3x_16(SDL_Surface *src_surface, SDL_Texture *dst_texture)
372 {
373 Uint8 *src = src_surface->pixels, *src_temp;
374 Uint8 *dst, *dst_temp;
375
376 int src_pitch = src_surface->pitch;
377 int dst_pitch;
378
379 const int dst_Bpp = 2, // dst_surface->format->BytesPerPixel
380 height = vga_height, // src_surface->h
381 width = vga_width; // src_surface->w
382
383 void* tmp_ptr;
384 SDL_LockTexture(dst_texture, NULL, &tmp_ptr, &dst_pitch);
385 dst = tmp_ptr;
386
387 int prevline, nextline;
388
389 Uint16 E0, E1, E2, E3, E4, E5, E6, E7, E8, A, B, C, D, E, F, G, H, I;
390 for (int y = 0; y < height; y++)
391 {
392 src_temp = src;
393 dst_temp = dst;
394
395 prevline = (y > 0) ? -src_pitch : 0;
396 nextline = (y < height - 1) ? src_pitch : 0;
397
398 for (int x = 0; x < width; x++)
399 {
400 A = rgb_palette[*(src + prevline - (x > 0 ? 1 : 0))];
401 B = rgb_palette[*(src + prevline)];
402 C = rgb_palette[*(src + prevline + (x < width - 1 ? 1 : 0))];
403 D = rgb_palette[*(src - (x > 0 ? 1 : 0))];
404 E = rgb_palette[*src];
405 F = rgb_palette[*(src + (x < width - 1 ? 1 : 0))];
406 G = rgb_palette[*(src + nextline - (x > 0 ? 1 : 0))];
407 H = rgb_palette[*(src + nextline)];
408 I = rgb_palette[*(src + nextline + (x < width - 1 ? 1 : 0))];
409
410 if (B != H && D != F)
411 {
412 E0 = D == B ? D : E;
413 E1 = (D == B && E != C) || (B == F && E != A) ? B : E;
414 E2 = B == F ? F : E;
415 E3 = (D == B && E != G) || (D == H && E != A) ? D : E;
416 E4 = E;
417 E5 = (B == F && E != I) || (H == F && E != C) ? F : E;
418 E6 = D == H ? D : E;
419 E7 = (D == H && E != I) || (H == F && E != G) ? H : E;
420 E8 = H == F ? F : E;
421 }
422 else
423 {
424 E0 = E1 = E2 = E3 = E4 = E5 = E6 = E7 = E8 = E;
425 }
426
427 *(Uint16 *)dst = E0;
428 *(Uint16 *)(dst + dst_Bpp) = E1;
429 *(Uint16 *)(dst + 2 * dst_Bpp) = E2;
430 *(Uint16 *)(dst + dst_pitch) = E3;
431 *(Uint16 *)(dst + dst_pitch + dst_Bpp) = E4;
432 *(Uint16 *)(dst + dst_pitch + 2 * dst_Bpp) = E5;
433 *(Uint16 *)(dst + 2 * dst_pitch) = E6;
434 *(Uint16 *)(dst + 2 * dst_pitch + dst_Bpp) = E7;
435 *(Uint16 *)(dst + 2 * dst_pitch + 2 * dst_Bpp) = E8;
436
437 src++;
438 dst += 3 * dst_Bpp;
439 }
440
441 src = src_temp + src_pitch;
442 dst = dst_temp + 3 * dst_pitch;
443 }
444
445 SDL_UnlockTexture(dst_texture);
446 }