/
sdl_utils.cpp
2300 lines (1880 loc) · 55.6 KB
/
sdl_utils.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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2003 - 2014 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
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.
See the COPYING file for more details.
*/
/**
* @file
* Support-routines for the SDL-graphics-library.
*/
#include "global.hpp"
#include "sdl_utils.hpp"
#include "floating_point_emulation.hpp"
#include "neon.hpp"
#include "video.hpp"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
#include <boost/math/constants/constants.hpp>
surface_lock::surface_lock(surface &surf) : surface_(surf), locked_(false)
{
if (SDL_MUSTLOCK(surface_))
locked_ = SDL_LockSurface(surface_) == 0;
}
surface_lock::~surface_lock()
{
if (locked_)
SDL_UnlockSurface(surface_);
}
const_surface_lock::const_surface_lock(const surface &surf) : surface_(surf), locked_(false)
{
if (SDL_MUSTLOCK(surface_))
locked_ = SDL_LockSurface(surface_) == 0;
}
const_surface_lock::~const_surface_lock()
{
if (locked_)
SDL_UnlockSurface(surface_);
}
SDL_Color int_to_color(const Uint32 rgb)
{
SDL_Color result;
result.r = (0x00FF0000 & rgb )>> 16;
result.g = (0x0000FF00 & rgb) >> 8;
result.b = (0x000000FF & rgb);
result.unused = 0;
return result;
}
SDL_Color create_color(const unsigned char red
, unsigned char green
, unsigned char blue
, unsigned char unused)
{
SDL_Color result;
result.r = red;
result.g = green;
result.b = blue;
result.unused = unused;
return result;
}
SDLKey sdl_keysym_from_name(std::string const &keyname)
{
static bool initialized = false;
typedef std::map<std::string const, SDLKey> keysym_map_t;
static keysym_map_t keysym_map;
if (!initialized) {
for(SDLKey i = SDLK_FIRST; i < SDLK_LAST; i = SDLKey(int(i) + 1)) {
std::string name = SDL_GetKeyName(i);
if (!name.empty())
keysym_map[name] = i;
}
initialized = true;
}
keysym_map_t::const_iterator it = keysym_map.find(keyname);
if (it != keysym_map.end())
return it->second;
else
return SDLK_UNKNOWN;
}
bool point_in_rect(int x, int y, const SDL_Rect& rect)
{
return x >= rect.x && y >= rect.y && x < rect.x + rect.w && y < rect.y + rect.h;
}
bool rects_overlap(const SDL_Rect& rect1, const SDL_Rect& rect2)
{
return (rect1.x < rect2.x+rect2.w && rect2.x < rect1.x+rect1.w &&
rect1.y < rect2.y+rect2.h && rect2.y < rect1.y+rect1.h);
}
SDL_Rect intersect_rects(SDL_Rect const &rect1, SDL_Rect const &rect2)
{
SDL_Rect res;
res.x = std::max<int>(rect1.x, rect2.x);
res.y = std::max<int>(rect1.y, rect2.y);
int w = std::min<int>(rect1.x + rect1.w, rect2.x + rect2.w) - res.x;
int h = std::min<int>(rect1.y + rect1.h, rect2.y + rect2.h) - res.y;
if (w <= 0 || h <= 0) return empty_rect;
res.w = w;
res.h = h;
return res;
}
SDL_Rect union_rects(SDL_Rect const &rect1, SDL_Rect const &rect2)
{
if (rect1.w == 0 || rect1.h == 0) return rect2;
if (rect2.w == 0 || rect2.h == 0) return rect1;
SDL_Rect res;
res.x = std::min<int>(rect1.x, rect2.x);
res.y = std::min<int>(rect1.y, rect2.y);
res.w = std::max<int>(rect1.x + rect1.w, rect2.x + rect2.w) - res.x;
res.h = std::max<int>(rect1.y + rect1.h, rect2.y + rect2.h) - res.y;
return res;
}
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
return rect;
}
bool operator<(const surface& a, const surface& b)
{
return a.get() < b.get();
}
bool is_neutral(const surface& surf)
{
return (surf->format->BytesPerPixel == 4 &&
surf->format->Rmask == 0xFF0000u &&
(surf->format->Amask | 0xFF000000u) == 0xFF000000u);
}
static SDL_PixelFormat& get_neutral_pixel_format()
{
static bool first_time = true;
static SDL_PixelFormat format;
if(first_time) {
first_time = false;
surface surf(SDL_CreateRGBSurface(SDL_SWSURFACE,1,1,32,0xFF0000,0xFF00,0xFF,0xFF000000));
format = *surf->format;
format.palette = NULL;
}
return format;
}
surface make_neutral_surface(const surface &surf)
{
if(surf == NULL) {
std::cerr << "null neutral surface...\n";
return NULL;
}
surface const result = SDL_ConvertSurface(surf,&get_neutral_pixel_format(),SDL_SWSURFACE);
if(result != NULL) {
SDL_SetAlpha(result,SDL_SRCALPHA,SDL_ALPHA_OPAQUE);
}
return result;
}
surface create_neutral_surface(int w, int h)
{
if (w < 0 || h < 0) {
std::cerr << "error : neutral surface with negative dimensions\n";
return NULL;
}
SDL_PixelFormat format = get_neutral_pixel_format();
surface result = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
format.BitsPerPixel,
format.Rmask,
format.Gmask,
format.Bmask,
format.Amask);
return result;
}
surface create_optimized_surface(const surface &surf)
{
if(surf == NULL)
return NULL;
surface const result = display_format_alpha(surf);
if(result == surf) {
std::cerr << "resulting surface is the same as the source!!!\n";
} else if(result == NULL) {
return surf;
}
SDL_SetAlpha(result,SDL_SRCALPHA|SDL_RLEACCEL,SDL_ALPHA_OPAQUE);
return result;
}
surface stretch_surface_horizontal(
const surface& surf, const unsigned w, const bool optimize)
{
// Since SDL version 1.1.5 0 is transparent, before 255 was transparent.
assert(SDL_ALPHA_TRANSPARENT==0);
if(surf == NULL)
return NULL;
if(static_cast<int>(w) == surf->w) {
return surf;
}
assert(w > 0);
surface dst(create_neutral_surface(w, surf->h));
surface src(make_neutral_surface(surf));
// Now both surfaces are always in the "neutral" pixel format
if(src == NULL || dst == NULL) {
std::cerr << "Could not create surface to scale onto\n";
return NULL;
}
{
// Extra scoping used for the surface_lock.
const_surface_lock src_lock(src);
surface_lock dst_lock(dst);
const Uint32* const src_pixels = src_lock.pixels();
Uint32* dst_pixels = dst_lock.pixels();
for(unsigned y = 0; y < static_cast<unsigned>(src->h); ++y) {
const Uint32 pixel = src_pixels [y * src->w];
for(unsigned x = 0; x < w; ++x) {
*dst_pixels++ = pixel;
}
}
}
return optimize ? create_optimized_surface(dst) : dst;
}
surface stretch_surface_vertical(
const surface& surf, const unsigned h, const bool optimize)
{
// Since SDL version 1.1.5 0 is transparent, before 255 was transparent.
assert(SDL_ALPHA_TRANSPARENT==0);
if(surf == NULL)
return NULL;
if(static_cast<int>(h) == surf->h) {
return surf;
}
assert(h > 0);
surface dst(create_neutral_surface(surf->w, h));
surface src(make_neutral_surface(surf));
// Now both surfaces are always in the "neutral" pixel format
if(src == NULL || dst == NULL) {
std::cerr << "Could not create surface to scale onto\n";
return NULL;
}
{
// Extra scoping used for the surface_lock.
const_surface_lock src_lock(src);
surface_lock dst_lock(dst);
const Uint32* const src_pixels = src_lock.pixels();
Uint32* dst_pixels = dst_lock.pixels();
for(unsigned y = 0; y < static_cast<unsigned>(h); ++y) {
for(unsigned x = 0; x < static_cast<unsigned>(src->w); ++x) {
*dst_pixels++ = src_pixels[x];
}
}
}
return optimize ? create_optimized_surface(dst) : dst;
}
#ifdef PANDORA
static void
scale_surface_down(surface& dst, const surface& src, const int w_dst, const int h_dst)
{
const_surface_lock src_lock(src);
surface_lock dst_lock(dst);
const Uint32* const src_pixels = src_lock.pixels();
Uint32* const dst_pixels = dst_lock.pixels();
int y_dst = 0; // The current y in the destination surface
int y_src = 0; // The current y in the source surface
int y_src_next = 0; // The next y in the source surface
int y_step = 0; // The y stepper
int h_src = src->h; // The height of the source surface
for( ; y_dst != h_dst; ++y_dst, y_src = y_src_next) {
y_step += h_src;
do {
++y_src_next;
y_step -= h_dst;
} while(y_step >= h_dst);
int x_dst = 0; // The current x in the destination surface
int x_src = 0; // The current x in the source surface
int x_src_next = 0; // The next x in the source surface
int x_step = 0; // The x stepper
int w_src = src->w; // The width of the source surface
for( ; x_dst != w_dst; ++x_dst, x_src = x_src_next) {
x_step += w_src;
do {
++x_src_next;
x_step -= w_dst;
} while(x_step >= w_dst);
int r_sum = 0, g_sum = 0, b_sum = 0, a_sum = 0;
int samples = 0;
// We now have a rectangle, (xsrc,ysrc,xratio,yratio)
// which we want to derive the pixel from
for(int x = x_src; x < x_src_next; ++x) {
for(int y = y_src; y < y_src_next; ++y) {
++samples;
const Uint32 pixel = src_pixels[y_src * w_src + x_src];
const Uint8 a = pixel >> 24;
if(a) {
a_sum += a;
r_sum += a * static_cast<Uint8>(pixel >> 16);
g_sum += a * static_cast<Uint8>(pixel >> 8);
b_sum += a * static_cast<Uint8>(pixel);
}
}
}
if(a_sum) {
const int adjustment = (a_sum | 1) >> 1;
r_sum += adjustment;
g_sum += adjustment;
b_sum += adjustment;
r_sum /= a_sum;
g_sum /= a_sum;
b_sum /= a_sum;
assert(samples == (x_src_next - x_src) * (y_src_next - y_src));
if(samples != 1) {
a_sum += (samples | 1) >> 1;
a_sum /= samples;
}
}
dst_pixels[y_dst * w_dst + x_dst] =
static_cast<Uint8>(a_sum) << 24
| static_cast<Uint8>(r_sum) << 16
| static_cast<Uint8>(g_sum) << 8
| static_cast<Uint8>(b_sum);
}
}
}
#endif
// NOTE: Don't pass this function 0 scaling arguments.
surface scale_surface(const surface &surf, int w, int h, bool optimize)
{
// Since SDL version 1.1.5 0 is transparent, before 255 was transparent.
assert(SDL_ALPHA_TRANSPARENT==0);
if(surf == NULL)
return NULL;
if(w == surf->w && h == surf->h) {
return surf;
}
assert(w >= 0);
assert(h >= 0);
surface dst(create_neutral_surface(w,h));
if (w == 0 || h ==0) {
std::cerr << "Create an empty image\n";
return create_optimized_surface(dst);
}
surface src(make_neutral_surface(surf));
// Now both surfaces are always in the "neutral" pixel format
if(src == NULL || dst == NULL) {
std::cerr << "Could not create surface to scale onto\n";
return NULL;
}
{
const_surface_lock src_lock(src);
surface_lock dst_lock(dst);
const Uint32* const src_pixels = src_lock.pixels();
Uint32* const dst_pixels = dst_lock.pixels();
fixed_t xratio = fxpdiv(surf->w,w);
fixed_t yratio = fxpdiv(surf->h,h);
fixed_t ysrc = ftofxp(0.0);
for(int ydst = 0; ydst != h; ++ydst, ysrc += yratio) {
fixed_t xsrc = ftofxp(0.0);
for(int xdst = 0; xdst != w; ++xdst, xsrc += xratio) {
const int xsrcint = fxptoi(xsrc);
const int ysrcint = fxptoi(ysrc);
const Uint32* const src_word = src_pixels + ysrcint*src->w + xsrcint;
Uint32* const dst_word = dst_pixels + ydst*dst->w + xdst;
const int dx = (xsrcint + 1 < src->w) ? 1 : 0;
const int dy = (ysrcint + 1 < src->h) ? src->w : 0;
Uint8 r,g,b,a;
Uint32 rr,gg,bb,aa;
Uint16 avg_r, avg_g, avg_b, avg_a;
Uint32 pix[4], bilin[4];
// This next part is the fixed point
// equivalent of "take everything to
// the right of the decimal point."
// These fundamental weights decide
// the contributions from various
// input pixels. The labels assume
// that the upper left corner of the
// screen ("northeast") is 0,0 but the
// code should still be consistent if
// the graphics origin is actually
// somewhere else.
const fixed_t e = 0x000000FF & xsrc;
const fixed_t s = 0x000000FF & ysrc;
const fixed_t n = 0xFF - s;
const fixed_t w = 0xFF - e;
pix[0] = *src_word; // northwest
pix[1] = *(src_word + dx); // northeast
pix[2] = *(src_word + dy); // southwest
pix[3] = *(src_word + dx + dy); // southeast
bilin[0] = n*w;
bilin[1] = n*e;
bilin[2] = s*w;
bilin[3] = s*e;
// Scope out the neighboorhood, see
// what the pixel values are like.
int count = 0;
avg_r = avg_g = avg_b = avg_a = 0;
int loc;
for (loc=0; loc<4; loc++) {
a = pix[loc] >> 24;
r = pix[loc] >> 16;
g = pix[loc] >> 8;
b = pix[loc] >> 0;
if (a != 0) {
avg_r += r;
avg_g += g;
avg_b += b;
avg_a += a;
count++;
}
}
if (count>0) {
avg_r /= count;
avg_b /= count;
avg_g /= count;
avg_a /= count;
}
// Perform modified bilinear interpolation.
// Don't trust any color information from
// an RGBA sample when the alpha channel
// is set to fully transparent.
//
// Some of the input images are hex tiles,
// created using a hexagon shaped alpha channel
// that is either set to full-on or full-off.
//
// If intermediate alpha values are introduced
// along a hex edge, it produces a gametime artifact.
// Moving the mouse around will leave behind
// "hexagon halos" from the temporary highlighting.
// In other words, the Wesnoth rendering engine
// freaks out.
//
// The alpha thresholding step attempts
// to accommodates this limitation.
// There is a small loss of quality.
// For example, skeleton bowstrings
// are not as good as they could be.
rr = gg = bb = aa = 0;
for (loc=0; loc<4; loc++) {
a = pix[loc] >> 24;
r = pix[loc] >> 16;
g = pix[loc] >> 8;
b = pix[loc] >> 0;
if (a == 0) {
r = static_cast<Uint8>(avg_r);
g = static_cast<Uint8>(avg_g);
b = static_cast<Uint8>(avg_b);
}
rr += r * bilin[loc];
gg += g * bilin[loc];
bb += b * bilin[loc];
aa += a * bilin[loc];
}
r = rr >> 16;
g = gg >> 16;
b = bb >> 16;
a = aa >> 16;
a = (a < avg_a/2) ? 0 : avg_a;
*dst_word = (a << 24) + (r << 16) + (g << 8) + b;
}
}
}
return optimize ? create_optimized_surface(dst) : dst;
}
surface scale_surface_sharp(const surface& surf, int w, int h, bool optimize)
{
// Since SDL version 1.1.5 0 is transparent, before 255 was transparent.
assert(SDL_ALPHA_TRANSPARENT==0);
if(surf == NULL)
return NULL;
if(w == surf->w && h == surf->h) {
return surf;
}
assert(w >= 0);
assert(h >= 0);
surface dst(create_neutral_surface(w,h));
if (w == 0 || h ==0) {
std::cerr << "Create an empty image\n";
return create_optimized_surface(dst);
}
surface src(make_neutral_surface(surf));
// Now both surfaces are always in the "neutral" pixel format
if(src == NULL || dst == NULL) {
std::cerr << "Could not create surface to scale onto\n";
return NULL;
}
#ifdef PANDORA
scale_surface_down(dst, src, w, h);
#else
{
const_surface_lock src_lock(src);
surface_lock dst_lock(dst);
const Uint32* const src_pixels = src_lock.pixels();
Uint32* const dst_pixels = dst_lock.pixels();
tfloat xratio = tfloat(surf->w) / w;
tfloat yratio = tfloat(surf->h) / h;
tfloat ysrc;
for(int ydst = 0; ydst != h; ++ydst, ysrc += yratio) {
tfloat xsrc;
for(int xdst = 0; xdst != w; ++xdst, xsrc += xratio) {
tfloat red, green, blue, alpha;
tfloat summation;
// We now have a rectangle, (xsrc,ysrc,xratio,yratio)
// which we want to derive the pixel from
for(tfloat xloc = xsrc; xloc < xsrc+xratio; xloc += 1) {
const tfloat xsize = std::min<tfloat>(floor(xloc + 1)-xloc,xsrc+xratio-xloc);
for(tfloat yloc = ysrc; yloc < ysrc+yratio; yloc += 1) {
const int xsrcint = std::max<int>(0,std::min<int>(src->w-1,xsrc.to_int()));
const int ysrcint = std::max<int>(0,std::min<int>(src->h-1,ysrc.to_int()));
const tfloat ysize = std::min<tfloat>(floor(yloc+1)-yloc,ysrc+yratio-yloc);
Uint8 r,g,b,a;
SDL_GetRGBA(src_pixels[ysrcint*src->w + xsrcint],src->format,&r,&g,&b,&a);
tfloat value = xsize * ysize;
summation += value;
if (!a) continue;
value *= a;
alpha += value;
red += r * value;
green += g * value;
blue += b * value;
}
}
if (alpha != 0) {
red = red / alpha + 0.5;
green = green / alpha + 0.5;
blue = blue / alpha + 0.5;
alpha = alpha / summation + 0.5;
}
dst_pixels[ydst*dst->w + xdst] = SDL_MapRGBA(
dst->format
, red.to_int()
, green.to_int()
, blue.to_int()
, alpha.to_int());
}
}
}
#endif
return optimize ? create_optimized_surface(dst) : dst;
}
surface tile_surface(const surface& surf, int w, int h, bool optimize)
{
if (surf->w == w && surf->h == h) {
return surf;
}
surface dest(create_neutral_surface(w, h));
surface src(make_neutral_surface(surf));
if (src == NULL || dest == NULL) {
std::cerr << "failed to make neutral surface\n";
return NULL;
}
{
const_surface_lock srclock(src);
surface_lock destlock(dest);
const Uint32* srcpixels = srclock.pixels();
Uint32* destpixels = destlock.pixels();
const int& sw = src->w;
const int& sh = src->h;
const int xoff = (w - sw) / 2;
const int yoff = (h - sh) / 2;
for (int i = 0; i<w*h; ++i) {
int x = ((i % w) - xoff);
int y = ((i / w) - yoff);
while ((x += sw) < 0) { /* DO NOTHING */ }
while ((y += sh) < 0) { /* DO NOTHING */ }
const int sx = x % sw;
const int sy = y % sh;
destpixels[i] = srcpixels[sy*sw + sx];
}
}
return optimize ? create_optimized_surface(dest) : dest;
}
surface adjust_surface_color(const surface &surf, int red, int green, int blue, bool optimize)
{
if(surf == NULL)
return NULL;
if((red == 0 && green == 0 && blue == 0))
return optimize ? create_optimized_surface(surf) : surf;
surface nsurf(make_neutral_surface(surf));
if(nsurf == NULL) {
std::cerr << "failed to make neutral surface\n";
return NULL;
}
{
surface_lock lock(nsurf);
Uint32* beg = lock.pixels();
Uint32* end = beg + nsurf->w*surf->h;
while(beg != end) {
Uint8 alpha = (*beg) >> 24;
if(alpha) {
Uint8 r, g, b;
r = (*beg) >> 16;
g = (*beg) >> 8;
b = (*beg) >> 0;
r = std::max<int>(0,std::min<int>(255,int(r)+red));
g = std::max<int>(0,std::min<int>(255,int(g)+green));
b = std::max<int>(0,std::min<int>(255,int(b)+blue));
*beg = (alpha << 24) + (r << 16) + (g << 8) + b;
}
++beg;
}
}
return optimize ? create_optimized_surface(nsurf) : nsurf;
}
surface greyscale_image(const surface &surf, bool optimize)
{
if(surf == NULL)
return NULL;
surface nsurf(make_neutral_surface(surf));
if(nsurf == NULL) {
std::cerr << "failed to make neutral surface\n";
return NULL;
}
{
surface_lock lock(nsurf);
Uint32* beg = lock.pixels();
Uint32* end = beg + nsurf->w*surf->h;
while(beg != end) {
Uint8 alpha = (*beg) >> 24;
if(alpha) {
Uint8 r, g, b;
r = (*beg) >> 16;
g = (*beg) >> 8;
b = (*beg);
//const Uint8 avg = (red+green+blue)/3;
// Use the correct formula for RGB to grayscale conversion.
// Ok, this is no big deal :)
// The correct formula being:
// gray=0.299red+0.587green+0.114blue
const Uint8 avg = static_cast<Uint8>((
77 * static_cast<Uint16>(r) +
150 * static_cast<Uint16>(g) +
29 * static_cast<Uint16>(b) ) / 256);
*beg = (alpha << 24) | (avg << 16) | (avg << 8) | avg;
}
++beg;
}
}
return optimize ? create_optimized_surface(nsurf) : nsurf;
}
surface shadow_image(const surface &surf, bool optimize)
{
if(surf == NULL)
return NULL;
// we blur it, and reuse the neutral surface created by the blur function (optimized = false)
surface nsurf (blur_alpha_surface(surf, 2, false));
if(nsurf == NULL) {
std::cerr << "failed to blur the shadow surface\n";
return NULL;
}
{
surface_lock lock(nsurf);
Uint32* beg = lock.pixels();
Uint32* end = beg + nsurf->w*surf->h;
while(beg != end) {
Uint8 alpha = (*beg) >> 24;
if(alpha) {
// increase alpha and color in black (RGB=0)
// with some stupid optimization for handling maximum values
if (alpha < 255/4)
*beg = (alpha*4) << 24;
else
*beg = 0xFF000000; // we hit the maximum
}
++beg;
}
}
return optimize ? create_optimized_surface(nsurf) : nsurf;
}
surface recolor_image(surface surf, const std::map<Uint32, Uint32>& map_rgb, bool optimize){
if(surf == NULL)
return NULL;
if(!map_rgb.empty()){
surface nsurf(make_neutral_surface(surf));
if(nsurf == NULL) {
std::cerr << "failed to make neutral surface\n";
return NULL;
}
surface_lock lock(nsurf);
Uint32* beg = lock.pixels();
Uint32* end = beg + nsurf->w*surf->h;
while(beg != end) {
Uint8 alpha = (*beg) >> 24;
if(alpha){ // don't recolor invisible pixels.
// palette use only RGB channels, so remove alpha
Uint32 oldrgb = (*beg) & 0x00FFFFFF;
std::map<Uint32, Uint32>::const_iterator i = map_rgb.find(oldrgb);
if(i != map_rgb.end()){
*beg = (alpha << 24) + i->second;
}
}
++beg;
}
return optimize ? create_optimized_surface(nsurf) : nsurf;
}
return surf;
}
surface brighten_image(const surface &surf, fixed_t amount, bool optimize)
{
if(surf == NULL) {
return NULL;
}
surface nsurf(make_neutral_surface(surf));
if(nsurf == NULL) {
std::cerr << "could not make neutral surface...\n";
return NULL;
}
{
surface_lock lock(nsurf);
Uint32* beg = lock.pixels();
Uint32* end = beg + nsurf->w*surf->h;
if (amount < 0) amount = 0;
while(beg != end) {
Uint8 alpha = (*beg) >> 24;
if(alpha) {
Uint8 r, g, b;
r = (*beg) >> 16;
g = (*beg) >> 8;
b = (*beg);
r = std::min<unsigned>(unsigned(fxpmult(r, amount)),255);
g = std::min<unsigned>(unsigned(fxpmult(g, amount)),255);
b = std::min<unsigned>(unsigned(fxpmult(b, amount)),255);
*beg = (alpha << 24) + (r << 16) + (g << 8) + b;
}
++beg;
}
}
return optimize ? create_optimized_surface(nsurf) : nsurf;
}
surface adjust_surface_alpha(const surface &surf, fixed_t amount, bool optimize)
{
if(surf== NULL) {
return NULL;
}
surface nsurf(make_neutral_surface(surf));
if(nsurf == NULL) {
std::cerr << "could not make neutral surface...\n";
return NULL;
}
{
surface_lock lock(nsurf);
Uint32* beg = lock.pixels();
Uint32* end = beg + nsurf->w*surf->h;
if (amount < 0) amount = 0;
while(beg != end) {
Uint8 alpha = (*beg) >> 24;
if(alpha) {
Uint8 r, g, b;
r = (*beg) >> 16;
g = (*beg) >> 8;
b = (*beg);
alpha = std::min<unsigned>(unsigned(fxpmult(alpha,amount)),255);
*beg = (alpha << 24) + (r << 16) + (g << 8) + b;
}
++beg;
}
}
return optimize ? create_optimized_surface(nsurf) : nsurf;
}
surface adjust_surface_alpha_add(const surface &surf, int amount, bool optimize)
{
if(surf== NULL) {
return NULL;
}
surface nsurf(make_neutral_surface(surf));
if(nsurf == NULL) {
std::cerr << "could not make neutral surface...\n";
return NULL;
}
{
surface_lock lock(nsurf);
Uint32* beg = lock.pixels();
Uint32* end = beg + nsurf->w*surf->h;
while(beg != end) {
Uint8 alpha = (*beg) >> 24;
if(alpha) {
Uint8 r, g, b;
r = (*beg) >> 16;
g = (*beg) >> 8;
b = (*beg);
alpha = Uint8(std::max<int>(0,std::min<int>(255,int(alpha) + amount)));
*beg = (alpha << 24) + (r << 16) + (g << 8) + b;
}
++beg;
}
}
return optimize ? create_optimized_surface(nsurf) : nsurf;
}
surface mask_surface(const surface &surf, const surface &mask, bool* empty_result, const std::string& filename)
{
if(surf == NULL) {
return NULL;
}
if(mask == NULL) {
return surf;
}
surface nsurf = make_neutral_surface(surf);
surface nmask(make_neutral_surface(mask));
if(nsurf == NULL || nmask == NULL) {
std::cerr << "could not make neutral surface...\n";