[1047] | 1 | /* This program converts a 128x128 16-bit color BMP image to a 128x128 8-bit RGB array of pixel values |
---|
| 2 | * suitable for use on the WARP User I/O Board's LCD screen. It produces two output formats; for both formats |
---|
| 3 | * the pixles are re-arranged into 256 64-pixel blocks to accomodate the User I/O board controller's |
---|
| 4 | * character map pixel ordering. |
---|
| 5 | * |
---|
| 6 | * See http://warp.rice.edu/trac/wiki/HardwareUsersGuides/UserIOBoard_v1.0/LCD for details |
---|
| 7 | * |
---|
| 8 | * Output formats: |
---|
| 9 | * RGB: a binary file with exactly 128*128 1-byte pixel values (16KB); each pixel is stored as [RRRGGGBB] |
---|
| 10 | * C Header: a header file with one variable (unsigned char imgArray[16384] defined) |
---|
| 11 | * |
---|
| 12 | * This program has been tested on Mac OS X 10.5.4. It should work on any Unix-ish OS. |
---|
| 13 | * |
---|
| 14 | * This code is not thoroughly tested, nor is it guaranteed error-free or even safe. Use it at your own risk. |
---|
| 15 | * |
---|
| 16 | * This code is available under the Rice WARP license. See http://warp.rice.edu/license for details. |
---|
| 17 | */ |
---|
| 18 | |
---|
[1046] | 19 | #include <stdio.h> |
---|
| 20 | |
---|
| 21 | #define BMP16_NUM_HEADER_BYTES 54 |
---|
| 22 | |
---|
| 23 | #define PIXCONV_RED_5b_TO_3b(c) ( (c>>12)&0x7 ) |
---|
| 24 | #define PIXCONV_GREEN_5b_TO_3b(c) ( (c>>7)&0x7 ) |
---|
| 25 | #define PIXCONV_BLUE_5b_TO_2b(c) ( (c>>3)&0x3 ) |
---|
| 26 | |
---|
| 27 | #define PIXCONV_16b_TO_8b(c) ( 0xFF & ( (PIXCONV_RED_5b_TO_3b(c)<<5) | (PIXCONV_GREEN_5b_TO_3b(c)<<2) | PIXCONV_BLUE_5b_TO_2b(c) )) |
---|
| 28 | |
---|
| 29 | void print_usage(char* progName); |
---|
| 30 | |
---|
| 31 | int main(int argc, char * argv[]) |
---|
| 32 | { |
---|
| 33 | int i, j, k, m, n; |
---|
| 34 | |
---|
| 35 | FILE *fp_in, *fp_outBin, *fp_outHeader; |
---|
| 36 | size_t bytesRead, bytesWritten; |
---|
| 37 | |
---|
| 38 | unsigned char outputHeader; |
---|
| 39 | |
---|
| 40 | unsigned char img_header[BMP16_NUM_HEADER_BYTES]; |
---|
| 41 | |
---|
| 42 | unsigned short img_orig[128*128]; |
---|
| 43 | unsigned char img_rgb8[128*128]; |
---|
| 44 | |
---|
| 45 | //Check the command line arguments |
---|
| 46 | if(argc < 3 | argc > 4) |
---|
| 47 | { |
---|
| 48 | printf("Bad input arguments!\n"); |
---|
| 49 | print_usage(argv[0]); |
---|
| 50 | return -1; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | if(argc == 3) |
---|
| 54 | { |
---|
| 55 | //User specified an input file and output RGB file; don't print the C header file |
---|
| 56 | fp_in = fopen(argv[1], "r+b"); |
---|
| 57 | fp_outBin = fopen(argv[2], "w+b"); |
---|
| 58 | outputHeader = 0; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | if(argc == 4) |
---|
| 62 | { |
---|
[1047] | 63 | //User specified an input file, output RGB file and output C header file |
---|
[1046] | 64 | fp_in = fopen(argv[1], "r+b"); |
---|
| 65 | fp_outBin = fopen(argv[2], "w+b"); |
---|
| 66 | fp_outHeader = fopen(argv[3], "w+"); |
---|
| 67 | outputHeader = 1; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | bytesRead = fread(&img_header, 1, BMP16_NUM_HEADER_BYTES, fp_in); |
---|
| 71 | if(bytesRead != BMP16_NUM_HEADER_BYTES) |
---|
| 72 | { |
---|
| 73 | printf("Error! - not enough bytes for the header\n"); |
---|
| 74 | return -1; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | bytesRead = fread(&img_orig, sizeof(short), 128*128, fp_in); |
---|
| 78 | if(bytesRead != (128*128)) |
---|
| 79 | { |
---|
| 80 | printf("Error! - not enough bytes of image data (read %d)\n", bytesRead); |
---|
| 81 | return -1; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | fclose(fp_in); |
---|
| 85 | |
---|
| 86 | //Now img_orig is a 128x128 array of 16-bit pixel values |
---|
| 87 | //We need an array of 16x16 64-pixel blocks, with each pixel occuping 1 byte |
---|
| 88 | //This is probably a really inefficient way to implement this re-ordering |
---|
| 89 | // but for 16K entries on a fast PC, who cares? |
---|
| 90 | m = 0; |
---|
| 91 | for(i=15; i>=0; i--)//Count down, since BMP files are stored last-line-first |
---|
| 92 | { |
---|
| 93 | for(j=0; j<16; j++) |
---|
| 94 | { |
---|
| 95 | for(k=7; k>=0; k--)//Count down, since BMP files are stored last-line-first |
---|
| 96 | { |
---|
| 97 | for(n=0; n<8; n++) |
---|
| 98 | { |
---|
| 99 | img_rgb8[m++] = (unsigned char)PIXCONV_16b_TO_8b(img_orig[i*1024 + j*8 + k*128 + n]); |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | if(outputHeader) |
---|
| 106 | { |
---|
| 107 | fprintf(fp_outHeader, "/***START imgArray ***/\nunsigned char imgArray[16384] = {"); |
---|
| 108 | |
---|
| 109 | for(i=0; i<16384; i++) |
---|
| 110 | { |
---|
| 111 | fprintf(fp_outHeader, "0x%x", img_rgb8[i]); |
---|
| 112 | if(i < 16383) fprintf(fp_outHeader, ","); |
---|
| 113 | if(i!=0 & i%128==0) fprintf(fp_outHeader, "\n"); |
---|
| 114 | } |
---|
| 115 | fprintf(fp_outHeader, "};\n/***END imgArray ***/\n"); |
---|
| 116 | |
---|
| 117 | fclose(fp_outHeader); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | bytesWritten = fwrite(&img_rgb8, 1, 128*128, fp_outBin); |
---|
[1047] | 121 | fclose(fp_outBin); |
---|
| 122 | |
---|
| 123 | if(bytesWritten != (128*128)) |
---|
[1046] | 124 | { |
---|
| 125 | printf("Error! - not enough bytes of image data written (wrote %d)\n", bytesWritten); |
---|
| 126 | return -1; |
---|
| 127 | } |
---|
| 128 | |
---|
[1047] | 129 | //Return successfully |
---|
[1046] | 130 | return 0; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void print_usage(char* progName) |
---|
| 134 | { |
---|
| 135 | printf("Usage: %s inputBitmap.bmp outputBinary.rgb <outputHeader.h>\n", progName); |
---|
[1047] | 136 | } |
---|