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