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
,
96 register int x_step
= ( 1 << SCALE_SHIFT
) / scale_x
;
97 register int y_step
= ( 1 << SCALE_SHIFT
) / scale_y
;
98 register int x
, x_scaled
;
100 for ( i
= 0; i
< ( render_y1
- render_y0
); i
++ )
102 const guchar
*src
= src_buf
+ ( ( ( i
+ render_y0
) * y_step
+ ( y_step
>> 1 ) ) >> SCALE_SHIFT
) * src_rowstride
;
103 guchar
*dest
= dest_buf
+ i
* dest_rowstride
;
104 x
= render_x0
* x_step
+ ( x_step
>> 1 );
106 for ( j
= 0; j
< ( render_x1
- render_x0
); j
++ )
108 x_scaled
= x
>> SCALE_SHIFT
;
109 *dest
++ = src
[ x_scaled
<< 1 ];
110 *dest
++ = src
[ ( ( x_scaled
>> 1 ) << 2 ) + ( ( j
& 1 ) << 1 ) + 1 ];
117 static inline guchar
*
118 scale_line ( int *weights
, int n_x
, int n_y
,
119 guchar
*dest
, int dest_x
, guchar
*dest_end
,
121 int x_init
, int x_step
, int src_width
)
123 register int x
= x_init
;
124 register int i
, j
, x_scaled
, y_index
, uv_index
;
126 while ( dest
< dest_end
)
128 unsigned int y
= 0, uv
= 0;
129 int *pixel_weights
= weights
+ ( ( x
>> ( SCALE_SHIFT
- SUBSAMPLE_BITS
) ) & SUBSAMPLE_MASK
) * n_x
* n_y
;
131 x_scaled
= x
>> SCALE_SHIFT
;
132 y_index
= x_scaled
<< 1;
133 uv_index
= ( ( x_scaled
>> 1 ) << 2 ) + ( ( dest_x
& 1 ) << 1 ) + 1;
135 for ( i
= 0; i
< n_y
; i
++ )
137 int *line_weights
= pixel_weights
+ n_x
* i
;
138 guchar
*q
= src
[ i
];
140 for ( j
= 0; j
< n_x
; j
++ )
142 unsigned int ta
= line_weights
[ j
];
144 y
+= ta
* q
[ y_index
];
145 uv
+= ta
* q
[ uv_index
];
149 *dest
++ = ( y
+ 0xffff ) >> SCALE_SHIFT
;
150 *dest
++ = ( uv
+ 0xffff ) >> SCALE_SHIFT
;
160 static inline guchar
*
161 scale_line_22_33_mmx_stub ( int *weights
, int n_x
, int n_y
,
162 guchar
*dest
, int dest_x
, guchar
*dest_end
,
164 int x_init
, int x_step
, int src_width
)
166 guint32 mmx_weights
[ 16 ][ 8 ];
169 for ( j
= 0; j
< 16; j
++ )
171 mmx_weights
[ j
][ 0 ] = 0x00010001 * ( weights
[ 4 * j
] >> 8 );
172 mmx_weights
[ j
][ 1 ] = 0x00010001 * ( weights
[ 4 * j
] >> 8 );
173 mmx_weights
[ j
][ 2 ] = 0x00010001 * ( weights
[ 4 * j
+ 1 ] >> 8 );
174 mmx_weights
[ j
][ 3 ] = 0x00010001 * ( weights
[ 4 * j
+ 1 ] >> 8 );
175 mmx_weights
[ j
][ 4 ] = 0x00010001 * ( weights
[ 4 * j
+ 2 ] >> 8 );
176 mmx_weights
[ j
][ 5 ] = 0x00010001 * ( weights
[ 4 * j
+ 2 ] >> 8 );
177 mmx_weights
[ j
][ 6 ] = 0x00010001 * ( weights
[ 4 * j
+ 3 ] >> 8 );
178 mmx_weights
[ j
][ 7 ] = 0x00010001 * ( weights
[ 4 * j
+ 3 ] >> 8 );
181 return pixops_scale_line_22_33_mmx ( mmx_weights
, dest
, src
[ 0 ], src
[ 1 ], x_step
, dest_end
, x_init
);
185 static inline guchar
*
186 scale_line_22_33 ( int *weights
, int n_x
, int n_y
,
187 guchar
*dest
, int dest_x
, guchar
*dest_end
,
189 int x_init
, int x_step
, int src_width
)
191 register int x
= x_init
;
192 register guchar
*src0
= src
[ 0 ];
193 register guchar
*src1
= src
[ 1 ];
194 register unsigned int p
;
195 register guchar
*q0
, *q1
;
196 register int w1
, w2
, w3
, w4
;
197 register int x_scaled
, x_aligned
, uv_index
;
199 while ( dest
< dest_end
)
201 int *pixel_weights
= weights
+ ( ( x
>> ( SCALE_SHIFT
- SUBSAMPLE_BITS
) ) & SUBSAMPLE_MASK
) * 4;
203 x_scaled
= x
>> SCALE_SHIFT
;
205 w1
= pixel_weights
[ 0 ];
206 w2
= pixel_weights
[ 1 ];
207 w3
= pixel_weights
[ 2 ];
208 w4
= pixel_weights
[ 3 ];
211 q0
= src0
+ ( x_scaled
<< 1 );
212 q1
= src1
+ ( x_scaled
<< 1 );
217 *dest
++ = ( p
+ 0x8000 ) >> SCALE_SHIFT
;
220 x_aligned
= ( ( x_scaled
>> 1 ) << 2 );
221 q0
= src0
+ x_aligned
;
222 q1
= src1
+ x_aligned
;
223 uv_index
= ( ( dest_x
& 1 ) << 1 ) + 1;
224 p
= w1
* q0
[ uv_index
];
225 p
+= w2
* q0
[ uv_index
];
226 p
+= w3
* q1
[ uv_index
];
227 p
+= w4
* q1
[ uv_index
];
228 *dest
++ = ( p
+ 0x8000 ) >> SCALE_SHIFT
;
239 process_pixel ( int *weights
, int n_x
, int n_y
,
240 guchar
*dest
, int dest_x
, int dest_channels
,
241 guchar
**src
, int src_channels
,
242 int x_start
, int src_width
)
244 register unsigned int y
= 0, uv
= 0;
246 int uv_index
= ( ( dest_x
& 1 ) << 1 ) + 1;
248 for ( i
= 0; i
< n_y
; i
++ )
250 int *line_weights
= weights
+ n_x
* i
;
252 for ( j
= 0; j
< n_x
; j
++ )
254 unsigned int ta
= 0xff * line_weights
[ j
];
256 if ( x_start
+ j
< 0 )
258 y
+= ta
* src
[ i
][ 0 ];
259 uv
+= ta
* src
[ i
][ uv_index
];
261 else if ( x_start
+ j
< src_width
)
263 y
+= ta
* src
[ i
][ ( x_start
+ j
) << 1 ];
264 uv
+= ta
* src
[ i
][ ( ( ( x_start
+ j
) >> 1 ) << 2) + uv_index
];
268 y
+= ta
* src
[ i
][ ( src_width
- 1 ) << 1 ];
269 uv
+= ta
* src
[ i
][ ( ( ( src_width
- 1 ) >> 1 ) << 2) + uv_index
];
274 *dest
++ = ( y
+ 0xffffff ) >> 24;
275 *dest
++ = ( uv
+ 0xffffff ) >> 24;
280 correct_total ( int *weights
,
284 double overall_alpha
)
286 int correction
= ( int ) ( 0.5 + 65536 * overall_alpha
) - total
;
287 int remaining
, c
, d
, i
;
289 if ( correction
!= 0 )
291 remaining
= correction
;
292 for ( d
= 1, c
= correction
; c
!= 0 && remaining
!= 0; d
++, c
= correction
/ d
)
293 for ( i
= n_x
* n_y
- 1; i
>= 0 && c
!= 0 && remaining
!= 0; i
-- )
294 if ( *( weights
+ i
) + c
>= 0 )
296 *( weights
+ i
) += c
;
298 if ( ( 0 < remaining
&& remaining
< c
) ||
299 ( 0 > remaining
&& remaining
> c
) )
307 make_filter_table ( PixopsFilter
*filter
)
309 int i_offset
, j_offset
;
310 int n_x
= filter
->x
.n
;
311 int n_y
= filter
->y
.n
;
312 int *weights
= g_new ( int, SUBSAMPLE
* SUBSAMPLE
* n_x
* n_y
);
314 for ( i_offset
= 0; i_offset
< SUBSAMPLE
; i_offset
++ )
315 for ( j_offset
= 0; j_offset
< SUBSAMPLE
; j_offset
++ )
318 int *pixel_weights
= weights
+ ( ( i_offset
* SUBSAMPLE
) + j_offset
) * n_x
* n_y
;
322 for ( i
= 0; i
< n_y
; i
++ )
323 for ( j
= 0; j
< n_x
; j
++ )
325 weight
= filter
->x
.weights
[ ( j_offset
* n_x
) + j
] *
326 filter
->y
.weights
[ ( i_offset
* n_y
) + i
] *
327 filter
->overall_alpha
* 65536 + 0.5;
329 total
+= ( int ) weight
;
331 *( pixel_weights
+ n_x
* i
+ j
) = weight
;
334 correct_total ( pixel_weights
, n_x
, n_y
, total
, filter
->overall_alpha
);
342 pixops_process ( guchar
*dest_buf
,
349 gboolean dest_has_alpha
,
350 const guchar
*src_buf
,
355 gboolean src_has_alpha
,
363 PixopsFilter
*filter
,
364 PixopsLineFunc line_func
)
367 int x
, y
; /* X and Y position in source (fixed_point) */
369 guchar
**line_bufs
= g_new ( guchar
*, filter
->y
.n
);
370 int *filter_weights
= make_filter_table ( filter
);
372 int x_step
= ( 1 << SCALE_SHIFT
) / scale_x
; /* X step in source (fixed point) */
373 int y_step
= ( 1 << SCALE_SHIFT
) / scale_y
; /* Y step in source (fixed point) */
375 int check_shift
= check_size ?
get_check_shift ( check_size
) : 0;
377 int scaled_x_offset
= floor ( filter
->x
.offset
* ( 1 << SCALE_SHIFT
) );
379 /* Compute the index where we run off the end of the source buffer. The furthest
380 * source pixel we access at index i is:
382 * ((render_x0 + i) * x_step + scaled_x_offset) >> SCALE_SHIFT + filter->x.n - 1
384 * So, run_end_index is the smallest i for which this pixel is src_width, i.e, for which:
386 * (i + render_x0) * x_step >= ((src_width - filter->x.n + 1) << SCALE_SHIFT) - scaled_x_offset
389 #define MYDIV(a,b) ((a) > 0 ? (a) / (b) : ((a) - (b) + 1) / (b)) /* Division so that -1/5 = -1 */
391 int run_end_x
= ( ( ( src_width
- filter
->x
.n
+ 1 ) << SCALE_SHIFT
) - scaled_x_offset
);
392 int run_end_index
= MYDIV ( run_end_x
+ x_step
- 1, x_step
) - render_x0
;
393 run_end_index
= MIN ( run_end_index
, render_x1
- render_x0
);
395 y
= render_y0
* y_step
+ floor ( filter
->y
.offset
* ( 1 << SCALE_SHIFT
) );
396 for ( i
= 0; i
< ( render_y1
- render_y0
); i
++ )
399 int y_start
= y
>> SCALE_SHIFT
;
401 int *run_weights
= filter_weights
+
402 ( ( y
>> ( SCALE_SHIFT
- SUBSAMPLE_BITS
) ) & SUBSAMPLE_MASK
) *
403 filter
->x
.n
* filter
->y
.n
* SUBSAMPLE
;
405 guint32 tcolor1
, tcolor2
;
407 guchar
*outbuf
= dest_buf
+ dest_rowstride
* i
;
408 guchar
*outbuf_end
= outbuf
+ dest_channels
* ( render_x1
- render_x0
);
410 if ( ( ( i
+ check_y
) >> check_shift
) & 1 )
421 for ( j
= 0; j
< filter
->y
.n
; j
++ )
424 line_bufs
[ j
] = ( guchar
* ) src_buf
;
425 else if ( y_start
< src_height
)
426 line_bufs
[ j
] = ( guchar
* ) src_buf
+ src_rowstride
* y_start
;
428 line_bufs
[ j
] = ( guchar
* ) src_buf
+ src_rowstride
* ( src_height
- 1 );
434 x
= render_x0
* x_step
+ scaled_x_offset
;
435 x_start
= x
>> SCALE_SHIFT
;
437 while ( x_start
< 0 && outbuf
< outbuf_end
)
439 process_pixel ( run_weights
+ ( ( x
>> ( SCALE_SHIFT
- SUBSAMPLE_BITS
) ) & SUBSAMPLE_MASK
) * ( filter
->x
.n
* filter
->y
.n
),
440 filter
->x
.n
, filter
->y
.n
,
441 outbuf
, dest_x
, dest_channels
,
442 line_bufs
, src_channels
,
443 x
>> SCALE_SHIFT
, src_width
);
446 x_start
= x
>> SCALE_SHIFT
;
448 outbuf
+= dest_channels
;
451 new_outbuf
= ( *line_func
) ( run_weights
, filter
->x
.n
, filter
->y
.n
,
453 dest_buf
+ dest_rowstride
* i
+ run_end_index
* dest_channels
,
455 x
, x_step
, src_width
);
457 dest_x
+= ( new_outbuf
- outbuf
) / dest_channels
;
459 x
= ( dest_x
- check_x
+ render_x0
) * x_step
+ scaled_x_offset
;
462 while ( outbuf
< outbuf_end
)
464 process_pixel ( run_weights
+ ( ( x
>> ( SCALE_SHIFT
- SUBSAMPLE_BITS
) ) & SUBSAMPLE_MASK
) * ( filter
->x
.n
* filter
->y
.n
),
465 filter
->x
.n
, filter
->y
.n
,
466 outbuf
, dest_x
, dest_channels
,
467 line_bufs
, src_channels
,
468 x
>> SCALE_SHIFT
, src_width
);
472 outbuf
+= dest_channels
;
478 g_free ( line_bufs
);
479 g_free ( filter_weights
);
483 /* Compute weights for reconstruction by replication followed by
484 * sampling with a box filter
487 tile_make_weights ( PixopsFilterDimension
*dim
,
490 int n
= ceil ( 1 / scale
+ 1 );
491 double *pixel_weights
= g_new ( double, SUBSAMPLE
* n
);
497 dim
->weights
= pixel_weights
;
499 for ( offset
= 0; offset
< SUBSAMPLE
; offset
++ )
501 double x
= ( double ) offset
/ SUBSAMPLE
;
502 double a
= x
+ 1 / scale
;
504 for ( i
= 0; i
< n
; i
++ )
509 * ( pixel_weights
++ ) = ( MIN ( i
+ 1, a
) - x
) * scale
;
511 *( pixel_weights
++ ) = 0;
516 * ( pixel_weights
++ ) = ( MIN ( i
+ 1, a
) - i
) * scale
;
518 *( pixel_weights
++ ) = 0;
524 /* Compute weights for a filter that, for minification
525 * is the same as 'tiles', and for magnification, is bilinear
526 * reconstruction followed by a sampling with a delta function.
529 bilinear_magnify_make_weights ( PixopsFilterDimension
*dim
,
532 double * pixel_weights
;
537 if ( scale
> 1.0 ) /* Linear */
540 dim
->offset
= 0.5 * ( 1 / scale
- 1 );
544 n
= ceil ( 1.0 + 1.0 / scale
);
549 dim
->weights
= g_new ( double, SUBSAMPLE
* n
);
551 pixel_weights
= dim
->weights
;
553 for ( offset
= 0; offset
< SUBSAMPLE
; offset
++ )
555 double x
= ( double ) offset
/ SUBSAMPLE
;
557 if ( scale
> 1.0 ) /* Linear */
559 for ( i
= 0; i
< n
; i
++ )
560 *( pixel_weights
++ ) = ( ( ( i
== 0 ) ?
( 1 - x
) : x
) / scale
) * scale
;
564 double a
= x
+ 1 / scale
;
567 * ---------|--.-|----|--.-|------- SRC
568 * ------------|---------|--------- DEST
570 for ( i
= 0; i
< n
; i
++ )
575 * ( pixel_weights
++ ) = ( MIN ( i
+ 1, a
) - x
) * scale
;
577 *( pixel_weights
++ ) = 0;
582 * ( pixel_weights
++ ) = ( MIN ( i
+ 1, a
) - i
) * scale
;
584 *( pixel_weights
++ ) = 0;
591 /* Computes the integral from b0 to b1 of
593 * f(x) = x; 0 <= x < 1
594 * f(x) = 0; otherwise
596 * We combine two of these to compute the convolution of
597 * a box filter with a triangular spike.
600 linear_box_half ( double b0
, double b1
)
629 return 0.5 * ( x1
* x1
- x0
* x0
);
632 /* Compute weights for reconstructing with bilinear
633 * interpolation, then sampling with a box filter
636 bilinear_box_make_weights ( PixopsFilterDimension
*dim
,
639 int n
= ceil ( 1 / scale
+ 2.0 );
640 double *pixel_weights
= g_new ( double, SUBSAMPLE
* n
);
646 dim
->weights
= pixel_weights
;
648 for ( offset
= 0 ; offset
< SUBSAMPLE
; offset
++ )
650 double x
= ( double ) offset
/ SUBSAMPLE
;
651 double a
= x
+ 1 / scale
;
653 for ( i
= 0; i
< n
; i
++ )
655 w
= linear_box_half ( 0.5 + i
- a
, 0.5 + i
- x
);
656 w
+= linear_box_half ( 1.5 + x
- i
, 1.5 + a
- i
);
658 *( pixel_weights
++ ) = w
* scale
;
665 make_weights ( PixopsFilter
*filter
,
666 PixopsInterpType interp_type
,
670 switch ( interp_type
)
672 case PIXOPS_INTERP_NEAREST
:
673 g_assert_not_reached ();
676 case PIXOPS_INTERP_TILES
:
677 tile_make_weights ( &filter
->x
, scale_x
);
678 tile_make_weights ( &filter
->y
, scale_y
);
681 case PIXOPS_INTERP_BILINEAR
:
682 bilinear_magnify_make_weights ( &filter
->x
, scale_x
);
683 bilinear_magnify_make_weights ( &filter
->y
, scale_y
);
686 case PIXOPS_INTERP_HYPER
:
687 bilinear_box_make_weights ( &filter
->x
, scale_x
);
688 bilinear_box_make_weights ( &filter
->y
, scale_y
);
695 yuv422_scale ( guchar
*dest_buf
,
702 gboolean dest_has_alpha
,
703 const guchar
*src_buf
,
708 gboolean src_has_alpha
,
711 PixopsInterpType interp_type
)
714 PixopsLineFunc line_func
;
717 gboolean found_mmx
= pixops_have_mmx();
720 //g_return_if_fail ( !( dest_channels == 3 && dest_has_alpha ) );
721 //g_return_if_fail ( !( src_channels == 3 && src_has_alpha ) );
722 //g_return_if_fail ( !( src_has_alpha && !dest_has_alpha ) );
724 if ( scale_x
== 0 || scale_y
== 0 )
727 if ( interp_type
== PIXOPS_INTERP_NEAREST
)
729 pixops_scale_nearest ( dest_buf
, render_x0
, render_y0
, render_x1
, render_y1
,
731 src_buf
, src_width
, src_height
, src_rowstride
,
736 filter
.overall_alpha
= 1.0;
737 make_weights ( &filter
, interp_type
, scale_x
, scale_y
);
739 if ( filter
.x
.n
== 2 && filter
.y
.n
== 2 )
742 if ( 0 && found_mmx
)
743 line_func
= scale_line_22_33_mmx_stub
;
747 line_func
= scale_line_22_33
;
750 line_func
= scale_line
;
752 pixops_process ( dest_buf
, render_x0
, render_y0
, render_x1
, render_y1
,
753 dest_rowstride
, dest_channels
, dest_has_alpha
,
754 src_buf
, src_width
, src_height
, src_rowstride
, src_channels
,
755 src_has_alpha
, scale_x
, scale_y
, 0, 0, 0, 0, 0,
756 &filter
, line_func
);
758 g_free ( filter
.x
.weights
);
759 g_free ( filter
.y
.weights
);