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