imbuf, mathutils & readfile: floats were being implicitly promoted to doubles, adjust...
[blender.git] / source / blender / imbuf / intern / scaling.c
1 /*
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  * allocimbuf.c
28  *
29  * $Id$
30  */
31
32 /** \file blender/imbuf/intern/scaling.c
33  *  \ingroup imbuf
34  */
35
36
37 #include "BLI_blenlib.h"
38 #include "BLI_utildefines.h"
39 #include "MEM_guardedalloc.h"
40
41 #include "imbuf.h"
42 #include "IMB_imbuf_types.h"
43 #include "IMB_imbuf.h"
44
45 #include "IMB_allocimbuf.h"
46 #include "IMB_filter.h"
47
48 #include "BLO_sys_types.h" // for intptr_t support
49
50 /************************************************************************/
51 /*                                                              SCALING                                                                 */
52 /************************************************************************/
53
54
55 struct ImBuf *IMB_half_x(struct ImBuf *ibuf1)
56 {
57         struct ImBuf *ibuf2;
58         uchar *p1,*_p1,*dest;
59         short a,r,g,b;
60         int x,y;
61         float af,rf,gf,bf, *p1f, *_p1f, *destf;
62         int do_rect, do_float;
63
64         if (ibuf1==NULL) return (NULL);
65         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (NULL);
66
67         do_rect= (ibuf1->rect != NULL);
68         do_float= (ibuf1->rect_float != NULL);
69         
70         if (ibuf1->x <= 1) return(IMB_dupImBuf(ibuf1));
71         
72         ibuf2 = IMB_allocImBuf((ibuf1->x)/2, ibuf1->y, ibuf1->depth, ibuf1->flags);
73         if (ibuf2==NULL) return (NULL);
74
75         _p1 = (uchar *) ibuf1->rect;
76         dest=(uchar *) ibuf2->rect;
77          
78         _p1f = ibuf1->rect_float;
79         destf= ibuf2->rect_float;
80
81         for(y=ibuf2->y;y>0;y--){
82                 p1 = _p1;
83                 p1f = _p1f;
84                 for(x = ibuf2->x ; x>0 ; x--){
85                         if (do_rect) {
86                                 a = *(p1++) ;
87                                 b = *(p1++) ;
88                                 g = *(p1++) ;
89                                 r = *(p1++);
90                                 a += *(p1++) ;
91                                 b += *(p1++) ;
92                                 g += *(p1++) ;
93                                 r += *(p1++);
94                                 *(dest++) = a >> 1;
95                                 *(dest++) = b >> 1;
96                                 *(dest++) = g >> 1;
97                                 *(dest++) = r >> 1;
98                         }
99                         if (do_float) {
100                                 af = *(p1f++);
101                                 bf = *(p1f++);
102                                 gf = *(p1f++);
103                                 rf = *(p1f++);
104                                 af += *(p1f++);
105                                 bf += *(p1f++);
106                                 gf += *(p1f++);
107                                 rf += *(p1f++);
108                                 *(destf++) = 0.5f*af;
109                                 *(destf++) = 0.5f*bf;
110                                 *(destf++) = 0.5f*gf;
111                                 *(destf++) = 0.5f*rf;
112                         }
113                 }
114                 if (do_rect) _p1 += (ibuf1->x << 2);
115                 if (do_float) _p1f += (ibuf1->x << 2);
116         }
117         return (ibuf2);
118 }
119
120
121 struct ImBuf *IMB_double_fast_x(struct ImBuf *ibuf1)
122 {
123         struct ImBuf *ibuf2;
124         int *p1,*dest, i, col, do_rect, do_float;
125         float *p1f, *destf;
126
127         if (ibuf1==NULL) return (NULL);
128         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (NULL);
129
130         do_rect= (ibuf1->rect != NULL);
131         do_float= (ibuf1->rect_float != NULL);
132         
133         ibuf2 = IMB_allocImBuf(2 * ibuf1->x , ibuf1->y , ibuf1->depth, ibuf1->flags);
134         if (ibuf2==NULL) return (NULL);
135
136         p1 = (int *) ibuf1->rect;
137         dest=(int *) ibuf2->rect;
138         p1f = (float *)ibuf1->rect_float;
139         destf = (float *)ibuf2->rect_float;
140
141         for(i = ibuf1->y * ibuf1->x ; i>0 ; i--) {
142                 if (do_rect) {
143                         col = *p1++;
144                         *dest++ = col;
145                         *dest++ = col;
146                 }
147                 if (do_float) {
148                         destf[0]= destf[4] =p1f[0];
149                         destf[1]= destf[5] =p1f[1];
150                         destf[2]= destf[6] =p1f[2];
151                         destf[3]= destf[7] =p1f[3];
152                         destf+= 8;
153                         p1f+= 4;
154                 }
155         }
156
157         return (ibuf2);
158 }
159
160 struct ImBuf *IMB_double_x(struct ImBuf *ibuf1)
161 {
162         struct ImBuf *ibuf2;
163
164         if (ibuf1==NULL) return (NULL);
165         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (NULL);
166
167         ibuf2 = IMB_double_fast_x(ibuf1);
168
169         imb_filterx(ibuf2);
170         return (ibuf2);
171 }
172
173
174 struct ImBuf *IMB_half_y(struct ImBuf *ibuf1)
175 {
176         struct ImBuf *ibuf2;
177         uchar *p1,*p2,*_p1,*dest;
178         short a,r,g,b;
179         int x,y;
180         int do_rect, do_float;
181         float af,rf,gf,bf,*p1f,*p2f,*_p1f,*destf;
182
183         p1= p2= NULL;
184         p1f= p2f= NULL;
185         if (ibuf1==NULL) return (NULL);
186         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (NULL);
187         if (ibuf1->y <= 1) return(IMB_dupImBuf(ibuf1));
188
189         do_rect= (ibuf1->rect != NULL);
190         do_float= (ibuf1->rect_float != NULL);
191
192         ibuf2 = IMB_allocImBuf(ibuf1->x , (ibuf1->y) / 2 , ibuf1->depth, ibuf1->flags);
193         if (ibuf2==NULL) return (NULL);
194
195         _p1 = (uchar *) ibuf1->rect;
196         dest=(uchar *) ibuf2->rect;
197         _p1f = (float *) ibuf1->rect_float;
198         destf= (float *) ibuf2->rect_float;
199
200         for(y=ibuf2->y ; y>0 ; y--){
201                 if (do_rect) {
202                         p1 = _p1;
203                         p2 = _p1 + (ibuf1->x << 2);
204                 }
205                 if (do_float) {
206                         p1f = _p1f;
207                         p2f = _p1f + (ibuf1->x << 2);
208                 }
209                 for(x = ibuf2->x ; x>0 ; x--){
210                         if (do_rect) {
211                                 a = *(p1++) ;
212                                 b = *(p1++) ;
213                                 g = *(p1++) ;
214                                 r = *(p1++);
215                                 a += *(p2++) ;
216                                 b += *(p2++) ;
217                                 g += *(p2++) ;
218                                 r += *(p2++);
219                                 *(dest++) = a >> 1;
220                                 *(dest++) = b >> 1;
221                                 *(dest++) = g >> 1;
222                                 *(dest++) = r >> 1;
223                         }
224                         if (do_float) {
225                                 af = *(p1f++) ;
226                                 bf = *(p1f++) ;
227                                 gf = *(p1f++) ;
228                                 rf = *(p1f++);
229                                 af += *(p2f++) ;
230                                 bf += *(p2f++) ;
231                                 gf += *(p2f++) ;
232                                 rf += *(p2f++);
233                                 *(destf++) = 0.5f*af;
234                                 *(destf++) = 0.5f*bf;
235                                 *(destf++) = 0.5f*gf;
236                                 *(destf++) = 0.5f*rf;
237                         }
238                 }
239                 if (do_rect) _p1 += (ibuf1->x << 3);
240                 if (do_float) _p1f += (ibuf1->x << 3);
241         }
242         return (ibuf2);
243 }
244
245
246 struct ImBuf *IMB_double_fast_y(struct ImBuf *ibuf1)
247 {
248         struct ImBuf *ibuf2;
249         int *p1, *dest1, *dest2;
250         float *p1f, *dest1f, *dest2f;
251         int x,y;
252         int do_rect, do_float;
253
254         if (ibuf1==NULL) return (NULL);
255         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (NULL);
256
257         do_rect= (ibuf1->rect != NULL);
258         do_float= (ibuf1->rect_float != NULL);
259
260         ibuf2 = IMB_allocImBuf(ibuf1->x , 2 * ibuf1->y , ibuf1->depth, ibuf1->flags);
261         if (ibuf2==NULL) return (NULL);
262
263         p1 = (int *) ibuf1->rect;
264         dest1= (int *) ibuf2->rect;
265         p1f = (float *) ibuf1->rect_float;
266         dest1f= (float *) ibuf2->rect_float;
267
268         for(y = ibuf1->y ; y>0 ; y--){
269                 if (do_rect) {
270                         dest2 = dest1 + ibuf2->x;
271                         for(x = ibuf2->x ; x>0 ; x--) *dest1++ = *dest2++ = *p1++;
272                         dest1 = dest2;
273                 }
274                  if (do_float) {
275                         dest2f = dest1f + (4*ibuf2->x);
276                         for(x = ibuf2->x*4 ; x>0 ; x--) *dest1f++ = *dest2f++ = *p1f++;
277                         dest1f = dest2f;
278                 }
279         }
280
281         return (ibuf2);
282 }
283
284 struct ImBuf *IMB_double_y(struct ImBuf *ibuf1)
285 {
286         struct ImBuf *ibuf2;
287
288         if (ibuf1==NULL) return (NULL);
289         if (ibuf1->rect==NULL) return (NULL);
290
291         ibuf2 = IMB_double_fast_y(ibuf1);
292         
293         IMB_filtery(ibuf2);
294         return (ibuf2);
295 }
296
297 /* result in ibuf2, scaling should be done correctly */
298 void imb_onehalf_no_alloc(struct ImBuf *ibuf2, struct ImBuf *ibuf1)
299 {
300         uchar *p1, *p2 = NULL, *dest;
301         float *p1f, *destf, *p2f = NULL;
302         int x,y;
303         int do_rect, do_float;
304
305         do_rect= (ibuf1->rect != NULL);
306         
307         p1f = ibuf1->rect_float;
308         destf=ibuf2->rect_float;
309         p1 = (uchar *) ibuf1->rect;
310         dest=(uchar *) ibuf2->rect;
311
312         do_float= (ibuf1->rect_float != NULL && ibuf2->rect_float != NULL);
313
314         for(y=ibuf2->y;y>0;y--){
315                 if (do_rect) p2 = p1 + (ibuf1->x << 2);
316                 if (do_float) p2f = p1f + (ibuf1->x << 2);
317                 for(x=ibuf2->x;x>0;x--){
318                         if (do_rect) {
319                                 dest[0] = (p1[0] + p2[0] + p1[4] + p2[4]) >> 2;
320                                 dest[1] = (p1[1] + p2[1] + p1[5] + p2[5]) >> 2;
321                                 dest[2] = (p1[2] + p2[2] + p1[6] + p2[6]) >> 2;
322                                 dest[3] = (p1[3] + p2[3] + p1[7] + p2[7]) >> 2;
323                                 p1 += 8; 
324                                 p2 += 8; 
325                                 dest += 4;
326                         }
327                         if (do_float){ 
328                                 destf[0] = 0.25f*(p1f[0] + p2f[0] + p1f[4] + p2f[4]);
329                                 destf[1] = 0.25f*(p1f[1] + p2f[1] + p1f[5] + p2f[5]);
330                                 destf[2] = 0.25f*(p1f[2] + p2f[2] + p1f[6] + p2f[6]);
331                                 destf[3] = 0.25f*(p1f[3] + p2f[3] + p1f[7] + p2f[7]);
332                                 p1f += 8; 
333                                 p2f += 8; 
334                                 destf += 4;
335                         }
336                 }
337                 if (do_rect) p1=p2;
338                 if (do_float) p1f=p2f;
339                 if(ibuf1->x & 1) {
340                         if (do_rect) p1+=4;
341                         if (do_float) p1f+=4;
342                 }
343         }
344         
345 }
346
347 struct ImBuf *IMB_onehalf(struct ImBuf *ibuf1)
348 {
349         struct ImBuf *ibuf2;
350
351         if (ibuf1==NULL) return (NULL);
352         if (ibuf1->rect==NULL && ibuf1->rect_float==NULL) return (NULL);
353         
354         if (ibuf1->x <= 1) return(IMB_half_y(ibuf1));
355         if (ibuf1->y <= 1) return(IMB_half_x(ibuf1));
356         
357         ibuf2=IMB_allocImBuf((ibuf1->x)/2, (ibuf1->y)/2, ibuf1->depth, ibuf1->flags);
358         if (ibuf2==NULL) return (NULL);
359         
360         imb_onehalf_no_alloc(ibuf2, ibuf1);
361         
362         return (ibuf2);
363 }
364
365 /* q_scale_linear_interpolation helper functions */
366
367 static void enlarge_picture_byte(
368         unsigned char* src, unsigned char* dst, int src_width, 
369         int src_height, int dst_width, int dst_height)
370 {
371         double ratiox = (double) (dst_width - 1.0) 
372                 / (double) (src_width - 1.001);
373         double ratioy = (double) (dst_height - 1.0) 
374                 / (double) (src_height - 1.001);
375         uintptr_t x_src, dx_src, x_dst;
376         uintptr_t y_src, dy_src, y_dst;
377
378         dx_src = 65536.0 / ratiox;
379         dy_src = 65536.0 / ratioy;
380
381         y_src = 0;
382         for (y_dst = 0; y_dst < dst_height; y_dst++) {
383                 unsigned char* line1 = src + (y_src >> 16) * 4 * src_width;
384                 unsigned char* line2 = line1 + 4 * src_width;
385                 uintptr_t weight1y = 65536 - (y_src & 0xffff);
386                 uintptr_t weight2y = 65536 - weight1y;
387
388                 if ((y_src >> 16) == src_height - 1) {
389                         line2 = line1;
390                 }
391
392                 x_src = 0;
393                 for (x_dst = 0; x_dst < dst_width; x_dst++) {
394                         uintptr_t weight1x = 65536 - (x_src & 0xffff);
395                         uintptr_t weight2x = 65536 - weight1x;
396
397                         unsigned long x = (x_src >> 16) * 4;
398
399                         *dst++ = ((((line1[x] * weight1y) >> 16) 
400                                    * weight1x) >> 16)
401                                 + ((((line2[x] * weight2y) >> 16) 
402                                         * weight1x) >> 16)
403                                 + ((((line1[4 + x] * weight1y) >> 16) 
404                                    * weight2x) >> 16)
405                                 + ((((line2[4 + x] * weight2y) >> 16) 
406                                         * weight2x) >> 16);
407
408                         *dst++ = ((((line1[x + 1] * weight1y) >> 16) 
409                                    * weight1x) >> 16)
410                                 + ((((line2[x + 1] * weight2y) >> 16) 
411                                         * weight1x) >> 16)
412                                 + ((((line1[4 + x + 1] * weight1y) >> 16) 
413                                    * weight2x) >> 16)
414                                 + ((((line2[4 + x + 1] * weight2y) >> 16) 
415                                         * weight2x) >> 16);
416
417                         *dst++ = ((((line1[x + 2] * weight1y) >> 16) 
418                                    * weight1x) >> 16)
419                                 + ((((line2[x + 2] * weight2y) >> 16) 
420                                         * weight1x) >> 16)
421                                 + ((((line1[4 + x + 2] * weight1y) >> 16) 
422                                    * weight2x) >> 16)
423                                 + ((((line2[4 + x + 2] * weight2y) >> 16) 
424                                         * weight2x) >> 16);
425
426                         *dst++ = ((((line1[x + 3] * weight1y) >> 16) 
427                                    * weight1x) >> 16)
428                                 + ((((line2[x + 3] * weight2y) >> 16) 
429                                         * weight1x) >> 16)
430                                 + ((((line1[4 + x + 3] * weight1y) >> 16) 
431                                    * weight2x) >> 16)
432                                 + ((((line2[4 + x + 3] * weight2y) >> 16) 
433                                         * weight2x) >> 16);
434
435                         x_src += dx_src;
436                 }
437                 y_src += dy_src;
438         }
439 }
440
441 struct scale_outpix_byte {
442         uintptr_t r;
443         uintptr_t g;
444         uintptr_t b;
445         uintptr_t a;
446
447         uintptr_t weight;
448 };
449
450 static void shrink_picture_byte(
451         unsigned char* src, unsigned char* dst, int src_width, 
452         int src_height, int dst_width, int dst_height)
453 {
454         double ratiox = (double) (dst_width) / (double) (src_width);
455         double ratioy = (double) (dst_height) / (double) (src_height);
456         uintptr_t x_src, dx_dst, x_dst;
457         uintptr_t y_src, dy_dst, y_dst;
458         intptr_t y_counter;
459         unsigned char * dst_begin = dst;
460
461         struct scale_outpix_byte * dst_line1 = NULL;
462         struct scale_outpix_byte * dst_line2 = NULL;
463
464         dst_line1 = (struct scale_outpix_byte*) MEM_callocN(
465                 (dst_width + 1) * sizeof(struct scale_outpix_byte), 
466                 "shrink_picture_byte 1");
467         dst_line2 = (struct scale_outpix_byte*) MEM_callocN(
468                 (dst_width + 1) * sizeof(struct scale_outpix_byte),
469                 "shrink_picture_byte 2");
470
471         dx_dst = 65536.0 * ratiox;
472         dy_dst = 65536.0 * ratioy;
473
474         y_dst = 0;
475         y_counter = 65536;
476         for (y_src = 0; y_src < src_height; y_src++) {
477                 unsigned char* line = src + y_src * 4 * src_width;
478                 uintptr_t weight1y = 65535 - (y_dst & 0xffff);
479                 uintptr_t weight2y = 65535 - weight1y;
480                 x_dst = 0;
481                 for (x_src = 0; x_src < src_width; x_src++) {
482                         uintptr_t weight1x = 65535 - (x_dst & 0xffff);
483                         uintptr_t weight2x = 65535 - weight1x;
484
485                         uintptr_t x = x_dst >> 16;
486
487                         uintptr_t w;
488
489                         w = (weight1y * weight1x) >> 16;
490
491                         /* ensure correct rounding, without this you get ugly banding, or too low color values (ton) */
492                         dst_line1[x].r += (line[0] * w + 32767) >> 16;
493                         dst_line1[x].g += (line[1] * w + 32767) >> 16;
494                         dst_line1[x].b += (line[2] * w + 32767) >> 16;
495                         dst_line1[x].a += (line[3] * w + 32767) >> 16;
496                         dst_line1[x].weight += w;
497
498                         w = (weight2y * weight1x) >> 16;
499
500                         dst_line2[x].r += (line[0] * w + 32767) >> 16;
501                         dst_line2[x].g += (line[1] * w + 32767) >> 16;
502                         dst_line2[x].b += (line[2] * w + 32767) >> 16;
503                         dst_line2[x].a += (line[3] * w + 32767) >> 16;
504                         dst_line2[x].weight += w;
505
506                         w = (weight1y * weight2x) >> 16;
507
508                         dst_line1[x+1].r += (line[0] * w + 32767) >> 16;
509                         dst_line1[x+1].g += (line[1] * w + 32767) >> 16;
510                         dst_line1[x+1].b += (line[2] * w + 32767) >> 16;
511                         dst_line1[x+1].a += (line[3] * w + 32767) >> 16;
512                         dst_line1[x+1].weight += w;
513
514                         w = (weight2y * weight2x) >> 16;
515
516                         dst_line2[x+1].r += (line[0] * w + 32767) >> 16;
517                         dst_line2[x+1].g += (line[1] * w + 32767) >> 16;
518                         dst_line2[x+1].b += (line[2] * w + 32767) >> 16;
519                         dst_line2[x+1].a += (line[3] * w + 32767) >> 16;
520                         dst_line2[x+1].weight += w;
521
522                         x_dst += dx_dst;
523                         line += 4;
524                 }
525
526                 y_dst += dy_dst;
527                 y_counter -= dy_dst;
528                 if (y_counter < 0) {
529                         int val;
530                         uintptr_t x;
531                         struct scale_outpix_byte * temp;
532
533                         y_counter += 65536;
534                         
535                         for (x=0; x < dst_width; x++) {
536                                 uintptr_t f =  0x80000000UL / dst_line1[x].weight;
537                                 *dst++ = (val= (dst_line1[x].r * f) >> 15) > 255 ? 255: val;
538                                 *dst++ = (val= (dst_line1[x].g * f) >> 15) > 255 ? 255: val;
539                                 *dst++ = (val= (dst_line1[x].b * f) >> 15) > 255 ? 255: val;
540                                 *dst++ = (val= (dst_line1[x].a * f) >> 15) > 255 ? 255: val;
541                         }
542                         memset(dst_line1, 0, dst_width *
543                                    sizeof(struct scale_outpix_byte));
544                         temp = dst_line1;
545                         dst_line1 = dst_line2;
546                         dst_line2 = temp;
547                 }
548         }
549         if (dst - dst_begin < dst_width * dst_height * 4) {
550                 int val;
551                 uintptr_t x;
552                 for (x = 0; x < dst_width; x++) {
553                         uintptr_t f = 0x80000000UL / dst_line1[x].weight;
554                         *dst++ = (val= (dst_line1[x].r * f) >> 15) > 255 ? 255: val;
555                         *dst++ = (val= (dst_line1[x].g * f) >> 15) > 255 ? 255: val;
556                         *dst++ = (val= (dst_line1[x].b * f) >> 15) > 255 ? 255: val;
557                         *dst++ = (val= (dst_line1[x].a * f) >> 15) > 255 ? 255: val;
558                 }
559         }
560         MEM_freeN(dst_line1);
561         MEM_freeN(dst_line2);
562 }
563
564
565 static void q_scale_byte(unsigned char* in, unsigned char* out, int in_width, 
566                          int in_height, int dst_width, int dst_height)
567 {
568         if (dst_width > in_width && dst_height > in_height) {
569                 enlarge_picture_byte(in, out, in_width, in_height,
570                                          dst_width, dst_height);
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         } else if (dst_width < in_width && dst_height < in_height) {
772                 shrink_picture_float(in, out, in_width, in_height,
773                                          dst_width, dst_height);
774         }
775 }
776
777 /* q_scale_linear_interpolation (derived from ppmqscale, http://libdv.sf.net)
778
779    q stands for quick _and_ quality :)
780
781    only handles common cases when we either
782
783    scale  both, x and y or
784    shrink both, x and y
785
786    but that is pretty fast:
787    * does only blit once instead of two passes like the old code
788          (fewer cache misses)
789    * uses fixed point integer arithmetic for byte buffers
790    * doesn't branch in tight loops
791
792    Should be comparable in speed to the ImBuf ..._fast functions at least 
793    for byte-buffers.
794
795    NOTE: disabled, due to inacceptable inaccuracy and quality loss, see bug #18609 (ton)
796
797 */
798 static int q_scale_linear_interpolation(
799         struct ImBuf *ibuf, int newx, int newy)
800 {
801         if ((newx >= ibuf->x && newy <= ibuf->y) ||
802                 (newx <= ibuf->x && newy >= ibuf->y)) {
803                 return FALSE;
804         }
805
806         if (ibuf->rect) {
807                 unsigned char * newrect = 
808                         MEM_mallocN(newx * newy * sizeof(int), "q_scale rect");
809                 q_scale_byte((unsigned char *)ibuf->rect, newrect, ibuf->x, ibuf->y,
810                                  newx, newy);
811
812                 imb_freerectImBuf(ibuf);
813                 ibuf->mall |= IB_rect;
814                 ibuf->rect = (unsigned int *) newrect;
815         }
816         if (ibuf->rect_float) {
817                 float * newrect = 
818                         MEM_mallocN(newx * newy * 4 *sizeof(float), 
819                                         "q_scale rectfloat");
820                 q_scale_float(ibuf->rect_float, newrect, ibuf->x, ibuf->y,
821                                   newx, newy);
822                 imb_freerectfloatImBuf(ibuf);
823                 ibuf->mall |= IB_rectfloat;
824                 ibuf->rect_float = newrect;
825         }
826         ibuf->x = newx;
827         ibuf->y = newy;
828
829         return TRUE;
830 }
831
832 static struct ImBuf *scaledownx(struct ImBuf *ibuf, int newx)
833 {
834         uchar *rect, *_newrect, *newrect;
835         float *rectf, *_newrectf, *newrectf;
836         float sample, add, val[4], nval[4], valf[4], nvalf[4];
837         int x, y, do_rect = 0, do_float = 0;
838         uchar *rect_end;
839         float *rectf_end;
840
841
842         rectf= _newrectf= newrectf= NULL; 
843         rect=_newrect= newrect= NULL; 
844         nval[0]=  nval[1]= nval[2]= nval[3]= 0.0f;
845         nvalf[0]=nvalf[1]=nvalf[2]=nvalf[3]= 0.0f;
846         
847         if (ibuf==NULL) return(NULL);
848         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
849
850         if (ibuf->rect) {
851                 do_rect = 1;
852                 _newrect = MEM_mallocN(newx * ibuf->y * sizeof(int), "scaledownx");
853                 if (_newrect==NULL) return(ibuf);
854                 rect_end= (unsigned char *)ibuf->rect + (ibuf->x * ibuf->y * sizeof(int));
855         }
856         if (ibuf->rect_float) {
857                 do_float = 1;
858                 _newrectf = MEM_mallocN(newx * ibuf->y * sizeof(float) * 4, "scaledownxf");
859                 if (_newrectf==NULL) {
860                         if (_newrect) MEM_freeN(_newrect);
861                         return(ibuf);
862                 }
863                 rectf_end= ibuf->rect_float + (ibuf->x * ibuf->y * sizeof(float) * 4);
864         }
865
866         add = (ibuf->x - 0.01) / newx;
867
868         if (do_rect) {
869                 rect = (uchar *) ibuf->rect;
870                 newrect = _newrect;
871         }
872         if (do_float) {
873                 rectf = ibuf->rect_float;
874                 newrectf = _newrectf;
875         }
876                 
877         for (y = ibuf->y; y>0 ; y--) {
878                 sample = 0.0f;
879                 val[0]=  val[1]= val[2]= val[3]= 0.0f;
880                 valf[0]=valf[1]=valf[2]=valf[3]= 0.0f;
881
882                 for (x = newx ; x>0 ; x--) {
883                         if (do_rect) {
884                                 nval[0] = - val[0] * sample;
885                                 nval[1] = - val[1] * sample;
886                                 nval[2] = - val[2] * sample;
887                                 nval[3] = - val[3] * sample;
888                         }
889                         if (do_float) {
890                                 nvalf[0] = - valf[0] * sample;
891                                 nvalf[1] = - valf[1] * sample;
892                                 nvalf[2] = - valf[2] * sample;
893                                 nvalf[3] = - valf[3] * sample;
894                         }
895                         
896                         sample += add;
897
898                         while (sample >= 1.0f){
899                                 sample -= 1.0f;
900                                 
901                                 if (do_rect) {
902                                         nval[0] += rect[0];
903                                         nval[1] += rect[1];
904                                         nval[2] += rect[2];
905                                         nval[3] += rect[3];
906                                         rect += 4;
907                                 }
908                                 if (do_float) {
909                                         nvalf[0] += rectf[0];
910                                         nvalf[1] += rectf[1];
911                                         nvalf[2] += rectf[2];
912                                         nvalf[3] += rectf[3];
913                                         rectf += 4;
914                                 }
915                         }
916                         
917                         if (do_rect) {
918                                 val[0]= rect[0];val[1]= rect[1];val[2]= rect[2];val[3]= rect[3];
919                                 rect += 4;
920                                 
921                                 newrect[0] = ((nval[0] + sample * val[0])/add + 0.5f);
922                                 newrect[1] = ((nval[1] + sample * val[1])/add + 0.5f);
923                                 newrect[2] = ((nval[2] + sample * val[2])/add + 0.5f);
924                                 newrect[3] = ((nval[3] + sample * val[3])/add + 0.5f);
925                                 
926                                 newrect += 4;
927                         }
928                         if (do_float) {
929                                 
930                                 valf[0]= rectf[0];valf[1]= rectf[1];valf[2]= rectf[2];valf[3]= rectf[3];
931                                 rectf += 4;
932                                 
933                                 newrectf[0] = ((nvalf[0] + sample * valf[0])/add);
934                                 newrectf[1] = ((nvalf[1] + sample * valf[1])/add);
935                                 newrectf[2] = ((nvalf[2] + sample * valf[2])/add);
936                                 newrectf[3] = ((nvalf[3] + sample * valf[3])/add);
937                                 
938                                 newrectf += 4;
939                         }
940                         
941                         sample -= 1.0f;
942                 }
943         }
944
945         if (do_rect) {
946                 BLI_assert(rect == rect_end); /* see bug [#26502] */
947                 imb_freerectImBuf(ibuf);
948                 ibuf->mall |= IB_rect;
949                 ibuf->rect = (unsigned int *) _newrect;
950         }
951         if (do_float) {
952                 BLI_assert(rectf == rectf_end); /* see bug [#26502] */
953                 imb_freerectfloatImBuf(ibuf);
954                 ibuf->mall |= IB_rectfloat;
955                 ibuf->rect_float = _newrectf;
956         }
957         
958         ibuf->x = newx;
959         return(ibuf);
960 }
961
962
963 static struct ImBuf *scaledowny(struct ImBuf *ibuf, int newy)
964 {
965         uchar *rect, *_newrect, *newrect;
966         float *rectf, *_newrectf, *newrectf;
967         float sample, add, val[4], nval[4], valf[4], nvalf[4];
968         int x, y, skipx, do_rect = 0, do_float = 0;
969         uchar *rect_end;
970         float *rectf_end;
971
972         rectf= _newrectf= newrectf= NULL; 
973         rect= _newrect= newrect= NULL; 
974         nval[0]=  nval[1]= nval[2]= nval[3]= 0.0f;
975         nvalf[0]=nvalf[1]=nvalf[2]=nvalf[3]= 0.0f;
976
977         if (ibuf==NULL) return(NULL);
978         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
979
980         if (ibuf->rect) {
981                 do_rect = 1;
982                 _newrect = MEM_mallocN(newy * ibuf->x * sizeof(int), "scaledowny");
983                 if (_newrect==NULL) return(ibuf);
984                 rect_end= (unsigned char *)ibuf->rect + (ibuf->x * ibuf->y * sizeof(int));
985         }
986         if (ibuf->rect_float) {
987                 do_float = 1;
988                 _newrectf = MEM_mallocN(newy * ibuf->x * sizeof(float) * 4, "scaledownyf");
989                 if (_newrectf==NULL) {
990                         if (_newrect) MEM_freeN(_newrect);
991                         return(ibuf);
992                 }
993                 rectf_end= ibuf->rect_float + (ibuf->x * ibuf->y * sizeof(float) * 4);
994         }
995
996         add = (ibuf->y - 0.01) / newy;
997         skipx = 4 * ibuf->x;
998
999         for (x = skipx - 4; x>=0 ; x-= 4) {
1000                 if (do_rect) {
1001                         rect = ((uchar *) ibuf->rect) + x;
1002                         newrect = _newrect + x;
1003                 }
1004                 if (do_float) {
1005                         rectf = ibuf->rect_float + x;
1006                         newrectf = _newrectf + x;
1007                 }
1008                 
1009                 sample = 0.0f;
1010                 val[0]=  val[1]= val[2]= val[3]= 0.0f;
1011                 valf[0]=valf[1]=valf[2]=valf[3]= 0.0f;
1012
1013                 for (y = newy ; y>0 ; y--) {
1014                         if (do_rect) {
1015                                 nval[0] = - val[0] * sample;
1016                                 nval[1] = - val[1] * sample;
1017                                 nval[2] = - val[2] * sample;
1018                                 nval[3] = - val[3] * sample;
1019                         }
1020                         if (do_float) {
1021                                 nvalf[0] = - valf[0] * sample;
1022                                 nvalf[1] = - valf[1] * sample;
1023                                 nvalf[2] = - valf[2] * sample;
1024                                 nvalf[3] = - valf[3] * sample;
1025                         }
1026                         
1027                         sample += add;
1028
1029                         while (sample >= 1.0f) {
1030                                 sample -= 1.0f;
1031                                 
1032                                 if (do_rect) {
1033                                         nval[0] += rect[0];
1034                                         nval[1] += rect[1];
1035                                         nval[2] += rect[2];
1036                                         nval[3] += rect[3];
1037                                         rect += skipx;
1038                                 }
1039                                 if (do_float) {
1040                                         nvalf[0] += rectf[0];
1041                                         nvalf[1] += rectf[1];
1042                                         nvalf[2] += rectf[2];
1043                                         nvalf[3] += rectf[3];
1044                                         rectf += skipx;
1045                                 }
1046                         }
1047
1048                         if (do_rect) {
1049                                 val[0]= rect[0];val[1]= rect[1];val[2]= rect[2];val[3]= rect[3];
1050                                 rect += skipx;
1051                                 
1052                                 newrect[0] = ((nval[0] + sample * val[0])/add + 0.5f);
1053                                 newrect[1] = ((nval[1] + sample * val[1])/add + 0.5f);
1054                                 newrect[2] = ((nval[2] + sample * val[2])/add + 0.5f);
1055                                 newrect[3] = ((nval[3] + sample * val[3])/add + 0.5f);
1056                                 
1057                                 newrect += skipx;
1058                         }
1059                         if (do_float) {
1060                                 
1061                                 valf[0]= rectf[0];valf[1]= rectf[1];valf[2]= rectf[2];valf[3]= rectf[3];
1062                                 rectf += skipx;
1063                                 
1064                                 newrectf[0] = ((nvalf[0] + sample * valf[0])/add);
1065                                 newrectf[1] = ((nvalf[1] + sample * valf[1])/add);
1066                                 newrectf[2] = ((nvalf[2] + sample * valf[2])/add);
1067                                 newrectf[3] = ((nvalf[3] + sample * valf[3])/add);
1068                                 
1069                                 newrectf += skipx;
1070                         }
1071                         
1072                         sample -= 1.0f;
1073                 }
1074         }       
1075
1076         if (do_rect) {
1077                 BLI_assert(rect == rect_end); /* see bug [#26502] */
1078                 imb_freerectImBuf(ibuf);
1079                 ibuf->mall |= IB_rect;
1080                 ibuf->rect = (unsigned int *) _newrect;
1081         }
1082         if (do_float) {
1083                 BLI_assert(rectf == rectf_end); /* see bug [#26502] */
1084                 imb_freerectfloatImBuf(ibuf);
1085                 ibuf->mall |= IB_rectfloat;
1086                 ibuf->rect_float = (float *) _newrectf;
1087         }
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