1 
2 /* Simple program to take a BMP image and turn it into a C header which can
3    be included in your program as an image data array.  You can then call
4    GUI_LoadImage() on the data in the header to create a usable SDL surface.
5 */
6 
7 #include <stdio.h>
8 #include "SDL.h"
9 
main(int argc,char * argv[])10 int main(int argc, char *argv[])
11 {
12 	SDL_Surface *bmp;
13 	int status, i, j;
14 	Uint8 *spot;
15 
16 	if ( argv[1] == NULL ) {
17 		fprintf(stderr, "Usage: %s <image.bmp>\n", argv[0]);
18 		exit(1);
19 	}
20 
21 	if ( SDL_Init(0) < 0 ) {
22 		fprintf(stderr, "Couldn't init SDL: %s\n", SDL_GetError());
23 		exit(-1);
24 	}
25 	atexit(SDL_Quit);
26 
27 	status = 0;
28 	bmp = SDL_LoadBMP(argv[1]);
29 	if ( bmp ) {
30 		if ( bmp->format->BitsPerPixel == 8 ) {
31 			SDL_Palette *palette;
32 
33 			printf("static int image_w = %d;\n", bmp->w);
34 			printf("static int image_h = %d;\n", bmp->h);
35 			printf("static Uint8 image_pal[] = {");
36 			palette = bmp->format->palette;
37 			for ( i=0; i<256; ++i ) {
38 				if ( (i%4) == 0 ) {
39 					printf("\n    ");
40 				}
41 				printf("0x%.2x, ", palette->colors[i].r);
42 				printf("0x%.2x, ", palette->colors[i].g);
43 				printf("0x%.2x, ", palette->colors[i].b);
44 			}
45 			printf("\n};\n");
46 			printf("static Uint8 image_data[] = {\n");
47 			for ( i=0; i<bmp->h; ++i ) {
48 				spot = (Uint8 *)bmp->pixels + i*bmp->pitch;
49 				printf("    ");
50 				for ( j=0; j<bmp->w; ++j ) {
51 					printf("0x%.2x, ", *spot++);
52 				}
53 				printf("\n");
54 			}
55 			printf("};\n");
56 		} else {
57 			fprintf(stderr,"Only 8-bit BMP images are supported\n");
58 			status = 3;
59 		}
60 		SDL_FreeSurface(bmp);
61 	} else {
62 		fprintf(stderr, "Unable to load %s: %s\n", argv[1],
63 							SDL_GetError());
64 		status = 2;
65 	}
66 	exit(status);
67 }
68