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