Issues (838)

tcod/image.py (1 issue)

1
2
from __future__ import absolute_import
3
4
import numpy as np
5
6
from tcod.libtcod import ffi, lib
7
from tcod.tcod import _console
8
9
class _ImageBufferArray(np.ndarray):
10
11
    def __new__(cls, image):
12
        size = image.height * image.width
13
        self = np.frombuffer(ffi.buffer(lib.TCOD_image_get_colors()[size]),
14
                             np.uint8)
15
        self = self.reshape((image.height, image.width, 3)).view(cls)
16
        self._image_c = image.cdata
17
        return self
18
19
    def __array_finalize__(self, obj):
20
        if obj is None:
21
            return
22
        self._image_c = getattr(obj, '_image_c', None)
23
24
    def __repr__(self):
25
        return repr(self.view(np.ndarray))
0 ignored issues
show
The Instance of _ImageBufferArray does not seem to have a member named view.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
26
27
    def __setitem__(self, index, value):
28
        """Must invalidate mipmaps on any write."""
29
        np.ndarray.__setitem__(self, index, value)
30
        if self._image_c is not None:
31
            lib.TCOD_image_invalidate_mipmaps(self._image_c)
32
33
34
class Image(object):
35
    """
36
    Args:
37
        width (int): Width of the new Image.
38
        height (int): Height of the new Image.
39
40
    Attributes:
41
        width (int): Read only width of this Image.
42
        height (int): Read only height of this Image.
43
    """
44
    def __init__(self, width, height):
45
        self.width, self.height = width, height
46
        self.image_c = ffi.gc(lib.TCOD_image_new(width, height),
47
                              lib.TCOD_image_delete)
48
49
    @classmethod
50
    def _from_cdata(cls, cdata):
51
        self = object.__new__(cls)
52
        self.image_c = cdata
53
        self.width, self.height = self._get_size()
54
        return self
55
56
    def clear(self, color):
57
        """Fill this entire Image with color.
58
59
        Args:
60
            color (Union[Tuple[int, int, int], Sequence[int]]):
61
                An (r, g, b) sequence or Color instance.
62
        """
63
        lib.TCOD_image_clear(self.image_c, color)
64
65
    def invert(self):
66
        """Invert all colors in this Image."""
67
        lib.TCOD_image_invert(self.image_c)
68
69
    def hflip(self):
70
        """Horizontally flip this Image."""
71
        lib.TCOD_image_hflip(self.image_c)
72
73
    def rotate90(self, rotations=1):
74
        """Rotate this Image clockwise in 90 degree steps.
75
76
        Args:
77
            rotations (int): Number of 90 degree clockwise rotations.
78
        """
79
        lib.TCOD_image_rotate90(self.image_c, rotations)
80
81
    def vflip(self):
82
        """Vertically flip this Image."""
83
        lib.TCOD_image_vflip(self.image_c)
84
85
    def scale(self, width, height):
86
        """Scale this Image to the new width and height.
87
88
        Args:
89
            width (int): The new width of the Image after scaling.
90
            height (int): The new height of the Image after scaling.
91
        """
92
        lib.TCOD_image_scale(self.image_c, width, height)
93
        self.width, self.height = width, height
94
95
    def set_key_color(self, color):
96
        """Set a color to be transparent during blitting functions.
97
98
        Args:
99
            color (Union[Tuple[int, int, int], Sequence[int]]):
100
                An (r, g, b) sequence or Color instance.
101
        """
102
        lib.TCOD_image_set_key_color(self.image_c, color)
103
104
    def get_alpha(self, x, y):
105
        """Get the Image alpha of the pixel at x, y.
106
107
        Args:
108
            x (int): X pixel of the image.  Starting from the left at 0.
109
            y (int): Y pixel of the image.  Starting from the top at 0.
110
111
        Returns:
112
            int: The alpha value of the pixel.
113
            With 0 being fully transparent and 255 being fully opaque.
114
        """
115
        return lib.TCOD_image_get_alpha(self.image_c, x, y)
116
117
    def refresh_console(self, console):
118
        """Update an Image created with :any:`tcod.image_from_console`.
119
120
        The console used with this function should have the same width and
121
        height as the Console given to :any:`tcod.image_from_console`.
122
        The font width and height must also be the same as when
123
        :any:`tcod.image_from_console` was called.
124
125
        Args:
126
            console (Console): A Console with a pixel width and height
127
                               matching this Image.
128
        """
129
        lib.TCOD_image_refresh_console(self.image_c, _console(console))
130
131
    def _get_size(self):
132
        """Return the (width, height) for this Image.
133
134
        Returns:
135
            Tuple[int, int]: The (width, height) of this Image
136
        """
137
        w = ffi.new('int *')
138
        h = ffi.new('int *')
139
        lib.TCOD_image_get_size(self.image_c, w, h)
140
        return w[0], h[0]
141
142
    def get_pixel(self, x, y):
143
        """Get the color of a pixel in this Image.
144
145
        Args:
146
            x (int): X pixel of the Image.  Starting from the left at 0.
147
            y (int): Y pixel of the Image.  Starting from the top at 0.
148
149
        Returns:
150
            Tuple[int, int, int]:
151
                An (r, g, b) tuple containing the pixels color value.
152
                Values are in a 0 to 255 range.
153
        """
154
        color = lib.TCOD_image_get_pixel(self.image_c, x, y)
155
        return color.r, color.g, color.b
156
157
    def get_mipmap_pixel(self, left, top, right, bottom):
158
        """Get the average color of a rectangle in this Image.
159
160
        Parameters should stay within the following limits:
161
        * 0 <= left < right < Image.width
162
        * 0 <= top < bottom < Image.height
163
164
        Args:
165
            left (int): Left corner of the region.
166
            top (int): Top corner of the region.
167
            right (int): Right corner of the region.
168
            bottom (int): Bottom corner of the region.
169
170
        Returns:
171
            Tuple[int, int, int]:
172
                An (r, g, b) tuple containing the averaged color value.
173
                Values are in a 0 to 255 range.
174
        """
175
        color = lib.TCOD_image_get_mipmap_pixel(self.image_c,
176
                                                left, top, right, bottom)
177
        return (color.r, color.g, color.b)
178
179
    def put_pixel(self, x, y, color):
180
        """Change a pixel on this Image.
181
182
        Args:
183
            x (int): X pixel of the Image.  Starting from the left at 0.
184
            y (int): Y pixel of the Image.  Starting from the top at 0.
185
            color (Union[Tuple[int, int, int], Sequence[int]]):
186
                An (r, g, b) sequence or Color instance.
187
        """
188
        lib.TCOD_image_put_pixel(self.image_c, x, y, color)
189
190
    def blit(self, console, x, y, bg_blend, scale_x, scale_y, angle):
191
        """Blit onto a Console using scaling and rotation.
192
193
        Args:
194
            console (Console): Blit destination Console.
195
            x (int): Console X position for the center of the Image blit.
196
            y (int): Console Y position for the center of the Image blit.
197
                     The Image blit is centered on this position.
198
            bg_blend (int): Background blending mode to use.
199
            scale_x (float): Scaling along Image x axis.
200
                             Set to 1 for no scaling.  Must be over 0.
201
            scale_y (float): Scaling along Image y axis.
202
                             Set to 1 for no scaling.  Must be over 0.
203
            angle (float): Rotation angle in radians. (Clockwise?)
204
        """
205
        lib.TCOD_image_blit(
206
            self.image_c, _console(console),
207
            x, y, bg_blend, scale_x, scale_y, angle)
208
209
    def blit_rect(self, console, x, y, width, height, bg_blend):
210
        """Blit onto a Console without scaling or rotation.
211
212
        Args:
213
            console (Console): Blit destination Console.
214
            x (int): Console tile X position starting from the left at 0.
215
            y (int): Console tile Y position starting from the top at 0.
216
            width (int): Use -1 for Image width.
217
            height (int): Use -1 for Image height.
218
            bg_blend (int): Background blending mode to use.
219
        """
220
        lib.TCOD_image_blit_rect(
221
            self.image_c, _console(console), x, y, width, height, bg_blend)
222
223
    def blit_2x(self, console, dest_x, dest_y,
224
                img_x=0, img_y=0, img_width=-1, img_height=-1):
225
        """Blit onto a Console with double resolution.
226
227
        Args:
228
            console (Console): Blit destination Console.
229
            dest_x (int): Console tile X position starting from the left at 0.
230
            dest_y (int): Console tile Y position starting from the top at 0.
231
            img_x (int): Left corner pixel of the Image to blit
232
            img_y (int): Top corner pixel of the Image to blit
233
            img_width (int): Width of the Image to blit.
234
                             Use -1 for the full Image width.
235
            img_height (int): Height of the Image to blit.
236
                              Use -1 for the full Image height.
237
        """
238
        lib.TCOD_image_blit_2x(
239
            self.image_c, _console(console),
240
            dest_x, dest_y, img_x, img_y, img_width, img_height)
241
242
    def save_as(self, filename):
243
        """Save the Image to a 32-bit .bmp or .png file.
244
245
        Args:
246
            filename (Text): File path to same this Image.
247
        """
248
        lib.TCOD_image_save(self.image_c, filename.encode('utf-8'))
249