19 #include <CoreFoundation/CoreFoundation.h>
20 #include <ApplicationServices/ApplicationServices.h>
35 static CGImageSourceRef
40 CGImageSourceRef image_source;
42 cfname = CFStringCreateWithCString(kCFAllocatorDefault,
44 kCFStringEncodingUTF8);
50 image_url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
60 image_source = CGImageSourceCreateWithURL(image_url, NULL);
65 (CGImageSourceGetStatus(image_source) != kCGImageStatusComplete ||
66 CGImageSourceGetCount(image_source) == 0)) {
67 CFRelease(image_source);
79 strncpy(cstr,
"CGImage loader: Error loading file", buflen);
92 CFRelease(image_source);
101 CGColorSpaceRef color_space;
102 CGImageSourceRef image_source;
105 unsigned char * newpx;
113 image = CGImageSourceCreateImageAtIndex(image_source, 0, NULL);
116 *width = CGImageGetWidth(image);
117 *height = CGImageGetHeight(image);
118 *numcomponents = CGImageGetBitsPerPixel(image)/CGImageGetBitsPerComponent(image);
120 if (*numcomponents <= 2) {
122 color_space = CGColorSpaceCreateDeviceGray();
126 color_space = CGColorSpaceCreateDeviceRGB();
130 newpx = (
unsigned char *)malloc(*width * *height * *numcomponents);
132 context = CGBitmapContextCreate(newpx, *width, *height, 8, *width * *numcomponents,
134 (*numcomponents == 1) ?
136 kCGImageAlphaPremultipliedLast);
139 CGContextScaleCTM(context, 1.0f, -1.0f);
140 CGContextTranslateCTM(context, 0.0f, -*height);
142 CGContextDrawImage(context, CGRectMake(0, 0, *width, *height), image);
145 CFRelease(color_space);
147 CFRelease(image_source);
156 unsigned int formats_size = 0;
158 char * formats = NULL;
160 CFArrayRef destinationTypes;
161 CFIndex fileext_len = 0;
166 format = (
char*)malloc(5);
167 memset(format, 0, 5);
169 jpegStr = CFSTR(
"jpeg");
170 tiffStr = CFSTR(
"tiff");
172 destinationTypes = CGImageDestinationCopyTypeIdentifiers();
173 for (idx = 0; idx < CFArrayGetCount(destinationTypes); idx++) {
174 fileext = UTTypeCopyPreferredTagWithClass(CFArrayGetValueAtIndex(destinationTypes, idx),
175 kUTTagClassFilenameExtension);
177 fileext_len = CFStringGetLength(fileext);
181 if (fileext_len == 4) {
182 if (!CFStringCompare(fileext, jpegStr, 0)) {
184 fileext = CFSTR(
"jpg");
186 }
else if (!CFStringCompare(fileext, tiffStr, 0)) {
188 fileext = CFSTR(
"tif");
193 formats_size += fileext_len + ((idx == 0) ? 1 : 2);
194 formats = (
char *)realloc(formats, formats_size);
198 CFStringGetCString(fileext, format, 5, kCFStringEncodingUTF8);
201 strncpy(formats, format, fileext_len+1);
203 strncat(formats,
",", 2);
204 strncat(formats, format, fileext_len+1);
210 CFRelease(destinationTypes);
218 const unsigned char * bytes,
225 CFStringRef file_ext;
226 CFStringRef type_name;
228 CGImageRef image_source;
229 CGImageDestinationRef image_dest;
230 CGColorSpaceRef color_space;
231 CGDataProviderRef provider;
233 int bitsPerComponent, bitsPerPixel, bytesPerRow;
236 unsigned int pos = 0, imgbufsize = width*height*numcomponents;
237 unsigned char * bytes_flipped = NULL;
241 bytes_flipped = (
unsigned char *)malloc(imgbufsize);
243 for (pos = 0; pos < imgbufsize; pos+=width*numcomponents) {
244 memcpy(bytes_flipped+pos,
245 bytes+imgbufsize-width*numcomponents-pos,
246 width*numcomponents);
249 provider = CGDataProviderCreateWithData(NULL, bytes_flipped,
253 bitsPerComponent = 8;
254 bitsPerPixel = bitsPerComponent*numcomponents;
255 bytesPerRow = numcomponents * width;
256 color_space = CGColorSpaceCreateDeviceRGB();
257 image_source = CGImageCreate(width, height, 8, 8*numcomponents,
260 kCGBitmapByteOrderDefault,
263 kCGRenderingIntentDefault);
265 CGDataProviderRelease(provider);
266 CGColorSpaceRelease(color_space);
269 file_ext = CFStringCreateWithCString(kCFAllocatorDefault,
271 kCFStringEncodingUTF8);
272 type_name = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
280 CFRelease(image_source);
284 cfname = CFStringCreateWithCString(kCFAllocatorDefault,
286 kCFStringEncodingUTF8);
290 CFRelease(type_name);
291 CFRelease(image_source);
295 image_url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
297 kCFURLPOSIXPathStyle,
304 CFRelease(type_name);
305 CFRelease(image_source);
309 image_dest = CGImageDestinationCreateWithURL(image_url, type_name, 1, NULL);
311 CFRelease(type_name);
312 CFRelease(image_url);
316 CFRelease(image_source);
320 CGImageDestinationAddImage(image_dest, image_source, NULL);
322 finalized = CGImageDestinationFinalize(image_dest);
327 CGImageRelease(image_source);
328 CFRelease(image_dest);
int simage_cgimage_save(const char *filename, const unsigned char *bytes, int width, int height, int numcomponents, const char *ext)
int simage_cgimage_identify(const char *file, const unsigned char *header, int headerlen)
static CGImageSourceRef create_image_source(const char *file)
int simage_cgimage_error(char *cstr, int buflen)
char * simage_cgimage_get_savers(void)
unsigned char * simage_cgimage_load(const char *file, int *width, int *height, int *numcomponents)