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);
822 scale_line_22_33 (int *weights, int n_x, int n_y,
823 guchar *dest, guchar *dest_end, int dest_channels, int dest_has_alpha,
824 guchar **src, int src_channels, gboolean src_has_alpha,
825 int x_init, int x_step, int src_width,
826 int check_size, guint32 color1, guint32 color2)
829 guchar *src0 = src[0];
830 guchar *src1 = src[1];
832 while (dest < dest_end)
834 unsigned int r, g, b;
835 int x_scaled = x >> SCALE_SHIFT;
840 q0 = src0 + x_scaled * 3;
841 q1 = src1 + x_scaled * 3;
843 pixel_weights = (int *)((char *)weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS - 4)) & (SUBSAMPLE_MASK << 4)));
845 w1 = pixel_weights[0];
846 w2 = pixel_weights[1];
847 w3 = pixel_weights[2];
848 w4 = pixel_weights[3];
879 process_pixel (int *weights, int n_x, int n_y,
880 guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
881 guchar **src, int src_channels, gboolean src_has_alpha,
882 int x_start, int src_width,
883 int check_size, guint32 color1, guint32 color2,
884 PixopsPixelFunc pixel_func)
886 unsigned int r = 0, g = 0, b = 0, a = 0;
889 for (i=0; i<n_y; i++)
891 int *line_weights = weights + n_x * i;
893 for (j=0; j<n_x; j++)
900 else if (x_start + j < src_width)
901 q = src[i] + (x_start + j) * src_channels;
903 q = src[i] + (src_width - 1) * src_channels;
906 ta = q[3] * line_weights[j];
908 ta = 0xff * line_weights[j];
917 (*pixel_func) (dest, dest_x, dest_channels, dest_has_alpha, src_has_alpha, check_size, color1, color2, r, g, b, a);
921 pixops_process (guchar *dest_buf,
928 gboolean dest_has_alpha,
929 const guchar *src_buf,
934 gboolean src_has_alpha,
942 PixopsFilter *filter,
943 PixopsLineFunc line_func,
944 PixopsPixelFunc pixel_func)
948 guchar **line_bufs = g_new (guchar *, filter->n_y);
950 int x_step = (1 << SCALE_SHIFT) / scale_x;
951 int y_step = (1 << SCALE_SHIFT) / scale_y;
954 int scaled_x_offset = floor (filter->x_offset * (1 << SCALE_SHIFT));
956 int run_end_index = (((src_width - filter->n_x + 1) << SCALE_SHIFT) - scaled_x_offset - 1) / x_step + 1 - render_x0;
957 int check_shift = check_size ? get_check_shift (check_size) : 0;
959 y = render_y0 * y_step + floor (filter->y_offset * (1 << SCALE_SHIFT));
960 for (i = 0; i < (render_y1 - render_y0); i++)
962 int y_start = y >> SCALE_SHIFT;
964 int *run_weights = filter->weights + ((y >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * filter->n_x * filter->n_y * SUBSAMPLE;
966 guint32 tcolor1, tcolor2;
968 guchar *outbuf = dest_buf + dest_rowstride * i;
969 guchar *outbuf_end = outbuf + dest_channels * (render_x1 - render_x0);
971 if (((i + check_y) >> check_shift) & 1)
982 for (j=0; j<filter->n_y; j++)
985 line_bufs[j] = (guchar *)src_buf;
986 else if (y_start < src_height)
987 line_bufs[j] = (guchar *)src_buf + src_rowstride * y_start;
989 line_bufs[j] = (guchar *)src_buf + src_rowstride * (src_height - 1);
995 x = render_x0 * x_step + scaled_x_offset;
996 x_start = x >> SCALE_SHIFT;
998 while (x_start < 0 && outbuf < outbuf_end)
1000 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->n_x * filter->n_y), filter->n_x, filter->n_y,
1001 outbuf, dest_x, dest_channels, dest_has_alpha,
1002 line_bufs, src_channels, src_has_alpha,
1003 x >> SCALE_SHIFT, src_width,
1004 check_size, tcolor1, tcolor2, pixel_func);
1007 x_start = x >> SCALE_SHIFT;
1009 outbuf += dest_channels;
1012 new_outbuf = (*line_func)(run_weights, filter->n_x, filter->n_y,
1014 MIN (outbuf_end, dest_buf + dest_rowstride * i + run_end_index * dest_channels),
1015 dest_channels, dest_has_alpha,
1016 line_bufs, src_channels, src_has_alpha,
1017 x, x_step, src_width, check_size, tcolor1, tcolor2);
1019 dest_x += (new_outbuf - outbuf) / dest_channels;
1021 x = (dest_x - check_x + render_x0) * x_step + scaled_x_offset;
1022 outbuf = new_outbuf;
1024 while (outbuf < outbuf_end)
1026 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->n_x * filter->n_y), filter->n_x, filter->n_y,
1027 outbuf, dest_x, dest_channels, dest_has_alpha,
1028 line_bufs, src_channels, src_has_alpha,
1029 x >> SCALE_SHIFT, src_width,
1030 check_size, tcolor1, tcolor2, pixel_func);
1034 outbuf += dest_channels;
1044 tile_make_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1046 int i_offset, j_offset;
1048 int n_x = ceil(1/x_scale + 1);
1049 int n_y = ceil(1/y_scale + 1);
1051 filter->x_offset = 0;
1052 filter->y_offset = 0;
1055 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1057 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1058 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1060 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1061 double x = (double)j_offset / 16;
1062 double y = (double)i_offset / 16;
1065 for (i = 0; i < n_y; i++)
1072 th = MIN(i+1, y + 1/y_scale) - y;
1078 if (y + 1/y_scale > i)
1079 th = MIN(i+1, y + 1/y_scale) - i;
1084 for (j = 0; j < n_x; j++)
1089 tw = MIN(j+1, x + 1/x_scale) - x;
1095 if (x + 1/x_scale > j)
1096 tw = MIN(j+1, x + 1/x_scale) - j;
1101 *(pixel_weights + n_x * i + j) = 65536 * tw * x_scale * th * y_scale * overall_alpha;
1108 bilinear_make_fast_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1110 int i_offset, j_offset;
1111 double *x_weights, *y_weights;
1114 if (x_scale > 1.0) /* Bilinear */
1117 filter->x_offset = 0.5 * (1/x_scale - 1);
1121 n_x = ceil(1.0 + 1.0/x_scale);
1122 filter->x_offset = 0.0;
1125 if (y_scale > 1.0) /* Bilinear */
1128 filter->y_offset = 0.5 * (1/y_scale - 1);
1132 n_y = ceil(1.0 + 1.0/y_scale);
1133 filter->y_offset = 0.0;
1138 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1140 x_weights = g_new (double, n_x);
1141 y_weights = g_new (double, n_y);
1143 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1144 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1146 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1147 double x = (double)j_offset / 16;
1148 double y = (double)i_offset / 16;
1151 if (x_scale > 1.0) /* Bilinear */
1153 for (i = 0; i < n_x; i++)
1155 x_weights[i] = ((i == 0) ? (1 - x) : x) / x_scale;
1160 for (i = 0; i < n_x; i++)
1165 x_weights[i] = MIN(i+1, x + 1/x_scale) - x;
1171 if (x + 1/x_scale > i)
1172 x_weights[i] = MIN(i+1, x + 1/x_scale) - i;
1179 if (y_scale > 1.0) /* Bilinear */
1181 for (i = 0; i < n_y; i++)
1183 y_weights[i] = ((i == 0) ? (1 - y) : y) / y_scale;
1188 for (i = 0; i < n_y; i++)
1193 y_weights[i] = MIN(i+1, y + 1/y_scale) - y;
1199 if (y + 1/y_scale > i)
1200 y_weights[i] = MIN(i+1, y + 1/y_scale) - i;
1207 for (i = 0; i < n_y; i++)
1208 for (j = 0; j < n_x; j++)
1209 *(pixel_weights + n_x * i + j) = 65536 * x_weights[j] * x_scale * y_weights[i] * y_scale * overall_alpha;
1217 bilinear_quadrant (double bx0, double bx1, double by0, double by1)
1219 double ax0, ax1, ay0, ay1;
1220 double x0, x1, y0, y1;
1232 x1 = MIN (ax1, bx1);
1242 x1 = MIN (ax1, bx1);
1253 y1 = MIN (ay1, by1);
1263 y1 = MIN (ay1, by1);
1269 return 0.25 * (x1*x1 - x0*x0) * (y1*y1 - y0*y0);
1273 bilinear_make_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1275 int i_offset, j_offset;
1277 int n_x = ceil(1/x_scale + 2.0);
1278 int n_y = ceil(1/y_scale + 2.0);
1280 filter->x_offset = -1.0;
1281 filter->y_offset = -1.0;
1285 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1287 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1288 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1290 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1291 double x = (double)j_offset / 16;
1292 double y = (double)i_offset / 16;
1295 for (i = 0; i < n_y; i++)
1296 for (j = 0; j < n_x; j++)
1300 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);
1301 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);
1302 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);
1303 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);
1305 *(pixel_weights + n_x * i + j) = 65536 * w * x_scale * y_scale * overall_alpha;
1311 pixops_composite_color (guchar *dest_buf,
1318 gboolean dest_has_alpha,
1319 const guchar *src_buf,
1324 gboolean src_has_alpha,
1327 GdkInterpType interp_type,
1335 PixopsFilter filter;
1336 PixopsLineFunc line_func;
1339 gboolean found_mmx = pixops_have_mmx();
1342 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1343 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1345 if (scale_x == 0 || scale_y == 0)
1348 if (!src_has_alpha && overall_alpha == 255)
1349 pixops_scale (dest_buf, render_x0, render_y0, render_x1, render_y1,
1350 dest_rowstride, dest_channels, dest_has_alpha,
1351 src_buf, src_width, src_height, src_rowstride, src_channels,
1352 src_has_alpha, scale_x, scale_y, interp_type);
1354 switch (interp_type)
1356 case GDK_INTERP_NEAREST:
1357 pixops_composite_color_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1358 dest_rowstride, dest_channels, dest_has_alpha,
1359 src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
1360 scale_x, scale_y, overall_alpha,
1361 check_x, check_y, check_size, color1, color2);
1364 case GDK_INTERP_TILES:
1365 tile_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1368 case GDK_INTERP_BILINEAR:
1369 bilinear_make_fast_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1372 case GDK_INTERP_HYPER:
1373 bilinear_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1378 if (filter.n_x == 2 && filter.n_y == 2 &&
1379 dest_channels == 4 && src_channels == 4 && src_has_alpha && !dest_has_alpha && found_mmx)
1380 line_func = composite_line_color_22_4a4_mmx_stub;
1383 line_func = composite_line_color;
1385 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1386 dest_rowstride, dest_channels, dest_has_alpha,
1387 src_buf, src_width, src_height, src_rowstride, src_channels,
1388 src_has_alpha, scale_x, scale_y, check_x, check_y, check_size, color1, color2,
1389 &filter, line_func, composite_pixel_color);
1391 g_free (filter.weights);
1395 pixops_composite (guchar *dest_buf,
1402 gboolean dest_has_alpha,
1403 const guchar *src_buf,
1408 gboolean src_has_alpha,
1411 GdkInterpType interp_type,
1414 PixopsFilter filter;
1415 PixopsLineFunc line_func;
1418 gboolean found_mmx = pixops_have_mmx();
1421 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1422 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1424 if (scale_x == 0 || scale_y == 0)
1427 if (!src_has_alpha && overall_alpha == 255)
1428 pixops_scale (dest_buf, render_x0, render_y0, render_x1, render_y1,
1429 dest_rowstride, dest_channels, dest_has_alpha,
1430 src_buf, src_width, src_height, src_rowstride, src_channels,
1431 src_has_alpha, scale_x, scale_y, interp_type);
1433 switch (interp_type)
1435 case GDK_INTERP_NEAREST:
1436 pixops_composite_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1437 dest_rowstride, dest_channels, dest_has_alpha,
1438 src_buf, src_width, src_height, src_rowstride, src_channels,
1439 src_has_alpha, scale_x, scale_y, overall_alpha);
1442 case GDK_INTERP_TILES:
1443 tile_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1446 case GDK_INTERP_BILINEAR:
1447 bilinear_make_fast_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1450 case GDK_INTERP_HYPER:
1451 bilinear_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1455 if (filter.n_x == 2 && filter.n_y == 2 &&
1456 dest_channels == 4 && src_channels == 4 && src_has_alpha && !dest_has_alpha)
1460 line_func = composite_line_22_4a4_mmx_stub;
1463 line_func = composite_line_22_4a4;
1466 line_func = composite_line;
1468 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1469 dest_rowstride, dest_channels, dest_has_alpha,
1470 src_buf, src_width, src_height, src_rowstride, src_channels,
1471 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
1472 &filter, line_func, composite_pixel);
1474 g_free (filter.weights);
1478 pixops_scale (guchar *dest_buf,
1485 gboolean dest_has_alpha,
1486 const guchar *src_buf,
1491 gboolean src_has_alpha,
1494 GdkInterpType interp_type)
1496 PixopsFilter filter;
1497 PixopsLineFunc line_func;
1500 gboolean found_mmx = pixops_have_mmx();
1503 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1504 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1505 g_return_if_fail (!(src_has_alpha && !dest_has_alpha));
1507 if (scale_x == 0 || scale_y == 0)
1510 switch (interp_type)
1512 case GDK_INTERP_NEAREST:
1513 pixops_scale_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1514 dest_rowstride, dest_channels, dest_has_alpha,
1515 src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
1519 case GDK_INTERP_TILES:
1520 tile_make_weights (&filter, scale_x, scale_y, 1.0);
1523 case GDK_INTERP_BILINEAR:
1524 bilinear_make_fast_weights (&filter, scale_x, scale_y, 1.0);
1527 case GDK_INTERP_HYPER:
1528 bilinear_make_weights (&filter, scale_x, scale_y, 1.0);
1533 if (filter.n_x == 2 && filter.n_y == 2 &&
1534 found_mmx && dest_channels == 3 && src_channels == 3)
1535 line_func = scale_line_22_33_mmx_stub;
1538 line_func = scale_line;
1540 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1541 dest_rowstride, dest_channels, dest_has_alpha,
1542 src_buf, src_width, src_height, src_rowstride, src_channels,
1543 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
1544 &filter, line_func, scale_pixel);
1546 g_free (filter.weights);