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