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