Skip to content

Commit

Permalink
Merge pull request python-pillow#8015 from hugovk/pre-commit-clang
Browse files Browse the repository at this point in the history
Add ClangFormat to pre-commit
  • Loading branch information
radarhere committed May 19, 2024
2 parents c6fb36f + 82c87c4 commit 22b64ff
Show file tree
Hide file tree
Showing 35 changed files with 607 additions and 486 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ BinPackParameters: false
BreakBeforeBraces: Attach
ColumnLimit: 88
DerivePointerAlignment: false
IndentGotoLabels: false
IndentWidth: 4
Language: Cpp
PointerAlignment: Right
Expand Down
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ repos:
- id: remove-tabs
exclude: (Makefile$|\.bat$|\.cmake$|\.eps$|\.fits$|\.gd$|\.opt$)

- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v18.1.4
hooks:
- id: clang-format
types: [c]
exclude: ^src/thirdparty/

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
Expand Down
19 changes: 6 additions & 13 deletions src/Tk/tkImaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,7 @@ PyImagingPhotoPut(
block.pixelPtr = (unsigned char *)im->block;

TK_PHOTO_PUT_BLOCK(
interp,
photo,
&block,
0,
0,
block.width,
block.height,
TK_PHOTO_COMPOSITE_SET);
interp, photo, &block, 0, 0, block.width, block.height, TK_PHOTO_COMPOSITE_SET);

return TCL_OK;
}
Expand Down Expand Up @@ -287,7 +280,7 @@ load_tkinter_funcs(void) {
* Return 0 for success, non-zero for failure.
*/

HMODULE* hMods = NULL;
HMODULE *hMods = NULL;
HANDLE hProcess;
DWORD cbNeeded;
unsigned int i;
Expand All @@ -313,7 +306,7 @@ load_tkinter_funcs(void) {
#endif
return 1;
}
if (!(hMods = (HMODULE*) malloc(cbNeeded))) {
if (!(hMods = (HMODULE *)malloc(cbNeeded))) {
PyErr_NoMemory();
return 1;
}
Expand Down Expand Up @@ -345,7 +338,7 @@ load_tkinter_funcs(void) {
} else if (found_tk == 0) {
PyErr_SetString(PyExc_RuntimeError, "Could not find Tk routines");
}
return (int) ((found_tcl != 1) || (found_tk != 1));
return (int)((found_tcl != 1) || (found_tk != 1));
}

#else /* not Windows */
Expand Down Expand Up @@ -400,8 +393,8 @@ _func_loader(void *lib) {
return 1;
}
return (
(TK_PHOTO_PUT_BLOCK =
(Tk_PhotoPutBlock_t)_dfunc(lib, "Tk_PhotoPutBlock")) == NULL);
(TK_PHOTO_PUT_BLOCK = (Tk_PhotoPutBlock_t)_dfunc(lib, "Tk_PhotoPutBlock")) ==
NULL);
}

int
Expand Down
54 changes: 33 additions & 21 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@

#define B16(p, i) ((((int)p[(i)]) << 8) + p[(i) + 1])
#define L16(p, i) ((((int)p[(i) + 1]) << 8) + p[(i)])
#define S16(v) ((v) < 32768 ? (v) : ((v)-65536))
#define S16(v) ((v) < 32768 ? (v) : ((v) - 65536))

/* -------------------------------------------------------------------- */
/* OBJECT ADMINISTRATION */
Expand Down Expand Up @@ -533,7 +533,9 @@ getink(PyObject *color, Imaging im, char *ink) {
/* unsigned integer, single layer */
if (rIsInt != 1) {
if (tupleSize != 1) {
PyErr_SetString(PyExc_TypeError, "color must be int or single-element tuple");
PyErr_SetString(
PyExc_TypeError,
"color must be int or single-element tuple");
return NULL;
} else if (!PyArg_ParseTuple(color, "L", &r)) {
return NULL;
Expand All @@ -552,15 +554,20 @@ getink(PyObject *color, Imaging im, char *ink) {
a = 255;
if (im->bands == 2) {
if (tupleSize != 1 && tupleSize != 2) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one or two elements");
PyErr_SetString(
PyExc_TypeError,
"color must be int, or tuple of one or two elements");
return NULL;
} else if (!PyArg_ParseTuple(color, "L|i", &r, &a)) {
return NULL;
}
g = b = r;
} else {
if (tupleSize != 3 && tupleSize != 4) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one, three or four elements");
PyErr_SetString(
PyExc_TypeError,
"color must be int, or tuple of one, three or four "
"elements");
return NULL;
} else if (!PyArg_ParseTuple(color, "Lii|i", &r, &g, &b, &a)) {
return NULL;
Expand Down Expand Up @@ -599,7 +606,9 @@ getink(PyObject *color, Imaging im, char *ink) {
g = (UINT8)(r >> 8);
r = (UINT8)r;
} else if (tupleSize != 3) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one or three elements");
PyErr_SetString(
PyExc_TypeError,
"color must be int, or tuple of one or three elements");
return NULL;
} else if (!PyArg_ParseTuple(color, "iiL", &b, &g, &r)) {
return NULL;
Expand Down Expand Up @@ -1537,14 +1546,14 @@ _putdata(ImagingObject *self, PyObject *args) {
return NULL;
}

#define set_value_to_item(seq, i) \
op = PySequence_Fast_GET_ITEM(seq, i); \
if (PySequence_Check(op)) { \
PyErr_SetString(PyExc_TypeError, "sequence must be flattened"); \
return NULL; \
} else { \
value = PyFloat_AsDouble(op); \
}
#define set_value_to_item(seq, i) \
op = PySequence_Fast_GET_ITEM(seq, i); \
if (PySequence_Check(op)) { \
PyErr_SetString(PyExc_TypeError, "sequence must be flattened"); \
return NULL; \
} else { \
value = PyFloat_AsDouble(op); \
}
if (image->image8) {
if (PyBytes_Check(data)) {
unsigned char *p;
Expand Down Expand Up @@ -1596,8 +1605,10 @@ if (PySequence_Check(op)) { \
value = value * scale + offset;
}
if (image->type == IMAGING_TYPE_SPECIAL) {
image->image8[y][x * 2 + (bigendian ? 1 : 0)] = CLIP8((int)value % 256);
image->image8[y][x * 2 + (bigendian ? 0 : 1)] = CLIP8((int)value >> 8);
image->image8[y][x * 2 + (bigendian ? 1 : 0)] =
CLIP8((int)value % 256);
image->image8[y][x * 2 + (bigendian ? 0 : 1)] =
CLIP8((int)value >> 8);
} else {
image->image8[y][x] = (UINT8)CLIP8(value);
}
Expand Down Expand Up @@ -1639,8 +1650,7 @@ if (PySequence_Check(op)) { \
for (i = x = y = 0; i < n; i++) {
double value;
set_value_to_item(seq, i);
IMAGING_PIXEL_INT32(image, x, y) =
(INT32)(value * scale + offset);
IMAGING_PIXEL_INT32(image, x, y) = (INT32)(value * scale + offset);
if (++x >= (int)image->xsize) {
x = 0, y++;
}
Expand Down Expand Up @@ -2785,8 +2795,8 @@ _font_getmask(ImagingFontObject *self, PyObject *args) {
glyph = &self->glyphs[text[i]];
if (i == 0 || text[i] != text[i - 1]) {
ImagingDelete(bitmap);
bitmap =
ImagingCrop(self->bitmap, glyph->sx0, glyph->sy0, glyph->sx1, glyph->sy1);
bitmap = ImagingCrop(
self->bitmap, glyph->sx0, glyph->sy0, glyph->sx1, glyph->sy1);
if (!bitmap) {
goto failed;
}
Expand Down Expand Up @@ -3315,7 +3325,8 @@ _draw_polygon(ImagingDrawObject *self, PyObject *args) {

free(xy);

if (ImagingDrawPolygon(self->image->image, n, ixy, &ink, fill, width, self->blend) < 0) {
if (ImagingDrawPolygon(self->image->image, n, ixy, &ink, fill, width, self->blend) <
0) {
free(ixy);
return NULL;
}
Expand Down Expand Up @@ -4411,7 +4422,8 @@ setup_module(PyObject *m) {
PyModule_AddObject(m, "HAVE_XCB", have_xcb);

PyObject *pillow_version = PyUnicode_FromString(version);
PyDict_SetItemString(d, "PILLOW_VERSION", pillow_version ? pillow_version : Py_None);
PyDict_SetItemString(
d, "PILLOW_VERSION", pillow_version ? pillow_version : Py_None);
Py_XDECREF(pillow_version);

return 0;
Expand Down
16 changes: 5 additions & 11 deletions src/_imagingcms.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,8 @@ cms_transform_dealloc(CmsTransformObject *self) {

static cmsUInt32Number
findLCMStype(char *PILmode) {
if (
strcmp(PILmode, "RGB") == 0 ||
strcmp(PILmode, "RGBA") == 0 ||
strcmp(PILmode, "RGBX") == 0
) {
if (strcmp(PILmode, "RGB") == 0 || strcmp(PILmode, "RGBA") == 0 ||
strcmp(PILmode, "RGBX") == 0) {
return TYPE_RGBA_8;
}
if (strcmp(PILmode, "RGBA;16B") == 0) {
Expand All @@ -232,10 +229,7 @@ findLCMStype(char *PILmode) {
if (strcmp(PILmode, "L;16B") == 0) {
return TYPE_GRAY_16_SE;
}
if (
strcmp(PILmode, "YCCA") == 0 ||
strcmp(PILmode, "YCC") == 0
) {
if (strcmp(PILmode, "YCCA") == 0 || strcmp(PILmode, "YCC") == 0) {
return TYPE_YCbCr_8;
}
if (strcmp(PILmode, "LAB") == 0) {
Expand Down Expand Up @@ -391,7 +385,7 @@ _buildTransform(
iRenderingIntent,
cmsFLAGS);

Py_END_ALLOW_THREADS
Py_END_ALLOW_THREADS;

if (!hTransform) {
PyErr_SetString(PyExc_ValueError, "cannot build transform");
Expand Down Expand Up @@ -425,7 +419,7 @@ _buildProofTransform(
iProofIntent,
cmsFLAGS);

Py_END_ALLOW_THREADS
Py_END_ALLOW_THREADS;

if (!hTransform) {
PyErr_SetString(PyExc_ValueError, "cannot build proof transform");
Expand Down

0 comments on commit 22b64ff

Please sign in to comment.