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