1 /* GdkPixbuf library - Scaling and compositing functions
4 * Copyright (C) 2000 Red Hat, Inc
5 * Author: Owen Taylor <otaylor@redhat.com>
7 * Modification for MLT:
8 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
9 * Author: Dan Dennedy <dan@dennedy.org>
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.
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.
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.
33 #define SUBSAMPLE_BITS 4
34 #define SUBSAMPLE (1 << SUBSAMPLE_BITS)
35 #define SUBSAMPLE_MASK ((1 << SUBSAMPLE_BITS)-1)
36 #define SCALE_SHIFT 16
38 typedef struct _PixopsFilter PixopsFilter
;
39 typedef struct _PixopsFilterDimension PixopsFilterDimension
;
41 struct _PixopsFilterDimension
50 PixopsFilterDimension x
;
51 PixopsFilterDimension y
;
55 typedef guchar
*( *PixopsLineFunc
) ( int *weights
, int n_x
, int n_y
,
56 guchar
*dest
, int dest_x
, guchar
*dest_end
,
58 int x_init
, int x_step
, int src_width
);
60 typedef void ( *PixopsPixelFunc
) ( guchar
*dest
, guint y1
, guint cr
, guint y2
, guint cb
);
63 /* mmx function declarations */
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 );
70 get_check_shift ( int check_size
)
73 g_return_val_if_fail ( check_size
>= 0, 4 );
75 while ( !( check_size
& 1 ) )
85 pixops_scale_nearest ( guchar
*dest_buf
,
91 const guchar
*src_buf
,
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
;
103 for ( i
= 0; i
< ( render_y1
- render_y0
); i
++ )
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 );
109 for ( j
= 0; j
< ( render_x1
- render_x0
); j
++ )
111 x_scaled
= x
>> SCALE_SHIFT
;
112 *dest
++ = src
[ x_scaled
<< 1 ];
113 *dest
++ = src
[ ( ( x_scaled
>> 1 ) << 2 ) + ( ( j
& 1 ) << 1 ) + 1 ];
120 static inline guchar
*
121 scale_line ( int *weights
, int n_x
, int n_y
,
122 guchar
*dest
, int dest_x
, guchar
*dest_end
,
124 int x_init
, int x_step
, int src_width
)
126 register int x
= x_init
;
127 register int i
, j
, x_scaled
, y_index
, uv_index
;
129 while ( dest
< dest_end
)
131 unsigned int y
= 0, uv
= 0;
132 int *pixel_weights
= weights
+ ( ( x
>> ( SCALE_SHIFT
- SUBSAMPLE_BITS
) ) & SUBSAMPLE_MASK
) * n_x
* n_y
;
134 x_scaled
= x
>> SCALE_SHIFT
;
135 y_index
= x_scaled
<< 1;
136 uv_index
= ( ( x_scaled
>> 1 ) << 2 ) + ( ( dest_x
& 1 ) << 1 ) + 1;
138 for ( i
= 0; i
< n_y
; i
++ )
140 int *line_weights
= pixel_weights
+ n_x
* i
;
141 guchar
*q
= src
[ i
];
143 for ( j
= 0; j
< n_x
; j
++ )
145 unsigned int ta
= line_weights
[ j
];
147 y
+= ta
* q
[ y_index
];
148 uv
+= ta
* q
[ uv_index
];
152 *dest
++ = ( y
+ 0xffff ) >> SCALE_SHIFT
;
153 *dest
++ = ( uv
+ 0xffff ) >> SCALE_SHIFT
;
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
,
167 int x_init
, int x_step
, int src_width
)
169 guint32 mmx_weights
[ 16 ][ 8 ];
172 for ( j
= 0; j
< 16; j
++ )
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 );
184 return pixops_scale_line_22_yuv_mmx ( mmx_weights
, dest
, src
[ 0 ], src
[ 1 ], x_step
, dest_end
, x_init
, dest_x
);
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
,
192 int x_init
, int x_step
, int src_width
)
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
;
202 while ( dest
< dest_end
)
204 int *pixel_weights
= weights
+ ( ( x
>> ( SCALE_SHIFT
- SUBSAMPLE_BITS
) ) & SUBSAMPLE_MASK
) * 4;
206 x_scaled
= x
>> SCALE_SHIFT
;
208 w1
= pixel_weights
[ 0 ];
209 w2
= pixel_weights
[ 1 ];
210 w3
= pixel_weights
[ 2 ];
211 w4
= pixel_weights
[ 3 ];
214 q0
= src0
+ ( x_scaled
<< 1 );
215 q1
= src1
+ ( x_scaled
<< 1 );
220 *dest
++ = ( p
+ 0x8000 ) >> SCALE_SHIFT
;
223 x_aligned
= ( ( x_scaled
>> 1 ) << 2 );
224 uv_index
= ( ( dest_x
& 1 ) << 1 ) + 1;
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
];
236 *dest
++ = ( p
+ 0x8000 ) >> SCALE_SHIFT
;
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
)
249 register unsigned int y
= 0, uv
= 0;
251 int uv_index
= ( ( dest_x
& 1 ) << 1 ) + 1;
253 for ( i
= 0; i
< n_y
; i
++ )
255 int *line_weights
= weights
+ n_x
* i
;
257 for ( j
= 0; j
< n_x
; j
++ )
259 unsigned int ta
= 0xff * line_weights
[ j
];
261 if ( x_start
+ j
< 0 )
263 y
+= ta
* src
[ i
][ 0 ];
264 uv
+= ta
* src
[ i
][ uv_index
];
266 else if ( x_start
+ j
< src_width
)
268 y
+= ta
* src
[ i
][ ( x_start
+ j
) << 1 ];
269 uv
+= ta
* src
[ i
][ ( ( ( x_start
+ j
) >> 1 ) << 2) + uv_index
];
273 y
+= ta
* src
[ i
][ ( src_width
- 1 ) << 1 ];
274 uv
+= ta
* src
[ i
][ ( ( ( src_width
- 1 ) >> 1 ) << 2) + uv_index
];
279 *dest
++ = ( y
+ 0xffffff ) >> 24;
280 *dest
++ = ( uv
+ 0xffffff ) >> 24;
285 correct_total ( int *weights
,
289 double overall_alpha
)
291 int correction
= ( int ) ( 0.5 + 65536 * overall_alpha
) - total
;
292 int remaining
, c
, d
, i
;
294 if ( correction
!= 0 )
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 )
301 *( weights
+ i
) += c
;
303 if ( ( 0 < remaining
&& remaining
< c
) ||
304 ( 0 > remaining
&& remaining
> c
) )
312 make_filter_table ( PixopsFilter
*filter
)
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
);
319 for ( i_offset
= 0; i_offset
< SUBSAMPLE
; i_offset
++ )
320 for ( j_offset
= 0; j_offset
< SUBSAMPLE
; j_offset
++ )
323 int *pixel_weights
= weights
+ ( ( i_offset
* SUBSAMPLE
) + j_offset
) * n_x
* n_y
;
327 for ( i
= 0; i
< n_y
; i
++ )
328 for ( j
= 0; j
< n_x
; j
++ )
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;
334 total
+= ( int ) weight
;
336 *( pixel_weights
+ n_x
* i
+ j
) = weight
;
339 correct_total ( pixel_weights
, n_x
, n_y
, total
, filter
->overall_alpha
);
347 pixops_process ( guchar
*dest_buf
,
354 gboolean dest_has_alpha
,
355 const guchar
*src_buf
,
360 gboolean src_has_alpha
,
368 PixopsFilter
*filter
,
369 PixopsLineFunc line_func
)
372 int x
, y
; /* X and Y position in source (fixed_point) */
374 guchar
**line_bufs
= g_new ( guchar
*, filter
->y
.n
);
375 int *filter_weights
= make_filter_table ( filter
);
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) */
380 int check_shift
= check_size ?
get_check_shift ( check_size
) : 0;
382 int scaled_x_offset
= floor ( filter
->x
.offset
* ( 1 << SCALE_SHIFT
) );
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:
387 * ((render_x0 + i) * x_step + scaled_x_offset) >> SCALE_SHIFT + filter->x.n - 1
389 * So, run_end_index is the smallest i for which this pixel is src_width, i.e, for which:
391 * (i + render_x0) * x_step >= ((src_width - filter->x.n + 1) << SCALE_SHIFT) - scaled_x_offset
394 #define MYDIV(a,b) ((a) > 0 ? (a) / (b) : ((a) - (b) + 1) / (b)) /* Division so that -1/5 = -1 */
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
);
400 y
= render_y0
* y_step
+ floor ( filter
->y
.offset
* ( 1 << SCALE_SHIFT
) );
401 for ( i
= 0; i
< ( render_y1
- render_y0
); i
++ )
404 int y_start
= y
>> SCALE_SHIFT
;
406 int *run_weights
= filter_weights
+
407 ( ( y
>> ( SCALE_SHIFT
- SUBSAMPLE_BITS
) ) & SUBSAMPLE_MASK
) *
408 filter
->x
.n
* filter
->y
.n
* SUBSAMPLE
;
410 guint32 tcolor1
, tcolor2
;
412 guchar
*outbuf
= dest_buf
+ dest_rowstride
* i
;
413 guchar
*outbuf_end
= outbuf
+ dest_channels
* ( render_x1
- render_x0
);
415 if ( ( ( i
+ check_y
) >> check_shift
) & 1 )
426 for ( j
= 0; j
< filter
->y
.n
; j
++ )
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
;
433 line_bufs
[ j
] = ( guchar
* ) src_buf
+ src_rowstride
* ( src_height
- 1 );
439 x
= render_x0
* x_step
+ scaled_x_offset
;
440 x_start
= x
>> SCALE_SHIFT
;
442 while ( x_start
< 0 && outbuf
< outbuf_end
)
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
);
451 x_start
= x
>> SCALE_SHIFT
;
453 outbuf
+= dest_channels
;
456 new_outbuf
= ( *line_func
) ( run_weights
, filter
->x
.n
, filter
->y
.n
,
458 dest_buf
+ dest_rowstride
* i
+ run_end_index
* dest_channels
,
460 x
, x_step
, src_width
);
462 dest_x
+= ( new_outbuf
- outbuf
) / dest_channels
;
464 x
= ( dest_x
- check_x
+ render_x0
) * x_step
+ scaled_x_offset
;
467 while ( outbuf
< outbuf_end
)
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
);
477 outbuf
+= dest_channels
;
483 g_free ( line_bufs
);
484 g_free ( filter_weights
);
488 /* Compute weights for reconstruction by replication followed by
489 * sampling with a box filter
492 tile_make_weights ( PixopsFilterDimension
*dim
,
495 int n
= ceil ( 1 / scale
+ 1 );
496 double *pixel_weights
= g_new ( double, SUBSAMPLE
* n
);
502 dim
->weights
= pixel_weights
;
504 for ( offset
= 0; offset
< SUBSAMPLE
; offset
++ )
506 double x
= ( double ) offset
/ SUBSAMPLE
;
507 double a
= x
+ 1 / scale
;
509 for ( i
= 0; i
< n
; i
++ )
514 * ( pixel_weights
++ ) = ( MIN ( i
+ 1, a
) - x
) * scale
;
516 *( pixel_weights
++ ) = 0;
521 * ( pixel_weights
++ ) = ( MIN ( i
+ 1, a
) - i
) * scale
;
523 *( pixel_weights
++ ) = 0;
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.
534 bilinear_magnify_make_weights ( PixopsFilterDimension
*dim
,
537 double * pixel_weights
;
542 if ( scale
> 1.0 ) /* Linear */
545 dim
->offset
= 0.5 * ( 1 / scale
- 1 );
549 n
= ceil ( 1.0 + 1.0 / scale
);
554 dim
->weights
= g_new ( double, SUBSAMPLE
* n
);
556 pixel_weights
= dim
->weights
;
558 for ( offset
= 0; offset
< SUBSAMPLE
; offset
++ )
560 double x
= ( double ) offset
/ SUBSAMPLE
;
562 if ( scale
> 1.0 ) /* Linear */
564 for ( i
= 0; i
< n
; i
++ )
565 *( pixel_weights
++ ) = ( ( ( i
== 0 ) ?
( 1 - x
) : x
) / scale
) * scale
;
569 double a
= x
+ 1 / scale
;
572 * ---------|--.-|----|--.-|------- SRC
573 * ------------|---------|--------- DEST
575 for ( i
= 0; i
< n
; i
++ )
580 * ( pixel_weights
++ ) = ( MIN ( i
+ 1, a
) - x
) * scale
;
582 *( pixel_weights
++ ) = 0;
587 * ( pixel_weights
++ ) = ( MIN ( i
+ 1, a
) - i
) * scale
;
589 *( pixel_weights
++ ) = 0;
596 /* Computes the integral from b0 to b1 of
598 * f(x) = x; 0 <= x < 1
599 * f(x) = 0; otherwise
601 * We combine two of these to compute the convolution of
602 * a box filter with a triangular spike.
605 linear_box_half ( double b0
, double b1
)
634 return 0.5 * ( x1
* x1
- x0
* x0
);
637 /* Compute weights for reconstructing with bilinear
638 * interpolation, then sampling with a box filter
641 bilinear_box_make_weights ( PixopsFilterDimension
*dim
,
644 int n
= ceil ( 1 / scale
+ 2.0 );
645 double *pixel_weights
= g_new ( double, SUBSAMPLE
* n
);
651 dim
->weights
= pixel_weights
;
653 for ( offset
= 0 ; offset
< SUBSAMPLE
; offset
++ )
655 double x
= ( double ) offset
/ SUBSAMPLE
;
656 double a
= x
+ 1 / scale
;
658 for ( i
= 0; i
< n
; i
++ )
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
);
663 *( pixel_weights
++ ) = w
* scale
;
670 make_weights ( PixopsFilter
*filter
,
671 PixopsInterpType interp_type
,
675 switch ( interp_type
)
677 case PIXOPS_INTERP_NEAREST
:
678 g_assert_not_reached ();
681 case PIXOPS_INTERP_TILES
:
682 tile_make_weights ( &filter
->x
, scale_x
);
683 tile_make_weights ( &filter
->y
, scale_y
);
686 case PIXOPS_INTERP_BILINEAR
:
687 bilinear_magnify_make_weights ( &filter
->x
, scale_x
);
688 bilinear_magnify_make_weights ( &filter
->y
, scale_y
);
691 case PIXOPS_INTERP_HYPER
:
692 bilinear_box_make_weights ( &filter
->x
, scale_x
);
693 bilinear_box_make_weights ( &filter
->y
, scale_y
);
700 yuv422_scale ( guchar
*dest_buf
,
707 gboolean dest_has_alpha
,
708 const guchar
*src_buf
,
713 gboolean src_has_alpha
,
716 PixopsInterpType interp_type
)
719 PixopsLineFunc line_func
;
722 gboolean found_mmx
= pixops_have_mmx();
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 ) );
729 if ( scale_x
== 0 || scale_y
== 0 )
732 if ( interp_type
== PIXOPS_INTERP_NEAREST
)
734 pixops_scale_nearest ( dest_buf
, render_x0
, render_y0
, render_x1
, render_y1
,
736 src_buf
, src_width
, src_height
, src_rowstride
,
741 filter
.overall_alpha
= 1.0;
742 make_weights ( &filter
, interp_type
, scale_x
, scale_y
);
744 if ( filter
.x
.n
== 2 && filter
.y
.n
== 2 )
749 //fprintf( stderr, "rescale: using mmx\n" );
750 line_func
= scale_line_22_yuv_mmx_stub
;
755 line_func
= scale_line_22_yuv
;
758 line_func
= scale_line
;
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
);
766 g_free ( filter
.x
.weights
);
767 g_free ( filter
.y
.weights
);