"Fossies" - the Fresh Open Source Software Archive

Member "libcaca-0.99.beta20/python/caca/dither.py" (21 Apr 2017, 12172 Bytes) of package /linux/privat/libcaca-0.99.beta20.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "dither.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.99.beta19_vs_0.99.beta20.

    1 # -*- coding: utf-8 -*-
    2 #
    3 # libcaca       Colour ASCII-Art library
    4 #               Python language bindings
    5 # Copyright (c) 2010 Alex Foulon <alxf@lavabit.com>
    6 #               All Rights Reserved
    7 #
    8 # This library is free software. It comes without any warranty, to
    9 # the extent permitted by applicable law. You can redistribute it
   10 # and/or modify it under the terms of the Do What the Fuck You Want
   11 # to Public License, Version 2, as published by Sam Hocevar. See
   12 # http://www.wtfpl.net/ for more details.
   13 #
   14 
   15 """ Libcaca Python bindings """
   16 
   17 import ctypes
   18 
   19 from caca import _lib
   20 from caca.canvas import _Canvas
   21 
   22 
   23 class _DitherStruct(ctypes.Structure):
   24     pass
   25 
   26 class _Dither(object):
   27     """ Model for Dither object.
   28     """
   29     def __init__(self):
   30         self._dither = None
   31 
   32     def from_param(self):
   33         """ Required by ctypes module to call object as parameter of
   34             a C function.
   35         """
   36         return self._dither
   37 
   38     def __del__(self):
   39         if self._dither:
   40             self._free()
   41 
   42     def __str__(self):
   43         return "<CacaDither>"
   44 
   45     def _free(self):
   46         """ Free a libcaca dither.
   47         """
   48         _lib.caca_free_dither.argtypes = [_Dither]
   49         _lib.caca_free_dither.restype = ctypes.c_int
   50 
   51         return _lib.caca_free_dither(self)
   52 
   53 class Dither(_Dither):
   54     """ Dither object, methods are libcaca functions with caca_dither_t as first
   55         argument.
   56     """
   57     def __init__(self, bpp, width, height, pitch, rmask, gmask, bmask, amask):
   58         """ Dither constructor
   59 
   60             bpp     -- bitmap depth in bits per pixels
   61             width   -- bitmap width in pixels
   62             height  -- bitmap height in pixels
   63             pitch   -- bitmap pitch in bytes
   64             rmask   -- bitmask for red values
   65             gmask   -- bitmask for green values
   66             bmask   -- bitmask for blue values
   67             amask   -- bitmask for alpha values
   68         """
   69         _lib.caca_create_dither.argtypes = [
   70             ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int,
   71             ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint,
   72         ]
   73         _lib.caca_create_dither.restype = ctypes.POINTER(_DitherStruct)
   74 
   75         self._dither = _lib.caca_create_dither(bpp, width, height, pitch,
   76                                                rmask, gmask, bmask, amask)
   77 
   78         if self._dither == 0:
   79             raise DitherError("Failed to create dither object")
   80 
   81     def set_palette(self, red, green, blue, alpha):
   82         """ Set the palette of an 8 bits per pixel bitmap. Values should be
   83         between 0 and 4095 (0xfff).
   84 
   85             red     -- array of 256 red values
   86             green   -- array of 256 green values
   87             blue    -- array of 256 blue values
   88             alpha   -- array of 256 alpha values
   89         """
   90         raise DitherError("Not implemented")
   91 
   92     def set_brightness(self, brightness):
   93         """ Set the brightness of the dither object.
   94 
   95             brightness  -- brightness value
   96         """
   97         if isinstance(brightness, int):
   98             brightness = float(brightness)
   99 
  100         _lib.caca_set_dither_brightness.argtypes = [_Dither, ctypes.c_float]
  101         _lib.caca_set_dither_brightness.restype  = ctypes.c_int
  102 
  103         return _lib.caca_set_dither_brightness(self, brightness)
  104 
  105     def get_brightness(self):
  106         """ Get the brightness of the dither object.
  107         """
  108         _lib.caca_get_dither_brightness.argtypes = [_Dither]
  109         _lib.caca_get_dither_brightness.restype  = ctypes.c_float
  110 
  111         return _lib.caca_get_dither_brightness(self)
  112 
  113     def set_gamma(self, gamma):
  114         """ Set the gamma of the dither object. A negative value causes colour
  115         inversion.
  116 
  117             gamma -- gamma value
  118         """
  119         if isinstance(gamma, int):
  120             gamma = float(gamma)
  121 
  122         _lib.caca_set_dither_gamma.argtypes = [_Dither, ctypes.c_float]
  123         _lib.caca_set_dither_gamma.restype  = ctypes.c_int
  124 
  125         return _lib.caca_set_dither_gamma(self, gamma)
  126 
  127     def get_gamma(self):
  128         """ Get the gamma of the dither object.
  129         """
  130         _lib.caca_get_dither_gamma.argtypes = [_Dither]
  131         _lib.caca_get_dither_gamma.restype  = ctypes.c_float
  132 
  133         return _lib.caca_get_dither_gamma(self)
  134 
  135     def set_contrast(self, contrast):
  136         """ Set the contrast of dither.
  137 
  138             contrast -- contrast value
  139         """
  140         if isinstance(contrast, int):
  141             contrast = float(contrast)
  142 
  143         _lib.caca_set_dither_contrast.argtypes = [_Dither, ctypes.c_float]
  144         _lib.caca_set_dither_contrast.restype  = ctypes.c_int
  145 
  146         return _lib.caca_set_dither_contrast(self, contrast)
  147 
  148     def get_contrast(self):
  149         """ Get the contrast of the dither object.
  150         """
  151         _lib.caca_get_dither_contrast.argtypes = [_Dither]
  152         _lib.caca_get_dither_contrast.restype  = ctypes.c_float
  153 
  154         return _lib.caca_get_dither_contrast(self)
  155 
  156     def set_antialias(self, value):
  157         """ Set dither antialiasing.
  158 
  159             value -- A string describing the antialiasing method that will
  160                      be used for the dithering.
  161 
  162                      + "none": no antialiasing
  163                      + "prefilter" or "default": simple prefilter antialiasing. (default)
  164         """
  165         _lib.caca_set_dither_antialias.argtypes = [_Dither, ctypes.c_char_p]
  166         _lib.caca_set_dither_antialias.restype  = ctypes.c_int
  167 
  168         return _lib.caca_set_dither_antialias(self, value)
  169 
  170     def get_antialias(self):
  171         """ Return the dither's current antialiasing method.
  172         """
  173         _lib.caca_get_dither_antialias.argtypes = [_Dither]
  174         _lib.caca_get_dither_antialias.restype  = ctypes.c_char_p
  175 
  176         return _lib.caca_get_dither_antialias(self)
  177 
  178     def get_antialias_list(self):
  179         """ Get available antialiasing methods.
  180         """
  181         lst = []
  182 
  183         _lib.caca_get_dither_antialias_list.argtypes = [_Dither]
  184         _lib.caca_get_dither_antialias_list.restype  = ctypes.POINTER(ctypes.c_char_p)
  185 
  186         for item in _lib.caca_get_dither_antialias_list(self):
  187             if item is not None and item != "":
  188                 lst.append(item)
  189             else:
  190                 #memory occurs otherwise
  191                 break
  192 
  193         return lst
  194 
  195     def set_color(self, value):
  196         """ Choose colours used for dithering.
  197 
  198             value -- A string describing the colour set that will be
  199                      used for the dithering.
  200 
  201                      + "mono": use light gray on a black background
  202                      + "gray": use white and two shades of gray on a black background
  203                      + "8": use the 8 ANSI colours on a black background
  204                      + "16": use the 16 ANSI colours on a black background
  205                      + "fullgray": use black, white and two shades of gray
  206                        for both the characters and the background
  207                      + "full8": use the 8 ANSI colours for both the characters and
  208                        the background
  209                      + "full16" or "default": use the 16 ANSI colours for both the
  210                        characters and the background (default)
  211         """
  212         _lib.caca_set_dither_color.argtypes = [_Dither, ctypes.c_char_p]
  213         _lib.caca_set_dither_color.restype  = ctypes.c_int
  214 
  215         return _lib.caca_set_dither_color(self, value)
  216 
  217     def get_color(self):
  218         """ Get current colour mode.
  219         """
  220         _lib.caca_get_dither_color.argtypes = [_Dither]
  221         _lib.caca_get_dither_color.restype  = ctypes.c_char_p
  222 
  223         return _lib.caca_get_dither_color(self)
  224 
  225     def get_color_list(self):
  226         """ Get available colour modes.
  227         """
  228         lst = []
  229 
  230         _lib.caca_get_dither_color_list.argtypes = [_Dither]
  231         _lib.caca_get_dither_color_list.restype  = ctypes.POINTER(ctypes.c_char_p)
  232 
  233         for item in _lib.caca_get_dither_color_list(self):
  234             if item is not None and item != "":
  235                 lst.append(item)
  236             else:
  237                 #memory occurs otherwise
  238                 break
  239 
  240         return lst
  241 
  242     def set_charset(self, value):
  243         """ Choose characters used for dithering.
  244 
  245             value -- A string describing the characters that need to be
  246                      used for the dithering.
  247 
  248                      + "ascii" or "default": use only ASCII characters (default).
  249                      + "shades": use Unicode characters "U+2591 LIGHT SHADE",
  250                        "U+2592 MEDIUM SHADE" and "U+2593 DARK SHADE". These characters are
  251                        also present in the CP437 codepage available on DOS and VGA.
  252                      + "blocks": use Unicode quarter-cell block combinations.
  253                        These characters are only found in the Unicode set.
  254         """
  255         _lib.caca_set_dither_charset.argtypes = [_Dither, ctypes.c_char_p]
  256         _lib.caca_set_dither_charset.restype  = ctypes.c_int
  257 
  258         return _lib.caca_set_dither_charset(self, value)
  259 
  260     def get_charset(self):
  261         """ Get current character set.
  262         """
  263         _lib.caca_get_dither_charset.argtypes = [_Dither]
  264         _lib.caca_get_dither_charset.restype  = ctypes.c_char_p
  265 
  266         return _lib.caca_get_dither_charset(self)
  267 
  268     def get_charset_list(self):
  269         """ Get available dither character sets.
  270         """
  271         lst = []
  272 
  273         _lib.caca_get_dither_color_list.argtypes = [_Dither]
  274         _lib.caca_get_dither_color_list.restype  = ctypes.POINTER(ctypes.c_char_p)
  275 
  276         for item in _lib.caca_get_dither_color_list(self):
  277             if item is not None and item != "":
  278                 lst.append(item)
  279             else:
  280                 #memory occurs otherwise
  281                 break
  282 
  283         return lst
  284 
  285     def set_algorithm(self, value):
  286         """ Set dithering algorithm.
  287 
  288             value -- A string describing the algorithm that needs to be
  289                      used for the dithering.
  290 
  291                      + "none": no dithering is used, the nearest matching colour is used.
  292                      + "ordered2": use a 2x2 Bayer matrix for dithering.
  293                      + "ordered4": use a 4x4 Bayer matrix for dithering.
  294                      + "ordered8": use a 8x8 Bayer matrix for dithering.
  295                      + "random": use random dithering.
  296                      + "fstein": use Floyd-Steinberg dithering (default).
  297         """
  298         _lib.caca_set_dither_algorithm.argtypes = [_Dither, ctypes.c_char_p]
  299         _lib.caca_set_dither_algorithm.restype  = ctypes.c_int
  300 
  301         return _lib.caca_set_dither_algorithm(self, value)
  302 
  303     def get_algorithm(self):
  304         """ Get dithering algorithms.
  305         """
  306         _lib.caca_get_dither_algorithm.argtypes = [_Dither]
  307         _lib.caca_get_dither_algorithm.restype  = ctypes.c_char_p
  308 
  309         return _lib.caca_get_dither_algorithm(self)
  310 
  311     def get_algorithm_list(self):
  312         """ Get dithering algorithms.
  313         """
  314         lst = []
  315 
  316         _lib.caca_get_dither_color_list.argtypes = [_Dither]
  317         _lib.caca_get_dither_color_list.restype  = ctypes.POINTER(ctypes.c_char_p)
  318 
  319         for item in _lib.caca_get_dither_color_list(self):
  320             if item is not None and item != "":
  321                 lst.append(item)
  322             else:
  323                 #memory occurs otherwise
  324                 break
  325 
  326         return lst
  327 
  328     def bitmap(self, canvas, x, y, width, height, pixels):
  329         """ Dither a bitmap on the canvas.
  330 
  331             canvas  -- a handle to libcaca canvas
  332             x       -- X coordinate of the upper-left corner of the drawing area
  333             y       -- Y coordinate of the upper-left corner of the drawing area
  334             width   -- width of the drawing area
  335             height  -- height of the drawing area
  336             pixels  -- bitmap's pixels
  337         """
  338         _lib.caca_dither_bitmap.argtypes = [
  339             _Canvas, ctypes.c_int, ctypes.c_int,
  340             ctypes.c_int, ctypes.c_int, _Dither,
  341             ctypes.c_char_p
  342         ]
  343         _lib.caca_dither_bitmap.restype  = ctypes.c_int
  344 
  345         return _lib.caca_dither_bitmap(canvas, x, y, width, height, self, pixels)
  346 
  347 class DitherError(Exception):
  348     pass
  349