… | |
… | |
49 | img->ref->ours = false; |
49 | img->ref->ours = false; |
50 | |
50 | |
51 | return img; |
51 | return img; |
52 | } |
52 | } |
53 | |
53 | |
|
|
54 | # if HAVE_PIXBUF |
54 | rxvt_img * |
55 | rxvt_img * |
55 | rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) |
56 | rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) |
56 | { |
57 | { |
57 | Display *dpy = s->display->dpy; |
58 | Display *dpy = s->display->dpy; |
58 | |
59 | |
… | |
… | |
166 | |
167 | |
167 | g_object_unref (pb); |
168 | g_object_unref (pb); |
168 | |
169 | |
169 | return img; |
170 | return img; |
170 | } |
171 | } |
|
|
172 | # endif |
171 | |
173 | |
172 | void |
174 | void |
173 | rxvt_img::destroy () |
175 | rxvt_img::destroy () |
174 | { |
176 | { |
175 | if (--ref->cnt) |
177 | if (--ref->cnt) |
… | |
… | |
209 | rxvt_img::unshare () |
211 | rxvt_img::unshare () |
210 | { |
212 | { |
211 | if (ref->cnt == 1 && ref->ours) |
213 | if (ref->cnt == 1 && ref->ours) |
212 | return; |
214 | return; |
213 | |
215 | |
214 | //TODO: maybe should reify instead |
|
|
215 | Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth); |
216 | Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth); |
216 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
217 | GC gc = XCreateGC (s->display->dpy, pm, 0, 0); |
217 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); |
218 | XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); |
218 | XFreeGC (s->display->dpy, gc); |
219 | XFreeGC (s->display->dpy, gc); |
219 | |
220 | |
… | |
… | |
265 | double *kernel = (double *)malloc (size * sizeof (double)); |
266 | double *kernel = (double *)malloc (size * sizeof (double)); |
266 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
267 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
267 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
268 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
268 | img->alloc (); |
269 | img->alloc (); |
269 | |
270 | |
270 | Picture src = src_picture (); |
|
|
271 | |
|
|
272 | XRenderPictureAttributes pa; |
271 | XRenderPictureAttributes pa; |
273 | pa.repeat = RepeatPad; |
272 | pa.repeat = RepeatPad; |
274 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); |
273 | Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); |
|
|
274 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
275 | |
275 | |
276 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
276 | Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); |
277 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
277 | Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); |
278 | XFreePixmap (dpy, tmp_pm); |
278 | XFreePixmap (dpy, tmp_pm); |
279 | |
279 | |
… | |
… | |
317 | |
317 | |
318 | return img; |
318 | return img; |
319 | } |
319 | } |
320 | |
320 | |
321 | static Picture |
321 | static Picture |
322 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) |
322 | create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha) |
323 | { |
323 | { |
324 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
324 | Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); |
325 | |
325 | |
326 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
326 | XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); |
327 | XRenderPictureAttributes pa; |
327 | XRenderPictureAttributes pa; |
328 | pa.repeat = True; |
328 | pa.repeat = RepeatNormal; |
|
|
329 | pa.component_alpha = component_alpha; |
329 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); |
330 | Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa); |
330 | |
331 | |
331 | XFreePixmap (dpy, pixmap); |
332 | XFreePixmap (dpy, pixmap); |
332 | |
333 | |
333 | return mask; |
334 | return mask; |
334 | } |
335 | } |
335 | |
336 | |
|
|
337 | static void |
|
|
338 | extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc) |
|
|
339 | { |
|
|
340 | int32_t x = clamp (c, cl0, cl1); |
|
|
341 | c -= x; |
|
|
342 | xc = x; |
|
|
343 | } |
|
|
344 | |
|
|
345 | static bool |
|
|
346 | extract (int32_t cl0, int32_t cl1, int32_t &r, int32_t &g, int32_t &b, int32_t &a, unsigned short &xr, unsigned short &xg, unsigned short &xb, unsigned short &xa) |
|
|
347 | { |
|
|
348 | extract (cl0, cl1, r, xr); |
|
|
349 | extract (cl0, cl1, g, xg); |
|
|
350 | extract (cl0, cl1, b, xb); |
|
|
351 | extract (cl0, cl1, a, xa); |
|
|
352 | |
|
|
353 | return xr | xg | xb | xa; |
|
|
354 | } |
|
|
355 | |
336 | void |
356 | void |
337 | rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
357 | rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a) |
338 | { |
358 | { |
|
|
359 | unshare (); |
|
|
360 | |
339 | Display *dpy = s->display->dpy; |
361 | Display *dpy = s->display->dpy; |
340 | Picture src = create_xrender_mask (dpy, pm, True); |
|
|
341 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
362 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
342 | |
363 | |
|
|
364 | // loop should not be needed for brightness, as only -1..1 makes sense |
|
|
365 | //while (r | g | b | a) |
|
|
366 | { |
|
|
367 | unsigned short xr, xg, xb, xa; |
343 | XRenderColor mask_c; |
368 | XRenderColor mask_c; |
344 | mask_c.red = r; |
369 | |
345 | mask_c.green = g; |
370 | if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
346 | mask_c.blue = b; |
371 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
347 | mask_c.alpha = a; |
372 | |
|
|
373 | if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
|
|
374 | { |
|
|
375 | XRenderColor mask_w = { 65535, 65535, 65535, 65535 }; |
|
|
376 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
|
|
377 | mask_c.red = -mask_c.red; //TODO: verify that doing clamp, assign, and negation does the right thing |
|
|
378 | mask_c.green = -mask_c.green; |
|
|
379 | mask_c.blue = -mask_c.blue; |
|
|
380 | mask_c.alpha = -mask_c.alpha; |
|
|
381 | XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); |
|
|
382 | XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); |
|
|
383 | } |
|
|
384 | } |
|
|
385 | |
|
|
386 | XRenderFreePicture (dpy, dst); |
|
|
387 | } |
|
|
388 | |
|
|
389 | void |
|
|
390 | rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a) |
|
|
391 | { |
|
|
392 | if (r < 0 || g < 0 || b < 0 || a < 0) |
|
|
393 | rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); |
|
|
394 | |
|
|
395 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
|
|
396 | img->alloc (); |
|
|
397 | |
|
|
398 | { |
|
|
399 | rxvt_color empty; |
|
|
400 | empty.set (s, rgba (0, 0, 0, 0)); |
|
|
401 | img->fill (empty); |
|
|
402 | } |
|
|
403 | |
|
|
404 | // premultiply (yeah, these are not exact, sue me or fix it) |
|
|
405 | r = (r * (a >> 8)) >> 8; |
|
|
406 | g = (g * (a >> 8)) >> 8; |
|
|
407 | b = (b * (a >> 8)) >> 8; |
|
|
408 | |
|
|
409 | Display *dpy = s->display->dpy; |
|
|
410 | |
|
|
411 | Picture src = src_picture (); |
|
|
412 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
|
|
413 | Picture mul = create_xrender_mask (dpy, pm, True, True); |
|
|
414 | |
|
|
415 | //TODO: this operator does not yet implement some useful contrast |
|
|
416 | while (r | g | b | a) |
|
|
417 | { |
|
|
418 | unsigned short xr, xg, xb, xa; |
|
|
419 | XRenderColor mask_c; |
|
|
420 | |
|
|
421 | if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) |
|
|
422 | { |
348 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
423 | XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1); |
349 | |
|
|
350 | XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
424 | XRenderComposite (dpy, PictOpAdd, src, mul, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
425 | } |
|
|
426 | } |
351 | |
427 | |
|
|
428 | XRenderFreePicture (dpy, mul); |
|
|
429 | XRenderFreePicture (dpy, dst); |
352 | XRenderFreePicture (dpy, src); |
430 | XRenderFreePicture (dpy, src); |
353 | XRenderFreePicture (dpy, dst); |
|
|
354 | } |
|
|
355 | |
431 | |
356 | void |
432 | ::swap (img->ref, ref); |
357 | rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) |
433 | ::swap (img->pm , pm ); |
358 | { |
|
|
359 | if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) |
|
|
360 | return; |
|
|
361 | |
434 | |
362 | Display *dpy = s->display->dpy; |
435 | delete img; |
363 | Picture src = create_xrender_mask (dpy, pm, True); |
|
|
364 | Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); |
|
|
365 | |
|
|
366 | XRenderColor mask_c; |
|
|
367 | mask_c.red = r; |
|
|
368 | mask_c.green = g; |
|
|
369 | mask_c.blue = b; |
|
|
370 | mask_c.alpha = a; |
|
|
371 | XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); |
|
|
372 | |
|
|
373 | XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
374 | |
|
|
375 | XRenderFreePicture (dpy, src); |
|
|
376 | XRenderFreePicture (dpy, dst); |
|
|
377 | } |
436 | } |
378 | |
437 | |
379 | rxvt_img * |
438 | rxvt_img * |
380 | rxvt_img::clone () |
439 | rxvt_img::clone () |
381 | { |
440 | { |
… | |
… | |
408 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
467 | if (x == 0 && y == 0 && w == ref->w && h == ref->h) |
409 | return clone (); |
468 | return clone (); |
410 | |
469 | |
411 | Display *dpy = s->display->dpy; |
470 | Display *dpy = s->display->dpy; |
412 | |
471 | |
|
|
472 | // add an alpha channel if... |
413 | bool alpha = !format->direct.alphaMask |
473 | bool alpha = !format->direct.alphaMask // pixmap has none yet |
414 | && (x || y) |
474 | && (x || y) // we need one because of non-zero offset |
415 | && repeat == RepeatNone; |
475 | && repeat == RepeatNone; // and we have no good pixels to fill with |
416 | |
476 | |
417 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
477 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
418 | img->alloc (); |
478 | img->alloc (); |
419 | |
479 | |
420 | Picture src = src_picture (); |
480 | Picture src = src_picture (); |
… | |
… | |
470 | |
530 | |
471 | for (int i = 0; i < 3; ++i) |
531 | for (int i = 0; i < 3; ++i) |
472 | for (int j = 0; j < 3; ++j) |
532 | for (int j = 0; j < 3; ++j) |
473 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
533 | xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); |
474 | |
534 | |
475 | #if 0 |
|
|
476 | xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO |
535 | xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO |
477 | xfrm.matrix [1][2] -= XDoubleToFixed (y); |
536 | xfrm.matrix [1][2] -= XDoubleToFixed (y); |
478 | #endif |
|
|
479 | |
537 | |
480 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
538 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
481 | XRenderSetPictureTransform (dpy, src, &xfrm); |
539 | XRenderSetPictureTransform (dpy, src, &xfrm); |
482 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
540 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); |
483 | |
541 | |
… | |
… | |
564 | { |
622 | { |
565 | rxvt_img *img2 = clone (); |
623 | rxvt_img *img2 = clone (); |
566 | Display *dpy = s->display->dpy; |
624 | Display *dpy = s->display->dpy; |
567 | Picture src = img->src_picture (); |
625 | Picture src = img->src_picture (); |
568 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
626 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
569 | Picture mask = create_xrender_mask (dpy, img->pm, False); |
627 | Picture mask = create_xrender_mask (dpy, img->pm, False, False); |
570 | |
628 | |
571 | XRenderColor mask_c; |
629 | XRenderColor mask_c; |
572 | |
630 | |
573 | mask_c.alpha = float_to_component (factor); |
631 | mask_c.alpha = float_to_component (factor); |
574 | mask_c.red = |
632 | mask_c.red = |