code cleanup: header cleanup, remove commented workaround for mingw since its no...
[blender.git] / source / blender / imbuf / intern / imageprocess.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  * This file was moved here from the src/ directory. It is meant to
27  * deal with endianness. It resided in a general blending lib. The
28  * other functions were only used during rendering. This single
29  * function remained. It should probably move to imbuf/intern/util.c,
30  * but we'll keep it here for the time being. (nzc)*/
31
32 /** \file blender/imbuf/intern/imageprocess.c
33  *  \ingroup imbuf
34  */
35
36
37 /*  imageprocess.c        MIXED MODEL
38  * 
39  *  april 95
40  * 
41  */
42
43 #include <stdlib.h>
44
45 #include "BLI_utildefines.h"
46
47 #include "IMB_imbuf_types.h"
48 #include "IMB_imbuf.h"
49 #include <math.h>
50
51 /* This define should be relocated to a global header some where  Kent Mein 
52  * I stole it from util.h in the plugins api */
53 #define MAX2(x,y)                ( (x)>(y) ? (x) : (y) )
54
55 /* Only this one is used liberally here, and in imbuf */
56 void IMB_convert_rgba_to_abgr(struct ImBuf *ibuf)
57 {
58         int size;
59         unsigned char rt, *cp = (unsigned char *)ibuf->rect;
60         float rtf, *cpf = ibuf->rect_float;
61
62         if (ibuf->rect) {
63                 size = ibuf->x * ibuf->y;
64
65                 while (size-- > 0) {
66                         rt= cp[0];
67                         cp[0]= cp[3];
68                         cp[3]= rt;
69                         rt= cp[1];
70                         cp[1]= cp[2];
71                         cp[2]= rt;
72                         cp+= 4;
73                 }
74         }
75
76         if (ibuf->rect_float) {
77                 size = ibuf->x * ibuf->y;
78
79                 while (size-- > 0) {
80                         rtf= cpf[0];
81                         cpf[0]= cpf[3];
82                         cpf[3]= rtf;
83                         rtf= cpf[1];
84                         cpf[1]= cpf[2];
85                         cpf[2]= rtf;
86                         cpf+= 4;
87                 }
88         }
89 }
90 static void pixel_from_buffer(struct ImBuf *ibuf, unsigned char **outI, float **outF, int x, int y)
91
92 {
93         int offset = ibuf->x * y * 4 + 4*x;
94         
95         if (ibuf->rect)
96                 *outI= (unsigned char *)ibuf->rect + offset;
97         
98         if (ibuf->rect_float)
99                 *outF= (float *)ibuf->rect_float + offset;
100 }
101
102 /**************************************************************************
103  *                            INTERPOLATIONS
104  *
105  * Reference and docs:
106  * http://wiki.blender.org/index.php/User:Damiles#Interpolations_Algorithms
107  ***************************************************************************/
108
109 /* BICUBIC Interpolation functions
110  *  More info: http://wiki.blender.org/index.php/User:Damiles#Bicubic_pixel_interpolation
111  * function assumes out to be zero'ed, only does RGBA */
112
113 static float P(float k)
114 {
115         float p1, p2, p3, p4;
116         p1 = MAX2(k+2.0f,0);
117         p2 = MAX2(k+1.0f,0);
118         p3 = MAX2(k,0);
119         p4 = MAX2(k-1.0f,0);
120         return (float)(1.0f/6.0f)*( p1*p1*p1 - 4.0f * p2*p2*p2 + 6.0f * p3*p3*p3 - 4.0f * p4*p4*p4);
121 }
122
123
124 #if 0
125 /* older, slower function, works the same as above */
126 static float P(float k)
127 {
128         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));
129 }
130 #endif
131
132 void bicubic_interpolation_color(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v)
133 {
134         int i,j,n,m,x1,y1;
135         unsigned char *dataI;
136         float a,b,w,wx,wy[4], outR,outG,outB,outA,*dataF;
137
138         /* sample area entirely outside image? */
139         if (ceil(u)<0 || floor(u)>in->x-1 || ceil(v)<0 || floor(v)>in->y-1)
140                 return;
141
142         /* ImBuf in must have a valid rect or rect_float, assume this is already checked */
143
144         i= (int)floor(u);
145         j= (int)floor(v);
146         a= u - i;
147         b= v - j;
148
149         outR = outG = outB = outA = 0.0f;
150         
151 /* Optimized and not so easy to read */
152         
153         /* avoid calling multiple times */
154         wy[0] = P(b-(-1));
155         wy[1] = P(b-  0);
156         wy[2] = P(b-  1);
157         wy[3] = P(b-  2);
158         
159         for (n= -1; n<= 2; n++) {
160                 x1= i+n;
161                 CLAMP(x1, 0, in->x-1);
162                 wx = P(n-a);
163                 for (m= -1; m<= 2; m++) {
164                         y1= j+m;
165                         CLAMP(y1, 0, in->y-1);
166                         /* normally we could do this */
167                         /* w = P(n-a) * P(b-m); */
168                         /* except that would call P() 16 times per pixel therefor pow() 64 times, better precalc these */
169                         w = wx * wy[m+1];
170
171                         if (outF) {
172                                 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
173                                 outR+= dataF[0] * w;
174                                 outG+= dataF[1] * w;
175                                 outB+= dataF[2] * w;
176                                 outA+= dataF[3] * w;
177                         }
178                         if (outI) {
179                                 dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1;
180                                 outR+= dataI[0] * w;
181                                 outG+= dataI[1] * w;
182                                 outB+= dataI[2] * w;
183                                 outA+= dataI[3] * w;
184                         }
185                 }
186         }
187
188 /* Done with optimized part */
189         
190 #if 0 
191         /* older, slower function, works the same as above */
192         for (n= -1; n<= 2; n++) {
193                 for (m= -1; m<= 2; m++) {
194                         x1= i+n;
195                         y1= j+m;
196                         if (x1>0 && x1 < in->x && y1>0 && y1<in->y) {
197                                 if (do_float) {
198                                         dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
199                                         outR+= dataF[0] * P(n-a) * P(b-m);
200                                         outG+= dataF[1] * P(n-a) * P(b-m);
201                                         outB+= dataF[2] * P(n-a) * P(b-m);
202                                         outA+= dataF[3] * P(n-a) * P(b-m);
203                                 }
204                                 if (do_rect) {
205                                         dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1;
206                                         outR+= dataI[0] * P(n-a) * P(b-m);
207                                         outG+= dataI[1] * P(n-a) * P(b-m);
208                                         outB+= dataI[2] * P(n-a) * P(b-m);
209                                         outA+= dataI[3] * P(n-a) * P(b-m);
210                                 }
211                         }
212                 }
213         }
214 #endif
215         
216         if (outI) {
217                 outI[0]= (int)outR;
218                 outI[1]= (int)outG;
219                 outI[2]= (int)outB;
220                 outI[3]= (int)outA;
221         }
222         if (outF) {
223                 outF[0]= outR;
224                 outF[1]= outG;
225                 outF[2]= outB;
226                 outF[3]= outA;
227         }
228 }
229
230
231 void bicubic_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout)
232 {
233         
234         unsigned char *outI = NULL;
235         float *outF = NULL;
236         
237         if (in == NULL || (in->rect == NULL && in->rect_float == NULL)) return;
238         
239         pixel_from_buffer(out, &outI, &outF, xout, yout); /* gcc warns these could be uninitialized, but its ok */
240         
241         bicubic_interpolation_color(in, outI, outF, u, v);
242 }
243
244 /* function assumes out to be zero'ed, only does RGBA */
245 /* BILINEAR INTERPOLATION */
246 void bilinear_interpolation_color(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v)
247 {
248         float *row1, *row2, *row3, *row4, a, b;
249         unsigned char *row1I, *row2I, *row3I, *row4I;
250         float a_b, ma_b, a_mb, ma_mb;
251         float empty[4]= {0.0f, 0.0f, 0.0f, 0.0f};
252         unsigned char emptyI[4]= {0, 0, 0, 0};
253         int y1, y2, x1, x2;
254         
255         
256         /* ImBuf in must have a valid rect or rect_float, assume this is already checked */
257
258         x1= (int)floor(u);
259         x2= (int)ceil(u);
260         y1= (int)floor(v);
261         y2= (int)ceil(v);
262
263         // sample area entirely outside image? 
264         if (x2<0 || x1>in->x-1 || y2<0 || y1>in->y-1) return;
265
266         if (outF) {
267                 // sample including outside of edges of image 
268                 if (x1<0 || y1<0) row1= empty;
269                 else row1= (float *)in->rect_float + in->x * y1 * 4 + 4*x1;
270                 
271                 if (x1<0 || y2>in->y-1) row2= empty;
272                 else row2= (float *)in->rect_float + in->x * y2 * 4 + 4*x1;
273                 
274                 if (x2>in->x-1 || y1<0) row3= empty;
275                 else row3= (float *)in->rect_float + in->x * y1 * 4 + 4*x2;
276                 
277                 if (x2>in->x-1 || y2>in->y-1) row4= empty;
278                 else row4= (float *)in->rect_float + in->x * y2 * 4 + 4*x2;
279                 
280                 a= u-floorf(u);
281                 b= v-floorf(v);
282                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
283                 
284                 outF[0]= ma_mb*row1[0] + a_mb*row3[0] + ma_b*row2[0]+ a_b*row4[0];
285                 outF[1]= ma_mb*row1[1] + a_mb*row3[1] + ma_b*row2[1]+ a_b*row4[1];
286                 outF[2]= ma_mb*row1[2] + a_mb*row3[2] + ma_b*row2[2]+ a_b*row4[2];
287                 outF[3]= ma_mb*row1[3] + a_mb*row3[3] + ma_b*row2[3]+ a_b*row4[3];
288         }
289         if (outI) {
290                 // sample including outside of edges of image 
291                 if (x1<0 || y1<0) row1I= emptyI;
292                 else row1I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
293                 
294                 if (x1<0 || y2>in->y-1) row2I= emptyI;
295                 else row2I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x1;
296                 
297                 if (x2>in->x-1 || y1<0) row3I= emptyI;
298                 else row3I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x2;
299                 
300                 if (x2>in->x-1 || y2>in->y-1) row4I= emptyI;
301                 else row4I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x2;
302                 
303                 a= u-floorf(u);
304                 b= v-floorf(v);
305                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
306                 
307                 /* need to add 0.5 to avoid rounding down (causes darken with the smear brush)
308                  * tested with white images and this should not wrap back to zero */
309                 outI[0]= (ma_mb*row1I[0] + a_mb*row3I[0] + ma_b*row2I[0]+ a_b*row4I[0]) + 0.5f;
310                 outI[1]= (ma_mb*row1I[1] + a_mb*row3I[1] + ma_b*row2I[1]+ a_b*row4I[1]) + 0.5f;
311                 outI[2]= (ma_mb*row1I[2] + a_mb*row3I[2] + ma_b*row2I[2]+ a_b*row4I[2]) + 0.5f;
312                 outI[3]= (ma_mb*row1I[3] + a_mb*row3I[3] + ma_b*row2I[3]+ a_b*row4I[3]) + 0.5f;
313         }
314 }
315
316 /* function assumes out to be zero'ed, only does RGBA */
317 /* BILINEAR INTERPOLATION */
318
319 /* Note about wrapping, the u/v still needs to be within the image bounds,
320  * just the interpolation is wrapped.
321  * This the same as bilinear_interpolation_color except it wraps rather than using empty and emptyI */
322 void bilinear_interpolation_color_wrap(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v)
323 {
324         float *row1, *row2, *row3, *row4, a, b;
325         unsigned char *row1I, *row2I, *row3I, *row4I;
326         float a_b, ma_b, a_mb, ma_mb;
327         int y1, y2, x1, x2;
328         
329         
330         /* ImBuf in must have a valid rect or rect_float, assume this is already checked */
331
332         x1= (int)floor(u);
333         x2= (int)ceil(u);
334         y1= (int)floor(v);
335         y2= (int)ceil(v);
336
337         // sample area entirely outside image? 
338         if (x2<0 || x1>in->x-1 || y2<0 || y1>in->y-1) return;
339         
340         /* wrap interpolation pixels - main difference from bilinear_interpolation_color  */
341         if (x1<0)x1= in->x+x1;
342         if (y1<0)y1= in->y+y1;
343         
344         if (x2>=in->x)x2= x2-in->x;
345         if (y2>=in->y)y2= y2-in->y;
346
347         if (outF) {
348                 // sample including outside of edges of image 
349                 row1= (float *)in->rect_float + in->x * y1 * 4 + 4*x1;
350                 row2= (float *)in->rect_float + in->x * y2 * 4 + 4*x1;
351                 row3= (float *)in->rect_float + in->x * y1 * 4 + 4*x2;
352                 row4= (float *)in->rect_float + in->x * y2 * 4 + 4*x2;
353                 
354                 a= u-floorf(u);
355                 b= v-floorf(v);
356                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
357                 
358                 outF[0]= ma_mb*row1[0] + a_mb*row3[0] + ma_b*row2[0]+ a_b*row4[0];
359                 outF[1]= ma_mb*row1[1] + a_mb*row3[1] + ma_b*row2[1]+ a_b*row4[1];
360                 outF[2]= ma_mb*row1[2] + a_mb*row3[2] + ma_b*row2[2]+ a_b*row4[2];
361                 outF[3]= ma_mb*row1[3] + a_mb*row3[3] + ma_b*row2[3]+ a_b*row4[3];
362         }
363         if (outI) {
364                 // sample including outside of edges of image 
365                 row1I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
366                 row2I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x1;
367                 row3I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x2;
368                 row4I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x2;
369                 
370                 a= u-floorf(u);
371                 b= v-floorf(v);
372                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
373                 
374                 /* need to add 0.5 to avoid rounding down (causes darken with the smear brush)
375                  * tested with white images and this should not wrap back to zero */
376                 outI[0]= (ma_mb*row1I[0] + a_mb*row3I[0] + ma_b*row2I[0]+ a_b*row4I[0]) + 0.5f;
377                 outI[1]= (ma_mb*row1I[1] + a_mb*row3I[1] + ma_b*row2I[1]+ a_b*row4I[1]) + 0.5f;
378                 outI[2]= (ma_mb*row1I[2] + a_mb*row3I[2] + ma_b*row2I[2]+ a_b*row4I[2]) + 0.5f;
379                 outI[3]= (ma_mb*row1I[3] + a_mb*row3I[3] + ma_b*row2I[3]+ a_b*row4I[3]) + 0.5f;
380         }
381 }
382
383 void bilinear_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout)
384 {
385         
386         unsigned char *outI = NULL;
387         float *outF = NULL;
388         
389         if (in == NULL || (in->rect == NULL && in->rect_float == NULL)) return;
390         
391         pixel_from_buffer(out, &outI, &outF, xout, yout); /* gcc warns these could be uninitialized, but its ok */
392         
393         bilinear_interpolation_color(in, outI, outF, u, v);
394 }
395
396 /* function assumes out to be zero'ed, only does RGBA */
397 /* NEAREST INTERPOLATION */
398 void neareast_interpolation_color(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v)
399 {
400         float *dataF;
401         unsigned char *dataI;
402         int y1, x1;
403
404         /* ImBuf in must have a valid rect or rect_float, assume this is already checked */
405         
406         x1= (int)(u);
407         y1= (int)(v);
408
409         // sample area entirely outside image? 
410         if (x1<0 || x1>in->x-1 || y1<0 || y1>in->y-1) return;
411         
412         // sample including outside of edges of image 
413         if (x1<0 || y1<0) {
414                 if (outI) {
415                         outI[0]= 0;
416                         outI[1]= 0;
417                         outI[2]= 0;
418                         outI[3]= 0;
419                 }
420                 if (outF) {
421                         outF[0]= 0.0f;
422                         outF[1]= 0.0f;
423                         outF[2]= 0.0f;
424                         outF[3]= 0.0f;
425                 }
426         }
427         else {
428                 dataI= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
429                 if (outI) {
430                         outI[0]= dataI[0];
431                         outI[1]= dataI[1];
432                         outI[2]= dataI[2];
433                         outI[3]= dataI[3];
434                 }
435                 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
436                 if (outF) {
437                         outF[0]= dataF[0];
438                         outF[1]= dataF[1];
439                         outF[2]= dataF[2];
440                         outF[3]= dataF[3];
441                 }
442         }       
443 }
444
445 void neareast_interpolation(ImBuf *in, ImBuf *out, float x, float y, int xout, int yout)
446 {
447         
448         unsigned char *outI = NULL;
449         float *outF = NULL;
450
451         if (in == NULL || (in->rect == NULL && in->rect_float == NULL)) return;
452         
453         pixel_from_buffer(out, &outI, &outF, xout, yout); /* gcc warns these could be uninitialized, but its ok */
454         
455         neareast_interpolation_color(in, outI, outF, x, y);
456 }