1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#include <ApplicationServices/ApplicationServices.h>
#include <CGDataConsumer.h>
#include <iostream>
CGDataConsumerCallbacks callbacks;
size_t write_bytes_to_ostream (void *info,const void *buffer,size_t count)
{
assert( info );
std::ostream *output = (std::ostream *)info;
output->write( (const char *) buffer, count );
return count;
}
CGDataConsumerRef dataconsumer CGDataConsumerCreateWithOstream(std::ostream &output)
{
callbacks.putBytes = write_bytes_to_ostream;
callbacks.releaseConsumer = NULL;
CGDataConsumerRef dc = CGDataConsumerCreate ( (void *)output, &callbacks );
return dc;
}
bool write_png(const char *filename, unsigned char *pixels, int width, int height)
{
assert( filename );
std::stringstream dummy;
write_png_base( filename, dummy, pixels, width, height );
}
bool write_png(std::ostream &output, unsigned char *pixels, int width, int height)
{
write_png_base( NULL, output, pixels, width, height );
}
bool write_png_base(const char *filename, std::ostream &output, unsigned char *pixels, int width, int height)
{
size_t rowBytes = width * 4;
// CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
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;
}
if ( filename == NULL ) {
CGDataConsumerRef dataconsumer = CGDataConsumerCreateWithOstream(output);
} else {
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);
if ( filename ) {
CFRelease(fileURL);
CFRelease(fname);
}
CFRelease(imageProps);
CGColorSpaceRelease(colorSpace);
CGImageRelease(imageRef);
return true;
}
|