Merged changes in the trunk up to revision 41368.
[blender.git] / source / blender / imbuf / intern / rotate.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  * rotate.c
27  *
28  */
29
30 /** \file blender/imbuf/intern/rotate.c
31  *  \ingroup imbuf
32  */
33
34
35 #include "BLI_blenlib.h"
36 #include "BLI_utildefines.h"
37
38
39 #include "MEM_guardedalloc.h"
40
41 #include "imbuf.h"
42 #include "IMB_imbuf_types.h"
43 #include "IMB_imbuf.h"
44
45 #include "IMB_allocimbuf.h"
46
47 void IMB_flipy(struct ImBuf * ibuf)
48 {
49         int x, y;
50
51         if (ibuf == NULL) return;
52
53         if (ibuf->rect) {
54                 unsigned int *top, *bottom, *line;
55
56                 x = ibuf->x;
57                 y = ibuf->y;
58
59                 top = ibuf->rect;
60                 bottom = top + ((y-1) * x);
61                 line= MEM_mallocN(x*sizeof(int), "linebuf");
62         
63                 y >>= 1;
64
65                 for(;y>0;y--) {
66                         memcpy(line, top, x*sizeof(int));
67                         memcpy(top, bottom, x*sizeof(int));
68                         memcpy(bottom, line, x*sizeof(int));
69                         bottom -= x;
70                         top+= x;
71                 }
72
73                 MEM_freeN(line);
74         }
75
76         if (ibuf->rect_float) {
77                 float *topf=NULL, *bottomf=NULL, *linef=NULL;
78
79                 x = ibuf->x;
80                 y = ibuf->y;
81
82                 topf= ibuf->rect_float;
83                 bottomf = topf + 4*((y-1) * x);
84                 linef= MEM_mallocN(4*x*sizeof(float), "linebuff");
85
86                 y >>= 1;
87
88                 for(;y>0;y--) {
89                         memcpy(linef, topf, 4*x*sizeof(float));
90                         memcpy(topf, bottomf, 4*x*sizeof(float));
91                         memcpy(bottomf, linef, 4*x*sizeof(float));
92                         bottomf -= 4*x;
93                         topf+= 4*x;
94                 }
95
96                 MEM_freeN(linef);
97         }
98 }
99
100 void IMB_flipx(struct ImBuf * ibuf)
101 {
102         int x, y, xr, xl, yi;
103         float px_f[4];
104         
105         if (ibuf == NULL) return;
106
107         x = ibuf->x;
108         y = ibuf->y;
109
110         if (ibuf->rect) {
111                 for(yi=y-1;yi>=0;yi--) {
112                         for(xr=x-1, xl=0; xr>=xl; xr--, xl++) {
113                                 SWAP(unsigned int, ibuf->rect[(x*yi)+xr], ibuf->rect[(x*yi)+xl]);
114                         }
115                 }
116         }
117         
118         if (ibuf->rect_float) {
119                 for(yi=y-1;yi>=0;yi--) {
120                         for(xr=x-1, xl=0; xr>=xl; xr--, xl++) {
121                                 memcpy(&px_f, &ibuf->rect_float[((x*yi)+xr)*4], 4*sizeof(float));
122                                 memcpy(&ibuf->rect_float[((x*yi)+xr)*4], &ibuf->rect_float[((x*yi)+xl)*4], 4*sizeof(float));
123                                 memcpy(&ibuf->rect_float[((x*yi)+xl)*4], &px_f, 4*sizeof(float));
124                         }
125                 }
126         }
127 }