doxygen: prevent GPL license block from being parsed as doxygen comment.
[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., 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  * 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 <stdlib.h>
41
42 #include "IMB_imbuf_types.h"
43 #include "IMB_imbuf.h"
44 #include "math.h"
45
46 /* This define should be relocated to a global header some where  Kent Mein 
47 I stole it from util.h in the plugins api */
48 #define MAX2(x,y)                ( (x)>(y) ? (x) : (y) )
49
50 /* Only this one is used liberally here, and in imbuf */
51 void IMB_convert_rgba_to_abgr(struct ImBuf *ibuf)
52 {
53         int size;
54         unsigned char rt, *cp = (unsigned char *)ibuf->rect;
55         float rtf, *cpf = ibuf->rect_float;
56
57         if (ibuf->rect) {
58                 size = ibuf->x * ibuf->y;
59
60                 while(size-- > 0) {
61                         rt= cp[0];
62                         cp[0]= cp[3];
63                         cp[3]= rt;
64                         rt= cp[1];
65                         cp[1]= cp[2];
66                         cp[2]= rt;
67                         cp+= 4;
68                 }
69         }
70
71         if (ibuf->rect_float) {
72                 size = ibuf->x * ibuf->y;
73
74                 while(size-- > 0) {
75                         rtf= cpf[0];
76                         cpf[0]= cpf[3];
77                         cpf[3]= rtf;
78                         rtf= cpf[1];
79                         cpf[1]= cpf[2];
80                         cpf[2]= rtf;
81                         cpf+= 4;
82                 }
83         }
84 }
85 static void pixel_from_buffer(struct ImBuf *ibuf, unsigned char **outI, float **outF, int x, int y)
86
87 {
88         int offset = ibuf->x * y * 4 + 4*x;
89         
90         if (ibuf->rect)
91                 *outI= (unsigned char *)ibuf->rect + offset;
92         
93         if (ibuf->rect_float)
94                 *outF= (float *)ibuf->rect_float + offset;
95 }
96
97 /**************************************************************************
98 *                            INTERPOLATIONS 
99 *
100 * Reference and docs:
101 * http://wiki.blender.org/index.php/User:Damiles#Interpolations_Algorithms
102 ***************************************************************************/
103
104 /* BICUBIC Interpolation functions */
105 /*  More info: http://wiki.blender.org/index.php/User:Damiles#Bicubic_pixel_interpolation
106 */
107 /* function assumes out to be zero'ed, only does RGBA */
108
109 static float P(float k){
110         float p1, p2, p3, p4;
111         p1 = MAX2(k+2.0f,0);
112         p2 = MAX2(k+1.0f,0);
113         p3 = MAX2(k,0);
114         p4 = MAX2(k-1.0f,0);
115         return (float)(1.0f/6.0f)*( p1*p1*p1 - 4.0f * p2*p2*p2 + 6.0f * p3*p3*p3 - 4.0f * p4*p4*p4);
116 }
117
118
119 #if 0
120 /* older, slower function, works the same as above */
121 static float P(float k){
122         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));
123 }
124 #endif
125
126 void bicubic_interpolation_color(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v)
127 {
128         int i,j,n,m,x1,y1;
129         unsigned char *dataI;
130         float a,b,w,wx,wy[4], outR,outG,outB,outA,*dataF;
131
132         /* ImBuf in must have a valid rect or rect_float, assume this is already checked */
133
134         i= (int)floor(u);
135         j= (int)floor(v);
136         a= u - i;
137         b= v - j;
138
139         outR = outG = outB = outA = 0.0f;
140         
141 /* Optimized and not so easy to read */
142         
143         /* avoid calling multiple times */
144         wy[0] = P(b-(-1));
145         wy[1] = P(b-  0);
146         wy[2] = P(b-  1);
147         wy[3] = P(b-  2);
148         
149         for(n= -1; n<= 2; n++){
150                 x1= i+n;
151                 if (x1>0 && x1 < in->x) {
152                         wx = P(n-a);
153                         for(m= -1; m<= 2; m++){
154                                 y1= j+m;
155                                 if (y1>0 && y1<in->y) {
156                                         /* normally we could do this */
157                                         /* w = P(n-a) * P(b-m); */
158                                         /* except that would call P() 16 times per pixel therefor pow() 64 times, better precalc these */
159                                         w = wx * wy[m+1];
160                                         
161                                         if (outF) {
162                                                 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
163                                                 outR+= dataF[0] * w;
164                                                 outG+= dataF[1] * w;
165                                                 outB+= dataF[2] * w;
166                                                 outA+= dataF[3] * w;
167                                         }
168                                         if (outI) {
169                                                 dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1;
170                                                 outR+= dataI[0] * w;
171                                                 outG+= dataI[1] * w;
172                                                 outB+= dataI[2] * w;
173                                                 outA+= dataI[3] * w;
174                                         }
175                                 }
176                         }
177                 }
178         }
179
180 /* Done with optimized part */
181         
182 #if 0 
183         /* older, slower function, works the same as above */
184         for(n= -1; n<= 2; n++){
185                 for(m= -1; m<= 2; m++){
186                         x1= i+n;
187                         y1= j+m;
188                         if (x1>0 && x1 < in->x && y1>0 && y1<in->y) {
189                                 if (do_float) {
190                                         dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
191                                         outR+= dataF[0] * P(n-a) * P(b-m);
192                                         outG+= dataF[1] * P(n-a) * P(b-m);
193                                         outB+= dataF[2] * P(n-a) * P(b-m);
194                                         outA+= dataF[3] * P(n-a) * P(b-m);
195                                 }
196                                 if (do_rect) {
197                                         dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1;
198                                         outR+= dataI[0] * P(n-a) * P(b-m);
199                                         outG+= dataI[1] * P(n-a) * P(b-m);
200                                         outB+= dataI[2] * P(n-a) * P(b-m);
201                                         outA+= dataI[3] * P(n-a) * P(b-m);
202                                 }
203                         }
204                 }
205         }
206 #endif
207         
208         if (outI) {
209                 outI[0]= (int)outR;
210                 outI[1]= (int)outG;
211                 outI[2]= (int)outB;
212                 outI[3]= (int)outA;
213         }
214         if (outF) {
215                 outF[0]= outR;
216                 outF[1]= outG;
217                 outF[2]= outB;
218                 outF[3]= outA;
219         }
220 }
221
222
223 void bicubic_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout)
224 {
225         
226         unsigned char *outI = NULL;
227         float *outF = NULL;
228         
229         if (in == NULL || (in->rect == NULL && in->rect_float == NULL)) return;
230         
231         pixel_from_buffer(out, &outI, &outF, xout, yout); /* gcc warns these could be uninitialized, but its ok */
232         
233         bicubic_interpolation_color(in, outI, outF, u, v);
234 }
235
236 /* function assumes out to be zero'ed, only does RGBA */
237 /* BILINEAR INTERPOLATION */
238 void bilinear_interpolation_color(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v)
239 {
240         float *row1, *row2, *row3, *row4, a, b;
241         unsigned char *row1I, *row2I, *row3I, *row4I;
242         float a_b, ma_b, a_mb, ma_mb;
243         float empty[4]= {0.0f, 0.0f, 0.0f, 0.0f};
244         unsigned char emptyI[4]= {0, 0, 0, 0};
245         int y1, y2, x1, x2;
246         
247         
248         /* ImBuf in must have a valid rect or rect_float, assume this is already checked */
249
250         x1= (int)floor(u);
251         x2= (int)ceil(u);
252         y1= (int)floor(v);
253         y2= (int)ceil(v);
254
255         // sample area entirely outside image? 
256         if (x2<0 || x1>in->x-1 || y2<0 || y1>in->y-1) return;
257
258         if (outF) {
259                 // sample including outside of edges of image 
260                 if (x1<0 || y1<0) row1= empty;
261                 else row1= (float *)in->rect_float + in->x * y1 * 4 + 4*x1;
262                 
263                 if (x1<0 || y2>in->y-1) row2= empty;
264                 else row2= (float *)in->rect_float + in->x * y2 * 4 + 4*x1;
265                 
266                 if (x2>in->x-1 || y1<0) row3= empty;
267                 else row3= (float *)in->rect_float + in->x * y1 * 4 + 4*x2;
268                 
269                 if (x2>in->x-1 || y2>in->y-1) row4= empty;
270                 else row4= (float *)in->rect_float + in->x * y2 * 4 + 4*x2;
271                 
272                 a= u-floor(u);
273                 b= v-floor(v);
274                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
275                 
276                 outF[0]= ma_mb*row1[0] + a_mb*row3[0] + ma_b*row2[0]+ a_b*row4[0];
277                 outF[1]= ma_mb*row1[1] + a_mb*row3[1] + ma_b*row2[1]+ a_b*row4[1];
278                 outF[2]= ma_mb*row1[2] + a_mb*row3[2] + ma_b*row2[2]+ a_b*row4[2];
279                 outF[3]= ma_mb*row1[3] + a_mb*row3[3] + ma_b*row2[3]+ a_b*row4[3];
280         }
281         if (outI) {
282                 // sample including outside of edges of image 
283                 if (x1<0 || y1<0) row1I= emptyI;
284                 else row1I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
285                 
286                 if (x1<0 || y2>in->y-1) row2I= emptyI;
287                 else row2I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x1;
288                 
289                 if (x2>in->x-1 || y1<0) row3I= emptyI;
290                 else row3I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x2;
291                 
292                 if (x2>in->x-1 || y2>in->y-1) row4I= emptyI;
293                 else row4I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x2;
294                 
295                 a= u-floor(u);
296                 b= v-floor(v);
297                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
298                 
299                 /* need to add 0.5 to avoid rounding down (causes darken with the smear brush)
300                  * tested with white images and this should not wrap back to zero */
301                 outI[0]= (ma_mb*row1I[0] + a_mb*row3I[0] + ma_b*row2I[0]+ a_b*row4I[0]) + 0.5f;
302                 outI[1]= (ma_mb*row1I[1] + a_mb*row3I[1] + ma_b*row2I[1]+ a_b*row4I[1]) + 0.5f;
303                 outI[2]= (ma_mb*row1I[2] + a_mb*row3I[2] + ma_b*row2I[2]+ a_b*row4I[2]) + 0.5f;
304                 outI[3]= (ma_mb*row1I[3] + a_mb*row3I[3] + ma_b*row2I[3]+ a_b*row4I[3]) + 0.5f;
305         }
306 }
307
308 /* function assumes out to be zero'ed, only does RGBA */
309 /* BILINEAR INTERPOLATION */
310
311 /* Note about wrapping, the u/v still needs to be within the image bounds,
312  * just the interpolation is wrapped.
313  * This the same as bilinear_interpolation_color except it wraps rather then using empty and emptyI */
314 void bilinear_interpolation_color_wrap(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v)
315 {
316         float *row1, *row2, *row3, *row4, a, b;
317         unsigned char *row1I, *row2I, *row3I, *row4I;
318         float a_b, ma_b, a_mb, ma_mb;
319         int y1, y2, x1, x2;
320         
321         
322         /* ImBuf in must have a valid rect or rect_float, assume this is already checked */
323
324         x1= (int)floor(u);
325         x2= (int)ceil(u);
326         y1= (int)floor(v);
327         y2= (int)ceil(v);
328
329         // sample area entirely outside image? 
330         if (x2<0 || x1>in->x-1 || y2<0 || y1>in->y-1) return;
331         
332         /* wrap interpolation pixels - main difference from bilinear_interpolation_color  */
333         if(x1<0)x1= in->x+x1;
334         if(y1<0)y1= in->y+y1;
335         
336         if(x2>=in->x)x2= x2-in->x;
337         if(y2>=in->y)y2= y2-in->y;
338
339         if (outF) {
340                 // sample including outside of edges of image 
341                 row1= (float *)in->rect_float + in->x * y1 * 4 + 4*x1;
342                 row2= (float *)in->rect_float + in->x * y2 * 4 + 4*x1;
343                 row3= (float *)in->rect_float + in->x * y1 * 4 + 4*x2;
344                 row4= (float *)in->rect_float + in->x * y2 * 4 + 4*x2;
345                 
346                 a= u-floor(u);
347                 b= v-floor(v);
348                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
349                 
350                 outF[0]= ma_mb*row1[0] + a_mb*row3[0] + ma_b*row2[0]+ a_b*row4[0];
351                 outF[1]= ma_mb*row1[1] + a_mb*row3[1] + ma_b*row2[1]+ a_b*row4[1];
352                 outF[2]= ma_mb*row1[2] + a_mb*row3[2] + ma_b*row2[2]+ a_b*row4[2];
353                 outF[3]= ma_mb*row1[3] + a_mb*row3[3] + ma_b*row2[3]+ a_b*row4[3];
354         }
355         if (outI) {
356                 // sample including outside of edges of image 
357                 row1I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
358                 row2I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x1;
359                 row3I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x2;
360                 row4I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x2;
361                 
362                 a= u-floor(u);
363                 b= v-floor(v);
364                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
365                 
366                 /* need to add 0.5 to avoid rounding down (causes darken with the smear brush)
367                  * tested with white images and this should not wrap back to zero */
368                 outI[0]= (ma_mb*row1I[0] + a_mb*row3I[0] + ma_b*row2I[0]+ a_b*row4I[0]) + 0.5f;
369                 outI[1]= (ma_mb*row1I[1] + a_mb*row3I[1] + ma_b*row2I[1]+ a_b*row4I[1]) + 0.5f;
370                 outI[2]= (ma_mb*row1I[2] + a_mb*row3I[2] + ma_b*row2I[2]+ a_b*row4I[2]) + 0.5f;
371                 outI[3]= (ma_mb*row1I[3] + a_mb*row3I[3] + ma_b*row2I[3]+ a_b*row4I[3]) + 0.5f;
372         }
373 }
374
375 void bilinear_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout)
376 {
377         
378         unsigned char *outI = NULL;
379         float *outF = NULL;
380         
381         if (in == NULL || (in->rect == NULL && in->rect_float == NULL)) return;
382         
383         pixel_from_buffer(out, &outI, &outF, xout, yout); /* gcc warns these could be uninitialized, but its ok */
384         
385         bilinear_interpolation_color(in, outI, outF, u, v);
386 }
387
388 /* function assumes out to be zero'ed, only does RGBA */
389 /* NEAREST INTERPOLATION */
390 void neareast_interpolation_color(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v)
391 {
392         float *dataF;
393         unsigned char *dataI;
394         int y1, x1;
395
396         /* ImBuf in must have a valid rect or rect_float, assume this is already checked */
397         
398         x1= (int)(u);
399         y1= (int)(v);
400
401         // sample area entirely outside image? 
402         if (x1<0 || x1>in->x-1 || y1<0 || y1>in->y-1) return;
403         
404         // sample including outside of edges of image 
405         if (x1<0 || y1<0) {
406                 if (outI) {
407                         outI[0]= 0;
408                         outI[1]= 0;
409                         outI[2]= 0;
410                         outI[3]= 0;
411                 }
412                 if (outF) {
413                         outF[0]= 0.0f;
414                         outF[1]= 0.0f;
415                         outF[2]= 0.0f;
416                         outF[3]= 0.0f;
417                 }
418         } else {
419                 dataI= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
420                 if (outI) {
421                         outI[0]= dataI[0];
422                         outI[1]= dataI[1];
423                         outI[2]= dataI[2];
424                         outI[3]= dataI[3];
425                 }
426                 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
427                 if (outF) {
428                         outF[0]= dataF[0];
429                         outF[1]= dataF[1];
430                         outF[2]= dataF[2];
431                         outF[3]= dataF[3];
432                 }
433         }       
434 }
435
436 void neareast_interpolation(ImBuf *in, ImBuf *out, float x, float y, int xout, int yout)
437 {
438         
439         unsigned char *outI = NULL;
440         float *outF = NULL;
441
442         if (in == NULL || (in->rect == NULL && in->rect_float == NULL)) return;
443         
444         pixel_from_buffer(out, &outI, &outF, xout, yout); /* gcc warns these could be uninitialized, but its ok */
445         
446         neareast_interpolation_color(in, outI, outF, x, y);
447 }