1# Licensed under GPL3 https://github.com/maoschanz/drawing/blob/master/LICENSE 2 3import cairo 4from gi.repository import Gdk 5from .abstract_filter import AbstractFilter 6 7class FilterContrast(AbstractFilter): 8 __gtype_name__ = 'FilterContrast' 9 10 def __init__(self, filter_id, filters_tool, *args): 11 super().__init__(filter_id, filters_tool) 12 self._label, self._spinbtn = self._tool.bar.add_spinbtn( \ 13 _("Increase contrast"), [0, 0, 100, 5, 10, 0], 3, '%') 14 # it's [value, lower, upper, step_increment, page_increment, page_size] 15 16 def get_preferred_minimum_width(self): 17 return self._label.get_preferred_width()[0] + \ 18 self._spinbtn.get_preferred_width()[0] 19 20 def set_filter_compact(self, is_active, is_compact): 21 self._label.set_visible(is_active and not is_compact) 22 self._spinbtn.set_visible(is_active) 23 24 def build_filter_op(self): 25 options = { 26 'percent': self._spinbtn.get_value() / 100 27 } 28 return options 29 30 def do_filter_operation(self, source_pixbuf, operation): 31 """Create a temp_pixbuf from a surface of the same size, whose cairo 32 context is first painted using the original surface (source operator), 33 which is basically a stupid way to copy it, and then painted again (with 34 alpha this time) using a blending mode that will increase the contrast. 35 Both OVERLAY, SOFT_LIGHT, and HARD_LIGHT can work as operators.""" 36 percent = operation['percent'] 37 surface = Gdk.cairo_surface_create_from_pixbuf(source_pixbuf, 0, None) 38 scale = self._tool.scale_factor() 39 surface.set_device_scale(scale, scale) 40 width = source_pixbuf.get_width() 41 height = source_pixbuf.get_height() 42 new_surface = cairo.ImageSurface(cairo.Format.ARGB32, width, height) 43 cairo_context = cairo.Context(new_surface) 44 cairo_context.set_source_surface(surface) 45 46 cairo_context.set_operator(cairo.Operator.SOURCE) 47 cairo_context.paint() 48 49 cairo_context.set_operator(cairo.Operator.SOFT_LIGHT) 50 # OVERLAY SOFT_LIGHT HARD_LIGHT 51 cairo_context.paint_with_alpha(percent) 52 53 new_pixbuf = Gdk.pixbuf_get_from_surface(new_surface, 0, 0, \ 54 new_surface.get_width(), new_surface.get_height()) 55 self._tool.get_image().set_temp_pixbuf(new_pixbuf) 56 57 ############################################################################ 58################################################################################ 59 60