-
Notifications
You must be signed in to change notification settings - Fork 40
/
transforms.c
159 lines (148 loc) · 2.87 KB
/
transforms.c
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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
unsigned char * simple_resize(unsigned char * orgin,
int ox, int oy, int dx, int dy)
{
unsigned char *cr, *p, *l;
int i, j, k, ip;
assert(cr = (unsigned char*)malloc(dx*dy*3));
l = cr;
for(j=0; j<dy; j++, l+=dx*3)
{
p = orgin + (j * oy/dy * ox * 3);
for(i=0, k=0; i<dx; i++, k+=3)
{
ip = i * ox/dx * 3;
l[k] = p[ip];
l[k+1] = p[ip+1];
l[k+2] = p[ip+2];
}
}
return cr;
}
unsigned char * alpha_resize(unsigned char * alpha, int ox, int oy, int dx, int dy)
{
unsigned char *cr,*p,*l;
int i,j,k;
assert(cr = (unsigned char*)malloc(dx*dy));
l = cr;
for(j=0; j<dy; j++,l+=dx)
{
p = alpha + (j * oy/dy * ox);
for(i=0,k=0; i<dx; i++)
l[k++] = p[i*ox/dx];
}
return cr;
}
unsigned char * color_average_resize(unsigned char * orgin, int ox, int oy, int dx, int dy)
{
unsigned char *cr,*p,*q;
int i,j,k,l,xa,xb,ya,yb;
int sq,r,g,b;
assert(cr=(unsigned char*) malloc(dx*dy*3)); p=cr;
for(j=0;j<dy;j++)
{
for(i=0;i<dx;i++,p+=3)
{
xa=i*ox/dx;
ya=j*oy/dy;
xb=(i+1)*ox/dx; if(xb>=ox) xb=ox-1;
yb=(j+1)*oy/dy; if(yb>=oy) yb=oy-1;
for(l=ya,r=0,g=0,b=0,sq=0;l<=yb;l++)
{
q=orgin+((l*ox+xa)*3);
for(k=xa;k<=xb;k++,q+=3,sq++)
{
r+=q[0]; g+=q[1]; b+=q[2];
}
}
p[0]=r/sq; p[1]=g/sq; p[2]=b/sq;
}
}
return cr;
}
unsigned char * rotate(unsigned char *i, int ox, int oy, int rot)
{
unsigned char * n, * p;
int x, y;
assert(n = (unsigned char*) malloc(ox * oy * 3));
switch(rot)
{
case 1: /* 90 deg right */
p = n + (oy - 1) * 3;
for(y = 0; y<oy; y++, p -= 3)
{
unsigned char * r = p;
for(x = 0; x<ox; x++, r += oy * 3)
{
r[0] = *(i++);
r[1] = *(i++);
r[2] = *(i++);
}
}
break;
case 2: /* 180 deg */
i += ox * oy * 3; p = n;
for(y = ox * oy; y > 0; y--)
{
i -= 3;
p[0] = i[0];
p[1] = i[1];
p[2] = i[2];
p += 3;
}
break;
case 3: /* 90 deg left */
p = n;
for(y = 0; y<oy; y++, p += 3)
{
unsigned char * r = p + ((ox * 3) * oy);
for(x = 0; x<ox; x++)
{
r -= oy * 3;
r[0] = *(i++);
r[1] = *(i++);
r[2] = *(i++);
}
}
break;
}
return n;
}
unsigned char * alpha_rotate(unsigned char *i, int ox, int oy, int rot)
{
unsigned char * n, * p;
int x, y;
assert(n = (unsigned char*) malloc(ox * oy));
switch(rot)
{
case 1: /* 90 deg right */
p = n + (oy - 1);
for(y = 0; y<oy; y++, p --)
{
unsigned char * r = p;
for(x = 0; x<ox; x++, r += oy)
*r = *(i++);
}
break;
case 2: /* 180 deg */
i += ox * oy; p = n;
for(y = ox * oy; y > 0; y--)
*(p++) = *(i--);
break;
case 3: /* 90 deg left */
p = n;
for(y = 0; y<oy; y++, p++)
{
unsigned char * r = p + (ox * oy);
for(x = 0; x<ox; x++)
{
r -= oy;
*r = *(i++);
}
}
break;
}
return n;
}