From a6880c8d026919a39efb6dddfde45b1987a28dd7 Mon Sep 17 00:00:00 2001 From: Glen Low Date: Thu, 5 Dec 2013 22:33:19 +0800 Subject: [PATCH] Quartz: format to UTI through function instead of array * Function is safer: non-existent format won't crash * Function is better encapsulated: strings not exposed in header --- plugin/quartz/gvdevice_quartz.c | 2 +- plugin/quartz/gvloadimage_quartz.c | 2 +- plugin/quartz/gvplugin_quartz.c | 53 +++++++++++++++++++----------- plugin/quartz/gvplugin_quartz.h | 2 +- plugin/quartz/gvrender_quartz.c | 5 ++- 5 files changed, 39 insertions(+), 25 deletions(-) diff --git a/plugin/quartz/gvdevice_quartz.c b/plugin/quartz/gvdevice_quartz.c index 9b5d3daaa..9c7ee40db 100644 --- a/plugin/quartz/gvdevice_quartz.c +++ b/plugin/quartz/gvdevice_quartz.c @@ -39,7 +39,7 @@ static void quartz_format(GVJ_t *job) /* image destination -> data consumer -> job's gvdevice */ /* data provider <- job's imagedata */ CGDataConsumerRef data_consumer = CGDataConsumerCreate(job, &device_data_consumer_callbacks); - CGImageDestinationRef image_destination = CGImageDestinationCreateWithDataConsumer(data_consumer, format_uti[job->device.id], 1, NULL); + CGImageDestinationRef image_destination = CGImageDestinationCreateWithDataConsumer(data_consumer, format_to_uti(job->device.id), 1, NULL); CGDataProviderRef data_provider = CGDataProviderCreateDirect(job->imagedata, BYTES_PER_PIXEL * job->width * job->height, &memory_data_provider_callbacks); /* add the bitmap image to the destination and save it */ diff --git a/plugin/quartz/gvloadimage_quartz.c b/plugin/quartz/gvloadimage_quartz.c index 11d95a7f9..28ddc6fbf 100644 --- a/plugin/quartz/gvloadimage_quartz.c +++ b/plugin/quartz/gvloadimage_quartz.c @@ -122,7 +122,7 @@ static CGImageRef quartz_loadimage(GVJ_t * job, usershape_t *us) CFDictionaryRef options = hint_format_type == FORMAT_NONE ? NULL : CFDictionaryCreate( kCFAllocatorDefault, (const void **)&kCGImageSourceTypeIdentifierHint, - (const void **)(format_uti + hint_format_type), + (const void **)format_to_uti(hint_format_type), 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); diff --git a/plugin/quartz/gvplugin_quartz.c b/plugin/quartz/gvplugin_quartz.c index 52ed554a4..947b0a771 100644 --- a/plugin/quartz/gvplugin_quartz.c +++ b/plugin/quartz/gvplugin_quartz.c @@ -27,25 +27,6 @@ extern gvplugin_installed_t gvdevice_quartz_types; #if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1040 extern gvplugin_installed_t gvdevice_quartz_types_for_cairo; - -/* Uniform Type Identifiers corresponding to each format_type */ -CFStringRef format_uti [] = { - NULL, - NULL, - CFSTR("com.microsoft.bmp"), - CFSTR("com.ilm.openexr-image"), - CFSTR("com.compuserve.gif"), - CFSTR("public.jpeg"), - CFSTR("public.jpeg-2000"), - CFSTR("com.adobe.pdf"), - CFSTR("com.apple.pict"), - CFSTR("public.png"), - CFSTR("com.adobe.photoshop-image"), - CFSTR("com.sgi.sgi-image"), - CFSTR("public.tiff"), - CFSTR("com.truevision.tga-image") -}; - #endif /* data consumer backed by the gvdevice */ @@ -59,6 +40,40 @@ CGDataConsumerCallbacks device_data_consumer_callbacks = { NULL }; +#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1040 + +CFStringRef format_to_uti(format_type format) +{ + switch (format) { + case FORMAT_BMP: + return CFSTR("com.microsoft.bmp"); + case FORMAT_EXR: + return CFSTR("com.ilm.openexr-image"); + case FORMAT_GIF: + return CFSTR("com.compuserve.gif"); + case FORMAT_JPEG: + return CFSTR("public.jpeg"); + case FORMAT_JPEG2000: + return CFSTR("public.jpeg-2000"); + case FORMAT_PICT: + return CFSTR("com.apple.pict"); + case FORMAT_PNG: + return CFSTR("public.png"); + case FORMAT_PSD: + return CFSTR("com.adobe.photoshop-image"); + case FORMAT_SGI: + return CFSTR("com.sgi.sgi-image"); + case FORMAT_TIFF: + return CFSTR("public.tiff"); + case FORMAT_TGA: + return CFSTR("com.truevision.tga-image"); + default: + return NULL; + } +} + +#endif + static gvplugin_api_t apis[] = { {API_render, &gvrender_quartz_types}, {API_textlayout, &gvtextlayout_quartz_types}, diff --git a/plugin/quartz/gvplugin_quartz.h b/plugin/quartz/gvplugin_quartz.h index 8716c4edd..7a1aa09d6 100644 --- a/plugin/quartz/gvplugin_quartz.h +++ b/plugin/quartz/gvplugin_quartz.h @@ -48,7 +48,7 @@ static const int BITS_PER_COMPONENT = 8; /* bits per color component */ static const int BYTES_PER_PIXEL = 4; /* bytes per pixel */ #if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1040 -extern CFStringRef format_uti []; +CFStringRef format_to_uti(format_type format); #endif extern CGDataConsumerCallbacks device_data_consumer_callbacks; diff --git a/plugin/quartz/gvrender_quartz.c b/plugin/quartz/gvrender_quartz.c index ddbf4fd81..cdf8c4cd1 100644 --- a/plugin/quartz/gvrender_quartz.c +++ b/plugin/quartz/gvrender_quartz.c @@ -67,9 +67,8 @@ static void quartzgen_end_job(GVJ_t * job) &device_data_consumer_callbacks); CGImageDestinationRef image_destination = CGImageDestinationCreateWithDataConsumer(data_consumer, - format_uti - [job->device. - id], 1, + format_to_uti(job->device.id), + 1, NULL); /* add the bitmap image to the destination and save it */ -- 2.40.0