diff src/filters/hq3x32.cpp @ 27:b970226568d2

brought in filters package
author Robert McIntyre <rlm@mit.edu>
date Sun, 04 Mar 2012 20:32:31 -0600
parents f9f4f1b99eed
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/filters/hq3x32.cpp	Sun Mar 04 20:32:31 2012 -0600
     1.3 @@ -0,0 +1,445 @@
     1.4 +#include "../Port.h"
     1.5 +#include "hq_shared32.h"
     1.6 +#include "interp.h"
     1.7 +
     1.8 +#define SIZE_PIXEL 2 // 16bit = 2 bytes
     1.9 +#define PIXELTYPE unsigned short
    1.10 +#define Interp1 Interp1_16
    1.11 +#define Interp2 Interp2_16
    1.12 +#define Interp3 Interp3_16
    1.13 +#define Interp4 Interp4_16
    1.14 +#define Interp5 Interp5_16
    1.15 +
    1.16 +void hq3x(unsigned char *pIn,  unsigned int srcPitch,
    1.17 +          unsigned char *,
    1.18 +          unsigned char *pOut, unsigned int dstPitch,
    1.19 +          int Xres, int Yres)
    1.20 +{
    1.21 +	int i, j;
    1.22 +	unsigned int line;
    1.23 +	PIXELTYPE	 c[10];
    1.24 +
    1.25 +	// +----+----+----+
    1.26 +	// |    |    |    |
    1.27 +	// | c1 | c2 | c3 |
    1.28 +	// +----+----+----+
    1.29 +	// |    |    |    |
    1.30 +	// | c4 | c5 | c6 |
    1.31 +	// +----+----+----+
    1.32 +	// |    |    |    |
    1.33 +	// | c7 | c8 | c9 |
    1.34 +	// +----+----+----+
    1.35 +
    1.36 +	for (j = 0; j < Yres; j++)
    1.37 +	{
    1.38 +		if ((j > 0) || (j < Yres - 1))
    1.39 +			line = srcPitch;
    1.40 +		else
    1.41 +			line = 0;
    1.42 +
    1.43 +		for (i = 0; i < Xres; i++)
    1.44 +		{
    1.45 +			c[2] = *((PIXELTYPE *)(pIn - line));
    1.46 +			c[5] = *((PIXELTYPE *)(pIn));
    1.47 +			c[8] = *((PIXELTYPE *)(pIn + line));
    1.48 +
    1.49 +			if (i > 0)
    1.50 +			{
    1.51 +				c[1] = *((PIXELTYPE *)(pIn - line - SIZE_PIXEL));
    1.52 +				c[4] = *((PIXELTYPE *)(pIn        - SIZE_PIXEL));
    1.53 +				c[7] = *((PIXELTYPE *)(pIn + line - SIZE_PIXEL));
    1.54 +			}
    1.55 +			else
    1.56 +			{
    1.57 +				c[1] = c[2];
    1.58 +				c[4] = c[5];
    1.59 +				c[7] = c[8];
    1.60 +			}
    1.61 +
    1.62 +			if (i < Xres - 1)
    1.63 +			{
    1.64 +				c[3] = *((PIXELTYPE *)(pIn - line + SIZE_PIXEL));
    1.65 +				c[6] = *((PIXELTYPE *)(pIn        + SIZE_PIXEL));
    1.66 +				c[9] = *((PIXELTYPE *)(pIn + line + SIZE_PIXEL));
    1.67 +			}
    1.68 +			else
    1.69 +			{
    1.70 +				c[3] = c[2];
    1.71 +				c[6] = c[5];
    1.72 +				c[9] = c[8];
    1.73 +			}
    1.74 +
    1.75 +			int pattern = 0;
    1.76 +
    1.77 +			if (interp_16_diff(c[1], c[5]))
    1.78 +				pattern |= 1 << 0;
    1.79 +			if (interp_16_diff(c[2], c[5]))
    1.80 +				pattern |= 1 << 1;
    1.81 +			if (interp_16_diff(c[3], c[5]))
    1.82 +				pattern |= 1 << 2;
    1.83 +			if (interp_16_diff(c[4], c[5]))
    1.84 +				pattern |= 1 << 3;
    1.85 +			if (interp_16_diff(c[6], c[5]))
    1.86 +				pattern |= 1 << 4;
    1.87 +			if (interp_16_diff(c[7], c[5]))
    1.88 +				pattern |= 1 << 5;
    1.89 +			if (interp_16_diff(c[8], c[5]))
    1.90 +				pattern |= 1 << 6;
    1.91 +			if (interp_16_diff(c[9], c[5]))
    1.92 +				pattern |= 1 << 7;
    1.93 +
    1.94 +#define Diff interp_16_diff
    1.95 +#include "hq3x32.h"
    1.96 +#undef Diff
    1.97 +			pIn	 += SIZE_PIXEL;
    1.98 +			pOut += 3 << 1;
    1.99 +		}
   1.100 +		pIn	 += srcPitch - (Xres << 1);
   1.101 +		pOut += dstPitch - (3 * Xres << 1);
   1.102 +		pOut += dstPitch << 1;
   1.103 +		//	pIn+=SIZE_PIXEL;
   1.104 +		//	pOut+=3*SIZE_PIXEL;
   1.105 +		//}
   1.106 +		//pIn+=srcPitch-(4*Xres);
   1.107 +		//pOut+=dstPitch-(3*Xres*SIZE_PIXEL);
   1.108 +		//pOut+=2*dstPitch;
   1.109 +	}
   1.110 +}
   1.111 +
   1.112 +void hq3xS(unsigned char *pIn,  unsigned int srcPitch,
   1.113 +           unsigned char *,
   1.114 +           unsigned char *pOut, unsigned int dstPitch,
   1.115 +           int Xres, int Yres)
   1.116 +{
   1.117 +	int i, j;
   1.118 +	PIXELTYPE c[10];
   1.119 +
   1.120 +	// +----+----+----+
   1.121 +	// |    |    |    |
   1.122 +	// | c1 | c2 | c3 |
   1.123 +	// +----+----+----+
   1.124 +	// |    |    |    |
   1.125 +	// | c4 | c5 | c6 |
   1.126 +	// +----+----+----+
   1.127 +	// |    |    |    |
   1.128 +	// | c7 | c8 | c9 |
   1.129 +	// +----+----+----+
   1.130 +
   1.131 +	for (j = 0; j < Yres; j++)
   1.132 +	{
   1.133 +		for (i = 0; i < Xres; i++)
   1.134 +		{
   1.135 +			c[2] = *((PIXELTYPE *)(pIn - srcPitch));
   1.136 +			c[5] = *((PIXELTYPE *)(pIn));
   1.137 +			c[8] = *((PIXELTYPE *)(pIn + srcPitch));
   1.138 +
   1.139 +			c[1] = *((PIXELTYPE *)(pIn - srcPitch - SIZE_PIXEL));
   1.140 +			c[4] = *((PIXELTYPE *)(pIn        - SIZE_PIXEL));
   1.141 +			c[7] = *((PIXELTYPE *)(pIn + srcPitch - SIZE_PIXEL));
   1.142 +
   1.143 +			c[3] = *((PIXELTYPE *)(pIn - srcPitch + SIZE_PIXEL));
   1.144 +			c[6] = *((PIXELTYPE *)(pIn        + SIZE_PIXEL));
   1.145 +			c[9] = *((PIXELTYPE *)(pIn + srcPitch + SIZE_PIXEL));
   1.146 +
   1.147 +			int pattern = 0;
   1.148 +
   1.149 +			// hq3xS dynamic edge detection:
   1.150 +			// simply comparing the center color against its surroundings will give bad results in many cases,
   1.151 +			// so, instead, compare the center color relative to the max difference in brightness of this 3x3 block
   1.152 +			int brightArray[10];
   1.153 +			int maxBright = 0, minBright = 999999;
   1.154 +			for (int j = 1; j < 10; j++)
   1.155 +			{
   1.156 +				int r, g, b;
   1.157 +				if (interp_bits_per_pixel == 16)
   1.158 +				{
   1.159 +					b = (int)((c[j] & 0x1F)) << 3;
   1.160 +					g = (int)((c[j] & 0x7E0)) >> 3;
   1.161 +					r = (int)((c[j] & 0xF800)) >> 8;
   1.162 +				}
   1.163 +				else
   1.164 +				{
   1.165 +					b = (int)((c[j] & 0x1F)) << 3;
   1.166 +					g = (int)((c[j] & 0x3E0)) >> 2;
   1.167 +					r = (int)((c[j] & 0x7C00)) >> 7;
   1.168 +				}
   1.169 +				const int bright = r + r + r + g + g + g + b + b;
   1.170 +				if (bright > maxBright) maxBright = bright;
   1.171 +				if (bright < minBright) minBright = bright;
   1.172 +
   1.173 +				brightArray[j] = bright;
   1.174 +			}
   1.175 +			const int diffBright = ((maxBright - minBright) * 7) >> 4;
   1.176 +			if (diffBright > 7)
   1.177 +			{
   1.178 +				#define ABS(x) ((x) < 0 ? -(x) : (x))
   1.179 +
   1.180 +				const int centerBright = brightArray[5];
   1.181 +				if (ABS(brightArray[1] - centerBright) > diffBright)
   1.182 +					pattern |= 1 << 0;
   1.183 +				if (ABS(brightArray[2] - centerBright) > diffBright)
   1.184 +					pattern |= 1 << 1;
   1.185 +				if (ABS(brightArray[3] - centerBright) > diffBright)
   1.186 +					pattern |= 1 << 2;
   1.187 +				if (ABS(brightArray[4] - centerBright) > diffBright)
   1.188 +					pattern |= 1 << 3;
   1.189 +				if (ABS(brightArray[6] - centerBright) > diffBright)
   1.190 +					pattern |= 1 << 4;
   1.191 +				if (ABS(brightArray[7] - centerBright) > diffBright)
   1.192 +					pattern |= 1 << 5;
   1.193 +				if (ABS(brightArray[8] - centerBright) > diffBright)
   1.194 +					pattern |= 1 << 6;
   1.195 +				if (ABS(brightArray[9] - centerBright) > diffBright)
   1.196 +					pattern |= 1 << 7;
   1.197 +			}
   1.198 +
   1.199 +#define Diff(x, y) false //(ABS((x) - (y)) > diffBright)
   1.200 +#undef cget
   1.201 +#define cget(x) brightArray[x]
   1.202 +#include "hq3x32.h"
   1.203 +#undef cget
   1.204 +#undef Diff
   1.205 +			pIn	 += SIZE_PIXEL;
   1.206 +			pOut += 3 << 1;
   1.207 +		}
   1.208 +		pIn	 += srcPitch - (Xres << 1);
   1.209 +		pOut += dstPitch - (3 * Xres << 1);
   1.210 +		pOut += dstPitch << 1;
   1.211 +		//	pIn+=SIZE_PIXEL;
   1.212 +		//	pOut+=3*SIZE_PIXEL;
   1.213 +		//}
   1.214 +		//pIn+=srcPitch-(4*Xres);
   1.215 +		//pOut+=dstPitch-(3*Xres*SIZE_PIXEL);
   1.216 +		//pOut+=2*dstPitch;
   1.217 +	}
   1.218 +}
   1.219 +
   1.220 +#undef Interp1
   1.221 +#undef Interp2
   1.222 +#undef Interp3
   1.223 +#undef Interp4
   1.224 +#undef Interp5
   1.225 +#undef SIZE_PIXEL
   1.226 +#undef PIXELTYPE
   1.227 +#define SIZE_PIXEL 4 // 32bit = 4 bytes
   1.228 +#define PIXELTYPE unsigned int
   1.229 +
   1.230 +void hq3x32(unsigned char *pIn,  unsigned int srcPitch,
   1.231 +            unsigned char *,
   1.232 +            unsigned char *pOut, unsigned int dstPitch,
   1.233 +            int Xres, int Yres)
   1.234 +{
   1.235 +	unsigned int YUV1, YUV2;
   1.236 +	int i, j, k;
   1.237 +	unsigned int line;
   1.238 +	PIXELTYPE c[10];
   1.239 +
   1.240 +	// +----+----+----+
   1.241 +	// |    |    |    |
   1.242 +	// | c1 | c2 | c3 |
   1.243 +	// +----+----+----+
   1.244 +	// |    |    |    |
   1.245 +	// | c4 | c5 | c6 |
   1.246 +	// +----+----+----+
   1.247 +	// |    |    |    |
   1.248 +	// | c7 | c8 | c9 |
   1.249 +	// +----+----+----+
   1.250 +
   1.251 +	for (j = 0; j < Yres; j++)
   1.252 +	{
   1.253 +		if ((j > 0) && (j < Yres - 1))
   1.254 +			line = srcPitch;
   1.255 +		else
   1.256 +			line = 0;
   1.257 +
   1.258 +		for (i = 0; i < Xres; i++)
   1.259 +		{
   1.260 +			c[2] = *((PIXELTYPE *)(pIn - line));
   1.261 +			c[5] = *((PIXELTYPE *)(pIn));
   1.262 +			c[8] = *((PIXELTYPE *)(pIn + line));
   1.263 +
   1.264 +			if (i > 0)
   1.265 +			{
   1.266 +				c[1] = *((PIXELTYPE *)(pIn - line - SIZE_PIXEL));
   1.267 +				c[4] = *((PIXELTYPE *)(pIn        - SIZE_PIXEL));
   1.268 +				c[7] = *((PIXELTYPE *)(pIn + line - SIZE_PIXEL));
   1.269 +			}
   1.270 +			else
   1.271 +			{
   1.272 +				c[1] = c[2];
   1.273 +				c[4] = c[5];
   1.274 +				c[7] = c[8];
   1.275 +			}
   1.276 +
   1.277 +			if (i < Xres - 1)
   1.278 +			{
   1.279 +				c[3] = *((PIXELTYPE *)(pIn - line + SIZE_PIXEL));
   1.280 +				c[6] = *((PIXELTYPE *)(pIn        + SIZE_PIXEL));
   1.281 +				c[9] = *((PIXELTYPE *)(pIn + line + SIZE_PIXEL));
   1.282 +			}
   1.283 +			else
   1.284 +			{
   1.285 +				c[3] = c[2];
   1.286 +				c[6] = c[5];
   1.287 +				c[9] = c[8];
   1.288 +			}
   1.289 +
   1.290 +			int pattern = 0;
   1.291 +			int flag	= 1;
   1.292 +
   1.293 +			YUV1 = RGBtoYUV(c[5]);
   1.294 +
   1.295 +			for (k = 1; k <= 9; k++)
   1.296 +			{
   1.297 +				if (k == 5) continue;
   1.298 +
   1.299 +				if (c[k] != c[5])
   1.300 +				{
   1.301 +					YUV2 = RGBtoYUV(c[k]);
   1.302 +					if (
   1.303 +					    (abs32((YUV1 & Ymask) - (YUV2 & Ymask)) > trY) ||
   1.304 +					    (abs32((YUV1 & Umask) - (YUV2 & Umask)) > trU) ||
   1.305 +					    (abs32((YUV1 & Vmask) - (YUV2 & Vmask)) > trV)
   1.306 +					    )
   1.307 +						pattern |= flag;
   1.308 +				}
   1.309 +				flag <<= 1;
   1.310 +			}
   1.311 +
   1.312 +#include "hq3x32.h"
   1.313 +			pIn	 += SIZE_PIXEL;
   1.314 +			pOut += 3 << 2;
   1.315 +		}
   1.316 +		pIn	 += srcPitch - (Xres << 2);
   1.317 +		pOut += dstPitch - (3 * Xres << 2);
   1.318 +		pOut += dstPitch << 1;
   1.319 +		//	pIn+=SIZE_PIXEL;
   1.320 +		//	pOut+=3*SIZE_PIXEL;
   1.321 +		//}
   1.322 +		//pIn+=srcPitch-(4*Xres);
   1.323 +		//pOut+=dstPitch-(3*Xres*SIZE_PIXEL);
   1.324 +		//pOut+=2*dstPitch;
   1.325 +	}
   1.326 +}
   1.327 +
   1.328 +void hq3xS32(unsigned char *pIn,  unsigned int srcPitch,
   1.329 +             unsigned char *,
   1.330 +             unsigned char *pOut, unsigned int dstPitch,
   1.331 +             int Xres, int Yres)
   1.332 +{
   1.333 +	int i, j;
   1.334 +	unsigned int line;
   1.335 +	PIXELTYPE c[10];
   1.336 +
   1.337 +	// +----+----+----+
   1.338 +	// |    |    |    |
   1.339 +	// | c1 | c2 | c3 |
   1.340 +	// +----+----+----+
   1.341 +	// |    |    |    |
   1.342 +	// | c4 | c5 | c6 |
   1.343 +	// +----+----+----+
   1.344 +	// |    |    |    |
   1.345 +	// | c7 | c8 | c9 |
   1.346 +	// +----+----+----+
   1.347 +
   1.348 +	for (j = 0; j < Yres; j++)
   1.349 +	{
   1.350 +		if ((j > 0) && (j < Yres - 1))
   1.351 +			line = srcPitch;
   1.352 +		else
   1.353 +			line = 0;
   1.354 +
   1.355 +		for (i = 0; i < Xres; i++)
   1.356 +		{
   1.357 +			c[2] = *((PIXELTYPE *)(pIn - line));
   1.358 +			c[5] = *((PIXELTYPE *)(pIn));
   1.359 +			c[8] = *((PIXELTYPE *)(pIn + line));
   1.360 +
   1.361 +			if (i > 0)
   1.362 +			{
   1.363 +				c[1] = *((PIXELTYPE *)(pIn - line - SIZE_PIXEL));
   1.364 +				c[4] = *((PIXELTYPE *)(pIn        - SIZE_PIXEL));
   1.365 +				c[7] = *((PIXELTYPE *)(pIn + line - SIZE_PIXEL));
   1.366 +			}
   1.367 +			else
   1.368 +			{
   1.369 +				c[1] = c[2];
   1.370 +				c[4] = c[5];
   1.371 +				c[7] = c[8];
   1.372 +			}
   1.373 +
   1.374 +			if (i < Xres - 1)
   1.375 +			{
   1.376 +				c[3] = *((PIXELTYPE *)(pIn - line + SIZE_PIXEL));
   1.377 +				c[6] = *((PIXELTYPE *)(pIn        + SIZE_PIXEL));
   1.378 +				c[9] = *((PIXELTYPE *)(pIn + line + SIZE_PIXEL));
   1.379 +			}
   1.380 +			else
   1.381 +			{
   1.382 +				c[3] = c[2];
   1.383 +				c[6] = c[5];
   1.384 +				c[9] = c[8];
   1.385 +			}
   1.386 +
   1.387 +			int pattern = 0;
   1.388 +
   1.389 +			// hq3xS dynamic edge detection:
   1.390 +			// simply comparing the center color against its surroundings will give bad results in many cases,
   1.391 +			// so, instead, compare the center color relative to the max difference in brightness of this 3x3 block
   1.392 +			int brightArray[10];
   1.393 +			int maxBright = 0, minBright = 999999;
   1.394 +			for (int j = 1; j < 10; j++)
   1.395 +			{
   1.396 +				const int b		 = (int)((c[j] & 0xF8));
   1.397 +				const int g		 = (int)((c[j] & 0xF800)) >> 8;
   1.398 +				const int r		 = (int)((c[j] & 0xF80000)) >> 16;
   1.399 +				const int bright = r + r + r + g + g + g + b + b;
   1.400 +				if (bright > maxBright) maxBright = bright;
   1.401 +				if (bright < minBright) minBright = bright;
   1.402 +
   1.403 +				brightArray[j] = bright;
   1.404 +			}
   1.405 +			int diffBright = ((maxBright - minBright) * 7) >> 4;
   1.406 +			if (diffBright > 7)
   1.407 +			{
   1.408 +				#define ABS(x) ((x) < 0 ? -(x) : (x))
   1.409 +
   1.410 +				const int centerBright = brightArray[5];
   1.411 +				if (ABS(brightArray[1] - centerBright) > diffBright)
   1.412 +					pattern |= 1 << 0;
   1.413 +				if (ABS(brightArray[2] - centerBright) > diffBright)
   1.414 +					pattern |= 1 << 1;
   1.415 +				if (ABS(brightArray[3] - centerBright) > diffBright)
   1.416 +					pattern |= 1 << 2;
   1.417 +				if (ABS(brightArray[4] - centerBright) > diffBright)
   1.418 +					pattern |= 1 << 3;
   1.419 +				if (ABS(brightArray[6] - centerBright) > diffBright)
   1.420 +					pattern |= 1 << 4;
   1.421 +				if (ABS(brightArray[7] - centerBright) > diffBright)
   1.422 +					pattern |= 1 << 5;
   1.423 +				if (ABS(brightArray[8] - centerBright) > diffBright)
   1.424 +					pattern |= 1 << 6;
   1.425 +				if (ABS(brightArray[9] - centerBright) > diffBright)
   1.426 +					pattern |= 1 << 7;
   1.427 +			}
   1.428 +
   1.429 +#define Diff(x, y) false //(ABS((x) - (y)) > diffBright)
   1.430 +#undef cget
   1.431 +#define cget(x) brightArray[x]
   1.432 +#include "hq3x32.h"
   1.433 +#undef cget
   1.434 +#undef Diff
   1.435 +			pIn	 += SIZE_PIXEL;
   1.436 +			pOut += 3 << 2;
   1.437 +		}
   1.438 +		pIn	 += srcPitch - (Xres << 2);
   1.439 +		pOut += dstPitch - (3 * Xres << 2);
   1.440 +		pOut += dstPitch << 1;
   1.441 +		//	pIn+=SIZE_PIXEL;
   1.442 +		//	pOut+=3*SIZE_PIXEL;
   1.443 +		//}
   1.444 +		//pIn+=srcPitch-(4*Xres);
   1.445 +		//pOut+=dstPitch-(3*Xres*SIZE_PIXEL);
   1.446 +		//pOut+=2*dstPitch;
   1.447 +	}
   1.448 +}
   1.449 \ No newline at end of file