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