3 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
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)*/
33 /* imageprocess.c MIXED MODEL
40 #include "IMB_imbuf_types.h"
41 #include "IMB_imbuf.h"
44 /* This define should be relocated to a global header some where Kent Mein
45 I stole it from util.h in the plugins api */
46 #define MAX2(x,y) ( (x)>(y) ? (x) : (y) )
48 /* Only this one is used liberally here, and in imbuf */
49 void IMB_convert_rgba_to_abgr(struct ImBuf *ibuf)
52 unsigned char rt, *cp = (unsigned char *)ibuf->rect;
53 float rtf, *cpf = ibuf->rect_float;
56 size = ibuf->x * ibuf->y;
69 if (ibuf->rect_float) {
70 size = ibuf->x * ibuf->y;
84 /**************************************************************************
88 * http://wiki.blender.org/index.php/User:Damiles#Interpolations_Algorithms
89 ***************************************************************************/
91 /* BICUBIC Interpolation functions */
92 /* More info: http://wiki.blender.org/index.php/User:Damiles#Bicubic_pixel_interpolation
94 /* function assumes out to be zero'ed, only does RGBA */
95 static float P(float k){
97 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));
101 void bicubic_interpolation(ImBuf *in, ImBuf *out, float x, float y, int xout, int yout)
104 unsigned char *dataI,*outI;
105 float a,b, outR,outG,outB,outA,*dataF,*outF;
106 int do_rect, do_float;
108 if (in == NULL) return;
109 if (in->rect == NULL && in->rect_float == NULL) return;
111 do_rect= (out->rect != NULL);
112 do_float= (out->rect_float != NULL);
123 for(n= -1; n<= 2; n++){
124 for(m= -1; m<= 2; m++){
127 if (x1>0 && x1 < in->x && y1>0 && y1<in->y) {
129 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
130 outR+= dataF[0] * P(n-a) * P(b-m);
131 outG+= dataF[1] * P(n-a) * P(b-m);
132 outB+= dataF[2] * P(n-a) * P(b-m);
133 outA+= dataF[3] * P(n-a) * P(b-m);
136 dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1;
137 outR+= dataI[0] * P(n-a) * P(b-m);
138 outG+= dataI[1] * P(n-a) * P(b-m);
139 outB+= dataI[2] * P(n-a) * P(b-m);
140 outA+= dataI[3] * P(n-a) * P(b-m);
146 outI= (unsigned char *)out->rect + out->x * yout * 4 + 4*xout;
153 outF= (float *)out->rect_float + out->x * yout * 4 + 4*xout;
161 /* function assumes out to be zero'ed, only does RGBA */
162 /* BILINEAR INTERPOLATION */
163 void bilinear_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout)
165 float *row1, *row2, *row3, *row4, a, b, *outF;
166 unsigned char *row1I, *row2I, *row3I, *row4I, *outI;
167 float a_b, ma_b, a_mb, ma_mb;
168 float empty[4]= {0.0f, 0.0f, 0.0f, 0.0f};
169 unsigned char emptyI[4]= {0, 0, 0, 0};
171 int do_rect, do_float;
173 if (in==NULL) return;
174 if (in->rect==NULL && in->rect_float==NULL) return;
176 do_rect= (out->rect != NULL);
177 do_float= (out->rect_float != NULL);
184 // sample area entirely outside image?
185 if (x2<0 || x1>in->x-1 || y2<0 || y1>in->y-1) return;
188 outI=(unsigned char *)out->rect + out->x * yout * 4 + 4*xout;
192 outF=(float *)out->rect_float + out->x * yout * 4 + 4*xout;
197 // sample including outside of edges of image
198 if (x1<0 || y1<0) row1= empty;
199 else row1= (float *)in->rect_float + in->x * y1 * 4 + 4*x1;
201 if (x1<0 || y2>in->y-1) row2= empty;
202 else row2= (float *)in->rect_float + in->x * y2 * 4 + 4*x1;
204 if (x2>in->x-1 || y1<0) row3= empty;
205 else row3= (float *)in->rect_float + in->x * y1 * 4 + 4*x2;
207 if (x2>in->x-1 || y2>in->y-1) row4= empty;
208 else row4= (float *)in->rect_float + in->x * y2 * 4 + 4*x2;
212 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
214 outF[0]= ma_mb*row1[0] + a_mb*row3[0] + ma_b*row2[0]+ a_b*row4[0];
215 outF[1]= ma_mb*row1[1] + a_mb*row3[1] + ma_b*row2[1]+ a_b*row4[1];
216 outF[2]= ma_mb*row1[2] + a_mb*row3[2] + ma_b*row2[2]+ a_b*row4[2];
217 outF[3]= ma_mb*row1[3] + a_mb*row3[3] + ma_b*row2[3]+ a_b*row4[3];
220 // sample including outside of edges of image
221 if (x1<0 || y1<0) row1I= emptyI;
222 else row1I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
224 if (x1<0 || y2>in->y-1) row2I= emptyI;
225 else row2I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x1;
227 if (x2>in->x-1 || y1<0) row3I= emptyI;
228 else row3I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x2;
230 if (x2>in->x-1 || y2>in->y-1) row4I= emptyI;
231 else row4I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x2;
235 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
237 outI[0]= ma_mb*row1I[0] + a_mb*row3I[0] + ma_b*row2I[0]+ a_b*row4I[0];
238 outI[1]= ma_mb*row1I[1] + a_mb*row3I[1] + ma_b*row2I[1]+ a_b*row4I[1];
239 outI[2]= ma_mb*row1I[2] + a_mb*row3I[2] + ma_b*row2I[2]+ a_b*row4I[2];
240 outI[3]= ma_mb*row1I[3] + a_mb*row3I[3] + ma_b*row2I[3]+ a_b*row4I[3];
244 /* function assumes out to be zero'ed, only does RGBA */
245 /* NEAREST INTERPOLATION */
246 void neareast_interpolation(ImBuf *in, ImBuf *out, float u, float v,int xout, int yout)
249 unsigned char *dataI,*outI;
251 int do_rect, do_float;
253 if (in==NULL) return;
254 if (in->rect==NULL && in->rect_float==NULL) return;
256 do_rect= (out->rect != NULL);
257 do_float= (out->rect_float != NULL);
263 outI=(unsigned char *)out->rect + out->x * yout * 4 + 4*xout;
267 outF=(float *)out->rect_float + out->x * yout * 4 + 4*xout;
271 // sample area entirely outside image?
272 if (x1<0 || x1>in->x-1 || y1<0 || y1>in->y-1) return;
274 // sample including outside of edges of image
289 dataI= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
296 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;