Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

345 wiersze
12 KiB

  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://sam.zoy.org/wtfpl/COPYING for more details.
  13. #
  14. """ Libcaca Python bindings """
  15. import ctypes
  16. from caca import _lib
  17. from caca.canvas import _Canvas
  18. class _Dither(object):
  19. """ Model for Dither object.
  20. """
  21. def __init__(self):
  22. self._dither = 0
  23. def from_param(self):
  24. """ Required by ctypes module to call object as parameter of
  25. a C function.
  26. """
  27. return self._dither
  28. def __del__(self):
  29. if self._dither > 0:
  30. self._free()
  31. def __str__(self):
  32. return "<CacaDither>"
  33. def _free(self):
  34. """ Free a libcaca dither.
  35. """
  36. _lib.caca_free_dither.argtypes = [_Dither]
  37. _lib.caca_free_dither.restype = ctypes.c_int
  38. return _lib.caca_free_dither(self)
  39. class Dither(_Dither):
  40. """ Dither object, methods are libcaca functions with caca_dither_t as first
  41. argument.
  42. """
  43. def __init__(self, bpp, width, height, pitch, rmask, gmask, bmask, amask):
  44. """ Dither constructor
  45. bpp -- bitmap depth in bits per pixels
  46. width -- bitmap width in pixels
  47. height -- bitmap height in pixels
  48. pitch -- bitmap pitch in bytes
  49. rmask -- bitmask for red values
  50. gmask -- bitmask for green values
  51. bmask -- bitmask for blue values
  52. amask -- bitmask for alpha values
  53. """
  54. _lib.caca_create_dither.argtypes = [
  55. ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int,
  56. ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint,
  57. ]
  58. self._dither = _lib.caca_create_dither(bpp, width, height, pitch,
  59. rmask, gmask, bmask, amask)
  60. if self._dither == 0:
  61. raise DitherError, "Failed to create dither object"
  62. def set_palette(self, red, green, blue, alpha):
  63. """ Set the palette of an 8 bits per pixel bitmap. Values should be
  64. between 0 and 4095 (0xfff).
  65. red -- array of 256 red values
  66. green -- array of 256 green values
  67. blue -- array of 256 blue values
  68. alpha -- array of 256 alpha values
  69. """
  70. raise DitherError, "Not implemented"
  71. def set_brightness(self, brightness):
  72. """ Set the brightness of the dither object.
  73. brightness -- brightness value
  74. """
  75. if isinstance(brightness, int):
  76. brightness = float(brightness)
  77. _lib.caca_set_dither_brightness.argtypes = [_Dither, ctypes.c_float]
  78. _lib.caca_set_dither_brightness.restype = ctypes.c_int
  79. return _lib.caca_set_dither_brightness(self, brightness)
  80. def get_brightness(self):
  81. """ Get the brightness of the dither object.
  82. """
  83. _lib.caca_get_dither_brightness.argtypes = [_Dither]
  84. _lib.caca_get_dither_brightness.restype = ctypes.c_float
  85. return _lib.caca_get_dither_brightness(self)
  86. def set_gamma(self, gamma):
  87. """ Set the gamma of the dither object. A negative value causes colour
  88. inversion.
  89. gamma -- gamma value
  90. """
  91. if isinstance(gamma, int):
  92. gamma = float(gamma)
  93. _lib.caca_set_dither_gamma.argtypes = [_Dither, ctypes.c_float]
  94. _lib.caca_set_dither_gamma.restype = ctypes.c_int
  95. return _lib.caca_set_dither_gamma(self, gamma)
  96. def get_gamma(self):
  97. """ Get the gamma of the dither object.
  98. """
  99. _lib.caca_get_dither_gamma.argtypes = [_Dither]
  100. _lib.caca_get_dither_gamma.restype = ctypes.c_float
  101. return _lib.caca_get_dither_gamma(self)
  102. def set_contrast(self, contrast):
  103. """ Set the contrast of dither.
  104. contrast -- contrast value
  105. """
  106. if isinstance(contrast, int):
  107. contrast = float(contrast)
  108. _lib.caca_set_dither_contrast.argtypes = [_Dither, ctypes.c_float]
  109. _lib.caca_set_dither_contrast.restype = ctypes.c_int
  110. return _lib.caca_set_dither_contrast(self, contrast)
  111. def get_contrast(self):
  112. """ Get the contrast of the dither object.
  113. """
  114. _lib.caca_get_dither_contrast.argtypes = [_Dither]
  115. _lib.caca_get_dither_contrast.restype = ctypes.c_float
  116. return _lib.caca_get_dither_contrast(self)
  117. def set_antialias(self, value):
  118. """ Set dither antialiasing.
  119. value -- A string describing the antialiasing method that will
  120. be used for the dithering.
  121. + "none": no antialiasing
  122. + "prefilter" or "default": simple prefilter antialiasing. (default)
  123. """
  124. _lib.caca_set_dither_antialias.argtypes = [_Dither, ctypes.c_char_p]
  125. _lib.caca_set_dither_antialias.restype = ctypes.c_int
  126. return _lib.caca_set_dither_antialias(self, value)
  127. def get_antialias(self):
  128. """ Return the dither's current antialiasing method.
  129. """
  130. _lib.caca_get_dither_antialias.argtypes = [_Dither]
  131. _lib.caca_get_dither_antialias.restype = ctypes.c_char_p
  132. return _lib.caca_get_dither_antialias(self)
  133. def get_antialias_list(self):
  134. """ Get available antialiasing methods.
  135. """
  136. lst = []
  137. _lib.caca_get_dither_antialias_list.argtypes = [_Dither]
  138. _lib.caca_get_dither_antialias_list.restype = ctypes.POINTER(ctypes.c_char_p)
  139. for item in _lib.caca_get_dither_antialias_list(self):
  140. if item is not None and item != "":
  141. lst.append(item)
  142. else:
  143. #memory occurs otherwise
  144. break
  145. return lst
  146. def set_color(self, value):
  147. """ Choose colours used for dithering.
  148. value -- A string describing the colour set that will be
  149. used for the dithering.
  150. + "mono": use light gray on a black background
  151. + "gray": use white and two shades of gray on a black background
  152. + "8": use the 8 ANSI colours on a black background
  153. + "16": use the 16 ANSI colours on a black background
  154. + "fullgray": use black, white and two shades of gray
  155. for both the characters and the background
  156. + "full8": use the 8 ANSI colours for both the characters and
  157. the background
  158. + "full16" or "default": use the 16 ANSI colours for both the
  159. characters and the background (default)
  160. """
  161. _lib.caca_set_dither_color.argtypes = [_Dither, ctypes.c_char_p]
  162. _lib.caca_set_dither_color.restype = ctypes.c_int
  163. return _lib.caca_set_dither_color(self, value)
  164. def get_color(self):
  165. """ Get current colour mode.
  166. """
  167. _lib.caca_get_dither_color.argtypes = [_Dither]
  168. _lib.caca_get_dither_color.restype = ctypes.c_char_p
  169. return _lib.caca_get_dither_color(self)
  170. def get_color_list(self):
  171. """ Get available colour modes.
  172. """
  173. lst = []
  174. _lib.caca_get_dither_color_list.argtypes = [_Dither]
  175. _lib.caca_get_dither_color_list.restype = ctypes.POINTER(ctypes.c_char_p)
  176. for item in _lib.caca_get_dither_color_list(self):
  177. if item is not None and item != "":
  178. lst.append(item)
  179. else:
  180. #memory occurs otherwise
  181. break
  182. return lst
  183. def set_charset(self, value):
  184. """ Choose characters used for dithering.
  185. value -- A string describing the characters that need to be
  186. used for the dithering.
  187. + "ascii" or "default": use only ASCII characters (default).
  188. + "shades": use Unicode characters "U+2591 LIGHT SHADE",
  189. "U+2592 MEDIUM SHADE" and "U+2593 DARK SHADE". These characters are
  190. also present in the CP437 codepage available on DOS and VGA.
  191. + "blocks": use Unicode quarter-cell block combinations.
  192. These characters are only found in the Unicode set.
  193. """
  194. _lib.caca_set_dither_charset.argtypes = [_Dither, ctypes.c_char_p]
  195. _lib.caca_set_dither_charset.restype = ctypes.c_int
  196. return _lib.caca_set_dither_charset(self, value)
  197. def get_charset(self):
  198. """ Get current character set.
  199. """
  200. _lib.caca_get_dither_charset.argtypes = [_Dither]
  201. _lib.caca_get_dither_charset.restype = ctypes.c_char_p
  202. return _lib.caca_get_dither_charset(self)
  203. def get_charset_list(self):
  204. """ Get available dither character sets.
  205. """
  206. lst = []
  207. _lib.caca_get_dither_color_list.argtypes = [_Dither]
  208. _lib.caca_get_dither_color_list.restype = ctypes.POINTER(ctypes.c_char_p)
  209. for item in _lib.caca_get_dither_color_list(self):
  210. if item is not None and item != "":
  211. lst.append(item)
  212. else:
  213. #memory occurs otherwise
  214. break
  215. return lst
  216. def set_algorithm(self, value):
  217. """ Set dithering algorithm.
  218. value -- A string describing the algorithm that needs to be
  219. used for the dithering.
  220. + "none": no dithering is used, the nearest matching colour is used.
  221. + "ordered2": use a 2x2 Bayer matrix for dithering.
  222. + "ordered4": use a 4x4 Bayer matrix for dithering.
  223. + "ordered8": use a 8x8 Bayer matrix for dithering.
  224. + "random": use random dithering.
  225. + "fstein": use Floyd-Steinberg dithering (default).
  226. """
  227. _lib.caca_set_dither_algorithm.argtypes = [_Dither, ctypes.c_char_p]
  228. _lib.caca_set_dither_algorithm.restype = ctypes.c_int
  229. return _lib.caca_set_dither_algorithm(self, value)
  230. def get_algorithm(self):
  231. """ Get dithering algorithms.
  232. """
  233. _lib.caca_get_dither_algorithm.argtypes = [_Dither]
  234. _lib.caca_get_dither_algorithm.restype = ctypes.c_char_p
  235. return _lib.caca_get_dither_algorithm(self)
  236. def get_algorithm_list(self):
  237. """ Get dithering algorithms.
  238. """
  239. lst = []
  240. _lib.caca_get_dither_color_list.argtypes = [_Dither]
  241. _lib.caca_get_dither_color_list.restype = ctypes.POINTER(ctypes.c_char_p)
  242. for item in _lib.caca_get_dither_color_list(self):
  243. if item is not None and item != "":
  244. lst.append(item)
  245. else:
  246. #memory occurs otherwise
  247. break
  248. return lst
  249. def bitmap(self, canvas, x, y, width, height, pixels):
  250. """ Dither a bitmap on the canvas.
  251. canvas -- a handle to libcaca canvas
  252. x -- X coordinate of the upper-left corner of the drawing area
  253. y -- Y coordinate of the upper-left corner of the drawing area
  254. width -- width of the drawing area
  255. height -- height of the drawing area
  256. pixels -- bitmap's pixels
  257. """
  258. _lib.caca_dither_bitmap.argtypes = [
  259. _Canvas, ctypes.c_int, ctypes.c_int,
  260. ctypes.c_int, ctypes.c_int, _Dither,
  261. ctypes.c_char_p
  262. ]
  263. _lib.caca_dither_bitmap.restype = ctypes.c_int
  264. return _lib.caca_dither_bitmap(canvas, x, y, width, height, self, pixels)
  265. class DitherError(Exception):
  266. pass