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