Loading...
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | /* setupdialog : converts a RAW image file into the c structure that the * kernel panic ui system expects. * * to build: cc -o setupdialog setupdialog.c */ #include <stdio.h> #include <sys/types.h> #include <fcntl.h> #define RUN_MAX 32767 void create_numbers_file( FILE *stream, char *outfile ); unsigned int encode_rle(unsigned char * fileArr, unsigned int filePos, unsigned int quantity, unsigned char value); void usage(void) { printf("\nusage: setupdialog -i <image file> -oi <image output> -n <numbers file> -on <numbers output>\n"); printf("\nYou can supply a panic image file, a numbers file, or both. Input files\n"); printf("must be in RAW format where each pixel is represented by an index into the\n"); printf("MacOS X system CLUT. The first %d bytes must be the width, height, and depth\n", 3 * sizeof(short)); printf("(in that order, %d bytes each).\n", sizeof(short)); printf("\nThe output files are generated C structures in the format the panic ui code\n"); printf("expects (default output files are panic_image.c and rendered_numbers.c).\n\n"); } int main( int argc, char *argv[] ) { int next; char *file = NULL, *ptr, *out = NULL, *numsfile = NULL, *numsout = NULL; FILE * stream, *out_stream; int * data; short width = 0, height = 0, depth = 0; char word[2]; char byte; unsigned int i, pixels, filePos; int err; unsigned char *fileArr; unsigned char nextP; unsigned int count; int currP; int fd; int pairs_this_line; // pull apart the arguments for( next = 1; next < argc; next++ ) { if (strcmp(argv[next], "-i") == 0) // image file (RAW/PICT?) file = argv[++next]; else if (strcmp(argv[next], "-n") == 0) // numbers/chars image file (RAW) numsfile = argv[++next]; else if (strcmp(argv[next], "-oi") == 0) // output file for image out = argv[++next]; else if (strcmp(argv[next], "-on") == 0) // output file for numbers numsout = argv[++next]; /* perhaps we should just let the user specify the W/H rather than require the header */ /* else if (strcmp(argv[next], "-w") == 0) // image width (pixels) width = strtoul(argv[++next], &ptr, 0); else if (strcmp(argv[next], "-h") == 0) // image height (pixels) width = strtoul(argv[++next], &ptr, 0); */ } if (!(numsfile || file)) { usage(); exit(1); } if (!numsfile) { printf("\nNo numbers file to process\n"); } else { stream = fopen(numsfile, "r"); if (!stream) { printf("bad nums infile.. bailing.\n"); exit(1); } create_numbers_file( stream, numsout ); fclose(stream); } if( file == NULL) { printf("\nNo image file to process\n"); exit(1); } stream = fopen(file, "r"); if (!stream) { printf("bad infile.. bailing.\n"); exit(1); } printf("\nReading image file...\n"); fread((void *) &width, sizeof(short), 1, stream); printf("got width: %d\n", width); fread((void *) &height, sizeof(short), 1, stream); printf("got height: %d\n", height); fread((void *) &depth, sizeof(short), 1, stream); printf("got depth: %d\n", depth); if (!(width && height && depth)) { printf("Invalid image file header (width, height, or depth is 0)\n"); exit(1); } pixels = width * height; if (!(fileArr = (unsigned char *) malloc(pixels))) { printf("couldn't malloc fileArr (%d pixels)... bailing.\n", pixels); exit(1); } currP = -1; count = 0; filePos = 0; // position in the file we're writing out for (i=0; i < pixels; i++) { nextP = fgetc(stream); count++; if (nextP == currP) { if (count >= RUN_MAX) { filePos += encode_rle(fileArr, filePos, count, (unsigned char) currP); count = 0; currP = -1; } } else { if (currP != -1) { filePos += encode_rle(fileArr, filePos, count-1, (unsigned char) currP); } currP = nextP; // start a new run count = 1; } } // write out any run that was in progress if (count > 0) { filePos += encode_rle(fileArr, filePos, count, (unsigned char) currP); } fclose( stream ); // now, generate the c file if ( out == NULL) out = "panic_image.c"; out_stream = fopen(out, "w"); if(out_stream == NULL) { printf("couldn't open out file.. bailing\n"); exit(1); } pairs_this_line = 0; fprintf( out_stream, "/* generated c file */\n\n"); fprintf( out_stream, "static const struct {\n"); fprintf( out_stream, " unsigned int pd_width;\n"); fprintf( out_stream, " unsigned int pd_height;\n"); fprintf( out_stream, " unsigned int bytes_per_pixel; /* 1: CLUT, 3:RGB, 4:RGBA */\n"); fprintf( out_stream, " unsigned char image_pixel_data[%#4.2x];\n", (filePos)); fprintf( out_stream, "} panic_dialog = {\n"); fprintf( out_stream, "\t%d, ", width); /* panic dialog x */ fprintf( out_stream, "%d, ", height); /* panic dialog y */ fprintf( out_stream, "1,\n"); /* bytes per pixel */ for( i=0; i < filePos;) { fprintf( out_stream, "0x%.2x,0x%.2x", fileArr[i], fileArr[i+1]); i+=2; pairs_this_line++; // if the first byte had a leading 1, this is a 3-byte encoding if ((fileArr[i-2] >> 7) == 1) { fprintf( out_stream, ",0x%.2x", fileArr[i++]); pairs_this_line++; } if (i >= filePos) // this is the last element fprintf( out_stream, "\n};"); else fprintf( out_stream, ", "); if(pairs_this_line > 8) { fprintf( out_stream, "\n"); pairs_this_line = 0; } } fclose( out_stream ); return 0; } /* Each number/char (0-f) has its own row in the pixmap array. When done, these rows each contain an RLE character. The image file is read row by row, so the individual characters must be constructed in the same way. The numPos array tracks the current position in each character's RLE array. */ void create_numbers_file( FILE *stream, char *outfile ) { int err; short height, depth, totalwidth; int numbers = 17; int width[17] = {9,7,8,6,9,7,8,7,8,7,10,7,9,10,7,6,4}; int numPos[17] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; int **pixmap; int row, col, item, line=0, currWidth; int nextP, currP; int count, currNum; FILE *out_stream; printf("\nReading numbers file...\n"); fread((void *) &totalwidth, sizeof(short), 1, stream); printf("got width: %d\n", totalwidth); fread((void *) &height, sizeof(short), 1, stream); printf("got height: %d\n", height); fread((void *) &depth, sizeof(short), 1, stream); printf("got depth: %d\n", depth); if (!(width && height && depth)) { printf("Invalid numbers file header (width, height, or depth is 0)\n"); return; } // allocate array to hold each number's RLE encoding (20 = 2xwidest width[i] value, 17 = num chars) pixmap = (int **) malloc( 17 * sizeof(int *) ); for( item=0; item<17; item++) pixmap[item] = (int *) malloc( 2*width[item]*height*sizeof(int) ); currP = -1; count = 0; currWidth = 0; currNum = 0; for( row=0; row < height; row++) { for( item=0; item < numbers; item++) { count = 0; currP = -1; // start each character fresh for( col=0; col < width[item]; col++) { nextP = fgetc( stream ); if( nextP == currP) { if( count == 127) { // probably never executed given the small widths pixmap[item][numPos[item]] = count; pixmap[item][numPos[item]+1] = currP; numPos[item]+=2; count = 0; currP = -1; } else count++; // add one to the current run } else { if( currP != -1) { pixmap[item][numPos[item]] = count; // currP was the end of the run pixmap[item][numPos[item]+1] = currP; numPos[item]+=2; } currP = nextP; // start a new run count = 1; } } // write out any run that was in progress if( count > 0) { pixmap[item][numPos[item]] = count; pixmap[item][numPos[item]+1] = currP; numPos[item]+=2; } } } // now, generate the c file if ( outfile == NULL) outfile = "rendered_numbers.c"; out_stream = fopen(outfile, "w"); if(out_stream == NULL) { printf("couldn't open numbers outfile.. bailing\n"); exit(1); } fprintf( out_stream, " /* generated c file */\n\n"); // iterate through all the numbers/chars for( item=0; item<numbers; item++) { fprintf( out_stream, "static const struct {\n"); fprintf( out_stream, " unsigned int num_w;\n"); fprintf( out_stream, " unsigned int num_h;\n"); fprintf( out_stream, " unsigned char num_pixel_data[%#4.2x];\n", numPos[item]); // num elems item == 16 ? fprintf( out_stream, "} num_colon = {\n") : fprintf( out_stream, "} num_%x = {\n", item); fprintf( out_stream, "/* w */ %d,\n", width[item]); fprintf( out_stream, "/* h */ %d,\n", height); fprintf( out_stream, "/* pixel_data */ \n"); for( col = 0; col < numPos[item];) { fprintf( out_stream, "0x%.2x,0x%.2x", pixmap[item][col], pixmap[item][col+1]); if (col == (numPos[item] - 2)) // this is the last element fprintf( out_stream, "\n};\n\n"); else fprintf( out_stream, ", "); line+=pixmap[item][col]; if( line >= width[item]) { fprintf( out_stream, "\n"); line = 0; } col+=2; } } fclose( out_stream ); } /* encode_rle applies a "modified-RLE encoding to a given image. The encoding works as follows: The quantity and value will be described by either two or three bytes. If the most significant bit of the first byte is a 0, then the next seven bits are the quantity (run-length) and the following 8 bits are the value (index into a clut, in this case). If the msb of the first byte is a 1, then the next 15 bits are the quantity and the following 8 are the value. Visually, the two possible encodings are: (q = quantity, v = value) Byte 1 Byte 2 Byte 3 case 1: [ 0 q6 q5 q4 q3 q2 q1 q0 ] [ v7 v6 v5 v4 v3 v2 v1 v0 ] [ ] case 2: [ 1 q14 q13 q12 a11 q10 q9 q8 ] [ q7 q6 q5 q4 q3 q2 q1 q0 ] [ v7 v6 v5 v4 v3 v2 v1 v0 ] */ unsigned int encode_rle(unsigned char * fileArr, unsigned int filePos, unsigned int quantity, unsigned char value) { unsigned char single_mask = 0x00; unsigned char double_mask = 0x80; unsigned char slots_used = 0; if (quantity < 128) { fileArr[filePos] = single_mask | quantity; slots_used = 1; } else { fileArr[filePos] = double_mask | (quantity >> 8); // high 7 bits (plus mask) fileArr[filePos+1] = (unsigned char) quantity; // low 8 bits slots_used = 2; } fileArr[filePos+slots_used] = value; slots_used++; return slots_used; } |