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