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