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