summaryrefslogtreecommitdiff
path: root/src/libs/graphics/sdl/2xscalers.c
blob: a612d2c44429d0b1e5499ff4a4d736c27a724fd5 (plain)
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
/*
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "libs/graphics/sdl/sdl_common.h"
#include "types.h"
#include "scalers.h"
#include "scaleint.h"
#include "2xscalers.h"


// Scaler function lookup table
//
const Scale_FuncDef_t
Scale_C_Functions[] =
{
	{TFB_GFXFLAGS_SCALE_BILINEAR,   Scale_BilinearFilter},
	{TFB_GFXFLAGS_SCALE_BIADAPT,    Scale_BiAdaptFilter},
	{TFB_GFXFLAGS_SCALE_BIADAPTADV, Scale_BiAdaptAdvFilter},
	{TFB_GFXFLAGS_SCALE_TRISCAN,    Scale_TriScanFilter},
	{TFB_GFXFLAGS_SCALE_HQXX,       Scale_HqFilter},
	// Default
	{0,                             Scale_Nearest}
};

// See
//	nearest2x.c  -- Nearest Neighboor scaling
//	bilinear2x.c -- Bilinear scaling
//	biadv2x.c    -- Advanced Biadapt scaling
//	triscan2x.c  -- Triscan scaling

// Biadapt scaling to 2x
void
SCALE_(BiAdaptFilter) (SDL_Surface *src, SDL_Surface *dst, SDL_Rect *r)
{
	int x, y;
	const int w = src->w, h = src->h;
	int xend, yend;
	int dsrc, ddst;
	SDL_Rect *region = r;
	SDL_Rect limits;
	SDL_PixelFormat *fmt = dst->format;
	const int sp = src->pitch, dp = dst->pitch;
	const int bpp = fmt->BytesPerPixel;
	const int slen = sp / bpp, dlen = dp / bpp;
	Uint32 *src_p = (Uint32 *)src->pixels;
	Uint32 *dst_p = (Uint32 *)dst->pixels;
	Uint32 pixval_tl, pixval_tr, pixval_bl, pixval_br;

	// these macros are for clarity; they make the current pixel (0,0)
	// and allow to access pixels in all directions
	#define SRC(x, y)   (src_p + (x) + ((y) * slen))

	SCALE_(PlatInit) ();

	// expand updated region if necessary
	// pixels neighbooring the updated region may
	// change as a result of updates
	limits.x = 0;
	limits.y = 0;
	limits.w = src->w;
	limits.h = src->h;
	Scale_ExpandRect (region, 2, &limits);

	xend = region->x + region->w;
	yend = region->y + region->h;
	dsrc = slen - region->w;
	ddst = (dlen - region->w) * 2;

	// move ptrs to the first updated pixel
	src_p += slen * region->y + region->x;
	dst_p += (dlen * region->y + region->x) * 2;

	for (y = region->y; y < yend; ++y, dst_p += ddst, src_p += dsrc)
	{
		for (x = region->x; x < xend; ++x, ++src_p, ++dst_p)
		{
			pixval_tl = SCALE_GETPIX (SRC (0, 0));
			
			SCALE_SETPIX (dst_p, pixval_tl);
			
			if (y + 1 < h)
			{
				// check pixel below the current one
				pixval_bl = SCALE_GETPIX (SRC (0, 1));

				if (pixval_tl == pixval_bl)
					SCALE_SETPIX (dst_p + dlen, pixval_tl);
				else
					SCALE_SETPIX (dst_p + dlen, Scale_Blend_11 (
							pixval_tl, pixval_bl)
							);
			}
			else
			{
				// last pixel in column - propagate
				SCALE_SETPIX (dst_p + dlen, pixval_tl);
				pixval_bl = pixval_tl;
			}
			++dst_p;

			if (x + 1 >= w)
			{
				// last pixel in row - propagate
				SCALE_SETPIX (dst_p, pixval_tl);

				if (pixval_tl == pixval_bl)
					SCALE_SETPIX (dst_p + dlen, pixval_tl);
				else
					SCALE_SETPIX (dst_p + dlen, Scale_Blend_11 (
							pixval_tl, pixval_bl)
							);
				continue;
			}
			
			// check pixel to the right from the current one
			pixval_tr = SCALE_GETPIX (SRC (1, 0));

			if (pixval_tl == pixval_tr)
				SCALE_SETPIX (dst_p, pixval_tr);
			else
				SCALE_SETPIX (dst_p, Scale_Blend_11 (
						pixval_tl, pixval_tr)
						);

			if (y + 1 >= h)
			{
				// last pixel in column - propagate
				SCALE_SETPIX (dst_p + dlen, pixval_tl);
				continue;
			}
			
			// check pixel to the bottom-right
			pixval_br = SCALE_GETPIX (SRC (1, 1));

			if (pixval_tl == pixval_br && pixval_tr == pixval_bl)
			{
				int cl, cr;
				Uint32 clr;

				if (pixval_tl == pixval_tr)
				{
					// all 4 are equal - propagate
					SCALE_SETPIX (dst_p + dlen, pixval_tl);
					continue;
				}

				// both pairs are equal, have to resolve the pixel
				// race; we try detecting which color is
				// the background by looking for a line or an edge
				// examine 8 pixels surrounding the current quad

				cl = cr = 1;

				if (x > 0)
				{
					clr = SCALE_GETPIX (SRC (-1, 0));
					if (clr == pixval_tl)
						cl++;
					else if (clr == pixval_tr)
						cr++;

					clr = SCALE_GETPIX (SRC (-1, 1));
					if (clr == pixval_tl)
						cl++;
					else if (clr == pixval_tr)
						cr++;
				}

				if (y > 0)
				{
					clr = SCALE_GETPIX (SRC (0, -1));
					if (clr == pixval_tl)
						cl++;
					else if (clr == pixval_tr)
						cr++;

					clr = SCALE_GETPIX (SRC (1, -1));
					if (clr == pixval_tl)
						cl++;
					else if (clr == pixval_tr)
						cr++;
				}

				if (x + 2 < w)
				{
					clr = SCALE_GETPIX (SRC (2, 0));
					if (clr == pixval_tl)
						cl++;
					else if (clr == pixval_tr)
						cr++;

					clr = SCALE_GETPIX (SRC (2, 1));
					if (clr == pixval_tl)
						cl++;
					else if (clr == pixval_tr)
						cr++;
				}

				if (y + 2 < h)
				{
					clr = SCALE_GETPIX (SRC (0, 2));
					if (clr == pixval_tl)
						cl++;
					else if (clr == pixval_tr)
						cr++;

					clr = SCALE_GETPIX (SRC (1, 2));
					if (clr == pixval_tl)
						cl++;
					else if (clr == pixval_tr)
						cr++;
				}
				
				// least count wins
				if (cl > cr)
					SCALE_SETPIX (dst_p + dlen, pixval_tr);
				else if (cr > cl)
					SCALE_SETPIX (dst_p + dlen, pixval_tl);
				else
					SCALE_SETPIX (dst_p + dlen,
							Scale_Blend_11 (pixval_tl, pixval_tr));
			}
			else if (pixval_tl == pixval_br)
			{
				// main diagonal is same color
				// use its value
				SCALE_SETPIX (dst_p + dlen, pixval_tl);
			}
			else if (pixval_tr == pixval_bl)
			{
				// 2nd diagonal is same color
				// use its value
				SCALE_SETPIX (dst_p + dlen, pixval_tr);
			}
			else
			{
				// blend all 4
				SCALE_SETPIX (dst_p + dlen, Scale_Blend_1111 (
						pixval_tl, pixval_bl, pixval_tr, pixval_br
						));
			}
		}
	}

	SCALE_(PlatDone) ();
}