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