Addes libopenjpeg (jpeg2000) support to the Makefiles
[blender.git] / source / blender / imbuf / intern / scaling.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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  * allocimbuf.c
28  *
29  * $Id$
30  */
31
32 #include "BLI_blenlib.h"
33
34 #include "imbuf.h"
35 #include "imbuf_patch.h"
36 #include "IMB_imbuf_types.h"
37 #include "IMB_imbuf.h"
38
39 #include "IMB_allocimbuf.h"
40 #include "IMB_filter.h"
41
42 #include "BLO_sys_types.h" // for intptr_t support
43
44 /************************************************************************/
45 /*                                                              SCALING                                                                 */
46 /************************************************************************/
47
48
49 struct ImBuf *IMB_half_x(struct ImBuf *ibuf1)
50 {
51         struct ImBuf *ibuf2;
52         uchar *p1,*_p1,*dest;
53         short a,r,g,b,x,y;
54         float af,rf,gf,bf, *p1f, *_p1f, *destf;
55         int do_rect, do_float;
56
57         if (ibuf1==NULL) return (0);
58         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
59
60         do_rect= (ibuf1->rect != NULL);
61         do_float= (ibuf1->rect_float != NULL);
62         
63         if (ibuf1->x <= 1) return(IMB_dupImBuf(ibuf1));
64         
65         ibuf2 = IMB_allocImBuf((ibuf1->x)/2, ibuf1->y, ibuf1->depth, ibuf1->flags, 0);
66         if (ibuf2==NULL) return (0);
67
68         _p1 = (uchar *) ibuf1->rect;
69         dest=(uchar *) ibuf2->rect;
70          
71         _p1f = ibuf1->rect_float;
72         destf= ibuf2->rect_float;
73
74         for(y=ibuf2->y;y>0;y--){
75                 p1 = _p1;
76                 p1f = _p1f;
77                 for(x = ibuf2->x ; x>0 ; x--){
78                         if (do_rect) {
79                                 a = *(p1++) ;
80                                 b = *(p1++) ;
81                                 g = *(p1++) ;
82                                 r = *(p1++);
83                                 a += *(p1++) ;
84                                 b += *(p1++) ;
85                                 g += *(p1++) ;
86                                 r += *(p1++);
87                                 *(dest++) = a >> 1;
88                                 *(dest++) = b >> 1;
89                                 *(dest++) = g >> 1;
90                                 *(dest++) = r >> 1;
91                         }
92                         if (do_float) {
93                                 af = *(p1f++);
94                                 bf = *(p1f++);
95                                 gf = *(p1f++);
96                                 rf = *(p1f++);
97                                 af += *(p1f++);
98                                 bf += *(p1f++);
99                                 gf += *(p1f++);
100                                 rf += *(p1f++);
101                                 *(destf++) = 0.5f*af;
102                                 *(destf++) = 0.5f*bf;
103                                 *(destf++) = 0.5f*gf;
104                                 *(destf++) = 0.5f*rf;
105                         }
106                 }
107                 if (do_rect) _p1 += (ibuf1->x << 2);
108                 if (do_float) _p1f += (ibuf1->x << 2);
109         }
110         return (ibuf2);
111 }
112
113
114 struct ImBuf *IMB_double_fast_x(struct ImBuf *ibuf1)
115 {
116         struct ImBuf *ibuf2;
117         int *p1,*dest, i, col, do_rect, do_float;
118         float *p1f, *destf;
119
120         if (ibuf1==NULL) return (0);
121         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
122
123         do_rect= (ibuf1->rect != NULL);
124         do_float= (ibuf1->rect_float != NULL);
125         
126         ibuf2 = IMB_allocImBuf(2 * ibuf1->x , ibuf1->y , ibuf1->depth, ibuf1->flags, 0);
127         if (ibuf2==NULL) return (0);
128
129         p1 = (int *) ibuf1->rect;
130         dest=(int *) ibuf2->rect;
131         p1f = (float *)ibuf1->rect_float;
132         destf = (float *)ibuf2->rect_float;
133
134         for(i = ibuf1->y * ibuf1->x ; i>0 ; i--) {
135                 if (do_rect) {
136                         col = *p1++;
137                         *dest++ = col;
138                         *dest++ = col;
139                 }
140                 if (do_float) {
141                         destf[0]= destf[4] =p1f[0];
142                         destf[1]= destf[5] =p1f[1];
143                         destf[2]= destf[6] =p1f[2];
144                         destf[3]= destf[7] =p1f[3];
145                         destf+= 8;
146                         p1f+= 4;
147                 }
148         }
149
150         return (ibuf2);
151 }
152
153 struct ImBuf *IMB_double_x(struct ImBuf *ibuf1)
154 {
155         struct ImBuf *ibuf2;
156
157         if (ibuf1==NULL) return (0);
158         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
159
160         ibuf2 = IMB_double_fast_x(ibuf1);
161
162         imb_filterx(ibuf2);
163         return (ibuf2);
164 }
165
166
167 struct ImBuf *IMB_half_y(struct ImBuf *ibuf1)
168 {
169         struct ImBuf *ibuf2;
170         uchar *p1,*p2,*_p1,*dest;
171         short a,r,g,b,x,y;
172         int do_rect, do_float;
173         float af,rf,gf,bf,*p1f,*p2f,*_p1f,*destf;
174
175         p1= p2= NULL;
176         p1f= p2f= NULL;
177         if (ibuf1==NULL) return (0);
178         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
179         if (ibuf1->y <= 1) return(IMB_dupImBuf(ibuf1));
180
181         do_rect= (ibuf1->rect != NULL);
182         do_float= (ibuf1->rect_float != NULL);
183
184         ibuf2 = IMB_allocImBuf(ibuf1->x , (ibuf1->y) / 2 , ibuf1->depth, ibuf1->flags, 0);
185         if (ibuf2==NULL) return (0);
186
187         _p1 = (uchar *) ibuf1->rect;
188         dest=(uchar *) ibuf2->rect;
189         _p1f = (float *) ibuf1->rect_float;
190         destf= (float *) ibuf2->rect_float;
191
192         for(y=ibuf2->y ; y>0 ; y--){
193                 if (do_rect) {
194                         p1 = _p1;
195                         p2 = _p1 + (ibuf1->x << 2);
196                 }
197                 if (do_float) {
198                         p1f = _p1f;
199                         p2f = _p1f + (ibuf1->x << 2);
200                 }
201                 for(x = ibuf2->x ; x>0 ; x--){
202                         if (do_rect) {
203                                 a = *(p1++) ;
204                                 b = *(p1++) ;
205                                 g = *(p1++) ;
206                                 r = *(p1++);
207                                 a += *(p2++) ;
208                                 b += *(p2++) ;
209                                 g += *(p2++) ;
210                                 r += *(p2++);
211                                 *(dest++) = a >> 1;
212                                 *(dest++) = b >> 1;
213                                 *(dest++) = g >> 1;
214                                 *(dest++) = r >> 1;
215                         }
216                         if (do_float) {
217                                 af = *(p1f++) ;
218                                 bf = *(p1f++) ;
219                                 gf = *(p1f++) ;
220                                 rf = *(p1f++);
221                                 af += *(p2f++) ;
222                                 bf += *(p2f++) ;
223                                 gf += *(p2f++) ;
224                                 rf += *(p2f++);
225                                 *(destf++) = 0.5f*af;
226                                 *(destf++) = 0.5f*bf;
227                                 *(destf++) = 0.5f*gf;
228                                 *(destf++) = 0.5f*rf;
229                         }
230                 }
231                 if (do_rect) _p1 += (ibuf1->x << 3);
232                 if (do_float) _p1f += (ibuf1->x << 3);
233         }
234         return (ibuf2);
235 }
236
237
238 struct ImBuf *IMB_double_fast_y(struct ImBuf *ibuf1)
239 {
240         struct ImBuf *ibuf2;
241         int *p1, *dest1, *dest2;
242         float *p1f, *dest1f, *dest2f;
243         short x,y;
244         int do_rect, do_float;
245
246         if (ibuf1==NULL) return (0);
247         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
248
249         do_rect= (ibuf1->rect != NULL);
250         do_float= (ibuf1->rect_float != NULL);
251
252         ibuf2 = IMB_allocImBuf(ibuf1->x , 2 * ibuf1->y , ibuf1->depth, ibuf1->flags, 0);
253         if (ibuf2==NULL) return (0);
254
255         p1 = (int *) ibuf1->rect;
256         dest1= (int *) ibuf2->rect;
257         p1f = (float *) ibuf1->rect_float;
258         dest1f= (float *) ibuf2->rect_float;
259
260         for(y = ibuf1->y ; y>0 ; y--){
261                 if (do_rect) {
262                         dest2 = dest1 + ibuf2->x;
263                         for(x = ibuf2->x ; x>0 ; x--) *dest1++ = *dest2++ = *p1++;
264                         dest1 = dest2;
265                 }
266                 if (do_float) {
267                         dest2f = dest1f + (4*ibuf2->x);
268                         for(x = ibuf2->x*4 ; x>0 ; x--) *dest1f++ = *dest2f++ = *p1f++;
269                         dest1f = dest2f;
270                 }
271         }
272
273         return (ibuf2);
274 }
275
276 struct ImBuf *IMB_double_y(struct ImBuf *ibuf1)
277 {
278         struct ImBuf *ibuf2;
279
280         if (ibuf1==NULL) return (0);
281         if (ibuf1->rect==NULL) return (0);
282
283         ibuf2 = IMB_double_fast_y(ibuf1);
284         
285         IMB_filtery(ibuf2);
286         return (ibuf2);
287 }
288
289
290 struct ImBuf *IMB_onehalf(struct ImBuf *ibuf1)
291 {
292         struct ImBuf *ibuf2;
293         uchar *p1, *p2 = NULL, *dest;
294         float *p1f, *destf, *p2f = NULL;
295         int x,y;
296         int do_rect, do_float;
297
298         if (ibuf1==NULL) return (0);
299         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
300
301         do_rect= (ibuf1->rect != NULL);
302         do_float= (ibuf1->rect_float != NULL);
303
304         if (ibuf1->x <= 1) return(IMB_half_y(ibuf1));
305         if (ibuf1->y <= 1) return(IMB_half_x(ibuf1));
306         
307         ibuf2=IMB_allocImBuf((ibuf1->x)/2, (ibuf1->y)/2, ibuf1->depth, ibuf1->flags, 0);
308         if (ibuf2==NULL) return (0);
309
310         p1f = ibuf1->rect_float;
311         destf=ibuf2->rect_float;
312         p1 = (uchar *) ibuf1->rect;
313         dest=(uchar *) ibuf2->rect;
314
315         for(y=ibuf2->y;y>0;y--){
316                 if (do_rect) p2 = p1 + (ibuf1->x << 2);
317                 if (do_float) p2f = p1f + (ibuf1->x << 2);
318                 for(x=ibuf2->x;x>0;x--){
319                         if (do_rect) {
320                                 dest[0] = (p1[0] + p2[0] + p1[4] + p2[4]) >> 2;
321                                 dest[1] = (p1[1] + p2[1] + p1[5] + p2[5]) >> 2;
322                                 dest[2] = (p1[2] + p2[2] + p1[6] + p2[6]) >> 2;
323                                 dest[3] = (p1[3] + p2[3] + p1[7] + p2[7]) >> 2;
324                                 p1 += 8; 
325                                 p2 += 8; 
326                                 dest += 4;
327                         }
328                         if (do_float){ 
329                                 destf[0] = 0.25f*(p1f[0] + p2f[0] + p1f[4] + p2f[4]);
330                                 destf[1] = 0.25f*(p1f[1] + p2f[1] + p1f[5] + p2f[5]);
331                                 destf[2] = 0.25f*(p1f[2] + p2f[2] + p1f[6] + p2f[6]);
332                                 destf[3] = 0.25f*(p1f[3] + p2f[3] + p1f[7] + p2f[7]);
333                                 p1f += 8; 
334                                 p2f += 8; 
335                                 destf += 4;
336                         }
337                 }
338                 if (do_rect) p1=p2;
339                 if (do_float) p1f=p2f;
340                 if(ibuf1->x & 1) {
341                         if (do_rect) p1+=4;
342                         if (do_float) p1f+=4;
343                 }
344         }
345         return (ibuf2);
346 }
347
348
349
350 struct ImBuf *IMB_onethird(struct ImBuf *ibuf1)
351 {
352         struct ImBuf *ibuf2;
353         uchar *p1,*p2,*p3,*dest;
354         float *p1f, *p2f, *p3f, *destf;
355         int do_rect, do_float;
356         short a,r,g,b,x,y,i;
357         float af,rf,gf,bf;
358
359         p2= p3= NULL;
360         p2f= p3f= NULL;
361         if (ibuf1==NULL) return (0);
362         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
363
364         do_rect= (ibuf1->rect != NULL);
365         do_float= (ibuf1->rect_float != NULL);
366
367         ibuf2=IMB_allocImBuf((ibuf1->x)/3, (ibuf1->y)/3, ibuf1->depth, ibuf1->flags, 0);
368         if (ibuf2==NULL) return (0);
369
370         p1f = ibuf1->rect_float;
371         destf = ibuf2->rect_float;
372         p1 = (uchar *) ibuf1->rect;
373         dest=(uchar *) ibuf2->rect;
374
375         for(y=ibuf2->y;y>0;y--){
376                 if (do_rect) {
377                         p2 = p1 + (ibuf1->x << 2);
378                         p3 = p2 + (ibuf1->x << 2);
379                 }
380                 if (do_float) {
381                         p2f = p1f + (ibuf1->x <<2);
382                         p3f = p2f + (ibuf1->x <<2);
383                 }
384                 for(x=ibuf2->x;x>0;x--){
385                         a=r=g=b=0;
386                         af=rf=gf=bf=0;
387                         for (i=3;i>0;i--){
388                                 if (do_rect) {
389                                         a += *(p1++) + *(p2++) + *(p3++);
390                                         b += *(p1++) + *(p2++) + *(p3++);
391                                         g += *(p1++) + *(p2++) + *(p3++);
392                                         r += *(p1++) + *(p2++) + *(p3++);
393                                 }
394                                 if (do_float) {
395                                         af += *(p1f++) + *(p2f++) + *(p3f++);
396                                         bf += *(p1f++) + *(p2f++) + *(p3f++);
397                                         gf += *(p1f++) + *(p2f++) + *(p3f++);
398                                         rf += *(p1f++) + *(p2f++) + *(p3f++);
399                                 }
400                         }
401                         if (do_rect) {
402                                 *(dest++) = a/9;
403                                 *(dest++) = b/9;
404                                 *(dest++) = g/9;
405                                 *(dest++) = r/9;
406                         }
407                         if (do_float) {
408                                 *(destf++) = af/9.0f;
409                                 *(destf++) = bf/9.0f;
410                                 *(destf++) = gf/9.0f;
411                                 *(destf++) = rf/9.0f;
412                         }
413                 }
414                 if (do_rect) p1=p3;
415                 if (do_float) p1f = p3f;
416         }
417         return (ibuf2);
418 }
419
420
421 struct ImBuf *IMB_halflace(struct ImBuf *ibuf1)
422 {
423         struct ImBuf *ibuf2;
424         uchar *p1,*p2,*dest;
425         float *p1f,*p2f,*destf;
426         short a,r,g,b,x,y,i;
427         float af,rf,gf,bf;
428         int do_rect, do_float;
429
430         p2= NULL;
431         p2f= NULL;
432         if (ibuf1==NULL) return (0);
433         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (0);
434
435         do_rect= (ibuf1->rect != NULL);
436         do_float= (ibuf1->rect_float != NULL);
437
438         ibuf2=IMB_allocImBuf((ibuf1->x)/4, (ibuf1->y)/2, ibuf1->depth, ibuf1->flags, 0);
439         if (ibuf2==NULL) return (0);
440
441         p1f = ibuf1->rect_float;
442         destf= ibuf2->rect_float;
443         p1 = (uchar *) ibuf1->rect;
444         dest=(uchar *) ibuf2->rect;
445
446         for(y= ibuf2->y / 2 ; y>0;y--){
447                 if (do_rect) p2 = p1 + (ibuf1->x << 3);
448                 if (do_float) p2f = p1f + (ibuf1->x << 3);
449                 for(x = 2 * ibuf2->x;x>0;x--){
450                         a=r=g=b=0;
451                         af=rf=gf=bf=0;
452                         for (i=4;i>0;i--){
453                                 if (do_rect) {
454                                         a += *(p1++) + *(p2++);
455                                         b += *(p1++) + *(p2++);
456                                         g += *(p1++) + *(p2++);
457                                         r += *(p1++) + *(p2++);
458                                 }
459                                 if (do_float) {
460                                         af += *(p1f++) + *(p2f++);
461                                         bf += *(p1f++) + *(p2f++);
462                                         gf += *(p1f++) + *(p2f++);
463                                         rf += *(p1f++) + *(p2f++);
464                                 }
465                         }
466                         if (do_rect) {
467                                 *(dest++) = a >> 3;
468                                 *(dest++) = b >> 3;
469                                 *(dest++) = g >> 3;
470                                 *(dest++) = r >> 3;
471                         }
472                         if (do_float) {
473                                 *(destf++) = 0.125f*af;
474                                 *(destf++) = 0.125f*bf;
475                                 *(destf++) = 0.125f*gf;
476                                 *(destf++) = 0.125f*rf;
477                         }
478                 }
479                 if (do_rect) p1 = p2;
480                 if (do_float) p1f = p2f;
481         }
482         return (ibuf2);
483 }
484
485 /* q_scale_linear_interpolation helper functions */
486
487 static void enlarge_picture_byte(
488         unsigned char* src, unsigned char* dst, int src_width, 
489         int src_height, int dst_width, int dst_height)
490 {
491         double ratiox = (double) (dst_width - 1.0) 
492                 / (double) (src_width - 1.001);
493         double ratioy = (double) (dst_height - 1.0) 
494                 / (double) (src_height - 1.001);
495         uintptr_t x_src, dx_src, x_dst;
496         uintptr_t y_src, dy_src, y_dst;
497
498         dx_src = 65536.0 / ratiox;
499         dy_src = 65536.0 / ratioy;
500
501         y_src = 0;
502         for (y_dst = 0; y_dst < dst_height; y_dst++) {
503                 unsigned char* line1 = src + (y_src >> 16) * 4 * src_width;
504                 unsigned char* line2 = line1 + 4 * src_width;
505                 uintptr_t weight1y = 65536 - (y_src & 0xffff);
506                 uintptr_t weight2y = 65536 - weight1y;
507
508                 if ((y_src >> 16) == src_height - 1) {
509                         line2 = line1;
510                 }
511
512                 x_src = 0;
513                 for (x_dst = 0; x_dst < dst_width; x_dst++) {
514                         uintptr_t weight1x = 65536 - (x_src & 0xffff);
515                         uintptr_t weight2x = 65536 - weight1x;
516
517                         unsigned long x = (x_src >> 16) * 4;
518
519                         *dst++ = ((((line1[x] * weight1y) >> 16) 
520                                    * weight1x) >> 16)
521                                 + ((((line2[x] * weight2y) >> 16) 
522                                     * weight1x) >> 16)
523                                 + ((((line1[4 + x] * weight1y) >> 16) 
524                                    * weight2x) >> 16)
525                                 + ((((line2[4 + x] * weight2y) >> 16) 
526                                     * weight2x) >> 16);
527
528                         *dst++ = ((((line1[x + 1] * weight1y) >> 16) 
529                                    * weight1x) >> 16)
530                                 + ((((line2[x + 1] * weight2y) >> 16) 
531                                     * weight1x) >> 16)
532                                 + ((((line1[4 + x + 1] * weight1y) >> 16) 
533                                    * weight2x) >> 16)
534                                 + ((((line2[4 + x + 1] * weight2y) >> 16) 
535                                     * weight2x) >> 16);
536
537                         *dst++ = ((((line1[x + 2] * weight1y) >> 16) 
538                                    * weight1x) >> 16)
539                                 + ((((line2[x + 2] * weight2y) >> 16) 
540                                     * weight1x) >> 16)
541                                 + ((((line1[4 + x + 2] * weight1y) >> 16) 
542                                    * weight2x) >> 16)
543                                 + ((((line2[4 + x + 2] * weight2y) >> 16) 
544                                     * weight2x) >> 16);
545
546                         *dst++ = ((((line1[x + 3] * weight1y) >> 16) 
547                                    * weight1x) >> 16)
548                                 + ((((line2[x + 3] * weight2y) >> 16) 
549                                     * weight1x) >> 16)
550                                 + ((((line1[4 + x + 3] * weight1y) >> 16) 
551                                    * weight2x) >> 16)
552                                 + ((((line2[4 + x + 3] * weight2y) >> 16) 
553                                     * weight2x) >> 16);
554
555                         x_src += dx_src;
556                 }
557                 y_src += dy_src;
558         }
559 }
560
561 struct scale_outpix_byte {
562         uintptr_t r;
563         uintptr_t g;
564         uintptr_t b;
565         uintptr_t a;
566
567         uintptr_t weight;
568 };
569
570 static void shrink_picture_byte(
571         unsigned char* src, unsigned char* dst, int src_width, 
572         int src_height, int dst_width, int dst_height)
573 {
574         double ratiox = (double) (dst_width) / (double) (src_width);
575         double ratioy = (double) (dst_height) / (double) (src_height);
576         uintptr_t x_src, dx_dst, x_dst;
577         uintptr_t y_src, dy_dst, y_dst;
578         intptr_t y_counter;
579         unsigned char * dst_begin = dst;
580
581         struct scale_outpix_byte * dst_line1 = NULL;
582         struct scale_outpix_byte * dst_line2 = NULL;
583
584         dst_line1 = (struct scale_outpix_byte*) MEM_callocN(
585                 (dst_width + 1) * sizeof(struct scale_outpix_byte), 
586                 "shrink_picture_byte 1");
587         dst_line2 = (struct scale_outpix_byte*) MEM_callocN(
588                 (dst_width + 1) * sizeof(struct scale_outpix_byte),
589                 "shrink_picture_byte 2");
590
591         dx_dst = 65536.0 * ratiox;
592         dy_dst = 65536.0 * ratioy;
593
594         y_dst = 0;
595         y_counter = 65536;
596         for (y_src = 0; y_src < src_height; y_src++) {
597                 unsigned char* line = src + y_src * 4 * src_width;
598                 uintptr_t weight1y = 65536 - (y_dst & 0xffff);
599                 uintptr_t weight2y = 65536 - weight1y;
600                 x_dst = 0;
601                 for (x_src = 0; x_src < src_width; x_src++) {
602                         uintptr_t weight1x = 65536 - (x_dst & 0xffff);
603                         uintptr_t weight2x = 65536 - weight1x;
604
605                         uintptr_t x = x_dst >> 16;
606
607                         uintptr_t w;
608
609                         w = (weight1y * weight1x) >> 16;
610
611                         dst_line1[x].r += (line[0] * w) >> 16;
612                         dst_line1[x].g += (line[1] * w) >> 16;
613                         dst_line1[x].b += (line[2] * w) >> 16;
614                         dst_line1[x].a += (line[3] * w) >> 16;
615                         dst_line1[x].weight += w;
616
617                         w = (weight2y * weight1x) >> 16;
618
619                         dst_line2[x].r += (line[0] * w) >> 16;
620                         dst_line2[x].g += (line[1] * w) >> 16;
621                         dst_line2[x].b += (line[2] * w) >> 16;
622                         dst_line2[x].a += (line[3] * w) >> 16;
623                         dst_line2[x].weight += w;
624
625                         w = (weight1y * weight2x) >> 16;
626
627                         dst_line1[x+1].r += (line[0] * w) >> 16;
628                         dst_line1[x+1].g += (line[1] * w) >> 16;
629                         dst_line1[x+1].b += (line[2] * w) >> 16;
630                         dst_line1[x+1].a += (line[3] * w) >> 16;
631                         dst_line1[x+1].weight += w;
632
633                         w = (weight2y * weight2x) >> 16;
634
635                         dst_line2[x+1].r += (line[0] * w) >> 16;
636                         dst_line2[x+1].g += (line[1] * w) >> 16;
637                         dst_line2[x+1].b += (line[2] * w) >> 16;
638                         dst_line2[x+1].a += (line[3] * w) >> 16;
639                         dst_line2[x+1].weight += w;
640
641                         x_dst += dx_dst;
642                         line += 4;
643                 }
644
645                 y_dst += dy_dst;
646                 y_counter -= dy_dst;
647                 if (y_counter < 0) {
648                         uintptr_t x;
649                         struct scale_outpix_byte * temp;
650
651                         y_counter += 65536;
652                         
653                         for (x=0; x < dst_width; x++) {
654                                 uintptr_t f = 0x80000000UL
655                                         / dst_line1[x].weight;
656                                 *dst++ = (dst_line1[x].r * f) >> 15;
657                                 *dst++ = (dst_line1[x].g * f) >> 15;
658                                 *dst++ = (dst_line1[x].b * f) >> 15;
659                                 *dst++ = (dst_line1[x].a * f) >> 15;
660                         }
661                         memset(dst_line1, 0, dst_width *
662                                sizeof(struct scale_outpix_byte));
663                         temp = dst_line1;
664                         dst_line1 = dst_line2;
665                         dst_line2 = temp;
666                 }
667         }
668         if (dst - dst_begin < dst_width * dst_height * 4) {
669                 uintptr_t x;
670                 for (x = 0; x < dst_width; x++) {
671                         uintptr_t f = 0x80000000UL / dst_line1[x].weight;
672                         *dst++ = (dst_line1[x].r * f) >> 15;
673                         *dst++ = (dst_line1[x].g * f) >> 15;
674                         *dst++ = (dst_line1[x].b * f) >> 15;
675                         *dst++ = (dst_line1[x].a * f) >> 15;
676                 }
677         }
678         MEM_freeN(dst_line1);
679         MEM_freeN(dst_line2);
680 }
681
682
683 static void q_scale_byte(unsigned char* in, unsigned char* out, int in_width, 
684                          int in_height, int dst_width, int dst_height)
685 {
686         if (dst_width > in_width && dst_height > in_height) {
687                 enlarge_picture_byte(in, out, in_width, in_height,
688                                      dst_width, dst_height);
689         } else if (dst_width < in_width && dst_height < in_height) {
690                 shrink_picture_byte(in, out, in_width, in_height,
691                                     dst_width, dst_height);
692         }
693 }
694
695 static void enlarge_picture_float(
696         float* src, float* dst, int src_width, 
697         int src_height, int dst_width, int dst_height)
698 {
699         double ratiox = (double) (dst_width - 1.0) 
700                 / (double) (src_width - 1.001);
701         double ratioy = (double) (dst_height - 1.0) 
702                 / (double) (src_height - 1.001);
703         uintptr_t x_dst;
704         uintptr_t y_dst;
705         double x_src, dx_src;
706         double y_src, dy_src;
707
708         dx_src = 1.0 / ratiox;
709         dy_src = 1.0 / ratioy;
710
711         y_src = 0;
712         for (y_dst = 0; y_dst < dst_height; y_dst++) {
713                 float* line1 = src + ((int) y_src) * 4 * src_width;
714                 float* line2 = line1 + 4 * src_width;
715                 float weight1y = 1.0 - (y_src - (int) y_src);
716                 float weight2y = 1.0 - weight1y;
717
718                 if ((int) y_src == src_height - 1) {
719                         line2 = line1;
720                 }
721                        
722                 x_src = 0;
723                 for (x_dst = 0; x_dst < dst_width; x_dst++) {
724                         float weight1x = 1.0 - (x_src - (int) x_src);
725                         float weight2x = 1.0 - weight1x;
726
727                         float w11 = weight1y * weight1x;
728                         float w21 = weight2y * weight1x;
729                         float w12 = weight1y * weight2x;
730                         float w22 = weight2y * weight2x;
731
732                         uintptr_t x = ((int) x_src) * 4;
733
734                         *dst++ =  line1[x]     * w11    
735                                 + line2[x]     * w21
736                                 + line1[4 + x] * w12 
737                                 + line2[4 + x] * w22;
738
739                         *dst++ =  line1[x + 1] * w11 
740                                 + line2[x + 1] * w21
741                                 + line1[4 + x + 1] * w12
742                                 + line2[4 + x + 1] * w22;
743
744                         *dst++ =  line1[x + 2] * w11 
745                                 + line2[x + 2] * w21
746                                 + line1[4 + x + 2] * w12  
747                                 + line2[4 + x + 2] * w22;
748
749                         *dst++ =  line1[x + 3] * w11 
750                                 + line2[x + 3] * w21
751                                 + line1[4 + x + 3] * w12  
752                                 + line2[4 + x + 3] * w22;
753
754                         x_src += dx_src;
755                 }
756                 y_src += dy_src;
757         }
758 }
759
760 struct scale_outpix_float {
761         float r;
762         float g;
763         float b;
764         float a;
765
766         float weight;
767 };
768
769 static void shrink_picture_float(
770         float* src, float* dst, int src_width, 
771         int src_height, int dst_width, int dst_height)
772 {
773         double ratiox = (double) (dst_width) / (double) (src_width);
774         double ratioy = (double) (dst_height) / (double) (src_height);
775         uintptr_t x_src;
776         uintptr_t y_src;
777         float dx_dst, x_dst;
778         float dy_dst, y_dst;
779         float y_counter;
780         float * dst_begin = dst;
781
782         struct scale_outpix_float * dst_line1;
783         struct scale_outpix_float * dst_line2;
784
785         dst_line1 = (struct scale_outpix_float*) MEM_callocN(
786                 (dst_width + 1) * sizeof(struct scale_outpix_float), 
787                 "shrink_picture_float 1");
788         dst_line2 = (struct scale_outpix_float*) MEM_callocN(
789                 (dst_width + 1) * sizeof(struct scale_outpix_float),
790                 "shrink_picture_float 2");
791
792         dx_dst = ratiox;
793         dy_dst = ratioy;
794
795         y_dst = 0;
796         y_counter = 1.0;
797         for (y_src = 0; y_src < src_height; y_src++) {
798                 float* line = src + y_src * 4 * src_width;
799                 uintptr_t weight1y = 1.0 - (y_dst - (int) y_dst);
800                 uintptr_t weight2y = 1.0 - weight1y;
801                 x_dst = 0;
802                 for (x_src = 0; x_src < src_width; x_src++) {
803                         uintptr_t weight1x = 1.0 - (x_dst - (int) x_dst);
804                         uintptr_t weight2x = 1.0 - weight1x;
805
806                         uintptr_t x = (int) x_dst;
807
808                         float w;
809
810                         w = weight1y * weight1x;
811
812                         dst_line1[x].r += line[0] * w;
813                         dst_line1[x].g += line[1] * w;
814                         dst_line1[x].b += line[2] * w;
815                         dst_line1[x].a += line[3] * w;
816                         dst_line1[x].weight += w;
817
818                         w = weight2y * weight1x;
819
820                         dst_line2[x].r += line[0] * w;
821                         dst_line2[x].g += line[1] * w;
822                         dst_line2[x].b += line[2] * w;
823                         dst_line2[x].a += line[3] * w;
824                         dst_line2[x].weight += w;
825
826                         w = weight1y * weight2x;
827
828                         dst_line1[x+1].r += line[0] * w;
829                         dst_line1[x+1].g += line[1] * w;
830                         dst_line1[x+1].b += line[2] * w;
831                         dst_line1[x+1].a += line[3] * w;
832                         dst_line1[x+1].weight += w;
833
834                         w = weight2y * weight2x;
835
836                         dst_line2[x+1].r += line[0] * w;
837                         dst_line2[x+1].g += line[1] * w;
838                         dst_line2[x+1].b += line[2] * w;
839                         dst_line2[x+1].a += line[3] * w;
840                         dst_line2[x+1].weight += w;
841
842                         x_dst += dx_dst;
843                         line += 4;
844                 }
845
846                 y_dst += dy_dst;
847                 y_counter -= dy_dst;
848                 if (y_counter < 0) {
849                         uintptr_t x;
850                         struct scale_outpix_float * temp;
851
852                         y_counter += 1.0;
853                         
854                         for (x=0; x < dst_width; x++) {
855                                 float f = 1.0 / dst_line1[x].weight;
856                                 *dst++ = dst_line1[x].r * f;
857                                 *dst++ = dst_line1[x].g * f;
858                                 *dst++ = dst_line1[x].b * f;
859                                 *dst++ = dst_line1[x].a * f;
860                         }
861                         memset(dst_line1, 0, dst_width *
862                                sizeof(struct scale_outpix_float));
863                         temp = dst_line1;
864                         dst_line1 = dst_line2;
865                         dst_line2 = temp;
866                 }
867         }
868         if (dst - dst_begin < dst_width * dst_height * 4) {
869                 uintptr_t x;
870                 for (x = 0; x < dst_width; x++) {
871                         float f = 1.0 / dst_line1[x].weight;
872                         *dst++ = dst_line1[x].r * f;
873                         *dst++ = dst_line1[x].g * f;
874                         *dst++ = dst_line1[x].b * f;
875                         *dst++ = dst_line1[x].a * f;
876                 }
877         }
878         MEM_freeN(dst_line1);
879         MEM_freeN(dst_line2);
880 }
881
882
883 static void q_scale_float(float* in, float* out, int in_width, 
884                          int in_height, int dst_width, int dst_height)
885 {
886         if (dst_width > in_width && dst_height > in_height) {
887                 enlarge_picture_float(in, out, in_width, in_height,
888                                       dst_width, dst_height);
889         } else if (dst_width < in_width && dst_height < in_height) {
890                 shrink_picture_float(in, out, in_width, in_height,
891                                      dst_width, dst_height);
892         }
893 }
894
895 /* q_scale_linear_interpolation (derived from ppmqscale, http://libdv.sf.net)
896
897    q stands for quick _and_ quality :)
898
899    only handles common cases when we either
900
901    scale  both, x and y or
902    shrink both, x and y
903
904    but that is pretty fast:
905    * does only blit once instead of two passes like the old code
906      (fewer cache misses)
907    * uses fixed point integer arithmetic for byte buffers
908    * doesn't branch in tight loops
909
910    Should be comparable in speed to the ImBuf ..._fast functions at least 
911    for byte-buffers.
912
913 */
914 static int q_scale_linear_interpolation(
915         struct ImBuf *ibuf, int newx, int newy)
916 {
917         if ((newx >= ibuf->x && newy <= ibuf->y) ||
918             (newx <= ibuf->x && newy >= ibuf->y)) {
919                 return FALSE;
920         }
921
922         if (ibuf->rect) {
923                 unsigned char * newrect = 
924                         MEM_mallocN(newx * newy * sizeof(int), "q_scale rect");
925                 q_scale_byte((unsigned char *)ibuf->rect, newrect, ibuf->x, ibuf->y,
926                              newx, newy);
927
928                 imb_freerectImBuf(ibuf);
929                 ibuf->mall |= IB_rect;
930                 ibuf->rect = (unsigned int *) newrect;
931         }
932         if (ibuf->rect_float) {
933                 float * newrect = 
934                         MEM_mallocN(newx * newy * 4 *sizeof(float), 
935                                     "q_scale rectfloat");
936                 q_scale_float(ibuf->rect_float, newrect, ibuf->x, ibuf->y,
937                               newx, newy);
938                 imb_freerectfloatImBuf(ibuf);
939                 ibuf->mall |= IB_rectfloat;
940                 ibuf->rect_float = newrect;
941         }
942         ibuf->x = newx;
943         ibuf->y = newy;
944
945         return TRUE;
946 }
947
948 static struct ImBuf *scaledownx(struct ImBuf *ibuf, int newx)
949 {
950         uchar *rect, *_newrect, *newrect;
951         float *rectf, *_newrectf, *newrectf;
952         float sample, add, val[4], nval[4], valf[4], nvalf[4];
953         int x, y, do_rect = 0, do_float = 0;
954
955         rectf= _newrectf= newrectf= NULL; 
956         rect=_newrect= newrect= NULL; 
957         nval[0]=  nval[1]= nval[2]= nval[3]= 0.0f;
958         nvalf[0]=nvalf[1]=nvalf[2]=nvalf[3]= 0.0f;
959         
960         if (ibuf==NULL) return(0);
961         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
962
963         if (ibuf->rect) {
964                 do_rect = 1;
965                 _newrect = MEM_mallocN(newx * ibuf->y * sizeof(int), "scaledownx");
966                 if (_newrect==NULL) return(ibuf);
967         }
968         if (ibuf->rect_float) {
969                 do_float = 1;
970                 _newrectf = MEM_mallocN(newx * ibuf->y * sizeof(float) * 4, "scaledownxf");
971                 if (_newrectf==NULL) {
972                         if (_newrect) MEM_freeN(_newrect);
973                         return(ibuf);
974                 }
975         }
976
977         add = (ibuf->x - 0.001) / newx;
978
979         if (do_rect) {
980                 rect = (uchar *) ibuf->rect;
981                 newrect = _newrect;
982         }
983         if (do_float) {
984                 rectf = ibuf->rect_float;
985                 newrectf = _newrectf;
986         }
987                 
988         for (y = ibuf->y; y>0 ; y--) {
989                 sample = 0.0f;
990                 val[0]=  val[1]= val[2]= val[3]= 0.0f;
991                 valf[0]=valf[1]=valf[2]=valf[3]= 0.0f;
992
993                 for (x = newx ; x>0 ; x--) {
994                         if (do_rect) {
995                                 nval[0] = - val[0] * sample;
996                                 nval[1] = - val[1] * sample;
997                                 nval[2] = - val[2] * sample;
998                                 nval[3] = - val[3] * sample;
999                         }
1000                         if (do_float) {
1001                                 nvalf[0] = - valf[0] * sample;
1002                                 nvalf[1] = - valf[1] * sample;
1003                                 nvalf[2] = - valf[2] * sample;
1004                                 nvalf[3] = - valf[3] * sample;
1005                         }
1006                         
1007                         sample += add;
1008
1009                         while (sample >= 1.0f){
1010                                 sample -= 1.0f;
1011                                 
1012                                 if (do_rect) {
1013                                         nval[0] += rect[0];
1014                                         nval[1] += rect[1];
1015                                         nval[2] += rect[2];
1016                                         nval[3] += rect[3];
1017                                         rect += 4;
1018                                 }
1019                                 if (do_float) {
1020                                         nvalf[0] += rectf[0];
1021                                         nvalf[1] += rectf[1];
1022                                         nvalf[2] += rectf[2];
1023                                         nvalf[3] += rectf[3];
1024                                         rectf += 4;
1025                                 }
1026                         }
1027                         
1028                         if (do_rect) {
1029                                 val[0]= rect[0];val[1]= rect[1];val[2]= rect[2];val[3]= rect[3];
1030                                 rect += 4;
1031                                 
1032                                 newrect[0] = ((nval[0] + sample * val[0])/add + 0.5f);
1033                                 newrect[1] = ((nval[1] + sample * val[1])/add + 0.5f);
1034                                 newrect[2] = ((nval[2] + sample * val[2])/add + 0.5f);
1035                                 newrect[3] = ((nval[3] + sample * val[3])/add + 0.5f);
1036                                 
1037                                 newrect += 4;
1038                         }
1039                         if (do_float) {
1040                                 
1041                                 valf[0]= rectf[0];valf[1]= rectf[1];valf[2]= rectf[2];valf[3]= rectf[3];
1042                                 rectf += 4;
1043                                 
1044                                 newrectf[0] = ((nvalf[0] + sample * valf[0])/add);
1045                                 newrectf[1] = ((nvalf[1] + sample * valf[1])/add);
1046                                 newrectf[2] = ((nvalf[2] + sample * valf[2])/add);
1047                                 newrectf[3] = ((nvalf[3] + sample * valf[3])/add);
1048                                 
1049                                 newrectf += 4;
1050                         }
1051                         
1052                         sample -= 1.0f;
1053                 }
1054         }
1055
1056         if (do_rect) {
1057                 imb_freerectImBuf(ibuf);
1058                 ibuf->mall |= IB_rect;
1059                 ibuf->rect = (unsigned int *) _newrect;
1060         }
1061         if (do_float) {
1062                 imb_freerectfloatImBuf(ibuf);
1063                 ibuf->mall |= IB_rectfloat;
1064                 ibuf->rect_float = _newrectf;
1065         }
1066         
1067         ibuf->x = newx;
1068         return(ibuf);
1069 }
1070
1071
1072 static struct ImBuf *scaledowny(struct ImBuf *ibuf, int newy)
1073 {
1074         uchar *rect, *_newrect, *newrect;
1075         float *rectf, *_newrectf, *newrectf;
1076         float sample, add, val[4], nval[4], valf[4], nvalf[4];
1077         int x, y, skipx, do_rect = 0, do_float = 0;
1078
1079         rectf= _newrectf= newrectf= NULL; 
1080         rect= _newrect= newrect= NULL; 
1081         nval[0]=  nval[1]= nval[2]= nval[3]= 0.0f;
1082         nvalf[0]=nvalf[1]=nvalf[2]=nvalf[3]= 0.0f;
1083
1084         if (ibuf==NULL) return(0);
1085         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
1086
1087         if (ibuf->rect) {
1088                 do_rect = 1;
1089                 _newrect = MEM_mallocN(newy * ibuf->x * sizeof(int), "scaledowny");
1090                 if (_newrect==NULL) return(ibuf);
1091         }
1092         if (ibuf->rect_float) {
1093                 do_float = 1;
1094                 _newrectf = MEM_mallocN(newy * ibuf->x * sizeof(float) * 4, "scaldownyf");
1095                 if (_newrectf==NULL) {
1096                         if (_newrect) MEM_freeN(_newrect);
1097                         return(ibuf);
1098                 }
1099         }
1100
1101         add = (ibuf->y - 0.001) / newy;
1102         skipx = 4 * ibuf->x;
1103
1104         for (x = skipx - 4; x>=0 ; x-= 4) {
1105                 if (do_rect) {
1106                         rect = ((uchar *) ibuf->rect) + x;
1107                         newrect = _newrect + x;
1108                 }
1109                 if (do_float) {
1110                         rectf = ibuf->rect_float + x;
1111                         newrectf = _newrectf + x;
1112                 }
1113                 
1114                 sample = 0.0f;
1115                 val[0]=  val[1]= val[2]= val[3]= 0.0f;
1116                 valf[0]=valf[1]=valf[2]=valf[3]= 0.0f;
1117
1118                 for (y = newy ; y>0 ; y--) {
1119                         if (do_rect) {
1120                                 nval[0] = - val[0] * sample;
1121                                 nval[1] = - val[1] * sample;
1122                                 nval[2] = - val[2] * sample;
1123                                 nval[3] = - val[3] * sample;
1124                         }
1125                         if (do_float) {
1126                                 nvalf[0] = - valf[0] * sample;
1127                                 nvalf[1] = - valf[1] * sample;
1128                                 nvalf[2] = - valf[2] * sample;
1129                                 nvalf[3] = - valf[3] * sample;
1130                         }
1131                         
1132                         sample += add;
1133
1134                         while (sample >= 1.0) {
1135                                 sample -= 1.0;
1136                                 
1137                                 if (do_rect) {
1138                                         nval[0] += rect[0];
1139                                         nval[1] += rect[1];
1140                                         nval[2] += rect[2];
1141                                         nval[3] += rect[3];
1142                                         rect += skipx;
1143                                 }
1144                                 if (do_float) {
1145                                         nvalf[0] += rectf[0];
1146                                         nvalf[1] += rectf[1];
1147                                         nvalf[2] += rectf[2];
1148                                         nvalf[3] += rectf[3];
1149                                         rectf += skipx;
1150                                 }
1151                         }
1152
1153                         if (do_rect) {
1154                                 val[0]= rect[0];val[1]= rect[1];val[2]= rect[2];val[3]= rect[3];
1155                                 rect += skipx;
1156                                 
1157                                 newrect[0] = ((nval[0] + sample * val[0])/add + 0.5f);
1158                                 newrect[1] = ((nval[1] + sample * val[1])/add + 0.5f);
1159                                 newrect[2] = ((nval[2] + sample * val[2])/add + 0.5f);
1160                                 newrect[3] = ((nval[3] + sample * val[3])/add + 0.5f);
1161                                 
1162                                 newrect += skipx;
1163                         }
1164                         if (do_float) {
1165                                 
1166                                 valf[0]= rectf[0];valf[1]= rectf[1];valf[2]= rectf[2];valf[3]= rectf[3];
1167                                 rectf += skipx;
1168                                 
1169                                 newrectf[0] = ((nvalf[0] + sample * valf[0])/add);
1170                                 newrectf[1] = ((nvalf[1] + sample * valf[1])/add);
1171                                 newrectf[2] = ((nvalf[2] + sample * valf[2])/add);
1172                                 newrectf[3] = ((nvalf[3] + sample * valf[3])/add);
1173                                 
1174                                 newrectf += skipx;
1175                         }
1176                         
1177                         sample -= 1.0;
1178                 }
1179         }       
1180
1181         if (do_rect) {
1182                 imb_freerectImBuf(ibuf);
1183                 ibuf->mall |= IB_rect;
1184                 ibuf->rect = (unsigned int *) _newrect;
1185         }
1186         if (do_float) {
1187                 imb_freerectfloatImBuf(ibuf);
1188                 ibuf->mall |= IB_rectfloat;
1189                 ibuf->rect_float = (float *) _newrectf;
1190         }
1191         
1192         ibuf->y = newy;
1193         return(ibuf);
1194 }
1195
1196
1197 static struct ImBuf *scaleupx(struct ImBuf *ibuf, int newx)
1198 {
1199         uchar *rect,*_newrect=NULL,*newrect;
1200         float *rectf,*_newrectf=NULL,*newrectf;
1201         float sample,add;
1202         float val_a,nval_a,diff_a;
1203         float val_b,nval_b,diff_b;
1204         float val_g,nval_g,diff_g;
1205         float val_r,nval_r,diff_r;
1206         float val_af,nval_af,diff_af;
1207         float val_bf,nval_bf,diff_bf;
1208         float val_gf,nval_gf,diff_gf;
1209         float val_rf,nval_rf,diff_rf;
1210         int x,y, do_rect = 0, do_float = 0;
1211
1212         val_a = nval_a = diff_a = val_b = nval_b = diff_b = 0;
1213         val_g = nval_g = diff_g = val_r = nval_r = diff_r = 0;
1214         val_af = nval_af = diff_af = val_bf = nval_bf = diff_bf = 0;
1215         val_gf = nval_gf = diff_gf = val_rf = nval_rf = diff_rf = 0;
1216         if (ibuf==NULL) return(0);
1217         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
1218
1219         if (ibuf->rect) {
1220                 do_rect = 1;
1221                 _newrect = MEM_mallocN(newx * ibuf->y * sizeof(int), "scaleupx");
1222                 if (_newrect==NULL) return(ibuf);
1223         }
1224         if (ibuf->rect_float) {
1225                 do_float = 1;
1226                 _newrectf = MEM_mallocN(newx * ibuf->y * sizeof(float) * 4, "scaleupxf");
1227                 if (_newrectf==NULL) {
1228                         if (_newrect) MEM_freeN(_newrect);
1229                         return(ibuf);
1230                 }
1231         }
1232
1233         add = (ibuf->x - 1.001) / (newx - 1.0);
1234
1235         rect = (uchar *) ibuf->rect;
1236         rectf = (float *) ibuf->rect_float;
1237         newrect = _newrect;
1238         newrectf = _newrectf;
1239
1240         for (y = ibuf->y; y>0 ; y--){
1241
1242                 sample = 0;
1243                 
1244                 if (do_rect) {
1245                         val_a = rect[0] ;
1246                         nval_a = rect[4];
1247                         diff_a = nval_a - val_a ;
1248                         val_a += 0.5;
1249
1250                         val_b = rect[1] ;
1251                         nval_b = rect[5];
1252                         diff_b = nval_b - val_b ;
1253                         val_b += 0.5;
1254
1255                         val_g = rect[2] ;
1256                         nval_g = rect[6];
1257                         diff_g = nval_g - val_g ;
1258                         val_g += 0.5;
1259
1260                         val_r = rect[3] ;
1261                         nval_r = rect[7];
1262                         diff_r = nval_r - val_r ;
1263                         val_r += 0.5;
1264
1265                         rect += 8;
1266                 }
1267                 if (do_float) {
1268                         val_af = rectf[0] ;
1269                         nval_af = rectf[4];
1270                         diff_af = nval_af - val_af;
1271         
1272                         val_bf = rectf[1] ;
1273                         nval_bf = rectf[5];
1274                         diff_bf = nval_bf - val_bf;
1275
1276                         val_gf = rectf[2] ;
1277                         nval_gf = rectf[6];
1278                         diff_gf = nval_gf - val_gf;
1279
1280                         val_rf = rectf[3] ;
1281                         nval_rf = rectf[7];
1282                         diff_rf = nval_rf - val_rf;
1283
1284                         rectf += 8;
1285                 }
1286                 for (x = newx ; x>0 ; x--){
1287                         if (sample >= 1.0){
1288                                 sample -= 1.0;
1289
1290                                 if (do_rect) {
1291                                         val_a = nval_a ;
1292                                         nval_a = rect[0] ;
1293                                         diff_a = nval_a - val_a ;
1294                                         val_a += 0.5;
1295
1296                                         val_b = nval_b ;
1297                                         nval_b = rect[1] ;
1298                                         diff_b = nval_b - val_b ;
1299                                         val_b += 0.5;
1300
1301                                         val_g = nval_g ;
1302                                         nval_g = rect[2] ;
1303                                         diff_g = nval_g - val_g ;
1304                                         val_g += 0.5;
1305
1306                                         val_r = nval_r ;
1307                                         nval_r = rect[3] ;
1308                                         diff_r = nval_r - val_r ;
1309                                         val_r += 0.5;
1310                                         rect += 4;
1311                                 }
1312                                 if (do_float) {
1313                                         val_af = nval_af ;
1314                                         nval_af = rectf[0] ;
1315                                         diff_af = nval_af - val_af ;
1316         
1317                                         val_bf = nval_bf ;
1318                                         nval_bf = rectf[1] ;
1319                                         diff_bf = nval_bf - val_bf ;
1320
1321                                         val_gf = nval_gf ;
1322                                         nval_gf = rectf[2] ;
1323                                         diff_gf = nval_gf - val_gf ;
1324
1325                                         val_rf = nval_rf ;
1326                                         nval_rf = rectf[3] ;
1327                                         diff_rf = nval_rf - val_rf;
1328                                         rectf += 4;
1329                                 }
1330                         }
1331                         if (do_rect) {
1332                                 newrect[0] = val_a + sample * diff_a;
1333                                 newrect[1] = val_b + sample * diff_b;
1334                                 newrect[2] = val_g + sample * diff_g;
1335                                 newrect[3] = val_r + sample * diff_r;
1336                                 newrect += 4;
1337                         }
1338                         if (do_float) {
1339                                 newrectf[0] = val_af + sample * diff_af;
1340                                 newrectf[1] = val_bf + sample * diff_bf;
1341                                 newrectf[2] = val_gf + sample * diff_gf;
1342                                 newrectf[3] = val_rf + sample * diff_rf;
1343                                 newrectf += 4;
1344                         }
1345                         sample += add;
1346                 }
1347         }
1348
1349         if (do_rect) {
1350                 imb_freerectImBuf(ibuf);
1351                 ibuf->mall |= IB_rect;
1352                 ibuf->rect = (unsigned int *) _newrect;
1353         }
1354         if (do_float) {
1355                 imb_freerectfloatImBuf(ibuf);
1356                 ibuf->mall |= IB_rectfloat;
1357                 ibuf->rect_float = (float *) _newrectf;
1358         }
1359         
1360         ibuf->x = newx;
1361         return(ibuf);
1362 }
1363
1364 static struct ImBuf *scaleupy(struct ImBuf *ibuf, int newy)
1365 {
1366         uchar *rect,*_newrect=NULL,*newrect;
1367         float *rectf,*_newrectf=NULL,*newrectf;
1368         float sample,add;
1369         float val_a,nval_a,diff_a;
1370         float val_b,nval_b,diff_b;
1371         float val_g,nval_g,diff_g;
1372         float val_r,nval_r,diff_r;
1373         float val_af,nval_af,diff_af;
1374         float val_bf,nval_bf,diff_bf;
1375         float val_gf,nval_gf,diff_gf;
1376         float val_rf,nval_rf,diff_rf;
1377         int x,y, do_rect = 0, do_float = 0, skipx;
1378
1379         val_a = nval_a = diff_a = val_b = nval_b = diff_b = 0;
1380         val_g = nval_g = diff_g = val_r = nval_r = diff_r = 0;
1381         val_af = nval_af = diff_af = val_bf = nval_bf = diff_bf = 0;
1382         val_gf = nval_gf = diff_gf = val_rf = nval_rf = diff_rf = 0;
1383         if (ibuf==NULL) return(0);
1384         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
1385
1386         if (ibuf->rect) {
1387                 do_rect = 1;
1388                 _newrect = MEM_mallocN(ibuf->x * newy * sizeof(int), "scaleupy");
1389                 if (_newrect==NULL) return(ibuf);
1390         }
1391         if (ibuf->rect_float) {
1392                 do_float = 1;
1393                 _newrectf = MEM_mallocN(ibuf->x * newy * sizeof(float) * 4, "scaleupyf");
1394                 if (_newrectf==NULL) {
1395                         if (_newrect) MEM_freeN(_newrect);
1396                         return(ibuf);
1397                 }
1398         }
1399
1400         add = (ibuf->y - 1.001) / (newy - 1.0);
1401         skipx = 4 * ibuf->x;
1402
1403         rect = (uchar *) ibuf->rect;
1404         rectf = (float *) ibuf->rect_float;
1405         newrect = _newrect;
1406         newrectf = _newrectf;
1407
1408         for (x = ibuf->x; x>0 ; x--){
1409
1410                 sample = 0;
1411                 if (do_rect) {
1412                         rect = ((uchar *)ibuf->rect) + 4*(x-1);
1413                         newrect = _newrect + 4*(x-1);
1414
1415                         val_a = rect[0] ;
1416                         nval_a = rect[skipx];
1417                         diff_a = nval_a - val_a ;
1418                         val_a += 0.5;
1419
1420                         val_b = rect[1] ;
1421                         nval_b = rect[skipx+1];
1422                         diff_b = nval_b - val_b ;
1423                         val_b += 0.5;
1424
1425                         val_g = rect[2] ;
1426                         nval_g = rect[skipx+2];
1427                         diff_g = nval_g - val_g ;
1428                         val_g += 0.5;
1429
1430                         val_r = rect[3] ;
1431                         nval_r = rect[skipx+4];
1432                         diff_r = nval_r - val_r ;
1433                         val_r += 0.5;
1434
1435                         rect += 2*skipx;
1436                 }
1437                 if (do_float) {
1438                         rectf = ((float *)ibuf->rect_float) + 4*(x-1);
1439                         newrectf = _newrectf + 4*(x-1);
1440
1441                         val_af = rectf[0] ;
1442                         nval_af = rectf[skipx];
1443                         diff_af = nval_af - val_af;
1444         
1445                         val_bf = rectf[1] ;
1446                         nval_bf = rectf[skipx+1];
1447                         diff_bf = nval_bf - val_bf;
1448
1449                         val_gf = rectf[2] ;
1450                         nval_gf = rectf[skipx+2];
1451                         diff_gf = nval_gf - val_gf;
1452
1453                         val_rf = rectf[3] ;
1454                         nval_rf = rectf[skipx+3];
1455                         diff_rf = nval_rf - val_rf;
1456
1457                         rectf += 2*skipx;
1458                 }
1459                 
1460                 for (y = newy ; y>0 ; y--){
1461                         if (sample >= 1.0){
1462                                 sample -= 1.0;
1463
1464                                 if (do_rect) {
1465                                         val_a = nval_a ;
1466                                         nval_a = rect[0] ;
1467                                         diff_a = nval_a - val_a ;
1468                                         val_a += 0.5;
1469
1470                                         val_b = nval_b ;
1471                                         nval_b = rect[1] ;
1472                                         diff_b = nval_b - val_b ;
1473                                         val_b += 0.5;
1474
1475                                         val_g = nval_g ;
1476                                         nval_g = rect[2] ;
1477                                         diff_g = nval_g - val_g ;
1478                                         val_g += 0.5;
1479
1480                                         val_r = nval_r ;
1481                                         nval_r = rect[3] ;
1482                                         diff_r = nval_r - val_r ;
1483                                         val_r += 0.5;
1484                                         rect += skipx;
1485                                 }
1486                                 if (do_float) {
1487                                         val_af = nval_af ;
1488                                         nval_af = rectf[0] ;
1489                                         diff_af = nval_af - val_af ;
1490         
1491                                         val_bf = nval_bf ;
1492                                         nval_bf = rectf[1] ;
1493                                         diff_bf = nval_bf - val_bf ;
1494
1495                                         val_gf = nval_gf ;
1496                                         nval_gf = rectf[2] ;
1497                                         diff_gf = nval_gf - val_gf ;
1498
1499                                         val_rf = nval_rf ;
1500                                         nval_rf = rectf[3] ;
1501                                         diff_rf = nval_rf - val_rf;
1502                                         rectf += skipx;
1503                                 }
1504                         }
1505                         if (do_rect) {
1506                                 newrect[0] = val_a + sample * diff_a;
1507                                 newrect[1] = val_b + sample * diff_b;
1508                                 newrect[2] = val_g + sample * diff_g;
1509                                 newrect[3] = val_r + sample * diff_r;
1510                                 newrect += skipx;
1511                         }
1512                         if (do_float) {
1513                                 newrectf[0] = val_af + sample * diff_af;
1514                                 newrectf[1] = val_bf + sample * diff_bf;
1515                                 newrectf[2] = val_gf + sample * diff_gf;
1516                                 newrectf[3] = val_rf + sample * diff_rf;
1517                                 newrectf += skipx;
1518                         }
1519                         sample += add;
1520                 }
1521         }
1522
1523         if (do_rect) {
1524                 imb_freerectImBuf(ibuf);
1525                 ibuf->mall |= IB_rect;
1526                 ibuf->rect = (unsigned int *) _newrect;
1527         }
1528         if (do_float) {
1529                 imb_freerectfloatImBuf(ibuf);
1530                 ibuf->mall |= IB_rectfloat;
1531                 ibuf->rect_float = (float *) _newrectf;
1532         }
1533         
1534         ibuf->y = newy;
1535         return(ibuf);
1536 }
1537
1538
1539 /* no float buf needed here! */
1540 static void scalefast_Z_ImBuf(ImBuf *ibuf, short newx, short newy)
1541 {
1542         unsigned int *rect, *_newrect, *newrect;
1543         int x, y;
1544         int ofsx, ofsy, stepx, stepy;
1545
1546         if (ibuf->zbuf) {
1547                 _newrect = MEM_mallocN(newx * newy * sizeof(int), "z rect");
1548                 if (_newrect==NULL) return;
1549                 
1550                 stepx = (65536.0 * (ibuf->x - 1.0) / (newx - 1.0)) + 0.5;
1551                 stepy = (65536.0 * (ibuf->y - 1.0) / (newy - 1.0)) + 0.5;
1552                 ofsy = 32768;
1553
1554                 newrect = _newrect;
1555         
1556                 for (y = newy; y > 0 ; y--){
1557                         rect = (unsigned int*) ibuf->zbuf;
1558                         rect += (ofsy >> 16) * ibuf->x;
1559                         ofsy += stepy;
1560                         ofsx = 32768;
1561                         for (x = newx ; x > 0 ; x--){
1562                                 *newrect++ = rect[ofsx >> 16];
1563                                 ofsx += stepx;
1564                         }
1565                 }
1566         
1567                 IMB_freezbufImBuf(ibuf);
1568                 ibuf->mall |= IB_zbuf;
1569                 ibuf->zbuf = (int*) _newrect;
1570         }
1571 }
1572
1573 struct ImBuf *IMB_scaleImBuf(struct ImBuf * ibuf, short newx, short newy)
1574 {
1575         if (ibuf==NULL) return (0);
1576         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
1577         
1578         if (newx == ibuf->x && newy == ibuf->y) { return ibuf; }
1579
1580         /* scaleup / scaledown functions below change ibuf->x and ibuf->y
1581            so we first scale the Z-buffer (if any) */
1582         scalefast_Z_ImBuf(ibuf, newx, newy);
1583
1584         /* try to scale common cases in a fast way */
1585         if (q_scale_linear_interpolation(ibuf, newx, newy)) {
1586                 return ibuf;
1587         }
1588
1589         if (newx < ibuf->x) if (newx) scaledownx(ibuf,newx);
1590         if (newy < ibuf->y) if (newy) scaledowny(ibuf,newy);
1591         if (newx > ibuf->x) if (newx) scaleupx(ibuf,newx);
1592         if (newy > ibuf->y) if (newy) scaleupy(ibuf,newy);
1593         
1594         return(ibuf);
1595 }
1596
1597 struct imbufRGBA {
1598         float r, g, b, a;
1599 };
1600
1601 struct ImBuf *IMB_scalefastImBuf(struct ImBuf *ibuf, short newx, short newy)
1602 {
1603         unsigned int *rect,*_newrect,*newrect;
1604         struct imbufRGBA *rectf, *_newrectf, *newrectf;
1605         int x,y, do_float=0, do_rect=0;
1606         int ofsx,ofsy,stepx,stepy;
1607
1608         rect = NULL; _newrect = NULL; newrect = NULL;
1609         rectf = NULL; _newrectf = NULL; newrectf = NULL;
1610
1611         if (ibuf==NULL) return(0);
1612         if (ibuf->rect) do_rect = 1;
1613         if (ibuf->rect_float) do_float = 1;
1614         if (do_rect==0 && do_float==0) return(ibuf);
1615         
1616         if (newx == ibuf->x && newy == ibuf->y) return(ibuf);
1617         
1618         if(do_rect) {
1619                 _newrect = MEM_mallocN(newx * newy * sizeof(int), "scalefastimbuf");
1620                 if (_newrect==NULL) return(ibuf);
1621                 newrect = _newrect;
1622         }
1623         
1624         if (do_float) {
1625                 _newrectf = MEM_mallocN(newx * newy * sizeof(float) * 4, "scalefastimbuf f");
1626                 if (_newrectf==NULL) {
1627                         if (_newrect) MEM_freeN(_newrect);
1628                         return(ibuf);
1629                 }
1630                 newrectf = _newrectf;
1631         }
1632
1633         stepx = (65536.0 * (ibuf->x - 1.0) / (newx - 1.0)) + 0.5;
1634         stepy = (65536.0 * (ibuf->y - 1.0) / (newy - 1.0)) + 0.5;
1635         ofsy = 32768;
1636
1637         for (y = newy; y > 0 ; y--){
1638                 if(do_rect) {
1639                         rect = ibuf->rect;
1640                         rect += (ofsy >> 16) * ibuf->x;
1641                 }
1642                 if (do_float) {
1643                         rectf = (struct imbufRGBA *)ibuf->rect_float;
1644                         rectf += (ofsy >> 16) * ibuf->x;
1645                 }
1646                 ofsy += stepy;
1647                 ofsx = 32768;
1648                 
1649                 if (do_rect) {
1650                         for (x = newx ; x>0 ; x--){
1651                                 *newrect++ = rect[ofsx >> 16];
1652                                 ofsx += stepx;
1653                         }
1654                 }
1655
1656                 if (do_float) {
1657                         ofsx = 32768;
1658                         for (x = newx ; x>0 ; x--){
1659                                 *newrectf++ = rectf[ofsx >> 16];
1660                                 ofsx += stepx;
1661                         }
1662                 }
1663         }
1664
1665         if (do_rect) {
1666                 imb_freerectImBuf(ibuf);
1667                 ibuf->mall |= IB_rect;
1668                 ibuf->rect = _newrect;
1669         }
1670         
1671         if (do_float) {
1672                 imb_freerectfloatImBuf(ibuf);
1673                 ibuf->mall |= IB_rectfloat;
1674                 ibuf->rect_float = (float *)_newrectf;
1675         }
1676         
1677         scalefast_Z_ImBuf(ibuf, newx, newy);
1678         
1679         ibuf->x = newx;
1680         ibuf->y = newy;
1681         return(ibuf);
1682 }
1683
1684
1685 static struct ImBuf *generic_fieldscale(struct ImBuf *ibuf, short newx, short newy, struct ImBuf *(*scalefunc)(ImBuf *, short, short) )
1686 {
1687         struct ImBuf *sbuf1, *sbuf2;
1688         
1689         sbuf1 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, ibuf->depth, ibuf->flags, 0);
1690         sbuf2 = IMB_allocImBuf(ibuf->x, ibuf->y / 2, ibuf->depth, ibuf->flags, 0);
1691         
1692         ibuf->x *= 2;
1693         
1694         /* more args needed, 0 assumed... (nzc) */
1695         IMB_rectcpy(sbuf1, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
1696         IMB_rectcpy(sbuf2, ibuf, 0, 0, sbuf2->x, 0, ibuf->x, ibuf->y);
1697         
1698         imb_freerectImBuf(ibuf);
1699         imb_freerectfloatImBuf(ibuf);
1700         
1701         ibuf->x = newx;
1702         ibuf->y = newy;
1703         
1704         imb_addrectImBuf(ibuf);
1705         if(ibuf->flags & IB_rectfloat)
1706                 imb_addrectfloatImBuf(ibuf);
1707                 
1708         scalefunc(sbuf1, newx, newy / 2);
1709         scalefunc(sbuf2, newx, newy / 2);       
1710         
1711         ibuf->x *= 2;
1712         
1713         /* more args needed, 0 assumed... (nzc) */
1714         IMB_rectcpy(ibuf, sbuf1, 0, 0, 0, 0, sbuf1->x, sbuf1->y);
1715         IMB_rectcpy(ibuf, sbuf2, sbuf2->x, 0, 0, 0, sbuf2->x, sbuf2->y);
1716         
1717         ibuf->x /= 2;
1718         
1719         IMB_freeImBuf(sbuf1);
1720         IMB_freeImBuf(sbuf2);
1721         
1722         return(ibuf);
1723 }
1724
1725
1726 struct ImBuf *IMB_scalefastfieldImBuf(struct ImBuf *ibuf, short newx, short newy)
1727 {
1728         return(generic_fieldscale(ibuf, newx, newy, IMB_scalefastImBuf));
1729 }
1730
1731 struct ImBuf *IMB_scalefieldImBuf(struct ImBuf *ibuf, short newx, short newy)
1732 {
1733         return(generic_fieldscale(ibuf, newx, newy, IMB_scaleImBuf));
1734 }
1735