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;
901 process_pixel (int *weights, int n_x, int n_y,
902 guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
903 guchar **src, int src_channels, gboolean src_has_alpha,
904 int x_start, int src_width,
905 int check_size, guint32 color1, guint32 color2,
906 PixopsPixelFunc pixel_func)
908 unsigned int r = 0, g = 0, b = 0, a = 0;
911 for (i=0; i<n_y; i++)
913 int *line_weights = weights + n_x * i;
915 for (j=0; j<n_x; j++)
922 else if (x_start + j < src_width)
923 q = src[i] + (x_start + j) * src_channels;
925 q = src[i] + (src_width - 1) * src_channels;
928 ta = q[3] * line_weights[j];
930 ta = 0xff * line_weights[j];
939 (*pixel_func) (dest, dest_x, dest_channels, dest_has_alpha, src_has_alpha, check_size, color1, color2, r, g, b, a);
943 pixops_process (guchar *dest_buf,
950 gboolean dest_has_alpha,
951 const guchar *src_buf,
956 gboolean src_has_alpha,
964 PixopsFilter *filter,
965 PixopsLineFunc line_func,
966 PixopsPixelFunc pixel_func)
969 int x, y; /* X and Y position in source (fixed_point) */
970 guchar **line_bufs = g_new (guchar *, filter->n_y);
972 int x_step = (1 << SCALE_SHIFT) / scale_x; /* X step in source (fixed point) */
973 int y_step = (1 << SCALE_SHIFT) / scale_y; /* Y step in source (fixed point) */
975 int check_shift = check_size ? get_check_shift (check_size) : 0;
977 int scaled_x_offset = floor (filter->x_offset * (1 << SCALE_SHIFT));
979 /* Compute the index where we run off the end of the source buffer. The furthest
980 * source pixel we access at index i is:
982 * ((render_x0 + i) * x_step + scaled_x_offset) >> SCALE_SHIFT + filter->n_x - 1
984 * So, run_end_index is the smallest i for which this pixel is src_width, i.e, for which:
986 * (i + render_x0) * x_step >= ((src_width - filter->n_x + 1) << SCALE_SHIFT) - scaled_x_offset
989 #define MYDIV(a,b) ((a) > 0 ? (a) / (b) : ((a) - (b) + 1) / (b)) /* Division so that -1/5 = -1 */
991 int run_end_x = (((src_width - filter->n_x + 1) << SCALE_SHIFT) - scaled_x_offset);
992 int run_end_index = MYDIV (run_end_x + x_step - 1, x_step) - render_x0;
993 run_end_index = MIN (run_end_index, render_x1 - render_x0);
995 y = render_y0 * y_step + floor (filter->y_offset * (1 << SCALE_SHIFT));
996 for (i = 0; i < (render_y1 - render_y0); i++)
999 int y_start = y >> SCALE_SHIFT;
1001 int *run_weights = filter->weights + ((y >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * filter->n_x * filter->n_y * SUBSAMPLE;
1003 guint32 tcolor1, tcolor2;
1005 guchar *outbuf = dest_buf + dest_rowstride * i;
1006 guchar *outbuf_end = outbuf + dest_channels * (render_x1 - render_x0);
1008 if (((i + check_y) >> check_shift) & 1)
1019 for (j=0; j<filter->n_y; j++)
1022 line_bufs[j] = (guchar *)src_buf;
1023 else if (y_start < src_height)
1024 line_bufs[j] = (guchar *)src_buf + src_rowstride * y_start;
1026 line_bufs[j] = (guchar *)src_buf + src_rowstride * (src_height - 1);
1032 x = render_x0 * x_step + scaled_x_offset;
1033 x_start = x >> SCALE_SHIFT;
1035 while (x_start < 0 && outbuf < outbuf_end)
1037 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->n_x * filter->n_y), filter->n_x, filter->n_y,
1038 outbuf, dest_x, dest_channels, dest_has_alpha,
1039 line_bufs, src_channels, src_has_alpha,
1040 x >> SCALE_SHIFT, src_width,
1041 check_size, tcolor1, tcolor2, pixel_func);
1044 x_start = x >> SCALE_SHIFT;
1046 outbuf += dest_channels;
1049 new_outbuf = (*line_func) (run_weights, filter->n_x, filter->n_y,
1051 dest_buf + dest_rowstride * i + run_end_index * dest_channels,
1052 dest_channels, dest_has_alpha,
1053 line_bufs, src_channels, src_has_alpha,
1054 x, x_step, src_width, check_size, tcolor1, tcolor2);
1056 dest_x += (new_outbuf - outbuf) / dest_channels;
1058 x = (dest_x - check_x + render_x0) * x_step + scaled_x_offset;
1059 outbuf = new_outbuf;
1061 while (outbuf < outbuf_end)
1063 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->n_x * filter->n_y), filter->n_x, filter->n_y,
1064 outbuf, dest_x, dest_channels, dest_has_alpha,
1065 line_bufs, src_channels, src_has_alpha,
1066 x >> SCALE_SHIFT, src_width,
1067 check_size, tcolor1, tcolor2, pixel_func);
1071 outbuf += dest_channels;
1081 correct_total (int *weights,
1085 double overall_alpha)
1087 int correction = (int)(0.5 + 65536 * overall_alpha) - total;
1089 for (i = n_x * n_y - 1; i >= 0; i--)
1091 if (*(weights + i) + correction >= 0)
1093 *(weights + i) += correction;
1100 tile_make_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1102 int i_offset, j_offset;
1104 int n_x = ceil(1/x_scale + 1);
1105 int n_y = ceil(1/y_scale + 1);
1107 filter->x_offset = 0;
1108 filter->y_offset = 0;
1111 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1113 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1114 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1116 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1117 double x = (double)j_offset / SUBSAMPLE;
1118 double y = (double)i_offset / SUBSAMPLE;
1122 for (i = 0; i < n_y; i++)
1130 th = MIN(i+1, y + 1/y_scale) - y;
1136 if (y + 1/y_scale > i)
1137 th = MIN(i+1, y + 1/y_scale) - i;
1142 for (j = 0; j < n_x; j++)
1149 tw = MIN(j+1, x + 1/x_scale) - x;
1155 if (x + 1/x_scale > j)
1156 tw = MIN(j+1, x + 1/x_scale) - j;
1161 weight = 65536 * tw * x_scale * th * y_scale * overall_alpha + 0.5;
1163 *(pixel_weights + n_x * i + j) = weight;
1167 correct_total (pixel_weights, n_x, n_y, total, overall_alpha);
1172 bilinear_make_fast_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1174 int i_offset, j_offset;
1175 double *x_weights, *y_weights;
1178 if (x_scale > 1.0) /* Bilinear */
1181 filter->x_offset = 0.5 * (1/x_scale - 1);
1185 n_x = ceil(1.0 + 1.0/x_scale);
1186 filter->x_offset = 0.0;
1189 if (y_scale > 1.0) /* Bilinear */
1192 filter->y_offset = 0.5 * (1/y_scale - 1);
1196 n_y = ceil(1.0 + 1.0/y_scale);
1197 filter->y_offset = 0.0;
1202 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1204 x_weights = g_new (double, n_x);
1205 y_weights = g_new (double, n_y);
1207 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1208 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1210 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1211 double x = (double)j_offset / SUBSAMPLE;
1212 double y = (double)i_offset / SUBSAMPLE;
1216 if (x_scale > 1.0) /* Bilinear */
1218 for (i = 0; i < n_x; i++)
1220 x_weights[i] = ((i == 0) ? (1 - x) : x) / x_scale;
1226 * ---------|--.-|----|--.-|------- SRC
1227 * ------------|---------|--------- DEST
1229 for (i = 0; i < n_x; i++)
1234 x_weights[i] = MIN(i+1, x + 1/x_scale) - x;
1240 if (x + 1/x_scale > i)
1241 x_weights[i] = MIN(i+1, x + 1/x_scale) - i;
1248 if (y_scale > 1.0) /* Bilinear */
1250 for (i = 0; i < n_y; i++)
1252 y_weights[i] = ((i == 0) ? (1 - y) : y) / y_scale;
1258 * ---------|--.-|----|--.-|------- SRC
1259 * ------------|---------|--------- DEST
1261 for (i = 0; i < n_y; i++)
1266 y_weights[i] = MIN(i+1, y + 1/y_scale) - y;
1272 if (y + 1/y_scale > i)
1273 y_weights[i] = MIN(i+1, y + 1/y_scale) - i;
1280 for (i = 0; i < n_y; i++)
1281 for (j = 0; j < n_x; j++)
1283 int weight = 65536 * x_weights[j] * x_scale * y_weights[i] * y_scale * overall_alpha + 0.5;
1284 *(pixel_weights + n_x * i + j) = weight;
1288 correct_total (pixel_weights, n_x, n_y, total, overall_alpha);
1296 bilinear_quadrant (double bx0, double bx1, double by0, double by1)
1298 double ax0, ax1, ay0, ay1;
1299 double x0, x1, y0, y1;
1311 x1 = MIN (ax1, bx1);
1321 x1 = MIN (ax1, bx1);
1332 y1 = MIN (ay1, by1);
1342 y1 = MIN (ay1, by1);
1348 return 0.25 * (x1*x1 - x0*x0) * (y1*y1 - y0*y0);
1352 bilinear_make_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1354 int i_offset, j_offset;
1356 int n_x = ceil(1/x_scale + 2.0);
1357 int n_y = ceil(1/y_scale + 2.0);
1359 filter->x_offset = -1.0;
1360 filter->y_offset = -1.0;
1364 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1366 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1367 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1369 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1370 double x = (double)j_offset / SUBSAMPLE;
1371 double y = (double)i_offset / SUBSAMPLE;
1375 for (i = 0; i < n_y; i++)
1376 for (j = 0; j < n_x; j++)
1381 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);
1382 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);
1383 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);
1384 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);
1385 weight = 65536 * w * x_scale * y_scale * overall_alpha + 0.5;
1386 *(pixel_weights + n_x * i + j) = weight;
1390 correct_total (pixel_weights, n_x, n_y, total, overall_alpha);
1395 pixops_composite_color (guchar *dest_buf,
1402 gboolean dest_has_alpha,
1403 const guchar *src_buf,
1408 gboolean src_has_alpha,
1411 PixopsInterpType interp_type,
1419 PixopsFilter filter;
1420 PixopsLineFunc line_func;
1423 gboolean found_mmx = pixops_have_mmx();
1426 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1427 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1429 if (scale_x == 0 || scale_y == 0)
1432 if (!src_has_alpha && overall_alpha == 255)
1433 pixops_scale (dest_buf, render_x0, render_y0, render_x1, render_y1,
1434 dest_rowstride, dest_channels, dest_has_alpha,
1435 src_buf, src_width, src_height, src_rowstride, src_channels,
1436 src_has_alpha, scale_x, scale_y, interp_type);
1438 switch (interp_type)
1440 case PIXOPS_INTERP_NEAREST:
1441 pixops_composite_color_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1442 dest_rowstride, dest_channels, dest_has_alpha,
1443 src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
1444 scale_x, scale_y, overall_alpha,
1445 check_x, check_y, check_size, color1, color2);
1448 case PIXOPS_INTERP_TILES:
1449 tile_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1452 case PIXOPS_INTERP_BILINEAR:
1453 bilinear_make_fast_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1456 case PIXOPS_INTERP_HYPER:
1457 bilinear_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1462 if (filter.n_x == 2 && filter.n_y == 2 &&
1463 dest_channels == 4 && src_channels == 4 && src_has_alpha && !dest_has_alpha && found_mmx)
1464 line_func = composite_line_color_22_4a4_mmx_stub;
1467 line_func = composite_line_color;
1469 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1470 dest_rowstride, dest_channels, dest_has_alpha,
1471 src_buf, src_width, src_height, src_rowstride, src_channels,
1472 src_has_alpha, scale_x, scale_y, check_x, check_y, check_size, color1, color2,
1473 &filter, line_func, composite_pixel_color);
1475 g_free (filter.weights);
1480 * @dest_buf: pointer to location to store result
1481 * @render_x0: x0 of region of scaled source to store into @dest_buf
1482 * @render_y0: y0 of region of scaled source to store into @dest_buf
1483 * @render_x1: x1 of region of scaled source to store into @dest_buf
1484 * @render_y1: x1 of region of scaled source to store into @dest_buf
1485 * @dest_rowstride: rowstride of @dest_buf
1486 * @dest_channels: number of channels in @dest_buf
1487 * @dest_has_alpha: whether @dest_buf has alpha
1488 * @src_buf: pointer to source pixels
1489 * @src_width: width of source (used for clipping)
1490 * @src_height: height of source (used for clipping)
1491 * @src_rowstride: rowstride of source
1492 * @src_channels: number of channels in @src_buf
1493 * @src_has_alpha: whether @src_buf has alpha
1494 * @scale_x: amount to scale source by in X direction
1495 * @scale_y: amount to scale source by in Y direction
1496 * @interp_type: type of enumeration
1497 * @overall_alpha: overall alpha factor to multiply source by
1499 * Scale source buffer by scale_x / scale_y, then composite a given rectangle
1500 * of the result into the destination buffer.
1503 pixops_composite (guchar *dest_buf,
1510 gboolean dest_has_alpha,
1511 const guchar *src_buf,
1516 gboolean src_has_alpha,
1519 PixopsInterpType interp_type,
1522 PixopsFilter filter;
1523 PixopsLineFunc line_func;
1526 gboolean found_mmx = pixops_have_mmx();
1529 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1530 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1532 if (scale_x == 0 || scale_y == 0)
1535 if (!src_has_alpha && overall_alpha == 255)
1536 pixops_scale (dest_buf, render_x0, render_y0, render_x1, render_y1,
1537 dest_rowstride, dest_channels, dest_has_alpha,
1538 src_buf, src_width, src_height, src_rowstride, src_channels,
1539 src_has_alpha, scale_x, scale_y, interp_type);
1541 switch (interp_type)
1543 case PIXOPS_INTERP_NEAREST:
1544 pixops_composite_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1545 dest_rowstride, dest_channels, dest_has_alpha,
1546 src_buf, src_width, src_height, src_rowstride, src_channels,
1547 src_has_alpha, scale_x, scale_y, overall_alpha);
1550 case PIXOPS_INTERP_TILES:
1551 tile_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1554 case PIXOPS_INTERP_BILINEAR:
1555 bilinear_make_fast_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1558 case PIXOPS_INTERP_HYPER:
1559 bilinear_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1563 if (filter.n_x == 2 && filter.n_y == 2 &&
1564 dest_channels == 4 && src_channels == 4 && src_has_alpha && !dest_has_alpha)
1568 line_func = composite_line_22_4a4_mmx_stub;
1571 line_func = composite_line_22_4a4;
1574 line_func = composite_line;
1576 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1577 dest_rowstride, dest_channels, dest_has_alpha,
1578 src_buf, src_width, src_height, src_rowstride, src_channels,
1579 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
1580 &filter, line_func, composite_pixel);
1582 g_free (filter.weights);
1586 pixops_scale (guchar *dest_buf,
1593 gboolean dest_has_alpha,
1594 const guchar *src_buf,
1599 gboolean src_has_alpha,
1602 PixopsInterpType interp_type)
1604 PixopsFilter filter;
1605 PixopsLineFunc line_func;
1608 gboolean found_mmx = pixops_have_mmx();
1611 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1612 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1613 g_return_if_fail (!(src_has_alpha && !dest_has_alpha));
1615 if (scale_x == 0 || scale_y == 0)
1618 switch (interp_type)
1620 case PIXOPS_INTERP_NEAREST:
1621 pixops_scale_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1622 dest_rowstride, dest_channels, dest_has_alpha,
1623 src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
1627 case PIXOPS_INTERP_TILES:
1628 tile_make_weights (&filter, scale_x, scale_y, 1.0);
1631 case PIXOPS_INTERP_BILINEAR:
1632 bilinear_make_fast_weights (&filter, scale_x, scale_y, 1.0);
1635 case PIXOPS_INTERP_HYPER:
1636 bilinear_make_weights (&filter, scale_x, scale_y, 1.0);
1640 if (filter.n_x == 2 && filter.n_y == 2 && dest_channels == 3 && src_channels == 3)
1644 line_func = scale_line_22_33_mmx_stub;
1647 line_func = scale_line_22_33;
1650 line_func = scale_line;
1652 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1653 dest_rowstride, dest_channels, dest_has_alpha,
1654 src_buf, src_width, src_height, src_rowstride, src_channels,
1655 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
1656 &filter, line_func, scale_pixel);
1658 g_free (filter.weights);