aboutsummaryrefslogtreecommitdiffstats
path: root/src/scale.c
blob: 19186dc877e744a27f4e58f5a259acfc02b29904 (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
#include <stdint.h>
#include <sys/time.h>
#include "meh.h"

#define TDEBUG 0

#define GETVAL0(x, c) ((ibuf[(x) * 3 + (c)] * (ur) + ibuf[(x) * 3 + 3+(c)] * (u)) * (vr) >> 20)
#define GETVAL(x, c) (( \
			( \
				ibuf[(x) * 3 + (c)] * (ur) + \
				ibuf[(x) * 3 + 3+(c)] * (u) \
			) * (vr) + \
			( \
				(ibufn[(x) * 3 + (c)]) * (ur) + \
				(ibufn[(x) * 3 + 3+(c)]) * (u)\
			) * (v)) >> 20)


#if TDEBUG
struct timeval t0;
struct timeval t1;
#endif

void scale(struct image *img, XImage *ximg){
	int x, y;
	unsigned char * __restrict__ ibuf;
	unsigned char * __restrict__ ibufn;
	unsigned char * const bufend = &img->buf[img->bufwidth * img->bufheight * 3];
	char* __restrict__ newBuf = ximg->data;
	unsigned int jdy = ximg->bytes_per_line / 4 - ximg->width;
	unsigned int dy = img->bufwidth * 3;

#if TDEBUG
	gettimeofday(&t0, NULL);
#endif

	unsigned int bufxs[ximg->width];
	unsigned int us[ximg->width];
	{
		unsigned int dx = (img->bufwidth << 10) / ximg->width;
		unsigned int bufx = img->bufwidth / ximg->width;
		for(x = 0; x < ximg->width; x++){
			bufxs[x] = bufx >> 10;
			us[x] = (bufx & 1023);
			bufx += dx;
		}
	}
	y = 0;
	ibuf = img->buf;
	ibufn = img->buf + dy;
	for(; y < ximg->height; y++){
		unsigned int bufy = (y << 10) * img->bufheight / ximg->height;
		unsigned int v = (bufy & 1023);
		unsigned int vr = 1023^(bufy & 1023);
		ibuf = &img->buf[y * img->bufheight / ximg->height * img->bufwidth * 3];
		ibufn = ibuf + dy;
		if(ibufn >= bufend){
			for(x = 0; x < ximg->width; x++){
				unsigned int bufx = bufxs[x];
				unsigned int u = us[x];
				unsigned int ur = 1023^u;

				*newBuf++ = GETVAL0(bufx, 2);
				*newBuf++ = GETVAL0(bufx, 1);
				*newBuf++ = GETVAL0(bufx, 0);
				newBuf++;
			}
		}else{
			for(x = 0; x < ximg->width; x++){
				unsigned int bufx = bufxs[x];
				unsigned int u = us[x];
				unsigned int ur = 1023^u;

				*newBuf++ = GETVAL(bufx, 2);
				*newBuf++ = GETVAL(bufx, 1);
				*newBuf++ = GETVAL(bufx, 0);
				newBuf++;
			}
		}
		newBuf += jdy;
	}

#if TDEBUG
	gettimeofday(&t1, NULL);
	printf("%li x100us\n", ((t1.tv_sec - t0.tv_sec) * 1000000 + t1.tv_usec - t0.tv_usec) / 100);
#endif
}

void linearscale(struct image *img, XImage *ximg){
	int x, y;
	unsigned char * __restrict__ ibuf;
	char* __restrict__ newBuf = ximg->data;
	unsigned int jdy = ximg->bytes_per_line / 4 - ximg->width;
	unsigned int dx = (img->bufwidth << 10) / ximg->width;

	struct timeval t0;
	struct timeval t1;
	gettimeofday(&t0, NULL);

	for(y = 0; y < ximg->height; y++){
		unsigned int bufx = img->bufwidth / ximg->width;
		ibuf = &img->buf[y * img->bufheight / ximg->height * img->bufwidth * 3];

		for(x = 0; x < ximg->width; x++){
			*newBuf++ = (ibuf[(bufx >> 10)*3+2]);
			*newBuf++ = (ibuf[(bufx >> 10)*3+1]);
			*newBuf++ = (ibuf[(bufx >> 10)*3+0]);
			newBuf++;
			bufx += dx;
		}
		newBuf += jdy;
	}

#if TDEBUG
	gettimeofday(&t1, NULL);
	printf("%li x100us\n", ((t1.tv_sec - t0.tv_sec) * 1000000 + t1.tv_usec - t0.tv_usec) / 100);
#endif
}