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