6 #include "pixops-internal.h"
8 #define SUBSAMPLE_BITS 4
9 #define SUBSAMPLE (1 << SUBSAMPLE_BITS)
10 #define SUBSAMPLE_MASK ((1 << SUBSAMPLE_BITS)-1)
11 #define SCALE_SHIFT 16
13 typedef struct _PixopsFilter PixopsFilter;
24 typedef guchar *(*PixopsLineFunc) (int *weights, int n_x, int n_y,
25 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
26 guchar **src, int src_channels, gboolean src_has_alpha,
27 int x_init, int x_step, int src_width,
28 int check_size, guint32 color1, guint32 color2);
30 typedef void (*PixopsPixelFunc) (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
31 int src_has_alpha, int check_size, guint32 color1,
33 int r, int g, int b, int a);
36 get_check_shift (int check_size)
39 g_return_val_if_fail (check_size >= 0, 4);
41 while (!(check_size & 1))
51 pixops_scale_nearest (guchar *dest_buf,
58 gboolean dest_has_alpha,
59 const guchar *src_buf,
64 gboolean src_has_alpha,
70 int x_step = (1 << SCALE_SHIFT) / scale_x;
71 int y_step = (1 << SCALE_SHIFT) / scale_y;
73 #define INNER_LOOP(SRC_CHANNELS,DEST_CHANNELS) \
74 for (j=0; j < (render_x1 - render_x0); j++) \
76 const guchar *p = src + (x >> SCALE_SHIFT) * SRC_CHANNELS; \
82 if (DEST_CHANNELS == 4) \
84 if (SRC_CHANNELS == 4) \
90 dest += DEST_CHANNELS; \
94 for (i = 0; i < (render_y1 - render_y0); i++)
96 const guchar *src = src_buf + (((i + render_y0) * y_step + y_step / 2) >> SCALE_SHIFT) * src_rowstride;
97 /* FIXME Owen needs to look at this */
98 guchar *dest = dest_buf + i * dest_rowstride;
100 x = render_x0 * x_step + x_step / 2;
102 if (src_channels == 3)
104 if (dest_channels == 3)
113 else if (src_channels == 4)
115 if (dest_channels == 3)
121 for (j=0; j < (render_x1 - render_x0); j++)
123 const guchar *p = src + (x >> SCALE_SHIFT) * 4;
126 p32 = (guint32 *) dest;
127 *p32 = *((guint32 *) p);
139 pixops_composite_nearest (guchar *dest_buf,
146 gboolean dest_has_alpha,
147 const guchar *src_buf,
152 gboolean src_has_alpha,
159 int x_step = (1 << SCALE_SHIFT) / scale_x;
160 int y_step = (1 << SCALE_SHIFT) / scale_y;
162 for (i = 0; i < (render_y1 - render_y0); i++)
164 const guchar *src = src_buf + (((i + render_y0) * y_step + y_step / 2) >> SCALE_SHIFT) * src_rowstride;
165 guchar *dest = dest_buf + i * dest_rowstride;
167 x = render_x0 * x_step + x_step / 2;
169 for (j=0; j < (render_x1 - render_x0); j++)
171 const guchar *p = src + (x >> SCALE_SHIFT) * src_channels;
175 a0 = (p[3] * overall_alpha) / 0xff;
181 unsigned int w0 = 0xff * a0;
182 unsigned int w1 = (0xff - a0) * dest[3];
183 unsigned int w = w0 + w1;
187 dest[0] = (w0 * p[0] + w1 * dest[0]) / w;
188 dest[1] = (w0 * p[1] + w1 * dest[1]) / w;
189 dest[2] = (w0 * p[2] + w1 * dest[2]) / w;
202 dest[0] = (a0 * p[0] + (0xff - a0) * dest[0]) / 0xff;
203 dest[1] = (a0 * p[1] + (0xff - a0) * dest[1]) / 0xff;
204 dest[2] = (a0 * p[2] + (0xff - a0) * dest[2]) / 0xff;
206 if (dest_channels == 4)
210 dest += dest_channels;
217 pixops_composite_color_nearest (guchar *dest_buf,
224 gboolean dest_has_alpha,
225 const guchar *src_buf,
230 gboolean src_has_alpha,
242 int x_step = (1 << SCALE_SHIFT) / scale_x;
243 int y_step = (1 << SCALE_SHIFT) / scale_y;
244 int r1, g1, b1, r2, g2, b2;
245 int check_shift = get_check_shift (check_size);
247 for (i = 0; i < (render_y1 - render_y0); i++)
249 const guchar *src = src_buf + (((i + render_y0) * y_step + y_step/2) >> SCALE_SHIFT) * src_rowstride;
250 guchar *dest = dest_buf + i * dest_rowstride;
252 x = render_x0 * x_step + x_step / 2;
254 if (((i + check_y) >> check_shift) & 1)
256 r1 = (color2 & 0xff0000) >> 16;
257 g1 = (color2 & 0xff00) >> 8;
260 r2 = (color1 & 0xff0000) >> 16;
261 g2 = (color1 & 0xff00) >> 8;
266 r1 = (color1 & 0xff0000) >> 16;
267 g1 = (color1 & 0xff00) >> 8;
270 r2 = (color2 & 0xff0000) >> 16;
271 g2 = (color2 & 0xff00) >> 8;
275 for (j=0 ; j < (render_x1 - render_x0); j++)
277 const guchar *p = src + (x >> SCALE_SHIFT) * src_channels;
282 a0 = (p[3] * overall_alpha + 0xff) >> 8;
293 if (((j + check_x) >> check_shift) & 1)
295 tmp = ((int) p[0] - r2) * a0;
296 dest[0] = r2 + ((tmp + (tmp >> 8) + 0x80) >> 8);
297 tmp = ((int) p[1] - g2) * a0;
298 dest[1] = g2 + ((tmp + (tmp >> 8) + 0x80) >> 8);
299 tmp = ((int) p[2] - b2) * a0;
300 dest[2] = b2 + ((tmp + (tmp >> 8) + 0x80) >> 8);
304 tmp = ((int) p[0] - r1) * a0;
305 dest[0] = r1 + ((tmp + (tmp >> 8) + 0x80) >> 8);
306 tmp = ((int) p[1] - g1) * a0;
307 dest[1] = g1 + ((tmp + (tmp >> 8) + 0x80) >> 8);
308 tmp = ((int) p[2] - b1) * a0;
309 dest[2] = b1 + ((tmp + (tmp >> 8) + 0x80) >> 8);
312 if (dest_channels == 4)
315 dest += dest_channels;
322 composite_pixel (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
323 int src_has_alpha, int check_size, guint32 color1, guint32 color2,
324 int r, int g, int b, int a)
328 unsigned int w0 = a - (a >> 8);
329 unsigned int w1 = ((0xff0000 - a) >> 8) * dest[3];
330 unsigned int w = w0 + w1;
334 dest[0] = (r - (r >> 8) + w1 * dest[0]) / w;
335 dest[1] = (g - (g >> 8) + w1 * dest[1]) / w;
336 dest[2] = (b - (b >> 8) + w1 * dest[2]) / w;
337 dest[3] = w / 0xff00;
349 dest[0] = (r + (0xff0000 - a) * dest[0]) / 0xff0000;
350 dest[1] = (g + (0xff0000 - a) * dest[1]) / 0xff0000;
351 dest[2] = (b + (0xff0000 - a) * dest[2]) / 0xff0000;
356 composite_line (int *weights, int n_x, int n_y,
357 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
358 guchar **src, int src_channels, gboolean src_has_alpha,
359 int x_init, int x_step, int src_width,
360 int check_size, guint32 color1, guint32 color2)
365 while (dest < dest_end)
367 int x_scaled = x >> SCALE_SHIFT;
368 unsigned int r = 0, g = 0, b = 0, a = 0;
371 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
373 for (i=0; i<n_y; i++)
375 guchar *q = src[i] + x_scaled * src_channels;
376 int *line_weights = pixel_weights + n_x * i;
378 for (j=0; j<n_x; j++)
383 ta = q[3] * line_weights[j];
385 ta = 0xff * line_weights[j];
398 unsigned int w0 = a - (a >> 8);
399 unsigned int w1 = ((0xff0000 - a) >> 8) * dest[3];
400 unsigned int w = w0 + w1;
404 dest[0] = (r - (r >> 8) + w1 * dest[0]) / w;
405 dest[1] = (g - (g >> 8) + w1 * dest[1]) / w;
406 dest[2] = (b - (b >> 8) + w1 * dest[2]) / w;
407 dest[3] = w / 0xff00;
419 dest[0] = (r + (0xff0000 - a) * dest[0]) / 0xff0000;
420 dest[1] = (g + (0xff0000 - a) * dest[1]) / 0xff0000;
421 dest[2] = (b + (0xff0000 - a) * dest[2]) / 0xff0000;
424 dest += dest_channels;
432 composite_line_22_4a4 (int *weights, int n_x, int n_y,
433 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
434 guchar **src, int src_channels, gboolean src_has_alpha,
435 int x_init, int x_step, int src_width,
436 int check_size, guint32 color1, guint32 color2)
439 guchar *src0 = src[0];
440 guchar *src1 = src[1];
442 g_return_val_if_fail (src_channels != 3, dest);
443 g_return_val_if_fail (src_has_alpha, dest);
445 while (dest < dest_end)
447 int x_scaled = x >> SCALE_SHIFT;
448 unsigned int r, g, b, a, ta;
453 q0 = src0 + x_scaled * 4;
454 q1 = src1 + x_scaled * 4;
456 pixel_weights = (int *)((char *)weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS - 4)) & (SUBSAMPLE_MASK << 4)));
458 w1 = pixel_weights[0];
459 w2 = pixel_weights[1];
460 w3 = pixel_weights[2];
461 w4 = pixel_weights[3];
486 dest[0] = ((0xff0000 - a) * dest[0] + r) >> 24;
487 dest[1] = ((0xff0000 - a) * dest[1] + g) >> 24;
488 dest[2] = ((0xff0000 - a) * dest[2] + b) >> 24;
500 composite_line_22_4a4_mmx_stub (int *weights, int n_x, int n_y,
501 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
502 guchar **src, int src_channels, gboolean src_has_alpha,
503 int x_init, int x_step, int src_width,
504 int check_size, guint32 color1, guint32 color2)
506 guint32 mmx_weights[16][8];
511 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
512 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
513 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
514 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
515 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
516 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
517 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
518 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
521 return pixops_composite_line_22_4a4_mmx (mmx_weights, dest, src[0], src[1], x_step, dest_end, x_init);
526 composite_pixel_color (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
527 int src_has_alpha, int check_size, guint32 color1, guint32 color2,
528 int r, int g, int b, int a)
530 int dest_r, dest_g, dest_b;
531 int check_shift = get_check_shift (check_size);
533 if ((dest_x >> check_shift) & 1)
535 dest_r = (color2 & 0xff0000) >> 16;
536 dest_g = (color2 & 0xff00) >> 8;
537 dest_b = color2 & 0xff;
541 dest_r = (color1 & 0xff0000) >> 16;
542 dest_g = (color1 & 0xff00) >> 8;
543 dest_b = color1 & 0xff;
546 dest[0] = ((0xff0000 - a) * dest_r + r) >> 24;
547 dest[1] = ((0xff0000 - a) * dest_g + g) >> 24;
548 dest[2] = ((0xff0000 - a) * dest_b + b) >> 24;
552 else if (dest_channels == 4)
557 composite_line_color (int *weights, int n_x, int n_y,
558 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
559 guchar **src, int src_channels, gboolean src_has_alpha,
560 int x_init, int x_step, int src_width,
561 int check_size, guint32 color1, guint32 color2)
565 int check_shift = get_check_shift (check_size);
566 int dest_r1, dest_g1, dest_b1;
567 int dest_r2, dest_g2, dest_b2;
569 g_return_val_if_fail (check_size != 0, dest);
571 dest_r1 = (color1 & 0xff0000) >> 16;
572 dest_g1 = (color1 & 0xff00) >> 8;
573 dest_b1 = color1 & 0xff;
575 dest_r2 = (color2 & 0xff0000) >> 16;
576 dest_g2 = (color2 & 0xff00) >> 8;
577 dest_b2 = color2 & 0xff;
579 while (dest < dest_end)
581 int x_scaled = x >> SCALE_SHIFT;
582 unsigned int r = 0, g = 0, b = 0, a = 0;
585 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
587 for (i=0; i<n_y; i++)
589 guchar *q = src[i] + x_scaled * src_channels;
590 int *line_weights = pixel_weights + n_x * i;
592 for (j=0; j<n_x; j++)
597 ta = q[3] * line_weights[j];
599 ta = 0xff * line_weights[j];
610 if ((dest_x >> check_shift) & 1)
612 dest[0] = ((0xff0000 - a) * dest_r2 + r) >> 24;
613 dest[1] = ((0xff0000 - a) * dest_g2 + g) >> 24;
614 dest[2] = ((0xff0000 - a) * dest_b2 + b) >> 24;
618 dest[0] = ((0xff0000 - a) * dest_r1 + r) >> 24;
619 dest[1] = ((0xff0000 - a) * dest_g1 + g) >> 24;
620 dest[2] = ((0xff0000 - a) * dest_b1 + b) >> 24;
625 else if (dest_channels == 4)
628 dest += dest_channels;
638 composite_line_color_22_4a4_mmx_stub (int *weights, int n_x, int n_y,
639 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
640 guchar **src, int src_channels, gboolean src_has_alpha,
641 int x_init, int x_step, int src_width,
642 int check_size, guint32 color1, guint32 color2)
644 guint32 mmx_weights[16][8];
645 int check_shift = get_check_shift (check_size);
651 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
652 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
653 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
654 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
655 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
656 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
657 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
658 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
661 colors[0] = (color1 & 0xff00) << 8 | (color1 & 0xff);
662 colors[1] = (color1 & 0xff0000) >> 16;
663 colors[2] = (color2 & 0xff00) << 8 | (color2 & 0xff);
664 colors[3] = (color2 & 0xff0000) >> 16;
666 return pixops_composite_line_color_22_4a4_mmx (mmx_weights, dest, src[0], src[1], x_step, dest_end, x_init,
667 dest_x, check_shift, colors);
672 scale_pixel (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
673 int src_has_alpha, int check_size, guint32 color1, guint32 color2,
674 int r, int g, int b, int a)
695 dest[0] = (r + 0xffffff) >> 24;
696 dest[1] = (g + 0xffffff) >> 24;
697 dest[2] = (b + 0xffffff) >> 24;
705 scale_line (int *weights, int n_x, int n_y,
706 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
707 guchar **src, int src_channels, gboolean src_has_alpha,
708 int x_init, int x_step, int src_width,
709 int check_size, guint32 color1, guint32 color2)
714 while (dest < dest_end)
716 int x_scaled = x >> SCALE_SHIFT;
719 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
723 unsigned int r = 0, g = 0, b = 0, a = 0;
724 for (i=0; i<n_y; i++)
726 guchar *q = src[i] + x_scaled * src_channels;
727 int *line_weights = pixel_weights + n_x * i;
729 for (j=0; j<n_x; j++)
733 ta = q[3] * line_weights[j];
760 unsigned int r = 0, g = 0, b = 0;
761 for (i=0; i<n_y; i++)
763 guchar *q = src[i] + x_scaled * src_channels;
764 int *line_weights = pixel_weights + n_x * i;
766 for (j=0; j<n_x; j++)
768 unsigned int ta = line_weights[j];
778 dest[0] = (r + 0xffff) >> 16;
779 dest[1] = (g + 0xffff) >> 16;
780 dest[2] = (b + 0xffff) >> 16;
786 dest += dest_channels;
796 scale_line_22_33_mmx_stub (int *weights, int n_x, int n_y,
797 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
798 guchar **src, int src_channels, gboolean src_has_alpha,
799 int x_init, int x_step, int src_width,
800 int check_size, guint32 color1, guint32 color2)
802 guint32 mmx_weights[16][8];
807 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
808 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
809 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
810 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
811 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
812 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
813 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
814 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
817 return pixops_scale_line_22_33_mmx (mmx_weights, dest, src[0], src[1], x_step, dest_end, x_init);
821 #ifdef SCALE_LINE_22_33_USED /* This dead code would need changes if we wanted to use it */
823 scale_line_22_33 (int *weights, int n_x, int n_y,
824 guchar *dest, guchar *dest_end, int dest_channels, int dest_has_alpha,
825 guchar **src, int src_channels, gboolean src_has_alpha,
826 int x_init, int x_step, int src_width,
827 int check_size, guint32 color1, guint32 color2)
830 guchar *src0 = src[0];
831 guchar *src1 = src[1];
833 while (dest < dest_end)
835 unsigned int r, g, b;
836 int x_scaled = x >> SCALE_SHIFT;
841 q0 = src0 + x_scaled * 3;
842 q1 = src1 + x_scaled * 3;
844 pixel_weights = (int *)((char *)weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS - 4)) & (SUBSAMPLE_MASK << 4)));
846 w1 = pixel_weights[0];
847 w2 = pixel_weights[1];
848 w3 = pixel_weights[2];
849 w4 = pixel_weights[3];
878 #endif /* SCALE_LINE_22_33_USED */
881 process_pixel (int *weights, int n_x, int n_y,
882 guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
883 guchar **src, int src_channels, gboolean src_has_alpha,
884 int x_start, int src_width,
885 int check_size, guint32 color1, guint32 color2,
886 PixopsPixelFunc pixel_func)
888 unsigned int r = 0, g = 0, b = 0, a = 0;
891 for (i=0; i<n_y; i++)
893 int *line_weights = weights + n_x * i;
895 for (j=0; j<n_x; j++)
902 else if (x_start + j < src_width)
903 q = src[i] + (x_start + j) * src_channels;
905 q = src[i] + (src_width - 1) * src_channels;
908 ta = q[3] * line_weights[j];
910 ta = 0xff * line_weights[j];
919 (*pixel_func) (dest, dest_x, dest_channels, dest_has_alpha, src_has_alpha, check_size, color1, color2, r, g, b, a);
923 pixops_process (guchar *dest_buf,
930 gboolean dest_has_alpha,
931 const guchar *src_buf,
936 gboolean src_has_alpha,
944 PixopsFilter *filter,
945 PixopsLineFunc line_func,
946 PixopsPixelFunc pixel_func)
950 guchar **line_bufs = g_new (guchar *, filter->n_y);
952 int x_step = (1 << SCALE_SHIFT) / scale_x;
953 int y_step = (1 << SCALE_SHIFT) / scale_y;
956 int scaled_x_offset = floor (filter->x_offset * (1 << SCALE_SHIFT));
958 int run_end_index = (((src_width - filter->n_x + 1) << SCALE_SHIFT) - scaled_x_offset - 1) / x_step + 1 - render_x0;
959 int check_shift = check_size ? get_check_shift (check_size) : 0;
961 y = render_y0 * y_step + floor (filter->y_offset * (1 << SCALE_SHIFT));
962 for (i = 0; i < (render_y1 - render_y0); i++)
964 int y_start = y >> SCALE_SHIFT;
966 int *run_weights = filter->weights + ((y >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * filter->n_x * filter->n_y * SUBSAMPLE;
968 guint32 tcolor1, tcolor2;
970 guchar *outbuf = dest_buf + dest_rowstride * i;
971 guchar *outbuf_end = outbuf + dest_channels * (render_x1 - render_x0);
973 if (((i + check_y) >> check_shift) & 1)
984 for (j=0; j<filter->n_y; j++)
987 line_bufs[j] = (guchar *)src_buf;
988 else if (y_start < src_height)
989 line_bufs[j] = (guchar *)src_buf + src_rowstride * y_start;
991 line_bufs[j] = (guchar *)src_buf + src_rowstride * (src_height - 1);
997 x = render_x0 * x_step + scaled_x_offset;
998 x_start = x >> SCALE_SHIFT;
1000 while (x_start < 0 && outbuf < outbuf_end)
1002 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->n_x * filter->n_y), filter->n_x, filter->n_y,
1003 outbuf, dest_x, dest_channels, dest_has_alpha,
1004 line_bufs, src_channels, src_has_alpha,
1005 x >> SCALE_SHIFT, src_width,
1006 check_size, tcolor1, tcolor2, pixel_func);
1009 x_start = x >> SCALE_SHIFT;
1011 outbuf += dest_channels;
1014 new_outbuf = (*line_func)(run_weights, filter->n_x, filter->n_y,
1016 MIN (outbuf_end, dest_buf + dest_rowstride * i + run_end_index * dest_channels),
1017 dest_channels, dest_has_alpha,
1018 line_bufs, src_channels, src_has_alpha,
1019 x, x_step, src_width, check_size, tcolor1, tcolor2);
1021 dest_x += (new_outbuf - outbuf) / dest_channels;
1023 x = (dest_x - check_x + render_x0) * x_step + scaled_x_offset;
1024 outbuf = new_outbuf;
1026 while (outbuf < outbuf_end)
1028 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->n_x * filter->n_y), filter->n_x, filter->n_y,
1029 outbuf, dest_x, dest_channels, dest_has_alpha,
1030 line_bufs, src_channels, src_has_alpha,
1031 x >> SCALE_SHIFT, src_width,
1032 check_size, tcolor1, tcolor2, pixel_func);
1036 outbuf += dest_channels;
1046 tile_make_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1048 int i_offset, j_offset;
1050 int n_x = ceil(1/x_scale + 1);
1051 int n_y = ceil(1/y_scale + 1);
1053 filter->x_offset = 0;
1054 filter->y_offset = 0;
1057 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1059 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1060 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1062 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1063 double x = (double)j_offset / 16;
1064 double y = (double)i_offset / 16;
1067 for (i = 0; i < n_y; i++)
1074 th = MIN(i+1, y + 1/y_scale) - y;
1080 if (y + 1/y_scale > i)
1081 th = MIN(i+1, y + 1/y_scale) - i;
1086 for (j = 0; j < n_x; j++)
1091 tw = MIN(j+1, x + 1/x_scale) - x;
1097 if (x + 1/x_scale > j)
1098 tw = MIN(j+1, x + 1/x_scale) - j;
1103 *(pixel_weights + n_x * i + j) = 65536 * tw * x_scale * th * y_scale * overall_alpha;
1110 bilinear_make_fast_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1112 int i_offset, j_offset;
1113 double *x_weights, *y_weights;
1116 if (x_scale > 1.0) /* Bilinear */
1119 filter->x_offset = 0.5 * (1/x_scale - 1);
1123 n_x = ceil(1.0 + 1.0/x_scale);
1124 filter->x_offset = 0.0;
1127 if (y_scale > 1.0) /* Bilinear */
1130 filter->y_offset = 0.5 * (1/y_scale - 1);
1134 n_y = ceil(1.0 + 1.0/y_scale);
1135 filter->y_offset = 0.0;
1140 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1142 x_weights = g_new (double, n_x);
1143 y_weights = g_new (double, n_y);
1145 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1146 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1148 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1149 double x = (double)j_offset / 16;
1150 double y = (double)i_offset / 16;
1153 if (x_scale > 1.0) /* Bilinear */
1155 for (i = 0; i < n_x; i++)
1157 x_weights[i] = ((i == 0) ? (1 - x) : x) / x_scale;
1162 for (i = 0; i < n_x; i++)
1167 x_weights[i] = MIN(i+1, x + 1/x_scale) - x;
1173 if (x + 1/x_scale > i)
1174 x_weights[i] = MIN(i+1, x + 1/x_scale) - i;
1181 if (y_scale > 1.0) /* Bilinear */
1183 for (i = 0; i < n_y; i++)
1185 y_weights[i] = ((i == 0) ? (1 - y) : y) / y_scale;
1190 for (i = 0; i < n_y; i++)
1195 y_weights[i] = MIN(i+1, y + 1/y_scale) - y;
1201 if (y + 1/y_scale > i)
1202 y_weights[i] = MIN(i+1, y + 1/y_scale) - i;
1209 for (i = 0; i < n_y; i++)
1210 for (j = 0; j < n_x; j++)
1211 *(pixel_weights + n_x * i + j) = 65536 * x_weights[j] * x_scale * y_weights[i] * y_scale * overall_alpha;
1219 bilinear_quadrant (double bx0, double bx1, double by0, double by1)
1221 double ax0, ax1, ay0, ay1;
1222 double x0, x1, y0, y1;
1234 x1 = MIN (ax1, bx1);
1244 x1 = MIN (ax1, bx1);
1255 y1 = MIN (ay1, by1);
1265 y1 = MIN (ay1, by1);
1271 return 0.25 * (x1*x1 - x0*x0) * (y1*y1 - y0*y0);
1275 bilinear_make_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1277 int i_offset, j_offset;
1279 int n_x = ceil(1/x_scale + 2.0);
1280 int n_y = ceil(1/y_scale + 2.0);
1282 filter->x_offset = -1.0;
1283 filter->y_offset = -1.0;
1287 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1289 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1290 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1292 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1293 double x = (double)j_offset / 16;
1294 double y = (double)i_offset / 16;
1297 for (i = 0; i < n_y; i++)
1298 for (j = 0; j < n_x; j++)
1302 w = bilinear_quadrant (0.5 + j - (x + 1 / x_scale), 0.5 + j - x, 0.5 + i - (y + 1 / y_scale), 0.5 + i - y);
1303 w += bilinear_quadrant (1.5 + x - j, 1.5 + (x + 1 / x_scale) - j, 0.5 + i - (y + 1 / y_scale), 0.5 + i - y);
1304 w += bilinear_quadrant (0.5 + j - (x + 1 / x_scale), 0.5 + j - x, 1.5 + y - i, 1.5 + (y + 1 / y_scale) - i);
1305 w += bilinear_quadrant (1.5 + x - j, 1.5 + (x + 1 / x_scale) - j, 1.5 + y - i, 1.5 + (y + 1 / y_scale) - i);
1307 *(pixel_weights + n_x * i + j) = 65536 * w * x_scale * y_scale * overall_alpha;
1313 pixops_composite_color (guchar *dest_buf,
1320 gboolean dest_has_alpha,
1321 const guchar *src_buf,
1326 gboolean src_has_alpha,
1329 GdkInterpType interp_type,
1337 PixopsFilter filter;
1338 PixopsLineFunc line_func;
1341 gboolean found_mmx = pixops_have_mmx();
1344 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1345 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1347 if (scale_x == 0 || scale_y == 0)
1350 if (!src_has_alpha && overall_alpha == 255)
1351 pixops_scale (dest_buf, render_x0, render_y0, render_x1, render_y1,
1352 dest_rowstride, dest_channels, dest_has_alpha,
1353 src_buf, src_width, src_height, src_rowstride, src_channels,
1354 src_has_alpha, scale_x, scale_y, interp_type);
1356 switch (interp_type)
1358 case GDK_INTERP_NEAREST:
1359 pixops_composite_color_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1360 dest_rowstride, dest_channels, dest_has_alpha,
1361 src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
1362 scale_x, scale_y, overall_alpha,
1363 check_x, check_y, check_size, color1, color2);
1366 case GDK_INTERP_TILES:
1367 tile_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1370 case GDK_INTERP_BILINEAR:
1371 bilinear_make_fast_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1374 case GDK_INTERP_HYPER:
1375 bilinear_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1380 if (filter.n_x == 2 && filter.n_y == 2 &&
1381 dest_channels == 4 && src_channels == 4 && src_has_alpha && !dest_has_alpha && found_mmx)
1382 line_func = composite_line_color_22_4a4_mmx_stub;
1385 line_func = composite_line_color;
1387 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1388 dest_rowstride, dest_channels, dest_has_alpha,
1389 src_buf, src_width, src_height, src_rowstride, src_channels,
1390 src_has_alpha, scale_x, scale_y, check_x, check_y, check_size, color1, color2,
1391 &filter, line_func, composite_pixel_color);
1393 g_free (filter.weights);
1397 pixops_composite (guchar *dest_buf,
1404 gboolean dest_has_alpha,
1405 const guchar *src_buf,
1410 gboolean src_has_alpha,
1413 GdkInterpType interp_type,
1416 PixopsFilter filter;
1417 PixopsLineFunc line_func;
1420 gboolean found_mmx = pixops_have_mmx();
1423 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1424 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1426 if (scale_x == 0 || scale_y == 0)
1429 if (!src_has_alpha && overall_alpha == 255)
1430 pixops_scale (dest_buf, render_x0, render_y0, render_x1, render_y1,
1431 dest_rowstride, dest_channels, dest_has_alpha,
1432 src_buf, src_width, src_height, src_rowstride, src_channels,
1433 src_has_alpha, scale_x, scale_y, interp_type);
1435 switch (interp_type)
1437 case GDK_INTERP_NEAREST:
1438 pixops_composite_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1439 dest_rowstride, dest_channels, dest_has_alpha,
1440 src_buf, src_width, src_height, src_rowstride, src_channels,
1441 src_has_alpha, scale_x, scale_y, overall_alpha);
1444 case GDK_INTERP_TILES:
1445 tile_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1448 case GDK_INTERP_BILINEAR:
1449 bilinear_make_fast_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1452 case GDK_INTERP_HYPER:
1453 bilinear_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1457 if (filter.n_x == 2 && filter.n_y == 2 &&
1458 dest_channels == 4 && src_channels == 4 && src_has_alpha && !dest_has_alpha)
1462 line_func = composite_line_22_4a4_mmx_stub;
1465 line_func = composite_line_22_4a4;
1468 line_func = composite_line;
1470 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1471 dest_rowstride, dest_channels, dest_has_alpha,
1472 src_buf, src_width, src_height, src_rowstride, src_channels,
1473 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
1474 &filter, line_func, composite_pixel);
1476 g_free (filter.weights);
1480 pixops_scale (guchar *dest_buf,
1487 gboolean dest_has_alpha,
1488 const guchar *src_buf,
1493 gboolean src_has_alpha,
1496 GdkInterpType interp_type)
1498 PixopsFilter filter;
1499 PixopsLineFunc line_func;
1502 gboolean found_mmx = pixops_have_mmx();
1505 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1506 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1507 g_return_if_fail (!(src_has_alpha && !dest_has_alpha));
1509 if (scale_x == 0 || scale_y == 0)
1512 switch (interp_type)
1514 case GDK_INTERP_NEAREST:
1515 pixops_scale_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1516 dest_rowstride, dest_channels, dest_has_alpha,
1517 src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
1521 case GDK_INTERP_TILES:
1522 tile_make_weights (&filter, scale_x, scale_y, 1.0);
1525 case GDK_INTERP_BILINEAR:
1526 bilinear_make_fast_weights (&filter, scale_x, scale_y, 1.0);
1529 case GDK_INTERP_HYPER:
1530 bilinear_make_weights (&filter, scale_x, scale_y, 1.0);
1535 if (filter.n_x == 2 && filter.n_y == 2 &&
1536 found_mmx && dest_channels == 3 && src_channels == 3)
1537 line_func = scale_line_22_33_mmx_stub;
1540 line_func = scale_line;
1542 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1543 dest_rowstride, dest_channels, dest_has_alpha,
1544 src_buf, src_width, src_height, src_rowstride, src_channels,
1545 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
1546 &filter, line_func, scale_pixel);
1548 g_free (filter.weights);