Debian Codegraph @

src/pygame_sdl2/transform.pyx

raw
1 # Copyright 2014 Patrick Dawson <[email protected]>
2 # Tom Rothamel <[email protected]>
3 #
4 # This software is provided 'as-is', without any express or implied
5 # warranty. In no event will the authors be held liable for any damages
6 # arising from the use of this software.
7 #
8 # Permission is granted to anyone to use this software for any purpose,
9 # including commercial applications, and to alter it and redistribute it
10 # freely, subject to the following restrictions:
11 #
12 # 1. The origin of this software must not be misrepresented; you must not
13 # claim that you wrote the original software. If you use this software
14 # in a product, an acknowledgment in the product documentation would be
15 # appreciated but is not required.
16 # 2. Altered source versions must be plainly marked as such, and must not be
17 # misrepresented as being the original software.
18 # 3. This notice may not be removed or altered from any source distribution.
19
20 from sdl2 cimport *
21 from sdl2_gfx cimport *
22 from pygame_sdl2.surface cimport *
23 from pygame_sdl2.error import error
24
25
26 def flip(Surface surface, bint xbool, bint ybool):
27
28 cdef Surface rv = Surface(surface.get_size(), surface.get_flags(), surface)
29 cdef SDL_Surface *src = surface.surface
30 cdef SDL_Surface *dest = rv.surface
31
32 cdef Uint32 *src_pixel
33 cdef Uint32 *src_end
34
35 cdef Uint32 *dest_pixel
36 cdef int dest_delta
37
38 cdef int y
39
40 with nogil:
41 for 0 <= y < src.h:
42
43 src_pixel = <Uint32 *> ((<Uint8 *> src.pixels) + y * src.pitch)
44 src_end = src_pixel + src.w
45
46 if ybool:
47 dest_pixel = <Uint32 *> ((<Uint8 *> dest.pixels) + (dest.h - y - 1) * dest.pitch)
48 else:
49 dest_pixel = <Uint32 *> ((<Uint8 *> dest.pixels) + y * dest.pitch)
50
51 if xbool:
52 dest_pixel += (src.w - 1)
53 dest_delta = -1
54 else:
55 dest_delta = 1
56
57 while src_pixel < src_end:
58 dest_pixel[0] = src_pixel[0]
59 src_pixel += 1
60 dest_pixel += dest_delta
61
62 return rv
63
64
65 def scale(Surface surface, size, Surface DestSurface=None):
66 cdef Surface surf_out
67 cdef int err = -1
68
69 if DestSurface == None:
70 surf_out = Surface(size, 0, surface)
71 else:
72 surf_out = DestSurface
73
74 with nogil:
75 SDL_SetSurfaceBlendMode(surface.surface, SDL_BLENDMODE_NONE)
76 err = SDL_UpperBlitScaled(surface.surface, NULL, surf_out.surface, NULL)
77
78 if err != 0:
79 raise error()
80
81 return surf_out
82
83 def rotate(Surface surface, angle):
84 # rotateSurface90Degrees always returns NULL without setting an error??
85 # cdef SDL_Surface *rsurf
86 # if angle % 90 == 0:
87 # rsurf = rotateSurface90Degrees(surface.surface, angle / 90)
88 # if rsurf == NULL:
89 # raise error()
90 return rotozoom(surface, angle, 1.0, SMOOTHING_OFF)
91
92 def rotozoom(Surface surface, double angle, double scale, int smooth=1):
93 cdef SDL_Surface *rsurf = NULL
94 cdef Surface rv
95
96 with nogil:
97 rsurf = rotozoomSurface(surface.surface, angle, scale, smooth)
98
99 if rsurf == NULL:
100 raise error()
101
102 rv = Surface(())
103 rv.take_surface(rsurf)
104
105 return rv
106
107 cdef uint32_t get_at(SDL_Surface *surf, int x, int y) noexcept nogil:
108 if x < 0:
109 x = 0
110 elif x >= surf.w:
111 x = surf.w - 1
112 if y < 0:
113 y = 0
114 elif y >= surf.h:
115 y = surf.h - 1
116
117 cdef uint32_t *p = <uint32_t*>surf.pixels
118 p += y * (surf.pitch // sizeof(uint32_t))
119 p += x
120 return p[0]
121
122 cdef void set_at(SDL_Surface *surf, int x, int y, uint32_t color) noexcept nogil:
123 cdef uint32_t *p = <uint32_t*>surf.pixels
124 p += y * (surf.pitch // sizeof(uint32_t))
125 p += x
126 p[0] = color
127
128 def scale2x(Surface surface, Surface DestSurface=None):
129 cdef int x, y
130
131 cdef uint32_t a, b, c, d, e, f, g, h, i
132 cdef uint32_t e0, e1, e2, e3
133
134 if surface.get_bytesize() != 4:
135 raise error("Surface has unsupported bytesize.")
136
137 cdef Surface surf_out = DestSurface
138 if surf_out == None:
139 surf_out = Surface((surface.get_width()*2, surface.get_height()*2), 0, surface)
140
141 surface.lock()
142 surf_out.lock()
143
144 cdef int width, height
145 width, height = surface.get_size()
146
147 with nogil:
148 for x in range(width):
149 for y in range(height):
150 # Get the surrounding 9 pixels.
151 a = get_at(surface.surface, x - 1, y - 1)
152 b = get_at(surface.surface, x, y - 1)
153 c = get_at(surface.surface, x + 1, y - 1)
154
155 d = get_at(surface.surface, x - 1, y)
156 e = get_at(surface.surface, x, y)
157 f = get_at(surface.surface, x + 1, y)
158
159 g = get_at(surface.surface, x - 1, y + 1)
160 h = get_at(surface.surface, x, y + 1)
161 i = get_at(surface.surface, x + 1, y + 1)
162
163 # Expand the center pixel.
164 if b != h and d != f:
165 e0 = d if d == b else e
166 e1 = f if b == f else e
167 e2 = d if d == h else e
168 e3 = f if h == f else e
169 else:
170 e0 = e1 = e2 = e3 = e
171
172 set_at(surf_out.surface, x*2, y*2, e0)
173 set_at(surf_out.surface, (x*2)+1, y*2, e1)
174 set_at(surf_out.surface, x*2, (y*2)+1, e2)
175 set_at(surf_out.surface, (x*2)+1, (y*2)+1, e3)
176
177 surf_out.unlock()
178 surface.unlock()
179
180 return surf_out
181
182 def smoothscale(Surface surface, size, Surface DestSurface=None):
183 cdef double scale_x = size[0] / <double>surface.surface.w
184 cdef double scale_y = size[1] / <double>surface.surface.h
185
186 cdef SDL_Surface *rsurf = NULL
187 cdef Surface rv
188
189 with nogil:
190 rsurf = rotozoomSurfaceXY(surface.surface, 0.0, scale_x, scale_y, SMOOTHING_ON)
191
192 if rsurf == NULL:
193 raise error()
194
195 rv = Surface(())
196 rv.take_surface(rsurf)
197
198 # This is inefficient.
199 if DestSurface:
200 with nogil:
201 SDL_SetSurfaceBlendMode(rv.surface, SDL_BLENDMODE_NONE)
202 SDL_UpperBlit(rv.surface, NULL, DestSurface.surface, NULL)
203
204 return rv