| 1 |
/* GTK+ Pixbuf Engine |
| 2 |
* Copyright (C) 1998-2000 Red Hat, Inc. |
| 3 |
* |
| 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. |
| 8 |
* |
| 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. |
| 13 |
* |
| 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. |
| 18 |
* |
| 19 |
* Written by Owen Taylor <[email protected]>, based on code by |
| 20 |
* Carsten Haitzler <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include "pixbuf.h" |
| 26 |
#include <gdk-pixbuf/gdk-pixbuf.h> |
| 27 |
|
| 28 |
static GCache *pixbuf_cache = NULL; |
| 29 |
|
| 30 |
static GdkPixbuf * |
| 31 |
bilinear_gradient (GdkPixbuf *src, |
| 32 |
gint src_x, |
| 33 |
gint src_y, |
| 34 |
gint width, |
| 35 |
gint height) |
| 36 |
{ |
| 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; |
| 41 |
guint dest_rowstride; |
| 42 |
guchar *dest_pixels; |
| 43 |
GdkPixbuf *result; |
| 44 |
int i, j, k; |
| 45 |
|
| 46 |
if (src_x == 0 || src_y == 0) |
| 47 |
{ |
| 48 |
g_warning ("invalid source position for bilinear gradient"); |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
|
| 52 |
p1 = src_pixels + (src_y - 1) * src_rowstride + (src_x - 1) * n_channels; |
| 53 |
p2 = p1 + n_channels; |
| 54 |
p3 = src_pixels + src_y * src_rowstride + (src_x - 1) * n_channels; |
| 55 |
p4 = p3 + n_channels; |
| 56 |
|
| 57 |
result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8, |
| 58 |
width, height); |
| 59 |
|
| 60 |
if (result == NULL) |
| 61 |
{ |
| 62 |
g_warning ("failed to create a %dx%d pixbuf", width, height); |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
dest_rowstride = gdk_pixbuf_get_rowstride (result); |
| 67 |
dest_pixels = gdk_pixbuf_get_pixels (result); |
| 68 |
|
| 69 |
for (i = 0; i < height; i++) |
| 70 |
{ |
| 71 |
guchar *p = dest_pixels + dest_rowstride *i; |
| 72 |
guint v[4]; |
| 73 |
gint dv[4]; |
| 74 |
|
| 75 |
for (k = 0; k < n_channels; k++) |
| 76 |
{ |
| 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); |
| 79 |
|
| 80 |
dv[k] = (((gint)end - (gint)start) << 16) / (width + 1); |
| 81 |
v[k] = (start << 16) + dv[k] + 0x8000; |
| 82 |
} |
| 83 |
|
| 84 |
for (j = width; j; j--) |
| 85 |
{ |
| 86 |
for (k = 0; k < n_channels; k++) |
| 87 |
{ |
| 88 |
*(p++) = v[k] >> 16; |
| 89 |
v[k] += dv[k]; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return result; |
| 95 |
} |
| 96 |
|
| 97 |
static GdkPixbuf * |
| 98 |
horizontal_gradient (GdkPixbuf *src, |
| 99 |
gint src_x, |
| 100 |
gint src_y, |
| 101 |
gint width, |
| 102 |
gint height) |
| 103 |
{ |
| 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; |
| 108 |
guchar *dest_pixels; |
| 109 |
GdkPixbuf *result; |
| 110 |
int i, j, k; |
| 111 |
|
| 112 |
if (src_x == 0) |
| 113 |
{ |
| 114 |
g_warning ("invalid source position for horizontal gradient"); |
| 115 |
return NULL; |
| 116 |
} |
| 117 |
|
| 118 |
result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8, |
| 119 |
width, height); |
| 120 |
|
| 121 |
if (result == NULL) |
| 122 |
{ |
| 123 |
g_warning ("failed to create a %dx%d pixbuf", width, height); |
| 124 |
return NULL; |
| 125 |
} |
| 126 |
|
| 127 |
dest_rowstride = gdk_pixbuf_get_rowstride (result); |
| 128 |
dest_pixels = gdk_pixbuf_get_pixels (result); |
| 129 |
|
| 130 |
for (i = 0; i < height; i++) |
| 131 |
{ |
| 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; |
| 135 |
|
| 136 |
guint v[4]; |
| 137 |
gint dv[4]; |
| 138 |
|
| 139 |
for (k = 0; k < n_channels; k++) |
| 140 |
{ |
| 141 |
dv[k] = (((gint)p2[k] - (gint)p1[k]) << 16) / (width + 1); |
| 142 |
v[k] = (p1[k] << 16) + dv[k] + 0x8000; |
| 143 |
} |
| 144 |
|
| 145 |
for (j = width; j; j--) |
| 146 |
{ |
| 147 |
for (k = 0; k < n_channels; k++) |
| 148 |
{ |
| 149 |
*(p++) = v[k] >> 16; |
| 150 |
v[k] += dv[k]; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return result; |
| 156 |
} |
| 157 |
|
| 158 |
static GdkPixbuf * |
| 159 |
vertical_gradient (GdkPixbuf *src, |
| 160 |
gint src_x, |
| 161 |
gint src_y, |
| 162 |
gint width, |
| 163 |
gint height) |
| 164 |
{ |
| 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; |
| 170 |
guchar *dest_pixels; |
| 171 |
GdkPixbuf *result; |
| 172 |
int i, j; |
| 173 |
|
| 174 |
if (src_y == 0) |
| 175 |
{ |
| 176 |
g_warning ("invalid source position for vertical gradient"); |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
|
| 180 |
top_pixels = src_pixels + (src_y - 1) * src_rowstride + (src_x) * n_channels; |
| 181 |
bottom_pixels = top_pixels + src_rowstride; |
| 182 |
|
| 183 |
result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8, |
| 184 |
width, height); |
| 185 |
|
| 186 |
if (result == NULL) |
| 187 |
{ |
| 188 |
g_warning ("failed to create a %dx%d pixbuf", width, height); |
| 189 |
return NULL; |
| 190 |
} |
| 191 |
|
| 192 |
dest_rowstride = gdk_pixbuf_get_rowstride (result); |
| 193 |
dest_pixels = gdk_pixbuf_get_pixels (result); |
| 194 |
|
| 195 |
for (i = 0; i < height; i++) |
| 196 |
{ |
| 197 |
guchar *p = dest_pixels + dest_rowstride *i; |
| 198 |
guchar *p1 = top_pixels; |
| 199 |
guchar *p2 = bottom_pixels; |
| 200 |
|
| 201 |
for (j = width * n_channels; j; j--) |
| 202 |
*(p++) = ((height - i) * *(p1++) + (1 + i) * *(p2++)) / (height + 1); |
| 203 |
} |
| 204 |
|
| 205 |
return result; |
| 206 |
} |
| 207 |
|
| 208 |
static GdkPixbuf * |
| 209 |
replicate_single (GdkPixbuf *src, |
| 210 |
gint src_x, |
| 211 |
gint src_y, |
| 212 |
gint width, |
| 213 |
gint height) |
| 214 |
{ |
| 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) + |
| 218 |
src_x * n_channels); |
| 219 |
guchar r = *(pixels++); |
| 220 |
guchar g = *(pixels++); |
| 221 |
guchar b = *(pixels++); |
| 222 |
guint dest_rowstride; |
| 223 |
guchar *dest_pixels; |
| 224 |
guchar a = 0; |
| 225 |
GdkPixbuf *result; |
| 226 |
int i, j; |
| 227 |
|
| 228 |
if (n_channels == 4) |
| 229 |
a = *(pixels++); |
| 230 |
|
| 231 |
result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8, |
| 232 |
width, height); |
| 233 |
|
| 234 |
if (result == NULL) |
| 235 |
{ |
| 236 |
g_warning ("failed to create a %dx%d pixbuf", width, height); |
| 237 |
return NULL; |
| 238 |
} |
| 239 |
|
| 240 |
dest_rowstride = gdk_pixbuf_get_rowstride (result); |
| 241 |
dest_pixels = gdk_pixbuf_get_pixels (result); |
| 242 |
|
| 243 |
for (i = 0; i < height; i++) |
| 244 |
{ |
| 245 |
guchar *p = dest_pixels + dest_rowstride *i; |
| 246 |
|
| 247 |
for (j = 0; j < width; j++) |
| 248 |
{ |
| 249 |
*(p++) = r; |
| 250 |
*(p++) = g; |
| 251 |
*(p++) = b; |
| 252 |
|
| 253 |
if (n_channels == 4) |
| 254 |
*(p++) = a; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
return result; |
| 259 |
} |
| 260 |
|
| 261 |
static GdkPixbuf * |
| 262 |
replicate_rows (GdkPixbuf *src, |
| 263 |
gint src_x, |
| 264 |
gint src_y, |
| 265 |
gint width, |
| 266 |
gint height) |
| 267 |
{ |
| 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); |
| 271 |
guchar *dest_pixels; |
| 272 |
GdkPixbuf *result; |
| 273 |
guint dest_rowstride; |
| 274 |
int i; |
| 275 |
|
| 276 |
result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8, |
| 277 |
width, height); |
| 278 |
|
| 279 |
if (result == NULL) |
| 280 |
{ |
| 281 |
g_warning ("failed to create a %dx%d pixbuf", width, height); |
| 282 |
return NULL; |
| 283 |
} |
| 284 |
|
| 285 |
dest_rowstride = gdk_pixbuf_get_rowstride (result); |
| 286 |
dest_pixels = gdk_pixbuf_get_pixels (result); |
| 287 |
|
| 288 |
for (i = 0; i < height; i++) |
| 289 |
memcpy (dest_pixels + dest_rowstride * i, pixels, n_channels * width); |
| 290 |
|
| 291 |
return result; |
| 292 |
} |
| 293 |
|
| 294 |
static GdkPixbuf * |
| 295 |
replicate_cols (GdkPixbuf *src, |
| 296 |
gint src_x, |
| 297 |
gint src_y, |
| 298 |
gint width, |
| 299 |
gint height) |
| 300 |
{ |
| 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); |
| 304 |
guchar *dest_pixels; |
| 305 |
GdkPixbuf *result; |
| 306 |
guint dest_rowstride; |
| 307 |
int i, j; |
| 308 |
|
| 309 |
result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8, |
| 310 |
width, height); |
| 311 |
|
| 312 |
if (result == NULL) |
| 313 |
{ |
| 314 |
g_warning ("failed to create a %dx%d pixbuf", width, height); |
| 315 |
return NULL; |
| 316 |
} |
| 317 |
|
| 318 |
dest_rowstride = gdk_pixbuf_get_rowstride (result); |
| 319 |
dest_pixels = gdk_pixbuf_get_pixels (result); |
| 320 |
|
| 321 |
for (i = 0; i < height; i++) |
| 322 |
{ |
| 323 |
guchar *p = dest_pixels + dest_rowstride * i; |
| 324 |
guchar *q = pixels + src_rowstride * i; |
| 325 |
|
| 326 |
guchar r = *(q++); |
| 327 |
guchar g = *(q++); |
| 328 |
guchar b = *(q++); |
| 329 |
guchar a = 0; |
| 330 |
|
| 331 |
if (n_channels == 4) |
| 332 |
a = *(q++); |
| 333 |
|
| 334 |
for (j = 0; j < width; j++) |
| 335 |
{ |
| 336 |
*(p++) = r; |
| 337 |
*(p++) = g; |
| 338 |
*(p++) = b; |
| 339 |
|
| 340 |
if (n_channels == 4) |
| 341 |
*(p++) = a; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
return result; |
| 346 |
} |
| 347 |
|
| 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 |
| 351 |
*/ |
| 352 |
static void |
| 353 |
pixbuf_render (GdkPixbuf *src, |
| 354 |
guint hints, |
| 355 |
GdkWindow *window, |
| 356 |
GdkBitmap *mask, |
| 357 |
GdkRectangle *clip_rect, |
| 358 |
gint src_x, |
| 359 |
gint src_y, |
| 360 |
gint src_width, |
| 361 |
gint src_height, |
| 362 |
gint dest_x, |
| 363 |
gint dest_y, |
| 364 |
gint dest_width, |
| 365 |
gint dest_height) |
| 366 |
{ |
| 367 |
GdkPixbuf *tmp_pixbuf = NULL; |
| 368 |
GdkRectangle rect; |
| 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); |
| 373 |
|
| 374 |
if (dest_width <= 0 || dest_height <= 0) |
| 375 |
return; |
| 376 |
|
| 377 |
rect.x = dest_x; |
| 378 |
rect.y = dest_y; |
| 379 |
rect.width = dest_width; |
| 380 |
rect.height = dest_height; |
| 381 |
|
| 382 |
if (hints & THEME_MISSING) |
| 383 |
return; |
| 384 |
|
| 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. |
| 389 |
*/ |
| 390 |
if (!mask && clip_rect) |
| 391 |
{ |
| 392 |
if (!gdk_rectangle_intersect (clip_rect, &rect, &rect)) |
| 393 |
return; |
| 394 |
} |
| 395 |
|
| 396 |
if (dest_width == src_width && dest_height == src_height) |
| 397 |
{ |
| 398 |
tmp_pixbuf = g_object_ref (src); |
| 399 |
|
| 400 |
x_offset = src_x + rect.x - dest_x; |
| 401 |
y_offset = src_y + rect.y - dest_y; |
| 402 |
} |
| 403 |
else if (src_width == 0 && src_height == 0) |
| 404 |
{ |
| 405 |
tmp_pixbuf = bilinear_gradient (src, src_x, src_y, dest_width, dest_height); |
| 406 |
|
| 407 |
x_offset = rect.x - dest_x; |
| 408 |
y_offset = rect.y - dest_y; |
| 409 |
} |
| 410 |
else if (src_width == 0 && dest_height == src_height) |
| 411 |
{ |
| 412 |
tmp_pixbuf = horizontal_gradient (src, src_x, src_y, dest_width, dest_height); |
| 413 |
|
| 414 |
x_offset = rect.x - dest_x; |
| 415 |
y_offset = rect.y - dest_y; |
| 416 |
} |
| 417 |
else if (src_height == 0 && dest_width == src_width) |
| 418 |
{ |
| 419 |
tmp_pixbuf = vertical_gradient (src, src_x, src_y, dest_width, dest_height); |
| 420 |
|
| 421 |
x_offset = rect.x - dest_x; |
| 422 |
y_offset = rect.y - dest_y; |
| 423 |
} |
| 424 |
else if ((hints & THEME_CONSTANT_COLS) && (hints & THEME_CONSTANT_ROWS)) |
| 425 |
{ |
| 426 |
tmp_pixbuf = replicate_single (src, src_x, src_y, dest_width, dest_height); |
| 427 |
|
| 428 |
x_offset = rect.x - dest_x; |
| 429 |
y_offset = rect.y - dest_y; |
| 430 |
} |
| 431 |
else if (dest_width == src_width && (hints & THEME_CONSTANT_COLS)) |
| 432 |
{ |
| 433 |
tmp_pixbuf = replicate_rows (src, src_x, src_y, dest_width, dest_height); |
| 434 |
|
| 435 |
x_offset = rect.x - dest_x; |
| 436 |
y_offset = rect.y - dest_y; |
| 437 |
} |
| 438 |
else if (dest_height == src_height && (hints & THEME_CONSTANT_ROWS)) |
| 439 |
{ |
| 440 |
tmp_pixbuf = replicate_cols (src, src_x, src_y, dest_width, dest_height); |
| 441 |
|
| 442 |
x_offset = rect.x - dest_x; |
| 443 |
y_offset = rect.y - dest_y; |
| 444 |
} |
| 445 |
else if (src_width > 0 && src_height > 0) |
| 446 |
{ |
| 447 |
double x_scale = (double)dest_width / src_width; |
| 448 |
double y_scale = (double)dest_height / src_height; |
| 449 |
guchar *pixels; |
| 450 |
GdkPixbuf *partial_src; |
| 451 |
|
| 452 |
pixels = (gdk_pixbuf_get_pixels (src) |
| 453 |
+ src_y * src_rowstride |
| 454 |
+ src_x * src_n_channels); |
| 455 |
|
| 456 |
partial_src = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, |
| 457 |
has_alpha, |
| 458 |
8, src_width, src_height, |
| 459 |
src_rowstride, |
| 460 |
NULL, NULL); |
| 461 |
|
| 462 |
tmp_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, |
| 463 |
has_alpha, 8, |
| 464 |
rect.width, rect.height); |
| 465 |
|
| 466 |
gdk_pixbuf_scale (partial_src, tmp_pixbuf, |
| 467 |
0, 0, rect.width, rect.height, |
| 468 |
dest_x - rect.x, dest_y - rect.y, |
| 469 |
x_scale, y_scale, |
| 470 |
GDK_INTERP_BILINEAR); |
| 471 |
|
| 472 |
g_object_unref (partial_src); |
| 473 |
|
| 474 |
x_offset = 0; |
| 475 |
y_offset = 0; |
| 476 |
} |
| 477 |
|
| 478 |
if (tmp_pixbuf) |
| 479 |
{ |
| 480 |
cairo_t *cr; |
| 481 |
|
| 482 |
if (mask) |
| 483 |
{ |
| 484 |
cr = gdk_cairo_create (mask); |
| 485 |
|
| 486 |
gdk_cairo_set_source_pixbuf (cr, tmp_pixbuf, |
| 487 |
-x_offset + rect.x, |
| 488 |
-y_offset + rect.y); |
| 489 |
gdk_cairo_rectangle (cr, &rect); |
| 490 |
cairo_fill (cr); |
| 491 |
|
| 492 |
cairo_destroy (cr); |
| 493 |
} |
| 494 |
|
| 495 |
cr = gdk_cairo_create (window); |
| 496 |
gdk_cairo_set_source_pixbuf (cr, |
| 497 |
tmp_pixbuf, |
| 498 |
-x_offset + rect.x, |
| 499 |
-y_offset + rect.y); |
| 500 |
gdk_cairo_rectangle (cr, &rect); |
| 501 |
cairo_fill (cr); |
| 502 |
|
| 503 |
cairo_destroy (cr); |
| 504 |
g_object_unref (tmp_pixbuf); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
ThemePixbuf * |
| 509 |
theme_pixbuf_new (void) |
| 510 |
{ |
| 511 |
ThemePixbuf *result = g_new0 (ThemePixbuf, 1); |
| 512 |
result->filename = NULL; |
| 513 |
result->pixbuf = NULL; |
| 514 |
|
| 515 |
result->stretch = TRUE; |
| 516 |
result->border_left = 0; |
| 517 |
result->border_right = 0; |
| 518 |
result->border_bottom = 0; |
| 519 |
result->border_top = 0; |
| 520 |
|
| 521 |
return result; |
| 522 |
} |
| 523 |
|
| 524 |
void |
| 525 |
theme_pixbuf_destroy (ThemePixbuf *theme_pb) |
| 526 |
{ |
| 527 |
if (G_LIKELY (theme_pb)) |
| 528 |
{ |
| 529 |
theme_pixbuf_set_filename (theme_pb, NULL); |
| 530 |
g_free (theme_pb); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
void |
| 535 |
theme_clear_pixbuf (ThemePixbuf **theme_pb) |
| 536 |
{ |
| 537 |
#if GLIB_CHECK_VERSION (2, 34, 0) |
| 538 |
g_clear_pointer (theme_pb, theme_pixbuf_destroy); |
| 539 |
#else |
| 540 |
if (*theme_pb) |
| 541 |
{ |
| 542 |
theme_pixbuf_destroy (*theme_pb); |
| 543 |
*theme_pb = NULL; |
| 544 |
} |
| 545 |
#endif |
| 546 |
} |
| 547 |
|
| 548 |
void |
| 549 |
theme_pixbuf_set_filename (ThemePixbuf *theme_pb, |
| 550 |
const char *filename) |
| 551 |
{ |
| 552 |
if (theme_pb->pixbuf) |
| 553 |
{ |
| 554 |
g_cache_remove (pixbuf_cache, theme_pb->pixbuf); |
| 555 |
theme_pb->pixbuf = NULL; |
| 556 |
} |
| 557 |
|
| 558 |
g_free (theme_pb->filename); |
| 559 |
|
| 560 |
if (filename) |
| 561 |
theme_pb->filename = g_strdup (filename); |
| 562 |
else |
| 563 |
theme_pb->filename = NULL; |
| 564 |
} |
| 565 |
|
| 566 |
static guint |
| 567 |
compute_hint (GdkPixbuf *pixbuf, |
| 568 |
gint x0, |
| 569 |
gint x1, |
| 570 |
gint y0, |
| 571 |
gint y1) |
| 572 |
{ |
| 573 |
int i, j; |
| 574 |
int hints = THEME_CONSTANT_ROWS | THEME_CONSTANT_COLS | THEME_MISSING; |
| 575 |
int n_channels = gdk_pixbuf_get_n_channels (pixbuf); |
| 576 |
|
| 577 |
guchar *data = gdk_pixbuf_get_pixels (pixbuf); |
| 578 |
int rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
| 579 |
|
| 580 |
if (x0 == x1 || y0 == y1) |
| 581 |
return 0; |
| 582 |
|
| 583 |
for (i = y0; i < y1; i++) |
| 584 |
{ |
| 585 |
guchar *p = data + i * rowstride + x0 * n_channels; |
| 586 |
guchar r = p[0]; |
| 587 |
guchar g = p[1]; |
| 588 |
guchar b = p[2]; |
| 589 |
guchar a = 0; |
| 590 |
|
| 591 |
if (n_channels == 4) |
| 592 |
a = p[3]; |
| 593 |
|
| 594 |
for (j = x0; j < x1 ; j++) |
| 595 |
{ |
| 596 |
if (n_channels != 4 || p[3] != 0) |
| 597 |
{ |
| 598 |
hints &= ~THEME_MISSING; |
| 599 |
if (!(hints & THEME_CONSTANT_ROWS)) |
| 600 |
goto cols; |
| 601 |
} |
| 602 |
|
| 603 |
if (r != *(p++) || |
| 604 |
g != *(p++) || |
| 605 |
b != *(p++) || |
| 606 |
(n_channels == 4 && a != *(p++))) |
| 607 |
{ |
| 608 |
hints &= ~THEME_CONSTANT_ROWS; |
| 609 |
if (!(hints & THEME_MISSING)) |
| 610 |
goto cols; |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
cols: |
| 616 |
for (i = y0 + 1; i < y1; i++) |
| 617 |
{ |
| 618 |
guchar *base = data + y0 * rowstride + x0 * n_channels; |
| 619 |
guchar *p = data + i * rowstride + x0 * n_channels; |
| 620 |
|
| 621 |
if (memcmp (p, base, n_channels * (x1 - x0)) != 0) |
| 622 |
{ |
| 623 |
hints &= ~THEME_CONSTANT_COLS; |
| 624 |
return hints; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
return hints; |
| 629 |
} |
| 630 |
|
| 631 |
static void |
| 632 |
theme_pixbuf_compute_hints (ThemePixbuf *theme_pb) |
| 633 |
{ |
| 634 |
int i, j; |
| 635 |
gint width = gdk_pixbuf_get_width (theme_pb->pixbuf); |
| 636 |
gint height = gdk_pixbuf_get_height (theme_pb->pixbuf); |
| 637 |
|
| 638 |
if (theme_pb->border_left + theme_pb->border_right > width || |
| 639 |
theme_pb->border_top + theme_pb->border_bottom > height) |
| 640 |
{ |
| 641 |
g_warning ("Invalid borders specified for theme pixmap:\n" |
| 642 |
" %s,\n" |
| 643 |
"borders don't fit within the image", theme_pb->filename); |
| 644 |
if (theme_pb->border_left + theme_pb->border_right > width) |
| 645 |
{ |
| 646 |
theme_pb->border_left = width / 2; |
| 647 |
theme_pb->border_right = (width + 1) / 2; |
| 648 |
} |
| 649 |
if (theme_pb->border_bottom + theme_pb->border_top > height) |
| 650 |
{ |
| 651 |
theme_pb->border_top = height / 2; |
| 652 |
theme_pb->border_bottom = (height + 1) / 2; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
for (i = 0; i < 3; i++) |
| 657 |
{ |
| 658 |
gint y0, y1; |
| 659 |
|
| 660 |
switch (i) |
| 661 |
{ |
| 662 |
case 0: |
| 663 |
y0 = 0; |
| 664 |
y1 = theme_pb->border_top; |
| 665 |
break; |
| 666 |
case 1: |
| 667 |
y0 = theme_pb->border_top; |
| 668 |
y1 = height - theme_pb->border_bottom; |
| 669 |
break; |
| 670 |
default: |
| 671 |
y0 = height - theme_pb->border_bottom; |
| 672 |
y1 = height; |
| 673 |
break; |
| 674 |
} |
| 675 |
|
| 676 |
for (j = 0; j < 3; j++) |
| 677 |
{ |
| 678 |
gint x0, x1; |
| 679 |
|
| 680 |
switch (j) |
| 681 |
{ |
| 682 |
case 0: |
| 683 |
x0 = 0; |
| 684 |
x1 = theme_pb->border_left; |
| 685 |
break; |
| 686 |
case 1: |
| 687 |
x0 = theme_pb->border_left; |
| 688 |
x1 = width - theme_pb->border_right; |
| 689 |
break; |
| 690 |
default: |
| 691 |
x0 = width - theme_pb->border_right; |
| 692 |
x1 = width; |
| 693 |
break; |
| 694 |
} |
| 695 |
|
| 696 |
theme_pb->hints[i][j] = compute_hint (theme_pb->pixbuf, x0, x1, y0, y1); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
} |
| 701 |
|
| 702 |
void |
| 703 |
theme_pixbuf_set_border (ThemePixbuf *theme_pb, |
| 704 |
gint left, |
| 705 |
gint right, |
| 706 |
gint top, |
| 707 |
gint bottom) |
| 708 |
{ |
| 709 |
theme_pb->border_left = left; |
| 710 |
theme_pb->border_right = right; |
| 711 |
theme_pb->border_top = top; |
| 712 |
theme_pb->border_bottom = bottom; |
| 713 |
|
| 714 |
if (theme_pb->pixbuf) |
| 715 |
theme_pixbuf_compute_hints (theme_pb); |
| 716 |
} |
| 717 |
|
| 718 |
void |
| 719 |
theme_pixbuf_set_stretch (ThemePixbuf *theme_pb, |
| 720 |
gboolean stretch) |
| 721 |
{ |
| 722 |
theme_pb->stretch = stretch; |
| 723 |
|
| 724 |
if (theme_pb->pixbuf) |
| 725 |
theme_pixbuf_compute_hints (theme_pb); |
| 726 |
} |
| 727 |
|
| 728 |
static GdkPixbuf * |
| 729 |
pixbuf_cache_value_new (gchar *filename) |
| 730 |
{ |
| 731 |
GError *err = NULL; |
| 732 |
|
| 733 |
GdkPixbuf *result = gdk_pixbuf_new_from_file (filename, &err); |
| 734 |
if (!result) |
| 735 |
{ |
| 736 |
g_warning ("Pixbuf theme: Cannot load pixmap file %s: %s\n", |
| 737 |
filename, err->message); |
| 738 |
g_error_free (err); |
| 739 |
} |
| 740 |
|
| 741 |
return result; |
| 742 |
} |
| 743 |
|
| 744 |
GdkPixbuf * |
| 745 |
theme_pixbuf_get_pixbuf (ThemePixbuf *theme_pb) |
| 746 |
{ |
| 747 |
if (!theme_pb->pixbuf) |
| 748 |
{ |
| 749 |
if (!pixbuf_cache) |
| 750 |
pixbuf_cache = g_cache_new ((GCacheNewFunc)pixbuf_cache_value_new, |
| 751 |
(GCacheDestroyFunc)g_object_unref, |
| 752 |
(GCacheDupFunc)g_strdup, |
| 753 |
(GCacheDestroyFunc)g_free, |
| 754 |
g_str_hash, g_direct_hash, g_str_equal); |
| 755 |
|
| 756 |
theme_pb->pixbuf = g_cache_insert (pixbuf_cache, theme_pb->filename); |
| 757 |
|
| 758 |
if (theme_pb->stretch) |
| 759 |
theme_pixbuf_compute_hints (theme_pb); |
| 760 |
} |
| 761 |
|
| 762 |
return theme_pb->pixbuf; |
| 763 |
} |
| 764 |
|
| 765 |
void |
| 766 |
theme_pixbuf_render (ThemePixbuf *theme_pb, |
| 767 |
GdkWindow *window, |
| 768 |
GdkBitmap *mask, |
| 769 |
GdkRectangle *clip_rect, |
| 770 |
guint component_mask, |
| 771 |
gboolean center, |
| 772 |
gint x, |
| 773 |
gint y, |
| 774 |
gint width, |
| 775 |
gint height) |
| 776 |
{ |
| 777 |
GdkPixbuf *pixbuf = theme_pixbuf_get_pixbuf (theme_pb); |
| 778 |
gint src_x[4], src_y[4], dest_x[4], dest_y[4]; |
| 779 |
gint pixbuf_width = gdk_pixbuf_get_width (pixbuf); |
| 780 |
gint pixbuf_height = gdk_pixbuf_get_height (pixbuf); |
| 781 |
|
| 782 |
if (!pixbuf) |
| 783 |
return; |
| 784 |
|
| 785 |
if (theme_pb->stretch) |
| 786 |
{ |
| 787 |
if (component_mask & COMPONENT_ALL) |
| 788 |
component_mask = (COMPONENT_ALL - 1) & ~component_mask; |
| 789 |
|
| 790 |
src_x[0] = 0; |
| 791 |
src_x[1] = theme_pb->border_left; |
| 792 |
src_x[2] = pixbuf_width - theme_pb->border_right; |
| 793 |
src_x[3] = pixbuf_width; |
| 794 |
|
| 795 |
src_y[0] = 0; |
| 796 |
src_y[1] = theme_pb->border_top; |
| 797 |
src_y[2] = pixbuf_height - theme_pb->border_bottom; |
| 798 |
src_y[3] = pixbuf_height; |
| 799 |
|
| 800 |
dest_x[0] = x; |
| 801 |
dest_x[1] = x + theme_pb->border_left; |
| 802 |
dest_x[2] = x + width - theme_pb->border_right; |
| 803 |
dest_x[3] = x + width; |
| 804 |
|
| 805 |
if (dest_x[1] > dest_x[2]) |
| 806 |
{ |
| 807 |
component_mask &= ~(COMPONENT_NORTH | COMPONENT_SOUTH | COMPONENT_CENTER); |
| 808 |
dest_x[1] = dest_x[2] = (dest_x[1] + dest_x[2]) / 2; |
| 809 |
} |
| 810 |
|
| 811 |
dest_y[0] = y; |
| 812 |
dest_y[1] = y + theme_pb->border_top; |
| 813 |
dest_y[2] = y + height - theme_pb->border_bottom; |
| 814 |
dest_y[3] = y + height; |
| 815 |
|
| 816 |
if (dest_y[1] > dest_y[2]) |
| 817 |
{ |
| 818 |
component_mask &= ~(COMPONENT_EAST | COMPONENT_WEST | COMPONENT_CENTER); |
| 819 |
dest_y[1] = dest_y[2] = (dest_y[1] + dest_y[2]) / 2; |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
#define RENDER_COMPONENT(X1,X2,Y1,Y2) \ |
| 825 |
pixbuf_render (pixbuf, theme_pb->hints[Y1][X1], window, mask, clip_rect, \ |
| 826 |
src_x[X1], src_y[Y1], \ |
| 827 |
src_x[X2] - src_x[X1], src_y[Y2] - src_y[Y1], \ |
| 828 |
dest_x[X1], dest_y[Y1], \ |
| 829 |
dest_x[X2] - dest_x[X1], dest_y[Y2] - dest_y[Y1]); |
| 830 |
|
| 831 |
if (component_mask & COMPONENT_NORTH_WEST) |
| 832 |
RENDER_COMPONENT (0, 1, 0, 1); |
| 833 |
|
| 834 |
if (component_mask & COMPONENT_NORTH) |
| 835 |
RENDER_COMPONENT (1, 2, 0, 1); |
| 836 |
|
| 837 |
if (component_mask & COMPONENT_NORTH_EAST) |
| 838 |
RENDER_COMPONENT (2, 3, 0, 1); |
| 839 |
|
| 840 |
if (component_mask & COMPONENT_WEST) |
| 841 |
RENDER_COMPONENT (0, 1, 1, 2); |
| 842 |
|
| 843 |
if (component_mask & COMPONENT_CENTER) |
| 844 |
RENDER_COMPONENT (1, 2, 1, 2); |
| 845 |
|
| 846 |
if (component_mask & COMPONENT_EAST) |
| 847 |
RENDER_COMPONENT (2, 3, 1, 2); |
| 848 |
|
| 849 |
if (component_mask & COMPONENT_SOUTH_WEST) |
| 850 |
RENDER_COMPONENT (0, 1, 2, 3); |
| 851 |
|
| 852 |
if (component_mask & COMPONENT_SOUTH) |
| 853 |
RENDER_COMPONENT (1, 2, 2, 3); |
| 854 |
|
| 855 |
if (component_mask & COMPONENT_SOUTH_EAST) |
| 856 |
RENDER_COMPONENT (2, 3, 2, 3); |
| 857 |
} |
| 858 |
else |
| 859 |
{ |
| 860 |
if (center) |
| 861 |
{ |
| 862 |
x += (width - pixbuf_width) / 2; |
| 863 |
y += (height - pixbuf_height) / 2; |
| 864 |
|
| 865 |
pixbuf_render (pixbuf, 0, window, NULL, clip_rect, |
| 866 |
0, 0, |
| 867 |
pixbuf_width, pixbuf_height, |
| 868 |
x, y, |
| 869 |
pixbuf_width, pixbuf_height); |
| 870 |
} |
| 871 |
else |
| 872 |
{ |
| 873 |
cairo_t *cr = gdk_cairo_create (window); |
| 874 |
|
| 875 |
gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0); |
| 876 |
cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT); |
| 877 |
|
| 878 |
if (clip_rect) |
| 879 |
gdk_cairo_rectangle (cr, clip_rect); |
| 880 |
else |
| 881 |
cairo_rectangle (cr, x, y, width, height); |
| 882 |
|
| 883 |
cairo_fill (cr); |
| 884 |
|
| 885 |
cairo_destroy (cr); |
| 886 |
} |
| 887 |
} |
| 888 |
} |