2 //cc progress.m -framework AppKit -Wall; ./a.out >/tmp/xx.c; cc /tmp/xx.c -Wall; cat /tmp/xx.c
4 #import <Foundation/Foundation.h>
5 #import <AppKit/AppKit.h>
12 #define MAX_COLORS 256
20 static uint32_t clut_size = 0;
21 static pixel_t clut[MAX_COLORS];
24 lookup_color(uint8_t r, uint8_t g, uint8_t b)
28 for (i = 0; i < clut_size; i++) {
35 if (clut_size >= MAX_COLORS) {
36 printf("Image must have no more than 256 unique pixel colors\n");
39 clut[clut_size].r = r;
40 clut[clut_size].g = g;
41 clut[clut_size].b = b;
43 return (uint8_t)clut_size++;;
46 void print_buffer (uint8_t * buffer, size_t width, size_t height, size_t row)
49 for (int y = 0; y < height; y++)
52 for (int x = 0; x < width; x++)
54 printf("0x%02x,", buffer[x + y*row]);
60 int onefile(const char * filename, int w, int h)
66 if ((file = fopen(filename, "r")) == NULL) {
68 printf ("ERROR!!! can not open resource file [%s]\n", filename);
73 NSString* filePath = [NSString stringWithUTF8String:filename];
74 NSData* fileData = [NSData dataWithContentsOfFile:filePath];
75 NSBitmapImageRep* bitmapImageRep = [[NSBitmapImageRep alloc] initWithData:fileData];
76 NSSize imageSize = [bitmapImageRep size];
78 size_t image_length = (int)imageSize.width * (int)imageSize.height;
79 uint8_t* uncompressed_color_buffer = malloc(image_length);
80 uint8_t* uncompressed_alpha_buffer = malloc(image_length);
82 bzero(clut, sizeof(clut));
87 for (int y = 0; y < imageSize.height; y++) {
88 for (int x = 0; x < imageSize.width; x++) {
89 NSUInteger pixel[4] = {};
90 [bitmapImageRep getPixel:pixel atX:x y:y];
92 color = lookup_color((uint8_t)pixel[0],
97 uint8_t alpha = pixel[3];
98 assert((alpha != 0) == color);
102 uncompressed_color_buffer[size] = color;
103 uncompressed_alpha_buffer[size] = alpha;
108 assert(clut_size == 2);
109 assert(clut[0].r == 0);
110 assert(clut[0].g == 0);
111 assert(clut[0].b == 0);
112 assert(clut[1].r == 0xff);
113 assert(clut[1].g == 0xff);
114 assert(clut[1].b == 0xff);
118 assert(w <= imageSize.width);
119 assert(h <= imageSize.height);
121 print_buffer (uncompressed_alpha_buffer, w, h, imageSize.width);
123 if (uncompressed_color_buffer != NULL) {
124 free (uncompressed_color_buffer);
126 if (uncompressed_alpha_buffer != NULL) {
127 free (uncompressed_alpha_buffer);
134 int main (int argc, char * argv[])
136 printf("#include <stdint.h>\n\n");
139 printf("\nstatic const unsigned char progressmeter_leftcap1x[2][%d * %d] = {", 9, 18);
140 onefile("ProgressBarFullLeftEndCap.png", 9, 18);
142 onefile("ProgressBarEmptyLeftEndCap.png", 9, 18);
145 printf("\nstatic const unsigned char progressmeter_leftcap2x[2][4 * %d * %d] = {", 9, 18);
146 onefile("ProgressBarFullLeftEndCap@2x.png", 2*9, 2*18);
148 onefile("ProgressBarEmptyLeftEndCap@2x.png", 2*9, 2*18);
151 printf("\nstatic const unsigned char progressmeter_middle1x[2][%d * %d] = {", 1, 18);
152 onefile("ProgressBarFullMiddle.png", 1, 18);
154 onefile("ProgressBarEmptyMiddle.png", 1, 18);
157 printf("\nstatic const unsigned char progressmeter_middle2x[2][2 * %d * %d] = {", 1, 18);
158 onefile("ProgressBarFullMiddle@2x.png", 1, 2*18);
160 onefile("ProgressBarEmptyMiddle@2x.png", 1, 2*18);
163 printf("\nstatic const unsigned char progressmeter_rightcap1x[2][%d * %d] = {", 9, 18);
164 onefile("ProgressBarFullRightEndCap.png", 9, 18);
166 onefile("ProgressBarEmptyRightEndCap.png", 9, 18);
169 printf("\nstatic const unsigned char progressmeter_rightcap2x[2][4 * %d * %d] = {", 9, 18);
170 onefile("ProgressBarFullRightEndCap@2x.png", 2*9, 2*18);
172 onefile("ProgressBarEmptyRightEndCap@2x.png", 2*9, 2*18);