]> git.sesse.net Git - mlt/blob - src/modules/gtk2/pixops.c
avformat/factory.c, jackrack/jack_rack.c, jackrack/plugin_settings.c, vmfx/filter_chr...
[mlt] / src / modules / gtk2 / pixops.c
1 /* GdkPixbuf library - Scaling and compositing functions
2  *
3  * Original:
4  * Copyright (C) 2000 Red Hat, Inc
5  * Author: Owen Taylor <otaylor@redhat.com>
6  *
7  * Modification for MLT:
8  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
9  * Author: Dan Dennedy <dan@dennedy.org>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  */
26  
27 #include <math.h>
28 #include <glib.h>
29 #include <stdio.h>
30
31 #include "pixops.h"
32
33 #define SUBSAMPLE_BITS 4
34 #define SUBSAMPLE (1 << SUBSAMPLE_BITS)
35 #define SUBSAMPLE_MASK ((1 << SUBSAMPLE_BITS)-1)
36 #define SCALE_SHIFT 16
37
38 typedef struct _PixopsFilter PixopsFilter;
39 typedef struct _PixopsFilterDimension PixopsFilterDimension;
40
41 struct _PixopsFilterDimension
42 {
43         int n;
44         double offset;
45         double *weights;
46 };
47
48 struct _PixopsFilter
49 {
50         PixopsFilterDimension x;
51         PixopsFilterDimension y;
52         double overall_alpha;
53 };
54
55 typedef guchar *( *PixopsLineFunc ) ( int *weights, int n_x, int n_y,
56                                       guchar *dest, int dest_x, guchar *dest_end,
57                                       guchar **src,
58                                       int x_init, int x_step, int src_width );
59
60 typedef void ( *PixopsPixelFunc ) ( guchar *dest, guint y1, guint cr, guint y2, guint cb );
61
62
63 /* mmx function declarations */
64 #ifdef USE_MMX
65 guchar *pixops_scale_line_22_yuv_mmx ( guint32 weights[ 16 ][ 8 ], guchar *p, guchar *q1, guchar *q2, int x_step, guchar *p_stop, int x_init, int destx );
66 int pixops_have_mmx ( void );
67 #endif
68
69 static inline int
70 get_check_shift ( int check_size )
71 {
72         int check_shift = 0;
73         g_return_val_if_fail ( check_size >= 0, 4 );
74
75         while ( !( check_size & 1 ) )
76         {
77                 check_shift++;
78                 check_size >>= 1;
79         }
80
81         return check_shift;
82 }
83
84 static inline void
85 pixops_scale_nearest ( guchar *dest_buf,
86                        int render_x0,
87                        int render_y0,
88                        int render_x1,
89                        int render_y1,
90                        int dest_rowstride,
91                        const guchar *src_buf,
92                        int src_width,
93                        int src_height,
94                        int src_rowstride,
95                        double scale_x,
96                        double scale_y )
97 {
98         register int i, j;
99         register int x_step = ( 1 << SCALE_SHIFT ) / scale_x;
100         register int y_step = ( 1 << SCALE_SHIFT ) / scale_y;
101         register int x, x_scaled;
102
103         for ( i = 0; i < ( render_y1 - render_y0 ); i++ )
104         {
105                 const guchar *src = src_buf + ( ( ( i + render_y0 ) * y_step + ( y_step >> 1 ) ) >> SCALE_SHIFT ) * src_rowstride;
106                 guchar *dest = dest_buf + i * dest_rowstride;
107                 x = render_x0 * x_step + ( x_step >> 1 );
108                 
109                 for ( j = 0; j < ( render_x1 - render_x0 ); j++ )
110                 {
111                         x_scaled = x >> SCALE_SHIFT;
112                         *dest++ = src[ x_scaled << 1 ];
113                         *dest++ = src[ ( ( x_scaled >> 1 ) << 2 ) + ( ( j & 1 ) << 1 ) + 1 ];
114                         x += x_step;
115                 }
116         }
117 }
118
119
120 static inline guchar *
121 scale_line ( int *weights, int n_x, int n_y,
122              guchar *dest, int dest_x, guchar *dest_end,
123              guchar **src,
124              int x_init, int x_step, int src_width )
125 {
126         register int x = x_init;
127         register int i, j, x_scaled, y_index, uv_index;
128
129         while ( dest < dest_end )
130         {
131                 unsigned int y = 0, uv = 0;
132                 int *pixel_weights = weights + ( ( x >> ( SCALE_SHIFT - SUBSAMPLE_BITS ) ) & SUBSAMPLE_MASK ) * n_x * n_y;
133
134                 x_scaled = x >> SCALE_SHIFT;
135                 y_index = x_scaled << 1;
136                 uv_index = ( ( x_scaled >> 1 ) << 2 ) + ( ( dest_x & 1 ) << 1 ) + 1;
137                 
138                 for ( i = 0; i < n_y; i++ )
139                 {
140                         int *line_weights = pixel_weights + n_x * i;
141                         guchar *q = src[ i ];
142
143                         for ( j = 0; j < n_x; j ++ )
144                         {
145                                 unsigned int ta = line_weights[ j ];
146
147                                 y  += ta * q[ y_index ];
148                                 uv += ta * q[ uv_index ];
149                         }
150                 }
151
152                 *dest++ = ( y  + 0xffff ) >> SCALE_SHIFT;
153                 *dest++ = ( uv + 0xffff ) >> SCALE_SHIFT;
154
155                 x += x_step;
156                 dest_x++;
157         }
158
159         return dest;
160 }
161
162 #ifdef USE_MMX
163 static inline guchar *
164 scale_line_22_yuv_mmx_stub ( int *weights, int n_x, int n_y,
165                             guchar *dest, int dest_x, guchar *dest_end,
166                             guchar **src,
167                             int x_init, int x_step, int src_width )
168 {
169         guint32 mmx_weights[ 16 ][ 8 ];
170         int j;
171
172         for ( j = 0; j < 16; j++ )
173         {
174                 mmx_weights[ j ][ 0 ] = 0x00010001 * ( weights[ 4 * j ] >> 8 );
175                 mmx_weights[ j ][ 1 ] = 0x00010001 * ( weights[ 4 * j ] >> 8 );
176                 mmx_weights[ j ][ 2 ] = 0x00010001 * ( weights[ 4 * j + 1 ] >> 8 );
177                 mmx_weights[ j ][ 3 ] = 0x00010001 * ( weights[ 4 * j + 1 ] >> 8 );
178                 mmx_weights[ j ][ 4 ] = 0x00010001 * ( weights[ 4 * j + 2 ] >> 8 );
179                 mmx_weights[ j ][ 5 ] = 0x00010001 * ( weights[ 4 * j + 2 ] >> 8 );
180                 mmx_weights[ j ][ 6 ] = 0x00010001 * ( weights[ 4 * j + 3 ] >> 8 );
181                 mmx_weights[ j ][ 7 ] = 0x00010001 * ( weights[ 4 * j + 3 ] >> 8 );
182         }
183
184         return pixops_scale_line_22_yuv_mmx ( mmx_weights, dest, src[ 0 ], src[ 1 ], x_step, dest_end, x_init, dest_x );
185 }
186 #endif /* USE_MMX */
187
188 static inline guchar *
189 scale_line_22_yuv ( int *weights, int n_x, int n_y,
190                    guchar *dest, int dest_x, guchar *dest_end,
191                    guchar **src,
192                    int x_init, int x_step, int src_width )
193 {
194         register int x = x_init;
195         register guchar *src0 = src[ 0 ];
196         register guchar *src1 = src[ 1 ];
197         register unsigned int p;
198         register guchar *q0, *q1;
199         register int w1, w2, w3, w4;
200         register int x_scaled, x_aligned, uv_index;
201
202         while ( dest < dest_end )
203         {
204                 int *pixel_weights = weights + ( ( x >> ( SCALE_SHIFT - SUBSAMPLE_BITS ) ) & SUBSAMPLE_MASK ) * 4;
205                 
206                 x_scaled = x >> SCALE_SHIFT;
207
208                 w1 = pixel_weights[ 0 ];
209                 w2 = pixel_weights[ 1 ];
210                 w3 = pixel_weights[ 2 ];
211                 w4 = pixel_weights[ 3 ];
212
213                 /* process Y */
214                 q0 = src0 + ( x_scaled << 1 );
215                 q1 = src1 + ( x_scaled << 1 );
216                 p  = w1 * q0[ 0 ];
217                 p += w2 * q0[ 2 ];
218                 p += w3 * q1[ 0 ];
219                 p += w4 * q1[ 2 ];
220                 *dest++ = ( p + 0x8000 ) >> SCALE_SHIFT;
221
222                 /* process U/V */
223                 x_aligned = ( ( x_scaled >> 1 ) << 2 );
224                 uv_index = ( ( dest_x & 1 ) << 1 ) + 1;
225                 
226                 q0 = src0 + x_aligned;
227                 q1 = src1 + x_aligned;
228                 p  = w1 * q0[ uv_index ];
229                 p += w3 * q1[ uv_index ];
230                 p += w2 * q0[ uv_index ];
231                 p += w4 * q1[ uv_index ];
232                 
233                 x += x_step;
234                 dest_x ++;
235
236                 *dest++ = ( p + 0x8000 ) >> SCALE_SHIFT;
237         }
238
239         return dest;
240 }
241
242
243 static inline void
244 process_pixel ( int *weights, int n_x, int n_y,
245                 guchar *dest, int dest_x, int dest_channels,
246                 guchar **src, int src_channels,
247                 int x_start, int src_width )
248 {
249         register unsigned int y = 0, uv = 0;
250         register int i, j;
251         int uv_index = ( ( dest_x & 1 ) << 1 ) + 1;
252
253         for ( i = 0; i < n_y; i++ )
254         {
255                 int *line_weights = weights + n_x * i;
256
257                 for ( j = 0; j < n_x; j++ )
258                 {
259                         unsigned int ta = 0xff * line_weights[ j ];
260
261                         if ( x_start + j < 0 )
262                         {
263                                 y  += ta * src[ i ][ 0 ];
264                                 uv += ta * src[ i ][ uv_index ];
265                         }
266                         else if ( x_start + j < src_width )
267                         {
268                                 y  += ta * src[ i ][ ( x_start + j ) << 1 ];
269                                 uv += ta * src[ i ][ ( ( ( x_start + j ) >> 1 ) << 2) + uv_index ];
270                         }
271                         else
272                         {
273                                 y  += ta * src[ i ][ ( src_width - 1 ) << 1 ];
274                                 uv += ta * src[ i ][ ( ( ( src_width - 1 ) >> 1 ) << 2) + uv_index ];
275                         }
276                 }
277         }
278
279         *dest++ = ( y  + 0xffffff ) >> 24;
280         *dest++ = ( uv + 0xffffff ) >> 24;
281 }
282
283
284 static inline void
285 correct_total ( int *weights,
286                 int n_x,
287                 int n_y,
288                 int total,
289                 double overall_alpha )
290 {
291         int correction = ( int ) ( 0.5 + 65536 * overall_alpha ) - total;
292         int remaining, c, d, i;
293
294         if ( correction != 0 )
295         {
296                 remaining = correction;
297                 for ( d = 1, c = correction; c != 0 && remaining != 0; d++, c = correction / d )
298                         for ( i = n_x * n_y - 1; i >= 0 && c != 0 && remaining != 0; i-- )
299                                 if ( *( weights + i ) + c >= 0 )
300                                 {
301                                         *( weights + i ) += c;
302                                         remaining -= c;
303                                         if ( ( 0 < remaining && remaining < c ) ||
304                                                 ( 0 > remaining && remaining > c ) )
305                                                 c = remaining;
306                                 }
307         }
308 }
309
310
311 static inline int *
312 make_filter_table ( PixopsFilter *filter )
313 {
314         int i_offset, j_offset;
315         int n_x = filter->x.n;
316         int n_y = filter->y.n;
317         int *weights = g_new ( int, SUBSAMPLE * SUBSAMPLE * n_x * n_y );
318
319         for ( i_offset = 0; i_offset < SUBSAMPLE; i_offset++ )
320                 for ( j_offset = 0; j_offset < SUBSAMPLE; j_offset++ )
321                 {
322                         double weight;
323                         int *pixel_weights = weights + ( ( i_offset * SUBSAMPLE ) + j_offset ) * n_x * n_y;
324                         int total = 0;
325                         int i, j;
326
327                         for ( i = 0; i < n_y; i++ )
328                                 for ( j = 0; j < n_x; j++ )
329                                 {
330                                         weight = filter->x.weights[ ( j_offset * n_x ) + j ] *
331                                                  filter->y.weights[ ( i_offset * n_y ) + i ] *
332                                                  filter->overall_alpha * 65536 + 0.5;
333
334                                         total += ( int ) weight;
335
336                                         *( pixel_weights + n_x * i + j ) = weight;
337                                 }
338
339                         correct_total ( pixel_weights, n_x, n_y, total, filter->overall_alpha );
340                 }
341
342         return weights;
343 }
344
345
346 static inline void
347 pixops_process ( guchar *dest_buf,
348                  int render_x0,
349                  int render_y0,
350                  int render_x1,
351                  int render_y1,
352                  int dest_rowstride,
353                  int dest_channels,
354                  gboolean dest_has_alpha,
355                  const guchar *src_buf,
356                  int src_width,
357                  int src_height,
358                  int src_rowstride,
359                  int src_channels,
360                  gboolean src_has_alpha,
361                  double scale_x,
362                  double scale_y,
363                  int check_x,
364                  int check_y,
365                  int check_size,
366                  guint32 color1,
367                  guint32 color2,
368                  PixopsFilter *filter,
369                  PixopsLineFunc line_func )
370 {
371         int i, j;
372         int x, y;                       /* X and Y position in source (fixed_point) */
373
374         guchar **line_bufs = g_new ( guchar *, filter->y.n );
375         int *filter_weights = make_filter_table ( filter );
376
377         int x_step = ( 1 << SCALE_SHIFT ) / scale_x; /* X step in source (fixed point) */
378         int y_step = ( 1 << SCALE_SHIFT ) / scale_y; /* Y step in source (fixed point) */
379
380         int check_shift = check_size ? get_check_shift ( check_size ) : 0;
381
382         int scaled_x_offset = floor ( filter->x.offset * ( 1 << SCALE_SHIFT ) );
383
384         /* Compute the index where we run off the end of the source buffer. The furthest
385          * source pixel we access at index i is:
386          *
387          *  ((render_x0 + i) * x_step + scaled_x_offset) >> SCALE_SHIFT + filter->x.n - 1
388          *
389          * So, run_end_index is the smallest i for which this pixel is src_width, i.e, for which:
390          *
391          *  (i + render_x0) * x_step >= ((src_width - filter->x.n + 1) << SCALE_SHIFT) - scaled_x_offset
392          *
393          */
394 #define MYDIV(a,b) ((a) > 0 ? (a) / (b) : ((a) - (b) + 1) / (b))    /* Division so that -1/5 = -1 */
395
396         int run_end_x = ( ( ( src_width - filter->x.n + 1 ) << SCALE_SHIFT ) - scaled_x_offset );
397         int run_end_index = MYDIV ( run_end_x + x_step - 1, x_step ) - render_x0;
398         run_end_index = MIN ( run_end_index, render_x1 - render_x0 );
399
400         y = render_y0 * y_step + floor ( filter->y.offset * ( 1 << SCALE_SHIFT ) );
401         for ( i = 0; i < ( render_y1 - render_y0 ); i++ )
402         {
403                 int dest_x;
404                 int y_start = y >> SCALE_SHIFT;
405                 int x_start;
406                 int *run_weights = filter_weights +
407                                    ( ( y >> ( SCALE_SHIFT - SUBSAMPLE_BITS ) ) & SUBSAMPLE_MASK ) *
408                                    filter->x.n * filter->y.n * SUBSAMPLE;
409                 guchar *new_outbuf;
410                 guint32 tcolor1, tcolor2;
411
412                 guchar *outbuf = dest_buf + dest_rowstride * i;
413                 guchar *outbuf_end = outbuf + dest_channels * ( render_x1 - render_x0 );
414
415                 if ( ( ( i + check_y ) >> check_shift ) & 1 )
416                 {
417                         tcolor1 = color2;
418                         tcolor2 = color1;
419                 }
420                 else
421                 {
422                         tcolor1 = color1;
423                         tcolor2 = color2;
424                 }
425
426                 for ( j = 0; j < filter->y.n; j++ )
427                 {
428                         if ( y_start < 0 )
429                                 line_bufs[ j ] = ( guchar * ) src_buf;
430                         else if ( y_start < src_height )
431                                 line_bufs[ j ] = ( guchar * ) src_buf + src_rowstride * y_start;
432                         else
433                                 line_bufs[ j ] = ( guchar * ) src_buf + src_rowstride * ( src_height - 1 );
434
435                         y_start++;
436                 }
437
438                 dest_x = check_x;
439                 x = render_x0 * x_step + scaled_x_offset;
440                 x_start = x >> SCALE_SHIFT;
441
442                 while ( x_start < 0 && outbuf < outbuf_end )
443                 {
444                         process_pixel ( run_weights + ( ( x >> ( SCALE_SHIFT - SUBSAMPLE_BITS ) ) & SUBSAMPLE_MASK ) * ( filter->x.n * filter->y.n ),
445                                         filter->x.n, filter->y.n,
446                                         outbuf, dest_x, dest_channels,
447                                         line_bufs, src_channels,
448                                         x >> SCALE_SHIFT, src_width );
449
450                         x += x_step;
451                         x_start = x >> SCALE_SHIFT;
452                         dest_x++;
453                         outbuf += dest_channels;
454                 }
455
456                 new_outbuf = ( *line_func ) ( run_weights, filter->x.n, filter->y.n,
457                                               outbuf, dest_x,
458                                               dest_buf + dest_rowstride * i + run_end_index * dest_channels,
459                                               line_bufs,
460                                               x, x_step, src_width );
461
462                 dest_x += ( new_outbuf - outbuf ) / dest_channels;
463
464                 x = ( dest_x - check_x + render_x0 ) * x_step + scaled_x_offset;
465                 outbuf = new_outbuf;
466
467                 while ( outbuf < outbuf_end )
468                 {
469                         process_pixel ( run_weights + ( ( x >> ( SCALE_SHIFT - SUBSAMPLE_BITS ) ) & SUBSAMPLE_MASK ) * ( filter->x.n * filter->y.n ),
470                                         filter->x.n, filter->y.n,
471                                         outbuf, dest_x, dest_channels,
472                                         line_bufs, src_channels,
473                                         x >> SCALE_SHIFT, src_width );
474
475                         x += x_step;
476                         dest_x++;
477                         outbuf += dest_channels;
478                 }
479
480                 y += y_step;
481         }
482
483         g_free ( line_bufs );
484         g_free ( filter_weights );
485 }
486
487
488 /* Compute weights for reconstruction by replication followed by
489  * sampling with a box filter
490  */
491 static inline void
492 tile_make_weights ( PixopsFilterDimension *dim,
493                     double scale )
494 {
495         int n = ceil ( 1 / scale + 1 );
496         double *pixel_weights = g_new ( double, SUBSAMPLE * n );
497         int offset;
498         int i;
499
500         dim->n = n;
501         dim->offset = 0;
502         dim->weights = pixel_weights;
503
504         for ( offset = 0; offset < SUBSAMPLE; offset++ )
505         {
506                 double x = ( double ) offset / SUBSAMPLE;
507                 double a = x + 1 / scale;
508
509                 for ( i = 0; i < n; i++ )
510                 {
511                         if ( i < x )
512                         {
513                                 if ( i + 1 > x )
514                                         * ( pixel_weights++ ) = ( MIN ( i + 1, a ) - x ) * scale;
515                                 else
516                                         *( pixel_weights++ ) = 0;
517                         }
518                         else
519                         {
520                                 if ( a > i )
521                                         * ( pixel_weights++ ) = ( MIN ( i + 1, a ) - i ) * scale;
522                                 else
523                                         *( pixel_weights++ ) = 0;
524                         }
525                 }
526         }
527 }
528
529 /* Compute weights for a filter that, for minification
530  * is the same as 'tiles', and for magnification, is bilinear
531  * reconstruction followed by a sampling with a delta function.
532  */
533 static inline void
534 bilinear_magnify_make_weights ( PixopsFilterDimension *dim,
535                                 double scale )
536 {
537         double * pixel_weights;
538         int n;
539         int offset;
540         int i;
541
542         if ( scale > 1.0 )              /* Linear */
543         {
544                 n = 2;
545                 dim->offset = 0.5 * ( 1 / scale - 1 );
546         }
547         else                          /* Tile */
548         {
549                 n = ceil ( 1.0 + 1.0 / scale );
550                 dim->offset = 0.0;
551         }
552
553         dim->n = n;
554         dim->weights = g_new ( double, SUBSAMPLE * n );
555
556         pixel_weights = dim->weights;
557
558         for ( offset = 0; offset < SUBSAMPLE; offset++ )
559         {
560                 double x = ( double ) offset / SUBSAMPLE;
561
562                 if ( scale > 1.0 )        /* Linear */
563                 {
564                         for ( i = 0; i < n; i++ )
565                                 *( pixel_weights++ ) = ( ( ( i == 0 ) ? ( 1 - x ) : x ) / scale ) * scale;
566                 }
567                 else                  /* Tile */
568                 {
569                         double a = x + 1 / scale;
570
571                         /*           x
572                          * ---------|--.-|----|--.-|-------  SRC
573                          * ------------|---------|---------  DEST
574                          */
575                         for ( i = 0; i < n; i++ )
576                         {
577                                 if ( i < x )
578                                 {
579                                         if ( i + 1 > x )
580                                                 * ( pixel_weights++ ) = ( MIN ( i + 1, a ) - x ) * scale;
581                                         else
582                                                 *( pixel_weights++ ) = 0;
583                                 }
584                                 else
585                                 {
586                                         if ( a > i )
587                                                 * ( pixel_weights++ ) = ( MIN ( i + 1, a ) - i ) * scale;
588                                         else
589                                                 *( pixel_weights++ ) = 0;
590                                 }
591                         }
592                 }
593         }
594 }
595
596 /* Computes the integral from b0 to b1 of
597  *
598  * f(x) = x; 0 <= x < 1
599  * f(x) = 0; otherwise
600  *
601  * We combine two of these to compute the convolution of
602  * a box filter with a triangular spike.
603  */
604 static inline double
605 linear_box_half ( double b0, double b1 )
606 {
607         double a0, a1;
608         double x0, x1;
609
610         a0 = 0.;
611         a1 = 1.;
612
613         if ( a0 < b0 )
614         {
615                 if ( a1 > b0 )
616                 {
617                         x0 = b0;
618                         x1 = MIN ( a1, b1 );
619                 }
620                 else
621                         return 0;
622         }
623         else
624         {
625                 if ( b1 > a0 )
626                 {
627                         x0 = a0;
628                         x1 = MIN ( a1, b1 );
629                 }
630                 else
631                         return 0;
632         }
633
634         return 0.5 * ( x1 * x1 - x0 * x0 );
635 }
636
637 /* Compute weights for reconstructing with bilinear
638  * interpolation, then sampling with a box filter
639  */
640 static inline void
641 bilinear_box_make_weights ( PixopsFilterDimension *dim,
642                             double scale )
643 {
644         int n = ceil ( 1 / scale + 2.0 );
645         double *pixel_weights = g_new ( double, SUBSAMPLE * n );
646         double w;
647         int offset, i;
648
649         dim->offset = -1.0;
650         dim->n = n;
651         dim->weights = pixel_weights;
652
653         for ( offset = 0 ; offset < SUBSAMPLE; offset++ )
654         {
655                 double x = ( double ) offset / SUBSAMPLE;
656                 double a = x + 1 / scale;
657
658                 for ( i = 0; i < n; i++ )
659                 {
660                         w = linear_box_half ( 0.5 + i - a, 0.5 + i - x );
661                         w += linear_box_half ( 1.5 + x - i, 1.5 + a - i );
662
663                         *( pixel_weights++ ) = w * scale;
664                 }
665         }
666 }
667
668
669 static inline void
670 make_weights ( PixopsFilter *filter,
671                PixopsInterpType interp_type,
672                double scale_x,
673                double scale_y )
674 {
675         switch ( interp_type )
676         {
677         case PIXOPS_INTERP_NEAREST:
678                 g_assert_not_reached ();
679                 break;
680
681         case PIXOPS_INTERP_TILES:
682                 tile_make_weights ( &filter->x, scale_x );
683                 tile_make_weights ( &filter->y, scale_y );
684                 break;
685
686         case PIXOPS_INTERP_BILINEAR:
687                 bilinear_magnify_make_weights ( &filter->x, scale_x );
688                 bilinear_magnify_make_weights ( &filter->y, scale_y );
689                 break;
690
691         case PIXOPS_INTERP_HYPER:
692                 bilinear_box_make_weights ( &filter->x, scale_x );
693                 bilinear_box_make_weights ( &filter->y, scale_y );
694                 break;
695         }
696 }
697
698
699 void
700 yuv422_scale ( guchar *dest_buf,
701                int render_x0,
702                int render_y0,
703                int render_x1,
704                int render_y1,
705                int dest_rowstride,
706                int dest_channels,
707                gboolean dest_has_alpha,
708                const guchar *src_buf,
709                int src_width,
710                int src_height,
711                int src_rowstride,
712                int src_channels,
713                gboolean src_has_alpha,
714                double scale_x,
715                double scale_y,
716                PixopsInterpType interp_type )
717 {
718         PixopsFilter filter = { { 0, 0, 0}, { 0, 0, 0 }, 0 };
719         PixopsLineFunc line_func;
720
721 #ifdef USE_MMX
722         gboolean found_mmx = pixops_have_mmx();
723 #endif
724
725         //g_return_if_fail ( !( dest_channels == 3 && dest_has_alpha ) );
726         //g_return_if_fail ( !( src_channels == 3 && src_has_alpha ) );
727         //g_return_if_fail ( !( src_has_alpha && !dest_has_alpha ) );
728
729         if ( scale_x == 0 || scale_y == 0 )
730                 return ;
731
732         if ( interp_type == PIXOPS_INTERP_NEAREST )
733         {
734                 pixops_scale_nearest ( dest_buf, render_x0, render_y0, render_x1, render_y1,
735                                        dest_rowstride,
736                                        src_buf, src_width, src_height, src_rowstride,
737                                        scale_x, scale_y );
738                 return;
739         }
740
741         filter.overall_alpha = 1.0;
742         make_weights ( &filter, interp_type, scale_x, scale_y );
743
744         if ( filter.x.n == 2 && filter.y.n == 2 )
745         {
746 #ifdef USE_MMX
747                 if ( found_mmx )
748                 {
749                         //fprintf( stderr, "rescale: using mmx\n" );
750                         line_func = scale_line_22_yuv_mmx_stub;
751                 }
752                 else
753 #endif
754
755                         line_func = scale_line_22_yuv;
756         }
757         else
758                 line_func = scale_line;
759
760         pixops_process ( dest_buf, render_x0, render_y0, render_x1, render_y1,
761                          dest_rowstride, dest_channels, dest_has_alpha,
762                          src_buf, src_width, src_height, src_rowstride, src_channels,
763                          src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
764                          &filter, line_func );
765
766         g_free ( filter.x.weights );
767         g_free ( filter.y.weights );
768 }
769