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