5 #include "pixops-internal.h"
7 #define SUBSAMPLE_BITS 4
8 #define SUBSAMPLE (1 << SUBSAMPLE_BITS)
9 #define SUBSAMPLE_MASK ((1 << SUBSAMPLE_BITS)-1)
10 #define SCALE_SHIFT 16
12 typedef struct _PixopsFilter PixopsFilter;
23 typedef guchar *(*PixopsLineFunc) (int *weights, int n_x, int n_y,
24 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
25 guchar **src, int src_channels, gboolean src_has_alpha,
26 int x_init, int x_step, int src_width,
27 int check_size, guint32 color1, guint32 color2);
29 typedef void (*PixopsPixelFunc) (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
30 int src_has_alpha, int check_size, guint32 color1,
32 int r, int g, int b, int a);
35 get_check_shift (int check_size)
38 g_return_val_if_fail (check_size >= 0, 4);
40 while (!(check_size & 1))
50 pixops_scale_nearest (guchar *dest_buf,
57 gboolean dest_has_alpha,
58 const guchar *src_buf,
63 gboolean src_has_alpha,
69 int x_step = (1 << SCALE_SHIFT) / scale_x;
70 int y_step = (1 << SCALE_SHIFT) / scale_y;
72 #define INNER_LOOP(SRC_CHANNELS,DEST_CHANNELS) \
73 for (j=0; j < (render_x1 - render_x0); j++) \
75 const guchar *p = src + (x >> SCALE_SHIFT) * SRC_CHANNELS; \
81 if (DEST_CHANNELS == 4) \
83 if (SRC_CHANNELS == 4) \
89 dest += DEST_CHANNELS; \
93 for (i = 0; i < (render_y1 - render_y0); i++)
95 const guchar *src = src_buf + (((i + render_y0) * y_step + y_step / 2) >> SCALE_SHIFT) * src_rowstride;
96 guchar *dest = dest_buf + i * dest_rowstride;
98 x = render_x0 * x_step + x_step / 2;
100 if (src_channels == 3)
102 if (dest_channels == 3)
111 else if (src_channels == 4)
113 if (dest_channels == 3)
119 for (j=0; j < (render_x1 - render_x0); j++)
121 const guchar *p = src + (x >> SCALE_SHIFT) * 4;
124 p32 = (guint32 *) dest;
125 *p32 = *((guint32 *) p);
137 pixops_composite_nearest (guchar *dest_buf,
144 gboolean dest_has_alpha,
145 const guchar *src_buf,
150 gboolean src_has_alpha,
157 int x_step = (1 << SCALE_SHIFT) / scale_x;
158 int y_step = (1 << SCALE_SHIFT) / scale_y;
160 for (i = 0; i < (render_y1 - render_y0); i++)
162 const guchar *src = src_buf + (((i + render_y0) * y_step + y_step / 2) >> SCALE_SHIFT) * src_rowstride;
163 guchar *dest = dest_buf + i * dest_rowstride + render_x0 * dest_channels;
165 x = render_x0 * x_step + x_step / 2;
167 for (j=0; j < (render_x1 - render_x0); j++)
169 const guchar *p = src + (x >> SCALE_SHIFT) * src_channels;
173 a0 = (p[3] * overall_alpha) / 0xff;
179 unsigned int w0 = 0xff * a0;
180 unsigned int w1 = (0xff - a0) * dest[3];
181 unsigned int w = w0 + w1;
185 dest[0] = (w0 * src[0] + w1 * dest[0]) / w;
186 dest[1] = (w0 * src[1] + w1 * dest[1]) / w;
187 dest[2] = (w0 * src[2] + w1 * dest[2]) / w;
200 dest[0] = (a0 * p[0] + (0xff - a0) * dest[0]) / 0xff;
201 dest[1] = (a0 * p[1] + (0xff - a0) * dest[1]) / 0xff;
202 dest[2] = (a0 * p[2] + (0xff - a0) * dest[2]) / 0xff;
204 if (dest_channels == 4)
208 dest += dest_channels;
215 pixops_composite_color_nearest (guchar *dest_buf,
222 gboolean dest_has_alpha,
223 const guchar *src_buf,
228 gboolean src_has_alpha,
240 int x_step = (1 << SCALE_SHIFT) / scale_x;
241 int y_step = (1 << SCALE_SHIFT) / scale_y;
242 int r1, g1, b1, r2, g2, b2;
243 int check_shift = get_check_shift (check_size);
245 for (i = 0; i < (render_y1 - render_y0); i++)
247 const guchar *src = src_buf + (((i + render_y0) * y_step + y_step/2) >> SCALE_SHIFT) * src_rowstride;
248 guchar *dest = dest_buf + i * dest_rowstride;
250 x = render_x0 * x_step + x_step / 2;
252 if (((i + check_y) >> check_shift) & 1)
254 r1 = (color2 & 0xff0000) >> 16;
255 g1 = (color2 & 0xff00) >> 8;
258 r2 = (color1 & 0xff0000) >> 16;
259 g2 = (color1 & 0xff00) >> 8;
264 r1 = (color1 & 0xff0000) >> 16;
265 g1 = (color1 & 0xff00) >> 8;
268 r2 = (color2 & 0xff0000) >> 16;
269 g2 = (color2 & 0xff00) >> 8;
273 for (j=0 ; j < (render_x1 - render_x0); j++)
275 const guchar *p = src + (x >> SCALE_SHIFT) * src_channels;
279 a0 = (p[3] * overall_alpha + 0xff) >> 8;
283 if (((j + check_x) >> check_shift) & 1)
285 dest[0] = r2 + ((a0 * ((int)p[0] - r2) + 0xff) >> 8);
286 dest[1] = g2 + ((a0 * ((int)p[1] - g2) + 0xff) >> 8);
287 dest[2] = b2 + ((a0 * ((int)p[2] - b2) + 0xff) >> 8);
291 dest[0] = r1 + ((a0 * ((int)p[0] - r1) + 0xff) >> 8);
292 dest[1] = g1 + ((a0 * ((int)p[1] - g1) + 0xff) >> 8);
293 dest[2] = b1 + ((a0 * ((int)p[2] - b1) + 0xff) >> 8);
296 if (dest_channels == 4)
299 dest += dest_channels;
306 composite_pixel (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
307 int src_has_alpha, int check_size, guint32 color1, guint32 color2,
308 int r, int g, int b, int a)
312 unsigned int w0 = a - (a >> 8);
313 unsigned int w1 = ((0xff0000 - a) >> 8) * dest[3];
314 unsigned int w = w0 + w1;
318 dest[0] = (r - (r >> 8) + w1 * dest[0]) / w;
319 dest[1] = (g - (g >> 8) + w1 * dest[1]) / w;
320 dest[2] = (b - (b >> 8) + w1 * dest[2]) / w;
321 dest[3] = w / 0xff00;
333 dest[0] = (r + (0xff0000 - a) * dest[0]) / 0xff0000;
334 dest[1] = (g + (0xff0000 - a) * dest[1]) / 0xff0000;
335 dest[2] = (b + (0xff0000 - a) * dest[2]) / 0xff0000;
340 composite_line (int *weights, int n_x, int n_y,
341 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
342 guchar **src, int src_channels, gboolean src_has_alpha,
343 int x_init, int x_step, int src_width,
344 int check_size, guint32 color1, guint32 color2)
349 while (dest < dest_end)
351 int x_scaled = x >> SCALE_SHIFT;
352 unsigned int r = 0, g = 0, b = 0, a = 0;
355 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
357 for (i=0; i<n_y; i++)
359 guchar *q = src[i] + x_scaled * src_channels;
360 int *line_weights = pixel_weights + n_x * i;
362 for (j=0; j<n_x; j++)
367 ta = q[3] * line_weights[j];
369 ta = 0xff * line_weights[j];
382 unsigned int w0 = a - (a >> 8);
383 unsigned int w1 = ((0xff0000 - a) >> 8) * dest[3];
384 unsigned int w = w0 + w1;
388 dest[0] = (r - (r >> 8) + w1 * dest[0]) / w;
389 dest[1] = (g - (g >> 8) + w1 * dest[1]) / w;
390 dest[2] = (b - (b >> 8) + w1 * dest[2]) / w;
391 dest[3] = w / 0xff00;
403 dest[0] = (r + (0xff0000 - a) * dest[0]) / 0xff0000;
404 dest[1] = (g + (0xff0000 - a) * dest[1]) / 0xff0000;
405 dest[2] = (b + (0xff0000 - a) * dest[2]) / 0xff0000;
408 dest += dest_channels;
416 composite_line_22_4a4 (int *weights, int n_x, int n_y,
417 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
418 guchar **src, int src_channels, gboolean src_has_alpha,
419 int x_init, int x_step, int src_width,
420 int check_size, guint32 color1, guint32 color2)
423 guchar *src0 = src[0];
424 guchar *src1 = src[1];
426 g_return_val_if_fail (src_channels != 3, dest);
427 g_return_val_if_fail (src_has_alpha, dest);
429 while (dest < dest_end)
431 int x_scaled = x >> SCALE_SHIFT;
432 unsigned int r, g, b, a, ta;
437 q0 = src0 + x_scaled * 4;
438 q1 = src1 + x_scaled * 4;
440 pixel_weights = (int *)((char *)weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS - 4)) & (SUBSAMPLE_MASK << 4)));
442 w1 = pixel_weights[0];
443 w2 = pixel_weights[1];
444 w3 = pixel_weights[2];
445 w4 = pixel_weights[3];
470 dest[0] = ((0xff0000 - a) * dest[0] + r) >> 24;
471 dest[1] = ((0xff0000 - a) * dest[1] + g) >> 24;
472 dest[2] = ((0xff0000 - a) * dest[2] + b) >> 24;
484 composite_line_22_4a4_mmx_stub (int *weights, int n_x, int n_y,
485 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
486 guchar **src, int src_channels, gboolean src_has_alpha,
487 int x_init, int x_step, int src_width,
488 int check_size, guint32 color1, guint32 color2)
490 guint32 mmx_weights[16][8];
495 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
496 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
497 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
498 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
499 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
500 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
501 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
502 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
505 return pixops_composite_line_22_4a4_mmx (mmx_weights, dest, src[0], src[1], x_step, dest_end, x_init);
510 composite_pixel_color (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
511 int src_has_alpha, int check_size, guint32 color1, guint32 color2,
512 int r, int g, int b, int a)
514 int dest_r, dest_g, dest_b;
515 int check_shift = get_check_shift (check_size);
517 if ((dest_x >> check_shift) & 1)
519 dest_r = (color2 & 0xff0000) >> 16;
520 dest_g = (color2 & 0xff00) >> 8;
521 dest_b = color2 & 0xff;
525 dest_r = (color1 & 0xff0000) >> 16;
526 dest_g = (color1 & 0xff00) >> 8;
527 dest_b = color1 & 0xff;
530 dest[0] = ((0xff0000 - a) * dest_r + r) >> 24;
531 dest[1] = ((0xff0000 - a) * dest_g + g) >> 24;
532 dest[2] = ((0xff0000 - a) * dest_b + b) >> 24;
536 else if (dest_channels == 4)
541 composite_line_color (int *weights, int n_x, int n_y,
542 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
543 guchar **src, int src_channels, gboolean src_has_alpha,
544 int x_init, int x_step, int src_width,
545 int check_size, guint32 color1, guint32 color2)
549 int check_shift = get_check_shift (check_size);
550 int dest_r1, dest_g1, dest_b1;
551 int dest_r2, dest_g2, dest_b2;
553 g_return_val_if_fail (check_size != 0, dest);
555 dest_r1 = (color1 & 0xff0000) >> 16;
556 dest_g1 = (color1 & 0xff00) >> 8;
557 dest_b1 = color1 & 0xff;
559 dest_r2 = (color2 & 0xff0000) >> 16;
560 dest_g2 = (color2 & 0xff00) >> 8;
561 dest_b2 = color2 & 0xff;
563 while (dest < dest_end)
565 int x_scaled = x >> SCALE_SHIFT;
566 unsigned int r = 0, g = 0, b = 0, a = 0;
569 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
571 for (i=0; i<n_y; i++)
573 guchar *q = src[i] + x_scaled * src_channels;
574 int *line_weights = pixel_weights + n_x * i;
576 for (j=0; j<n_x; j++)
581 ta = q[3] * line_weights[j];
583 ta = 0xff * line_weights[j];
594 if ((dest_x >> check_shift) & 1)
596 dest[0] = ((0xff0000 - a) * dest_r2 + r) >> 24;
597 dest[1] = ((0xff0000 - a) * dest_g2 + g) >> 24;
598 dest[2] = ((0xff0000 - a) * dest_b2 + b) >> 24;
602 dest[0] = ((0xff0000 - a) * dest_r1 + r) >> 24;
603 dest[1] = ((0xff0000 - a) * dest_g1 + g) >> 24;
604 dest[2] = ((0xff0000 - a) * dest_b1 + b) >> 24;
609 else if (dest_channels == 4)
612 dest += dest_channels;
622 composite_line_color_22_4a4_mmx_stub (int *weights, int n_x, int n_y,
623 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
624 guchar **src, int src_channels, gboolean src_has_alpha,
625 int x_init, int x_step, int src_width,
626 int check_size, guint32 color1, guint32 color2)
628 guint32 mmx_weights[16][8];
629 int check_shift = get_check_shift (check_size);
635 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
636 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
637 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
638 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
639 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
640 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
641 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
642 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
645 colors[0] = (color1 & 0xff00) << 8 | (color1 & 0xff);
646 colors[1] = (color1 & 0xff0000) >> 16;
647 colors[2] = (color2 & 0xff00) << 8 | (color2 & 0xff);
648 colors[3] = (color2 & 0xff0000) >> 16;
650 return pixops_composite_line_color_22_4a4_mmx (mmx_weights, dest, src[0], src[1], x_step, dest_end, x_init,
651 dest_x, check_shift, colors);
656 scale_pixel (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
657 int src_has_alpha, int check_size, guint32 color1, guint32 color2,
658 int r, int g, int b, int a)
679 dest[0] = (r + 0xffffff) >> 24;
680 dest[1] = (g + 0xffffff) >> 24;
681 dest[2] = (b + 0xffffff) >> 24;
689 scale_line (int *weights, int n_x, int n_y,
690 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
691 guchar **src, int src_channels, gboolean src_has_alpha,
692 int x_init, int x_step, int src_width,
693 int check_size, guint32 color1, guint32 color2)
698 while (dest < dest_end)
700 int x_scaled = x >> SCALE_SHIFT;
703 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
707 unsigned int r = 0, g = 0, b = 0, a = 0;
708 for (i=0; i<n_y; i++)
710 guchar *q = src[i] + x_scaled * src_channels;
711 int *line_weights = pixel_weights + n_x * i;
713 for (j=0; j<n_x; j++)
717 ta = q[3] * line_weights[j];
744 unsigned int r = 0, g = 0, b = 0;
745 for (i=0; i<n_y; i++)
747 guchar *q = src[i] + x_scaled * src_channels;
748 int *line_weights = pixel_weights + n_x * i;
750 for (j=0; j<n_x; j++)
752 unsigned int ta = line_weights[j];
762 dest[0] = (r + 0xffff) >> 16;
763 dest[1] = (g + 0xffff) >> 16;
764 dest[2] = (b + 0xffff) >> 16;
770 dest += dest_channels;
780 scale_line_22_33_mmx_stub (int *weights, int n_x, int n_y,
781 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
782 guchar **src, int src_channels, gboolean src_has_alpha,
783 int x_init, int x_step, int src_width,
784 int check_size, guint32 color1, guint32 color2)
786 guint32 mmx_weights[16][8];
791 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
792 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
793 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
794 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
795 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
796 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
797 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
798 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
801 return pixops_scale_line_22_33_mmx (mmx_weights, dest, src[0], src[1], x_step, dest_end, x_init);
806 scale_line_22_33 (int *weights, int n_x, int n_y,
807 guchar *dest, guchar *dest_end, int dest_channels, int dest_has_alpha,
808 guchar **src, int src_channels, gboolean src_has_alpha,
809 int x_init, int x_step, int src_width,
810 int check_size, guint32 color1, guint32 color2)
813 guchar *src0 = src[0];
814 guchar *src1 = src[1];
816 while (dest < dest_end)
818 unsigned int r, g, b;
819 int x_scaled = x >> SCALE_SHIFT;
824 q0 = src0 + x_scaled * 3;
825 q1 = src1 + x_scaled * 3;
827 pixel_weights = (int *)((char *)weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS - 4)) & (SUBSAMPLE_MASK << 4)));
829 w1 = pixel_weights[0];
830 w2 = pixel_weights[1];
831 w3 = pixel_weights[2];
832 w4 = pixel_weights[3];
863 process_pixel (int *weights, int n_x, int n_y,
864 guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
865 guchar **src, int src_channels, gboolean src_has_alpha,
866 int x_start, int src_width,
867 int check_size, guint32 color1, guint32 color2,
868 PixopsPixelFunc pixel_func)
870 unsigned int r = 0, g = 0, b = 0, a = 0;
873 for (i=0; i<n_y; i++)
875 int *line_weights = weights + n_x * i;
877 for (j=0; j<n_x; j++)
884 else if (x_start + j < src_width)
885 q = src[i] + (x_start + j) * src_channels;
887 q = src[i] + (src_width - 1) * src_channels;
890 ta = q[3] * line_weights[j];
892 ta = 0xff * line_weights[j];
901 (*pixel_func) (dest, dest_x, dest_channels, dest_has_alpha, src_has_alpha, check_size, color1, color2, r, g, b, a);
905 pixops_process (guchar *dest_buf,
912 gboolean dest_has_alpha,
913 const guchar *src_buf,
918 gboolean src_has_alpha,
926 PixopsFilter *filter,
927 PixopsLineFunc line_func,
928 PixopsPixelFunc pixel_func)
932 guchar **line_bufs = g_new (guchar *, filter->n_y);
934 int x_step = (1 << SCALE_SHIFT) / scale_x;
935 int y_step = (1 << SCALE_SHIFT) / scale_y;
938 int scaled_x_offset = floor (filter->x_offset * (1 << SCALE_SHIFT));
940 int run_end_index = (((src_width - filter->n_x + 1) << SCALE_SHIFT) - scaled_x_offset - 1) / x_step + 1 - render_x0;
941 int check_shift = check_size ? get_check_shift (check_size) : 0;
943 y = render_y0 * y_step + floor (filter->y_offset * (1 << SCALE_SHIFT));
944 for (i = 0; i < (render_y1 - render_y0); i++)
946 int y_start = y >> SCALE_SHIFT;
948 int *run_weights = filter->weights + ((y >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * filter->n_x * filter->n_y * SUBSAMPLE;
950 guint32 tcolor1, tcolor2;
952 guchar *outbuf = dest_buf + dest_rowstride * i;
953 guchar *outbuf_end = outbuf + dest_channels * (render_x1 - render_x0);
955 if (((i + check_y) >> check_shift) & 1)
966 for (j=0; j<filter->n_y; j++)
969 line_bufs[j] = (guchar *)src_buf;
970 else if (y_start < src_height)
971 line_bufs[j] = (guchar *)src_buf + src_rowstride * y_start;
973 line_bufs[j] = (guchar *)src_buf + src_rowstride * (src_height - 1);
979 x = render_x0 * x_step + scaled_x_offset;
980 x_start = x >> SCALE_SHIFT;
982 while (x_start < 0 && outbuf < outbuf_end)
984 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->n_x * filter->n_y), filter->n_x, filter->n_y,
985 outbuf, dest_x, dest_channels, dest_has_alpha,
986 line_bufs, src_channels, src_has_alpha,
987 x >> SCALE_SHIFT, src_width,
988 check_size, tcolor1, tcolor2, pixel_func);
991 x_start = x >> SCALE_SHIFT;
993 outbuf += dest_channels;
996 new_outbuf = (*line_func)(run_weights, filter->n_x, filter->n_y,
998 MIN (outbuf_end, dest_buf + dest_rowstride * i + run_end_index * dest_channels),
999 dest_channels, dest_has_alpha,
1000 line_bufs, src_channels, src_has_alpha,
1001 x, x_step, src_width, check_size, tcolor1, tcolor2);
1003 dest_x += (new_outbuf - outbuf) / dest_channels;
1005 x = dest_x * x_step + scaled_x_offset;
1006 outbuf = new_outbuf;
1008 while (outbuf < outbuf_end)
1010 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->n_x * filter->n_y), filter->n_x, filter->n_y,
1011 outbuf, dest_x, dest_channels, dest_has_alpha,
1012 line_bufs, src_channels, src_has_alpha,
1013 x >> SCALE_SHIFT, src_width,
1014 check_size, tcolor1, tcolor2, pixel_func);
1018 outbuf += dest_channels;
1028 tile_make_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1030 int i_offset, j_offset;
1032 int n_x = ceil(1/x_scale + 1);
1033 int n_y = ceil(1/y_scale + 1);
1035 filter->x_offset = 0;
1036 filter->y_offset = 0;
1039 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1041 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1042 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1044 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1045 double x = (double)j_offset / 16;
1046 double y = (double)i_offset / 16;
1049 for (i = 0; i < n_y; i++)
1056 th = MIN(i+1, y + 1/y_scale) - y;
1062 if (y + 1/y_scale > i)
1063 th = MIN(i+1, y + 1/y_scale) - i;
1068 for (j = 0; j < n_x; j++)
1073 tw = MIN(j+1, x + 1/x_scale) - x;
1079 if (x + 1/x_scale > j)
1080 tw = MIN(j+1, x + 1/x_scale) - j;
1085 *(pixel_weights + n_x * i + j) = 65536 * tw * x_scale * th * y_scale * overall_alpha;
1092 bilinear_make_fast_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1094 int i_offset, j_offset;
1095 double *x_weights, *y_weights;
1098 if (x_scale > 1.0) /* Bilinear */
1101 filter->x_offset = 0.5 * (1/x_scale - 1);
1105 n_x = ceil(1.0 + 1.0/x_scale);
1106 filter->x_offset = 0.0;
1109 if (y_scale > 1.0) /* Bilinear */
1112 filter->y_offset = 0.5 * (1/y_scale - 1);
1116 n_y = ceil(1.0 + 1.0/y_scale);
1117 filter->y_offset = 0.0;
1122 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1124 x_weights = g_new (double, n_x);
1125 y_weights = g_new (double, n_y);
1127 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1128 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1130 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1131 double x = (double)j_offset / 16;
1132 double y = (double)i_offset / 16;
1135 if (x_scale > 1.0) /* Bilinear */
1137 for (i = 0; i < n_x; i++)
1139 x_weights[i] = ((i == 0) ? (1 - x) : x) / x_scale;
1144 for (i = 0; i < n_x; i++)
1149 x_weights[i] = MIN(i+1, x + 1/x_scale) - x;
1155 if (x + 1/x_scale > i)
1156 x_weights[i] = MIN(i+1, x + 1/x_scale) - i;
1163 if (y_scale > 1.0) /* Bilinear */
1165 for (i = 0; i < n_y; i++)
1167 y_weights[i] = ((i == 0) ? (1 - y) : y) / y_scale;
1172 for (i = 0; i < n_y; i++)
1177 y_weights[i] = MIN(i+1, y + 1/y_scale) - y;
1183 if (y + 1/y_scale > i)
1184 y_weights[i] = MIN(i+1, y + 1/y_scale) - i;
1191 for (i = 0; i < n_y; i++)
1192 for (j = 0; j < n_x; j++)
1193 *(pixel_weights + n_x * i + j) = 65536 * x_weights[j] * x_scale * y_weights[i] * y_scale * overall_alpha;
1201 bilinear_quadrant (double bx0, double bx1, double by0, double by1)
1203 double ax0, ax1, ay0, ay1;
1204 double x0, x1, y0, y1;
1216 x1 = MIN (ax1, bx1);
1226 x1 = MIN (ax1, bx1);
1237 y1 = MIN (ay1, by1);
1247 y1 = MIN (ay1, by1);
1253 return 0.25 * (x1*x1 - x0*x0) * (y1*y1 - y0*y0);
1257 bilinear_make_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1259 int i_offset, j_offset;
1261 int n_x = ceil(1/x_scale + 2.0);
1262 int n_y = ceil(1/y_scale + 2.0);
1264 filter->x_offset = -1.0;
1265 filter->y_offset = -1.0;
1269 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1271 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1272 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1274 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1275 double x = (double)j_offset / 16;
1276 double y = (double)i_offset / 16;
1279 for (i = 0; i < n_y; i++)
1280 for (j = 0; j < n_x; j++)
1284 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);
1285 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);
1286 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);
1287 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);
1289 *(pixel_weights + n_x * i + j) = 65536 * w * x_scale * y_scale * overall_alpha;
1295 pixops_composite_color (guchar *dest_buf,
1302 gboolean dest_has_alpha,
1303 const guchar *src_buf,
1308 gboolean src_has_alpha,
1311 GdkInterpType interp_type,
1319 PixopsFilter filter;
1320 PixopsLineFunc line_func;
1323 gboolean found_mmx = pixops_have_mmx();
1326 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1327 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1329 if (scale_x == 0 || scale_y == 0)
1332 if (!src_has_alpha && overall_alpha == 255)
1333 pixops_scale (dest_buf, render_x0, render_y0, render_x1, render_y1,
1334 dest_rowstride, dest_channels, dest_has_alpha,
1335 src_buf, src_width, src_height, src_rowstride, src_channels,
1336 src_has_alpha, scale_x, scale_y, interp_type);
1338 switch (interp_type)
1340 case GDK_INTERP_NEAREST:
1341 pixops_composite_color_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1342 dest_rowstride, dest_channels, dest_has_alpha,
1343 src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
1344 scale_x, scale_y, overall_alpha,
1345 check_x, check_y, check_size, color1, color2);
1348 case GDK_INTERP_TILES:
1349 tile_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1352 case GDK_INTERP_BILINEAR:
1353 bilinear_make_fast_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1356 case GDK_INTERP_HYPER:
1357 bilinear_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1362 if (filter.n_x == 2 && filter.n_y == 2 &&
1363 dest_channels == 4 && src_channels == 4 && src_has_alpha && !dest_has_alpha && found_mmx)
1364 line_func = composite_line_color_22_4a4_mmx_stub;
1367 line_func = composite_line_color;
1369 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1370 dest_rowstride, dest_channels, dest_has_alpha,
1371 src_buf, src_width, src_height, src_rowstride, src_channels,
1372 src_has_alpha, scale_x, scale_y, check_x, check_y, check_size, color1, color2,
1373 &filter, line_func, composite_pixel_color);
1375 g_free (filter.weights);
1379 pixops_composite (guchar *dest_buf,
1386 gboolean dest_has_alpha,
1387 const guchar *src_buf,
1392 gboolean src_has_alpha,
1395 GdkInterpType interp_type,
1398 PixopsFilter filter;
1399 PixopsLineFunc line_func;
1402 gboolean found_mmx = pixops_have_mmx();
1405 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1406 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1408 if (scale_x == 0 || scale_y == 0)
1411 if (!src_has_alpha && overall_alpha == 255)
1412 pixops_scale (dest_buf, render_x0, render_y0, render_x1, render_y1,
1413 dest_rowstride, dest_channels, dest_has_alpha,
1414 src_buf, src_width, src_height, src_rowstride, src_channels,
1415 src_has_alpha, scale_x, scale_y, interp_type);
1417 switch (interp_type)
1419 case GDK_INTERP_NEAREST:
1420 pixops_composite_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1421 dest_rowstride, dest_channels, dest_has_alpha,
1422 src_buf, src_width, src_height, src_rowstride, src_channels,
1423 src_has_alpha, scale_x, scale_y, overall_alpha);
1426 case GDK_INTERP_TILES:
1427 tile_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1430 case GDK_INTERP_BILINEAR:
1431 bilinear_make_fast_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1434 case GDK_INTERP_HYPER:
1435 bilinear_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1439 if (filter.n_x == 2 && filter.n_y == 2 &&
1440 dest_channels == 4 && src_channels == 4 && src_has_alpha && !dest_has_alpha)
1444 line_func = composite_line_22_4a4_mmx_stub;
1447 line_func = composite_line_22_4a4;
1450 line_func = composite_line;
1452 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1453 dest_rowstride, dest_channels, dest_has_alpha,
1454 src_buf, src_width, src_height, src_rowstride, src_channels,
1455 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
1456 &filter, line_func, composite_pixel);
1458 g_free (filter.weights);
1462 pixops_scale (guchar *dest_buf,
1469 gboolean dest_has_alpha,
1470 const guchar *src_buf,
1475 gboolean src_has_alpha,
1478 GdkInterpType interp_type)
1480 PixopsFilter filter;
1481 PixopsLineFunc line_func;
1484 gboolean found_mmx = pixops_have_mmx();
1487 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1488 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1489 g_return_if_fail (!(src_has_alpha && !dest_has_alpha));
1491 if (scale_x == 0 || scale_y == 0)
1494 switch (interp_type)
1496 case GDK_INTERP_NEAREST:
1497 pixops_scale_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1498 dest_rowstride, dest_channels, dest_has_alpha,
1499 src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
1503 case GDK_INTERP_TILES:
1504 tile_make_weights (&filter, scale_x, scale_y, 1.0);
1507 case GDK_INTERP_BILINEAR:
1508 bilinear_make_fast_weights (&filter, scale_x, scale_y, 1.0);
1511 case GDK_INTERP_HYPER:
1512 bilinear_make_weights (&filter, scale_x, scale_y, 1.0);
1517 if (filter.n_x == 2 && filter.n_y == 2 &&
1518 found_mmx && dest_channels == 3 && src_channels == 3)
1519 line_func = scale_line_22_33_mmx_stub;
1522 line_func = scale_line;
1524 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1525 dest_rowstride, dest_channels, dest_has_alpha,
1526 src_buf, src_width, src_height, src_rowstride, src_channels,
1527 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
1528 &filter, line_func, scale_pixel);
1530 g_free (filter.weights);