-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize.c
96 lines (92 loc) · 2.2 KB
/
resize.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
/*
* sysinfo
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
unsigned char * color_average_resize(unsigned char * orgin, int ox, int oy, int dx, int dy, int alpha)
{
unsigned char *cr,*p,*q;
int i,j,k,l,ya,yb;
int sq,r,g,b,a;
cr = (unsigned char*) malloc(dx * dy * ((alpha) ? 4 : 3));
if(cr==NULL)
{
printf("Error: malloc\n");
return(orgin);
}
p=cr;
int xa_v[dx];
for(i=0;i<dx;i++)
xa_v[i] = i*ox/dx;
int xb_v[dx+1];
for(i=0;i<dx;i++)
{
xb_v[i]= (i+1)*ox/dx;
if(xb_v[i]>=ox)
xb_v[i]=ox-1;
}
if (alpha)
{
for(j=0;j<dy;j++)
{
ya= j*oy/dy;
yb= (j+1)*oy/dy; if(yb>=oy) yb=oy-1;
for(i=0;i<dx;i++,p+=4)
{
for(l=ya,r=0,g=0,b=0,a=0,sq=0;l<=yb;l++)
{
q=orgin+((l*ox+xa_v[i])*4);
for(k=xa_v[i];k<=xb_v[i];k++,q+=4,sq++)
{
r+=q[0]; g+=q[1]; b+=q[2]; a+=q[3];
}
}
int sq_tmp = sq ? sq : 1;//avoid division by zero
p[0]= (uint8_t)(r/sq_tmp);
p[1]= (uint8_t)(g/sq_tmp);
p[2]= (uint8_t)(b/sq_tmp);
p[3]= (uint8_t)(a/sq_tmp);
}
}
}else
{
for(j=0;j<dy;j++)
{
ya= j*oy/dy;
yb= (j+1)*oy/dy; if(yb>=oy) yb=oy-1;
for(i=0;i<dx;i++,p+=3)
{
for(l=ya,r=0,g=0,b=0,sq=0;l<=yb;l++)
{
q=orgin+((l*ox+xa_v[i])*3);
for(k=xa_v[i];k<=xb_v[i];k++,q+=3,sq++)
{
r+=q[0]; g+=q[1]; b+=q[2];
}
}
int sq_tmp = sq ? sq : 1;//avoid division by zero
p[0]= (uint8_t)(r/sq_tmp);
p[1]= (uint8_t)(g/sq_tmp);
p[2]= (uint8_t)(b/sq_tmp);
}
}
}
free(orgin);
return(cr);
}