converted more mixed tab/space indentations to tabs. only whitespace changes.
[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         const int do_rect= (ibuf->rect != NULL);
835         const int do_float= (ibuf->rect_float != NULL);
836         const size_t rect_size= ibuf->x * ibuf->y * 4;
837
838         uchar *rect, *_newrect, *newrect;
839         float *rectf, *_newrectf, *newrectf;
840         float sample, add, val[4], nval[4], valf[4], nvalf[4];
841         int x, y;
842
843         rectf= _newrectf= newrectf= NULL; 
844         rect=_newrect= newrect= NULL; 
845         nval[0]=  nval[1]= nval[2]= nval[3]= 0.0f;
846         nvalf[0]=nvalf[1]=nvalf[2]=nvalf[3]= 0.0f;
847
848         if (!do_rect && !do_float) return (ibuf);
849
850         if (do_rect) {
851                 _newrect = MEM_mallocN(newx * ibuf->y * sizeof(uchar) * 4, "scaledownx");
852                 if (_newrect==NULL) return(ibuf);
853         }
854         if (do_float) {
855                 _newrectf = MEM_mallocN(newx * ibuf->y * sizeof(float) * 4, "scaledownxf");
856                 if (_newrectf==NULL) {
857                         if (_newrect) MEM_freeN(_newrect);
858                         return(ibuf);
859                 }
860         }
861
862         add = (ibuf->x - 0.01) / newx;
863
864         if (do_rect) {
865                 rect = (uchar *) ibuf->rect;
866                 newrect = _newrect;
867         }
868         if (do_float) {
869                 rectf = ibuf->rect_float;
870                 newrectf = _newrectf;
871         }
872                 
873         for (y = ibuf->y; y>0 ; y--) {
874                 sample = 0.0f;
875                 val[0]=  val[1]= val[2]= val[3]= 0.0f;
876                 valf[0]=valf[1]=valf[2]=valf[3]= 0.0f;
877
878                 for (x = newx ; x>0 ; x--) {
879                         if (do_rect) {
880                                 nval[0] = - val[0] * sample;
881                                 nval[1] = - val[1] * sample;
882                                 nval[2] = - val[2] * sample;
883                                 nval[3] = - val[3] * sample;
884                         }
885                         if (do_float) {
886                                 nvalf[0] = - valf[0] * sample;
887                                 nvalf[1] = - valf[1] * sample;
888                                 nvalf[2] = - valf[2] * sample;
889                                 nvalf[3] = - valf[3] * sample;
890                         }
891                         
892                         sample += add;
893
894                         while (sample >= 1.0f){
895                                 sample -= 1.0f;
896                                 
897                                 if (do_rect) {
898                                         nval[0] += rect[0];
899                                         nval[1] += rect[1];
900                                         nval[2] += rect[2];
901                                         nval[3] += rect[3];
902                                         rect += 4;
903                                 }
904                                 if (do_float) {
905                                         nvalf[0] += rectf[0];
906                                         nvalf[1] += rectf[1];
907                                         nvalf[2] += rectf[2];
908                                         nvalf[3] += rectf[3];
909                                         rectf += 4;
910                                 }
911                         }
912                         
913                         if (do_rect) {
914                                 val[0]= rect[0];val[1]= rect[1];val[2]= rect[2];val[3]= rect[3];
915                                 rect += 4;
916                                 
917                                 newrect[0] = ((nval[0] + sample * val[0])/add + 0.5f);
918                                 newrect[1] = ((nval[1] + sample * val[1])/add + 0.5f);
919                                 newrect[2] = ((nval[2] + sample * val[2])/add + 0.5f);
920                                 newrect[3] = ((nval[3] + sample * val[3])/add + 0.5f);
921                                 
922                                 newrect += 4;
923                         }
924                         if (do_float) {
925                                 
926                                 valf[0]= rectf[0];valf[1]= rectf[1];valf[2]= rectf[2];valf[3]= rectf[3];
927                                 rectf += 4;
928                                 
929                                 newrectf[0] = ((nvalf[0] + sample * valf[0])/add);
930                                 newrectf[1] = ((nvalf[1] + sample * valf[1])/add);
931                                 newrectf[2] = ((nvalf[2] + sample * valf[2])/add);
932                                 newrectf[3] = ((nvalf[3] + sample * valf[3])/add);
933                                 
934                                 newrectf += 4;
935                         }
936                         
937                         sample -= 1.0f;
938                 }
939         }
940
941         if (do_rect) {
942                 // printf("%ld %ld\n", (uchar *)rect - ((uchar *)ibuf->rect), rect_size);
943                 BLI_assert((uchar *)rect - ((uchar *)ibuf->rect) == rect_size); /* see bug [#26502] */
944                 imb_freerectImBuf(ibuf);
945                 ibuf->mall |= IB_rect;
946                 ibuf->rect = (unsigned int *) _newrect;
947         }
948         if (do_float) {
949                 // printf("%ld %ld\n", rectf - ibuf->rect_float, rect_size);
950                 BLI_assert((rectf - ibuf->rect_float) == rect_size); /* see bug [#26502] */
951                 imb_freerectfloatImBuf(ibuf);
952                 ibuf->mall |= IB_rectfloat;
953                 ibuf->rect_float = _newrectf;
954         }
955         
956         ibuf->x = newx;
957         return(ibuf);
958 }
959
960
961 static struct ImBuf *scaledowny(struct ImBuf *ibuf, int newy)
962 {
963         const int do_rect= (ibuf->rect != NULL);
964         const int do_float= (ibuf->rect_float != NULL);
965         const size_t rect_size= ibuf->x * ibuf->y * 4;
966
967         uchar *rect, *_newrect, *newrect;
968         float *rectf, *_newrectf, *newrectf;
969         float sample, add, val[4], nval[4], valf[4], nvalf[4];
970         int x, y, skipx;
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 (!do_rect && !do_float) return (ibuf);
978
979         if (do_rect) {
980                 _newrect = MEM_mallocN(newy * ibuf->x * sizeof(uchar) * 4, "scaledowny");
981                 if (_newrect==NULL) return(ibuf);
982         }
983         if (do_float) {
984                 _newrectf = MEM_mallocN(newy * ibuf->x * sizeof(float) * 4, "scaledownyf");
985                 if (_newrectf==NULL) {
986                         if (_newrect) MEM_freeN(_newrect);
987                         return(ibuf);
988                 }
989         }
990
991         add = (ibuf->y - 0.01) / newy;
992         skipx = 4 * ibuf->x;
993
994         for (x = skipx - 4; x>=0 ; x-= 4) {
995                 if (do_rect) {
996                         rect = ((uchar *) ibuf->rect) + x;
997                         newrect = _newrect + x;
998                 }
999                 if (do_float) {
1000                         rectf = ibuf->rect_float + x;
1001                         newrectf = _newrectf + x;
1002                 }
1003                 
1004                 sample = 0.0f;
1005                 val[0]=  val[1]= val[2]= val[3]= 0.0f;
1006                 valf[0]=valf[1]=valf[2]=valf[3]= 0.0f;
1007
1008                 for (y = newy ; y>0 ; y--) {
1009                         if (do_rect) {
1010                                 nval[0] = - val[0] * sample;
1011                                 nval[1] = - val[1] * sample;
1012                                 nval[2] = - val[2] * sample;
1013                                 nval[3] = - val[3] * sample;
1014                         }
1015                         if (do_float) {
1016                                 nvalf[0] = - valf[0] * sample;
1017                                 nvalf[1] = - valf[1] * sample;
1018                                 nvalf[2] = - valf[2] * sample;
1019                                 nvalf[3] = - valf[3] * sample;
1020                         }
1021                         
1022                         sample += add;
1023
1024                         while (sample >= 1.0f) {
1025                                 sample -= 1.0f;
1026                                 
1027                                 if (do_rect) {
1028                                         nval[0] += rect[0];
1029                                         nval[1] += rect[1];
1030                                         nval[2] += rect[2];
1031                                         nval[3] += rect[3];
1032                                         rect += skipx;
1033                                 }
1034                                 if (do_float) {
1035                                         nvalf[0] += rectf[0];
1036                                         nvalf[1] += rectf[1];
1037                                         nvalf[2] += rectf[2];
1038                                         nvalf[3] += rectf[3];
1039                                         rectf += skipx;
1040                                 }
1041                         }
1042
1043                         if (do_rect) {
1044                                 val[0]= rect[0];val[1]= rect[1];val[2]= rect[2];val[3]= rect[3];
1045                                 rect += skipx;
1046                                 
1047                                 newrect[0] = ((nval[0] + sample * val[0])/add + 0.5f);
1048                                 newrect[1] = ((nval[1] + sample * val[1])/add + 0.5f);
1049                                 newrect[2] = ((nval[2] + sample * val[2])/add + 0.5f);
1050                                 newrect[3] = ((nval[3] + sample * val[3])/add + 0.5f);
1051                                 
1052                                 newrect += skipx;
1053                         }
1054                         if (do_float) {
1055                                 
1056                                 valf[0]= rectf[0];valf[1]= rectf[1];valf[2]= rectf[2];valf[3]= rectf[3];
1057                                 rectf += skipx;
1058                                 
1059                                 newrectf[0] = ((nvalf[0] + sample * valf[0])/add);
1060                                 newrectf[1] = ((nvalf[1] + sample * valf[1])/add);
1061                                 newrectf[2] = ((nvalf[2] + sample * valf[2])/add);
1062                                 newrectf[3] = ((nvalf[3] + sample * valf[3])/add);
1063                                 
1064                                 newrectf += skipx;
1065                         }
1066                         
1067                         sample -= 1.0f;
1068                 }
1069         }       
1070
1071         if (do_rect) {
1072                 // printf("%ld %ld\n", (uchar *)rect - ((uchar *)ibuf->rect), rect_size);
1073                 BLI_assert((uchar *)rect - ((uchar *)ibuf->rect) == rect_size); /* see bug [#26502] */
1074                 imb_freerectImBuf(ibuf);
1075                 ibuf->mall |= IB_rect;
1076                 ibuf->rect = (unsigned int *) _newrect;
1077         }
1078         if (do_float) {
1079                 // printf("%ld %ld\n", rectf - ibuf->rect_float, rect_size);
1080                 BLI_assert((rectf - ibuf->rect_float) == rect_size); /* see bug [#26502] */
1081                 imb_freerectfloatImBuf(ibuf);
1082                 ibuf->mall |= IB_rectfloat;
1083                 ibuf->rect_float = (float *) _newrectf;
1084         }
1085         
1086         ibuf->y = newy;
1087         return(ibuf);
1088 }
1089
1090
1091 static struct ImBuf *scaleupx(struct ImBuf *ibuf, int newx)
1092 {
1093         uchar *rect,*_newrect=NULL,*newrect;
1094         float *rectf,*_newrectf=NULL,*newrectf;
1095         float sample,add;
1096         float val_a,nval_a,diff_a;
1097         float val_b,nval_b,diff_b;
1098         float val_g,nval_g,diff_g;
1099         float val_r,nval_r,diff_r;
1100         float val_af,nval_af,diff_af;
1101         float val_bf,nval_bf,diff_bf;
1102         float val_gf,nval_gf,diff_gf;
1103         float val_rf,nval_rf,diff_rf;
1104         int x,y, do_rect = 0, do_float = 0;
1105
1106         val_a = nval_a = diff_a = val_b = nval_b = diff_b = 0;
1107         val_g = nval_g = diff_g = val_r = nval_r = diff_r = 0;
1108         val_af = nval_af = diff_af = val_bf = nval_bf = diff_bf = 0;
1109         val_gf = nval_gf = diff_gf = val_rf = nval_rf = diff_rf = 0;
1110         if (ibuf==NULL) return(NULL);
1111         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
1112
1113         if (ibuf->rect) {
1114                 do_rect = 1;
1115                 _newrect = MEM_mallocN(newx * ibuf->y * sizeof(int), "scaleupx");
1116                 if (_newrect==NULL) return(ibuf);
1117         }
1118         if (ibuf->rect_float) {
1119                 do_float = 1;
1120                 _newrectf = MEM_mallocN(newx * ibuf->y * sizeof(float) * 4, "scaleupxf");
1121                 if (_newrectf==NULL) {
1122                         if (_newrect) MEM_freeN(_newrect);
1123                         return(ibuf);
1124                 }
1125         }
1126
1127         add = (ibuf->x - 1.001) / (newx - 1.0);
1128
1129         rect = (uchar *) ibuf->rect;
1130         rectf = (float *) ibuf->rect_float;
1131         newrect = _newrect;
1132         newrectf = _newrectf;
1133
1134         for (y = ibuf->y; y>0 ; y--){
1135
1136                 sample = 0;
1137                 
1138                 if (do_rect) {
1139                         val_a = rect[0] ;
1140                         nval_a = rect[4];
1141                         diff_a = nval_a - val_a ;
1142                         val_a += 0.5f;
1143
1144                         val_b = rect[1] ;
1145                         nval_b = rect[5];
1146                         diff_b = nval_b - val_b ;
1147                         val_b += 0.5f;
1148
1149                         val_g = rect[2] ;
1150                         nval_g = rect[6];
1151                         diff_g = nval_g - val_g ;
1152                         val_g += 0.5f;
1153
1154                         val_r = rect[3] ;
1155                         nval_r = rect[7];
1156                         diff_r = nval_r - val_r ;
1157                         val_r += 0.5f;
1158
1159                         rect += 8;
1160                 }
1161                 if (do_float) {
1162                         val_af = rectf[0] ;
1163                         nval_af = rectf[4];
1164                         diff_af = nval_af - val_af;
1165         
1166                         val_bf = rectf[1] ;
1167                         nval_bf = rectf[5];
1168                         diff_bf = nval_bf - val_bf;
1169
1170                         val_gf = rectf[2] ;
1171                         nval_gf = rectf[6];
1172                         diff_gf = nval_gf - val_gf;
1173
1174                         val_rf = rectf[3] ;
1175                         nval_rf = rectf[7];
1176                         diff_rf = nval_rf - val_rf;
1177
1178                         rectf += 8;
1179                 }
1180                 for (x = newx ; x>0 ; x--){
1181                         if (sample >= 1.0f){
1182                                 sample -= 1.0f;
1183
1184                                 if (do_rect) {
1185                                         val_a = nval_a ;
1186                                         nval_a = rect[0] ;
1187                                         diff_a = nval_a - val_a ;
1188                                         val_a += 0.5f;
1189
1190                                         val_b = nval_b ;
1191                                         nval_b = rect[1] ;
1192                                         diff_b = nval_b - val_b ;
1193                                         val_b += 0.5f;
1194
1195                                         val_g = nval_g ;
1196                                         nval_g = rect[2] ;
1197                                         diff_g = nval_g - val_g ;
1198                                         val_g += 0.5f;
1199
1200                                         val_r = nval_r ;
1201                                         nval_r = rect[3] ;
1202                                         diff_r = nval_r - val_r ;
1203                                         val_r += 0.5f;
1204                                         rect += 4;
1205                                 }
1206                                 if (do_float) {
1207                                         val_af = nval_af ;
1208                                         nval_af = rectf[0] ;
1209                                         diff_af = nval_af - val_af ;
1210         
1211                                         val_bf = nval_bf ;
1212                                         nval_bf = rectf[1] ;
1213                                         diff_bf = nval_bf - val_bf ;
1214
1215                                         val_gf = nval_gf ;
1216                                         nval_gf = rectf[2] ;
1217                                         diff_gf = nval_gf - val_gf ;
1218
1219                                         val_rf = nval_rf ;
1220                                         nval_rf = rectf[3] ;
1221                                         diff_rf = nval_rf - val_rf;
1222                                         rectf += 4;
1223                                 }
1224                         }
1225                         if (do_rect) {
1226                                 newrect[0] = val_a + sample * diff_a;
1227                                 newrect[1] = val_b + sample * diff_b;
1228                                 newrect[2] = val_g + sample * diff_g;
1229                                 newrect[3] = val_r + sample * diff_r;
1230                                 newrect += 4;
1231                         }
1232                         if (do_float) {
1233                                 newrectf[0] = val_af + sample * diff_af;
1234                                 newrectf[1] = val_bf + sample * diff_bf;
1235                                 newrectf[2] = val_gf + sample * diff_gf;
1236                                 newrectf[3] = val_rf + sample * diff_rf;
1237                                 newrectf += 4;
1238                         }
1239                         sample += add;
1240                 }
1241         }
1242
1243         if (do_rect) {
1244                 imb_freerectImBuf(ibuf);
1245                 ibuf->mall |= IB_rect;
1246                 ibuf->rect = (unsigned int *) _newrect;
1247         }
1248         if (do_float) {
1249                 imb_freerectfloatImBuf(ibuf);
1250                 ibuf->mall |= IB_rectfloat;
1251                 ibuf->rect_float = (float *) _newrectf;
1252         }
1253         
1254         ibuf->x = newx;
1255         return(ibuf);
1256 }
1257
1258 static struct ImBuf *scaleupy(struct ImBuf *ibuf, int newy)
1259 {
1260         uchar *rect,*_newrect=NULL,*newrect;
1261         float *rectf,*_newrectf=NULL,*newrectf;
1262         float sample,add;
1263         float val_a,nval_a,diff_a;
1264         float val_b,nval_b,diff_b;
1265         float val_g,nval_g,diff_g;
1266         float val_r,nval_r,diff_r;
1267         float val_af,nval_af,diff_af;
1268         float val_bf,nval_bf,diff_bf;
1269         float val_gf,nval_gf,diff_gf;
1270         float val_rf,nval_rf,diff_rf;
1271         int x,y, do_rect = 0, do_float = 0, skipx;
1272
1273         val_a = nval_a = diff_a = val_b = nval_b = diff_b = 0;
1274         val_g = nval_g = diff_g = val_r = nval_r = diff_r = 0;
1275         val_af = nval_af = diff_af = val_bf = nval_bf = diff_bf = 0;
1276         val_gf = nval_gf = diff_gf = val_rf = nval_rf = diff_rf = 0;
1277         if (ibuf==NULL) return(NULL);
1278         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
1279
1280         if (ibuf->rect) {
1281                 do_rect = 1;
1282                 _newrect = MEM_mallocN(ibuf->x * newy * sizeof(int), "scaleupy");
1283                 if (_newrect==NULL) return(ibuf);
1284         }
1285         if (ibuf->rect_float) {
1286                 do_float = 1;
1287                 _newrectf = MEM_mallocN(ibuf->x * newy * sizeof(float) * 4, "scaleupyf");
1288                 if (_newrectf==NULL) {
1289                         if (_newrect) MEM_freeN(_newrect);
1290                         return(ibuf);
1291                 }
1292         }
1293
1294         add = (ibuf->y - 1.001) / (newy - 1.0);
1295         skipx = 4 * ibuf->x;
1296
1297         rect = (uchar *) ibuf->rect;
1298         rectf = (float *) ibuf->rect_float;
1299         newrect = _newrect;
1300         newrectf = _newrectf;
1301
1302         for (x = ibuf->x; x>0 ; x--){
1303
1304                 sample = 0;
1305                 if (do_rect) {
1306                         rect = ((uchar *)ibuf->rect) + 4*(x-1);
1307                         newrect = _newrect + 4*(x-1);
1308
1309                         val_a = rect[0] ;
1310                         nval_a = rect[skipx];
1311                         diff_a = nval_a - val_a ;
1312                         val_a += 0.5f;
1313
1314                         val_b = rect[1] ;
1315                         nval_b = rect[skipx+1];
1316                         diff_b = nval_b - val_b ;
1317                         val_b += 0.5f;
1318
1319                         val_g = rect[2] ;
1320                         nval_g = rect[skipx+2];
1321                         diff_g = nval_g - val_g ;
1322                         val_g += 0.5f;
1323
1324                         val_r = rect[3] ;
1325                         nval_r = rect[skipx+4];
1326                         diff_r = nval_r - val_r ;
1327                         val_r += 0.5f;
1328
1329                         rect += 2*skipx;
1330                 }
1331                 if (do_float) {
1332                         rectf = ((float *)ibuf->rect_float) + 4*(x-1);
1333                         newrectf = _newrectf + 4*(x-1);
1334
1335                         val_af = rectf[0] ;
1336                         nval_af = rectf[skipx];
1337                         diff_af = nval_af - val_af;
1338         
1339                         val_bf = rectf[1] ;
1340                         nval_bf = rectf[skipx+1];
1341                         diff_bf = nval_bf - val_bf;
1342
1343                         val_gf = rectf[2] ;
1344                         nval_gf = rectf[skipx+2];
1345                         diff_gf = nval_gf - val_gf;
1346
1347                         val_rf = rectf[3] ;
1348                         nval_rf = rectf[skipx+3];
1349                         diff_rf = nval_rf - val_rf;
1350
1351                         rectf += 2*skipx;
1352                 }
1353                 
1354                 for (y = newy ; y>0 ; y--){
1355                         if (sample >= 1.0f){
1356                                 sample -= 1.0f;
1357
1358                                 if (do_rect) {
1359                                         val_a = nval_a ;
1360                                         nval_a = rect[0] ;
1361                                         diff_a = nval_a - val_a ;
1362                                         val_a += 0.5f;
1363
1364                                         val_b = nval_b ;
1365                                         nval_b = rect[1] ;
1366                                         diff_b = nval_b - val_b ;
1367                                         val_b += 0.5f;
1368
1369                                         val_g = nval_g ;
1370                                         nval_g = rect[2] ;
1371                                         diff_g = nval_g - val_g ;
1372                                         val_g += 0.5f;
1373
1374                                         val_r = nval_r ;
1375                                         nval_r = rect[3] ;
1376                                         diff_r = nval_r - val_r ;
1377                                         val_r += 0.5f;
1378                                         rect += skipx;
1379                                 }
1380                                 if (do_float) {
1381                                         val_af = nval_af ;
1382                                         nval_af = rectf[0] ;
1383                                         diff_af = nval_af - val_af ;
1384         
1385                                         val_bf = nval_bf ;
1386                                         nval_bf = rectf[1] ;
1387                                         diff_bf = nval_bf - val_bf ;
1388
1389                                         val_gf = nval_gf ;
1390                                         nval_gf = rectf[2] ;
1391                                         diff_gf = nval_gf - val_gf ;
1392
1393                                         val_rf = nval_rf ;
1394                                         nval_rf = rectf[3] ;
1395                                         diff_rf = nval_rf - val_rf;
1396                                         rectf += skipx;
1397                                 }
1398                         }
1399                         if (do_rect) {
1400                                 newrect[0] = val_a + sample * diff_a;
1401                                 newrect[1] = val_b + sample * diff_b;
1402                                 newrect[2] = val_g + sample * diff_g;
1403                                 newrect[3] = val_r + sample * diff_r;
1404                                 newrect += skipx;
1405                         }
1406                         if (do_float) {
1407                                 newrectf[0] = val_af + sample * diff_af;
1408                                 newrectf[1] = val_bf + sample * diff_bf;
1409                                 newrectf[2] = val_gf + sample * diff_gf;
1410                                 newrectf[3] = val_rf + sample * diff_rf;
1411                                 newrectf += skipx;
1412                         }
1413                         sample += add;
1414                 }
1415         }
1416
1417         if (do_rect) {
1418                 imb_freerectImBuf(ibuf);
1419                 ibuf->mall |= IB_rect;
1420                 ibuf->rect = (unsigned int *) _newrect;
1421         }
1422         if (do_float) {
1423                 imb_freerectfloatImBuf(ibuf);
1424                 ibuf->mall |= IB_rectfloat;
1425                 ibuf->rect_float = (float *) _newrectf;
1426         }
1427         
1428         ibuf->y = newy;
1429         return(ibuf);
1430 }
1431
1432
1433 /* no float buf needed here! */
1434 static void scalefast_Z_ImBuf(ImBuf *ibuf, int newx, int newy)
1435 {
1436         unsigned int *rect, *_newrect, *newrect;
1437         int x, y;
1438         int ofsx, ofsy, stepx, stepy;
1439
1440         if (ibuf->zbuf) {
1441                 _newrect = MEM_mallocN(newx * newy * sizeof(int), "z rect");
1442                 if (_newrect==NULL) return;
1443                 
1444                 stepx = (65536.0 * (ibuf->x - 1.0) / (newx - 1.0)) + 0.5;
1445                 stepy = (65536.0 * (ibuf->y - 1.0) / (newy - 1.0)) + 0.5;
1446                 ofsy = 32768;
1447
1448                 newrect = _newrect;
1449         
1450                 for (y = newy; y > 0 ; y--){
1451                         rect = (unsigned int*) ibuf->zbuf;
1452                         rect += (ofsy >> 16) * ibuf->x;
1453                         ofsy += stepy;
1454                         ofsx = 32768;
1455                         for (x = newx ; x > 0 ; x--){
1456                                 *newrect++ = rect[ofsx >> 16];
1457                                 ofsx += stepx;
1458                         }
1459                 }
1460         
1461                 IMB_freezbufImBuf(ibuf);
1462                 ibuf->mall |= IB_zbuf;
1463                 ibuf->zbuf = (int*) _newrect;
1464         }
1465 }
1466
1467 struct ImBuf *IMB_scaleImBuf(struct ImBuf * ibuf, unsigned int newx, unsigned int newy)
1468 {
1469         if (ibuf==NULL) return (NULL);
1470         if (ibuf->rect==NULL && ibuf->rect_float==NULL) return (ibuf);
1471         
1472         if (newx == ibuf->x && newy == ibuf->y) { return ibuf; }
1473
1474         /* scaleup / scaledown functions below change ibuf->x and ibuf->y
1475            so we first scale the Z-buffer (if any) */
1476         scalefast_Z_ImBuf(ibuf, newx, newy);
1477
1478         /* try to scale common cases in a fast way */
1479         /* disabled, quality loss is inacceptable, see report #18609  (ton) */
1480         if (0 && q_scale_linear_interpolation(ibuf, newx, newy)) {
1481                 return ibuf;
1482         }
1483
1484         if (newx < ibuf->x) if (newx) scaledownx(ibuf,newx);
1485         if (newy < ibuf->y) if (newy) scaledowny(ibuf,newy);
1486         if (newx > ibuf->x) if (newx) scaleupx(ibuf,newx);
1487         if (newy > ibuf->y) if (newy) scaleupy(ibuf,newy);
1488         
1489         return(ibuf);
1490 }
1491
1492 struct imbufRGBA {
1493         float r, g, b, a;
1494 };
1495
1496 struct ImBuf *IMB_scalefastImBuf(struct ImBuf *ibuf, unsigned int newx, unsigned int newy)
1497 {
1498         unsigned int *rect,*_newrect,*newrect;
1499         struct imbufRGBA *rectf, *_newrectf, *newrectf;
1500         int x,y, do_float=0, do_rect=0;
1501         int ofsx,ofsy,stepx,stepy;
1502
1503         rect = NULL; _newrect = NULL; newrect = NULL;
1504         rectf = NULL; _newrectf = NULL; newrectf = NULL;
1505
1506         if (ibuf==NULL) return(NULL);
1507         if (ibuf->rect) do_rect = 1;
1508         if (ibuf->rect_float) do_float = 1;
1509         if (do_rect==0 && do_float==0) return(ibuf);
1510         
1511         if (newx == ibuf->x && newy == ibuf->y) return(ibuf);
1512         
1513         if(do_rect) {
1514                 _newrect = MEM_mallocN(newx * newy * sizeof(int), "scalefastimbuf");
1515                 if (_newrect==NULL) return(ibuf);
1516                 newrect = _newrect;
1517         }
1518         
1519         if (do_float) {
1520                 _newrectf = MEM_mallocN(newx * newy * sizeof(float) * 4, "scalefastimbuf f");
1521                 if (_newrectf==NULL) {
1522                         if (_newrect) MEM_freeN(_newrect);
1523                         return(ibuf);
1524                 }
1525                 newrectf = _newrectf;
1526         }
1527
1528         stepx = (65536.0 * (ibuf->x - 1.0) / (newx - 1.0)) + 0.5;
1529         stepy = (65536.0 * (ibuf->y - 1.0) / (newy - 1.0)) + 0.5;
1530         ofsy = 32768;
1531
1532         for (y = newy; y > 0 ; y--){
1533                 if(do_rect) {
1534                         rect = ibuf->rect;
1535                         rect += (ofsy >> 16) * ibuf->x;
1536                 }
1537                 if (do_float) {
1538                         rectf = (struct imbufRGBA *)ibuf->rect_float;
1539                         rectf += (ofsy >> 16) * ibuf->x;
1540                 }
1541                 ofsy += stepy;
1542                 ofsx = 32768;
1543                 
1544                 if (do_rect) {
1545                         for (x = newx ; x>0 ; x--){
1546                                 *newrect++ = rect[ofsx >> 16];
1547                                 ofsx += stepx;
1548                         }
1549                 }
1550
1551                 if (do_float) {
1552                         ofsx = 32768;
1553                         for (x = newx ; x>0 ; x--){
1554                                 *newrectf++ = rectf[ofsx >> 16];
1555                                 ofsx += stepx;
1556                         }
1557                 }
1558         }
1559
1560         if (do_rect) {
1561                 imb_freerectImBuf(ibuf);
1562                 ibuf->mall |= IB_rect;
1563                 ibuf->rect = _newrect;
1564         }
1565         
1566         if (do_float) {
1567                 imb_freerectfloatImBuf(ibuf);
1568                 ibuf->mall |= IB_rectfloat;
1569                 ibuf->rect_float = (float *)_newrectf;
1570         }
1571         
1572         scalefast_Z_ImBuf(ibuf, newx, newy);
1573         
1574         ibuf->x = newx;
1575         ibuf->y = newy;
1576         return(ibuf);
1577 }
1578