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