2 * Copyright (C) 1998-2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Written by Owen Taylor <otaylor@redhat.com>, based on code by
20 * Carsten Haitzler <raster@rasterman.com>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
28 static GCache *pixbuf_cache = NULL;
31 bilinear_gradient (GdkPixbuf *src,
37 guint n_channels = gdk_pixbuf_get_n_channels (src);
38 guint src_rowstride = gdk_pixbuf_get_rowstride (src);
39 guchar *src_pixels = gdk_pixbuf_get_pixels (src);
40 guchar *p1, *p2, *p3, *p4;
46 if (src_x == 0 || src_y == 0)
48 g_warning ("invalid source position for bilinear gradient");
52 p1 = src_pixels + (src_y - 1) * src_rowstride + (src_x - 1) * n_channels;
54 p3 = src_pixels + src_y * src_rowstride + (src_x - 1) * n_channels;
57 result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8,
62 g_warning ("failed to create a %dx%d pixbuf", width, height);
66 dest_rowstride = gdk_pixbuf_get_rowstride (result);
67 dest_pixels = gdk_pixbuf_get_pixels (result);
69 for (i = 0; i < height; i++)
71 guchar *p = dest_pixels + dest_rowstride *i;
75 for (k = 0; k < n_channels; k++)
77 guint start = ((height - i) * p1[k] + (1 + i) * p3[k]) / (height + 1);
78 guint end = ((height - i) * p2[k] + (1 + i) * p4[k]) / (height + 1);
80 dv[k] = (((gint)end - (gint)start) << 16) / (width + 1);
81 v[k] = (start << 16) + dv[k] + 0x8000;
84 for (j = width; j; j--)
86 for (k = 0; k < n_channels; k++)
98 horizontal_gradient (GdkPixbuf *src,
104 guint n_channels = gdk_pixbuf_get_n_channels (src);
105 guint src_rowstride = gdk_pixbuf_get_rowstride (src);
106 guchar *src_pixels = gdk_pixbuf_get_pixels (src);
107 guint dest_rowstride;
114 g_warning ("invalid source position for horizontal gradient");
118 result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8,
123 g_warning ("failed to create a %dx%d pixbuf", width, height);
127 dest_rowstride = gdk_pixbuf_get_rowstride (result);
128 dest_pixels = gdk_pixbuf_get_pixels (result);
130 for (i = 0; i < height; i++)
132 guchar *p = dest_pixels + dest_rowstride *i;
133 guchar *p1 = src_pixels + (src_y + i) * src_rowstride + (src_x - 1) * n_channels;
134 guchar *p2 = p1 + n_channels;
139 for (k = 0; k < n_channels; k++)
141 dv[k] = (((gint)p2[k] - (gint)p1[k]) << 16) / (width + 1);
142 v[k] = (p1[k] << 16) + dv[k] + 0x8000;
145 for (j = width; j; j--)
147 for (k = 0; k < n_channels; k++)
159 vertical_gradient (GdkPixbuf *src,
165 guint n_channels = gdk_pixbuf_get_n_channels (src);
166 guint src_rowstride = gdk_pixbuf_get_rowstride (src);
167 guchar *src_pixels = gdk_pixbuf_get_pixels (src);
168 guchar *top_pixels, *bottom_pixels;
169 guint dest_rowstride;
176 g_warning ("invalid source position for vertical gradient");
180 top_pixels = src_pixels + (src_y - 1) * src_rowstride + (src_x) * n_channels;
181 bottom_pixels = top_pixels + src_rowstride;
183 result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8,
188 g_warning ("failed to create a %dx%d pixbuf", width, height);
192 dest_rowstride = gdk_pixbuf_get_rowstride (result);
193 dest_pixels = gdk_pixbuf_get_pixels (result);
195 for (i = 0; i < height; i++)
197 guchar *p = dest_pixels + dest_rowstride *i;
198 guchar *p1 = top_pixels;
199 guchar *p2 = bottom_pixels;
201 for (j = width * n_channels; j; j--)
202 *(p++) = ((height - i) * *(p1++) + (1 + i) * *(p2++)) / (height + 1);
209 replicate_single (GdkPixbuf *src,
215 guint n_channels = gdk_pixbuf_get_n_channels (src);
216 guchar *pixels = (gdk_pixbuf_get_pixels (src) +
217 src_y * gdk_pixbuf_get_rowstride (src) +
219 guchar r = *(pixels++);
220 guchar g = *(pixels++);
221 guchar b = *(pixels++);
222 guint dest_rowstride;
231 result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8,
236 g_warning ("failed to create a %dx%d pixbuf", width, height);
240 dest_rowstride = gdk_pixbuf_get_rowstride (result);
241 dest_pixels = gdk_pixbuf_get_pixels (result);
243 for (i = 0; i < height; i++)
245 guchar *p = dest_pixels + dest_rowstride *i;
247 for (j = 0; j < width; j++)
262 replicate_rows (GdkPixbuf *src,
268 guint n_channels = gdk_pixbuf_get_n_channels (src);
269 guint src_rowstride = gdk_pixbuf_get_rowstride (src);
270 guchar *pixels = (gdk_pixbuf_get_pixels (src) + src_y * src_rowstride + src_x * n_channels);
273 guint dest_rowstride;
276 result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8,
281 g_warning ("failed to create a %dx%d pixbuf", width, height);
285 dest_rowstride = gdk_pixbuf_get_rowstride (result);
286 dest_pixels = gdk_pixbuf_get_pixels (result);
288 for (i = 0; i < height; i++)
289 memcpy (dest_pixels + dest_rowstride * i, pixels, n_channels * width);
295 replicate_cols (GdkPixbuf *src,
301 guint n_channels = gdk_pixbuf_get_n_channels (src);
302 guint src_rowstride = gdk_pixbuf_get_rowstride (src);
303 guchar *pixels = (gdk_pixbuf_get_pixels (src) + src_y * src_rowstride + src_x * n_channels);
306 guint dest_rowstride;
309 result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8,
314 g_warning ("failed to create a %dx%d pixbuf", width, height);
318 dest_rowstride = gdk_pixbuf_get_rowstride (result);
319 dest_pixels = gdk_pixbuf_get_pixels (result);
321 for (i = 0; i < height; i++)
323 guchar *p = dest_pixels + dest_rowstride * i;
324 guchar *q = pixels + src_rowstride * i;
334 for (j = 0; j < width; j++)
348 /* Scale the rectangle (src_x, src_y, src_width, src_height)
349 * onto the rectangle (dest_x, dest_y, dest_width, dest_height)
350 * of the destination, clip by clip_rect and render
353 pixbuf_render (GdkPixbuf *src,
357 GdkRectangle *clip_rect,
367 GdkPixbuf *tmp_pixbuf = NULL;
369 int x_offset, y_offset;
370 gboolean has_alpha = gdk_pixbuf_get_has_alpha (src);
371 gint src_rowstride = gdk_pixbuf_get_rowstride (src);
372 gint src_n_channels = gdk_pixbuf_get_n_channels (src);
374 if (dest_width <= 0 || dest_height <= 0)
379 rect.width = dest_width;
380 rect.height = dest_height;
382 if (hints & THEME_MISSING)
385 /* FIXME: Because we use the mask to shape windows, we don't use
386 * clip_rect to clip what we draw to the mask, only to clip
387 * what we actually draw. But this leads to the horrible ineffiency
388 * of scale the whole image to get a little bit of it.
390 if (!mask && clip_rect)
392 if (!gdk_rectangle_intersect (clip_rect, &rect, &rect))
396 if (dest_width == src_width && dest_height == src_height)
398 tmp_pixbuf = g_object_ref (src);
400 x_offset = src_x + rect.x - dest_x;
401 y_offset = src_y + rect.y - dest_y;
403 else if (src_width == 0 && src_height == 0)
405 tmp_pixbuf = bilinear_gradient (src, src_x, src_y, dest_width, dest_height);
407 x_offset = rect.x - dest_x;
408 y_offset = rect.y - dest_y;
410 else if (src_width == 0 && dest_height == src_height)
412 tmp_pixbuf = horizontal_gradient (src, src_x, src_y, dest_width, dest_height);
414 x_offset = rect.x - dest_x;
415 y_offset = rect.y - dest_y;
417 else if (src_height == 0 && dest_width == src_width)
419 tmp_pixbuf = vertical_gradient (src, src_x, src_y, dest_width, dest_height);
421 x_offset = rect.x - dest_x;
422 y_offset = rect.y - dest_y;
424 else if ((hints & THEME_CONSTANT_COLS) && (hints & THEME_CONSTANT_ROWS))
426 tmp_pixbuf = replicate_single (src, src_x, src_y, dest_width, dest_height);
428 x_offset = rect.x - dest_x;
429 y_offset = rect.y - dest_y;
431 else if (dest_width == src_width && (hints & THEME_CONSTANT_COLS))
433 tmp_pixbuf = replicate_rows (src, src_x, src_y, dest_width, dest_height);
435 x_offset = rect.x - dest_x;
436 y_offset = rect.y - dest_y;
438 else if (dest_height == src_height && (hints & THEME_CONSTANT_ROWS))
440 tmp_pixbuf = replicate_cols (src, src_x, src_y, dest_width, dest_height);
442 x_offset = rect.x - dest_x;
443 y_offset = rect.y - dest_y;
445 else if (src_width > 0 && src_height > 0)
447 double x_scale = (double)dest_width / src_width;
448 double y_scale = (double)dest_height / src_height;
450 GdkPixbuf *partial_src;
452 pixels = (gdk_pixbuf_get_pixels (src)
453 + src_y * src_rowstride
454 + src_x * src_n_channels);
456 partial_src = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB,
458 8, src_width, src_height,
462 tmp_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
464 rect.width, rect.height);
466 gdk_pixbuf_scale (partial_src, tmp_pixbuf,
467 0, 0, rect.width, rect.height,
468 dest_x - rect.x, dest_y - rect.y,
470 GDK_INTERP_BILINEAR);
472 g_object_unref (partial_src);
482 cr = gdk_cairo_create (window);
485 gdk_pixbuf_render_threshold_alpha (tmp_pixbuf, mask,
488 rect.width, rect.height,
492 gdk_cairo_set_source_pixbuf (cr,
496 gdk_cairo_rectangle (cr, &rect);
500 g_object_unref (tmp_pixbuf);
505 theme_pixbuf_new (void)
507 ThemePixbuf *result = g_new0 (ThemePixbuf, 1);
508 result->filename = NULL;
509 result->pixbuf = NULL;
511 result->stretch = TRUE;
512 result->border_left = 0;
513 result->border_right = 0;
514 result->border_bottom = 0;
515 result->border_top = 0;
521 theme_pixbuf_destroy (ThemePixbuf *theme_pb)
523 theme_pixbuf_set_filename (theme_pb, NULL);
528 theme_pixbuf_set_filename (ThemePixbuf *theme_pb,
529 const char *filename)
531 if (theme_pb->pixbuf)
533 g_cache_remove (pixbuf_cache, theme_pb->pixbuf);
534 theme_pb->pixbuf = NULL;
537 g_free (theme_pb->filename);
540 theme_pb->filename = g_strdup (filename);
542 theme_pb->filename = NULL;
546 compute_hint (GdkPixbuf *pixbuf,
553 int hints = THEME_CONSTANT_ROWS | THEME_CONSTANT_COLS | THEME_MISSING;
554 int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
556 guchar *data = gdk_pixbuf_get_pixels (pixbuf);
557 int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
559 if (x0 == x1 || y0 == y1)
562 for (i = y0; i < y1; i++)
564 guchar *p = data + i * rowstride + x0 * n_channels;
573 for (j = x0; j < x1 ; j++)
575 if (n_channels != 4 || p[3] != 0)
577 hints &= ~THEME_MISSING;
578 if (!(hints & THEME_CONSTANT_ROWS))
585 (n_channels != 4 && a != *(p++)))
587 hints &= ~THEME_CONSTANT_ROWS;
588 if (!(hints & THEME_MISSING))
595 for (i = y0 + 1; i < y1; i++)
597 guchar *base = data + y0 * rowstride + x0 * n_channels;
598 guchar *p = data + i * rowstride + x0 * n_channels;
600 if (memcmp (p, base, n_channels * (x1 - x0)) != 0)
602 hints &= ~THEME_CONSTANT_COLS;
611 theme_pixbuf_compute_hints (ThemePixbuf *theme_pb)
614 gint width = gdk_pixbuf_get_width (theme_pb->pixbuf);
615 gint height = gdk_pixbuf_get_height (theme_pb->pixbuf);
617 if (theme_pb->border_left + theme_pb->border_right > width ||
618 theme_pb->border_top + theme_pb->border_bottom > height)
620 g_warning ("Invalid borders specified for theme pixmap:\n"
622 "borders don't fit within the image", theme_pb->filename);
623 if (theme_pb->border_left + theme_pb->border_right > width)
625 theme_pb->border_left = width / 2;
626 theme_pb->border_right = (width + 1) / 2;
628 if (theme_pb->border_bottom + theme_pb->border_top > height)
630 theme_pb->border_top = height / 2;
631 theme_pb->border_bottom = (height + 1) / 2;
635 for (i = 0; i < 3; i++)
643 y1 = theme_pb->border_top;
646 y0 = theme_pb->border_top;
647 y1 = height - theme_pb->border_bottom;
650 y0 = height - theme_pb->border_bottom;
655 for (j = 0; j < 3; j++)
663 x1 = theme_pb->border_left;
666 x0 = theme_pb->border_left;
667 x1 = width - theme_pb->border_right;
670 x0 = width - theme_pb->border_right;
675 theme_pb->hints[i][j] = compute_hint (theme_pb->pixbuf, x0, x1, y0, y1);
682 theme_pixbuf_set_border (ThemePixbuf *theme_pb,
688 theme_pb->border_left = left;
689 theme_pb->border_right = right;
690 theme_pb->border_top = top;
691 theme_pb->border_bottom = bottom;
693 if (theme_pb->pixbuf)
694 theme_pixbuf_compute_hints (theme_pb);
698 theme_pixbuf_set_stretch (ThemePixbuf *theme_pb,
701 theme_pb->stretch = stretch;
703 if (theme_pb->pixbuf)
704 theme_pixbuf_compute_hints (theme_pb);
708 pixbuf_cache_value_new (gchar *filename)
712 GdkPixbuf *result = gdk_pixbuf_new_from_file (filename, &err);
715 g_warning ("Pixbuf theme: Cannot load pixmap file %s: %s\n",
716 filename, err->message);
724 theme_pixbuf_get_pixbuf (ThemePixbuf *theme_pb)
726 if (!theme_pb->pixbuf)
729 pixbuf_cache = g_cache_new ((GCacheNewFunc)pixbuf_cache_value_new,
730 (GCacheDestroyFunc)g_object_unref,
731 (GCacheDupFunc)g_strdup,
732 (GCacheDestroyFunc)g_free,
733 g_str_hash, g_direct_hash, g_str_equal);
735 theme_pb->pixbuf = g_cache_insert (pixbuf_cache, theme_pb->filename);
737 if (theme_pb->stretch)
738 theme_pixbuf_compute_hints (theme_pb);
741 return theme_pb->pixbuf;
745 theme_pixbuf_render (ThemePixbuf *theme_pb,
748 GdkRectangle *clip_rect,
749 guint component_mask,
756 GdkPixbuf *pixbuf = theme_pixbuf_get_pixbuf (theme_pb);
757 gint src_x[4], src_y[4], dest_x[4], dest_y[4];
758 gint pixbuf_width = gdk_pixbuf_get_width (pixbuf);
759 gint pixbuf_height = gdk_pixbuf_get_height (pixbuf);
764 if (theme_pb->stretch)
766 if (component_mask & COMPONENT_ALL)
767 component_mask = (COMPONENT_ALL - 1) & ~component_mask;
770 src_x[1] = theme_pb->border_left;
771 src_x[2] = pixbuf_width - theme_pb->border_right;
772 src_x[3] = pixbuf_width;
775 src_y[1] = theme_pb->border_top;
776 src_y[2] = pixbuf_height - theme_pb->border_bottom;
777 src_y[3] = pixbuf_height;
780 dest_x[1] = x + theme_pb->border_left;
781 dest_x[2] = x + width - theme_pb->border_right;
782 dest_x[3] = x + width;
784 if (dest_x[1] > dest_x[2])
786 component_mask &= ~(COMPONENT_NORTH | COMPONENT_SOUTH | COMPONENT_CENTER);
787 dest_x[1] = dest_x[2] = (dest_x[1] + dest_x[2]) / 2;
791 dest_y[1] = y + theme_pb->border_top;
792 dest_y[2] = y + height - theme_pb->border_bottom;
793 dest_y[3] = y + height;
795 if (dest_y[1] > dest_y[2])
797 component_mask &= ~(COMPONENT_EAST | COMPONENT_WEST | COMPONENT_CENTER);
798 dest_y[1] = dest_y[2] = (dest_y[1] + dest_y[2]) / 2;
803 #define RENDER_COMPONENT(X1,X2,Y1,Y2) \
804 pixbuf_render (pixbuf, theme_pb->hints[Y1][X1], window, mask, clip_rect, \
805 src_x[X1], src_y[Y1], \
806 src_x[X2] - src_x[X1], src_y[Y2] - src_y[Y1], \
807 dest_x[X1], dest_y[Y1], \
808 dest_x[X2] - dest_x[X1], dest_y[Y2] - dest_y[Y1]);
810 if (component_mask & COMPONENT_NORTH_WEST)
811 RENDER_COMPONENT (0, 1, 0, 1);
813 if (component_mask & COMPONENT_NORTH)
814 RENDER_COMPONENT (1, 2, 0, 1);
816 if (component_mask & COMPONENT_NORTH_EAST)
817 RENDER_COMPONENT (2, 3, 0, 1);
819 if (component_mask & COMPONENT_WEST)
820 RENDER_COMPONENT (0, 1, 1, 2);
822 if (component_mask & COMPONENT_CENTER)
823 RENDER_COMPONENT (1, 2, 1, 2);
825 if (component_mask & COMPONENT_EAST)
826 RENDER_COMPONENT (2, 3, 1, 2);
828 if (component_mask & COMPONENT_SOUTH_WEST)
829 RENDER_COMPONENT (0, 1, 2, 3);
831 if (component_mask & COMPONENT_SOUTH)
832 RENDER_COMPONENT (1, 2, 2, 3);
834 if (component_mask & COMPONENT_SOUTH_EAST)
835 RENDER_COMPONENT (2, 3, 2, 3);
841 x += (width - pixbuf_width) / 2;
842 y += (height - pixbuf_height) / 2;
844 pixbuf_render (pixbuf, 0, window, NULL, clip_rect,
846 pixbuf_width, pixbuf_height,
848 pixbuf_width, pixbuf_height);
852 cairo_t *cr = gdk_cairo_create (window);
854 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
855 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
858 gdk_cairo_rectangle (cr, clip_rect);
860 cairo_rectangle (cr, x, y, width, height);