ImageIO provider: Retrieve supported image sources file extensions during runtime (#8623)

* ImageIO provider: Get supported sources file extensions during runtime

* Remove visionOS as is not available on older XCode versions. * will be enough as this API is supported from visionOS 1.0 onwards
This commit is contained in:
Mirko Galimberti 2024-02-22 18:11:04 +01:00 committed by GitHub
parent dc7fd3d8ae
commit c039371ea1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 144 additions and 7 deletions

View File

@ -1,3 +1,5 @@
# distutils: language = c++
'''
ImageIO OSX framework
=====================
@ -140,6 +142,16 @@ cdef extern from "Accelerate/Accelerate.h" nogil:
int flags)
cdef extern from "img_imageio_implem.mm":
cppclass KivyImageIOProviderSupportedExtensionList:
int count()
char* get(int index)
cppclass KivyImageIOProvider:
KivyImageIOProvider()
KivyImageIOProviderSupportedExtensionList* supported_source_image_extensions
def load_image_data(bytes _url, bytes _data=None):
cdef size_t width, height
cdef char *r_data = NULL
@ -310,19 +322,36 @@ def save_image(filenm, width, height, fmt, data, flipped):
free(pixels)
cdef class _ImageIOInterface:
cdef KivyImageIOProvider* _provider
def __cinit__(self):
self._provider = new KivyImageIOProvider()
def __dealloc__(self):
del self._provider
cdef list get_supported_source_extensions(self):
cdef list ret = []
for i in range(self._provider.supported_source_image_extensions.count()):
ret.append(self._provider.supported_source_image_extensions.get(i).decode('utf-8'))
return ret
# A list of the supported source extensions is served by the static method
# extensions() of the ImageLoaderImageIO class.
# To avoid the creation of a new _ImageIOInterface object each time we need to
# get the list of supported extensions, we create a single use object and get
# the list of supported extensions at the module level.
cdef list _supported_source_extensions = _ImageIOInterface().get_supported_source_extensions()
class ImageLoaderImageIO(ImageLoaderBase):
'''Image loader based on ImageIO OS X Framework
'''
@staticmethod
def extensions():
# FIXME check which one are available on osx
return ('bmp', 'bufr', 'cur', 'dcx', 'fits', 'fl', 'fpx', 'gbr',
'gd', 'grib', 'hdf5', 'ico', 'im', 'imt', 'iptc',
'jpeg', 'jpg', 'jpe', 'mcidas', 'mic', 'mpeg', 'msp',
'pcd', 'pcx', 'pixar', 'png', 'ppm', 'psd', 'sgi',
'spider', 'tga', 'tiff', 'wal', 'wmf', 'xbm', 'xpm',
'xv', 'icns')
return _supported_source_extensions
def load(self, filename):
# FIXME: if the filename is unicode, the loader is failing.

View File

@ -0,0 +1,23 @@
#include <Foundation/Foundation.h>
#include <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
class KivyImageIOProviderSupportedExtensionList {
public:
KivyImageIOProviderSupportedExtensionList();
~KivyImageIOProviderSupportedExtensionList();
void add(NSString* extension);
char* get(int index);
int count();
void clear();
private:
NSMutableArray* extensions;
};
class KivyImageIOProvider {
public:
KivyImageIOProvider();
~KivyImageIOProvider();
KivyImageIOProviderSupportedExtensionList* supported_source_image_extensions;
private:
void load_supported_source_extensions();
};

View File

@ -0,0 +1,84 @@
#include "img_imageio_implem.h"
/*
* KivyImageIOProviderSupportedExtensionList
*/
KivyImageIOProviderSupportedExtensionList::KivyImageIOProviderSupportedExtensionList()
{
this->extensions = [[NSMutableArray alloc] init];
}
KivyImageIOProviderSupportedExtensionList::~KivyImageIOProviderSupportedExtensionList()
{
}
void KivyImageIOProviderSupportedExtensionList::add(NSString *extension)
{
if (![this->extensions containsObject:extension])
{
[this->extensions addObject:extension];
}
}
int KivyImageIOProviderSupportedExtensionList::count()
{
return [this->extensions count];
}
char *KivyImageIOProviderSupportedExtensionList::get(int index)
{
NSString *extension = [this->extensions objectAtIndex:index];
return (char *)[extension UTF8String];
}
void KivyImageIOProviderSupportedExtensionList::clear()
{
[this->extensions removeAllObjects];
}
/*
* KivyImageIOProvider
*/
KivyImageIOProvider::KivyImageIOProvider()
{
this->supported_source_image_extensions = new KivyImageIOProviderSupportedExtensionList();
this->load_supported_source_extensions();
}
KivyImageIOProvider::~KivyImageIOProvider()
{
delete this->supported_source_image_extensions;
}
void KivyImageIOProvider::load_supported_source_extensions()
{
this->supported_source_image_extensions->clear();
CFArrayRef type_identifiers = CGImageSourceCopyTypeIdentifiers();
for (CFIndex i = 0; i < CFArrayGetCount(type_identifiers); i++)
{
CFStringRef uti = (CFStringRef)CFArrayGetValueAtIndex(type_identifiers, i);
NSArray *uti_extensions;
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *))
{
UTType *uttype = [UTType typeWithIdentifier:(NSString *)uti];
uti_extensions = uttype.tags[@"public.filename-extension"];
}
else
{
// UTTypeCopyAllTagsWithClass is deprecated, we're leaving this here
// for compatibility with older versions of macOS and iOS
uti_extensions = CFBridgingRelease(
UTTypeCopyAllTagsWithClass(uti, kUTTagClassFilenameExtension));
}
for (NSString *extension in uti_extensions)
{
this->supported_source_image_extensions->add(extension);
}
}
}

View File

@ -974,6 +974,7 @@ if platform in ('darwin', 'ios'):
else:
osx_flags = {'extra_link_args': [
'-framework', 'ApplicationServices']}
osx_flags['extra_compile_args'] = ['-ObjC++']
sources['core/image/img_imageio.pyx'] = merge(
base_flags, osx_flags)