2 * Copyright (C) 2000 Red Hat, Inc
3 * mediaLib integration Copyright (c) 2001-2007 Sun Microsystems, Inc.
4 * All rights reserved. (Brian Cameron, Dmitriy Demin, James Cheng,
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include "pixops-internal.h"
29 #define SUBSAMPLE_BITS 4
30 #define SUBSAMPLE (1 << SUBSAMPLE_BITS)
31 #define SUBSAMPLE_MASK ((1 << SUBSAMPLE_BITS)-1)
32 #define SCALE_SHIFT 16
35 _pixops_scale_real (guchar *dest_buf,
42 gboolean dest_has_alpha,
43 const guchar *src_buf,
48 gboolean src_has_alpha,
51 PixopsInterpType interp_type);
53 typedef struct _PixopsFilter PixopsFilter;
54 typedef struct _PixopsFilterDimension PixopsFilterDimension;
56 struct _PixopsFilterDimension
65 PixopsFilterDimension x;
66 PixopsFilterDimension y;
70 typedef guchar *(*PixopsLineFunc) (int *weights, int n_x, int n_y,
71 guchar *dest, int dest_x, guchar *dest_end,
72 int dest_channels, int dest_has_alpha,
73 guchar **src, int src_channels,
74 gboolean src_has_alpha, int x_init,
75 int x_step, int src_width, int check_size,
76 guint32 color1, guint32 color2);
77 typedef void (*PixopsPixelFunc) (guchar *dest, int dest_x, int dest_channels,
78 int dest_has_alpha, int src_has_alpha,
79 int check_size, guint32 color1,
81 guint r, guint g, guint b, guint a);
86 #include <mlib_image.h>
96 #if defined(HAVE_SYS_SYSTEMINFO_H)
97 #include <sys/systeminfo.h>
98 #elif defined(HAVE_SYS_SYSINFO_H)
99 #include <sys/sysinfo.h>
102 static void pixops_medialib_composite (guchar *dest_buf,
108 const guchar *src_buf,
116 int dest_region_width,
117 int dest_region_height,
122 PixopsInterpType interp_type,
125 static void pixops_medialib_scale (guchar *dest_buf,
131 const guchar *src_buf,
139 int dest_region_width,
140 int dest_region_height,
145 PixopsInterpType interp_type);
147 typedef struct _mlInterp mlInterp;
153 PixopsFilter po_filter;
157 static gboolean medialib_initialized = FALSE;
158 static gboolean use_medialib = TRUE;
161 * Sun mediaLib(tm) support.
163 * http://www.sun.com/processors/vis/mlib.html
167 _pixops_use_medialib ()
169 char *mlib_version_string;
173 medialib_initialized = TRUE;
175 if (getenv ("GDK_DISABLE_MEDIALIB"))
177 use_medialib = FALSE;
182 * The imaging functions we want to use were added in mediaLib version 2.
183 * So turn off mediaLib support if the user has an older version.
184 * mlib_version returns a string in this format:
186 * mediaLib:0210:20011101:v8plusa
187 * ^^^^^^^^ ^^^^ ^^^^^^^^ ^^^^^^^
188 * libname vers build ISALIST identifier
189 * date (in this case sparcv8plus+vis)
191 * The first 2 digits of the version are the major version. The 3rd digit
192 * is the minor version, and the 4th digit is the micro version. So the
193 * above string corresponds to version 2.1.0. In the following test we only
194 * care about the major version.
196 mlib_version_string = mlib_version ();
198 count = sysinfo (SI_ARCHITECTURE, &sys_info[0], 257);
202 if (strcmp (sys_info, "i386") == 0)
204 char *mlib_target_isa = &mlib_version_string[23];
207 * For x86 processors mediaLib generic C implementation
208 * does not give any performance advantage so disable it
210 if (strncmp (mlib_target_isa, "sse", 3) != 0)
212 use_medialib = FALSE;
217 * For x86 processors use of libumem conflicts with
218 * mediaLib, so avoid using it.
220 if ((dlsym (RTLD_DEFAULT, "umem_alloc") != NULL) ||
221 (dlsym (RTLD_PROBE, "umem_alloc") != NULL) ||
222 (dlsym (RTLD_NEXT, "umem_alloc") != NULL) ||
223 (dlsym (RTLD_SELF, "umem_alloc") != NULL))
225 use_medialib = FALSE;
232 /* Failed to get system architecture, disable mediaLib anyway */
233 use_medialib = FALSE;
240 get_check_shift (int check_size)
243 g_return_val_if_fail (check_size >= 0, 4);
245 while (!(check_size & 1))
255 pixops_scale_nearest (guchar *dest_buf,
262 gboolean dest_has_alpha,
263 const guchar *src_buf,
268 gboolean src_has_alpha,
274 int x_step = (1 << SCALE_SHIFT) / scale_x;
275 int y_step = (1 << SCALE_SHIFT) / scale_y;
276 int xmax, xstart, xstop, x_pos, y_pos;
279 #define INNER_LOOP(SRC_CHANNELS,DEST_CHANNELS,ASSIGN_PIXEL) \
280 xmax = x + (render_x1 - render_x0) * x_step; \
281 xstart = MIN (0, xmax); \
282 xstop = MIN (src_width << SCALE_SHIFT, xmax); \
283 p = src + (CLAMP (x, xstart, xstop) >> SCALE_SHIFT) * SRC_CHANNELS; \
287 dest += DEST_CHANNELS; \
292 p = src + (x >> SCALE_SHIFT) * SRC_CHANNELS; \
294 dest += DEST_CHANNELS; \
297 x_pos = x >> SCALE_SHIFT; \
298 p = src + CLAMP (x_pos, 0, src_width - 1) * SRC_CHANNELS; \
302 dest += DEST_CHANNELS; \
306 for (i = 0; i < (render_y1 - render_y0); i++)
310 y_pos = ((i + render_y0) * y_step + y_step / 2) >> SCALE_SHIFT;
311 y_pos = CLAMP (y_pos, 0, src_height - 1);
312 src = src_buf + y_pos * src_rowstride;
313 dest = dest_buf + i * dest_rowstride;
315 x = render_x0 * x_step + x_step / 2;
317 if (src_channels == 3)
319 if (dest_channels == 3)
321 INNER_LOOP (3, 3, dest[0]=p[0];dest[1]=p[1];dest[2]=p[2]);
325 INNER_LOOP (3, 4, dest[0]=p[0];dest[1]=p[1];dest[2]=p[2];dest[3]=0xff);
328 else if (src_channels == 4)
330 if (dest_channels == 3)
332 INNER_LOOP (4, 3, dest[0]=p[0];dest[1]=p[1];dest[2]=p[2]);
337 INNER_LOOP(4, 4, p32=(guint32*)dest;*p32=*((guint32*)p));
344 pixops_composite_nearest (guchar *dest_buf,
351 gboolean dest_has_alpha,
352 const guchar *src_buf,
357 gboolean src_has_alpha,
364 int x_step = (1 << SCALE_SHIFT) / scale_x;
365 int y_step = (1 << SCALE_SHIFT) / scale_y;
366 int xmax, xstart, xstop, x_pos, y_pos;
370 for (i = 0; i < (render_y1 - render_y0); i++)
374 y_pos = ((i + render_y0) * y_step + y_step / 2) >> SCALE_SHIFT;
375 y_pos = CLAMP (y_pos, 0, src_height - 1);
376 src = src_buf + y_pos * src_rowstride;
377 dest = dest_buf + i * dest_rowstride;
379 x = render_x0 * x_step + x_step / 2;
381 INNER_LOOP(src_channels, dest_channels,
383 a0 = (p[3] * overall_alpha) / 0xff;
401 unsigned int w0 = 0xff * a0;
402 unsigned int w1 = (0xff - a0) * dest[3];
403 unsigned int w = w0 + w1;
405 dest[0] = (w0 * p[0] + w1 * dest[0]) / w;
406 dest[1] = (w0 * p[1] + w1 * dest[1]) / w;
407 dest[2] = (w0 * p[2] + w1 * dest[2]) / w;
412 unsigned int a1 = 0xff - a0;
415 tmp = a0 * p[0] + a1 * dest[0] + 0x80;
416 dest[0] = (tmp + (tmp >> 8)) >> 8;
417 tmp = a0 * p[1] + a1 * dest[1] + 0x80;
418 dest[1] = (tmp + (tmp >> 8)) >> 8;
419 tmp = a0 * p[2] + a1 * dest[2] + 0x80;
420 dest[2] = (tmp + (tmp >> 8)) >> 8;
429 pixops_composite_color_nearest (guchar *dest_buf,
436 gboolean dest_has_alpha,
437 const guchar *src_buf,
442 gboolean src_has_alpha,
454 int x_step = (1 << SCALE_SHIFT) / scale_x;
455 int y_step = (1 << SCALE_SHIFT) / scale_y;
456 int r1, g1, b1, r2, g2, b2;
457 int check_shift = get_check_shift (check_size);
458 int xmax, xstart, xstop, x_pos, y_pos;
462 for (i = 0; i < (render_y1 - render_y0); i++)
466 y_pos = ((i + render_y0) * y_step + y_step / 2) >> SCALE_SHIFT;
467 y_pos = CLAMP (y_pos, 0, src_height - 1);
468 src = src_buf + y_pos * src_rowstride;
469 dest = dest_buf + i * dest_rowstride;
471 x = render_x0 * x_step + x_step / 2;
474 if (((i + check_y) >> check_shift) & 1)
476 r1 = (color2 & 0xff0000) >> 16;
477 g1 = (color2 & 0xff00) >> 8;
480 r2 = (color1 & 0xff0000) >> 16;
481 g2 = (color1 & 0xff00) >> 8;
486 r1 = (color1 & 0xff0000) >> 16;
487 g1 = (color1 & 0xff00) >> 8;
490 r2 = (color2 & 0xff0000) >> 16;
491 g2 = (color2 & 0xff00) >> 8;
496 INNER_LOOP(src_channels, dest_channels,
498 a0 = (p[3] * overall_alpha + 0xff) >> 8;
505 if (((j + check_x) >> check_shift) & 1)
526 if (((j + check_x) >> check_shift) & 1)
528 tmp = ((int) p[0] - r2) * a0;
529 dest[0] = r2 + ((tmp + (tmp >> 8) + 0x80) >> 8);
530 tmp = ((int) p[1] - g2) * a0;
531 dest[1] = g2 + ((tmp + (tmp >> 8) + 0x80) >> 8);
532 tmp = ((int) p[2] - b2) * a0;
533 dest[2] = b2 + ((tmp + (tmp >> 8) + 0x80) >> 8);
537 tmp = ((int) p[0] - r1) * a0;
538 dest[0] = r1 + ((tmp + (tmp >> 8) + 0x80) >> 8);
539 tmp = ((int) p[1] - g1) * a0;
540 dest[1] = g1 + ((tmp + (tmp >> 8) + 0x80) >> 8);
541 tmp = ((int) p[2] - b1) * a0;
542 dest[2] = b1 + ((tmp + (tmp >> 8) + 0x80) >> 8);
548 if (dest_channels == 4)
558 composite_pixel (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
559 int src_has_alpha, int check_size, guint32 color1, guint32 color2,
560 guint r, guint g, guint b, guint a)
564 unsigned int w0 = a - (a >> 8);
565 unsigned int w1 = ((0xff0000 - a) >> 8) * dest[3];
566 unsigned int w = w0 + w1;
570 dest[0] = (r - (r >> 8) + w1 * dest[0]) / w;
571 dest[1] = (g - (g >> 8) + w1 * dest[1]) / w;
572 dest[2] = (b - (b >> 8) + w1 * dest[2]) / w;
573 dest[3] = w / 0xff00;
585 dest[0] = (r + (0xff0000 - a) * dest[0]) / 0xff0000;
586 dest[1] = (g + (0xff0000 - a) * dest[1]) / 0xff0000;
587 dest[2] = (b + (0xff0000 - a) * dest[2]) / 0xff0000;
592 composite_line (int *weights, int n_x, int n_y,
593 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
594 guchar **src, int src_channels, gboolean src_has_alpha,
595 int x_init, int x_step, int src_width,
596 int check_size, guint32 color1, guint32 color2)
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];
634 unsigned int w0 = a - (a >> 8);
635 unsigned int w1 = ((0xff0000 - a) >> 8) * dest[3];
636 unsigned int w = w0 + w1;
640 dest[0] = (r - (r >> 8) + w1 * dest[0]) / w;
641 dest[1] = (g - (g >> 8) + w1 * dest[1]) / w;
642 dest[2] = (b - (b >> 8) + w1 * dest[2]) / w;
643 dest[3] = w / 0xff00;
655 dest[0] = (r + (0xff0000 - a) * dest[0]) / 0xff0000;
656 dest[1] = (g + (0xff0000 - a) * dest[1]) / 0xff0000;
657 dest[2] = (b + (0xff0000 - a) * dest[2]) / 0xff0000;
660 dest += dest_channels;
668 composite_line_22_4a4 (int *weights, int n_x, int n_y,
669 guchar *dest, int dest_x, guchar *dest_end, int dest_channels, int dest_has_alpha,
670 guchar **src, int src_channels, gboolean src_has_alpha,
671 int x_init, int x_step, int src_width,
672 int check_size, guint32 color1, guint32 color2)
675 guchar *src0 = src[0];
676 guchar *src1 = src[1];
678 g_return_val_if_fail (src_channels != 3, dest);
679 g_return_val_if_fail (src_has_alpha, dest);
681 while (dest < dest_end)
683 int x_scaled = x >> SCALE_SHIFT;
684 unsigned int r, g, b, a, ta;
689 q0 = src0 + x_scaled * 4;
690 q1 = src1 + x_scaled * 4;
692 pixel_weights = (int *)((char *)weights +
693 ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS - 4)) & (SUBSAMPLE_MASK << 4)));
695 w1 = pixel_weights[0];
696 w2 = pixel_weights[1];
697 w3 = pixel_weights[2];
698 w4 = pixel_weights[3];
723 dest[0] = ((0xff0000 - a) * dest[0] + r) >> 24;
724 dest[1] = ((0xff0000 - a) * dest[1] + g) >> 24;
725 dest[2] = ((0xff0000 - a) * dest[2] + b) >> 24;
737 composite_line_22_4a4_mmx_stub (int *weights, int n_x, int n_y, guchar *dest,
738 int dest_x, guchar *dest_end,
739 int dest_channels, int dest_has_alpha,
740 guchar **src, int src_channels,
741 gboolean src_has_alpha, int x_init,
742 int x_step, int src_width, int check_size,
743 guint32 color1, guint32 color2)
745 guint32 mmx_weights[16][8];
750 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
751 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
752 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
753 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
754 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
755 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
756 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
757 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
760 return _pixops_composite_line_22_4a4_mmx (mmx_weights, dest, src[0], src[1],
761 x_step, dest_end, x_init);
766 composite_pixel_color (guchar *dest, int dest_x, int dest_channels,
767 int dest_has_alpha, int src_has_alpha, int check_size,
768 guint32 color1, guint32 color2, guint r, guint g,
771 int dest_r, dest_g, dest_b;
772 int check_shift = get_check_shift (check_size);
774 if ((dest_x >> check_shift) & 1)
776 dest_r = (color2 & 0xff0000) >> 16;
777 dest_g = (color2 & 0xff00) >> 8;
778 dest_b = color2 & 0xff;
782 dest_r = (color1 & 0xff0000) >> 16;
783 dest_g = (color1 & 0xff00) >> 8;
784 dest_b = color1 & 0xff;
787 dest[0] = ((0xff0000 - a) * dest_r + r) >> 24;
788 dest[1] = ((0xff0000 - a) * dest_g + g) >> 24;
789 dest[2] = ((0xff0000 - a) * dest_b + b) >> 24;
793 else if (dest_channels == 4)
798 composite_line_color (int *weights, int n_x, int n_y, guchar *dest,
799 int dest_x, guchar *dest_end, int dest_channels,
800 int dest_has_alpha, guchar **src, int src_channels,
801 gboolean src_has_alpha, int x_init, int x_step,
802 int src_width, int check_size, guint32 color1,
807 int check_shift = get_check_shift (check_size);
808 int dest_r1, dest_g1, dest_b1;
809 int dest_r2, dest_g2, dest_b2;
811 g_return_val_if_fail (check_size != 0, dest);
813 dest_r1 = (color1 & 0xff0000) >> 16;
814 dest_g1 = (color1 & 0xff00) >> 8;
815 dest_b1 = color1 & 0xff;
817 dest_r2 = (color2 & 0xff0000) >> 16;
818 dest_g2 = (color2 & 0xff00) >> 8;
819 dest_b2 = color2 & 0xff;
821 while (dest < dest_end)
823 int x_scaled = x >> SCALE_SHIFT;
824 unsigned int r = 0, g = 0, b = 0, a = 0;
827 pixel_weights = weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
829 for (i=0; i<n_y; i++)
831 guchar *q = src[i] + x_scaled * src_channels;
832 int *line_weights = pixel_weights + n_x * i;
834 for (j=0; j<n_x; j++)
839 ta = q[3] * line_weights[j];
841 ta = 0xff * line_weights[j];
852 if ((dest_x >> check_shift) & 1)
854 dest[0] = ((0xff0000 - a) * dest_r2 + r) >> 24;
855 dest[1] = ((0xff0000 - a) * dest_g2 + g) >> 24;
856 dest[2] = ((0xff0000 - a) * dest_b2 + b) >> 24;
860 dest[0] = ((0xff0000 - a) * dest_r1 + r) >> 24;
861 dest[1] = ((0xff0000 - a) * dest_g1 + g) >> 24;
862 dest[2] = ((0xff0000 - a) * dest_b1 + b) >> 24;
867 else if (dest_channels == 4)
870 dest += dest_channels;
880 composite_line_color_22_4a4_mmx_stub (int *weights, int n_x, int n_y,
881 guchar *dest, int dest_x,
882 guchar *dest_end, int dest_channels,
883 int dest_has_alpha, guchar **src,
884 int src_channels, gboolean src_has_alpha,
885 int x_init, int x_step, int src_width,
886 int check_size, guint32 color1,
889 guint32 mmx_weights[16][8];
890 int check_shift = get_check_shift (check_size);
896 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
897 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
898 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
899 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
900 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
901 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
902 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
903 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
906 colors[0] = (color1 & 0xff00) << 8 | (color1 & 0xff);
907 colors[1] = (color1 & 0xff0000) >> 16;
908 colors[2] = (color2 & 0xff00) << 8 | (color2 & 0xff);
909 colors[3] = (color2 & 0xff0000) >> 16;
911 return _pixops_composite_line_color_22_4a4_mmx (mmx_weights, dest, src[0],
912 src[1], x_step, dest_end, x_init, dest_x, check_shift, colors);
917 scale_pixel (guchar *dest, int dest_x, int dest_channels, int dest_has_alpha,
918 int src_has_alpha, int check_size, guint32 color1, guint32 color2,
919 guint r, guint g, guint b, guint a)
940 dest[0] = (r + 0xffffff) >> 24;
941 dest[1] = (g + 0xffffff) >> 24;
942 dest[2] = (b + 0xffffff) >> 24;
950 scale_line (int *weights, int n_x, int n_y, guchar *dest, int dest_x,
951 guchar *dest_end, int dest_channels, int dest_has_alpha,
952 guchar **src, int src_channels, gboolean src_has_alpha, int x_init,
953 int x_step, int src_width, int check_size, guint32 color1,
959 while (dest < dest_end)
961 int x_scaled = x >> SCALE_SHIFT;
964 pixel_weights = weights +
965 ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * n_x * n_y;
969 unsigned int r = 0, g = 0, b = 0, a = 0;
970 for (i=0; i<n_y; i++)
972 guchar *q = src[i] + x_scaled * src_channels;
973 int *line_weights = pixel_weights + n_x * i;
975 for (j=0; j<n_x; j++)
979 ta = q[3] * line_weights[j];
1006 unsigned int r = 0, g = 0, b = 0;
1007 for (i=0; i<n_y; i++)
1009 guchar *q = src[i] + x_scaled * src_channels;
1010 int *line_weights = pixel_weights + n_x * i;
1012 for (j=0; j<n_x; j++)
1014 unsigned int ta = line_weights[j];
1024 dest[0] = (r + 0xffff) >> 16;
1025 dest[1] = (g + 0xffff) >> 16;
1026 dest[2] = (b + 0xffff) >> 16;
1032 dest += dest_channels;
1042 scale_line_22_33_mmx_stub (int *weights, int n_x, int n_y, guchar *dest,
1043 int dest_x, guchar *dest_end, int dest_channels,
1044 int dest_has_alpha, guchar **src, int src_channels,
1045 gboolean src_has_alpha, int x_init, int x_step,
1046 int src_width, int check_size, guint32 color1,
1049 guint32 mmx_weights[16][8];
1052 for (j=0; j<16; j++)
1054 mmx_weights[j][0] = 0x00010001 * (weights[4*j] >> 8);
1055 mmx_weights[j][1] = 0x00010001 * (weights[4*j] >> 8);
1056 mmx_weights[j][2] = 0x00010001 * (weights[4*j + 1] >> 8);
1057 mmx_weights[j][3] = 0x00010001 * (weights[4*j + 1] >> 8);
1058 mmx_weights[j][4] = 0x00010001 * (weights[4*j + 2] >> 8);
1059 mmx_weights[j][5] = 0x00010001 * (weights[4*j + 2] >> 8);
1060 mmx_weights[j][6] = 0x00010001 * (weights[4*j + 3] >> 8);
1061 mmx_weights[j][7] = 0x00010001 * (weights[4*j + 3] >> 8);
1064 return _pixops_scale_line_22_33_mmx (mmx_weights, dest, src[0], src[1],
1065 x_step, dest_end, x_init);
1067 #endif /* USE_MMX */
1070 scale_line_22_33 (int *weights, int n_x, int n_y, guchar *dest, int dest_x,
1071 guchar *dest_end, int dest_channels, int dest_has_alpha,
1072 guchar **src, int src_channels, gboolean src_has_alpha,
1073 int x_init, int x_step, int src_width,
1074 int check_size, guint32 color1, guint32 color2)
1077 guchar *src0 = src[0];
1078 guchar *src1 = src[1];
1080 while (dest < dest_end)
1082 unsigned int r, g, b;
1083 int x_scaled = x >> SCALE_SHIFT;
1088 q0 = src0 + x_scaled * 3;
1089 q1 = src1 + x_scaled * 3;
1091 pixel_weights = weights +
1092 ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * 4;
1094 w1 = pixel_weights[0];
1095 w2 = pixel_weights[1];
1096 w3 = pixel_weights[2];
1097 w4 = pixel_weights[3];
1115 dest[0] = (r + 0x8000) >> 16;
1116 dest[1] = (g + 0x8000) >> 16;
1117 dest[2] = (b + 0x8000) >> 16;
1127 process_pixel (int *weights, int n_x, int n_y, guchar *dest, int dest_x,
1128 int dest_channels, int dest_has_alpha, guchar **src,
1129 int src_channels, gboolean src_has_alpha, int x_start,
1130 int src_width, int check_size, guint32 color1, guint32 color2,
1131 PixopsPixelFunc pixel_func)
1133 unsigned int r = 0, g = 0, b = 0, a = 0;
1136 for (i=0; i<n_y; i++)
1138 int *line_weights = weights + n_x * i;
1140 for (j=0; j<n_x; j++)
1145 if (x_start + j < 0)
1147 else if (x_start + j < src_width)
1148 q = src[i] + (x_start + j) * src_channels;
1150 q = src[i] + (src_width - 1) * src_channels;
1153 ta = q[3] * line_weights[j];
1155 ta = 0xff * line_weights[j];
1164 (*pixel_func) (dest, dest_x, dest_channels, dest_has_alpha, src_has_alpha,
1165 check_size, color1, color2, r, g, b, a);
1169 correct_total (int *weights,
1173 double overall_alpha)
1175 int correction = (int)(0.5 + 65536 * overall_alpha) - total;
1176 int remaining, c, d, i;
1178 if (correction != 0)
1180 remaining = correction;
1181 for (d = 1, c = correction; c != 0 && remaining != 0; d++, c = correction / d)
1182 for (i = n_x * n_y - 1; i >= 0 && c != 0 && remaining != 0; i--)
1183 if (*(weights + i) + c >= 0)
1185 *(weights + i) += c;
1187 if ((0 < remaining && remaining < c) ||
1188 (0 > remaining && remaining > c))
1195 make_filter_table (PixopsFilter *filter)
1197 int i_offset, j_offset;
1198 int n_x = filter->x.n;
1199 int n_y = filter->y.n;
1200 int *weights = g_new (int, SUBSAMPLE * SUBSAMPLE * n_x * n_y);
1202 for (i_offset=0; i_offset < SUBSAMPLE; i_offset++)
1203 for (j_offset=0; j_offset < SUBSAMPLE; j_offset++)
1206 int *pixel_weights = weights + ((i_offset*SUBSAMPLE) + j_offset) * n_x * n_y;
1210 for (i=0; i < n_y; i++)
1211 for (j=0; j < n_x; j++)
1213 weight = filter->x.weights[(j_offset * n_x) + j] *
1214 filter->y.weights[(i_offset * n_y) + i] *
1215 filter->overall_alpha * 65536 + 0.5;
1217 total += (int)weight;
1219 *(pixel_weights + n_x * i + j) = weight;
1222 correct_total (pixel_weights, n_x, n_y, total, filter->overall_alpha);
1229 pixops_process (guchar *dest_buf,
1236 gboolean dest_has_alpha,
1237 const guchar *src_buf,
1242 gboolean src_has_alpha,
1250 PixopsFilter *filter,
1251 PixopsLineFunc line_func,
1252 PixopsPixelFunc pixel_func)
1255 int x, y; /* X and Y position in source (fixed_point) */
1257 guchar **line_bufs = g_new (guchar *, filter->y.n);
1258 int *filter_weights = make_filter_table (filter);
1260 int x_step = (1 << SCALE_SHIFT) / scale_x; /* X step in source (fixed point) */
1261 int y_step = (1 << SCALE_SHIFT) / scale_y; /* Y step in source (fixed point) */
1263 int check_shift = check_size ? get_check_shift (check_size) : 0;
1265 int scaled_x_offset = floor (filter->x.offset * (1 << SCALE_SHIFT));
1267 /* Compute the index where we run off the end of the source buffer. The
1268 * furthest source pixel we access at index i is:
1270 * ((render_x0 + i) * x_step + scaled_x_offset) >> SCALE_SHIFT + filter->x.n - 1
1272 * So, run_end_index is the smallest i for which this pixel is src_width,
1275 * (i + render_x0) * x_step >= ((src_width - filter->x.n + 1) << SCALE_SHIFT) - scaled_x_offset
1278 #define MYDIV(a,b) ((a) > 0 ? (a) / (b) : ((a) - (b) + 1) / (b)) /* Division so that -1/5 = -1 */
1280 int run_end_x = (((src_width - filter->x.n + 1) << SCALE_SHIFT) - scaled_x_offset);
1281 int run_end_index = MYDIV (run_end_x + x_step - 1, x_step) - render_x0;
1282 run_end_index = MIN (run_end_index, render_x1 - render_x0);
1284 y = render_y0 * y_step + floor (filter->y.offset * (1 << SCALE_SHIFT));
1285 for (i = 0; i < (render_y1 - render_y0); i++)
1288 int y_start = y >> SCALE_SHIFT;
1290 int *run_weights = filter_weights +
1291 ((y >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) *
1292 filter->x.n * filter->y.n * SUBSAMPLE;
1294 guint32 tcolor1, tcolor2;
1296 guchar *outbuf = dest_buf + dest_rowstride * i;
1297 guchar *outbuf_end = outbuf + dest_channels * (render_x1 - render_x0);
1299 if (((i + check_y) >> check_shift) & 1)
1310 for (j=0; j<filter->y.n; j++)
1313 line_bufs[j] = (guchar *)src_buf;
1314 else if (y_start < src_height)
1315 line_bufs[j] = (guchar *)src_buf + src_rowstride * y_start;
1317 line_bufs[j] = (guchar *)src_buf + src_rowstride * (src_height - 1);
1323 x = render_x0 * x_step + scaled_x_offset;
1324 x_start = x >> SCALE_SHIFT;
1326 while (x_start < 0 && outbuf < outbuf_end)
1328 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->x.n * filter->y.n), filter->x.n, filter->y.n,
1329 outbuf, dest_x, dest_channels, dest_has_alpha,
1330 line_bufs, src_channels, src_has_alpha,
1331 x >> SCALE_SHIFT, src_width,
1332 check_size, tcolor1, tcolor2, pixel_func);
1335 x_start = x >> SCALE_SHIFT;
1337 outbuf += dest_channels;
1340 new_outbuf = (*line_func) (run_weights, filter->x.n, filter->y.n,
1341 outbuf, dest_x, dest_buf + dest_rowstride *
1342 i + run_end_index * dest_channels,
1343 dest_channels, dest_has_alpha,
1344 line_bufs, src_channels, src_has_alpha,
1345 x, x_step, src_width, check_size, tcolor1,
1348 dest_x += (new_outbuf - outbuf) / dest_channels;
1350 x = (dest_x - check_x + render_x0) * x_step + scaled_x_offset;
1351 outbuf = new_outbuf;
1353 while (outbuf < outbuf_end)
1355 process_pixel (run_weights + ((x >> (SCALE_SHIFT - SUBSAMPLE_BITS)) & SUBSAMPLE_MASK) * (filter->x.n * filter->y.n), filter->x.n, filter->y.n,
1356 outbuf, dest_x, dest_channels, dest_has_alpha,
1357 line_bufs, src_channels, src_has_alpha,
1358 x >> SCALE_SHIFT, src_width,
1359 check_size, tcolor1, tcolor2, pixel_func);
1363 outbuf += dest_channels;
1370 g_free (filter_weights);
1373 /* Compute weights for reconstruction by replication followed by
1374 * sampling with a box filter
1377 tile_make_weights (PixopsFilterDimension *dim,
1380 int n = ceil (1 / scale + 1);
1381 double *pixel_weights = g_new (double, SUBSAMPLE * n);
1387 dim->weights = pixel_weights;
1389 for (offset = 0; offset < SUBSAMPLE; offset++)
1391 double x = (double)offset / SUBSAMPLE;
1392 double a = x + 1 / scale;
1394 for (i = 0; i < n; i++)
1399 *(pixel_weights++) = (MIN (i + 1, a) - x) * scale;
1401 *(pixel_weights++) = 0;
1406 *(pixel_weights++) = (MIN (i + 1, a) - i) * scale;
1408 *(pixel_weights++) = 0;
1414 /* Compute weights for a filter that, for minification
1415 * is the same as 'tiles', and for magnification, is bilinear
1416 * reconstruction followed by a sampling with a delta function.
1419 bilinear_magnify_make_weights (PixopsFilterDimension *dim,
1422 double *pixel_weights;
1427 if (scale > 1.0) /* Linear */
1430 dim->offset = 0.5 * (1 / scale - 1);
1434 n = ceil (1.0 + 1.0 / scale);
1439 dim->weights = g_new (double, SUBSAMPLE * n);
1441 pixel_weights = dim->weights;
1443 for (offset=0; offset < SUBSAMPLE; offset++)
1445 double x = (double)offset / SUBSAMPLE;
1447 if (scale > 1.0) /* Linear */
1449 for (i = 0; i < n; i++)
1450 *(pixel_weights++) = (((i == 0) ? (1 - x) : x) / scale) * scale;
1454 double a = x + 1 / scale;
1457 * ---------|--.-|----|--.-|------- SRC
1458 * ------------|---------|--------- DEST
1460 for (i = 0; i < n; i++)
1465 *(pixel_weights++) = (MIN (i + 1, a) - x) * scale;
1467 *(pixel_weights++) = 0;
1472 *(pixel_weights++) = (MIN (i + 1, a) - i) * scale;
1474 *(pixel_weights++) = 0;
1481 /* Computes the integral from b0 to b1 of
1483 * f(x) = x; 0 <= x < 1
1484 * f(x) = 0; otherwise
1486 * We combine two of these to compute the convolution of
1487 * a box filter with a triangular spike.
1490 linear_box_half (double b0, double b1)
1519 return 0.5 * (x1*x1 - x0*x0);
1522 /* Compute weights for reconstructing with bilinear
1523 * interpolation, then sampling with a box filter
1526 bilinear_box_make_weights (PixopsFilterDimension *dim,
1529 int n = ceil (1/scale + 3.0);
1530 double *pixel_weights = g_new (double, SUBSAMPLE * n);
1536 dim->weights = pixel_weights;
1538 for (offset = 0; offset < SUBSAMPLE; offset++)
1540 double x = (double)offset / SUBSAMPLE;
1541 double a = x + 1 / scale;
1543 for (i = 0; i < n; i++)
1545 w = linear_box_half (0.5 + i - a, 0.5 + i - x);
1546 w += linear_box_half (1.5 + x - i, 1.5 + a - i);
1548 *(pixel_weights++) = w * scale;
1554 make_weights (PixopsFilter *filter,
1555 PixopsInterpType interp_type,
1559 switch (interp_type)
1561 case PIXOPS_INTERP_NEAREST:
1562 g_assert_not_reached ();
1565 case PIXOPS_INTERP_TILES:
1566 tile_make_weights (&filter->x, scale_x);
1567 tile_make_weights (&filter->y, scale_y);
1570 case PIXOPS_INTERP_BILINEAR:
1571 bilinear_magnify_make_weights (&filter->x, scale_x);
1572 bilinear_magnify_make_weights (&filter->y, scale_y);
1575 case PIXOPS_INTERP_HYPER:
1576 bilinear_box_make_weights (&filter->x, scale_x);
1577 bilinear_box_make_weights (&filter->y, scale_y);
1583 _pixops_composite_color_real (guchar *dest_buf,
1590 gboolean dest_has_alpha,
1591 const guchar *src_buf,
1596 gboolean src_has_alpha,
1599 PixopsInterpType interp_type,
1607 PixopsFilter filter;
1608 PixopsLineFunc line_func;
1611 gboolean found_mmx = _pixops_have_mmx ();
1614 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1615 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1617 if (scale_x == 0 || scale_y == 0)
1620 if (interp_type == PIXOPS_INTERP_NEAREST)
1622 pixops_composite_color_nearest (dest_buf, render_x0, render_y0,
1623 render_x1, render_y1, dest_rowstride,
1624 dest_channels, dest_has_alpha, src_buf,
1625 src_width, src_height, src_rowstride,
1626 src_channels, src_has_alpha, scale_x,
1627 scale_y, overall_alpha, check_x, check_y,
1628 check_size, color1, color2);
1632 filter.overall_alpha = overall_alpha / 255.;
1633 make_weights (&filter, interp_type, scale_x, scale_y);
1636 if (filter.x.n == 2 && filter.y.n == 2 &&
1637 dest_channels == 4 && src_channels == 4 &&
1638 src_has_alpha && !dest_has_alpha && found_mmx)
1639 line_func = composite_line_color_22_4a4_mmx_stub;
1642 line_func = composite_line_color;
1644 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1645 dest_rowstride, dest_channels, dest_has_alpha,
1646 src_buf, src_width, src_height, src_rowstride, src_channels,
1647 src_has_alpha, scale_x, scale_y, check_x, check_y, check_size, color1, color2,
1648 &filter, line_func, composite_pixel_color);
1650 g_free (filter.x.weights);
1651 g_free (filter.y.weights);
1655 _pixops_composite_color (guchar *dest_buf,
1660 gboolean dest_has_alpha,
1661 const guchar *src_buf,
1666 gboolean src_has_alpha,
1669 int dest_region_width,
1670 int dest_region_height,
1675 PixopsInterpType interp_type,
1683 guchar *new_dest_buf;
1689 if (!src_has_alpha && overall_alpha == 255)
1691 _pixops_scale (dest_buf, dest_width, dest_height, dest_rowstride,
1692 dest_channels, dest_has_alpha, src_buf, src_width,
1693 src_height, src_rowstride, src_channels, src_has_alpha,
1694 dest_x, dest_y, dest_region_width, dest_region_height,
1695 offset_x, offset_y, scale_x, scale_y, interp_type);
1699 new_dest_buf = dest_buf + dest_y * dest_rowstride + dest_x *
1701 render_x0 = dest_x - offset_x;
1702 render_y0 = dest_y - offset_y;
1703 render_x1 = dest_x + dest_region_width - offset_x;
1704 render_y1 = dest_y + dest_region_height - offset_y;
1706 _pixops_composite_color_real (new_dest_buf, render_x0, render_y0, render_x1,
1707 render_y1, dest_rowstride, dest_channels,
1708 dest_has_alpha, src_buf, src_width,
1709 src_height, src_rowstride, src_channels,
1710 src_has_alpha, scale_x, scale_y,
1711 (PixopsInterpType)interp_type, overall_alpha,
1712 check_x, check_y, check_size, color1, color2);
1716 * _pixops_composite_real:
1717 * @dest_buf: pointer to location to store result
1718 * @render_x0: x0 of region of scaled source to store into @dest_buf
1719 * @render_y0: y0 of region of scaled source to store into @dest_buf
1720 * @render_x1: x1 of region of scaled source to store into @dest_buf
1721 * @render_y1: y1 of region of scaled source to store into @dest_buf
1722 * @dest_rowstride: rowstride of @dest_buf
1723 * @dest_channels: number of channels in @dest_buf
1724 * @dest_has_alpha: whether @dest_buf has alpha
1725 * @src_buf: pointer to source pixels
1726 * @src_width: width of source (used for clipping)
1727 * @src_height: height of source (used for clipping)
1728 * @src_rowstride: rowstride of source
1729 * @src_channels: number of channels in @src_buf
1730 * @src_has_alpha: whether @src_buf has alpha
1731 * @scale_x: amount to scale source by in X direction
1732 * @scale_y: amount to scale source by in Y direction
1733 * @interp_type: type of enumeration
1734 * @overall_alpha: overall alpha factor to multiply source by
1736 * Scale source buffer by scale_x / scale_y, then composite a given rectangle
1737 * of the result into the destination buffer.
1740 _pixops_composite_real (guchar *dest_buf,
1747 gboolean dest_has_alpha,
1748 const guchar *src_buf,
1753 gboolean src_has_alpha,
1756 PixopsInterpType interp_type,
1759 PixopsFilter filter;
1760 PixopsLineFunc line_func;
1763 gboolean found_mmx = _pixops_have_mmx ();
1766 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
1767 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
1769 if (scale_x == 0 || scale_y == 0)
1772 if (interp_type == PIXOPS_INTERP_NEAREST)
1774 pixops_composite_nearest (dest_buf, render_x0, render_y0, render_x1,
1775 render_y1, dest_rowstride, dest_channels,
1776 dest_has_alpha, src_buf, src_width, src_height,
1777 src_rowstride, src_channels, src_has_alpha,
1778 scale_x, scale_y, overall_alpha);
1782 filter.overall_alpha = overall_alpha / 255.;
1783 make_weights (&filter, interp_type, scale_x, scale_y);
1785 if (filter.x.n == 2 && filter.y.n == 2 && dest_channels == 4 &&
1786 src_channels == 4 && src_has_alpha && !dest_has_alpha)
1790 line_func = composite_line_22_4a4_mmx_stub;
1793 line_func = composite_line_22_4a4;
1796 line_func = composite_line;
1798 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
1799 dest_rowstride, dest_channels, dest_has_alpha,
1800 src_buf, src_width, src_height, src_rowstride, src_channels,
1801 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
1802 &filter, line_func, composite_pixel);
1804 g_free (filter.x.weights);
1805 g_free (filter.y.weights);
1809 _pixops_composite (guchar *dest_buf,
1815 const guchar *src_buf,
1823 int dest_region_width,
1824 int dest_region_height,
1829 PixopsInterpType interp_type,
1832 guchar *new_dest_buf;
1838 if (!src_has_alpha && overall_alpha == 255)
1840 _pixops_scale (dest_buf, dest_width, dest_height, dest_rowstride,
1841 dest_channels, dest_has_alpha, src_buf, src_width,
1842 src_height, src_rowstride, src_channels, src_has_alpha,
1843 dest_x, dest_y, dest_region_width, dest_region_height,
1844 offset_x, offset_y, scale_x, scale_y, interp_type);
1849 pixops_medialib_composite (dest_buf, dest_width, dest_height, dest_rowstride,
1850 dest_channels, dest_has_alpha, src_buf,
1851 src_width, src_height, src_rowstride,
1852 src_channels, src_has_alpha, dest_x, dest_y,
1853 dest_region_width, dest_region_height, offset_x,
1854 offset_y, scale_x, scale_y,
1855 (PixopsInterpType)interp_type, overall_alpha);
1859 new_dest_buf = dest_buf + dest_y * dest_rowstride + dest_x * dest_channels;
1860 render_x0 = dest_x - offset_x;
1861 render_y0 = dest_y - offset_y;
1862 render_x1 = dest_x + dest_region_width - offset_x;
1863 render_y1 = dest_y + dest_region_height - offset_y;
1865 _pixops_composite_real (new_dest_buf, render_x0, render_y0, render_x1,
1866 render_y1, dest_rowstride, dest_channels,
1867 dest_has_alpha, src_buf, src_width, src_height,
1868 src_rowstride, src_channels, src_has_alpha, scale_x,
1869 scale_y, (PixopsInterpType)interp_type,
1875 medialib_get_interpolation (mlInterp * ml_interp,
1876 PixopsInterpType interp_type,
1879 double overall_alpha)
1881 mlib_s32 leftPadding, topPadding;
1882 ml_interp->interp_table = NULL;
1885 * medialib 2.1 and later supports scaling with user-defined interpolation
1886 * tables, so this logic is used.
1888 * bilinear_magnify_make_weights builds an interpolation table of size 2x2 if
1889 * the scale factor >= 1.0 and "ceil (1.0 + 1.0/scale)" otherwise. These map
1890 * most closely to MLIB_BILINEAR, which uses an interpolation table of size
1893 * tile_make_weights builds an interpolation table of size 2x2 if the scale
1894 * factor >= 1.0 and "ceil (1.0 + 1.0/scale)" otherwise. These map most
1895 * closely to MLIB_BILINEAR, which uses an interpolation table of size 2x2.
1897 * bilinear_box_make_weights builds an interpolation table of size 4x4 if the
1898 * scale factor >= 1.0 and "ceil (1.0 + 1.0/scale)" otherwise. These map most
1899 * closely to MLIB_BICUBIC, which uses an interpolation table of size 4x4.
1901 * PIXOPS_INTERP_NEAREST calls pixops_scale_nearest which does not use an
1902 * interpolation table. This maps to MLIB_NEAREST.
1904 switch (interp_type)
1906 case PIXOPS_INTERP_BILINEAR:
1907 bilinear_magnify_make_weights (&(ml_interp->po_filter.x), scale_x);
1908 bilinear_magnify_make_weights (&(ml_interp->po_filter.y), scale_y);
1913 ml_interp->tx = 0.5 * (1 - scale_x);
1915 ml_interp->tx = 0.0;
1918 ml_interp->ty = 0.5 * (1 - scale_y);
1920 ml_interp->ty = 0.0;
1924 case PIXOPS_INTERP_TILES:
1925 tile_make_weights (&(ml_interp->po_filter.x), scale_x);
1926 tile_make_weights (&(ml_interp->po_filter.y), scale_y);
1929 ml_interp->tx = 0.5 * (1 - scale_x);
1930 ml_interp->ty = 0.5 * (1 - scale_y);
1933 case PIXOPS_INTERP_HYPER:
1934 bilinear_box_make_weights (&(ml_interp->po_filter.x), scale_x);
1935 bilinear_box_make_weights (&(ml_interp->po_filter.y), scale_y);
1938 ml_interp->tx = 0.5 * (1 - scale_x);
1939 ml_interp->ty = 0.5 * (1 - scale_y);
1942 case PIXOPS_INTERP_NEAREST:
1945 * Note that this function should not be called in the
1946 * PIXOPS_INTERP_NEAREST case since it does not use an interpolation
1949 g_assert_not_reached ();
1954 * If overall_alpha is not 1.0, then multiply the vectors built by the
1955 * sqrt (overall_alpha). This will cause overall_alpha to get evenly
1956 * blended across both axis.
1958 * Note there is no need to multiply the vectors built by the various
1959 * make-weight functions by sqrt (overall_alpha) since the make-weight
1960 * functions are called with overall_alpha hardcoded to 1.0.
1962 if (overall_alpha != 1.0)
1964 double sqrt_alpha = sqrt (overall_alpha);
1967 for (i=0; i < SUBSAMPLE * ml_interp->po_filter.x.n; i++)
1968 ml_interp->po_filter.x.weights[i] *= sqrt_alpha;
1969 for (i=0; i < SUBSAMPLE * ml_interp->po_filter.y.n; i++)
1970 ml_interp->po_filter.y.weights[i] *= sqrt_alpha;
1973 ml_interp->interp_table = (void *) mlib_ImageInterpTableCreate (MLIB_DOUBLE,
1974 ml_interp->po_filter.x.n, ml_interp->po_filter.y.n, leftPadding,
1975 topPadding, SUBSAMPLE_BITS, SUBSAMPLE_BITS, 8,
1976 ml_interp->po_filter.x.weights, ml_interp->po_filter.y.weights);
1978 g_free (ml_interp->po_filter.x.weights);
1979 g_free (ml_interp->po_filter.y.weights);
1983 pixops_medialib_composite (guchar *dest_buf,
1989 const guchar *src_buf,
1997 int dest_region_width,
1998 int dest_region_height,
2003 PixopsInterpType interp_type,
2007 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
2008 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
2010 if (scale_x == 0 || scale_y == 0)
2013 if (!medialib_initialized)
2014 _pixops_use_medialib ();
2018 /* Use non-mediaLib version */
2019 _pixops_composite_real (dest_buf + dest_y * dest_rowstride + dest_x *
2020 dest_channels, dest_x - offset_x, dest_y -
2021 offset_y, dest_x + dest_region_width - offset_x,
2022 dest_y + dest_region_height - offset_y,
2023 dest_rowstride, dest_channels, dest_has_alpha,
2024 src_buf, src_width, src_height, src_rowstride,
2025 src_channels, src_has_alpha, scale_x, scale_y,
2026 interp_type, overall_alpha);
2031 mlib_image img_src, img_dest;
2032 double ml_offset_x, ml_offset_y;
2034 if (!src_has_alpha && overall_alpha == 255 &&
2035 dest_channels <= src_channels)
2037 pixops_medialib_scale (dest_buf, dest_region_width,
2038 dest_region_height, dest_rowstride,
2039 dest_channels, dest_has_alpha, src_buf,
2040 src_width, src_height, src_rowstride,
2041 src_channels, src_has_alpha, dest_x, dest_y,
2042 dest_region_width, dest_region_height,
2043 offset_x, offset_y, scale_x, scale_y,
2048 mlib_ImageSetStruct (&img_src, MLIB_BYTE, src_channels,
2049 src_width, src_height, src_rowstride, src_buf);
2051 if (dest_x == 0 && dest_y == 0 &&
2052 dest_width == dest_region_width &&
2053 dest_height == dest_region_height)
2055 mlib_ImageSetStruct (&img_dest, MLIB_BYTE, dest_channels,
2056 dest_width, dest_height, dest_rowstride,
2061 mlib_u8 *data = dest_buf + (dest_y * dest_rowstride) +
2062 (dest_x * dest_channels);
2064 mlib_ImageSetStruct (&img_dest, MLIB_BYTE, dest_channels,
2065 dest_region_width, dest_region_height,
2066 dest_rowstride, data);
2069 ml_offset_x = floor (offset_x) - dest_x;
2070 ml_offset_y = floor (offset_y) - dest_y;
2072 if (interp_type == PIXOPS_INTERP_NEAREST)
2074 blend = src_has_alpha ? MLIB_BLEND_GTK_SRC_OVER2 : MLIB_BLEND_GTK_SRC;
2076 mlib_ImageZoomTranslateBlend (&img_dest,
2083 MLIB_EDGE_SRC_EXTEND_INDEF,
2090 blend = src_has_alpha ? MLIB_BLEND_GTK_SRC_OVER : MLIB_BLEND_GTK_SRC;
2092 if (interp_type == PIXOPS_INTERP_BILINEAR &&
2093 scale_x > 1.0 && scale_y > 1.0)
2095 mlib_ImageZoomTranslateBlend (&img_dest,
2102 MLIB_EDGE_SRC_EXTEND_INDEF,
2109 medialib_get_interpolation (&ml_interp, interp_type, scale_x,
2110 scale_y, overall_alpha/255.0);
2112 if (ml_interp.interp_table != NULL)
2114 mlib_ImageZoomTranslateTableBlend (&img_dest,
2118 ml_offset_x + ml_interp.tx,
2119 ml_offset_y + ml_interp.ty,
2120 ml_interp.interp_table,
2121 MLIB_EDGE_SRC_EXTEND_INDEF,
2124 mlib_ImageInterpTableDelete (ml_interp.interp_table);
2128 /* Should not happen - Use non-mediaLib version */
2129 _pixops_composite_real (dest_buf + dest_y * dest_rowstride +
2130 dest_x * dest_channels,
2131 dest_x - offset_x, dest_y - offset_y,
2132 dest_x + dest_region_width - offset_x,
2133 dest_y + dest_region_height - offset_y,
2134 dest_rowstride, dest_channels,
2135 dest_has_alpha, src_buf, src_width,
2136 src_height, src_rowstride,
2137 src_channels, src_has_alpha, scale_x,
2138 scale_y, interp_type, overall_alpha);
2147 _pixops_scale_real (guchar *dest_buf,
2154 gboolean dest_has_alpha,
2155 const guchar *src_buf,
2160 gboolean src_has_alpha,
2163 PixopsInterpType interp_type)
2165 PixopsFilter filter;
2166 PixopsLineFunc line_func;
2169 gboolean found_mmx = _pixops_have_mmx ();
2172 g_return_if_fail (!(dest_channels == 3 && dest_has_alpha));
2173 g_return_if_fail (!(src_channels == 3 && src_has_alpha));
2174 g_return_if_fail (!(src_has_alpha && !dest_has_alpha));
2176 if (scale_x == 0 || scale_y == 0)
2179 if (interp_type == PIXOPS_INTERP_NEAREST)
2181 pixops_scale_nearest (dest_buf, render_x0, render_y0, render_x1,
2182 render_y1, dest_rowstride, dest_channels,
2183 dest_has_alpha, src_buf, src_width, src_height,
2184 src_rowstride, src_channels, src_has_alpha,
2189 filter.overall_alpha = 1.0;
2190 make_weights (&filter, interp_type, scale_x, scale_y);
2192 if (filter.x.n == 2 && filter.y.n == 2 && dest_channels == 3 && src_channels == 3)
2196 line_func = scale_line_22_33_mmx_stub;
2199 line_func = scale_line_22_33;
2202 line_func = scale_line;
2204 pixops_process (dest_buf, render_x0, render_y0, render_x1, render_y1,
2205 dest_rowstride, dest_channels, dest_has_alpha,
2206 src_buf, src_width, src_height, src_rowstride, src_channels,
2207 src_has_alpha, scale_x, scale_y, 0, 0, 0, 0, 0,
2208 &filter, line_func, scale_pixel);
2210 g_free (filter.x.weights);
2211 g_free (filter.y.weights);
2215 _pixops_scale (guchar *dest_buf,
2221 const guchar *src_buf,
2229 int dest_region_width,
2230 int dest_region_height,
2235 PixopsInterpType interp_type)
2237 guchar *new_dest_buf;
2244 pixops_medialib_scale (dest_buf, dest_width, dest_height, dest_rowstride,
2245 dest_channels, dest_has_alpha, src_buf, src_width,
2246 src_height, src_rowstride, src_channels,
2247 src_has_alpha, dest_x, dest_y, dest_region_width,
2248 dest_region_height, offset_x, offset_y, scale_x,
2249 scale_y, (PixopsInterpType)interp_type);
2253 new_dest_buf = dest_buf + dest_y * dest_rowstride + dest_x * dest_channels;
2254 render_x0 = dest_x - offset_x;
2255 render_y0 = dest_y - offset_y;
2256 render_x1 = dest_x + dest_region_width - offset_x;
2257 render_y1 = dest_y + dest_region_height - offset_y;
2259 _pixops_scale_real (new_dest_buf, render_x0, render_y0, render_x1,
2260 render_y1, dest_rowstride, dest_channels,
2261 dest_has_alpha, src_buf, src_width, src_height,
2262 src_rowstride, src_channels, src_has_alpha,
2263 scale_x, scale_y, (PixopsInterpType)interp_type);
2268 pixops_medialib_scale (guchar *dest_buf,
2274 const guchar *src_buf,
2282 int dest_region_width,
2283 int dest_region_height,
2288 PixopsInterpType interp_type)
2290 if (scale_x == 0 || scale_y == 0)
2293 if (!medialib_initialized)
2294 _pixops_use_medialib ();
2297 * We no longer support mediaLib 2.1 because it has a core dumping problem
2298 * in the mlib_ImageZoomTranslateTable function that has been corrected in
2299 * 2.2. Although the mediaLib_zoom function could be used, it does not
2300 * work properly if the source and destination images have different
2301 * values for "has_alpha" or "num_channels". The complicated if-logic
2302 * required to support both versions is not worth supporting
2303 * mediaLib 2.1 moving forward.
2307 _pixops_scale_real (dest_buf + dest_y * dest_rowstride + dest_x *
2308 dest_channels, dest_x - offset_x, dest_y - offset_y,
2309 dest_x + dest_region_width - offset_x,
2310 dest_y + dest_region_height - offset_y,
2311 dest_rowstride, dest_channels, dest_has_alpha,
2312 src_buf, src_width, src_height, src_rowstride,
2313 src_channels, src_has_alpha, scale_x, scale_y,
2319 mlib_image img_orig_src, img_src, img_dest;
2320 double ml_offset_x, ml_offset_y;
2321 guchar *tmp_buf = NULL;
2323 mlib_ImageSetStruct (&img_orig_src, MLIB_BYTE, src_channels, src_width,
2324 src_height, src_rowstride, src_buf);
2326 if (dest_x == 0 && dest_y == 0 &&
2327 dest_width == dest_region_width &&
2328 dest_height == dest_region_height)
2330 mlib_ImageSetStruct (&img_dest, MLIB_BYTE, dest_channels,
2331 dest_width, dest_height, dest_rowstride,
2336 mlib_u8 *data = dest_buf + (dest_y * dest_rowstride) +
2337 (dest_x * dest_channels);
2339 mlib_ImageSetStruct (&img_dest, MLIB_BYTE, dest_channels,
2340 dest_region_width, dest_region_height,
2341 dest_rowstride, data);
2344 ml_offset_x = floor (offset_x) - dest_x;
2345 ml_offset_y = floor (offset_y) - dest_y;
2348 * Note that zoomTranslate and zoomTranslateTable are faster
2349 * than zoomTranslateBlend and zoomTranslateTableBlend. However
2350 * the faster functions only work in the following case:
2352 * if (src_channels == dest_channels &&
2353 * (!src_alpha && interp_table != PIXOPS_INTERP_NEAREST))
2355 * We use the faster versions if we can.
2357 * Note when the interp_type is BILINEAR and the interpolation
2358 * table will be size 2x2 (when both x/y scale factors > 1.0),
2359 * then we do not bother building the interpolation table. In
2360 * this case we can just use MLIB_BILINEAR, which is faster than
2361 * using a specified interpolation table.
2363 img_src = img_orig_src;
2367 if (src_channels > dest_channels)
2370 int rowstride = (channels * src_width + 3) & ~3;
2372 tmp_buf = g_malloc (src_rowstride * src_height);
2374 if (src_buf != NULL)
2376 src_channels = channels;
2377 src_rowstride = rowstride;
2379 mlib_ImageSetStruct (&img_src, MLIB_BYTE, src_channels,
2380 src_width, src_height, src_rowstride,
2382 mlib_ImageChannelExtract (&img_src, &img_orig_src, 0xE);
2387 if (interp_type == PIXOPS_INTERP_NEAREST)
2389 if (src_channels == dest_channels)
2391 mlib_ImageZoomTranslate (&img_dest,
2398 MLIB_EDGE_SRC_EXTEND_INDEF);
2402 mlib_ImageZoomTranslateBlend (&img_dest,
2409 MLIB_EDGE_SRC_EXTEND_INDEF,
2415 else if (src_channels == dest_channels && !src_has_alpha)
2417 if (interp_type == PIXOPS_INTERP_BILINEAR &&
2418 scale_x > 1.0 && scale_y > 1.0)
2420 mlib_ImageZoomTranslate (&img_dest,
2427 MLIB_EDGE_SRC_EXTEND_INDEF);
2431 medialib_get_interpolation (&ml_interp, interp_type,
2432 scale_x, scale_y, 1.0);
2434 if (ml_interp.interp_table != NULL)
2436 mlib_ImageZoomTranslateTable (&img_dest,
2440 ml_offset_x + ml_interp.tx,
2441 ml_offset_y + ml_interp.ty,
2442 ml_interp.interp_table,
2443 MLIB_EDGE_SRC_EXTEND_INDEF);
2445 mlib_ImageInterpTableDelete (ml_interp.interp_table);
2449 /* Should not happen. */
2450 mlib_filter ml_filter;
2452 switch (interp_type)
2454 case PIXOPS_INTERP_BILINEAR:
2455 ml_filter = MLIB_BILINEAR;
2458 case PIXOPS_INTERP_TILES:
2459 ml_filter = MLIB_BILINEAR;
2462 case PIXOPS_INTERP_HYPER:
2463 ml_filter = MLIB_BICUBIC;
2467 mlib_ImageZoomTranslate (&img_dest,
2474 MLIB_EDGE_SRC_EXTEND_INDEF);
2479 /* Deal with case where src_channels != dest_channels || src_has_alpha */
2480 else if (interp_type == PIXOPS_INTERP_BILINEAR &&
2481 scale_x > 1.0 && scale_y > 1.0)
2483 mlib_ImageZoomTranslateBlend (&img_dest,
2490 MLIB_EDGE_SRC_EXTEND_INDEF,
2497 medialib_get_interpolation (&ml_interp, interp_type,
2498 scale_x, scale_y, 1.0);
2500 if (ml_interp.interp_table != NULL)
2502 mlib_ImageZoomTranslateTableBlend (&img_dest,
2506 ml_offset_x + ml_interp.tx,
2507 ml_offset_y + ml_interp.ty,
2508 ml_interp.interp_table,
2509 MLIB_EDGE_SRC_EXTEND_INDEF,
2512 mlib_ImageInterpTableDelete (ml_interp.interp_table);
2516 mlib_filter ml_filter;
2518 switch (interp_type)
2520 case PIXOPS_INTERP_BILINEAR:
2521 ml_filter = MLIB_BILINEAR;
2524 case PIXOPS_INTERP_TILES:
2525 ml_filter = MLIB_BILINEAR;
2528 case PIXOPS_INTERP_HYPER:
2529 ml_filter = MLIB_BICUBIC;
2533 mlib_ImageZoomTranslate (&img_dest,
2540 MLIB_EDGE_SRC_EXTEND_INDEF);
2544 if (tmp_buf != NULL)