]>
Commit | Line | Data |
---|---|---|
fe8ab488 A |
1 | // |
2 | //cc progress.m -framework AppKit -Wall; ./a.out >/tmp/xx.c; cc /tmp/xx.c -Wall; cat /tmp/xx.c | |
3 | ||
4 | #import <Foundation/Foundation.h> | |
5 | #import <AppKit/AppKit.h> | |
6 | #import <stdlib.h> | |
7 | #import <stdint.h> | |
8 | #include <getopt.h> | |
9 | #import <string.h> | |
10 | ||
11 | ||
12 | #define MAX_COLORS 256 | |
13 | ||
14 | typedef struct { | |
15 | uint8_t r; | |
16 | uint8_t g; | |
17 | uint8_t b; | |
18 | } pixel_t; | |
19 | ||
20 | static uint32_t clut_size = 0; | |
21 | static pixel_t clut[MAX_COLORS]; | |
22 | ||
23 | static uint8_t | |
24 | lookup_color(uint8_t r, uint8_t g, uint8_t b) | |
25 | { | |
26 | unsigned int i; | |
27 | ||
28 | for (i = 0; i < clut_size; i++) { | |
29 | if (clut[i].r == r && | |
30 | clut[i].g == g && | |
31 | clut[i].b == b) { | |
32 | return i; | |
33 | } | |
34 | } | |
35 | if (clut_size >= MAX_COLORS) { | |
36 | printf("Image must have no more than 256 unique pixel colors\n"); | |
37 | exit(1); | |
38 | } | |
39 | clut[clut_size].r = r; | |
40 | clut[clut_size].g = g; | |
41 | clut[clut_size].b = b; | |
42 | ||
43 | return (uint8_t)clut_size++;; | |
44 | } | |
45 | ||
46 | void print_buffer (uint8_t * buffer, size_t width, size_t height, size_t row) | |
47 | { | |
48 | printf("{"); | |
49 | for (int y = 0; y < height; y++) | |
50 | { | |
51 | printf("\n "); | |
52 | for (int x = 0; x < width; x++) | |
53 | { | |
54 | printf("0x%02x,", buffer[x + y*row]); | |
55 | } | |
56 | } | |
57 | printf("\n}"); | |
58 | } | |
59 | ||
60 | int onefile(const char * filename, int w, int h) | |
61 | { | |
62 | int size; | |
63 | uint8_t color; | |
64 | ||
65 | FILE *file; | |
66 | if ((file = fopen(filename, "r")) == NULL) { | |
67 | fclose(file); | |
68 | printf ("ERROR!!! can not open resource file [%s]\n", filename); | |
69 | return 1; | |
70 | } | |
71 | fclose(file); | |
72 | ||
73 | NSString* filePath = [NSString stringWithUTF8String:filename]; | |
74 | NSData* fileData = [NSData dataWithContentsOfFile:filePath]; | |
75 | NSBitmapImageRep* bitmapImageRep = [[NSBitmapImageRep alloc] initWithData:fileData]; | |
76 | NSSize imageSize = [bitmapImageRep size]; | |
77 | ||
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); | |
81 | ||
82 | bzero(clut, sizeof(clut)); | |
83 | ||
84 | clut_size = 0; | |
85 | size = 0; | |
86 | ||
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]; | |
91 | ||
92 | color = lookup_color((uint8_t)pixel[0], | |
93 | (uint8_t)pixel[1], | |
94 | (uint8_t)pixel[2]); | |
95 | ||
96 | assert(color <= 1); | |
97 | uint8_t alpha = pixel[3]; | |
98 | assert((alpha != 0) == color); | |
99 | ||
100 | alpha = 255 - alpha; | |
101 | ||
102 | uncompressed_color_buffer[size] = color; | |
103 | uncompressed_alpha_buffer[size] = alpha; | |
104 | size++; | |
105 | } | |
106 | } | |
107 | ||
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); | |
115 | ||
116 | printf("\n"); | |
117 | ||
118 | assert(w <= imageSize.width); | |
119 | assert(h <= imageSize.height); | |
120 | ||
121 | print_buffer (uncompressed_alpha_buffer, w, h, imageSize.width); | |
122 | ||
123 | if (uncompressed_color_buffer != NULL) { | |
124 | free (uncompressed_color_buffer); | |
125 | } | |
126 | if (uncompressed_alpha_buffer != NULL) { | |
127 | free (uncompressed_alpha_buffer); | |
128 | } | |
129 | ||
130 | return 0; | |
131 | } | |
132 | ||
133 | ||
134 | int main (int argc, char * argv[]) | |
135 | { | |
136 | printf("#include <stdint.h>\n\n"); | |
137 | ||
138 | ||
139 | printf("\nstatic const unsigned char progressmeter_leftcap1x[2][%d * %d] = {", 9, 18); | |
140 | onefile("ProgressBarFullLeftEndCap.png", 9, 18); | |
141 | printf(","); | |
142 | onefile("ProgressBarEmptyLeftEndCap.png", 9, 18); | |
143 | printf("};\n"); | |
144 | ||
145 | printf("\nstatic const unsigned char progressmeter_leftcap2x[2][4 * %d * %d] = {", 9, 18); | |
146 | onefile("ProgressBarFullLeftEndCap@2x.png", 2*9, 2*18); | |
147 | printf(","); | |
148 | onefile("ProgressBarEmptyLeftEndCap@2x.png", 2*9, 2*18); | |
149 | printf("};\n"); | |
150 | ||
151 | printf("\nstatic const unsigned char progressmeter_middle1x[2][%d * %d] = {", 1, 18); | |
152 | onefile("ProgressBarFullMiddle.png", 1, 18); | |
153 | printf(","); | |
154 | onefile("ProgressBarEmptyMiddle.png", 1, 18); | |
155 | printf("};\n"); | |
156 | ||
157 | printf("\nstatic const unsigned char progressmeter_middle2x[2][2 * %d * %d] = {", 1, 18); | |
158 | onefile("ProgressBarFullMiddle@2x.png", 1, 2*18); | |
159 | printf(","); | |
160 | onefile("ProgressBarEmptyMiddle@2x.png", 1, 2*18); | |
161 | printf("};\n"); | |
162 | ||
163 | printf("\nstatic const unsigned char progressmeter_rightcap1x[2][%d * %d] = {", 9, 18); | |
164 | onefile("ProgressBarFullRightEndCap.png", 9, 18); | |
165 | printf(","); | |
166 | onefile("ProgressBarEmptyRightEndCap.png", 9, 18); | |
167 | printf("};\n"); | |
168 | ||
169 | printf("\nstatic const unsigned char progressmeter_rightcap2x[2][4 * %d * %d] = {", 9, 18); | |
170 | onefile("ProgressBarFullRightEndCap@2x.png", 2*9, 2*18); | |
171 | printf(","); | |
172 | onefile("ProgressBarEmptyRightEndCap@2x.png", 2*9, 2*18); | |
173 | printf("};\n"); | |
174 | ||
175 | ||
176 | } | |
177 |