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