surfarray.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. ## pygame - Python Game Library
  2. ## Copyright (C) 2007 Marcus von Appen
  3. ##
  4. ## This library is free software; you can redistribute it and/or
  5. ## modify it under the terms of the GNU Library General Public
  6. ## License as published by the Free Software Foundation; either
  7. ## version 2 of the License, or (at your option) any later version.
  8. ##
  9. ## This library is distributed in the hope that it will be useful,
  10. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ## Library General Public License for more details.
  13. ##
  14. ## You should have received a copy of the GNU Library General Public
  15. ## License along with this library; if not, write to the Free
  16. ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. ##
  18. ## Marcus von Appen
  19. ## mva@sysfault.org
  20. """pygame module for accessing surface pixel data using array interfaces
  21. Functions to convert pixel data between pygame Surfaces and arrays. This
  22. module will only be functional when pygame can use the external Numpy or
  23. Numeric packages.
  24. Every pixel is stored as a single integer value to represent the red,
  25. green, and blue colors. The 8bit images use a value that looks into a
  26. colormap. Pixels with higher depth use a bit packing process to place
  27. three or four values into a single number.
  28. The arrays are indexed by the X axis first, followed by the Y
  29. axis. Arrays that treat the pixels as a single integer are referred to
  30. as 2D arrays. This module can also separate the red, green, and blue
  31. color values into separate indices. These types of arrays are referred
  32. to as 3D arrays, and the last index is 0 for red, 1 for green, and 2 for
  33. blue.
  34. Supported array types are
  35. numpy
  36. numeric (deprecated; will be removed in Pygame 1.9.3.)
  37. The default will be numpy, if installed. Otherwise, Numeric will be set
  38. as default if installed, and a deprecation warning will be issued. If
  39. neither numpy nor Numeric are installed, the module will raise an
  40. ImportError.
  41. The array type to use can be changed at runtime using the use_arraytype()
  42. method, which requires one of the above types as string.
  43. Note: numpy and Numeric are not completely compatible. Certain array
  44. manipulations, which work for one type, might behave differently or even
  45. completely break for the other.
  46. Additionally, in contrast to Numeric, numpy does use unsigned 16-bit
  47. integers. Images with 16-bit data will be treated as unsigned
  48. integers. Numeric instead uses signed integers for the representation,
  49. which is important to keep in mind, if you use the module's functions
  50. and wonder about the values.
  51. """
  52. # Try to import the necessary modules.
  53. import pygame._numpysurfarray as numpysf
  54. from pygame.pixelcopy import array_to_surface, make_surface as pc_make_surface
  55. def blit_array (surface, array):
  56. """pygame.surfarray.blit_array(Surface, array): return None
  57. Blit directly from a array values.
  58. Directly copy values from an array into a Surface. This is faster than
  59. converting the array into a Surface and blitting. The array must be the
  60. same dimensions as the Surface and will completely replace all pixel
  61. values. Only integer, ascii character and record arrays are accepted.
  62. This function will temporarily lock the Surface as the new values are
  63. copied.
  64. """
  65. return numpysf.blit_array (surface, array)
  66. def array2d (surface):
  67. """pygame.surfarray.array2d (Surface): return array
  68. Copy pixels into a 2d array.
  69. Copy the pixels from a Surface into a 2D array. The bit depth of the
  70. surface will control the size of the integer values, and will work
  71. for any type of pixel format.
  72. This function will temporarily lock the Surface as pixels are copied
  73. (see the Surface.lock - lock the Surface memory for pixel access
  74. method).
  75. """
  76. return numpysf.array2d (surface)
  77. def pixels2d (surface):
  78. """pygame.surfarray.pixels2d (Surface): return array
  79. Reference pixels into a 2d array.
  80. Create a new 2D array that directly references the pixel values in a
  81. Surface. Any changes to the array will affect the pixels in the
  82. Surface. This is a fast operation since no data is copied.
  83. Pixels from a 24-bit Surface cannot be referenced, but all other
  84. Surface bit depths can.
  85. The Surface this references will remain locked for the lifetime of
  86. the array (see the Surface.lock - lock the Surface memory for pixel
  87. access method).
  88. """
  89. return numpysf.pixels2d (surface)
  90. def array3d (surface):
  91. """pygame.surfarray.array3d (Surface): return array
  92. Copy pixels into a 3d array.
  93. Copy the pixels from a Surface into a 3D array. The bit depth of the
  94. surface will control the size of the integer values, and will work
  95. for any type of pixel format.
  96. This function will temporarily lock the Surface as pixels are copied
  97. (see the Surface.lock - lock the Surface memory for pixel access
  98. method).
  99. """
  100. return numpysf.array3d (surface)
  101. def pixels3d (surface):
  102. """pygame.surfarray.pixels3d (Surface): return array
  103. Reference pixels into a 3d array.
  104. Create a new 3D array that directly references the pixel values in a
  105. Surface. Any changes to the array will affect the pixels in the
  106. Surface. This is a fast operation since no data is copied.
  107. This will only work on Surfaces that have 24-bit or 32-bit
  108. formats. Lower pixel formats cannot be referenced.
  109. The Surface this references will remain locked for the lifetime of
  110. the array (see the Surface.lock - lock the Surface memory for pixel
  111. access method).
  112. """
  113. return numpysf.pixels3d (surface)
  114. def array_alpha (surface):
  115. """pygame.surfarray.array_alpha (Surface): return array
  116. Copy pixel alphas into a 2d array.
  117. Copy the pixel alpha values (degree of transparency) from a Surface
  118. into a 2D array. This will work for any type of Surface
  119. format. Surfaces without a pixel alpha will return an array with all
  120. opaque values.
  121. This function will temporarily lock the Surface as pixels are copied
  122. (see the Surface.lock - lock the Surface memory for pixel access
  123. method).
  124. """
  125. return numpysf.array_alpha (surface)
  126. def pixels_alpha (surface):
  127. """pygame.surfarray.pixels_alpha (Surface): return array
  128. Reference pixel alphas into a 2d array.
  129. Create a new 2D array that directly references the alpha values
  130. (degree of transparency) in a Surface. Any changes to the array will
  131. affect the pixels in the Surface. This is a fast operation since no
  132. data is copied.
  133. This can only work on 32-bit Surfaces with a per-pixel alpha value.
  134. The Surface this array references will remain locked for the
  135. lifetime of the array.
  136. """
  137. return numpysf.pixels_alpha (surface)
  138. def pixels_red (surface):
  139. """pygame.surfarray.pixels_red (Surface): return array
  140. Reference pixel red into a 2d array.
  141. Create a new 2D array that directly references the red values
  142. in a Surface. Any changes to the array will affect the pixels
  143. in the Surface. This is a fast operation since no data is copied.
  144. This can only work on 24-bit or 32-bit Surfaces.
  145. The Surface this array references will remain locked for the
  146. lifetime of the array.
  147. """
  148. return numpysf.pixels_red (surface)
  149. def pixels_green (surface):
  150. """pygame.surfarray.pixels_green (Surface): return array
  151. Reference pixel green into a 2d array.
  152. Create a new 2D array that directly references the green values
  153. in a Surface. Any changes to the array will affect the pixels
  154. in the Surface. This is a fast operation since no data is copied.
  155. This can only work on 24-bit or 32-bit Surfaces.
  156. The Surface this array references will remain locked for the
  157. lifetime of the array.
  158. """
  159. return numpysf.pixels_green (surface)
  160. def pixels_blue (surface):
  161. """pygame.surfarray.pixels_blue (Surface): return array
  162. Reference pixel blue into a 2d array.
  163. Create a new 2D array that directly references the blue values
  164. in a Surface. Any changes to the array will affect the pixels
  165. in the Surface. This is a fast operation since no data is copied.
  166. This can only work on 24-bit or 32-bit Surfaces.
  167. The Surface this array references will remain locked for the
  168. lifetime of the array.
  169. """
  170. return numpysf.pixels_blue (surface)
  171. def array_colorkey (surface):
  172. """pygame.surfarray.array_colorkey (Surface): return array
  173. Copy the colorkey values into a 2d array.
  174. Create a new array with the colorkey transparency value from each
  175. pixel. If the pixel matches the colorkey it will be fully
  176. tranparent; otherwise it will be fully opaque.
  177. This will work on any type of Surface format. If the image has no
  178. colorkey a solid opaque array will be returned.
  179. This function will temporarily lock the Surface as pixels are
  180. copied.
  181. """
  182. return numpysf.array_colorkey (surface)
  183. def make_surface(array):
  184. """pygame.surfarray.make_surface (array): return Surface
  185. Copy an array to a new surface.
  186. Create a new Surface that best resembles the data and format on the
  187. array. The array can be 2D or 3D with any sized integer values.
  188. """
  189. return numpysf.make_surface (array)
  190. def map_array (surface, array):
  191. """pygame.surfarray.map_array (Surface, array3d): return array2d
  192. Map a 3D array into a 2D array.
  193. Convert a 3D array into a 2D array. This will use the given Surface
  194. format to control the conversion. Palette surface formats are not
  195. supported.
  196. """
  197. return numpysf.map_array (surface, array)
  198. def use_arraytype (arraytype):
  199. """pygame.surfarray.use_arraytype (arraytype): return None
  200. DEPRECATED - only numpy arrays are now supported.
  201. """
  202. arraytype = arraytype.lower ()
  203. if arraytype != "numpy":
  204. raise ValueError("invalid array type")
  205. def get_arraytype ():
  206. """pygame.surfarray.get_arraytype (): return str
  207. DEPRECATED - only numpy arrays are now supported.
  208. """
  209. return "numpy"
  210. def get_arraytypes ():
  211. """pygame.surfarray.get_arraytypes (): return tuple
  212. DEPRECATED - only numpy arrays are now supported.
  213. """
  214. return ("numpy",)