A little comment about a compilation matter: -lungif parameter is not usually correct...
[fbv.git] / transforms.c
blobcf7624c029d8299b8915b0e0c9ddcfac6d17a241
1 /*
2 fbv -- simple image viewer for the linux framebuffer
3 Copyright (C) 2000, 2001, 2003 Mateusz Golicz
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <assert.h>
23 unsigned char * simple_resize(unsigned char * orgin,int ox,int oy,int dx,int dy)
25 unsigned char *cr,*p,*l;
26 int i,j,k,ip;
27 assert(cr = (unsigned char*) malloc(dx*dy*3));
28 l=cr;
30 for(j=0;j<dy;j++,l+=dx*3)
32 p=orgin+(j*oy/dy*ox*3);
33 for(i=0,k=0;i<dx;i++,k+=3)
35 ip=i*ox/dx*3;
36 l[k]=p[ip];
37 l[k+1]=p[ip+1];
38 l[k+2]=p[ip+2];
41 return(cr);
44 unsigned char * alpha_resize(unsigned char * alpha,int ox,int oy,int dx,int dy)
46 unsigned char *cr,*p,*l;
47 int i,j,k;
48 cr=(unsigned char*) malloc(dx*dy); l=cr;
50 for(j=0;j<dy;j++,l+=dx)
52 p = alpha+(j*oy/dy*ox);
53 for(i=0,k=0;i<dx;i++)
54 l[k++]=p[i*ox/dx];
57 return(cr);
60 unsigned char * color_average_resize(unsigned char * orgin,int ox,int oy,int dx,int dy)
62 unsigned char *cr,*p,*q;
63 int i,j,k,l,xa,xb,ya,yb;
64 int sq,r,g,b;
65 assert(cr=(unsigned char*) malloc(dx*dy*3)); p=cr;
67 for(j=0;j<dy;j++)
69 for(i=0;i<dx;i++,p+=3)
71 xa=i*ox/dx;
72 ya=j*oy/dy;
73 xb=(i+1)*ox/dx; if(xb>=ox) xb=ox-1;
74 yb=(j+1)*oy/dy; if(yb>=oy) yb=oy-1;
75 for(l=ya,r=0,g=0,b=0,sq=0;l<=yb;l++)
77 q=orgin+((l*ox+xa)*3);
78 for(k=xa;k<=xb;k++,q+=3,sq++)
80 r+=q[0]; g+=q[1]; b+=q[2];
83 p[0]=r/sq; p[1]=g/sq; p[2]=b/sq;
86 return(cr);
89 unsigned char * rotate(unsigned char *i, int ox, int oy, int rot)
91 unsigned char * n, * p;
92 int x, y;
93 assert(n = (unsigned char*) malloc(ox * oy * 3));
95 switch(rot)
97 case 1: /* 90 deg right */
98 p = n + (oy - 1) * 3;
99 for(y = 0; y<oy; y++, p -= 3)
101 unsigned char * r = p;
102 for(x = 0; x<ox; x++, r += oy * 3)
104 r[0] = *(i++);
105 r[1] = *(i++);
106 r[2] = *(i++);
109 break;
110 case 2: /* 180 deg */
111 i += ox * oy * 3; p = n;
112 for(y = ox * oy; y > 0; y--)
114 i -= 3;
115 p[0] = i[0];
116 p[1] = i[1];
117 p[2] = i[2];
118 p += 3;
120 break;
121 case 3: /* 90 deg left */
122 p = n;
123 for(y = 0; y<oy; y++, p += 3)
125 unsigned char * r = p + ((ox * 3) * oy);
126 for(x = 0; x<ox; x++)
128 r -= oy * 3;
129 r[0] = *(i++);
130 r[1] = *(i++);
131 r[2] = *(i++);
134 break;
136 return(n);
139 unsigned char * alpha_rotate(unsigned char *i, int ox, int oy, int rot)
141 unsigned char * n, * p;
142 int x, y;
143 assert(n = (unsigned char*) malloc(ox * oy));
145 switch(rot)
147 case 1: /* 90 deg right */
148 p = n + (oy - 1);
149 for(y = 0; y<oy; y++, p --)
151 unsigned char * r = p;
152 for(x = 0; x<ox; x++, r += oy)
153 *r = *(i++);
155 break;
156 case 2: /* 180 deg */
157 i += ox * oy; p = n;
158 for(y = ox * oy; y > 0; y--)
159 *(p++) = *(i--);
160 break;
161 case 3: /* 90 deg left */
162 p = n;
163 for(y = 0; y<oy; y++, p++)
165 unsigned char * r = p + (ox * oy);
166 for(x = 0; x<ox; x++)
168 r -= oy;
169 *r = *(i++);
172 break;
174 return(n);