diff options
author | Marius Kintel <marius@kintel.net> | 2011-09-28 23:57:52 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2011-09-28 23:57:52 (GMT) |
commit | 2b3c140bd264f8a5cf94d3fa83f875933fbb4928 (patch) | |
tree | 91a96f286fe13d6530de513faa53249a669da44c /tests/imageutils-macosx.cc | |
parent | 6c3ce9934755bcc579ac30104d651608c2c71622 (diff) | |
parent | da08b50c03418110a74a6f2667be1d916b607b87 (diff) |
Merge branch 'master' into visitortests
Conflicts:
tests/opencsgtest.cc
Diffstat (limited to 'tests/imageutils-macosx.cc')
-rw-r--r-- | tests/imageutils-macosx.cc | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/imageutils-macosx.cc b/tests/imageutils-macosx.cc new file mode 100644 index 0000000..358bdcf --- /dev/null +++ b/tests/imageutils-macosx.cc @@ -0,0 +1,63 @@ +#include <ApplicationServices/ApplicationServices.h> +#include <iostream> + +bool write_png(const char *filename, unsigned char *pixels, int width, int height) +{ + size_t rowBytes = width * 4; + CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); + CGBitmapInfo bitmapInfo = kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big; // BGRA + int bitsPerComponent = 8; + CGContextRef contextRef = CGBitmapContextCreate(pixels, width, height, + bitsPerComponent, rowBytes, + colorSpace, bitmapInfo); + if (!contextRef) { + std::cerr << "Unable to create CGContextRef."; + return false; + } + + CGImageRef imageRef = CGBitmapContextCreateImage(contextRef); + if (!imageRef) { + std::cerr << "Unable to create CGImageRef."; + return false; + } + + CFStringRef fname = CFStringCreateWithCString(kCFAllocatorDefault, filename, kCFStringEncodingUTF8); + CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, + fname, kCFURLPOSIXPathStyle, false); + if (!fileURL) { + std::cerr << "Unable to create file URL ref."; + return false; + } + + CGDataConsumerRef dataconsumer = CGDataConsumerCreateWithURL(fileURL); + CFIndex fileImageIndex = 1; + CFMutableDictionaryRef fileDict = NULL; + CFStringRef fileUTType = kUTTypePNG; + // Create an image destination opaque reference for authoring an image file + CGImageDestinationRef imageDest = CGImageDestinationCreateWithDataConsumer(dataconsumer, + fileUTType, + fileImageIndex, + fileDict); + if (!imageDest) { + std::cerr << "Unable to create CGImageDestinationRef."; + return false; + } + + CFIndex capacity = 1; + CFMutableDictionaryRef imageProps = + CFDictionaryCreateMutable(kCFAllocatorDefault, + capacity, + &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks); + CGImageDestinationAddImage(imageDest, imageRef, imageProps); + CGImageDestinationFinalize(imageDest); + + CFRelease(imageDest); + CFRelease(dataconsumer); + CFRelease(fileURL); + CFRelease(fname); + CFRelease(imageProps); + CGColorSpaceRelease(colorSpace); + CGImageRelease(imageRef); + return true; +} |