svn merge -r 31314:31370 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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 #include "MEM_guardedalloc.h"
35
36 #include "imbuf.h"
37 #include "IMB_imbuf_types.h"
38 #include "IMB_imbuf.h"
39
40 #include "IMB_allocimbuf.h"
41
42 void IMB_flipy(struct ImBuf * ibuf)
43 {
44         int x, y;
45
46         if (ibuf == NULL) return;
47
48         if (ibuf->rect) {
49                 unsigned int *top, *bottom, *line;
50
51                 x = ibuf->x;
52                 y = ibuf->y;
53
54                 top = ibuf->rect;
55                 bottom = top + ((y-1) * x);
56                 line= MEM_mallocN(x*sizeof(int), "linebuf");
57         
58                 y >>= 1;
59
60                 for(;y>0;y--) {
61                         memcpy(line, top, x*sizeof(int));
62                         memcpy(top, bottom, x*sizeof(int));
63                         memcpy(bottom, line, x*sizeof(int));
64                         bottom -= x;
65                         top+= x;
66                 }
67
68                 MEM_freeN(line);
69         }
70
71         if (ibuf->rect_float) {
72                 float *topf=NULL, *bottomf=NULL, *linef=NULL;
73
74                 x = ibuf->x;
75                 y = ibuf->y;
76
77                 topf= ibuf->rect_float;
78                 bottomf = topf + 4*((y-1) * x);
79                 linef= MEM_mallocN(4*x*sizeof(float), "linebuff");
80
81                 y >>= 1;
82
83                 for(;y>0;y--) {
84                         memcpy(linef, topf, 4*x*sizeof(float));
85                         memcpy(topf, bottomf, 4*x*sizeof(float));
86                         memcpy(bottomf, linef, 4*x*sizeof(float));
87                         bottomf -= 4*x;
88                         topf+= 4*x;
89                 }
90
91                 MEM_freeN(linef);
92         }
93 }
94
95 void IMB_flipx(struct ImBuf * ibuf)
96 {
97         short x, y, xr, xl, yi;
98         float px_f[4];
99         
100         if (ibuf == NULL) return;
101
102         x = ibuf->x;
103         y = ibuf->y;
104
105         if (ibuf->rect) {
106                 for(yi=y-1;yi>=0;yi--) {
107                         for(xr=x-1, xl=0; xr>=xl; xr--, xl++) {
108                                 SWAP(unsigned int, ibuf->rect[(x*yi)+xr], ibuf->rect[(x*yi)+xl]);
109                         }
110                 }
111         }
112         
113         if (ibuf->rect_float) {
114                 for(yi=y-1;yi>=0;yi--) {
115                         for(xr=x-1, xl=0; xr>=xl; xr--, xl++) {
116                                 memcpy(&px_f, &ibuf->rect_float[((x*yi)+xr)*4], 4*sizeof(float));
117                                 memcpy(&ibuf->rect_float[((x*yi)+xr)*4], &ibuf->rect_float[((x*yi)+xl)*4], 4*sizeof(float));
118                                 memcpy(&ibuf->rect_float[((x*yi)+xl)*4], &px_f, 4*sizeof(float));
119                         }
120                 }
121         }
122 }