mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 01:46:18 +03:00
Simplify feature checking, fix transparent webp detection, add webpmux detection
This commit is contained in:
parent
fe183f7448
commit
d012f50975
|
@ -6,41 +6,22 @@ modules = {
|
||||||
"freetype2": "PIL._imagingft",
|
"freetype2": "PIL._imagingft",
|
||||||
"littlecms2": "PIL._imagingcms",
|
"littlecms2": "PIL._imagingcms",
|
||||||
"webp": "PIL._webp",
|
"webp": "PIL._webp",
|
||||||
"transp_webp": ("WEBP", "WebPDecoderBuggyAlpha")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def check_module(feature):
|
def check_module(feature):
|
||||||
if feature not in modules:
|
if not (feature in modules):
|
||||||
raise ValueError("Unknown module %s" % feature)
|
raise ValueError("Unknown module %s" % feature)
|
||||||
|
|
||||||
module = modules[feature]
|
module = modules[feature]
|
||||||
|
|
||||||
method_to_call = None
|
|
||||||
if isinstance(module, tuple):
|
|
||||||
module, method_to_call = module
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
imported_module = __import__(module)
|
imported_module = __import__(module)
|
||||||
except ImportError:
|
|
||||||
# If a method is being checked, None means that
|
|
||||||
# rather than the method failing, the module required for the method
|
|
||||||
# failed to be imported first
|
|
||||||
return None if method_to_call else False
|
|
||||||
|
|
||||||
if method_to_call:
|
|
||||||
method = getattr(imported_module, method_to_call)
|
|
||||||
return method() is True
|
|
||||||
else:
|
|
||||||
return True
|
return True
|
||||||
|
except ImportError:
|
||||||
|
return False
|
||||||
|
|
||||||
def get_supported_modules():
|
def get_supported_modules():
|
||||||
supported_modules = []
|
return [f for f in modules if check_module(f)]
|
||||||
for feature in modules:
|
|
||||||
if check_module(feature):
|
|
||||||
supported_modules.append(feature)
|
|
||||||
return supported_modules
|
|
||||||
|
|
||||||
codecs = {
|
codecs = {
|
||||||
"jpg": "jpeg",
|
"jpg": "jpeg",
|
||||||
|
@ -49,7 +30,6 @@ codecs = {
|
||||||
"libtiff": "libtiff"
|
"libtiff": "libtiff"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def check_codec(feature):
|
def check_codec(feature):
|
||||||
if feature not in codecs:
|
if feature not in codecs:
|
||||||
raise ValueError("Unknown codec %s" % feature)
|
raise ValueError("Unknown codec %s" % feature)
|
||||||
|
@ -60,8 +40,38 @@ def check_codec(feature):
|
||||||
|
|
||||||
|
|
||||||
def get_supported_codecs():
|
def get_supported_codecs():
|
||||||
supported_codecs = []
|
return [f for f in codecs if check_codec(f)]
|
||||||
for feature in codecs:
|
|
||||||
if check_codec(feature):
|
features = {
|
||||||
supported_codecs.append(feature)
|
"webp_mux": ("PIL._webp", 'HAVE_WEBPMUX'),
|
||||||
return supported_codecs
|
"transp_webp": ("PIL._webp", "HAVE_TRANSPARENCY"),
|
||||||
|
}
|
||||||
|
|
||||||
|
def check_feature(feature):
|
||||||
|
if feature not in features:
|
||||||
|
raise ValueError("Unknown feature %s" % feature)
|
||||||
|
|
||||||
|
module, flag = features[feature]
|
||||||
|
|
||||||
|
try:
|
||||||
|
imported_module = __import__(module, fromlist=['PIL'])
|
||||||
|
return getattr(imported_module, flag)
|
||||||
|
except ImportError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def get_supported_features():
|
||||||
|
return [f for f in features if check_feature(f)]
|
||||||
|
|
||||||
|
|
||||||
|
def check(feature):
|
||||||
|
return (feature in modules and check_module(feature) or \
|
||||||
|
feature in codecs and check_codec(feature) or \
|
||||||
|
feature in features and check_feature(feature))
|
||||||
|
|
||||||
|
def get_supported():
|
||||||
|
ret = get_supported_modules()
|
||||||
|
ret.extend(get_supported_features())
|
||||||
|
ret.extend(get_supported_codecs())
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
13
_webp.c
13
_webp.c
|
@ -247,8 +247,12 @@ PyObject* WebPDecoderVersion_wrapper(PyObject* self, PyObject* args){
|
||||||
* The version of webp that ships with (0.1.3) Ubuntu 12.04 doesn't handle alpha well.
|
* The version of webp that ships with (0.1.3) Ubuntu 12.04 doesn't handle alpha well.
|
||||||
* Files that are valid with 0.3 are reported as being invalid.
|
* Files that are valid with 0.3 are reported as being invalid.
|
||||||
*/
|
*/
|
||||||
|
int WebPDecoderBuggyAlpha() {
|
||||||
|
return WebPGetDecoderVersion()==0x0103;
|
||||||
|
}
|
||||||
|
|
||||||
PyObject* WebPDecoderBuggyAlpha_wrapper(PyObject* self, PyObject* args){
|
PyObject* WebPDecoderBuggyAlpha_wrapper(PyObject* self, PyObject* args){
|
||||||
return Py_BuildValue("i", WebPGetDecoderVersion()==0x0103);
|
return Py_BuildValue("i", WebPDecoderBuggyAlpha());
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyMethodDef webpMethods[] =
|
static PyMethodDef webpMethods[] =
|
||||||
|
@ -268,6 +272,11 @@ void addMuxFlagToModule(PyObject* m) {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addTransparencyFlagToModule(PyObject* m) {
|
||||||
|
PyModule_AddObject(m, "HAVE_TRANSPARENCY",
|
||||||
|
PyBool_FromLong(!WebPDecoderBuggyAlpha()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#if PY_VERSION_HEX >= 0x03000000
|
#if PY_VERSION_HEX >= 0x03000000
|
||||||
PyMODINIT_FUNC
|
PyMODINIT_FUNC
|
||||||
|
@ -284,6 +293,7 @@ PyInit__webp(void) {
|
||||||
|
|
||||||
m = PyModule_Create(&module_def);
|
m = PyModule_Create(&module_def);
|
||||||
addMuxFlagToModule(m);
|
addMuxFlagToModule(m);
|
||||||
|
addTransparencyFlagToModule(m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
@ -292,5 +302,6 @@ init_webp(void)
|
||||||
{
|
{
|
||||||
PyObject* m = Py_InitModule("_webp", webpMethods);
|
PyObject* m = Py_InitModule("_webp", webpMethods);
|
||||||
addMuxFlagToModule(m);
|
addMuxFlagToModule(m);
|
||||||
|
addTransparencyFlagToModule(m);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
17
selftest.py
17
selftest.py
|
@ -178,25 +178,14 @@ if __name__ == "__main__":
|
||||||
("freetype2", "FREETYPE2"),
|
("freetype2", "FREETYPE2"),
|
||||||
("littlecms2", "LITTLECMS2"),
|
("littlecms2", "LITTLECMS2"),
|
||||||
("webp", "WEBP"),
|
("webp", "WEBP"),
|
||||||
("transp_webp", "Transparent WEBP")
|
("transp_webp", "Transparent WEBP"),
|
||||||
]:
|
("webp_mux", "WEBPMUX"),
|
||||||
supported = features.check_module(name)
|
|
||||||
|
|
||||||
if supported is None:
|
|
||||||
# A method was being tested, but the module required
|
|
||||||
# for the method could not be correctly imported
|
|
||||||
pass
|
|
||||||
elif supported:
|
|
||||||
print("---", feature, "support ok")
|
|
||||||
else:
|
|
||||||
print("***", feature, "support not installed")
|
|
||||||
for name, feature in [
|
|
||||||
("jpg", "JPEG"),
|
("jpg", "JPEG"),
|
||||||
("jpg_2000", "OPENJPEG (JPEG2000)"),
|
("jpg_2000", "OPENJPEG (JPEG2000)"),
|
||||||
("zlib", "ZLIB (PNG/ZIP)"),
|
("zlib", "ZLIB (PNG/ZIP)"),
|
||||||
("libtiff", "LIBTIFF")
|
("libtiff", "LIBTIFF")
|
||||||
]:
|
]:
|
||||||
if features.check_codec(name):
|
if features.check(name):
|
||||||
print("---", feature, "support ok")
|
print("---", feature, "support ok")
|
||||||
else:
|
else:
|
||||||
print("***", feature, "support not installed")
|
print("***", feature, "support not installed")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user