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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
# -*- python -*-
# Copyright 2004-2024 Tom Rothamel <pytom@bishoujo.us>
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import print_function
def version():
return (6, 12, 0)
from sdl2 cimport *
from pygame_sdl2 cimport *
from pygame_sdl2 import Surface
cdef extern from "renpy.h":
void core_init()
void save_png_core(object, SDL_RWops *, int)
void pixellate32_core(object, object, int, int, int, int)
void pixellate24_core(object, object, int, int, int, int)
void map32_core(object, object,
char *,
char *,
char *,
char *)
void map24_core(object, object,
char *,
char *,
char *)
void linmap32_core(object, object,
int,
int,
int,
int)
void linmap24_core(object, object,
int,
int,
int)
void blur32_core(object, object, object, float, float)
void blur24_core(object, object, object, float, float)
void linblur32_core(object, object, int, int)
void linblur24_core(object, object, int, int)
void alphamunge_core(object, object, int, int, int, char *)
void scale32_core(object, object,
float, float, float, float,
float, float, float, float,
int)
void scale24_core(object, object,
float, float, float, float,
float, float, float, float)
void transform32_core(object, object,
float, float, float, float, float, float,
int, float, int)
void blend32_core(object, object, object, int)
void imageblend32_core(object, object, object, object, int, char *)
void colormatrix32_core(object, object,
float, float, float, float, float,
float, float, float, float, float,
float, float, float, float, float,
float, float, float, float, float)
void staticgray_core(object, object,
int, int, int, int, int, char *)
void PyErr_Clear()
from pygame_sdl2 import Surface as PygameSurface
def save_png(surf, file, compress=-1):
if not isinstance(surf, PygameSurface):
raise Exception("save_png requires a pygame Surface as its first argument.")
save_png_core(surf, RWopsFromPython(file), compress)
def pixellate(pysrc, pydst, avgwidth, avgheight, outwidth, outheight):
if not isinstance(pysrc, PygameSurface):
raise Exception("pixellate requires a pygame Surface as its first argument.")
if not isinstance(pydst, PygameSurface):
raise Exception("pixellate requires a pygame Surface as its second argument.")
if pysrc.get_bitsize() not in (24, 32):
raise Exception("pixellate requires a 24 or 32 bit surface.")
if pydst.get_bitsize() != pysrc.get_bitsize():
raise Exception("pixellate requires both surfaces have the same bitsize.")
# pysrc.lock()
# pydst.lock()
if pysrc.get_bitsize() == 32:
pixellate32_core(pysrc, pydst, avgwidth, avgheight, outwidth, outheight)
else:
pixellate24_core(pysrc, pydst, avgwidth, avgheight, outwidth, outheight)
# pydst.unlock()
# pysrc.unlock()
# Please note that r, g, b, and a are not necessarily red, green, blue
# and alpha. Instead, they are the first through fourth byte of data.
# The mapping between byte and color/alpha varies from system to
# system, and needs to be determined at a higher level.
def map(pysrc, pydst, r, g, b, a): # @ReservedAssignment
if not isinstance(pysrc, PygameSurface):
raise Exception("map requires a pygame Surface as its first argument.")
if not isinstance(pydst, PygameSurface):
raise Exception("map requires a pygame Surface as its second argument.")
if pysrc.get_bitsize() not in (24, 32):
raise Exception("map requires a 24 or 32 bit surface.")
if pydst.get_bitsize() != pysrc.get_bitsize():
raise Exception("map requires both surfaces have the same bitsize.")
if pydst.get_size() != pysrc.get_size():
raise Exception("map requires both surfaces have the same size.")
# pysrc.lock()
# pydst.lock()
if pysrc.get_bitsize() == 32:
map32_core(pysrc, pydst, r, g, b, a)
else:
map24_core(pysrc, pydst, r, g, b)
# pydst.unlock()
# pysrc.unlock()
# Please note that r, g, b, and a are not necessarily red, green, blue
# and alpha. Instead, they are the first through fourth byte of data.
# The mapping between byte and color/alpha varies from system to
# system, and needs to be determined at a higher level.
def linmap(pysrc, pydst, r, g, b, a):
if not isinstance(pysrc, PygameSurface):
raise Exception("map requires a pygame Surface as its first argument.")
if not isinstance(pydst, PygameSurface):
raise Exception("map requires a pygame Surface as its second argument.")
if pysrc.get_bitsize() not in (24, 32):
raise Exception("map requires a 24 or 32 bit surface.")
if pydst.get_bitsize() != pysrc.get_bitsize():
raise Exception("map requires both surfaces have the same bitsize.")
if pydst.get_size() != pysrc.get_size():
raise Exception("map requires both surfaces have the same size.")
# pysrc.lock()
# pydst.lock()
if pysrc.get_bitsize() == 32:
linmap32_core(pysrc, pydst, r, g, b, a)
else:
linmap24_core(pysrc, pydst, r, g, b)
# pydst.unlock()
# pysrc.unlock()
def blur(pysrc, pywrk, pydst, xrad, yrad=None):
if not isinstance(pysrc, PygameSurface):
raise Exception("blur requires a pygame Surface as its first argument.")
if not isinstance(pywrk, PygameSurface):
raise Exception("blur requires a pygame Surface as its second argument.")
if not isinstance(pydst, PygameSurface):
raise Exception("blur requires a pygame Surface as its third argument.")
if pysrc.get_bitsize() not in (24, 32):
raise Exception("blur requires a 24 or 32 bit surface.")
if pywrk.get_bitsize() != pysrc.get_bitsize() \
or pydst.get_bitsize() != pysrc.get_bitsize():
raise Exception("blur requires all surfaces have the same bitsize.")
if pywrk.get_size() != pysrc.get_size() \
or pydst.get_size() != pysrc.get_size():
raise Exception("blur requires all surfaces have the same size.")
if yrad is None:
yrad = xrad
if xrad < 0 or yrad < 0:
raise Exception("blur requires a positive radius.")
# pysrc.lock()
# pywrk.lock()
# pydst.lock()
if pysrc.get_bitsize() == 32:
blur32_core(pysrc, pywrk, pydst, xrad, yrad)
else:
blur24_core(pysrc, pywrk, pydst, xrad, yrad)
# pydst.unlock()
# pywrk.unlock()
# pysrc.unlock()
def linblur(pysrc, pydst, radius, vertical=0):
if not isinstance(pysrc, PygameSurface):
raise Exception("linblur requires a pygame Surface as its first argument.")
if not isinstance(pydst, PygameSurface):
raise Exception("linblur requires a pygame Surface as its second argument.")
if pysrc.get_bitsize() not in (24, 32):
raise Exception("linblur requires a 24 or 32 bit surface.")
if pydst.get_bitsize() != pysrc.get_bitsize():
raise Exception("linblur requires both surfaces have the same bitsize.")
if pydst.get_size() != pysrc.get_size():
raise Exception("linblur requires both surfaces have the same size.")
if radius < 1:
raise Exception("linblur requires a non-zero radius.")
# pysrc.lock()
# pydst.lock()
if pysrc.get_bitsize() == 32:
linblur32_core(pysrc, pydst, radius, vertical)
else:
linblur24_core(pysrc, pydst, radius, vertical)
# pydst.unlock()
# pysrc.unlock()
def alpha_munge(pysrc, pydst, srcchan, dstchan, amap):
if not isinstance(pysrc, PygameSurface):
raise Exception("alpha_munge requires a pygame Surface as its first argument.")
if not isinstance(pydst, PygameSurface):
raise Exception("alpha_munge requires a pygame Surface as its second argument.")
if pysrc.get_bitsize() not in (24, 32):
raise Exception("alpha_munge requires a 24 or 32 bit surface.")
if pydst.get_bitsize() != pysrc.get_bitsize():
raise Exception("alpha_munge requires both surfaces have the same bitsize.")
if pydst.get_size() != pysrc.get_size():
raise Exception("alpha_munge requires both surfaces have the same size.")
if pysrc.get_bitsize() == 24:
bytes = 3
else:
bytes = 4
# pysrc.lock()
# pydst.lock()
alphamunge_core(pysrc, pydst, bytes, srcchan, dstchan, amap)
# pydst.unlock()
# pysrc.unlock()
# def stretch(pysrc, pydst, rect):
# if not isinstance(pysrc, PygameSurface):
# raise Exception("stretch requires a pygame Surface as its first argument.")
# if not isinstance(pydst, PygameSurface):
# raise Exception("stretch requires a pygame Surface as its second argument.")
# if pydst.get_bitsize() != pysrc.get_bitsize():
# raise Exception("stretch requires both surfaces have the same bitsize.")
# if rect:
# x, y, w, h = rect
# else:
# x, y = 0, 0
# w, h = pysrc.get_size()
# return stretch_core(pysrc, pydst, x, y, w, h)
def bilinear(pysrc, pydst,
source_xoff=0.0, source_yoff=0.0, source_width=None, source_height=None,
dest_xoff=0.0, dest_yoff=0.0, dest_width=None, dest_height=None,
precise=0):
if not isinstance(pysrc, PygameSurface):
raise Exception("bilinear requires a pygame Surface as its first argument.")
if not isinstance(pydst, PygameSurface):
raise Exception("bilinear requires a pygame Surface as its second argument.")
if pysrc.get_bitsize() not in (24, 32):
raise Exception("bilinear requires a 24 or 32 bit surface.")
if pydst.get_bitsize() != pysrc.get_bitsize():
raise Exception("bilinear requires both surfaces have the same bitsize.")
if source_width is None or source_height is None:
source_width, source_height = pysrc.get_size()
if dest_width is None or dest_height is None:
dest_width, dest_height = pydst.get_size()
# pysrc.lock()
# pydst.lock()
if pysrc.get_bitsize() == 32:
scale32_core(pysrc, pydst,
source_xoff, source_yoff, source_width, source_height,
dest_xoff, dest_yoff, dest_width, dest_height, precise)
else:
scale24_core(pysrc, pydst,
source_xoff, source_yoff, source_width, source_height,
dest_xoff, dest_yoff, dest_width, dest_height)
# pydst.unlock()
# pysrc.unlock()
def check(surf):
if not isinstance(surf, PygameSurface):
raise Exception("Surface must be a pygame surface.")
if surf.get_bitsize() != 32:
raise Exception("Surface must be 32-bit.")
def transform(pysrc, pydst,
corner_x, corner_y,
xdx, ydx, xdy, ydy, a=1.0, precise=0):
check(pysrc)
check(pydst)
# pysrc.lock()
# pydst.lock()
transform32_core(pysrc, pydst,
corner_x, corner_y,
xdx, ydx,
xdy, ydy,
pysrc.get_shifts()[3], a, precise)
# pydst.unlock()
# pysrc.unlock()
def blend(pysrca, pysrcb, pydst, alpha):
check(pysrca)
check(pysrcb)
check(pydst)
# pysrca.lock()
# pysrcb.lock()
# pydst.lock()
blend32_core(pysrca, pysrcb, pydst, alpha)
# pydst.unlock()
# pysrcb.unlock()
# pysrca.unlock()
def imageblend(pysrca, pysrcb, pydst, pyimg, aoff, amap):
check(pysrca)
check(pysrcb)
check(pydst)
check(pyimg)
# pysrca.lock()
# pysrcb.lock()
# pydst.lock()
# pyimg.lock()
imageblend32_core(pysrca, pysrcb, pydst, pyimg, aoff, amap)
# pyimg.unlock()
# pydst.unlock()
# pysrcb.unlock()
# pysrca.unlock()
def colormatrix(pysrc, pydst,
c00, c01, c02, c03, c04,
c10, c11, c12, c13, c14,
c20, c21, c22, c23, c24,
c30, c31, c32, c33, c34):
check(pysrc)
check(pydst)
# pysrc.lock()
# pydst.lock()
colormatrix32_core(pysrc, pydst,
c00, c01, c02, c03, c04,
c10, c11, c12, c13, c14,
c20, c21, c22, c23, c24,
c30, c31, c32, c33, c34)
# pydst.unlock()
# pysrc.unlock()
def staticgray(pysrc, pydst, rmul, gmul, bmul, amul, shift, vmap):
PyErr_Clear()
staticgray_core(pysrc, pydst, rmul, gmul, bmul, amul, shift, vmap)
def subpixel(pysrc, pydst, xoffset, yoffset, shift):
pydst.blit(pysrc, (int(xoffset), int(yoffset)))
# Be sure to update scale.py when adding something new here!
import_pygame_sdl2()
core_init()
|