Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

scale.c 19.6 KB
Newer Older
Deucе's avatar
Deucе committed
1
#include <math.h>
Deucе's avatar
Deucе committed
2 3
#include <stdbool.h>
#include <stdlib.h>
4
#include <stdio.h>
Deucе's avatar
Deucе committed
5

6
#include "ciolib.h"
Deucе's avatar
Deucе committed
7 8 9
#include "scale.h"
#include "xbr.h"

Deucе's avatar
Deucе committed
10 11 12 13
uint32_t r2y[1<<24];
uint32_t y2r[1<<24];
static int r2y_inited;

Deucе's avatar
Deucе committed
14 15
static void pointy_scale3(uint32_t* src, uint32_t* dest, int width, int height);
static void pointy_scale5(uint32_t* src, uint32_t* dest, int width, int height);
Deucе's avatar
Deucе committed
16
static void pointy_scale_odd(uint32_t* src, uint32_t* dest, int width, int height, int mult);
Deucе's avatar
Deucе committed
17
static void interpolate_height(uint32_t* src, uint32_t* dst, int width, int height, int newheight);
Deucе's avatar
Deucе committed
18
static void interpolate_width(uint32_t* src, uint32_t* dst, int width, int height, int newwidth);
Deucе's avatar
Deucе committed
19 20 21 22 23 24 25 26 27 28 29
static void multiply_scale(uint32_t* src, uint32_t* dst, int width, int height, int xmult, int ymult);

static struct graphics_buffer *free_list;

#define CLAMP(x) do { \
	if (x < 0) \
		x = 0; \
	else if (x > 255) \
		x = 255; \
} while(0)

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
/*
 * Corrects width/height to have the specified aspect ratio
 */
void
aspect_fix(int *x, int *y, int aspect_width, int aspect_height)
{
	int bestx, besty;

	// Nothing we can do here...
	if (aspect_width == 0 || aspect_height == 0)
		return;
	bestx = lround((double)*y * aspect_width / aspect_height);
	besty = lround((double)*x * aspect_height / aspect_width);

	if (bestx < *x && bestx > 0)
		*x = bestx;
	else
		*y = besty;
}

/*
 * Given a width/height of a source image, adjust it to match the current aspect
 * ratio.  Will not reduce either number
 */
void
aspect_correct(int *x, int *y, int aspect_width, int aspect_height)
{
	int width = *x;
	int height;

	if (!aspect_height || !aspect_width)
		return;
	height = lround((double)(width * aspect_height) / aspect_width);
	if (height < *y) {
		height = *y;
		width = lround(((double)height * aspect_width) / aspect_height);
	}

	*x = width;
	*y = height;
}

/*
 * Essentially the opposite of the above.  Given an output width/height, translates to
 * the size of the source image.
 *
 * Note that this is much trickier as the "source" bitmap may have been integer scaled
 * differently in both directions... so what this does is reverse the aspect ratio
 * calculation, then find the next lowest even multiple of the mode bitmap size.
 */
void
aspect_reverse(int *x, int *y, int scrnwidth, int scrnheight, int aspect_width, int aspect_height)
{
	int width = *x;
	int height = *y;
	int cheight;
	int cwidth;

	if (!aspect_height || !aspect_width) {
		width = scrnwidth * (*x / scrnwidth);
		if (width < scrnwidth)
			width = scrnwidth;
		height = scrnheight * (*x / scrnheight);
		if (height < scrnheight)
			height = scrnheight;
		return;
	}
	// First, find the "controlling" dimension... the one that won't be scaled (ie: the one that gets smaller)
	cwidth = lround((double)(height * aspect_width) / aspect_height * scrnwidth / scrnheight);
	cheight = lround((double)(width * aspect_height) / aspect_width * scrnheight / scrnwidth);
	if (cwidth > width) {
		// Width controls, so this is simply finding the largest width multiple that fits in the box
		width = scrnwidth * (*x / scrnwidth);
		if (width < scrnwidth)
			width = scrnwidth;

		// Now we need to find the largest bitmap height that would fit in the output height
		// So, we scale the height to bitmap size...
		height = lround((double)*y / ((double)scrnwidth / scrnheight) * ((double)aspect_width / aspect_height));
		// And do the same calculation...
		height = lround((double)scrnheight * ((double)height / scrnheight));
	}
	else if (cheight > height) {
		// Height controls
		height = scrnheight * (*x / scrnheight);
		if (height < scrnheight)
			height = scrnheight;

		width = lround((double)*x / ((double)scrnheight / scrnwidth) * ((double)aspect_height / aspect_width));
		width = lround((double)scrnwidth * ((double)width / scrnwidth));
	}

	*x = width;
	*y = height;
}

Deucе's avatar
Deucе committed
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
void
init_r2y(void)
{
	int r, g, b;
	int y, u, v;
	const double luma = 255.0 / 219;
	const double col  = 255.0 / 224;

	if (r2y_inited)
		return;
	for (r = 0; r < 256; r++) {
		for (g = 0; g < 256; g++) {
			for (b = 0; b < 256; b++) {
				y =  16 + ( 65.738 * r + 129.057 * g +  25.064 * b + 128) / 256;
				CLAMP(y);
				u = 128 + (-37.945 * r -  74.494 * g + 112.439 * b + 128) / 256;
				CLAMP(u);
				v = 128 + (112.439 * r -  94.154 * g -  18.285 * b + 128) / 256;
				CLAMP(v);

				r2y[(r<<16) | (g<<8) | b] = (y<<16)|(u<<8)|v;
			}
		}
	}
	for (y = 0; y < 256; y++) {
		for (u = 0; u < 256; u++) {
			for (v = 0; v < 256; v++) {
				const int c = y - 16;
				const int d = u - 128;
				const int e = v - 128;
				r = luma * c                                     + col * 1.402 * e;
				CLAMP(r);
				g = luma * c - col * 1.772 * (0.114 / 0.587) * d - col * 1.402 * (0.299 / 0.587) * e;
				CLAMP(g);
				b = luma * c + col * 1.772 * d;
				CLAMP(b);
Deucе's avatar
Deucе committed
162

Deucе's avatar
Deucе committed
163 164 165 166 167 168 169 170 171
				y2r[(y<<16) | (u<<8) | v] = (r<<16)|(g<<8)|b;
			}
		}
	}
	r2y_inited = 1;
}

struct graphics_buffer *
get_buffer(void)
Deucе's avatar
Deucе committed
172
{
Deucе's avatar
Deucе committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	struct graphics_buffer* ret = NULL;

	if (free_list) {
		ret = free_list;
		free_list = free_list->next;
		ret->next = NULL;
		return ret;
	}

	ret = calloc(1, sizeof(struct graphics_buffer));
	return ret;
}

void
release_buffer(struct graphics_buffer *buf)
{
Deucе's avatar
Deucе committed
189 190
	if (buf == NULL)
		return;
Deucе's avatar
Deucе committed
191 192 193 194 195
	buf->next = free_list;
	free_list = buf;
}

struct graphics_buffer *
196
do_scale(struct rectlist* rect, int xscale, int yscale, int aspect_width, int aspect_height)
Deucе's avatar
Deucе committed
197 198 199
{
	struct graphics_buffer* ret1 = get_buffer();
	struct graphics_buffer* ret2 = get_buffer();
Deucе's avatar
Deucе committed
200
	int pointymult = 1;
Deucе's avatar
Deucе committed
201 202 203 204
	int pointy5 = 0;
	int pointy3 = 0;
	int xbr2 = 0;
	int xbr4 = 0;
Deucе's avatar
Deucе committed
205 206 207 208 209 210
	int ymult = 1;
	int xmult = 1;
	int total_xscaling = 1;
	int total_yscaling = 1;
	struct graphics_buffer *ctarget;
	struct graphics_buffer *csrc;
Deucе's avatar
Deucе committed
211
	uint32_t* nt;
Deucе's avatar
Deucе committed
212
	int fheight;
Deucе's avatar
Deucе committed
213
	int fwidth;
214
	bool swapxy = false;
Deucе's avatar
Deucе committed
215

216 217 218 219 220 221 222 223 224 225
	if (xscale > yscale) {
		swapxy = true;
		total_xscaling = xscale;
		xscale = yscale;
		yscale = total_xscaling;
	}
	total_xscaling = xscale;
	xscale = 1;
	total_yscaling = yscale;
	yscale = 1;
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
	// If x/y scaling isn't a simple multiple, block scale everything...
	if ((total_yscaling % total_xscaling) == 0) {
		if (!(cio_api.options & CONIO_OPT_BLOCKY_SCALING)) {
			if ((total_xscaling & 1) == 1 && total_xscaling > 5) {
				pointymult = total_xscaling;
				total_xscaling /= pointymult;
				xscale *= pointymult;
				total_yscaling /= pointymult;
				yscale *= pointymult;
			}
			while (total_xscaling > 1 && ((total_xscaling % 5) == 0) && ((total_yscaling % 5) == 0)) {
				pointy5++;
				total_xscaling /= 5;
				xscale *= 5;
				total_yscaling /= 5;
				yscale *= 5;
			}
			while (total_xscaling > 1 && ((total_xscaling % 3) == 0) && ((total_yscaling % 3) == 0)) {
				pointy3++;
				total_xscaling /= 3;
				xscale *= 3;
				total_yscaling /= 3;
				yscale *= 3;
			}
			while (total_xscaling > 1 && ((total_xscaling % 4) == 0) && ((total_yscaling % 4) == 0)) {
				xbr4++;
				total_xscaling /= 4;
				xscale *= 4;
				total_yscaling /= 4;
				yscale *= 4;
			}
			while (total_xscaling > 1 && ((total_xscaling % 2) == 0) && ((total_yscaling % 2) == 0)) {
				xbr2++;
				total_xscaling /= 2;
				xscale *= 2;
				total_yscaling /= 2;
				yscale *= 2;
			}
264
		}
Deucе's avatar
Deucе committed
265 266
	}

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	xmult = total_xscaling;
	xscale *= xmult;
	total_xscaling = 1;

	ymult = total_yscaling;
	yscale *= ymult;
	total_yscaling = 1;

	if (swapxy) {
		int tmp;

		tmp = ymult;
		ymult = xmult;
		xmult = tmp;

		tmp = xscale;
		xscale = yscale;
		yscale = tmp;
Deucе's avatar
Deucе committed
285 286
	}

287 288 289 290
	// Calculate the scaled height from rxscaleatio...
	fwidth = rect->rect.width * xscale;
	fheight = rect->rect.height * yscale;
	aspect_correct(&fwidth, &fheight, aspect_width, aspect_height);
Deucе's avatar
Deucе committed
291

Deucе's avatar
Deucе committed
292
	// Now make sure target is big enough...
Deucе's avatar
Deucе committed
293
	size_t needsz = fwidth * fheight * sizeof(uint32_t);
Deucе's avatar
Deucе committed
294 295
	if (needsz > ret1->sz) {
		nt = realloc(ret1->data, needsz);
296 297 298
		if (nt == NULL) {
			release_buffer(ret1);
			release_buffer(ret2);
Deucе's avatar
Deucе committed
299
			return NULL;
300
		}
Deucе's avatar
Deucе committed
301 302
		ret1->data = nt;
		ret1->sz = needsz;
Deucе's avatar
Deucе committed
303
	}
Deucе's avatar
Deucе committed
304 305 306

	if (needsz > ret2->sz) {
		nt = realloc(ret2->data, needsz);
307 308 309
		if (nt == NULL) {
			release_buffer(ret1);
			release_buffer(ret2);
Deucе's avatar
Deucе committed
310
			return NULL;
311
		}
Deucе's avatar
Deucе committed
312 313
		ret2->data = nt;
		ret2->sz = needsz;
Deucе's avatar
Deucе committed
314 315
	}

Deucе's avatar
Deucе committed
316 317 318 319 320 321 322 323
	// Copy rect into first buffer
	// TODO: Unify bitmap rects and scaling buffers so this can just whomp on over.
	csrc = ret1;
	ctarget = ret2;
	memcpy(csrc->data, rect->data, rect->rect.width * rect->rect.height * sizeof(rect->data[0]));
	csrc->w = rect->rect.width;
	csrc->h = rect->rect.height;

324 325
#if 0
fprintf(stderr, "Plan:\n"
326
"start:       %dx%d\n"
327 328 329 330 331 332 333 334
"pointymulti: %d\n"
"pointy5:     %d\n"
"pointy3:     %d\n"
"xBR4:        %d\n"
"xBR2:        %d\n"
"Multiply:    %dx%d\n"
"hinterp:     %zu -> %zu\n"
"winterp:     %zu -> %zu\n",
335
csrc->w, csrc->h, pointymult, pointy5, pointy3, xbr4, xbr2, xmult, ymult, csrc->h * yscale, fheight, csrc->w * xscale, fwidth);
336
#endif
Deucе's avatar
Deucе committed
337
	// And scale...
338 339 340 341 342 343 344 345 346 347 348 349
	if (ymult != 1 || xmult != 1) {
		multiply_scale(csrc->data, ctarget->data, csrc->w, csrc->h, xmult, ymult);
		ctarget->w = csrc->w * xmult;
		ctarget->h = csrc->h * ymult;
		ymult = 1;
		xmult = 1;
		csrc = ctarget;
		if (ctarget == ret1)
			ctarget = ret2;
		else
			ctarget = ret1;
	}
Deucе's avatar
Deucе committed
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
	if (pointymult > 1 && pointymult & 1) {
		pointy_scale_odd(csrc->data, ctarget->data, csrc->w, csrc->h, pointymult);
		ctarget->w = csrc->w * pointymult;
		ctarget->h = csrc->h * pointymult;
		pointymult = 1;
		csrc = ctarget;
		if (ctarget == ret1)
			ctarget = ret2;
		else
			ctarget = ret1;
	}
	while (pointy5 > 0) {
		pointy_scale5(csrc->data, ctarget->data, csrc->w, csrc->h);
		pointy5--;
		ctarget->w = csrc->w * 5;
		ctarget->h = csrc->h * 5;
		csrc = ctarget;
		if (ctarget == ret1)
			ctarget = ret2;
		else
			ctarget = ret1;
	}
	while (pointy3 > 0) {
		pointy_scale3(csrc->data, ctarget->data, csrc->w, csrc->h);
		pointy3--;
		ctarget->w = csrc->w * 3;
		ctarget->h = csrc->h * 3;
		csrc = ctarget;
		if (ctarget == ret1)
			ctarget = ret2;
		else
			ctarget = ret1;
	}
Deucе's avatar
Deucе committed
383
	while (xbr4 > 0) {
Deucе's avatar
Deucе committed
384
		xbr_filter(csrc->data, ctarget->data, csrc->w, csrc->h, 4);
Deucе's avatar
Deucе committed
385
		xbr4--;
Deucе's avatar
Deucе committed
386 387
		ctarget->w = csrc->w * 4;
		ctarget->h = csrc->h * 4;
Deucе's avatar
Deucе committed
388
		csrc = ctarget;
Deucе's avatar
Deucе committed
389 390
		if (ctarget == ret1)
			ctarget = ret2;
Deucе's avatar
Deucе committed
391
		else
Deucе's avatar
Deucе committed
392
			ctarget = ret1;
Deucе's avatar
Deucе committed
393 394
	}
	while (xbr2 > 0) {
Deucе's avatar
Deucе committed
395
		xbr_filter(csrc->data, ctarget->data, csrc->w, csrc->h, 2);
Deucе's avatar
Deucе committed
396
		xbr2--;
Deucе's avatar
Deucе committed
397 398
		ctarget->w = csrc->w * 2;
		ctarget->h = csrc->h * 2;
Deucе's avatar
Deucе committed
399
		csrc = ctarget;
Deucе's avatar
Deucе committed
400 401
		if (ctarget == ret1)
			ctarget = ret2;
Deucе's avatar
Deucе committed
402
		else
Deucе's avatar
Deucе committed
403
			ctarget = ret1;
Deucе's avatar
Deucе committed
404
	}
Deucе's avatar
Deucе committed
405 406

	// And finally, interpolate if needed
Deucе's avatar
Deucе committed
407
	if (fheight != csrc->h) {
Deucе's avatar
Deucе committed
408 409 410 411 412 413 414 415 416 417
		interpolate_height(csrc->data, ctarget->data, csrc->w, csrc->h, fheight);
		ctarget->h = fheight;
		ctarget->w = csrc->w;
		csrc = ctarget;
		if (ctarget == ret1)
			ctarget = ret2;
		else
			ctarget = ret1;
	}

Deucе's avatar
Deucе committed
418
	if (fwidth != csrc->w) {
Deucе's avatar
Deucе committed
419 420 421 422 423 424 425 426 427 428
		interpolate_width(csrc->data, ctarget->data, csrc->w, csrc->h, fwidth);
		ctarget->h = csrc->h;
		ctarget->w = fwidth;
		csrc = ctarget;
		if (ctarget == ret1)
			ctarget = ret2;
		else
			ctarget = ret1;
	}

Deucе's avatar
Deucе committed
429
	release_buffer(ctarget);
Deucе's avatar
Deucе committed
430 431 432
	return csrc;
}

Deucе's avatar
Deucе committed
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
static void
pointy_scale_odd(uint32_t* src, uint32_t* dest, int width, int height, int mult)
{
	int x, y;
	uint32_t* s;
	uint32_t* d;
	int prevline, prevcol, nextline, nextcol;
	int i, j;
	int mid = mult / 2;
	int multoff = mult - 1;
	int dline = width * mult;
	int dbott;
	int dstripe = dline * mult;

	s = src;
	d = dest;
	prevline = 0;
	nextline = width;
	for (y = 0; y < height; y++) {
		if (y == height - 1)
			nextline = 0;
		prevcol = 0;
		nextcol = 1;
		for (x = 0; x < width; x++) {
			if (x == width - 1)
				nextcol = 0;

			for (i = 0; i < mid; i++) {
				d = &dest[dstripe * y + dline * i + x * mult];
				dbott = dline * (multoff - i * 2);

				for (j = 0; j < mid - i; j++) {
					if (s[prevline + prevcol] == s[0]) {
						d[j] = s[0];
					}
					else if (s[prevline] == s[prevcol]) {
						d[j] = s[prevcol];
					}
					else {
						d[j] = s[0];
					}

					if (s[prevline + nextcol] == s[0]) {
						d[multoff - j] = s[0];
					}
					else if (s[prevline] == s[nextcol]) {
						d[multoff - j] = s[nextcol];
					}
					else {
						d[multoff - j] = s[0];
					}

					if (s[prevcol + nextline] == s[0]) {
						d[dbott + j] = s[0];
					}
					else if(s[prevcol] == s[nextline]) {
						d[dbott + j] = s[prevcol];
					}
					else {
						d[dbott + j] = s[0];
					}

					if (s[nextcol + nextline] == s[0]) {
						d[dbott + multoff - j] = s[0];
					}
					else if (s[nextcol] == s[nextline]) {
						d[dbott + multoff - j] = s[nextcol];
					}
					else {
						d[dbott + multoff - j] = s[0];
					}
				}

				// And the rest is always kept the same
				for (; j < mid; j++) {
					d[j] = s[0];
					d[multoff - j] = s[0];
					d[dbott + j] = s[0];
					d[dbott + multoff - j] = s[0];
				}

				// And the middle dot.
				d[j] = s[0];
				d[dbott + j] = s[0];
			}

			d = &dest[dstripe * y + dline * i + x * mult];

			for (j = 0; j < mid; j++) {
				d[j] = s[0];
				d[multoff - j] = s[0];
			}
			d[j] = s[0];

			s++;

			if (x == 0)
				prevcol = -1;
		}
		if (y == 0)
			prevline = -width;
	}
}

Deucе's avatar
Deucе committed
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
static void
pointy_scale5(uint32_t* src, uint32_t* dest, int width, int height)
{
	int x, y;
	uint32_t* s;
	uint32_t* d;
	int w5 = width * 5;
	int w10 = width * 10;
	int w15 = width * 15;
	int w20 = width * 20;
	int prevline, prevcol, nextline, nextcol;

	s = src;
	d = dest;
	prevline = 0;
	nextline = width;
	for (y = 0; y < height; y++) {
		if (y == height - 1)
			nextline = 0;
		prevcol = 0;
		nextcol = 1;
		for (x = 0; x < width; x++) {
			if (x == width - 1)
				nextcol = 0;

			if (s[prevline + prevcol] == s[0]) {
				d[0] = s[0];
				d[1] = s[0];
				d[w5] = s[0];
			}
			else if (s[prevcol] == s[prevline]) {
				d[0] = s[prevcol];
				d[1] = s[prevcol];
				d[w5] = s[prevcol];
			}
			else {
				d[0] = *s;
				d[1] = *s;
				d[w5] = *s;
			}

			// Top-middle stays OG.
			d[2] = *s;
			d[w5+1] = *s;
			d[w5+2] = *s;
			d[w5+3] = *s;

			// And so on around the outside (round the outside)
			if (s[prevline + nextcol] == s[0]) {
				d[3] = s[0];
				d[4] = s[0];
				d[w5 + 4] = s[0];
			}
			else if (s[nextcol] == s[prevline]) {
				d[3] = s[nextcol];
				d[4] = s[nextcol];
				d[w5 + 4] = s[nextcol];
			}
			else {
				d[3] = s[0];
				d[4] = s[0];
				d[w5 + 4] = s[0];
			}

			d[w10] = *s;
			d[w10+1] = *s;
			d[w10+2] = *s;
			d[w10+3] = *s;
			d[w10+4] = *s;

			if (s[prevcol + nextline] == s[0]) {
				d[w15] = s[0];
				d[w20] = s[0];
				d[w20 + 1] = s[0];
			}
			else if(s[prevcol] == s[nextline]) {
				d[w15] = s[prevcol];
				d[w20] = s[prevcol];
				d[w20 + 1] = s[prevcol];
			}
			else {
				d[w15] = s[0];
				d[w20] = s[0];
				d[w20 + 1] = s[0];
			}

			d[w15 + 1] = *s;
			d[w15 + 2] = *s;
			d[w15 + 3] = *s;
			d[w20 + 2] = *s;

			if (s[nextcol + nextline] == s[0]) {
				d[w15 + 4] = s[0];
				d[w20 + 3] = s[0];
				d[w20 + 4] = s[0];
			}
			else if (s[nextcol] == s[nextline]) {
				d[w15 + 4] = s[nextcol];
				d[w20 + 3] = s[nextcol];
				d[w20 + 4] = s[nextcol];
			}
			else {
				d[w15 + 4] = s[0];
				d[w20 + 3] = s[0];
				d[w20 + 4] = s[0];
			}

			d += 5;
			s++;

			if (x == 0)
				prevcol = -1;
		}
		d += w20;
		if (y == 0)
			prevline = -width;
	}
}

static void
pointy_scale3(uint32_t* src, uint32_t* dest, int width, int height)
{
	int x, y;
	uint32_t* s;
	uint32_t* d;
	int w3 = width * 3;
	int w6 = width * 6;
	int prevline, prevcol, nextline, nextcol;

	s = src;
	d = dest;
	prevline = 0;
	nextline = width;
	for (y = 0; y < height; y++) {
		if (y == height - 1)
			nextline = 0;
		prevcol = 0;
		nextcol = 1;
		for (x = 0; x < width; x++) {
			if (x == width - 1)
				nextcol = 0;

			// Top-left is filled if both left and top are the same.
			if (s[prevline + prevcol] == s[0])
				d[0] = s[0];
			else if (s[prevcol] == s[prevline])
				d[0] = s[prevcol];
			else
				d[0] = *s;

			// Top-middle stays OG.
			d[1] = *s;

			// And so on around the outside (round the outside)
			if (s[prevline + nextcol] == s[0])
				d[2] = s[0];
			else if (s[nextcol] == s[prevline])
				d[2] = s[nextcol];
			else
				d[2] = *s;

			d[w3] = *s;
			d[w3 + 1] = *s;
			d[w3 + 2] = *s;

			if (s[prevcol + nextline] == s[0])
				d[w6] = s[0];
			else if(s[prevcol] == s[nextline])
				d[w6] = s[prevcol];
			else
				d[w6] = *s;

			d[w6 + 1] = *s;

			if (s[nextcol + nextline] == s[0])
				d[w6 + 2] = s[0];
			else if (s[nextcol] == s[nextline])
				d[w6 + 2] = s[nextcol];
			else
				d[w6 + 2] = s[0];

			d += 3;
			s++;

			if (x == 0)
				prevcol = -1;
		}
		d += w6;
		if (y == 0)
			prevline = -width;
	}
}
Deucе's avatar
Deucе committed
729

Deucе's avatar
Deucе committed
730 731
static uint32_t
blend(const uint32_t c1, const uint32_t c2, int weight)
Deucе's avatar
Deucе committed
732 733 734
{
	uint8_t yuv1[4];
	uint8_t yuv2[4];
Deucе's avatar
Deucе committed
735 736
	uint8_t yuv3[4];
	const double iw = 256 - weight;
Deucе's avatar
Deucе committed
737 738 739 740

	*(uint32_t *)yuv1 = r2y[c1];
	*(uint32_t *)yuv2 = r2y[c2];
#ifdef __BIG_ENDIAN__
Deucе's avatar
Deucе committed
741 742 743 744
	yuv3[0] = 0;
	yuv3[1] = (yuv1[1] * iw + yuv2[1] * weight) / 256;
	yuv3[2] = (yuv1[2] * iw + yuv2[2] * weight) / 256;
	yuv3[3] = (yuv1[3] * iw + yuv2[3] * weight) / 256;
Deucе's avatar
Deucе committed
745
#else
Deucе's avatar
Deucе committed
746 747 748 749
	yuv3[3] = 0;
	yuv3[2] = (yuv1[2] * iw + yuv2[2] * weight) / 256;
	yuv3[1] = (yuv1[1] * iw + yuv2[1] * weight) / 256;
	yuv3[0] = (yuv1[0] * iw + yuv2[0] * weight) / 256;
Deucе's avatar
Deucе committed
750 751
#endif

Deucе's avatar
Deucе committed
752
	return y2r[*(uint32_t*)yuv3];
Deucе's avatar
Deucе committed
753 754
}

Deucе's avatar
Deucе committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
/*
 * This does non-integer *width* scaling.  It does not scale in the other
 * direction.  This does the interpolation using Y'UV to prevent dimming of
 * pixels.
 */
static void
interpolate_width(uint32_t* src, uint32_t* dst, int width, int height, int newwidth)
{
	int x, y;
	const double mult = (double)width / newwidth;

	for (y = 0; y < height; y++) {
		for (x = 0; x < newwidth; x++) {
			// First, calculate which two pixels this is between.
			const double xpos = mult * x;
			const int xposi = xpos;
			if (x == xpos) {
				// Exact match!
				*dst = src[width * y + x];
			}
			else {
				const double weight = xpos - xposi;
				// Now pick the two pixels
Deucе's avatar
Deucе committed
778
				const uint32_t pix1 = src[y * width + xposi];
Deucе's avatar
Deucе committed
779 780
				uint32_t pix2;
				if (xposi < width - 1)
Deucе's avatar
Deucе committed
781
					pix2 = src[y * width + xposi + 1];
Deucе's avatar
Deucе committed
782
				else
Deucе's avatar
Deucе committed
783
					pix2 = src[y * width + xposi];
Deucе's avatar
Deucе committed
784 785 786 787 788 789 790 791 792 793 794
				if (pix1 == pix2)
					*dst = pix1;
				else {
					*dst = blend(pix1, pix2, weight);
				}
			}
			dst++;
		}
	}
}

Deucе's avatar
Deucе committed
795 796 797 798 799 800 801 802 803 804
/*
 * This does non-integer *height* scaling.  It does not scale in the other
 * direction.  This does the interpolation using Y'UV to prevent dimming of
 * pixels.
 */
static void
interpolate_height(uint32_t* src, uint32_t* dst, int width, int height, int newheight)
{
	int x, y;
	const double mult = (double)height / newheight;
Deucе's avatar
Deucе committed
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
	double ypos = 0;
	int last_yposi = 0;
	int ywn = width;
	static uint32_t *nline = NULL;
	static uint32_t *tline = NULL;
	static size_t nsz = 0;
	static size_t tsz = 0;
	uint32_t *stmp;

	if (nsz < width * 4) {
		stmp = realloc(nline, width * 4);
		if (stmp == NULL)
			goto fail;
		nline = stmp;
		nsz = width * 4;
	}
	if (tsz < width * 4) {
		stmp = realloc(tline, width * 4);
		if (stmp == NULL)
			goto fail;
		tline = stmp;
		tsz = width * 4;
	}
Deucе's avatar
Deucе committed
828

Deucе's avatar
Deucе committed
829 830
	memcpy(tline, src, width * sizeof(*tline));
	memcpy(nline, src + width, width * sizeof(*tline));
Deucе's avatar
Deucе committed
831
	for (y = 0; y < newheight; y++) {
832
		const int yposi = ypos;
Deucе's avatar
Deucе committed
833 834 835 836 837 838 839 840 841 842
		if (yposi != last_yposi) {
			ywn += width;
			last_yposi = yposi;
			stmp = tline;
			tline = nline;
			nline = stmp;
			memcpy(nline, &src[ywn], nsz);
		}
		if (y == ypos || yposi >= height - 1) {
			memcpy(dst, tline, tsz);
843 844 845
			dst += width;
		}
		else {
Deucе's avatar
Deucе committed
846
			const uint8_t weight = ypos * 256;
847
			for (x = 0; x < width; x++) {
Deucе's avatar
Deucе committed
848
				// Now pick the two pixels
Deucе's avatar
Deucе committed
849 850
				const uint32_t pix1 = tline[x];
				const uint32_t pix2 = nline[x];
Deucе's avatar
Deucе committed
851 852
				if (pix1 == pix2)
					*dst = pix1;
853
				else
Deucе's avatar
Deucе committed
854
					*dst = blend(pix1, pix2, weight);
855
				dst++;
Deucе's avatar
Deucе committed
856 857
			}
		}
Deucе's avatar
Deucе committed
858
		ypos += mult;
Deucе's avatar
Deucе committed
859
	}
Deucе's avatar
Deucе committed
860 861 862 863 864 865 866 867 868 869 870

	return;
fail:
	free(nline);
	free(tline);
	nline = NULL;
	tline = NULL;
	nsz = 0;
	tsz = 0;
	memcpy(src, dst, width * height * sizeof(*src));
	fprintf(stderr, "Allocation failure in interpolate_height()!");
Deucе's avatar
Deucе committed
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
}

static void
multiply_scale(uint32_t* src, uint32_t* dst, int width, int height, int xmult, int ymult)
{
	int x, y;
	int mx, my;
	uint32_t* slstart;

	for (y = 0; y < height; y++) {
		slstart = src;
		for (my = 0; my < ymult; my++) {
			src = slstart;
			for (x = 0; x < width; x++) {
				for (mx = 0; mx < xmult; mx++) {
					*dst = *src;
					dst++;
				}
				src++;
			}
		}
	}
}