bicubic_interpolation function was re-calculating a variable it didnt need to - ...
[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         return (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));
97 }
98
99 void bicubic_interpolation(ImBuf *in, ImBuf *out, float x, float y, int xout, int yout)
100 {
101         int i,j,n,m,x1,y1;
102         unsigned char *dataI,*outI;
103         float a,b,w,wx,wy[4], outR,outG,outB,outA,*dataF,*outF;
104         int do_rect, do_float;
105
106         if (in == NULL) return;
107         if (in->rect == NULL && in->rect_float == NULL) return;
108
109         do_rect= (out->rect != NULL);
110         do_float= (out->rect_float != NULL);
111
112         i= (int)floor(x);
113         j= (int)floor(y);
114         a= x - i;
115         b= y - j;
116
117         outR= 0.0f;
118         outG= 0.0f;
119         outB= 0.0f;
120         outA= 0.0f;
121         
122         /* avoid calling multiple times */
123         wy[0] = P(b-(-1));
124         wy[1] = P(b-  0);
125         wy[2] = P(b-  1);
126         wy[3] = P(b-  2);
127         
128         for(n= -1; n<= 2; n++){
129                 x1= i+n;
130                 if (x1>0 && x1 < in->x) {
131                         wx = P(n-a);
132                         for(m= -1; m<= 2; m++){
133                                 y1= j+m;
134                                 if (y1>0 && y1<in->y) {
135                                         /* normally we could do this */
136                                         /* w = P(n-a) * P(b-m); */
137                                         /* except that would call P() 16 times per pixel therefor pow() 64 times, better precalc these */
138                                         w = wx * wy[m+1];
139                                         
140                                         if (do_float) {
141                                                 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
142                                                 outR+= dataF[0] * w;
143                                                 outG+= dataF[1] * w;
144                                                 outB+= dataF[2] * w;
145                                                 outA+= dataF[3] * w;
146                                         }
147                                         if (do_rect) {
148                                                 dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1;
149                                                 outR+= dataI[0] * w;
150                                                 outG+= dataI[1] * w;
151                                                 outB+= dataI[2] * w;
152                                                 outA+= dataI[3] * w;
153                                         }
154                                 }
155                         }
156                 }
157         }
158         if (do_rect) {
159                 outI= (unsigned char *)out->rect + out->x * yout * 4 + 4*xout;
160                 outI[0]= (int)outR;
161                 outI[1]= (int)outG;
162                 outI[2]= (int)outB;
163                 outI[3]= (int)outA;
164         }
165         if (do_float) {
166                 outF= (float *)out->rect_float + out->x * yout * 4 + 4*xout;
167                 outF[0]= outR;
168                 outF[1]= outG;
169                 outF[2]= outB;
170                 outF[3]= outA;
171         }
172 }
173
174 /* function assumes out to be zero'ed, only does RGBA */
175 /* BILINEAR INTERPOLATION */
176 void bilinear_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout)
177 {
178         float *row1, *row2, *row3, *row4, a, b, *outF;
179         unsigned char *row1I, *row2I, *row3I, *row4I, *outI;
180         float a_b, ma_b, a_mb, ma_mb;
181         float empty[4]= {0.0f, 0.0f, 0.0f, 0.0f};
182         unsigned char emptyI[4]= {0, 0, 0, 0};
183         int y1, y2, x1, x2;
184         int do_rect, do_float;
185
186         if (in==NULL) return;
187         if (in->rect==NULL && in->rect_float==NULL) return;
188
189         do_rect= (out->rect != NULL);
190         do_float= (out->rect_float != NULL);
191
192         x1= (int)floor(u);
193         x2= (int)ceil(u);
194         y1= (int)floor(v);
195         y2= (int)ceil(v);
196
197         // sample area entirely outside image? 
198         if (x2<0 || x1>in->x-1 || y2<0 || y1>in->y-1) return;
199
200         if (do_rect)
201                 outI=(unsigned char *)out->rect + out->x * yout * 4 + 4*xout;
202         else
203                 outI= NULL;
204         if (do_float)
205                 outF=(float *)out->rect_float + out->x * yout * 4 + 4*xout;
206         else    
207                 outF= NULL;
208
209         if (do_float) {
210                 // sample including outside of edges of image 
211                 if (x1<0 || y1<0) row1= empty;
212                 else row1= (float *)in->rect_float + in->x * y1 * 4 + 4*x1;
213                 
214                 if (x1<0 || y2>in->y-1) row2= empty;
215                 else row2= (float *)in->rect_float + in->x * y2 * 4 + 4*x1;
216                 
217                 if (x2>in->x-1 || y1<0) row3= empty;
218                 else row3= (float *)in->rect_float + in->x * y1 * 4 + 4*x2;
219                 
220                 if (x2>in->x-1 || y2>in->y-1) row4= empty;
221                 else row4= (float *)in->rect_float + in->x * y2 * 4 + 4*x2;
222                 
223                 a= u-floor(u);
224                 b= v-floor(v);
225                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
226                 
227                 outF[0]= ma_mb*row1[0] + a_mb*row3[0] + ma_b*row2[0]+ a_b*row4[0];
228                 outF[1]= ma_mb*row1[1] + a_mb*row3[1] + ma_b*row2[1]+ a_b*row4[1];
229                 outF[2]= ma_mb*row1[2] + a_mb*row3[2] + ma_b*row2[2]+ a_b*row4[2];
230                 outF[3]= ma_mb*row1[3] + a_mb*row3[3] + ma_b*row2[3]+ a_b*row4[3];
231         }
232         if (do_rect) {
233                 // sample including outside of edges of image 
234                 if (x1<0 || y1<0) row1I= emptyI;
235                 else row1I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
236                 
237                 if (x1<0 || y2>in->y-1) row2I= emptyI;
238                 else row2I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x1;
239                 
240                 if (x2>in->x-1 || y1<0) row3I= emptyI;
241                 else row3I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x2;
242                 
243                 if (x2>in->x-1 || y2>in->y-1) row4I= emptyI;
244                 else row4I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x2;
245                 
246                 a= u-floor(u);
247                 b= v-floor(v);
248                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
249                 
250                 outI[0]= ma_mb*row1I[0] + a_mb*row3I[0] + ma_b*row2I[0]+ a_b*row4I[0];
251                 outI[1]= ma_mb*row1I[1] + a_mb*row3I[1] + ma_b*row2I[1]+ a_b*row4I[1];
252                 outI[2]= ma_mb*row1I[2] + a_mb*row3I[2] + ma_b*row2I[2]+ a_b*row4I[2];
253                 outI[3]= ma_mb*row1I[3] + a_mb*row3I[3] + ma_b*row2I[3]+ a_b*row4I[3];
254         }
255 }
256
257 /* function assumes out to be zero'ed, only does RGBA */
258 /* NEAREST INTERPOLATION */
259 void neareast_interpolation(ImBuf *in, ImBuf *out, float u, float v,int xout, int yout)
260 {
261         float *outF,*dataF;
262         unsigned char *dataI,*outI;
263         int y1, x1;
264         int do_rect, do_float;
265
266         if (in==NULL) return;
267         if (in->rect==NULL && in->rect_float==NULL) return;
268
269         do_rect= (out->rect != NULL);
270         do_float= (out->rect_float != NULL);
271
272         x1= (int)(u);
273         y1= (int)(v);
274
275         if (do_rect)
276                 outI=(unsigned char *)out->rect + out->x * yout * 4 + 4*xout;
277         else
278                 outI= NULL;
279         if (do_float)
280                 outF=(float *)out->rect_float + out->x * yout * 4 + 4*xout;
281         else
282                 outF= NULL;
283
284         // sample area entirely outside image? 
285         if (x1<0 || x1>in->x-1 || y1<0 || y1>in->y-1) return;
286         
287         // sample including outside of edges of image 
288         if (x1<0 || y1<0) {
289                 if (do_rect) {
290                         outI[0]= 0;
291                         outI[1]= 0;
292                         outI[2]= 0;
293                         outI[3]= 0;
294                 }
295                 if (do_float) {
296                         outF[0]= 0.0f;
297                         outF[1]= 0.0f;
298                         outF[2]= 0.0f;
299                         outF[3]= 0.0f;
300                 }
301         } else {
302                 dataI= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
303                 if (do_rect) {
304                         outI[0]= dataI[0];
305                         outI[1]= dataI[1];
306                         outI[2]= dataI[2];
307                         outI[3]= dataI[3];
308                 }
309                 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
310                 if (do_float) {
311                         outF[0]= dataF[0];
312                         outF[1]= dataF[1];
313                         outF[2]= dataF[2];
314                         outF[3]= dataF[3];
315                 }
316         }       
317 }