merge trunk 17122:17213
[blender.git] / source / blender / imbuf / intern / imageprocess.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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  * This file was moved here from the src/ directory. It is meant to
28  * deal with endianness. It resided in a general blending lib. The
29  * other functions were only used during rendering. This single
30  * function remained. It should probably move to imbuf/intern/util.c,
31  * but we'll keep it here for the time being. (nzc)*/
32
33 /*  imageprocess.c        MIXED MODEL
34  * 
35  *  april 95
36  * 
37  * $Id$
38  */
39
40 #include "IMB_imbuf_types.h"
41 #include "IMB_imbuf.h"
42 #include "math.h"
43
44 /* This define should be relocated to a global header some where  Kent Mein 
45 I stole it from util.h in the plugins api */
46 #define MAX2(x,y)                ( (x)>(y) ? (x) : (y) )
47
48 /* Only this one is used liberally here, and in imbuf */
49 void IMB_convert_rgba_to_abgr(struct ImBuf *ibuf)
50 {
51         int size;
52         unsigned char rt, *cp = (unsigned char *)ibuf->rect;
53         float rtf, *cpf = ibuf->rect_float;
54
55         if (ibuf->rect) {
56                 size = ibuf->x * ibuf->y;
57
58                 while(size-- > 0) {
59                         rt= cp[0];
60                         cp[0]= cp[3];
61                         cp[3]= rt;
62                         rt= cp[1];
63                         cp[1]= cp[2];
64                         cp[2]= rt;
65                         cp+= 4;
66                 }
67         }
68
69         if (ibuf->rect_float) {
70                 size = ibuf->x * ibuf->y;
71
72                 while(size-- > 0) {
73                         rtf= cpf[0];
74                         cpf[0]= cpf[3];
75                         cpf[3]= rtf;
76                         rtf= cpf[1];
77                         cpf[1]= cpf[2];
78                         cpf[2]= rtf;
79                         cpf+= 4;
80                 }
81         }
82 }
83
84 /**************************************************************************
85 *                            INTERPOLATIONS 
86 *
87 * Reference and docs:
88 * http://wiki.blender.org/index.php/User:Damiles#Interpolations_Algorithms
89 ***************************************************************************/
90
91 /* BICUBIC Interpolation functions */
92 /*  More info: http://wiki.blender.org/index.php/User:Damiles#Bicubic_pixel_interpolation
93 */
94 /* function assumes out to be zero'ed, only does RGBA */
95 static float P(float k){
96         float aux;
97         aux=(float)(1.0f/6.0f)*( pow( MAX2(k+2.0f,0) , 3.0f ) - 4.0f * pow( MAX2(k+1.0f,0) , 3.0f ) + 6.0f * pow( MAX2(k,0) , 3.0f ) - 4.0f * pow( MAX2(k-1.0f,0) , 3.0f));
98         return aux ;
99 }
100
101 void bicubic_interpolation(ImBuf *in, ImBuf *out, float x, float y, int xout, int yout)
102 {
103         int i,j,n,m,x1,y1;
104         unsigned char *dataI,*outI;
105         float a,b, outR,outG,outB,outA,*dataF,*outF;
106         int do_rect, do_float;
107
108         if (in == NULL) return;
109         if (in->rect == NULL && in->rect_float == NULL) return;
110
111         do_rect= (out->rect != NULL);
112         do_float= (out->rect_float != NULL);
113
114         i= (int)floor(x);
115         j= (int)floor(y);
116         a= x - i;
117         b= y - j;
118
119         outR= 0.0f;
120         outG= 0.0f;
121         outB= 0.0f;
122         outA= 0.0f;
123         for(n= -1; n<= 2; n++){
124                 for(m= -1; m<= 2; m++){
125                         x1= i+n;
126                         y1= j+m;
127                         if (x1>0 && x1 < in->x && y1>0 && y1<in->y) {
128                                 if (do_float) {
129                                         dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
130                                         outR+= dataF[0] * P(n-a) * P(b-m);
131                                         outG+= dataF[1] * P(n-a) * P(b-m);
132                                         outB+= dataF[2] * P(n-a) * P(b-m);
133                                         outA+= dataF[3] * P(n-a) * P(b-m);
134                                 }
135                                 if (do_rect) {
136                                         dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1;
137                                         outR+= dataI[0] * P(n-a) * P(b-m);
138                                         outG+= dataI[1] * P(n-a) * P(b-m);
139                                         outB+= dataI[2] * P(n-a) * P(b-m);
140                                         outA+= dataI[3] * P(n-a) * P(b-m);
141                                 }
142                         }
143                 }
144         }
145         if (do_rect) {
146                 outI= (unsigned char *)out->rect + out->x * yout * 4 + 4*xout;
147                 outI[0]= (int)outR;
148                 outI[1]= (int)outG;
149                 outI[2]= (int)outB;
150                 outI[3]= (int)outA;
151         }
152         if (do_float) {
153                 outF= (float *)out->rect_float + out->x * yout * 4 + 4*xout;
154                 outF[0]= outR;
155                 outF[1]= outG;
156                 outF[2]= outB;
157                 outF[3]= outA;
158         }
159 }
160
161 /* function assumes out to be zero'ed, only does RGBA */
162 /* BILINEAR INTERPOLATION */
163 void bilinear_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout)
164 {
165         float *row1, *row2, *row3, *row4, a, b, *outF;
166         unsigned char *row1I, *row2I, *row3I, *row4I, *outI;
167         float a_b, ma_b, a_mb, ma_mb;
168         float empty[4]= {0.0f, 0.0f, 0.0f, 0.0f};
169         unsigned char emptyI[4]= {0, 0, 0, 0};
170         int y1, y2, x1, x2;
171         int do_rect, do_float;
172
173         if (in==NULL) return;
174         if (in->rect==NULL && in->rect_float==NULL) return;
175
176         do_rect= (out->rect != NULL);
177         do_float= (out->rect_float != NULL);
178
179         x1= (int)floor(u);
180         x2= (int)ceil(u);
181         y1= (int)floor(v);
182         y2= (int)ceil(v);
183
184         // sample area entirely outside image? 
185         if (x2<0 || x1>in->x-1 || y2<0 || y1>in->y-1) return;
186
187         if (do_rect)
188                 outI=(unsigned char *)out->rect + out->x * yout * 4 + 4*xout;
189         else
190                 outI= NULL;
191         if (do_float)
192                 outF=(float *)out->rect_float + out->x * yout * 4 + 4*xout;
193         else    
194                 outF= NULL;
195
196         if (do_float) {
197                 // sample including outside of edges of image 
198                 if (x1<0 || y1<0) row1= empty;
199                 else row1= (float *)in->rect_float + in->x * y1 * 4 + 4*x1;
200                 
201                 if (x1<0 || y2>in->y-1) row2= empty;
202                 else row2= (float *)in->rect_float + in->x * y2 * 4 + 4*x1;
203                 
204                 if (x2>in->x-1 || y1<0) row3= empty;
205                 else row3= (float *)in->rect_float + in->x * y1 * 4 + 4*x2;
206                 
207                 if (x2>in->x-1 || y2>in->y-1) row4= empty;
208                 else row4= (float *)in->rect_float + in->x * y2 * 4 + 4*x2;
209                 
210                 a= u-floor(u);
211                 b= v-floor(v);
212                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
213                 
214                 outF[0]= ma_mb*row1[0] + a_mb*row3[0] + ma_b*row2[0]+ a_b*row4[0];
215                 outF[1]= ma_mb*row1[1] + a_mb*row3[1] + ma_b*row2[1]+ a_b*row4[1];
216                 outF[2]= ma_mb*row1[2] + a_mb*row3[2] + ma_b*row2[2]+ a_b*row4[2];
217                 outF[3]= ma_mb*row1[3] + a_mb*row3[3] + ma_b*row2[3]+ a_b*row4[3];
218         }
219         if (do_rect) {
220                 // sample including outside of edges of image 
221                 if (x1<0 || y1<0) row1I= emptyI;
222                 else row1I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
223                 
224                 if (x1<0 || y2>in->y-1) row2I= emptyI;
225                 else row2I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x1;
226                 
227                 if (x2>in->x-1 || y1<0) row3I= emptyI;
228                 else row3I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x2;
229                 
230                 if (x2>in->x-1 || y2>in->y-1) row4I= emptyI;
231                 else row4I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x2;
232                 
233                 a= u-floor(u);
234                 b= v-floor(v);
235                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
236                 
237                 outI[0]= ma_mb*row1I[0] + a_mb*row3I[0] + ma_b*row2I[0]+ a_b*row4I[0];
238                 outI[1]= ma_mb*row1I[1] + a_mb*row3I[1] + ma_b*row2I[1]+ a_b*row4I[1];
239                 outI[2]= ma_mb*row1I[2] + a_mb*row3I[2] + ma_b*row2I[2]+ a_b*row4I[2];
240                 outI[3]= ma_mb*row1I[3] + a_mb*row3I[3] + ma_b*row2I[3]+ a_b*row4I[3];
241         }
242 }
243
244 /* function assumes out to be zero'ed, only does RGBA */
245 /* NEAREST INTERPOLATION */
246 void neareast_interpolation(ImBuf *in, ImBuf *out, float u, float v,int xout, int yout)
247 {
248         float *outF,*dataF;
249         unsigned char *dataI,*outI;
250         int y1, x1;
251         int do_rect, do_float;
252
253         if (in==NULL) return;
254         if (in->rect==NULL && in->rect_float==NULL) return;
255
256         do_rect= (out->rect != NULL);
257         do_float= (out->rect_float != NULL);
258
259         x1= (int)(u);
260         y1= (int)(v);
261
262         if (do_rect)
263                 outI=(unsigned char *)out->rect + out->x * yout * 4 + 4*xout;
264         else
265                 outI= NULL;
266         if (do_float)
267                 outF=(float *)out->rect_float + out->x * yout * 4 + 4*xout;
268         else
269                 outF= NULL;
270
271         // sample area entirely outside image? 
272         if (x1<0 || x1>in->x-1 || y1<0 || y1>in->y-1) return;
273         
274         // sample including outside of edges of image 
275         if (x1<0 || y1<0) {
276                 if (do_rect) {
277                         outI[0]= 0;
278                         outI[1]= 0;
279                         outI[2]= 0;
280                         outI[3]= 0;
281                 }
282                 if (do_float) {
283                         outF[0]= 0.0f;
284                         outF[1]= 0.0f;
285                         outF[2]= 0.0f;
286                         outF[3]= 0.0f;
287                 }
288         } else {
289                 dataI= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
290                 if (do_rect) {
291                         outI[0]= dataI[0];
292                         outI[1]= dataI[1];
293                         outI[2]= dataI[2];
294                         outI[3]= dataI[3];
295                 }
296                 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
297                 if (do_float) {
298                         outF[0]= dataF[0];
299                         outF[1]= dataF[1];
300                         outF[2]= dataF[2];
301                         outF[3]= dataF[3];
302                 }
303         }       
304 }