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 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 + render_x0 * dest_channels;
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) >> 8;
180 unsigned int a1 = dest[3];
181 unsigned int total = a0 + a1;
185 dest[0] = (a0 * src[0] + a1 * dest[0]) / (total);
186 dest[1] = (a0 * src[1] + a1 * dest[1]) / (total);
187 dest[2] = (a0 * src[2] + a1 * dest[2]) / (total);
188 dest[3] = total - ((a0 * a1 + 0xff) >> 8);
200 dest[0] = dest[0] + ((a0 * (p[0] - dest[0]) + 0xff) >> 8);
201 dest[1] = dest[1] + ((a0 * (p[1] - dest[1]) + 0xff) >> 8);
202 dest[2] = dest[2] + ((a0 * (p[2] - dest[2]) + 0xff) >> 8);
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 w = (((1 << 16) - a) * dest[3]) >> 8;
313 unsigned int total = a + w;
317 dest[0] = (r + w * dest[0]) / total;
318 dest[1] = (g + w * dest[1]) / total;
319 dest[2] = (b + w * dest[2]) / total;
320 dest[3] = (r * w) >> 16;
332 dest[0] = ((0xff0000 - a) * dest[0] + r) >> 24;
333 dest[1] = ((0xff0000 - a) * dest[1] + g) >> 24;
334 dest[2] = ((0xff0000 - a) * dest[2] + b) >> 24;
339 composite_line (int *weights, int n_x, int n_y,
340 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
341 guchar **src, int src_channels, gboolean src_has_alpha,
342 int x_init, int x_step, int src_width,
343 int check_size, guint32 color1, guint32 color2)
348 while (dest < dest_end)
350 int x_scaled = x >> SCALE_SHIFT;
351 unsigned int r = 0, g = 0, b = 0, a = 0;
354 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
356 for (i=0; i<n_y; i++)
358 guchar *q = src[i] + x_scaled * src_channels;
359 int *line_weights = pixel_weights + n_x * i;
361 for (j=0; j<n_x; j++)
366 ta = q[3] * line_weights[j];
368 ta = 0xff * line_weights[j];
381 unsigned int w = (((1 << 16) - a) * dest[3]) >> 8;
382 unsigned int total = a + w;
386 dest[0] = (r + w * dest[0]) / total;
387 dest[1] = (r + w * dest[1]) / total;
388 dest[2] = (r + w * dest[2]) / total;
389 dest[3] = (r * w) >> 16;
401 dest[0] = ((0xff0000 - a) * dest[0] + r) >> 24;
402 dest[1] = ((0xff0000 - a) * dest[1] + g) >> 24;
403 dest[2] = ((0xff0000 - a) * dest[2] + b) >> 24;
406 dest += dest_channels;
414 composite_line_22_4a4 (int *weights, int n_x, int n_y,
415 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
416 guchar **src, int src_channels, gboolean src_has_alpha,
417 int x_init, int x_step, int src_width,
418 int check_size, guint32 color1, guint32 color2)
421 guchar *src0 = src[0];
422 guchar *src1 = src[1];
424 g_return_val_if_fail (src_channels != 3, dest);
425 g_return_val_if_fail (src_has_alpha, dest);
427 while (dest < dest_end)
429 int x_scaled = x >> SCALE_SHIFT;
430 unsigned int r, g, b, a, ta;
435 q0 = src0 + x_scaled * 4;
436 q1 = src1 + x_scaled * 4;
438 pixel_weights = (int *)((char *)weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS - 4)) & (SUBSAMPLE_MASK << 4)));
440 w1 = pixel_weights[0];
441 w2 = pixel_weights[1];
442 w3 = pixel_weights[2];
443 w4 = pixel_weights[3];
468 dest[0] = ((0xff0000 - a) * dest[0] + r) >> 24;
469 dest[1] = ((0xff0000 - a) * dest[1] + g) >> 24;
470 dest[2] = ((0xff0000 - a) * dest[2] + b) >> 24;
482 composite_line_22_4a4_mmx_stub (int *weights, int n_x, int n_y,
483 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
484 guchar **src, int src_channels, gboolean src_has_alpha,
485 int x_init, int x_step, int src_width,
486 int check_size, guint32 color1, guint32 color2)
488 guint32 mmx_weights[16][8];
493 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
494 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
495 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
496 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
497 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
498 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
499 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
500 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
503 return pixops_composite_line_22_4a4_mmx (mmx_weights, dest, src[0], src[1], x_step, dest_end, x_init);
508 composite_pixel_color (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
509 int src_has_alpha, int check_size, guint32 color1, guint32 color2,
510 int r, int g, int b, int a)
512 int dest_r, dest_g, dest_b;
513 int check_shift = get_check_shift (check_size);
515 if ((dest_x >> check_shift) & 1)
517 dest_r = (color2 & 0xff0000) >> 16;
518 dest_g = (color2 & 0xff00) >> 8;
519 dest_b = color2 & 0xff;
523 dest_r = (color1 & 0xff0000) >> 16;
524 dest_g = (color1 & 0xff00) >> 8;
525 dest_b = color1 & 0xff;
528 dest[0] = ((0xff0000 - a) * dest_r + r) >> 24;
529 dest[1] = ((0xff0000 - a) * dest_g + g) >> 24;
530 dest[2] = ((0xff0000 - a) * dest_b + b) >> 24;
534 else if (dest_channels == 4)
539 composite_line_color (int *weights, int n_x, int n_y,
540 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
541 guchar **src, int src_channels, gboolean src_has_alpha,
542 int x_init, int x_step, int src_width,
543 int check_size, guint32 color1, guint32 color2)
547 int check_shift = get_check_shift (check_size);
548 int dest_r1, dest_g1, dest_b1;
549 int dest_r2, dest_g2, dest_b2;
551 g_return_val_if_fail (check_size != 0, dest);
553 dest_r1 = (color1 & 0xff0000) >> 16;
554 dest_g1 = (color1 & 0xff00) >> 8;
555 dest_b1 = color1 & 0xff;
557 dest_r2 = (color2 & 0xff0000) >> 16;
558 dest_g2 = (color2 & 0xff00) >> 8;
559 dest_b2 = color2 & 0xff;
561 while (dest < dest_end)
563 int x_scaled = x >> SCALE_SHIFT;
564 unsigned int r = 0, g = 0, b = 0, a = 0;
567 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
569 for (i=0; i<n_y; i++)
571 guchar *q = src[i] + x_scaled * src_channels;
572 int *line_weights = pixel_weights + n_x * i;
574 for (j=0; j<n_x; j++)
579 ta = q[3] * line_weights[j];
581 ta = 0xff * line_weights[j];
592 if ((dest_x >> check_shift) & 1)
594 dest[0] = ((0xff0000 - a) * dest_r2 + r) >> 24;
595 dest[1] = ((0xff0000 - a) * dest_g2 + g) >> 24;
596 dest[2] = ((0xff0000 - a) * dest_b2 + b) >> 24;
600 dest[0] = ((0xff0000 - a) * dest_r1 + r) >> 24;
601 dest[1] = ((0xff0000 - a) * dest_g1 + g) >> 24;
602 dest[2] = ((0xff0000 - a) * dest_b1 + b) >> 24;
607 else if (dest_channels == 4)
610 dest += dest_channels;
620 composite_line_color_22_4a4_mmx_stub (int *weights, int n_x, int n_y,
621 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
622 guchar **src, int src_channels, gboolean src_has_alpha,
623 int x_init, int x_step, int src_width,
624 int check_size, guint32 color1, guint32 color2)
626 guint32 mmx_weights[16][8];
627 int check_shift = get_check_shift (check_size);
633 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
634 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
635 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
636 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
637 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
638 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
639 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
640 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
643 colors[0] = (color1 & 0xff00) << 8 | (color1 & 0xff);
644 colors[1] = (color1 & 0xff0000) >> 16;
645 colors[2] = (color2 & 0xff00) << 8 | (color2 & 0xff);
646 colors[3] = (color2 & 0xff0000) >> 16;
648 return pixops_composite_line_color_22_4a4_mmx (mmx_weights, dest, src[0], src[1], x_step, dest_end, x_init,
649 dest_x, check_shift, colors);
654 scale_pixel (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
655 int src_has_alpha, int check_size, guint32 color1, guint32 color2,
656 int r, int g, int b, int a)
677 dest[0] = (r + 0xffffff) >> 24;
678 dest[1] = (g + 0xffffff) >> 24;
679 dest[2] = (b + 0xffffff) >> 24;
687 scale_line (int *weights, int n_x, int n_y,
688 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
689 guchar **src, int src_channels, gboolean src_has_alpha,
690 int x_init, int x_step, int src_width,
691 int check_size, guint32 color1, guint32 color2)
696 while (dest < dest_end)
698 int x_scaled = x >> SCALE_SHIFT;
701 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
705 unsigned int r = 0, g = 0, b = 0, a = 0;
706 for (i=0; i<n_y; i++)
708 guchar *q = src[i] + x_scaled * src_channels;
709 int *line_weights = pixel_weights + n_x * i;
711 for (j=0; j<n_x; j++)
715 ta = q[3] * line_weights[j];
742 unsigned int r = 0, g = 0, b = 0;
743 for (i=0; i<n_y; i++)
745 guchar *q = src[i] + x_scaled * src_channels;
746 int *line_weights = pixel_weights + n_x * i;
748 for (j=0; j<n_x; j++)
750 unsigned int ta = line_weights[j];
760 dest[0] = (r + 0xffff) >> 16;
761 dest[1] = (g + 0xffff) >> 16;
762 dest[2] = (b + 0xffff) >> 16;
768 dest += dest_channels;
778 scale_line_22_33_mmx_stub (int *weights, int n_x, int n_y,
779 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
780 guchar **src, int src_channels, gboolean src_has_alpha,
781 int x_init, int x_step, int src_width,
782 int check_size, guint32 color1, guint32 color2)
784 guint32 mmx_weights[16][8];
789 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
790 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
791 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
792 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
793 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
794 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
795 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
796 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
799 return pixops_scale_line_22_33_mmx (mmx_weights, dest, src[0], src[1], x_step, dest_end, x_init);
804 scale_line_22_33 (int *weights, int n_x, int n_y,
805 guchar *dest, guchar *dest_end, int dest_channels, int dest_has_alpha,
806 guchar **src, int src_channels, gboolean src_has_alpha,
807 int x_init, int x_step, int src_width,
808 int check_size, guint32 color1, guint32 color2)
811 guchar *src0 = src[0];
812 guchar *src1 = src[1];
814 while (dest < dest_end)
816 unsigned int r, g, b;
817 int x_scaled = x >> SCALE_SHIFT;
822 q0 = src0 + x_scaled * 3;
823 q1 = src1 + x_scaled * 3;
825 pixel_weights = (int *)((char *)weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS - 4)) & (SUBSAMPLE_MASK << 4)));
827 w1 = pixel_weights[0];
828 w2 = pixel_weights[1];
829 w3 = pixel_weights[2];
830 w4 = pixel_weights[3];
861 process_pixel (int *weights, int n_x, int n_y,
862 guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
863 guchar **src, int src_channels, gboolean src_has_alpha,
864 int x_start, int src_width,
865 int check_size, guint32 color1, guint32 color2,
866 PixopsPixelFunc pixel_func)
868 unsigned int r = 0, g = 0, b = 0, a = 0;
871 for (i=0; i<n_y; i++)
873 int *line_weights = weights + n_x * i;
875 for (j=0; j<n_x; j++)
882 else if (x_start + j < src_width)
883 q = src[i] + (x_start + j) * src_channels;
885 q = src[i] + (src_width - 1) * src_channels;
888 ta = q[3] * line_weights[j];
890 ta = 0xff * line_weights[j];
899 (*pixel_func) (dest, dest_x, dest_channels, dest_has_alpha, src_has_alpha, check_size, color1, color2, r, g, b, a);
903 pixops_process (guchar *dest_buf,
910 gboolean dest_has_alpha,
911 const guchar *src_buf,
916 gboolean src_has_alpha,
924 PixopsFilter *filter,
925 PixopsLineFunc line_func,
926 PixopsPixelFunc pixel_func)
930 guchar **line_bufs = g_new (guchar *, filter->n_y);
932 int x_step = (1 << SCALE_SHIFT) / scale_x;
933 int y_step = (1 << SCALE_SHIFT) / scale_y;
936 int scaled_x_offset = floor (filter->x_offset * (1 << SCALE_SHIFT));
938 int run_end_index = (((src_width - filter->n_x + 1) << SCALE_SHIFT) - scaled_x_offset - 1) / x_step + 1 - render_x0;
939 int check_shift = check_size ? get_check_shift (check_size) : 0;
941 y = render_y0 * y_step + floor (filter->y_offset * (1 << SCALE_SHIFT));
942 for (i = 0; i < (render_y1 - render_y0); i++)
944 int y_start = y >> SCALE_SHIFT;
946 int *run_weights = filter->weights + ((y >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * filter->n_x * filter->n_y * SUBSAMPLE;
948 guint32 tcolor1, tcolor2;
950 guchar *outbuf = dest_buf + dest_rowstride * i;
951 guchar *outbuf_end = outbuf + dest_channels * (render_x1 - render_x0);
953 if (((i + check_y) >> check_shift) & 1)
964 for (j=0; j<filter->n_y; j++)
967 line_bufs[j] = (guchar *)src_buf;
968 else if (y_start < src_height)
969 line_bufs[j] = (guchar *)src_buf + src_rowstride * y_start;
971 line_bufs[j] = (guchar *)src_buf + src_rowstride * (src_height - 1);
977 x = render_x0 * x_step + scaled_x_offset;
978 x_start = x >> SCALE_SHIFT;
980 while (x_start < 0 && outbuf < outbuf_end)
982 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->n_x * filter->n_y), filter->n_x, filter->n_y,
983 outbuf, dest_x, dest_channels, dest_has_alpha,
984 line_bufs, src_channels, src_has_alpha,
985 x >> SCALE_SHIFT, src_width,
986 check_size, tcolor1, tcolor2, pixel_func);
989 x_start = x >> SCALE_SHIFT;
991 outbuf += dest_channels;
994 new_outbuf = (*line_func)(run_weights, filter->n_x, filter->n_y,
996 MIN (outbuf_end, dest_buf + dest_rowstride * i + run_end_index * dest_channels),
997 dest_channels, dest_has_alpha,
998 line_bufs, src_channels, src_has_alpha,
999 x, x_step, src_width, check_size, tcolor1, tcolor2);
1001 dest_x += (new_outbuf - outbuf) / dest_channels;
1003 x = dest_x * x_step + scaled_x_offset;
1004 outbuf = new_outbuf;
1006 while (outbuf < outbuf_end)
1008 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->n_x * filter->n_y), filter->n_x, filter->n_y,
1009 outbuf, dest_x, dest_channels, dest_has_alpha,
1010 line_bufs, src_channels, src_has_alpha,
1011 x >> SCALE_SHIFT, src_width,
1012 check_size, tcolor1, tcolor2, pixel_func);
1016 outbuf += dest_channels;
1026 tile_make_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1028 int i_offset, j_offset;
1030 int n_x = ceil(1/x_scale + 1);
1031 int n_y = ceil(1/y_scale + 1);
1033 filter->x_offset = 0;
1034 filter->y_offset = 0;
1037 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1039 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1040 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1042 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1043 double x = (double)j_offset / 16;
1044 double y = (double)i_offset / 16;
1047 for (i = 0; i < n_y; i++)
1054 th = MIN(i+1, y + 1/y_scale) - y;
1060 if (y + 1/y_scale > i)
1061 th = MIN(i+1, y + 1/y_scale) - i;
1066 for (j = 0; j < n_x; j++)
1071 tw = MIN(j+1, x + 1/x_scale) - x;
1077 if (x + 1/x_scale > j)
1078 tw = MIN(j+1, x + 1/x_scale) - j;
1083 *(pixel_weights + n_x * i + j) = 65536 * tw * x_scale * th * y_scale * overall_alpha;
1090 bilinear_make_fast_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1092 int i_offset, j_offset;
1093 double *x_weights, *y_weights;
1096 if (x_scale > 1.0) /* Bilinear */
1099 filter->x_offset = 0.5 * (1/x_scale - 1);
1103 n_x = ceil(1.0 + 1.0/x_scale);
1104 filter->x_offset = 0.0;
1107 if (y_scale > 1.0) /* Bilinear */
1110 filter->y_offset = 0.5 * (1/y_scale - 1);
1114 n_y = ceil(1.0 + 1.0/y_scale);
1115 filter->y_offset = 0.0;
1120 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1122 x_weights = g_new (double, n_x);
1123 y_weights = g_new (double, n_y);
1125 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1126 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1128 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1129 double x = (double)j_offset / 16;
1130 double y = (double)i_offset / 16;
1133 if (x_scale > 1.0) /* Bilinear */
1135 for (i = 0; i < n_x; i++)
1137 x_weights[i] = ((i == 0) ? (1 - x) : x) / x_scale;
1142 for (i = 0; i < n_x; i++)
1147 x_weights[i] = MIN(i+1, x + 1/x_scale) - x;
1153 if (x + 1/x_scale > i)
1154 x_weights[i] = MIN(i+1, x + 1/x_scale) - i;
1161 if (y_scale > 1.0) /* Bilinear */
1163 for (i = 0; i < n_y; i++)
1165 y_weights[i] = ((i == 0) ? (1 - y) : y) / y_scale;
1170 for (i = 0; i < n_y; i++)
1175 y_weights[i] = MIN(i+1, y + 1/y_scale) - y;
1181 if (y + 1/y_scale > i)
1182 y_weights[i] = MIN(i+1, y + 1/y_scale) - i;
1189 for (i = 0; i < n_y; i++)
1190 for (j = 0; j < n_x; j++)
1191 *(pixel_weights + n_x * i + j) = 65536 * x_weights[j] * x_scale * y_weights[i] * y_scale * overall_alpha;
1199 bilinear_quadrant (double bx0, double bx1, double by0, double by1)
1201 double ax0, ax1, ay0, ay1;
1202 double x0, x1, y0, y1;
1214 x1 = MIN (ax1, bx1);
1224 x1 = MIN (ax1, bx1);
1235 y1 = MIN (ay1, by1);
1245 y1 = MIN (ay1, by1);
1251 return 0.25 * (x1*x1 - x0*x0) * (y1*y1 - y0*y0);
1255 bilinear_make_weights (PixopsFilter *filter, double x_scale, double y_scale, double overall_alpha)
1257 int i_offset, j_offset;
1259 int n_x = ceil(1/x_scale + 2.0);
1260 int n_y = ceil(1/y_scale + 2.0);
1262 filter->x_offset = -1.0;
1263 filter->y_offset = -1.0;
1267 filter->weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1269 for (i_offset=0; i_offset<SUBSAMPLE; i_offset++)
1270 for (j_offset=0; j_offset<SUBSAMPLE; j_offset++)
1272 int *pixel_weights = filter->weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1273 double x = (double)j_offset / 16;
1274 double y = (double)i_offset / 16;
1277 for (i = 0; i < n_y; i++)
1278 for (j = 0; j < n_x; j++)
1282 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);
1283 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);
1284 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);
1285 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);
1287 *(pixel_weights + n_x * i + j) = 65536 * w * x_scale * y_scale * overall_alpha;
1293 pixops_composite_color (guchar *dest_buf,
1300 gboolean dest_has_alpha,
1301 const guchar *src_buf,
1306 gboolean src_has_alpha,
1309 GdkInterpType interp_type,
1317 PixopsFilter filter;
1318 PixopsLineFunc line_func;
1321 gboolean found_mmx = pixops_have_mmx();
1324 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1325 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1327 if (scale_x == 0 || scale_y == 0)
1330 if (!src_has_alpha && overall_alpha == 255)
1331 pixops_scale (dest_buf, render_x0, render_y0, render_x1, render_y1,
1332 dest_rowstride, dest_channels, dest_has_alpha,
1333 src_buf, src_width, src_height, src_rowstride, src_channels,
1334 src_has_alpha, scale_x, scale_y, interp_type);
1336 switch (interp_type)
1338 case GDK_INTERP_NEAREST:
1339 pixops_composite_color_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1340 dest_rowstride, dest_channels, dest_has_alpha,
1341 src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
1342 scale_x, scale_y, overall_alpha,
1343 check_x, check_y, check_size, color1, color2);
1346 case GDK_INTERP_TILES:
1347 tile_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1350 case GDK_INTERP_BILINEAR:
1351 bilinear_make_fast_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1354 case GDK_INTERP_HYPER:
1355 bilinear_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1360 if (filter.n_x == 2 && filter.n_y == 2 &&
1361 dest_channels == 4 && src_channels == 4 && src_has_alpha && !dest_has_alpha && found_mmx)
1362 line_func = composite_line_color_22_4a4_mmx_stub;
1365 line_func = composite_line_color;
1367 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1368 dest_rowstride, dest_channels, dest_has_alpha,
1369 src_buf, src_width, src_height, src_rowstride, src_channels,
1370 src_has_alpha, scale_x, scale_y, check_x, check_y, check_size, color1, color2,
1371 &filter, line_func, composite_pixel_color);
1373 g_free (filter.weights);
1377 pixops_composite (guchar *dest_buf,
1384 gboolean dest_has_alpha,
1385 const guchar *src_buf,
1390 gboolean src_has_alpha,
1393 GdkInterpType interp_type,
1396 PixopsFilter filter;
1397 PixopsLineFunc line_func;
1400 gboolean found_mmx = pixops_have_mmx();
1403 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1404 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1406 if (scale_x == 0 || scale_y == 0)
1409 if (!src_has_alpha && overall_alpha == 255)
1410 pixops_scale (dest_buf, render_x0, render_y0, render_x1, render_y1,
1411 dest_rowstride, dest_channels, dest_has_alpha,
1412 src_buf, src_width, src_height, src_rowstride, src_channels,
1413 src_has_alpha, scale_x, scale_y, interp_type);
1415 switch (interp_type)
1417 case GDK_INTERP_NEAREST:
1418 pixops_composite_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1419 dest_rowstride, dest_channels, dest_has_alpha,
1420 src_buf, src_width, src_height, src_rowstride, src_channels,
1421 src_has_alpha, scale_x, scale_y, overall_alpha);
1424 case GDK_INTERP_TILES:
1425 tile_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1428 case GDK_INTERP_BILINEAR:
1429 bilinear_make_fast_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1432 case GDK_INTERP_HYPER:
1433 bilinear_make_weights (&filter, scale_x, scale_y, overall_alpha / 255.);
1437 if (filter.n_x == 2 && filter.n_y == 2 &&
1438 dest_channels == 4 && src_channels == 4 && src_has_alpha && !dest_has_alpha)
1442 line_func = composite_line_22_4a4_mmx_stub;
1445 line_func = composite_line_22_4a4;
1448 line_func = composite_line;
1450 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1451 dest_rowstride, dest_channels, dest_has_alpha,
1452 src_buf, src_width, src_height, src_rowstride, src_channels,
1453 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
1454 &filter, line_func, composite_pixel);
1456 g_free (filter.weights);
1460 pixops_scale (guchar *dest_buf,
1467 gboolean dest_has_alpha,
1468 const guchar *src_buf,
1473 gboolean src_has_alpha,
1476 GdkInterpType interp_type)
1478 PixopsFilter filter;
1479 PixopsLineFunc line_func;
1482 gboolean found_mmx = pixops_have_mmx();
1485 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1486 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1487 g_return_if_fail (!(src_has_alpha && !dest_has_alpha));
1489 if (scale_x == 0 || scale_y == 0)
1492 switch (interp_type)
1494 case GDK_INTERP_NEAREST:
1495 pixops_scale_nearest (dest_buf, render_x0, render_y0, render_x1, render_y1,
1496 dest_rowstride, dest_channels, dest_has_alpha,
1497 src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
1501 case GDK_INTERP_TILES:
1502 tile_make_weights (&filter, scale_x, scale_y, 1.0);
1505 case GDK_INTERP_BILINEAR:
1506 bilinear_make_fast_weights (&filter, scale_x, scale_y, 1.0);
1509 case GDK_INTERP_HYPER:
1510 bilinear_make_weights (&filter, scale_x, scale_y, 1.0);
1515 if (filter.n_x == 2 && filter.n_y == 2 &&
1516 found_mmx && dest_channels == 3 && src_channels == 3)
1517 line_func = scale_line_22_33_mmx_stub;
1520 line_func = scale_line;
1522 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1523 dest_rowstride, dest_channels, dest_has_alpha,
1524 src_buf, src_width, src_height, src_rowstride, src_channels,
1525 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
1526 &filter, line_func, scale_pixel);
1528 g_free (filter.weights);