|
432
|
1 /*
|
|
|
2 pygame - Python Game Library
|
|
|
3 Copyright (C) 2000-2001 Pete Shinners
|
|
|
4
|
|
|
5 This library is free software; you can redistribute it and/or
|
|
|
6 modify it under the terms of the GNU Library General Public
|
|
|
7 License as published by the Free Software Foundation; either
|
|
|
8 version 2 of the License, or (at your option) any later version.
|
|
|
9
|
|
|
10 This library is distributed in the hope that it will be useful,
|
|
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
13 Library General Public License for more details.
|
|
|
14
|
|
|
15 You should have received a copy of the GNU Library General Public
|
|
|
16 License along with this library; if not, write to the Free
|
|
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
18
|
|
|
19 Pete Shinners
|
|
|
20 pete@shinners.org
|
|
|
21 */
|
|
|
22
|
|
|
23 /*
|
|
|
24 This implements the AdvanceMAME Scale2x feature found on this page,
|
|
|
25 http://advancemame.sourceforge.net/scale2x.html
|
|
|
26
|
|
|
27 It is an incredibly simple and powerful image doubling routine that does
|
|
|
28 an astonishing job of doubling game graphic data while interpolating out
|
|
|
29 the jaggies. Congrats to the AdvanceMAME team, I'm very impressed and
|
|
|
30 surprised with this code!
|
|
|
31 */
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35 #include <SDL.h>
|
|
|
36 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
|
|
|
37 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
|
|
|
38
|
|
|
39
|
|
|
40 #define READINT24(x) ((x)[0]<<16 | (x)[1]<<8 | (x)[2])
|
|
|
41 #define WRITEINT24(x, i) {(x)[0]=i>>16; (x)[1]=(i>>8)&0xff; x[2]=i&0xff; }
|
|
|
42
|
|
|
43 /*
|
|
|
44 this requires a destination surface already setup to be twice as
|
|
|
45 large as the source. oh, and formats must match too. this will just
|
|
|
46 blindly assume you didn't flounder.
|
|
|
47 */
|
|
|
48
|
|
|
49 void scale2x(SDL_Surface *src, SDL_Surface *dst)
|
|
|
50 {
|
|
|
51 int looph, loopw;
|
|
|
52
|
|
|
53 Uint8* srcpix = (Uint8*)src->pixels;
|
|
|
54 Uint8* dstpix = (Uint8*)dst->pixels;
|
|
|
55
|
|
|
56 const int srcpitch = src->pitch;
|
|
|
57 const int dstpitch = dst->pitch;
|
|
|
58 const int width = src->w;
|
|
|
59 const int height = src->h;
|
|
|
60
|
|
|
61 switch(src->format->BytesPerPixel)
|
|
|
62 {
|
|
|
63 case 1: {
|
|
|
64 Uint8 E0, E1, E2, E3, B, D, E, F, H;
|
|
|
65 for(looph = 0; looph < height; ++looph)
|
|
|
66 {
|
|
|
67 for(loopw = 0; loopw < width; ++ loopw)
|
|
|
68 {
|
|
|
69 B = *(Uint8*)(srcpix + (MAX(0,looph-1)*srcpitch) + (1*loopw));
|
|
|
70 D = *(Uint8*)(srcpix + (looph*srcpitch) + (1*MAX(0,loopw-1)));
|
|
|
71 E = *(Uint8*)(srcpix + (looph*srcpitch) + (1*loopw));
|
|
|
72 F = *(Uint8*)(srcpix + (looph*srcpitch) + (1*MIN(width-1,loopw+1)));
|
|
|
73 H = *(Uint8*)(srcpix + (MIN(height-1,looph+1)*srcpitch) + (1*loopw));
|
|
|
74
|
|
|
75 E0 = D == B && B != F && D != H ? D : E;
|
|
|
76 E1 = B == F && B != D && F != H ? F : E;
|
|
|
77 E2 = D == H && D != B && H != F ? D : E;
|
|
|
78 E3 = H == F && D != H && B != F ? F : E;
|
|
|
79
|
|
|
80 *(Uint8*)(dstpix + looph*2*dstpitch + loopw*2*1) = E0;
|
|
|
81 *(Uint8*)(dstpix + looph*2*dstpitch + (loopw*2+1)*1) = E1;
|
|
|
82 *(Uint8*)(dstpix + (looph*2+1)*dstpitch + loopw*2*1) = E2;
|
|
|
83 *(Uint8*)(dstpix + (looph*2+1)*dstpitch + (loopw*2+1)*1) = E3;
|
|
|
84 }
|
|
|
85 }break;}
|
|
|
86 case 2: {
|
|
|
87 Uint16 E0, E1, E2, E3, B, D, E, F, H;
|
|
|
88 for(looph = 0; looph < height; ++looph)
|
|
|
89 {
|
|
|
90 for(loopw = 0; loopw < width; ++ loopw)
|
|
|
91 {
|
|
|
92 B = *(Uint16*)(srcpix + (MAX(0,looph-1)*srcpitch) + (2*loopw));
|
|
|
93 D = *(Uint16*)(srcpix + (looph*srcpitch) + (2*MAX(0,loopw-1)));
|
|
|
94 E = *(Uint16*)(srcpix + (looph*srcpitch) + (2*loopw));
|
|
|
95 F = *(Uint16*)(srcpix + (looph*srcpitch) + (2*MIN(width-1,loopw+1)));
|
|
|
96 H = *(Uint16*)(srcpix + (MIN(height-1,looph+1)*srcpitch) + (2*loopw));
|
|
|
97
|
|
|
98 E0 = D == B && B != F && D != H ? D : E;
|
|
|
99 E1 = B == F && B != D && F != H ? F : E;
|
|
|
100 E2 = D == H && D != B && H != F ? D : E;
|
|
|
101 E3 = H == F && D != H && B != F ? F : E;
|
|
|
102
|
|
|
103 *(Uint16*)(dstpix + looph*2*dstpitch + loopw*2*2) = E0;
|
|
|
104 *(Uint16*)(dstpix + looph*2*dstpitch + (loopw*2+1)*2) = E1;
|
|
|
105 *(Uint16*)(dstpix + (looph*2+1)*dstpitch + loopw*2*2) = E2;
|
|
|
106 *(Uint16*)(dstpix + (looph*2+1)*dstpitch + (loopw*2+1)*2) = E3;
|
|
|
107 }
|
|
|
108 }break;}
|
|
|
109 case 3: {
|
|
|
110 int E0, E1, E2, E3, B, D, E, F, H;
|
|
|
111 for(looph = 0; looph < height; ++looph)
|
|
|
112 {
|
|
|
113 for(loopw = 0; loopw < width; ++ loopw)
|
|
|
114 {
|
|
|
115 B = READINT24(srcpix + (MAX(0,looph-1)*srcpitch) + (3*loopw));
|
|
|
116 D = READINT24(srcpix + (looph*srcpitch) + (3*MAX(0,loopw-1)));
|
|
|
117 E = READINT24(srcpix + (looph*srcpitch) + (3*loopw));
|
|
|
118 F = READINT24(srcpix + (looph*srcpitch) + (3*MIN(width-1,loopw+1)));
|
|
|
119 H = READINT24(srcpix + (MIN(height-1,looph+1)*srcpitch) + (3*loopw));
|
|
|
120
|
|
|
121 E0 = D == B && B != F && D != H ? D : E;
|
|
|
122 E1 = B == F && B != D && F != H ? F : E;
|
|
|
123 E2 = D == H && D != B && H != F ? D : E;
|
|
|
124 E3 = H == F && D != H && B != F ? F : E;
|
|
|
125
|
|
|
126 WRITEINT24((dstpix + looph*2*dstpitch + loopw*2*3), E0);
|
|
|
127 WRITEINT24((dstpix + looph*2*dstpitch + (loopw*2+1)*3), E1);
|
|
|
128 WRITEINT24((dstpix + (looph*2+1)*dstpitch + loopw*2*3), E2);
|
|
|
129 WRITEINT24((dstpix + (looph*2+1)*dstpitch + (loopw*2+1)*3), E3);
|
|
|
130 }
|
|
|
131 }break;}
|
|
|
132 default: { /*case 4:*/
|
|
|
133 Uint32 E0, E1, E2, E3, B, D, E, F, H;
|
|
|
134 for(looph = 0; looph < height; ++looph)
|
|
|
135 {
|
|
|
136 for(loopw = 0; loopw < width; ++ loopw)
|
|
|
137 {
|
|
|
138 B = *(Uint32*)(srcpix + (MAX(0,looph-1)*srcpitch) + (4*loopw));
|
|
|
139 D = *(Uint32*)(srcpix + (looph*srcpitch) + (4*MAX(0,loopw-1)));
|
|
|
140 E = *(Uint32*)(srcpix + (looph*srcpitch) + (4*loopw));
|
|
|
141 F = *(Uint32*)(srcpix + (looph*srcpitch) + (4*MIN(width-1,loopw+1)));
|
|
|
142 H = *(Uint32*)(srcpix + (MIN(height-1,looph+1)*srcpitch) + (4*loopw));
|
|
|
143
|
|
|
144 E0 = D == B && B != F && D != H ? D : E;
|
|
|
145 E1 = B == F && B != D && F != H ? F : E;
|
|
|
146 E2 = D == H && D != B && H != F ? D : E;
|
|
|
147 E3 = H == F && D != H && B != F ? F : E;
|
|
|
148
|
|
|
149 *(Uint32*)(dstpix + looph*2*dstpitch + loopw*2*4) = E0;
|
|
|
150 *(Uint32*)(dstpix + looph*2*dstpitch + (loopw*2+1)*4) = E1;
|
|
|
151 *(Uint32*)(dstpix + (looph*2+1)*dstpitch + loopw*2*4) = E2;
|
|
|
152 *(Uint32*)(dstpix + (looph*2+1)*dstpitch + (loopw*2+1)*4) = E3;
|
|
|
153 }
|
|
|
154 }break;}
|
|
|
155 }
|
|
|
156 }
|
|
|
157
|