1 /* GdkPixbuf library - Scaling and compositing functions
3 * Copyright (C) 1999 The Free Software Foundation
5 * Author: Owen Taylor <otaylor@redhat.com>
6 * Modified for YUV422 by Dan Dennedy <dan@dennedy.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
30 #define SUBSAMPLE_BITS 4
31 #define SUBSAMPLE (1 << SUBSAMPLE_BITS)
32 #define SUBSAMPLE_MASK ((1 << SUBSAMPLE_BITS)-1)
33 #define SCALE_SHIFT 16
35 typedef struct _PixopsFilter PixopsFilter
;
36 typedef struct _PixopsFilterDimension PixopsFilterDimension
;
38 struct _PixopsFilterDimension
47 PixopsFilterDimension x
;
48 PixopsFilterDimension y
;
52 typedef guchar
*( *PixopsLineFunc
) ( int *weights
, int n_x
, int n_y
,
53 guchar
*dest
, int dest_x
, guchar
*dest_end
,
55 int x_init
, int x_step
, int src_width
);
57 typedef void ( *PixopsPixelFunc
) ( guchar
*dest
, guint y1
, guint cr
, guint y2
, guint cb
);
60 /* mmx function declarations */
62 guchar
*pixops_scale_line_22_33_mmx ( guint32 weights
[ 16 ][ 8 ], guchar
*p
, guchar
*q1
, guchar
*q2
, int x_step
, guchar
*p_stop
, int x_init
);
63 int pixops_have_mmx ( void );
67 get_check_shift ( int check_size
)
70 g_return_val_if_fail ( check_size
>= 0, 4 );
72 while ( !( check_size
& 1 ) )
82 pixops_scale_nearest ( guchar
*dest_buf
,
88 const guchar
*src_buf
,
97 int x_step
= ( 1 << SCALE_SHIFT
) / scale_x
;
98 int y_step
= ( 1 << SCALE_SHIFT
) / scale_y
;
100 for ( i
= 0; i
< ( render_y1
- render_y0
); i
++ )
102 const guchar
*src
= src_buf
+ ( ( ( i
+ render_y0
) * y_step
+ y_step
/ 2 ) >> SCALE_SHIFT
) * src_rowstride
;
103 guchar
*dest
= dest_buf
+ i
* dest_rowstride
;
105 x
= render_x0
* x_step
+ x_step
/ 2;
107 for ( j
= 0; j
< ( render_x1
- render_x0
); j
++ )
109 const guchar
*p
= src
+ ( x
>> SCALE_SHIFT
) * 4;
112 p32
= ( guint32
* ) dest
;
113 *p32
= *( ( guint32
* ) p
);
122 static inline guchar
*
123 scale_line ( int *weights
, int n_x
, int n_y
,
124 guchar
*dest
, int dest_x
, guchar
*dest_end
,
126 int x_init
, int x_step
, int src_width
)
131 while ( dest
< dest_end
)
133 int x_scaled
= x
>> SCALE_SHIFT
;
136 pixel_weights
= weights
+ ( ( x
>> ( SCALE_SHIFT
- SUBSAMPLE_BITS
) ) & SUBSAMPLE_MASK
) * n_x
* n_y
;
138 unsigned int y1
= 0, cb
= 0, y2
= 0, cr
= 0;
139 for ( i
= 0; i
< n_y
; i
++ )
141 guchar
*q
= src
[ i
] + x_scaled
* 4;
142 int *line_weights
= pixel_weights
+ n_x
* i
;
144 for ( j
= 0; j
< n_x
; j
++ )
146 unsigned int ta
= line_weights
[ j
];
157 dest
[ 0 ] = ( y1
+ 0xffff ) >> 16;
158 dest
[ 1 ] = ( cb
+ 0xffff ) >> 16;
159 dest
[ 2 ] = ( y2
+ 0xffff ) >> 16;
160 dest
[ 3 ] = ( cr
+ 0xffff ) >> 16;
171 static inline guchar
*
172 scale_line_22_33_mmx_stub ( int *weights
, int n_x
, int n_y
,
173 guchar
*dest
, int dest_x
, guchar
*dest_end
,
175 int x_init
, int x_step
, int src_width
)
177 guint32 mmx_weights
[ 16 ][ 8 ];
180 for ( j
= 0; j
< 16; j
++ )
182 mmx_weights
[ j
][ 0 ] = 0x00010001 * ( weights
[ 4 * j
] >> 8 );
183 mmx_weights
[ j
][ 1 ] = 0x00010001 * ( weights
[ 4 * j
] >> 8 );
184 mmx_weights
[ j
][ 2 ] = 0x00010001 * ( weights
[ 4 * j
+ 1 ] >> 8 );
185 mmx_weights
[ j
][ 3 ] = 0x00010001 * ( weights
[ 4 * j
+ 1 ] >> 8 );
186 mmx_weights
[ j
][ 4 ] = 0x00010001 * ( weights
[ 4 * j
+ 2 ] >> 8 );
187 mmx_weights
[ j
][ 5 ] = 0x00010001 * ( weights
[ 4 * j
+ 2 ] >> 8 );
188 mmx_weights
[ j
][ 6 ] = 0x00010001 * ( weights
[ 4 * j
+ 3 ] >> 8 );
189 mmx_weights
[ j
][ 7 ] = 0x00010001 * ( weights
[ 4 * j
+ 3 ] >> 8 );
192 return pixops_scale_line_22_33_mmx ( mmx_weights
, dest
, src
[ 0 ], src
[ 1 ], x_step
, dest_end
, x_init
);
196 static inline guchar
*
197 scale_line_22_33 ( int *weights
, int n_x
, int n_y
,
198 guchar
*dest
, int dest_x
, guchar
*dest_end
,
200 int x_init
, int x_step
, int src_width
)
203 guchar
*src0
= src
[ 0 ];
204 guchar
*src1
= src
[ 1 ];
206 while ( dest
< dest_end
)
208 unsigned int y1
, cb
, y2
, cr
;
209 int x_scaled
= x
>> SCALE_SHIFT
;
214 q0
= src0
+ x_scaled
* 4;
215 q1
= src1
+ x_scaled
* 4;
217 pixel_weights
= weights
+ ( ( x
>> ( SCALE_SHIFT
- SUBSAMPLE_BITS
) ) & SUBSAMPLE_MASK
) * 4;
219 w1
= pixel_weights
[ 0 ];
220 w2
= pixel_weights
[ 1 ];
221 w3
= pixel_weights
[ 2 ];
222 w4
= pixel_weights
[ 3 ];
244 dest
[ 0 ] = ( y1
+ 0x8000 ) >> 16;
245 dest
[ 1 ] = ( cb
+ 0x8000 ) >> 16;
246 dest
[ 2 ] = ( y2
+ 0x8000 ) >> 16;
247 dest
[ 3 ] = ( cr
+ 0x8000 ) >> 16;
258 process_pixel ( int *weights
, int n_x
, int n_y
,
259 guchar
*dest
, int dest_x
, int dest_channels
,
260 guchar
**src
, int src_channels
,
261 int x_start
, int src_width
)
263 unsigned int y1
= 0, cb
= 0, y2
= 0, cr
= 0;
266 for ( i
= 0; i
< n_y
; i
++ )
268 int *line_weights
= weights
+ n_x
* i
;
270 for ( j
= 0; j
< n_x
; j
++ )
275 if ( x_start
+ j
< 0 )
277 else if ( x_start
+ j
< src_width
)
278 q
= src
[ i
] + ( x_start
+ j
) * src_channels
;
280 q
= src
[ i
] + ( src_width
- 1 ) * src_channels
;
282 ta
= 0xff * line_weights
[ j
];
291 dest
[ 0 ] = ( y1
+ 0xffffff ) >> 24;
292 dest
[ 1 ] = ( cb
+ 0xffffff ) >> 24;
293 dest
[ 2 ] = ( y2
+ 0xffffff ) >> 24;
294 dest
[ 3 ] = ( cr
+ 0xffffff ) >> 24;
299 correct_total ( int *weights
,
303 double overall_alpha
)
305 int correction
= ( int ) ( 0.5 + 65536 * overall_alpha
) - total
;
306 int remaining
, c
, d
, i
;
308 if ( correction
!= 0 )
310 remaining
= correction
;
311 for ( d
= 1, c
= correction
; c
!= 0 && remaining
!= 0; d
++, c
= correction
/ d
)
312 for ( i
= n_x
* n_y
- 1; i
>= 0 && c
!= 0 && remaining
!= 0; i
-- )
313 if ( *( weights
+ i
) + c
>= 0 )
315 *( weights
+ i
) += c
;
317 if ( ( 0 < remaining
&& remaining
< c
) ||
318 ( 0 > remaining
&& remaining
> c
) )
326 make_filter_table ( PixopsFilter
*filter
)
328 int i_offset
, j_offset
;
329 int n_x
= filter
->x
.n
;
330 int n_y
= filter
->y
.n
;
331 int *weights
= g_new ( int, SUBSAMPLE
* SUBSAMPLE
* n_x
* n_y
);
333 for ( i_offset
= 0; i_offset
< SUBSAMPLE
; i_offset
++ )
334 for ( j_offset
= 0; j_offset
< SUBSAMPLE
; j_offset
++ )
337 int *pixel_weights
= weights
+ ( ( i_offset
* SUBSAMPLE
) + j_offset
) * n_x
* n_y
;
341 for ( i
= 0; i
< n_y
; i
++ )
342 for ( j
= 0; j
< n_x
; j
++ )
344 weight
= filter
->x
.weights
[ ( j_offset
* n_x
) + j
] *
345 filter
->y
.weights
[ ( i_offset
* n_y
) + i
] *
346 filter
->overall_alpha
* 65536 + 0.5;
348 total
+= ( int ) weight
;
350 *( pixel_weights
+ n_x
* i
+ j
) = weight
;
353 correct_total ( pixel_weights
, n_x
, n_y
, total
, filter
->overall_alpha
);
361 pixops_process ( guchar
*dest_buf
,
368 gboolean dest_has_alpha
,
369 const guchar
*src_buf
,
374 gboolean src_has_alpha
,
382 PixopsFilter
*filter
,
383 PixopsLineFunc line_func
)
386 int x
, y
; /* X and Y position in source (fixed_point) */
388 guchar
**line_bufs
= g_new ( guchar
*, filter
->y
.n
);
389 int *filter_weights
= make_filter_table ( filter
);
391 int x_step
= ( 1 << SCALE_SHIFT
) / scale_x
; /* X step in source (fixed point) */
392 int y_step
= ( 1 << SCALE_SHIFT
) / scale_y
; /* Y step in source (fixed point) */
394 int check_shift
= check_size ?
get_check_shift ( check_size
) : 0;
396 int scaled_x_offset
= floor ( filter
->x
.offset
* ( 1 << SCALE_SHIFT
) );
398 /* Compute the index where we run off the end of the source buffer. The furthest
399 * source pixel we access at index i is:
401 * ((render_x0 + i) * x_step + scaled_x_offset) >> SCALE_SHIFT + filter->x.n - 1
403 * So, run_end_index is the smallest i for which this pixel is src_width, i.e, for which:
405 * (i + render_x0) * x_step >= ((src_width - filter->x.n + 1) << SCALE_SHIFT) - scaled_x_offset
408 #define MYDIV(a,b) ((a) > 0 ? (a) / (b) : ((a) - (b) + 1) / (b)) /* Division so that -1/5 = -1 */
410 int run_end_x
= ( ( ( src_width
- filter
->x
.n
+ 1 ) << SCALE_SHIFT
) - scaled_x_offset
);
411 int run_end_index
= MYDIV ( run_end_x
+ x_step
- 1, x_step
) - render_x0
;
412 run_end_index
= MIN ( run_end_index
, render_x1
- render_x0
);
414 y
= render_y0
* y_step
+ floor ( filter
->y
.offset
* ( 1 << SCALE_SHIFT
) );
415 for ( i
= 0; i
< ( render_y1
- render_y0
); i
++ )
418 int y_start
= y
>> SCALE_SHIFT
;
420 int *run_weights
= filter_weights
+
421 ( ( y
>> ( SCALE_SHIFT
- SUBSAMPLE_BITS
) ) & SUBSAMPLE_MASK
) *
422 filter
->x
.n
* filter
->y
.n
* SUBSAMPLE
;
424 guint32 tcolor1
, tcolor2
;
426 guchar
*outbuf
= dest_buf
+ dest_rowstride
* i
;
427 guchar
*outbuf_end
= outbuf
+ dest_channels
* ( render_x1
- render_x0
);
429 if ( ( ( i
+ check_y
) >> check_shift
) & 1 )
440 for ( j
= 0; j
< filter
->y
.n
; j
++ )
443 line_bufs
[ j
] = ( guchar
* ) src_buf
;
444 else if ( y_start
< src_height
)
445 line_bufs
[ j
] = ( guchar
* ) src_buf
+ src_rowstride
* y_start
;
447 line_bufs
[ j
] = ( guchar
* ) src_buf
+ src_rowstride
* ( src_height
- 1 );
453 x
= render_x0
* x_step
+ scaled_x_offset
;
454 x_start
= x
>> SCALE_SHIFT
;
456 while ( x_start
< 0 && outbuf
< outbuf_end
)
458 process_pixel ( run_weights
+ ( ( x
>> ( SCALE_SHIFT
- SUBSAMPLE_BITS
) ) & SUBSAMPLE_MASK
) * ( filter
->x
.n
* filter
->y
.n
),
459 filter
->x
.n
, filter
->y
.n
,
460 outbuf
, dest_x
, dest_channels
,
461 line_bufs
, src_channels
,
462 x
>> SCALE_SHIFT
, src_width
);
465 x_start
= x
>> SCALE_SHIFT
;
467 outbuf
+= dest_channels
;
470 new_outbuf
= ( *line_func
) ( run_weights
, filter
->x
.n
, filter
->y
.n
,
472 dest_buf
+ dest_rowstride
* i
+ run_end_index
* dest_channels
,
474 x
, x_step
, src_width
);
476 dest_x
+= ( new_outbuf
- outbuf
) / dest_channels
;
478 x
= ( dest_x
- check_x
+ render_x0
) * x_step
+ scaled_x_offset
;
481 while ( outbuf
< outbuf_end
)
483 process_pixel ( run_weights
+ ( ( x
>> ( SCALE_SHIFT
- SUBSAMPLE_BITS
) ) & SUBSAMPLE_MASK
) * ( filter
->x
.n
* filter
->y
.n
),
484 filter
->x
.n
, filter
->y
.n
,
485 outbuf
, dest_x
, dest_channels
,
486 line_bufs
, src_channels
,
487 x
>> SCALE_SHIFT
, src_width
);
491 outbuf
+= dest_channels
;
497 g_free ( line_bufs
);
498 g_free ( filter_weights
);
502 /* Compute weights for reconstruction by replication followed by
503 * sampling with a box filter
506 tile_make_weights ( PixopsFilterDimension
*dim
,
509 int n
= ceil ( 1 / scale
+ 1 );
510 double *pixel_weights
= g_new ( double, SUBSAMPLE
* n
);
516 dim
->weights
= pixel_weights
;
518 for ( offset
= 0; offset
< SUBSAMPLE
; offset
++ )
520 double x
= ( double ) offset
/ SUBSAMPLE
;
521 double a
= x
+ 1 / scale
;
523 for ( i
= 0; i
< n
; i
++ )
528 * ( pixel_weights
++ ) = ( MIN ( i
+ 1, a
) - x
) * scale
;
530 *( pixel_weights
++ ) = 0;
535 * ( pixel_weights
++ ) = ( MIN ( i
+ 1, a
) - i
) * scale
;
537 *( pixel_weights
++ ) = 0;
543 /* Compute weights for a filter that, for minification
544 * is the same as 'tiles', and for magnification, is bilinear
545 * reconstruction followed by a sampling with a delta function.
548 bilinear_magnify_make_weights ( PixopsFilterDimension
*dim
,
551 double * pixel_weights
;
556 if ( scale
> 1.0 ) /* Linear */
559 dim
->offset
= 0.5 * ( 1 / scale
- 1 );
563 n
= ceil ( 1.0 + 1.0 / scale
);
568 dim
->weights
= g_new ( double, SUBSAMPLE
* n
);
570 pixel_weights
= dim
->weights
;
572 for ( offset
= 0; offset
< SUBSAMPLE
; offset
++ )
574 double x
= ( double ) offset
/ SUBSAMPLE
;
576 if ( scale
> 1.0 ) /* Linear */
578 for ( i
= 0; i
< n
; i
++ )
579 *( pixel_weights
++ ) = ( ( ( i
== 0 ) ?
( 1 - x
) : x
) / scale
) * scale
;
583 double a
= x
+ 1 / scale
;
586 * ---------|--.-|----|--.-|------- SRC
587 * ------------|---------|--------- DEST
589 for ( i
= 0; i
< n
; i
++ )
594 * ( pixel_weights
++ ) = ( MIN ( i
+ 1, a
) - x
) * scale
;
596 *( pixel_weights
++ ) = 0;
601 * ( pixel_weights
++ ) = ( MIN ( i
+ 1, a
) - i
) * scale
;
603 *( pixel_weights
++ ) = 0;
610 /* Computes the integral from b0 to b1 of
612 * f(x) = x; 0 <= x < 1
613 * f(x) = 0; otherwise
615 * We combine two of these to compute the convolution of
616 * a box filter with a triangular spike.
619 linear_box_half ( double b0
, double b1
)
648 return 0.5 * ( x1
* x1
- x0
* x0
);
651 /* Compute weights for reconstructing with bilinear
652 * interpolation, then sampling with a box filter
655 bilinear_box_make_weights ( PixopsFilterDimension
*dim
,
658 int n
= ceil ( 1 / scale
+ 2.0 );
659 double *pixel_weights
= g_new ( double, SUBSAMPLE
* n
);
665 dim
->weights
= pixel_weights
;
667 for ( offset
= 0 ; offset
< SUBSAMPLE
; offset
++ )
669 double x
= ( double ) offset
/ SUBSAMPLE
;
670 double a
= x
+ 1 / scale
;
672 for ( i
= 0; i
< n
; i
++ )
674 w
= linear_box_half ( 0.5 + i
- a
, 0.5 + i
- x
);
675 w
+= linear_box_half ( 1.5 + x
- i
, 1.5 + a
- i
);
677 *( pixel_weights
++ ) = w
* scale
;
684 make_weights ( PixopsFilter
*filter
,
685 PixopsInterpType interp_type
,
689 switch ( interp_type
)
691 case PIXOPS_INTERP_NEAREST
:
692 g_assert_not_reached ();
695 case PIXOPS_INTERP_TILES
:
696 tile_make_weights ( &filter
->x
, scale_x
);
697 tile_make_weights ( &filter
->y
, scale_y
);
700 case PIXOPS_INTERP_BILINEAR
:
701 bilinear_magnify_make_weights ( &filter
->x
, scale_x
);
702 bilinear_magnify_make_weights ( &filter
->y
, scale_y
);
705 case PIXOPS_INTERP_HYPER
:
706 bilinear_box_make_weights ( &filter
->x
, scale_x
);
707 bilinear_box_make_weights ( &filter
->y
, scale_y
);
714 yuv422_scale ( guchar
*dest_buf
,
721 gboolean dest_has_alpha
,
722 const guchar
*src_buf
,
727 gboolean src_has_alpha
,
730 PixopsInterpType interp_type
)
733 PixopsLineFunc line_func
;
736 gboolean found_mmx
= pixops_have_mmx();
739 //g_return_if_fail ( !( dest_channels == 3 && dest_has_alpha ) );
740 //g_return_if_fail ( !( src_channels == 3 && src_has_alpha ) );
741 //g_return_if_fail ( !( src_has_alpha && !dest_has_alpha ) );
743 if ( scale_x
== 0 || scale_y
== 0 )
746 if ( interp_type
== PIXOPS_INTERP_NEAREST
)
748 pixops_scale_nearest ( dest_buf
, render_x0
, render_y0
, render_x1
, render_y1
,
750 src_buf
, src_width
, src_height
, src_rowstride
,
755 filter
.overall_alpha
= 1.0;
756 make_weights ( &filter
, interp_type
, scale_x
, scale_y
);
758 fprintf( stderr
, "RESCALE: %d %d\n", filter
.x
.n
, filter
.y
.n
);
759 if ( filter
.x
.n
== 2 && filter
.y
.n
== 2 )
762 if ( 0 && found_mmx
)
763 line_func
= scale_line_22_33_mmx_stub
;
767 line_func
= scale_line_22_33
;
770 line_func
= scale_line
;
772 pixops_process ( dest_buf
, render_x0
, render_y0
, render_x1
, render_y1
,
773 dest_rowstride
, dest_channels
, dest_has_alpha
,
774 src_buf
, src_width
, src_height
, src_rowstride
, src_channels
,
775 src_has_alpha
, scale_x
, scale_y
, 0, 0, 0, 0, 0,
776 &filter
, line_func
);
778 g_free ( filter
.x
.weights
);
779 g_free ( filter
.y
.weights
);