1/*
2cc -g -o /tmp/img img.c -framework ApplicationServices -framework IOKit -Wall
3*/
4
5#include <CoreFoundation/CoreFoundation.h>
6#include <ApplicationServices/ApplicationServices.h>
7#include <stdlib.h>
8#include <stdio.h>
9
10static void spit(char * filename)
11{
12    CFStringRef string;
13    CFURLRef   url;
14    char                        path[256];
15
16    snprintf(path, sizeof(path), "%s.png", filename);
17
18    string = CFStringCreateWithCString( kCFAllocatorDefault, path,
19                                        kCFStringEncodingMacRoman );
20
21    url = CFURLCreateWithFileSystemPath( kCFAllocatorDefault, string, kCFURLPOSIXPathStyle, FALSE);
22
23    CGImageSourceRef isrc = CGImageSourceCreateWithURL(url, nil);
24    CGImageRef        imageRef;
25
26    imageRef = CGImageSourceCreateImageAtIndex(isrc, 0, NULL);
27    CFRelease(isrc);
28
29    size_t pixelsWide = CGImageGetWidth(imageRef);
30    size_t pixelsHigh = CGImageGetHeight(imageRef);
31//    printf("%ld x %ld @ %ld\n", pixelsWide, pixelsHigh, CGImageGetBitsPerPixel(imageRef));
32
33    CGContextRef    context = NULL;
34    CGColorSpaceRef colorSpace;
35    void *          bitmapData;
36    int             bitmapByteCount;
37    int             bitmapBytesPerRow;
38
39    bitmapBytesPerRow   = (pixelsWide * 1);// 1
40    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);
41
42//    colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);// 2
43    colorSpace = CGColorSpaceCreateDeviceGray();// 2
44    bitmapData = calloc( bitmapByteCount, sizeof(char) );// 3
45    if (bitmapData == NULL)
46    {
47        fprintf (stderr, "Memory not allocated!");
48    }
49    context = CGBitmapContextCreate (bitmapData,// 4
50                                    pixelsWide,
51                                    pixelsHigh,
52                                    8,      // bits per component
53                                    bitmapBytesPerRow,
54                                    colorSpace,
55//                                    kCGImageAlphaPremultipliedLast);
56                                    kCGImageAlphaNone);
57    if (context== NULL)
58    {
59        free (bitmapData);// 5
60        fprintf (stderr, "Context not created!");
61    }
62    CGColorSpaceRelease( colorSpace );
63
64//    printf("%p\n", context);
65
66    CGRect bounds = CGRectMake (0, 0, pixelsWide, pixelsHigh);
67
68    CGContextSetGrayFillColor(context, 0xbf / 255.0, 1.00);
69    CGContextFillRect(context, bounds);
70
71    CGContextDrawImage(context, bounds, imageRef);
72
73    size_t x, y;
74    uint8_t * bits;
75    unsigned char data;
76
77#if 0
78    bits = (uint8_t *) bitmapData;
79    for (y = 0; y < pixelsHigh; y++)
80    {
81        for (x = 0; x < pixelsWide; x++)
82        {
83            data = *bits++;
84            assert (data <= 0xbf);
85
86            printf("0x%02x, ", data);
87        }
88        printf("\n");
89    }
90#endif
91    printf("\n\nunsigned char %s[%d * %d] = {\n", filename, pixelsWide, pixelsHigh);
92
93    bits = (uint8_t *) bitmapData;
94    for (y = 0; y < pixelsHigh; y++)
95    {
96        for (x = 0; x < pixelsWide; x++)
97        {
98            unsigned char data;
99            data = *bits++;
100            assert (data <= 0xbf);
101
102            data = (0x100 * data - (0xbf / 2)) / 0xbf;
103
104            if (!x) printf("    ");
105            printf("0x%02x, ", data);
106        }
107        printf("\n");
108    }
109    printf("};\n");
110
111}
112
113int main(int argc, char * argv[])
114{
115    int i;
116    for (i = 1; i < argc; i++)
117        spit(argv[i]);
118}
119
120
121
122