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 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
|
import os
from ctypes import POINTER, Structure, c_int, c_char_p
from ctypes import POINTER as _P
from .dll import DLL, SDLFunc, AttributeDict
from .version import SDL_version, SDL_VERSIONNUM
from .surface import SDL_Surface
from .rwops import SDL_RWops
from .render import SDL_Texture, SDL_Renderer
from .error import SDL_SetError, SDL_GetError
__all__ = [
# Defines
"SDL_IMAGE_MAJOR_VERSION", "SDL_IMAGE_MINOR_VERSION", "SDL_IMAGE_PATCHLEVEL",
# Enums
"IMG_InitFlags",
"IMG_INIT_JPG", "IMG_INIT_PNG", "IMG_INIT_TIF", "IMG_INIT_WEBP",
"IMG_INIT_JXL", "IMG_INIT_AVIF",
# Macro Functions
"SDL_IMAGE_VERSION", "SDL_IMAGE_COMPILEDVERSION",
"SDL_IMAGE_VERSION_ATLEAST",
# Function Aliases
"IMG_GetError", "IMG_SetError",
# Python Functions
"get_dll_file"
]
try:
dll = DLL(
"SDL2_image", ["SDL2_image", "SDL2_image-2.0"], os.getenv("PYSDL2_DLL_PATH")
)
except RuntimeError as exc:
raise ImportError(exc)
def get_dll_file():
"""Gets the file name of the loaded SDL2_image library."""
return dll.libfile
_bind = dll.bind_function
# Constants, enums, type definitions, and macros
SDL_IMAGE_MAJOR_VERSION = 2
SDL_IMAGE_MINOR_VERSION = 8
SDL_IMAGE_PATCHLEVEL = 2
def SDL_IMAGE_VERSION(x):
x.major = SDL_IMAGE_MAJOR_VERSION
x.minor = SDL_IMAGE_MINOR_VERSION
x.patch = SDL_IMAGE_PATCHLEVEL
SDL_IMAGE_COMPILEDVERSION = SDL_VERSIONNUM(SDL_IMAGE_MAJOR_VERSION, SDL_IMAGE_MINOR_VERSION, SDL_IMAGE_PATCHLEVEL)
SDL_IMAGE_VERSION_ATLEAST = lambda x, y, z: (SDL_IMAGE_COMPILEDVERSION >= SDL_VERSIONNUM(x, y, z))
IMG_InitFlags = c_int
IMG_INIT_JPG = 0x00000001
IMG_INIT_PNG = 0x00000002
IMG_INIT_TIF = 0x00000004
IMG_INIT_WEBP = 0x00000008
IMG_INIT_JXL = 0x00000010
IMG_INIT_AVIF = 0x00000020
class IMG_Animation(Structure):
"""A structure containing the frame data for an animation.
When working with this data structure in Python, you need to be careful not
to access an out-of-range frame or delay number to avoid illegal memory
access problems. To convert the animation data to a more Pythonic format,
you can do something similar to the example below::
# Load animation and verify it loaded correctly
anim = sdlimage.IMG_LoadAnimation("test.gif").contents
if not anim:
err = sdlimage.IMG_GetError()
raise RuntimeError(err.encode("utf-8"))
# Load frames/delays from the IMG_Animation structure to lists
frames = []
delays = []
for frame in range(anim.count):
frames.append(anim.frames[frame].contents)
delays.append(anim.delays[frame])
Note that even if you copy the frame surfaces into a Python list, the
pixel data will still be stored within the animation object. As such, you
cannot call :func:`IMG_FreeAnimation` on an object until you are fully
done with its data.
Attributes:
w (int): The width (in pixels) of the animation.
h (int): The height (in pixels) of the animation.
count (int): The number of frames in the animation.
frames (POINTER(POINTER(:obj:`SDL_Surface`))): An array of pointers
(of length ``count``) to the SDL surfaces for the frames of the
animation.
delays (POINTER(int)): An array (of length ``count``) containing the
delays (in milliseconds) between each frame in the animation.
"""
_fields_ = [
("w", c_int),
("h", c_int),
("count", c_int),
("frames", POINTER(POINTER(SDL_Surface))),
("delays", POINTER(c_int)),
]
# Raw ctypes function definitions
_funcdefs = [
SDLFunc("IMG_Linked_Version", None, _P(SDL_version)),
SDLFunc("IMG_Init", [c_int], c_int),
SDLFunc("IMG_Quit"),
SDLFunc("IMG_LoadTyped_RW", [_P(SDL_RWops), c_int, c_char_p], _P(SDL_Surface)),
SDLFunc("IMG_Load", [c_char_p], _P(SDL_Surface)),
SDLFunc("IMG_Load_RW", [_P(SDL_RWops), c_int], _P(SDL_Surface)),
SDLFunc("IMG_LoadTexture", [_P(SDL_Renderer), c_char_p], _P(SDL_Texture)),
SDLFunc("IMG_LoadTexture_RW", [_P(SDL_Renderer), _P(SDL_RWops), c_int], _P(SDL_Texture)),
SDLFunc("IMG_LoadTextureTyped_RW", [_P(SDL_Renderer), _P(SDL_RWops), c_int, c_char_p], _P(SDL_Texture)),
SDLFunc("IMG_isAVIF", [_P(SDL_RWops)], c_int, added='2.6.0'),
SDLFunc("IMG_isICO", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_isCUR", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_isBMP", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_isGIF", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_isJPG", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_isJXL", [_P(SDL_RWops)], c_int, added='2.6.0'),
SDLFunc("IMG_isLBM", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_isPCX", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_isPNG", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_isPNM", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_isSVG", [_P(SDL_RWops)], c_int, added='2.0.2'),
SDLFunc("IMG_isQOI", [_P(SDL_RWops)], c_int, added='2.6.0'),
SDLFunc("IMG_isTIF", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_isXCF", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_isXPM", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_isXV", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_isWEBP", [_P(SDL_RWops)], c_int),
SDLFunc("IMG_LoadAVIF_RW", [_P(SDL_RWops)], _P(SDL_Surface), added='2.6.0'),
SDLFunc("IMG_LoadICO_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadCUR_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadBMP_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadGIF_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadJPG_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadJXL_RW", [_P(SDL_RWops)], _P(SDL_Surface), added='2.6.0'),
SDLFunc("IMG_LoadLBM_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadPCX_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadPNG_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadPNM_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadSVG_RW", [_P(SDL_RWops)], _P(SDL_Surface), added='2.0.2'),
SDLFunc("IMG_LoadQOI_RW", [_P(SDL_RWops)], _P(SDL_Surface), added='2.6.0'),
SDLFunc("IMG_LoadTGA_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadTIF_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadXCF_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadXPM_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadXV_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadWEBP_RW", [_P(SDL_RWops)], _P(SDL_Surface)),
SDLFunc("IMG_LoadSizedSVG_RW", [_P(SDL_RWops), c_int, c_int], _P(SDL_Surface), added='2.6.0'),
SDLFunc("IMG_ReadXPMFromArray", [_P(c_char_p)], _P(SDL_Surface)),
SDLFunc("IMG_ReadXPMFromArrayToRGB888", [_P(c_char_p)], _P(SDL_Surface), added='2.6.0'),
SDLFunc("IMG_SaveJPG", [_P(SDL_Surface), c_char_p, c_int], c_int, added='2.0.2'),
SDLFunc("IMG_SaveJPG_RW", [_P(SDL_Surface), _P(SDL_RWops), c_int, c_int], c_int, added='2.0.2'),
SDLFunc("IMG_SavePNG", [_P(SDL_Surface), c_char_p], c_int),
SDLFunc("IMG_SavePNG_RW", [_P(SDL_Surface), _P(SDL_RWops), c_int], c_int),
SDLFunc("IMG_LoadAnimation", [c_char_p], _P(IMG_Animation), added='2.6.0'),
SDLFunc("IMG_LoadAnimation_RW", [_P(SDL_RWops), c_int], _P(IMG_Animation), added='2.6.0'),
SDLFunc("IMG_LoadAnimationTyped_RW", [_P(SDL_RWops), c_int, c_char_p], _P(IMG_Animation), added='2.6.0'),
SDLFunc("IMG_FreeAnimation", [_P(IMG_Animation)], added='2.6.0'),
SDLFunc("IMG_LoadGIFAnimation_RW", [_P(SDL_RWops)], _P(IMG_Animation), added='2.6.0'),
SDLFunc("IMG_LoadWEBPAnimation_RW", [_P(SDL_RWops)], _P(IMG_Animation), added='2.8.0'),
]
_ctypes = AttributeDict()
for f in _funcdefs:
_ctypes[f.name] = _bind(f.name, f.args, f.returns, f.added)
__all__.append(f.name) # Add all bound functions to module namespace
# Python wrapper functions
def IMG_Linked_Version():
"""Gets the version of the dynamically-linked **SDL_image** library.
Returns:
POINTER(:obj:`SDL_version`): A pointer to a structure containing the
version of the SDL_image library currently in use.
"""
return _ctypes.IMG_Linked_Version()
def IMG_Init(flags):
"""Initializes the SDL_image library.
Calling this function enables support for the JPEG, PNG, TIF, WebP, JPEG XL,
and/or AVIF image formats as requested by the init flags. All other image
formats can be loaded or used regardless of whether this has been called.
The following init flags are supported:
======= ================= ======
Format Init flag Since
======= ================= ======
JPEG ``IMG_INIT_JPG`` 2.0.0
PNG ``IMG_INIT_PNG`` 2.0.0
TIFF ``IMG_INIT_TIF`` 2.0.0
WebP ``IMG_INIT_WEBP`` 2.0.0
JPEG XL ``IMG_INIT_JXL`` 2.6.0
AVIF ``IMG_INIT_AVIF`` 2.6.0
======= ================= ======
This can be called multiple times to enable support for these formats
separately, or can initialize multiple formats at once by passing a set of
flags as a bitwise OR. You can also call this function with 0 as a flag
to check which image libraries have already been loaded, or to test
whether a given image format is available on the current system::
# Initialize JPEG and PNG support separately
for flag in [IMG_INIT_JPG, IMG_INIT_PNG]:
IMG_Init(flag)
err = IMG_GetError() # check for any errors loading library
if len(err):
print(err)
# Initialize JPEG and PNG support at the same time
flags = IMG_INIT_JPG | IMG_INIT_PNG
IMG_Init(flags)
if IMG_Init(0) != flags: # verify both libraries loaded properly
print(IMG_GetError())
.. note:: AVIF and JPEG XL support are not included in the official
SDL_image binaries, and are likewise not enabled on most Linux
distributions.
Args:
flags (int): A bitwise OR'd set of the flags of the image formats to
load support for.
Returns:
int: A bitmask of all the currently initialized image loaders.
"""
return _ctypes["IMG_Init"](flags)
def IMG_Quit():
"""De-initializes the SDL_image library.
Calling this function disables JPEG, PNG, TIF, and WEBP support and frees
all associated memory. Once this has been called, you can re-initialize
support for those image formats using :func:`IMG_Init` and the corresponding
init flags.
You only need to call this function once, no matter how many times
:func:`IMG_Init` was called.
"""
return _ctypes["IMG_Quit"]()
def IMG_LoadTyped_RW(src, freesrc, type):
"""Loads an image file from an SDL file object as a specific format.
This function allows you to explicitly specify the format type of the image
to load. Here are the different possible image format strings:
============= ===========================
Format String Format Type
============= ===========================
b"TGA" TrueVision Targa
b"CUR" Windows Cursor
b"ICO" Windows Icon
b"BMP" Windows Bitmap
b"GIF" Graphics Interchange Format
b"JPG" JPEG
b"LBM" Interleaved Bitmap
b"PCX" IBM PC Paintbrush
b"PNG" Portable Network Graphics
b"PNM" Portable Anymap
b"SVG" Scalable Vector Graphics
b"TIF" Tagged Image File Format
b"XCF" GIMP native format
b"XPM" X11 Pixmap
b"XV" XV Thumbnail
b"WEBP" WebP
b"QOI" Quite OK Image
b"JXL" JPEG XL
b"AVIF" AV1 Image File Format
============= ===========================
Note that loading QOI, JXL, and AVIF images requires SDL_image 2.6.0 or
newer. See :func:`IMG_Load` for more information.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the image.
freesrc (int): If non-zero, the input file object will be closed and
freed after it has been read.
type (bytes): A bytestring indicating the image format with which the
file object should be loaded.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to the new surface containing the
image, or a null pointer if there was an error.
"""
return _ctypes["IMG_LoadTyped_RW"](src, freesrc, type)
def IMG_Load(file):
"""Loads an image file to a new surface.
This can load all supported image files, including TGA as long as the
filename ends with ".tga".
It is best to call this outside of event loops and keep the loaded
images around until you are really done with them, as disk speed and
image conversion to a surface can be slow.
Once you are done with a loaded image, you can call :func:`SDL_FreeSurface`
on the returned surface pointer to free up the memory associated with it.
If the image format supports a transparent pixel, SDL_image will set the
colorkey for the surface. You can enable RLE acceleration on the surface
afterwards by calling::
SDL_SetColorKey(image, SDL_RLEACCEL, image.contents.format.colorkey)
.. note::
If the loader for the image's format requires initialization (e.g. PNG)
and it is not already initialized, this function will attempt to load
it automatically.
Args:
file (bytes): A UTF8-encoded bytestring containing the path to the
image file to load.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to the new surface containing the
image, or a null pointer if there was an error.
"""
return _ctypes["IMG_Load"](file)
def IMG_Load_RW(src, freesrc):
"""Loads an image file from an SDL file object to a new surface.
This can load all supported formats, *except* TGA. See :func:`IMG_Load`
for more information.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the image.
freesrc (int): If non-zero, the input file object will be closed and
freed after it has been read.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to the new surface containing the
image, or a null pointer if there was an error.
"""
return _ctypes["IMG_Load_RW"](src, freesrc)
def IMG_LoadTexture(renderer, file):
"""Loads an image file to a new texture using a given renderer.
This function can load all supported image files, including TGA as long as
the filename ends with ".tga".
It is best to call this outside of event loops and keep the loaded
images around until you are really done with them, as disk speed and
image conversion to a texture can be slow.
Once you are done with a loaded image, you can call
:func:`SDL_DestroyTexture` on the returned texture pointer to free up the
memory associated with it.
.. note::
If the image loader for the format of the given image requires
initialization (e.g. PNG) and it is not already initialized, this
function will attempt to load it automatically.
Args:
renderer (:obj:`SDL_Renderer`): The SDL rendering context with which to
create the texture.
file (bytes): A UTF8-encoded bytestring containing the path to the
image file to load.
Returns:
POINTER(:obj:`SDL_Texture`): A pointer to the new texture containing
the image, or a null pointer if there was an error.
"""
return _ctypes["IMG_LoadTexture"](renderer, file)
def IMG_LoadTexture_RW(renderer, src, freesrc):
"""Loads an image from a file object to a texture using a given renderer.
This function can load all supported formats, *except* TGA. See
:func:`IMG_LoadTexture` for more information.
Args:
renderer (:obj:`SDL_Renderer`): The SDL rendering context with which to
create the texture.
src (:obj:`SDL_RWops`): The file object from which to load the image.
freesrc (int): If non-zero, the input file object will be closed and
freed after it has been read.
Returns:
POINTER(:obj:`SDL_Texture`): A pointer to the new texture containing
the image, or a null pointer if there was an error.
"""
return _ctypes["IMG_LoadTexture_RW"](renderer, src, freesrc)
def IMG_LoadTextureTyped_RW(renderer, src, freesrc, type):
"""Loads an image file from a file object to a texture as a specific format.
This function allows you to explicitly specify the format type of the image
to load. The different possible format strings are listed in the
documentation for :func:`IMG_LoadTyped_RW`.
See :func:`IMG_LoadTexture` for more information.
Args:
renderer (:obj:`SDL_Renderer`): The SDL rendering context with which to
create the texture.
src (:obj:`SDL_RWops`): The file object from which to load the image.
freesrc (int): If non-zero, the input file object will be closed and
freed after it has been read.
type (bytes): A bytestring indicating the image format with which the
file object should be loaded.
Returns:
POINTER(:obj:`SDL_Texture`): A pointer to the new texture containing
the image, or a null pointer if there was an error.
"""
return _ctypes["IMG_LoadTextureTyped_RW"](renderer, src, freesrc, type)
def IMG_isAVIF(src):
"""Tests whether a file object contains an AVIF image.
`Note: Added in SDL_image 2.6.0`
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if AVIFs are supported and file is a valid AVIF, otherwise 0.
"""
return _ctypes["IMG_isAVIF"](src)
def IMG_isICO(src):
"""Tests whether a file object contains an ICO (Windows icon) image.
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if BMPs are supported and file is a valid ICO, otherwise 0.
"""
return _ctypes["IMG_isICO"](src)
def IMG_isCUR(src):
"""Tests whether a file object contains a CUR (Windows cursor) image.
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if BMPs are supported and file is a valid CUR, otherwise 0.
"""
return _ctypes["IMG_isCUR"](src)
def IMG_isBMP(src):
"""Tests whether a file object contains a BMP (Windows bitmap) image.
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if BMPs are supported and file is a valid BMP, otherwise 0.
"""
return _ctypes["IMG_isBMP"](src)
def IMG_isGIF(src):
"""Tests whether a file object contains a GIF image.
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if GIFs are supported and file is a valid GIF, otherwise 0.
"""
return _ctypes["IMG_isGIF"](src)
def IMG_isJPG(src):
"""Tests whether a file object contains a JPEG image.
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if JPEGs are supported and file is a valid JPEG, otherwise 0.
"""
return _ctypes["IMG_isJPG"](src)
def IMG_isJXL(src):
"""Tests whether a file object contains a JPEG XL image.
`Note: Added in SDL_image 2.6.0`
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if JPEG XL is supported and file is a valid JXL, otherwise 0.
"""
return _ctypes["IMG_isJXL"](src)
def IMG_isLBM(src):
"""Tests whether a file object contains an LBM (Interleaved Bitmap) image.
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if LBMs are supported and file is a valid LBM, otherwise 0.
"""
return _ctypes["IMG_isLBM"](src)
def IMG_isPCX(src):
"""Tests whether a file object contains a PCX (IBM PC Paintbrush) image.
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if PCXs are supported and file is a valid PCX, otherwise 0.
"""
return _ctypes["IMG_isPCX"](src)
def IMG_isPNG(src):
"""Tests whether a file object contains a PNG image.
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if PNGs are supported and file is a valid PNG, otherwise 0.
"""
return _ctypes["IMG_isPNG"](src)
def IMG_isPNM(src):
"""Tests whether a file object contains a PNM (Portable Anymap) image.
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if PNMs are supported and file is a valid PNM, otherwise 0.
"""
return _ctypes["IMG_isPNM"](src)
def IMG_isSVG(src):
"""Tests whether a file object contains an SVG image.
`Note: Added in SDL_image 2.0.2`
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if SVGs are supported and file is a valid SVG, otherwise 0.
"""
return _ctypes["IMG_isSVG"](src)
def IMG_isQOI(src):
"""Tests whether a file object contains a QOI image.
`Note: Added in SDL_image 2.6.0`
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if QOI is supported and file is a valid QOI, otherwise 0.
"""
return _ctypes["IMG_isQOI"](src)
def IMG_isTIF(src):
"""Tests whether a file object contains a TIFF image.
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if TIFFs are supported and file is a valid TIFF, otherwise 0.
"""
return _ctypes["IMG_isTIF"](src)
def IMG_isXCF(src):
"""Tests whether a file object contains an XCF (native GIMP format) image.
.. note:: XCF support is currently missing in official macOS binaries
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if XCFs are supported and file is a valid XCF, otherwise 0.
"""
return _ctypes["IMG_isXCF"](src)
def IMG_isXPM(src):
"""Tests whether a file object contains an XPM (X11 Pixmap) image.
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if XPMs are supported and file is a valid XPM, otherwise 0.
"""
return _ctypes["IMG_isXPM"](src)
def IMG_isXV(src):
"""Tests whether a file object contains an XV (XV Visual Schnauzer) image.
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if XVs are supported and file is a valid XV, otherwise 0.
"""
return _ctypes["IMG_isXV"](src)
def IMG_isWEBP(src):
"""Tests whether a file object contains a WebP image.
Args:
src (:obj:`SDL_RWops`): The file object to check.
Returns:
int: 1 if WebPs are supported and file is a valid WebP, otherwise 0.
"""
return _ctypes["IMG_isWEBP"](src)
def IMG_LoadAVIF_RW(src):
"""Loads an AVIF image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
`Note: Added in SDL_image 2.6.0`
Args:
src (:obj:`SDL_RWops`): The file object from which to load the AVIF.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadAVIF_RW"](src)
def IMG_LoadICO_RW(src):
"""Loads an ICO (Windows icon) image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the ICO.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadICO_RW"](src)
def IMG_LoadCUR_RW(src):
"""Loads a CUR (Windows cursor) image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the CUR.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadCUR_RW"](src)
def IMG_LoadBMP_RW(src):
"""Loads a BMP (Windows bitmap) image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the BMP.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadBMP_RW"](src)
def IMG_LoadGIF_RW(src):
"""Loads a GIF (Graphics Interchange Format) image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the GIF.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadGIF_RW"](src)
def IMG_LoadJPG_RW(src):
"""Loads a JPEG image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the JPEG.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadJPG_RW"](src)
def IMG_LoadJXL_RW(src):
"""Loads a JPEG XL image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
`Note: Added in SDL_image 2.6.0`
Args:
src (:obj:`SDL_RWops`): The file object from which to load the JPEG XL.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadJXL_RW"](src)
def IMG_LoadLBM_RW(src):
"""Loads an LBM (Interleaved Bitmap) image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the LBM.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadLBM_RW"](src)
def IMG_LoadPCX_RW(src):
"""Loads a PCX (IBM PC Paintbrush) image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the PCX.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadPCX_RW"](src)
def IMG_LoadPNG_RW(src):
"""Loads a PNG image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the PNG.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadPNG_RW"](src)
def IMG_LoadPNM_RW(src):
"""Loads a PNM (Portable Anymap) image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the PNM.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadPNM_RW"](src)
def IMG_LoadSVG_RW(src):
"""Loads an SVG image from an SDL file object.
Note that this function only loads SVGs at their self-reported resolutions.
To load an SVG at an arbitrary resolution, see :func:`IMG_LoadSizedSVG_RW`.
Use the :func:`IMG_GetError` function to check for any errors.
`Note: Added in SDL_image 2.0.2`
Args:
src (:obj:`SDL_RWops`): The file object from which to load the SVG.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadSVG_RW"](src)
def IMG_LoadQOI_RW(src):
"""Loads a QOI image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
`Note: Added in SDL_image 2.6.0`
Args:
src (:obj:`SDL_RWops`): The file object from which to load the QOI.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadQOI_RW"](src)
def IMG_LoadTGA_RW(src):
"""Loads a TGA (TrueVision Targa) image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the TGA.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadTGA_RW"](src)
def IMG_LoadTIF_RW(src):
"""Loads a TIFF (Tagged Image File Format) image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the TIFF.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadTIF_RW"](src)
def IMG_LoadXCF_RW(src):
"""Loads an TIFF (native GIMP format) image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the XCF.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadXCF_RW"](src)
def IMG_LoadXPM_RW(src):
"""Loads an XPM (X11 Pixmap) image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the XPM.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadXPM_RW"](src)
def IMG_LoadXV_RW(src):
"""Loads an XV thumbnail (XV Visual Schnauzer) from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the XV.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadXV_RW"](src)
def IMG_LoadWEBP_RW(src):
"""Loads a WebP image from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
Args:
src (:obj:`SDL_RWops`): The file object from which to load the WebP.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadWEBP_RW"](src)
def IMG_LoadSizedSVG_RW(src, width, height):
"""Loads an SVG image at a given size from an SDL file object.
Use the :func:`IMG_GetError` function to check for any errors.
`Note: Added in SDL_image 2.6.0`
Args:
src (:obj:`SDL_RWops`): The file object from which to load the SVG.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_LoadSizedSVG_RW"](src, width, height)
def IMG_ReadXPMFromArray(xpm):
"""Loads an X11 Pixmap from an array to a new surface.
The XPM format consists of a C header with an array of strings defining the
dimensions, colors, and pixels of a pixel art image: this is the data format
that this function expects to be passed.
The input string for this function must be cast to a ``ctypes.c_char_p``
(pointer to a character array) and passed by reference using
:func:`ctypes.byref`.
Note that this function will return an 8-bit indexed (palette) surface when
possible. To obtain a 32-bit RGB surface from an XPM array, use the
:func:`IMG_ReadXPMFromArrayToRGB888` function.
Args:
xpm (byref(:obj:`ctypes.c_char_p`)): A pointer to a ctypes string
defining an XPM image.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_ReadXPMFromArray"](xpm)
def IMG_ReadXPMFromArrayToRGB888(xpm):
"""Loads an X11 Pixmap from an array to a new 32-bit surface.
This function is the same as :func:`IMG_ReadXPMFromArray` except that it
always returns a 32-bit ARGB surface.
`Note: Added in SDL_image 2.6.0`
Args:
xpm (byref(:obj:`ctypes.c_char_p`)): A pointer to a ctypes string
defining an XPM image.
Returns:
POINTER(:obj:`SDL_Surface`): A pointer to a new surface containing the
image, or ``None`` if there was an error.
"""
return _ctypes["IMG_ReadXPMFromArrayToRGB888"](xpm)
def IMG_SavePNG(surface, file):
"""Saves an SDL surface to a PNG file.
.. note::
This should work regardless of whether PNG support was
successfully initialized with :func:`IMG_Init`, but the full set
of PNG features may not be available.
Args:
surface (:obj:`SDL_Surface`): The surface to be saved to PNG.
file (bytes): A UTF-8 encoded bytestring containing the path at which
to save the PNG.
Returns:
int: 0 on success or a negative error code on failure, can call
:func:`IMG_GetError` for more information.
"""
return _ctypes["IMG_SavePNG"](surface, file)
def IMG_SavePNG_RW(surface, dst, freedst):
"""Saves an SDL surface to a file object containing a PNG.
See :func:`IMG_SavePNG` for more information.
Args:
surface (:obj:`SDL_Surface`): The surface to be saved to PNG.
dst (:obj:`SDL_RWops`): The destination file object for the saved PNG.
freedst (int): If non-zero, the destination file object will be closed
once the PNG has been written.
Returns:
int: 0 on success or a negative error code on failure, can call
:func:`IMG_GetError` for more information.
"""
return _ctypes["IMG_SavePNG_RW"](surface, dst, freedst)
def IMG_SaveJPG(surface, file, quality):
"""Saves an SDL surface to a JPEG file at a given quality.
JPEG support must be already initialized using :func:`IMG_Init` before this
function can be used. Otherwise, this function will fail without an explicit
error that can be retrieved with :func:`IMG_GetError`.
.. note::
JPEG saving was not supported in the official libsdl.org macOS binaries
until SDL_image 2.6.0.
Args:
surface (:obj:`SDL_Surface`): The surface to be saved to JPEG.
file (bytes): A UTF-8 encoded bytestring containing the path at which
to save the PNG.
quality (int): The compression level of the saved JPEG, from 1
(lowest quality) to 100 (highest quality).
Returns:
int: 0 on success or a negative error code on failure, can call
:func:`IMG_GetError` for more information.
"""
# NOTE: Not available in official macOS binaries
return _ctypes["IMG_SaveJPG"](surface, file, quality)
def IMG_SaveJPG_RW(surface, dst, freedst, quality):
"""Saves an SDL surface to a file object containing a JPEG.
.. note::
As of SDL_image 2.0.5, JPEG saving is not supported by the official
libsdl.org macOS binaries.
Args:
surface (:obj:`SDL_Surface`): The surface to be saved to JPEG.
file (bytes): A UTF-8 encoded bytestring containing the path at which
to save the PNG.
quality (int): The compression level of the saved JPEG, from 1
(lowest quality) to 100 (highest quality).
Returns:
int: 0 on success or a negative error code on failure, can call
:func:`IMG_GetError` for more information.
"""
# NOTE: Not available in official macOS binaries
return _ctypes["IMG_SaveJPG_RW"](surface, dst, freedst, quality)
def IMG_LoadAnimation(file):
"""Loads an animated image from a file.
For more information on how to work with imported animations, see the
:class:`IMG_Animation` documentation. As of SDL_image 2.8.0, this currently
supports GIF and WEBP animations.
`Note: Added in SDL_image 2.6.0`
Args:
file (bytes): A UTF8-encoded bytestring containing the path to the
animation file to load.
Returns:
POINTER(:obj:`IMG_Animation`): A pointer to an animation object, or a
null pointer if there was an error.
"""
return _ctypes["IMG_LoadAnimation"](file)
def IMG_LoadAnimation_RW(src, freesrc):
"""Loads an animated image from an SDL file object.
For more information on how to work with imported animations, see the
:class:`IMG_Animation` documentation. As of SDL_image 2.8.0, this currently
supports GIF and WEBP animations.
`Note: Added in SDL_image 2.6.0`
Args:
src (:obj:`SDL_RWops`): The file object from which to load the
animation.
freesrc (int): If non-zero, the input file object will be closed and
freed after it has been read.
Returns:
POINTER(:obj:`IMG_Animation`): A pointer to an animation object, or a
null pointer if there was an error.
"""
return _ctypes["IMG_LoadAnimation_RW"](src, freesrc)
def IMG_LoadAnimationTyped_RW(src, freesrc, type):
"""Loads an animated image from an SDL file object as a specific format.
For more information on how to work with imported animations, see the
:class:`IMG_Animation` documentation. As of SDL_image 2.8.0, the only
supported animation types are ``b'GIF'`` and ``b'WEBP'``.
`Note: Added in SDL_image 2.6.0`
Args:
src (:obj:`SDL_RWops`): The file object from which to load the
animation.
freesrc (int): If non-zero, the input file object will be closed and
freed after it has been read.
type (bytes): A bytestring indicating the animation format with which
the file object should be loaded.
Returns:
POINTER(:obj:`IMG_Animation`): A pointer to an animation object, or a
null pointer if there was an error.
"""
return _ctypes["IMG_LoadAnimationTyped_RW"](src, freesrc, type)
def IMG_FreeAnimation(anim):
"""Closes and frees the memory associated with a given animation.
This function should be called on an animation after you are done with it.
An :obj:`IMG_Animation` and its frame data cannot be used after it has been
closed.
`Note: Added in SDL_image 2.6.0`
Args:
anim (:obj:`IMG_Animation`): The animation object to close.
"""
return _ctypes["IMG_FreeAnimation"](anim)
def IMG_LoadGIFAnimation_RW(src):
"""Loads a GIF animation from an SDL file object.
For more information on how to work with imported animations, see the
:class:`IMG_Animation` documentation. Use the :func:`IMG_GetError`
function to check for any errors.
`Note: Added in SDL_image 2.6.0`
Args:
src (:obj:`SDL_RWops`): The file object from which to load the GIF.
Returns:
POINTER(:obj:`IMG_Animation`): A pointer to an animation object, or a
a null pointer if there was an error.
"""
return _ctypes["IMG_LoadGIFAnimation_RW"](src)
def IMG_LoadWEBPAnimation_RW(src):
"""Loads a WEBP animation from an SDL file object.
For more information on how to work with imported animations, see the
:class:`IMG_Animation` documentation. Use the :func:`IMG_GetError`
function to check for any errors.
`Note: Added in SDL_image 2.8.0`
Args:
src (:obj:`SDL_RWops`): The file object from which to load the WEBP.
Returns:
POINTER(:obj:`IMG_Animation`): A pointer to an animation object, or a
a null pointer if there was an error.
"""
return _ctypes["IMG_LoadWEBPAnimation_RW"](src)
IMG_SetError = SDL_SetError
IMG_GetError = SDL_GetError
|