Skip to content

Use enums for Modes and RawModes in C #9100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 60 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
cd93629
use a struct for mode names instead of just a string
Yay295 Apr 20, 2024
63a45ad
add special modes
Yay295 Apr 21, 2024
12409e4
use mode structs in _imaging.c
eyedav Jul 19, 2025
a37f53c
use mode structs in tkImaging.c
Yay295 Apr 21, 2024
a12dc30
use mode structs in encode.c and decode.c
eyedav Jul 19, 2025
0df2ed0
use mode structs in Access.c
eyedav Jul 19, 2025
82182ba
use mode structs in AlphaComposite.c
Yay295 Apr 21, 2024
d0541a7
use mode structs in Bands.c
Yay295 Apr 21, 2024
38c75b9
use mode structs in Blend.c
Yay295 Apr 21, 2024
6f6e1f9
use mode structs in BoxBlur.c
Yay295 Apr 21, 2024
ecf1fce
use mode structs in Chops.c
Yay295 Apr 21, 2024
9bf3495
use mode structs in Convert.c
eyedav Jul 19, 2025
bcfe5f2
use mode structs in Draw.c
eyedav Jul 19, 2025
b5c4b82
use mode structs in Effects.c
Yay295 Apr 22, 2024
19c0d1d
use mode structs in File.c
Yay295 Apr 22, 2024
6202eef
use mode structs in Fill.c
Yay295 Apr 22, 2024
af22363
use mode structs in Filter.c
eyedav Jul 19, 2025
cfe9155
use mode structs in Geometry.c
Yay295 Oct 13, 2024
2668338
use mode structs in GetBBox.c
Yay295 Apr 22, 2024
2749770
use mode structs in Histo.c
Yay295 Apr 22, 2024
3327258
use mode structs in Jpeg2KDecode.c
Yay295 Apr 22, 2024
98a2c63
use mode structs in Jpeg2KEncode.c
Yay295 Apr 22, 2024
30d4cd0
use mode structs in JpegDecode.c
Yay295 Apr 22, 2024
0abfdd2
use mode structs in JpegEncode.c
Yay295 Apr 22, 2024
378c3bd
use mode structs in Matrix.c
Yay295 Apr 22, 2024
4906285
add function isModeI16() to check if a mode is an I;16 mode
eyedav Jul 19, 2025
e5bc5b4
use mode structs in Pack.c
eyedav Jul 19, 2025
af3c24e
use mode structs in Palette.c
Yay295 Apr 22, 2024
2a9d712
use mode structs in Paste.c
Yay295 Apr 22, 2024
7e48697
use mode structs in Point.c
Yay295 Apr 22, 2024
fb73d90
use mode structs in Quant.c
Yay295 Apr 22, 2024
c80fba3
use mode structs in Reduce.c
Yay295 Apr 22, 2024
858b0b3
use mode structs in Resample.c
eyedav Jul 19, 2025
e75a0a9
use mode structs in Storage.c
eyedav Jul 19, 2025
141c95d
use mode structs in TiffDecode.c
Yay295 Apr 22, 2024
39d434b
use (void) for empty function parameters
Yay295 Apr 22, 2024
31118b0
set pointer to NULL after free
Yay295 Apr 22, 2024
d11819c
use mode structs in Unpack.c
eyedav Jul 19, 2025
feb7e6e
use mode structs in map.c
Yay295 Apr 22, 2024
c9c50ac
initialize accessors similar to converters/packers/unpackers
eyedav Jul 19, 2025
cacb8b3
define rawmodes
Yay295 Apr 22, 2024
20a5aea
fix findRawMode()
Yay295 Apr 22, 2024
579c55e
check for null input in findMode() and findRawMode()
Yay295 Apr 22, 2024
422eb1e
replace some string function usage with imaging mode checks
Yay295 Apr 23, 2024
16fc61e
use RawMode struct for jpegmode
Yay295 Apr 23, 2024
4b07ed5
use Mode struct for windows display code
Yay295 Apr 23, 2024
9527ce7
change mode structs to enums
eyedav Jul 19, 2025
4d721bc
use mode enums in _webp.c
Yay295 Apr 23, 2024
aa39e84
use mode enums in Jpeg2KDecode.c
eyedav Jul 19, 2025
a53f83f
use mode enums in _imagingft.c
Yay295 Apr 23, 2024
f8bfa2f
use more mode enums in decode.c
Yay295 Apr 23, 2024
4750347
add Mode.c as a dependency for _imagingft.c and _webp.c
eyedav Jul 19, 2025
e483a97
use a different temp build dir for each module
Yay295 Apr 24, 2024
28adda9
build Mode.c as a common library
eyedav Jul 19, 2025
0567f06
add debug check that all modes and rawmodes are defined
Yay295 Apr 26, 2024
2f169fa
use mode enums in _imagingcms.c
Yay295 Apr 26, 2024
d82576f
require types-setuptools>=75.2.0
eyedav Jul 19, 2025
84aa437
linter changes
eyedav Jul 19, 2025
adfb66f
Fix Compliation errors from rebase
wiredfool Jul 20, 2025
726de49
Merge branch 'main' into mode_enums
radarhere Aug 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/requirements-mypy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ sphinx
types-atheris
types-defusedxml
types-olefile
types-setuptools
types-setuptools>=75.2.0
Copy link
Member

@radarhere radarhere Jul 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
types-setuptools>=75.2.0
types-setuptools

We're currently at 80.9.0. I think this has sufficiently propagated?

17 changes: 13 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
import sys
import warnings
from collections.abc import Iterator
from typing import TYPE_CHECKING, Any

from pybind11.setup_helpers import ParallelCompile
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext

if TYPE_CHECKING:
from setuptools import _BuildInfo

configuration: dict[str, list[str]] = {}

# parse configuration from _custom_build/backend.py
Expand Down Expand Up @@ -1072,16 +1076,20 @@ def debug_build() -> bool:
return hasattr(sys, "gettotalrefcount") or FUZZING_BUILD


libraries: list[tuple[str, _BuildInfo]] = [
("pil_imaging_mode", {"sources": ["src/libImaging/Mode.c"]}),
]

files: list[str | os.PathLike[str]] = ["src/_imaging.c"]
for src_file in _IMAGING:
files.append("src/" + src_file + ".c")
for src_file in _LIB_IMAGING:
files.append(os.path.join("src/libImaging", src_file + ".c"))
ext_modules = [
Extension("PIL._imaging", files),
Extension("PIL._imagingft", ["src/_imagingft.c"]),
Extension("PIL._imagingcms", ["src/_imagingcms.c"]),
Extension("PIL._webp", ["src/_webp.c"]),
Extension("PIL._imaging", files, libraries=["pil_imaging_mode"]),
Extension("PIL._imagingft", ["src/_imagingft.c"], libraries=["pil_imaging_mode"]),
Extension("PIL._imagingcms", ["src/_imagingcms.c"], libraries=["pil_imaging_mode"]),
Extension("PIL._webp", ["src/_webp.c"], libraries=["pil_imaging_mode"]),
Extension("PIL._avif", ["src/_avif.c"]),
Extension("PIL._imagingtk", ["src/_imagingtk.c", "src/Tk/tkImaging.c"]),
Extension("PIL._imagingmath", ["src/_imagingmath.c"]),
Expand All @@ -1093,6 +1101,7 @@ def debug_build() -> bool:
setup(
cmdclass={"build_ext": pil_build_ext},
ext_modules=ext_modules,
libraries=libraries,
zip_safe=not (debug_build() or PLATFORM_MINGW),
)
except RequiredDependencyException as err:
Expand Down
7 changes: 4 additions & 3 deletions src/Tk/tkImaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,16 @@ PyImagingPhotoPut(

/* Mode */

if (strcmp(im->mode, "1") == 0 || strcmp(im->mode, "L") == 0) {
if (im->mode == IMAGING_MODE_1 || im->mode == IMAGING_MODE_L) {
block.pixelSize = 1;
block.offset[0] = block.offset[1] = block.offset[2] = block.offset[3] = 0;
} else if (strncmp(im->mode, "RGB", 3) == 0) {
} else if (im->mode == IMAGING_MODE_RGB || im->mode == IMAGING_MODE_RGBA ||
im->mode == IMAGING_MODE_RGBX || im->mode == IMAGING_MODE_RGBa) {
block.pixelSize = 4;
block.offset[0] = 0;
block.offset[1] = 1;
block.offset[2] = 2;
if (strcmp(im->mode, "RGBA") == 0) {
if (im->mode == IMAGING_MODE_RGBA) {
block.offset[3] = 3; /* alpha (or reserved, under Tk 8.2) */
} else {
block.offset[3] = 0; /* no alpha */
Expand Down
Loading
Loading