Recreating my GSoC branch.
[blender.git] / source / blender / imbuf / intern / rotate.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
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.
14  *
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 Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  * rotate.c
28  *
29  * $Id$
30  */
31
32 #include "BLI_blenlib.h"
33 #include  "BKE_utildefines.h"
34
35 #include "imbuf.h"
36 #include "IMB_imbuf_types.h"
37 #include "IMB_imbuf.h"
38
39 #include "IMB_allocimbuf.h"
40
41 void IMB_flipy(struct ImBuf * ibuf)
42 {
43         int x, y;
44
45         if (ibuf == NULL) return;
46
47         if (ibuf->rect) {
48                 unsigned int *top, *bottom, *line;
49
50                 x = ibuf->x;
51                 y = ibuf->y;
52
53                 top = ibuf->rect;
54                 bottom = top + ((y-1) * x);
55                 line= MEM_mallocN(x*sizeof(int), "linebuf");
56         
57                 y >>= 1;
58
59                 for(;y>0;y--) {
60                         memcpy(line, top, x*sizeof(int));
61                         memcpy(top, bottom, x*sizeof(int));
62                         memcpy(bottom, line, x*sizeof(int));
63                         bottom -= x;
64                         top+= x;
65                 }
66
67                 MEM_freeN(line);
68         }
69
70         if (ibuf->rect_float) {
71                 float *topf=NULL, *bottomf=NULL, *linef=NULL;
72
73                 x = ibuf->x;
74                 y = ibuf->y;
75
76                 topf= ibuf->rect_float;
77                 bottomf = topf + 4*((y-1) * x);
78                 linef= MEM_mallocN(4*x*sizeof(float), "linebuff");
79
80                 y >>= 1;
81
82                 for(;y>0;y--) {
83                         memcpy(linef, topf, 4*x*sizeof(float));
84                         memcpy(topf, bottomf, 4*x*sizeof(float));
85                         memcpy(bottomf, linef, 4*x*sizeof(float));
86                         bottomf -= 4*x;
87                         topf+= 4*x;
88                 }
89
90                 MEM_freeN(linef);
91         }
92 }
93
94 void IMB_flipx(struct ImBuf * ibuf)
95 {
96         short x, y, xr, xl, yi;
97         float px_f[4];
98         
99         if (ibuf == NULL) return;
100
101         x = ibuf->x;
102         y = ibuf->y;
103
104         if (ibuf->rect) {
105                 for(yi=y-1;yi>=0;yi--) {
106                         for(xr=x-1, xl=0; xr>=xl; xr--, xl++) {
107                                 SWAP(unsigned int, ibuf->rect[(x*yi)+xr], ibuf->rect[(x*yi)+xl]);
108                         }
109                 }
110         }
111         
112         if (ibuf->rect_float) {
113                 for(yi=y-1;yi>=0;yi--) {
114                         for(xr=x-1, xl=0; xr>=xl; xr--, xl++) {
115                                 memcpy(&px_f, &ibuf->rect_float[((x*yi)+xr)*4], 4*sizeof(float));
116                                 memcpy(&ibuf->rect_float[((x*yi)+xr)*4], &ibuf->rect_float[((x*yi)+xl)*4], 4*sizeof(float));
117                                 memcpy(&ibuf->rect_float[((x*yi)+xl)*4], &px_f, 4*sizeof(float));
118                         }
119                 }
120         }
121 }