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){
96 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));
99 void bicubic_interpolation(ImBuf *in, ImBuf *out, float x, float y, int xout, int yout)
102 unsigned char *dataI,*outI;
103 float a,b,w,wx,wy[4], outR,outG,outB,outA,*dataF,*outF;
104 int do_rect, do_float;
106 if (in == NULL) return;
107 if (in->rect == NULL && in->rect_float == NULL) return;
109 do_rect= (out->rect != NULL);
110 do_float= (out->rect_float != NULL);
122 /* avoid calling multiple times */
128 for(n= -1; n<= 2; n++){
130 if (x1>0 && x1 < in->x) {
132 for(m= -1; m<= 2; m++){
134 if (y1>0 && y1<in->y) {
135 /* normally we could do this */
136 /* w = P(n-a) * P(b-m); */
137 /* except that would call P() 16 times per pixel therefor pow() 64 times, better precalc these */
141 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;
148 dataI= (unsigned char*)in->rect + in->x * y1 * 4 + 4*x1;
159 outI= (unsigned char *)out->rect + out->x * yout * 4 + 4*xout;
166 outF= (float *)out->rect_float + out->x * yout * 4 + 4*xout;
174 /* function assumes out to be zero'ed, only does RGBA */
175 /* BILINEAR INTERPOLATION */
176 void bilinear_interpolation(ImBuf *in, ImBuf *out, float u, float v, int xout, int yout)
178 float *row1, *row2, *row3, *row4, a, b, *outF;
179 unsigned char *row1I, *row2I, *row3I, *row4I, *outI;
180 float a_b, ma_b, a_mb, ma_mb;
181 float empty[4]= {0.0f, 0.0f, 0.0f, 0.0f};
182 unsigned char emptyI[4]= {0, 0, 0, 0};
184 int do_rect, do_float;
186 if (in==NULL) return;
187 if (in->rect==NULL && in->rect_float==NULL) return;
189 do_rect= (out->rect != NULL);
190 do_float= (out->rect_float != NULL);
197 // sample area entirely outside image?
198 if (x2<0 || x1>in->x-1 || y2<0 || y1>in->y-1) return;
201 outI=(unsigned char *)out->rect + out->x * yout * 4 + 4*xout;
205 outF=(float *)out->rect_float + out->x * yout * 4 + 4*xout;
210 // sample including outside of edges of image
211 if (x1<0 || y1<0) row1= empty;
212 else row1= (float *)in->rect_float + in->x * y1 * 4 + 4*x1;
214 if (x1<0 || y2>in->y-1) row2= empty;
215 else row2= (float *)in->rect_float + in->x * y2 * 4 + 4*x1;
217 if (x2>in->x-1 || y1<0) row3= empty;
218 else row3= (float *)in->rect_float + in->x * y1 * 4 + 4*x2;
220 if (x2>in->x-1 || y2>in->y-1) row4= empty;
221 else row4= (float *)in->rect_float + in->x * y2 * 4 + 4*x2;
225 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
227 outF[0]= ma_mb*row1[0] + a_mb*row3[0] + ma_b*row2[0]+ a_b*row4[0];
228 outF[1]= ma_mb*row1[1] + a_mb*row3[1] + ma_b*row2[1]+ a_b*row4[1];
229 outF[2]= ma_mb*row1[2] + a_mb*row3[2] + ma_b*row2[2]+ a_b*row4[2];
230 outF[3]= ma_mb*row1[3] + a_mb*row3[3] + ma_b*row2[3]+ a_b*row4[3];
233 // sample including outside of edges of image
234 if (x1<0 || y1<0) row1I= emptyI;
235 else row1I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
237 if (x1<0 || y2>in->y-1) row2I= emptyI;
238 else row2I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x1;
240 if (x2>in->x-1 || y1<0) row3I= emptyI;
241 else row3I= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x2;
243 if (x2>in->x-1 || y2>in->y-1) row4I= emptyI;
244 else row4I= (unsigned char *)in->rect + in->x * y2 * 4 + 4*x2;
248 a_b= a*b; ma_b= (1.0f-a)*b; a_mb= a*(1.0f-b); ma_mb= (1.0f-a)*(1.0f-b);
250 outI[0]= ma_mb*row1I[0] + a_mb*row3I[0] + ma_b*row2I[0]+ a_b*row4I[0];
251 outI[1]= ma_mb*row1I[1] + a_mb*row3I[1] + ma_b*row2I[1]+ a_b*row4I[1];
252 outI[2]= ma_mb*row1I[2] + a_mb*row3I[2] + ma_b*row2I[2]+ a_b*row4I[2];
253 outI[3]= ma_mb*row1I[3] + a_mb*row3I[3] + ma_b*row2I[3]+ a_b*row4I[3];
257 /* function assumes out to be zero'ed, only does RGBA */
258 /* NEAREST INTERPOLATION */
259 void neareast_interpolation(ImBuf *in, ImBuf *out, float u, float v,int xout, int yout)
262 unsigned char *dataI,*outI;
264 int do_rect, do_float;
266 if (in==NULL) return;
267 if (in->rect==NULL && in->rect_float==NULL) return;
269 do_rect= (out->rect != NULL);
270 do_float= (out->rect_float != NULL);
276 outI=(unsigned char *)out->rect + out->x * yout * 4 + 4*xout;
280 outF=(float *)out->rect_float + out->x * yout * 4 + 4*xout;
284 // sample area entirely outside image?
285 if (x1<0 || x1>in->x-1 || y1<0 || y1>in->y-1) return;
287 // sample including outside of edges of image
302 dataI= (unsigned char *)in->rect + in->x * y1 * 4 + 4*x1;
309 dataF= in->rect_float + in->x * y1 * 4 + 4*x1;