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 guint r, guint g, guint b, guint 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 guchar *dest = dest_buf + i * dest_rowstride;
99 x = render_x0 * x_step + x_step / 2;
101 if (src_channels == 3)
103 if (dest_channels == 3)
112 else if (src_channels == 4)
114 if (dest_channels == 3)
120 for (j=0; j < (render_x1 - render_x0); j++)
122 const guchar *p = src + (x >> SCALE_SHIFT) * 4;
125 p32 = (guint32 *) dest;
126 *p32 = *((guint32 *) p);
138 pixops_composite_nearest (guchar *dest_buf,
145 gboolean dest_has_alpha,
146 const guchar *src_buf,
151 gboolean src_has_alpha,
158 int x_step = (1 << SCALE_SHIFT) / scale_x;
159 int y_step = (1 << SCALE_SHIFT) / scale_y;
161 for (i = 0; i < (render_y1 - render_y0); i++)
163 const guchar *src = src_buf + (((i + render_y0) * y_step + y_step / 2) >> SCALE_SHIFT) * src_rowstride;
164 guchar *dest = dest_buf + i * dest_rowstride;
166 x = render_x0 * x_step + x_step / 2;
168 for (j=0; j < (render_x1 - render_x0); j++)
170 const guchar *p = src + (x >> SCALE_SHIFT) * src_channels;
174 a0 = (p[3] * overall_alpha) / 0xff;
192 unsigned int w0 = 0xff * a0;
193 unsigned int w1 = (0xff - a0) * dest[3];
194 unsigned int w = w0 + w1;
196 dest[0] = (w0 * p[0] + w1 * dest[0]) / w;
197 dest[1] = (w0 * p[1] + w1 * dest[1]) / w;
198 dest[2] = (w0 * p[2] + w1 * dest[2]) / w;
203 unsigned int a1 = 0xff - a0;
206 tmp = a0 * p[0] + a1 * dest[0] + 0x80;
207 dest[0] = (tmp + (tmp >> 8)) >> 8;
208 tmp = a0 * p[1] + a1 * dest[1] + 0x80;
209 dest[1] = (tmp + (tmp >> 8)) >> 8;
210 tmp = a0 * p[2] + a1 * dest[2] + 0x80;
211 dest[2] = (tmp + (tmp >> 8)) >> 8;
215 dest += dest_channels;
222 pixops_composite_color_nearest (guchar *dest_buf,
229 gboolean dest_has_alpha,
230 const guchar *src_buf,
235 gboolean src_has_alpha,
247 int x_step = (1 << SCALE_SHIFT) / scale_x;
248 int y_step = (1 << SCALE_SHIFT) / scale_y;
249 int r1, g1, b1, r2, g2, b2;
250 int check_shift = get_check_shift (check_size);
252 for (i = 0; i < (render_y1 - render_y0); i++)
254 const guchar *src = src_buf + (((i + render_y0) * y_step + y_step/2) >> SCALE_SHIFT) * src_rowstride;
255 guchar *dest = dest_buf + i * dest_rowstride;
257 x = render_x0 * x_step + x_step / 2;
259 if (((i + check_y) >> check_shift) & 1)
261 r1 = (color2 & 0xff0000) >> 16;
262 g1 = (color2 & 0xff00) >> 8;
265 r2 = (color1 & 0xff0000) >> 16;
266 g2 = (color1 & 0xff00) >> 8;
271 r1 = (color1 & 0xff0000) >> 16;
272 g1 = (color1 & 0xff00) >> 8;
275 r2 = (color2 & 0xff0000) >> 16;
276 g2 = (color2 & 0xff00) >> 8;
280 for (j=0 ; j < (render_x1 - render_x0); j++)
282 const guchar *p = src + (x >> SCALE_SHIFT) * src_channels;
287 a0 = (p[3] * overall_alpha + 0xff) >> 8;
294 if (((j + check_x) >> check_shift) & 1)
313 if (((j + check_x) >> check_shift) & 1)
315 tmp = ((int) p[0] - r2) * a0;
316 dest[0] = r2 + ((tmp + (tmp >> 8) + 0x80) >> 8);
317 tmp = ((int) p[1] - g2) * a0;
318 dest[1] = g2 + ((tmp + (tmp >> 8) + 0x80) >> 8);
319 tmp = ((int) p[2] - b2) * a0;
320 dest[2] = b2 + ((tmp + (tmp >> 8) + 0x80) >> 8);
324 tmp = ((int) p[0] - r1) * a0;
325 dest[0] = r1 + ((tmp + (tmp >> 8) + 0x80) >> 8);
326 tmp = ((int) p[1] - g1) * a0;
327 dest[1] = g1 + ((tmp + (tmp >> 8) + 0x80) >> 8);
328 tmp = ((int) p[2] - b1) * a0;
329 dest[2] = b1 + ((tmp + (tmp >> 8) + 0x80) >> 8);
334 if (dest_channels == 4)
337 dest += dest_channels;
344 composite_pixel (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
345 int src_has_alpha, int check_size, guint32 color1, guint32 color2,
346 guint r, guint g, guint b, guint a)
350 unsigned int w0 = a - (a >> 8);
351 unsigned int w1 = ((0xff0000 - a) >> 8) * dest[3];
352 unsigned int w = w0 + w1;
356 dest[0] = (r - (r >> 8) + w1 * dest[0]) / w;
357 dest[1] = (g - (g >> 8) + w1 * dest[1]) / w;
358 dest[2] = (b - (b >> 8) + w1 * dest[2]) / w;
359 dest[3] = w / 0xff00;
371 dest[0] = (r + (0xff0000 - a) * dest[0]) / 0xff0000;
372 dest[1] = (g + (0xff0000 - a) * dest[1]) / 0xff0000;
373 dest[2] = (b + (0xff0000 - a) * dest[2]) / 0xff0000;
378 composite_line (int *weights, int n_x, int n_y,
379 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
380 guchar **src, int src_channels, gboolean src_has_alpha,
381 int x_init, int x_step, int src_width,
382 int check_size, guint32 color1, guint32 color2)
387 while (dest < dest_end)
389 int x_scaled = x >> SCALE_SHIFT;
390 unsigned int r = 0, g = 0, b = 0, a = 0;
393 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
395 for (i=0; i<n_y; i++)
397 guchar *q = src[i] + x_scaled * src_channels;
398 int *line_weights = pixel_weights + n_x * i;
400 for (j=0; j<n_x; j++)
405 ta = q[3] * line_weights[j];
407 ta = 0xff * line_weights[j];
420 unsigned int w0 = a - (a >> 8);
421 unsigned int w1 = ((0xff0000 - a) >> 8) * dest[3];
422 unsigned int w = w0 + w1;
426 dest[0] = (r - (r >> 8) + w1 * dest[0]) / w;
427 dest[1] = (g - (g >> 8) + w1 * dest[1]) / w;
428 dest[2] = (b - (b >> 8) + w1 * dest[2]) / w;
429 dest[3] = w / 0xff00;
441 dest[0] = (r + (0xff0000 - a) * dest[0]) / 0xff0000;
442 dest[1] = (g + (0xff0000 - a) * dest[1]) / 0xff0000;
443 dest[2] = (b + (0xff0000 - a) * dest[2]) / 0xff0000;
446 dest += dest_channels;
454 composite_line_22_4a4 (int *weights, int n_x, int n_y,
455 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
456 guchar **src, int src_channels, gboolean src_has_alpha,
457 int x_init, int x_step, int src_width,
458 int check_size, guint32 color1, guint32 color2)
461 guchar *src0 = src[0];
462 guchar *src1 = src[1];
464 g_return_val_if_fail (src_channels != 3, dest);
465 g_return_val_if_fail (src_has_alpha, dest);
467 while (dest < dest_end)
469 int x_scaled = x >> SCALE_SHIFT;
470 unsigned int r, g, b, a, ta;
475 q0 = src0 + x_scaled * 4;
476 q1 = src1 + x_scaled * 4;
478 pixel_weights = (int *)((char *)weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS - 4)) & (SUBSAMPLE_MASK << 4)));
480 w1 = pixel_weights[0];
481 w2 = pixel_weights[1];
482 w3 = pixel_weights[2];
483 w4 = pixel_weights[3];
508 dest[0] = ((0xff0000 - a) * dest[0] + r) >> 24;
509 dest[1] = ((0xff0000 - a) * dest[1] + g) >> 24;
510 dest[2] = ((0xff0000 - a) * dest[2] + b) >> 24;
522 composite_line_22_4a4_mmx_stub (int *weights, int n_x, int n_y,
523 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
524 guchar **src, int src_channels, gboolean src_has_alpha,
525 int x_init, int x_step, int src_width,
526 int check_size, guint32 color1, guint32 color2)
528 guint32 mmx_weights[16][8];
533 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
534 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
535 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
536 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
537 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
538 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
539 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
540 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
543 return pixops_composite_line_22_4a4_mmx (mmx_weights, dest, src[0], src[1], x_step, dest_end, x_init);
548 composite_pixel_color (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
549 int src_has_alpha, int check_size, guint32 color1, guint32 color2,
550 guint r, guint g, guint b, guint a)
552 int dest_r, dest_g, dest_b;
553 int check_shift = get_check_shift (check_size);
555 if ((dest_x >> check_shift) & 1)
557 dest_r = (color2 & 0xff0000) >> 16;
558 dest_g = (color2 & 0xff00) >> 8;
559 dest_b = color2 & 0xff;
563 dest_r = (color1 & 0xff0000) >> 16;
564 dest_g = (color1 & 0xff00) >> 8;
565 dest_b = color1 & 0xff;
568 dest[0] = ((0xff0000 - a) * dest_r + r) >> 24;
569 dest[1] = ((0xff0000 - a) * dest_g + g) >> 24;
570 dest[2] = ((0xff0000 - a) * dest_b + b) >> 24;
574 else if (dest_channels == 4)
579 composite_line_color (int *weights, int n_x, int n_y,
580 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
581 guchar **src, int src_channels, gboolean src_has_alpha,
582 int x_init, int x_step, int src_width,
583 int check_size, guint32 color1, guint32 color2)
587 int check_shift = get_check_shift (check_size);
588 int dest_r1, dest_g1, dest_b1;
589 int dest_r2, dest_g2, dest_b2;
591 g_return_val_if_fail (check_size != 0, dest);
593 dest_r1 = (color1 & 0xff0000) >> 16;
594 dest_g1 = (color1 & 0xff00) >> 8;
595 dest_b1 = color1 & 0xff;
597 dest_r2 = (color2 & 0xff0000) >> 16;
598 dest_g2 = (color2 & 0xff00) >> 8;
599 dest_b2 = color2 & 0xff;
601 while (dest < dest_end)
603 int x_scaled = x >> SCALE_SHIFT;
604 unsigned int r = 0, g = 0, b = 0, a = 0;
607 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
609 for (i=0; i<n_y; i++)
611 guchar *q = src[i] + x_scaled * src_channels;
612 int *line_weights = pixel_weights + n_x * i;
614 for (j=0; j<n_x; j++)
619 ta = q[3] * line_weights[j];
621 ta = 0xff * line_weights[j];
632 if ((dest_x >> check_shift) & 1)
634 dest[0] = ((0xff0000 - a) * dest_r2 + r) >> 24;
635 dest[1] = ((0xff0000 - a) * dest_g2 + g) >> 24;
636 dest[2] = ((0xff0000 - a) * dest_b2 + b) >> 24;
640 dest[0] = ((0xff0000 - a) * dest_r1 + r) >> 24;
641 dest[1] = ((0xff0000 - a) * dest_g1 + g) >> 24;
642 dest[2] = ((0xff0000 - a) * dest_b1 + b) >> 24;
647 else if (dest_channels == 4)
650 dest += dest_channels;
660 composite_line_color_22_4a4_mmx_stub (int *weights, int n_x, int n_y,
661 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
662 guchar **src, int src_channels, gboolean src_has_alpha,
663 int x_init, int x_step, int src_width,
664 int check_size, guint32 color1, guint32 color2)
666 guint32 mmx_weights[16][8];
667 int check_shift = get_check_shift (check_size);
673 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
674 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
675 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
676 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
677 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
678 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
679 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
680 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
683 colors[0] = (color1 & 0xff00) << 8 | (color1 & 0xff);
684 colors[1] = (color1 & 0xff0000) >> 16;
685 colors[2] = (color2 & 0xff00) << 8 | (color2 & 0xff);
686 colors[3] = (color2 & 0xff0000) >> 16;
688 return pixops_composite_line_color_22_4a4_mmx (mmx_weights, dest, src[0], src[1], x_step, dest_end, x_init,
689 dest_x, check_shift, colors);
694 scale_pixel (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
695 int src_has_alpha, int check_size, guint32 color1, guint32 color2,
696 guint r, guint g, guint b, guint a)
717 dest[0] = (r + 0xffffff) >> 24;
718 dest[1] = (g + 0xffffff) >> 24;
719 dest[2] = (b + 0xffffff) >> 24;
727 scale_line (int *weights, int n_x, int n_y,
728 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
729 guchar **src, int src_channels, gboolean src_has_alpha,
730 int x_init, int x_step, int src_width,
731 int check_size, guint32 color1, guint32 color2)
736 while (dest < dest_end)
738 int x_scaled = x >> SCALE_SHIFT;
741 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
745 unsigned int r = 0, g = 0, b = 0, a = 0;
746 for (i=0; i<n_y; i++)
748 guchar *q = src[i] + x_scaled * src_channels;
749 int *line_weights = pixel_weights + n_x * i;
751 for (j=0; j<n_x; j++)
755 ta = q[3] * line_weights[j];
782 unsigned int r = 0, g = 0, b = 0;
783 for (i=0; i<n_y; i++)
785 guchar *q = src[i] + x_scaled * src_channels;
786 int *line_weights = pixel_weights + n_x * i;
788 for (j=0; j<n_x; j++)
790 unsigned int ta = line_weights[j];
800 dest[0] = (r + 0xffff) >> 16;
801 dest[1] = (g + 0xffff) >> 16;
802 dest[2] = (b + 0xffff) >> 16;
808 dest += dest_channels;
818 scale_line_22_33_mmx_stub (int *weights, int n_x, int n_y,
819 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
820 guchar **src, int src_channels, gboolean src_has_alpha,
821 int x_init, int x_step, int src_width,
822 int check_size, guint32 color1, guint32 color2)
824 guint32 mmx_weights[16][8];
829 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
830 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
831 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
832 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
833 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
834 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
835 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
836 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
839 return pixops_scale_line_22_33_mmx (mmx_weights, dest, src[0], src[1], x_step, dest_end, x_init);
844 scale_line_22_33 (int *weights, int n_x, int n_y,
845 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
846 guchar **src, int src_channels, gboolean src_has_alpha,
847 int x_init, int x_step, int src_width,
848 int check_size, guint32 color1, guint32 color2)
851 guchar *src0 = src[0];
852 guchar *src1 = src[1];
854 while (dest < dest_end)
856 unsigned int r, g, b;
857 int x_scaled = x >> SCALE_SHIFT;
862 q0 = src0 + x_scaled * 3;
863 q1 = src1 + x_scaled * 3;
865 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * 4;
867 w1 = pixel_weights[0];
868 w2 = pixel_weights[1];
869 w3 = pixel_weights[2];
870 w4 = pixel_weights[3];
888 dest[0] = (r + 0x8000) >> 16;
889 dest[1] = (g + 0x8000) >> 16;
890 dest[2] = (b + 0x8000) >> 16;
900 process_pixel (int *weights, int n_x, int n_y,
901 guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
902 guchar **src, int src_channels, gboolean src_has_alpha,
903 int x_start, int src_width,
904 int check_size, guint32 color1, guint32 color2,
905 PixopsPixelFunc pixel_func)
907 unsigned int r = 0, g = 0, b = 0, a = 0;
910 for (i=0; i<n_y; i++)
912 int *line_weights = weights + n_x * i;
914 for (j=0; j<n_x; j++)
921 else if (x_start + j < src_width)
922 q = src[i] + (x_start + j) * src_channels;
924 q = src[i] + (src_width - 1) * src_channels;
927 ta = q[3] * line_weights[j];
929 ta = 0xff * line_weights[j];
938 (*pixel_func) (dest, dest_x, dest_channels, dest_has_alpha, src_has_alpha, check_size, color1, color2, r, g, b, a);
942 pixops_process (guchar *dest_buf,
949 gboolean dest_has_alpha,
950 const guchar *src_buf,
955 gboolean src_has_alpha,
963 PixopsFilter *filter,
964 PixopsLineFunc line_func,
965 PixopsPixelFunc pixel_func)
968 int x, y; /* X and Y position in source (fixed_point) */
969 guchar **line_bufs = g_new (guchar *, filter->n_y);
971 int x_step = (1 << SCALE_SHIFT) / scale_x; /* X step in source (fixed point) */
972 int y_step = (1 << SCALE_SHIFT) / scale_y; /* Y step in source (fixed point) */
974 int check_shift = check_size ? get_check_shift (check_size) : 0;
976 int scaled_x_offset = floor (filter->x_offset * (1 << SCALE_SHIFT));
978 /* Compute the index where we run off the end of the source buffer. The furthest
979 * source pixel we access at index i is:
981 * ((render_x0 + i) * x_step + scaled_x_offset) >> SCALE_SHIFT + filter->n_x - 1
983 * So, run_end_index is the smallest i for which this pixel is src_width, i.e, for which:
985 * (i + render_x0) * x_step >= ((src_width - filter->n_x + 1) << SCALE_SHIFT) - scaled_x_offset
988 #define MYDIV(a,b) ((a) > 0 ? (a) / (b) : ((a) - (b) + 1) / (b)) /* Division so that -1/5 = -1 */
990 int run_end_x = (((src_width - filter->n_x + 1) << SCALE_SHIFT) - scaled_x_offset);
991 int run_end_index = MYDIV (run_end_x + x_step - 1, x_step) - render_x0;
992 run_end_index = MIN (run_end_index, render_x1 - render_x0);
994 y = render_y0 * y_step + floor (filter->y_offset * (1 << SCALE_SHIFT));
995 for (i = 0; i < (render_y1 - render_y0); i++)
998 int y_start = y >> SCALE_SHIFT;
1000 int *run_weights = filter->weights + ((y >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * filter->n_x * filter->n_y * SUBSAMPLE;
1002 guint32 tcolor1, tcolor2;
1004 guchar *outbuf = dest_buf + dest_rowstride * i;
1005 guchar *outbuf_end = outbuf + dest_channels * (render_x1 - render_x0);
1007 if (((i + check_y) >> check_shift) & 1)
1018 for (j=0; j<filter->n_y; j++)
1021 line_bufs[j] = (guchar *)src_buf;
1022 else if (y_start < src_height)
1023 line_bufs[j] = (guchar *)src_buf + src_rowstride * y_start;
1025 line_bufs[j] = (guchar *)src_buf + src_rowstride * (src_height - 1);
1031 x = render_x0 * x_step + scaled_x_offset;
1032 x_start = x >> SCALE_SHIFT;
1034 while (x_start < 0 && outbuf < outbuf_end)
1036 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->n_x * filter->n_y), filter->n_x, filter->n_y,
1037 outbuf, dest_x, dest_channels, dest_has_alpha,
1038 line_bufs, src_channels, src_has_alpha,
1039 x >> SCALE_SHIFT, src_width,
1040 check_size, tcolor1, tcolor2, pixel_func);
1043 x_start = x >> SCALE_SHIFT;
1045 outbuf += dest_channels;
1048 new_outbuf = (*line_func) (run_weights, filter->n_x, filter->n_y,
1050 dest_buf + dest_rowstride * i + run_end_index * dest_channels,
1051 dest_channels, dest_has_alpha,
1052 line_bufs, src_channels, src_has_alpha,
1053 x, x_step, src_width, check_size, tcolor1, tcolor2);
1055 dest_x += (new_outbuf - outbuf) / dest_channels;
1057 x = (dest_x - check_x + render_x0) * x_step + scaled_x_offset;
1058 outbuf = new_outbuf;
1060 while (outbuf < outbuf_end)
1062 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->n_x * filter->n_y), filter->n_x, filter->n_y,
1063 outbuf, dest_x, dest_channels, dest_has_alpha,
1064 line_bufs, src_channels, src_has_alpha,
1065 x >> SCALE_SHIFT, src_width,
1066 check_size, tcolor1, tcolor2, pixel_func);
1070 outbuf += dest_channels;
1080 correct_total (int *weights,
1084 double overall_alpha)
1086 int correction = (int)(0.5 + 65536 * overall_alpha) - total;
1088 for (i = n_x * n_y - 1; i >= 0; i--)
1090 if (*(weights + i) + correction >= 0)
1092 *(weights + i) += correction;
1099 tile_make_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1101 int i_offset, j_offset;
1103 int n_x = ceil(1/x_scale + 1);
1104 int n_y = ceil(1/y_scale + 1);
1106 filter->x_offset = 0;
1107 filter->y_offset = 0;
1110 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1112 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1113 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1115 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1116 double x = (double)j_offset / SUBSAMPLE;
1117 double y = (double)i_offset / SUBSAMPLE;
1121 for (i = 0; i < n_y; i++)
1129 th = MIN(i+1, y + 1/y_scale) - y;
1135 if (y + 1/y_scale > i)
1136 th = MIN(i+1, y + 1/y_scale) - i;
1141 for (j = 0; j < n_x; j++)
1148 tw = MIN(j+1, x + 1/x_scale) - x;
1154 if (x + 1/x_scale > j)
1155 tw = MIN(j+1, x + 1/x_scale) - j;
1160 weight = 65536 * tw * x_scale * th * y_scale * overall_alpha + 0.5;
1162 *(pixel_weights + n_x * i + j) = weight;
1166 correct_total (pixel_weights, n_x, n_y, total, overall_alpha);
1171 bilinear_make_fast_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1173 int i_offset, j_offset;
1174 double *x_weights, *y_weights;
1177 if (x_scale > 1.0) /* Bilinear */
1180 filter->x_offset = 0.5 * (1/x_scale - 1);
1184 n_x = ceil(1.0 + 1.0/x_scale);
1185 filter->x_offset = 0.0;
1188 if (y_scale > 1.0) /* Bilinear */
1191 filter->y_offset = 0.5 * (1/y_scale - 1);
1195 n_y = ceil(1.0 + 1.0/y_scale);
1196 filter->y_offset = 0.0;
1201 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1203 x_weights = g_new (double, n_x);
1204 y_weights = g_new (double, n_y);
1206 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1207 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1209 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1210 double x = (double)j_offset / SUBSAMPLE;
1211 double y = (double)i_offset / SUBSAMPLE;
1215 if (x_scale > 1.0) /* Bilinear */
1217 for (i = 0; i < n_x; i++)
1219 x_weights[i] = ((i == 0) ? (1 - x) : x) / x_scale;
1225 * ---------|--.-|----|--.-|------- SRC
1226 * ------------|---------|--------- DEST
1228 for (i = 0; i < n_x; i++)
1233 x_weights[i] = MIN(i+1, x + 1/x_scale) - x;
1239 if (x + 1/x_scale > i)
1240 x_weights[i] = MIN(i+1, x + 1/x_scale) - i;
1247 if (y_scale > 1.0) /* Bilinear */
1249 for (i = 0; i < n_y; i++)
1251 y_weights[i] = ((i == 0) ? (1 - y) : y) / y_scale;
1257 * ---------|--.-|----|--.-|------- SRC
1258 * ------------|---------|--------- DEST
1260 for (i = 0; i < n_y; i++)
1265 y_weights[i] = MIN(i+1, y + 1/y_scale) - y;
1271 if (y + 1/y_scale > i)
1272 y_weights[i] = MIN(i+1, y + 1/y_scale) - i;
1279 for (i = 0; i < n_y; i++)
1280 for (j = 0; j < n_x; j++)
1282 int weight = 65536 * x_weights[j] * x_scale * y_weights[i] * y_scale * overall_alpha + 0.5;
1283 *(pixel_weights + n_x * i + j) = weight;
1287 correct_total (pixel_weights, n_x, n_y, total, overall_alpha);
1295 bilinear_quadrant (double bx0, double bx1, double by0, double by1)
1297 double ax0, ax1, ay0, ay1;
1298 double x0, x1, y0, y1;
1310 x1 = MIN (ax1, bx1);
1320 x1 = MIN (ax1, bx1);
1331 y1 = MIN (ay1, by1);
1341 y1 = MIN (ay1, by1);
1347 return 0.25 * (x1*x1 - x0*x0) * (y1*y1 - y0*y0);
1351 bilinear_make_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1353 int i_offset, j_offset;
1355 int n_x = ceil(1/x_scale + 2.0);
1356 int n_y = ceil(1/y_scale + 2.0);
1358 filter->x_offset = -1.0;
1359 filter->y_offset = -1.0;
1363 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1365 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1366 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1368 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1369 double x = (double)j_offset / SUBSAMPLE;
1370 double y = (double)i_offset / SUBSAMPLE;
1374 for (i = 0; i < n_y; i++)
1375 for (j = 0; j < n_x; j++)
1380 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);
1381 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);
1382 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);
1383 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);
1384 weight = 65536 * w * x_scale * y_scale * overall_alpha + 0.5;
1385 *(pixel_weights + n_x * i + j) = weight;
1389 correct_total (pixel_weights, n_x, n_y, total, overall_alpha);
1394 pixops_composite_color (guchar *dest_buf,
1401 gboolean dest_has_alpha,
1402 const guchar *src_buf,
1407 gboolean src_has_alpha,
1410 PixopsInterpType interp_type,
1418 PixopsFilter filter;
1419 PixopsLineFunc line_func;
1422 gboolean found_mmx = pixops_have_mmx();
1425 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1426 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1428 if (scale_x == 0 || scale_y == 0)
1431 if (!src_has_alpha && overall_alpha == 255)
1432 pixops_scale (dest_buf, render_x0, render_y0, render_x1, render_y1,
1433 dest_rowstride, dest_channels, dest_has_alpha,
1434 src_buf, src_width, src_height, src_rowstride, src_channels,
1435 src_has_alpha, scale_x, scale_y, interp_type);
1437 switch (interp_type)
1439 case PIXOPS_INTERP_NEAREST:
1440 pixops_composite_color_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1441 dest_rowstride, dest_channels, dest_has_alpha,
1442 src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
1443 scale_x, scale_y, overall_alpha,
1444 check_x, check_y, check_size, color1, color2);
1447 case PIXOPS_INTERP_TILES:
1448 tile_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1451 case PIXOPS_INTERP_BILINEAR:
1452 bilinear_make_fast_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1455 case PIXOPS_INTERP_HYPER:
1456 bilinear_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1461 if (filter.n_x == 2 && filter.n_y == 2 &&
1462 dest_channels == 4 && src_channels == 4 && src_has_alpha && !dest_has_alpha && found_mmx)
1463 line_func = composite_line_color_22_4a4_mmx_stub;
1466 line_func = composite_line_color;
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, check_x, check_y, check_size, color1, color2,
1472 &filter, line_func, composite_pixel_color);
1474 g_free (filter.weights);
1479 * @dest_buf: pointer to location to store result
1480 * @render_x0: x0 of region of scaled source to store into @dest_buf
1481 * @render_y0: y0 of region of scaled source to store into @dest_buf
1482 * @render_x1: x1 of region of scaled source to store into @dest_buf
1483 * @render_y1: x1 of region of scaled source to store into @dest_buf
1484 * @dest_rowstride: rowstride of @dest_buf
1485 * @dest_channels: number of channels in @dest_buf
1486 * @dest_has_alpha: whether @dest_buf has alpha
1487 * @src_buf: pointer to source pixels
1488 * @src_width: width of source (used for clipping)
1489 * @src_height: height of source (used for clipping)
1490 * @src_rowstride: rowstride of source
1491 * @src_channels: number of channels in @src_buf
1492 * @src_has_alpha: whether @src_buf has alpha
1493 * @scale_x: amount to scale source by in X direction
1494 * @scale_y: amount to scale source by in Y direction
1495 * @interp_type: type of enumeration
1496 * @overall_alpha: overall alpha factor to multiply source by
1498 * Scale source buffer by scale_x / scale_y, then composite a given rectangle
1499 * of the result into the destination buffer.
1502 pixops_composite (guchar *dest_buf,
1509 gboolean dest_has_alpha,
1510 const guchar *src_buf,
1515 gboolean src_has_alpha,
1518 PixopsInterpType interp_type,
1521 PixopsFilter filter;
1522 PixopsLineFunc line_func;
1525 gboolean found_mmx = pixops_have_mmx();
1528 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1529 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1531 if (scale_x == 0 || scale_y == 0)
1534 if (!src_has_alpha && overall_alpha == 255)
1535 pixops_scale (dest_buf, render_x0, render_y0, render_x1, render_y1,
1536 dest_rowstride, dest_channels, dest_has_alpha,
1537 src_buf, src_width, src_height, src_rowstride, src_channels,
1538 src_has_alpha, scale_x, scale_y, interp_type);
1540 switch (interp_type)
1542 case PIXOPS_INTERP_NEAREST:
1543 pixops_composite_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1544 dest_rowstride, dest_channels, dest_has_alpha,
1545 src_buf, src_width, src_height, src_rowstride, src_channels,
1546 src_has_alpha, scale_x, scale_y, overall_alpha);
1549 case PIXOPS_INTERP_TILES:
1550 tile_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1553 case PIXOPS_INTERP_BILINEAR:
1554 bilinear_make_fast_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1557 case PIXOPS_INTERP_HYPER:
1558 bilinear_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1562 if (filter.n_x == 2 && filter.n_y == 2 &&
1563 dest_channels == 4 && src_channels == 4 && src_has_alpha && !dest_has_alpha)
1567 line_func = composite_line_22_4a4_mmx_stub;
1570 line_func = composite_line_22_4a4;
1573 line_func = composite_line;
1575 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1576 dest_rowstride, dest_channels, dest_has_alpha,
1577 src_buf, src_width, src_height, src_rowstride, src_channels,
1578 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
1579 &filter, line_func, composite_pixel);
1581 g_free (filter.weights);
1585 pixops_scale (guchar *dest_buf,
1592 gboolean dest_has_alpha,
1593 const guchar *src_buf,
1598 gboolean src_has_alpha,
1601 PixopsInterpType interp_type)
1603 PixopsFilter filter;
1604 PixopsLineFunc line_func;
1607 gboolean found_mmx = pixops_have_mmx();
1610 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1611 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1612 g_return_if_fail (!(src_has_alpha && !dest_has_alpha));
1614 if (scale_x == 0 || scale_y == 0)
1617 switch (interp_type)
1619 case PIXOPS_INTERP_NEAREST:
1620 pixops_scale_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1621 dest_rowstride, dest_channels, dest_has_alpha,
1622 src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
1626 case PIXOPS_INTERP_TILES:
1627 tile_make_weights (&filter, scale_x, scale_y, 1.0);
1630 case PIXOPS_INTERP_BILINEAR:
1631 bilinear_make_fast_weights (&filter, scale_x, scale_y, 1.0);
1634 case PIXOPS_INTERP_HYPER:
1635 bilinear_make_weights (&filter, scale_x, scale_y, 1.0);
1639 if (filter.n_x == 2 && filter.n_y == 2 && dest_channels == 3 && src_channels == 3)
1643 line_func = scale_line_22_33_mmx_stub;
1646 line_func = scale_line_22_33;
1649 line_func = scale_line;
1651 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1652 dest_rowstride, dest_channels, dest_has_alpha,
1653 src_buf, src_width, src_height, src_rowstride, src_channels,
1654 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
1655 &filter, line_func, scale_pixel);
1657 g_free (filter.weights);