From 07bc3ce871303a94b9e49de6448de955d4f44f55 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 30 Jul 2024 20:22:16 +0530 Subject: [PATCH] Make NSImage to PNG code re-useable --- kitty/cocoa_window.m | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/kitty/cocoa_window.m b/kitty/cocoa_window.m index c130025fd..6a6d6524b 100644 --- a/kitty/cocoa_window.m +++ b/kitty/cocoa_window.m @@ -1072,22 +1072,7 @@ cocoa_set_uncaught_exception_handler(void) { } static PyObject* -bundle_image_as_png(PyObject *self UNUSED, PyObject *args, PyObject *kw) {@autoreleasepool { - const char *b, *output_path = NULL; int is_identifier = 0; unsigned image_size = 256; - static const char* kwlist[] = {"path_or_identifier", "output_path", "image_size", "is_identifier", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kw, "s|sIp", (char**)kwlist, &b, &output_path, &image_size, &is_identifier)) return NULL; - NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; // autoreleased - NSImage *icon = nil; - if (is_identifier) { - NSURL *url = [workspace URLForApplicationWithBundleIdentifier:@(b)]; // autoreleased - if (!url) { - PyErr_Format(PyExc_KeyError, "Failed to find bundle path for identifier: %s", b); return NULL; - } - icon = [workspace iconForFile:@(url.fileSystemRepresentation)]; - } else icon = [workspace iconForFile:@(b)]; - if (!icon) { - PyErr_Format(PyExc_ValueError, "Failed to load icon for bundle: %s", b); return NULL; - } +convert_image_to_png(NSImage *icon, unsigned image_size, const char *output_path) { NSRect r = NSMakeRect(0, 0, image_size, image_size); RAII_CoreFoundation(CGColorSpaceRef, colorSpace, CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); RAII_CoreFoundation(CGContextRef, cgContext, CGBitmapContextCreate(NULL, image_size, image_size, 8, 4*image_size, colorSpace, kCGBitmapByteOrderDefault|kCGImageAlphaPremultipliedLast)); @@ -1104,6 +1089,26 @@ bundle_image_as_png(PyObject *self UNUSED, PyObject *args, PyObject *kw) {@autor return PyBytes_FromStringAndSize(NULL, 0); } return PyBytes_FromStringAndSize(png.bytes, png.length); +} + +static PyObject* +bundle_image_as_png(PyObject *self UNUSED, PyObject *args, PyObject *kw) {@autoreleasepool { + const char *b, *output_path = NULL; int is_identifier = 0; unsigned image_size = 256; + static const char* kwlist[] = {"path_or_identifier", "output_path", "image_size", "is_identifier", NULL}; + if (!PyArg_ParseTupleAndKeywords(args, kw, "s|sIp", (char**)kwlist, &b, &output_path, &image_size, &is_identifier)) return NULL; + NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; // autoreleased + NSImage *icon = nil; + if (is_identifier) { + NSURL *url = [workspace URLForApplicationWithBundleIdentifier:@(b)]; // autoreleased + if (!url) { + PyErr_Format(PyExc_KeyError, "Failed to find bundle path for identifier: %s", b); return NULL; + } + icon = [workspace iconForFile:@(url.fileSystemRepresentation)]; + } else icon = [workspace iconForFile:@(b)]; + if (!icon) { + PyErr_Format(PyExc_ValueError, "Failed to load icon for bundle: %s", b); return NULL; + } + return convert_image_to_png(icon, image_size, output_path); }} static PyMethodDef module_methods[] = {