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