imbuf, mathutils & readfile: floats were being implicitly promoted to doubles, adjust...
[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 /** \file blender/imbuf/intern/imageprocess.c
34  *  \ingroup imbuf
35  */
36
37
38 /*  imageprocess.c        MIXED MODEL
39  * 
40  *  april 95
41  * 
42  * $Id$
43  */
44
45 #include <stdlib.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 */
112 /* function assumes out to be zero'ed, only does RGBA */
113
114 static float P(float k){
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         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));
128 }
129 #endif
130
131 void bicubic_interpolation_color(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v)
132 {
133         int i,j,n,m,x1,y1;
134         unsigned char *dataI;
135         float a,b,w,wx,wy[4], outR,outG,outB,outA,*dataF;
136
137         /* ImBuf in must have a valid rect or rect_float, assume this is already checked */
138
139         i= (int)floor(u);
140         j= (int)floor(v);
141         a= u - i;
142         b= v - j;
143
144         outR = outG = outB = outA = 0.0f;
145         
146 /* Optimized and not so easy to read */
147         
148         /* avoid calling multiple times */
149         wy[0] = P(b-(-1));
150         wy[1] = P(b-  0);
151         wy[2] = P(b-  1);
152         wy[3] = P(b-  2);
153         
154         for(n= -1; n<= 2; n++){
155                 x1= i+n;
156                 if (x1>0 && x1 < in->x) {
157                         wx = P(n-a);
158                         for(m= -1; m<= 2; m++){
159                                 y1= j+m;
160                                 if (y1>0 && y1<in->y) {
161                                         /* normally we could do this */
162                                         /* w = P(n-a) * P(b-m); */
163                                         /* except that would call P() 16 times per pixel therefor pow() 64 times, better precalc these */
164                                         w = wx * wy[m+1];
165                                         
166                                         if (outF) {
167                                                 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
168                                                 outR+= dataF[0] * w;
169                                                 outG+= dataF[1] * w;
170                                                 outB+= dataF[2] * w;
171                                                 outA+= dataF[3] * w;
172                                         }
173                                         if (outI) {
174                                                 dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1;
175                                                 outR+= dataI[0] * w;
176                                                 outG+= dataI[1] * w;
177                                                 outB+= dataI[2] * w;
178                                                 outA+= dataI[3] * w;
179                                         }
180                                 }
181                         }
182                 }
183         }
184
185 /* Done with optimized part */
186         
187 #if 0 
188         /* older, slower function, works the same as above */
189         for(n= -1; n<= 2; n++){
190                 for(m= -1; m<= 2; m++){
191                         x1= i+n;
192                         y1= j+m;
193                         if (x1>0 && x1 < in->x && y1>0 && y1<in->y) {
194                                 if (do_float) {
195                                         dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
196                                         outR+= dataF[0] * P(n-a) * P(b-m);
197                                         outG+= dataF[1] * P(n-a) * P(b-m);
198                                         outB+= dataF[2] * P(n-a) * P(b-m);
199                                         outA+= dataF[3] * P(n-a) * P(b-m);
200                                 }
201                                 if (do_rect) {
202                                         dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1;
203                                         outR+= dataI[0] * P(n-a) * P(b-m);
204                                         outG+= dataI[1] * P(n-a) * P(b-m);
205                                         outB+= dataI[2] * P(n-a) * P(b-m);
206                                         outA+= dataI[3] * P(n-a) * P(b-m);
207                                 }
208                         }
209                 }
210         }
211 #endif
212         
213         if (outI) {
214                 outI[0]= (int)outR;
215                 outI[1]= (int)outG;
216                 outI[2]= (int)outB;
217                 outI[3]= (int)outA;
218         }
219         if (outF) {
220                 outF[0]= outR;
221                 outF[1]= outG;
222                 outF[2]= outB;
223                 outF[3]= outA;
224         }
225 }
226
227
228 void bicubic_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout)
229 {
230         
231         unsigned char *outI = NULL;
232         float *outF = NULL;
233         
234         if (in == NULL || (in->rect == NULL && in->rect_float == NULL)) return;
235         
236         pixel_from_buffer(out, &outI, &outF, xout, yout); /* gcc warns these could be uninitialized, but its ok */
237         
238         bicubic_interpolation_color(in, outI, outF, u, v);
239 }
240
241 /* function assumes out to be zero'ed, only does RGBA */
242 /* BILINEAR INTERPOLATION */
243 void bilinear_interpolation_color(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v)
244 {
245         float *row1, *row2, *row3, *row4, a, b;
246         unsigned char *row1I, *row2I, *row3I, *row4I;
247         float a_b, ma_b, a_mb, ma_mb;
248         float empty[4]= {0.0f, 0.0f, 0.0f, 0.0f};
249         unsigned char emptyI[4]= {0, 0, 0, 0};
250         int y1, y2, x1, x2;
251         
252         
253         /* ImBuf in must have a valid rect or rect_float, assume this is already checked */
254
255         x1= (int)floor(u);
256         x2= (int)ceil(u);
257         y1= (int)floor(v);
258         y2= (int)ceil(v);
259
260         // sample area entirely outside image? 
261         if (x2<0 || x1>in->x-1 || y2<0 || y1>in->y-1) return;
262
263         if (outF) {
264                 // sample including outside of edges of image 
265                 if (x1<0 || y1<0) row1= empty;
266                 else row1= (float *)in->rect_float + in->x * y1 * 4 + 4*x1;
267                 
268                 if (x1<0 || y2>in->y-1) row2= empty;
269                 else row2= (float *)in->rect_float + in->x * y2 * 4 + 4*x1;
270                 
271                 if (x2>in->x-1 || y1<0) row3= empty;
272                 else row3= (float *)in->rect_float + in->x * y1 * 4 + 4*x2;
273                 
274                 if (x2>in->x-1 || y2>in->y-1) row4= empty;
275                 else row4= (float *)in->rect_float + in->x * y2 * 4 + 4*x2;
276                 
277                 a= u-floorf(u);
278                 b= v-floorf(v);
279                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
280                 
281                 outF[0]= ma_mb*row1[0] + a_mb*row3[0] + ma_b*row2[0]+ a_b*row4[0];
282                 outF[1]= ma_mb*row1[1] + a_mb*row3[1] + ma_b*row2[1]+ a_b*row4[1];
283                 outF[2]= ma_mb*row1[2] + a_mb*row3[2] + ma_b*row2[2]+ a_b*row4[2];
284                 outF[3]= ma_mb*row1[3] + a_mb*row3[3] + ma_b*row2[3]+ a_b*row4[3];
285         }
286         if (outI) {
287                 // sample including outside of edges of image 
288                 if (x1<0 || y1<0) row1I= emptyI;
289                 else row1I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
290                 
291                 if (x1<0 || y2>in->y-1) row2I= emptyI;
292                 else row2I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x1;
293                 
294                 if (x2>in->x-1 || y1<0) row3I= emptyI;
295                 else row3I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x2;
296                 
297                 if (x2>in->x-1 || y2>in->y-1) row4I= emptyI;
298                 else row4I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x2;
299                 
300                 a= u-floorf(u);
301                 b= v-floorf(v);
302                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
303                 
304                 /* need to add 0.5 to avoid rounding down (causes darken with the smear brush)
305                  * tested with white images and this should not wrap back to zero */
306                 outI[0]= (ma_mb*row1I[0] + a_mb*row3I[0] + ma_b*row2I[0]+ a_b*row4I[0]) + 0.5f;
307                 outI[1]= (ma_mb*row1I[1] + a_mb*row3I[1] + ma_b*row2I[1]+ a_b*row4I[1]) + 0.5f;
308                 outI[2]= (ma_mb*row1I[2] + a_mb*row3I[2] + ma_b*row2I[2]+ a_b*row4I[2]) + 0.5f;
309                 outI[3]= (ma_mb*row1I[3] + a_mb*row3I[3] + ma_b*row2I[3]+ a_b*row4I[3]) + 0.5f;
310         }
311 }
312
313 /* function assumes out to be zero'ed, only does RGBA */
314 /* BILINEAR INTERPOLATION */
315
316 /* Note about wrapping, the u/v still needs to be within the image bounds,
317  * just the interpolation is wrapped.
318  * This the same as bilinear_interpolation_color except it wraps rather then using empty and emptyI */
319 void bilinear_interpolation_color_wrap(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v)
320 {
321         float *row1, *row2, *row3, *row4, a, b;
322         unsigned char *row1I, *row2I, *row3I, *row4I;
323         float a_b, ma_b, a_mb, ma_mb;
324         int y1, y2, x1, x2;
325         
326         
327         /* ImBuf in must have a valid rect or rect_float, assume this is already checked */
328
329         x1= (int)floor(u);
330         x2= (int)ceil(u);
331         y1= (int)floor(v);
332         y2= (int)ceil(v);
333
334         // sample area entirely outside image? 
335         if (x2<0 || x1>in->x-1 || y2<0 || y1>in->y-1) return;
336         
337         /* wrap interpolation pixels - main difference from bilinear_interpolation_color  */
338         if(x1<0)x1= in->x+x1;
339         if(y1<0)y1= in->y+y1;
340         
341         if(x2>=in->x)x2= x2-in->x;
342         if(y2>=in->y)y2= y2-in->y;
343
344         if (outF) {
345                 // sample including outside of edges of image 
346                 row1= (float *)in->rect_float + in->x * y1 * 4 + 4*x1;
347                 row2= (float *)in->rect_float + in->x * y2 * 4 + 4*x1;
348                 row3= (float *)in->rect_float + in->x * y1 * 4 + 4*x2;
349                 row4= (float *)in->rect_float + in->x * y2 * 4 + 4*x2;
350                 
351                 a= u-floorf(u);
352                 b= v-floorf(v);
353                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
354                 
355                 outF[0]= ma_mb*row1[0] + a_mb*row3[0] + ma_b*row2[0]+ a_b*row4[0];
356                 outF[1]= ma_mb*row1[1] + a_mb*row3[1] + ma_b*row2[1]+ a_b*row4[1];
357                 outF[2]= ma_mb*row1[2] + a_mb*row3[2] + ma_b*row2[2]+ a_b*row4[2];
358                 outF[3]= ma_mb*row1[3] + a_mb*row3[3] + ma_b*row2[3]+ a_b*row4[3];
359         }
360         if (outI) {
361                 // sample including outside of edges of image 
362                 row1I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
363                 row2I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x1;
364                 row3I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x2;
365                 row4I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x2;
366                 
367                 a= u-floorf(u);
368                 b= v-floorf(v);
369                 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
370                 
371                 /* need to add 0.5 to avoid rounding down (causes darken with the smear brush)
372                  * tested with white images and this should not wrap back to zero */
373                 outI[0]= (ma_mb*row1I[0] + a_mb*row3I[0] + ma_b*row2I[0]+ a_b*row4I[0]) + 0.5f;
374                 outI[1]= (ma_mb*row1I[1] + a_mb*row3I[1] + ma_b*row2I[1]+ a_b*row4I[1]) + 0.5f;
375                 outI[2]= (ma_mb*row1I[2] + a_mb*row3I[2] + ma_b*row2I[2]+ a_b*row4I[2]) + 0.5f;
376                 outI[3]= (ma_mb*row1I[3] + a_mb*row3I[3] + ma_b*row2I[3]+ a_b*row4I[3]) + 0.5f;
377         }
378 }
379
380 void bilinear_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout)
381 {
382         
383         unsigned char *outI = NULL;
384         float *outF = NULL;
385         
386         if (in == NULL || (in->rect == NULL && in->rect_float == NULL)) return;
387         
388         pixel_from_buffer(out, &outI, &outF, xout, yout); /* gcc warns these could be uninitialized, but its ok */
389         
390         bilinear_interpolation_color(in, outI, outF, u, v);
391 }
392
393 /* function assumes out to be zero'ed, only does RGBA */
394 /* NEAREST INTERPOLATION */
395 void neareast_interpolation_color(struct ImBuf *in, unsigned char *outI, float *outF, float u, float v)
396 {
397         float *dataF;
398         unsigned char *dataI;
399         int y1, x1;
400
401         /* ImBuf in must have a valid rect or rect_float, assume this is already checked */
402         
403         x1= (int)(u);
404         y1= (int)(v);
405
406         // sample area entirely outside image? 
407         if (x1<0 || x1>in->x-1 || y1<0 || y1>in->y-1) return;
408         
409         // sample including outside of edges of image 
410         if (x1<0 || y1<0) {
411                 if (outI) {
412                         outI[0]= 0;
413                         outI[1]= 0;
414                         outI[2]= 0;
415                         outI[3]= 0;
416                 }
417                 if (outF) {
418                         outF[0]= 0.0f;
419                         outF[1]= 0.0f;
420                         outF[2]= 0.0f;
421                         outF[3]= 0.0f;
422                 }
423         } else {
424                 dataI= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
425                 if (outI) {
426                         outI[0]= dataI[0];
427                         outI[1]= dataI[1];
428                         outI[2]= dataI[2];
429                         outI[3]= dataI[3];
430                 }
431                 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
432                 if (outF) {
433                         outF[0]= dataF[0];
434                         outF[1]= dataF[1];
435                         outF[2]= dataF[2];
436                         outF[3]= dataF[3];
437                 }
438         }       
439 }
440
441 void neareast_interpolation(ImBuf *in, ImBuf *out, float x, float y, int xout, int yout)
442 {
443         
444         unsigned char *outI = NULL;
445         float *outF = NULL;
446
447         if (in == NULL || (in->rect == NULL && in->rect_float == NULL)) return;
448         
449         pixel_from_buffer(out, &outI, &outF, xout, yout); /* gcc warns these could be uninitialized, but its ok */
450         
451         neareast_interpolation_color(in, outI, outF, x, y);
452 }