Repository navigation
Speed up BMP grayscale palette detection #10574
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4258,6 +4258,49 @@ _effect_spread(ImagingObject *self, PyObject *args) { | |
| /* UTILITIES */ | ||
| /* -------------------------------------------------------------------- */ | ||
|
|
||
| static PyObject * | ||
| _is_grayscale_palette(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { | ||
| Py_ssize_t colors, padding, i; | ||
| const UINT8 *palette; | ||
|
|
||
| if (nargs != 3) { | ||
| PyErr_SetString(PyExc_TypeError, "is_grayscale_palette requires 3 arguments"); | ||
| return NULL; | ||
| } | ||
| if (!PyBytes_Check(args[0])) { | ||
| PyErr_SetString(PyExc_TypeError, "palette must be bytes"); | ||
| return NULL; | ||
| } | ||
| colors = PyLong_AsSsize_t(args[1]); | ||
| if (colors == -1 && PyErr_Occurred()) { | ||
| return NULL; | ||
| } | ||
| padding = PyLong_AsSsize_t(args[2]); | ||
| if (padding == -1 && PyErr_Occurred()) { | ||
| return NULL; | ||
| } | ||
|
Comment on lines
+4266
to
+4281
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The native code in Pillow generally uses |
||
| if (colors < 1 || colors > 256) { | ||
| PyErr_SetString(PyExc_ValueError, "palette size must be between 1 and 256"); | ||
| return NULL; | ||
| } | ||
| if (padding != 3 && padding != 4) { | ||
| PyErr_SetString(PyExc_ValueError, "palette entries must contain 3 or 4 bytes"); | ||
| return NULL; | ||
| } | ||
| if (PyBytes_GET_SIZE(args[0]) < (colors - 1) * padding + 3) { | ||
| Py_RETURN_FALSE; | ||
| } | ||
| palette = (const UINT8 *)PyBytes_AS_STRING(args[0]); | ||
| for (i = 0; i < colors; i++, palette += padding) { | ||
| UINT8 expected = colors == 2 && i == 1 ? 255 : (UINT8)i; | ||
| if (palette[0] != expected || palette[1] != expected || | ||
| palette[2] != expected) { | ||
| Py_RETURN_FALSE; | ||
| } | ||
| } | ||
| Py_RETURN_TRUE; | ||
| } | ||
|
|
||
| static PyObject * | ||
| _getcodecstatus(PyObject *self, PyObject *args) { | ||
| int status; | ||
|
|
@@ -5087,6 +5130,7 @@ static PyMethodDef functions[] = { | |
|
|
||
| /* Utilities */ | ||
| {"getcodecstatus", (PyCFunction)_getcodecstatus, METH_VARARGS}, | ||
| {"is_grayscale_palette", (PyCFunction)_is_grayscale_palette, METH_FASTCALL}, | ||
|
|
||
| /* Special effects (experimental) */ | ||
| {"effect_mandelbrot", (PyCFunction)_effect_mandelbrot, METH_VARARGS}, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Palettes are a separate concept in Pillow -- I'd name this somehow differently, since what it does is, uh... read a bytes argument that is expected to be RGB/RGBX u8 values, and checks that they're in strictly ascending order?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all the existing comments and code call this palette? and this is also what its called in file format, I think, would it help if it said palette + "colors" or "color bytes"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, isn't this specific to the particular format in BMPs before it is unserialized into a Pillow palette?