2palpic: use getopt, improve usage output, fix sprite_count 0 in binary out
[rofl0r-openDOW.git] / utils / 2palpic.c
blob21eef7423f2a4fa472b99cce4b99881c6f03919a
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include "../palpic.h"
6 #include <leptonica/allheaders.h>
7 #define assert(x) if(!(x)) __asm__("int3");
9 #pragma RcB2 LINK "-llept"
10 /* leptonica:
11 enum {
12 COLOR_RED = 0,
13 COLOR_GREEN = 1,
14 COLOR_BLUE = 2,
15 L_ALPHA_CHANNEL = 3
18 static const l_int32 L_RED_SHIFT =
19 8 * (sizeof(l_uint32) - 1 - COLOR_RED); //24
20 static const l_int32 L_GREEN_SHIFT =
21 8 * (sizeof(l_uint32) - 1 - COLOR_GREEN); //16
22 static const l_int32 L_BLUE_SHIFT =
23 8 * (sizeof(l_uint32) - 1 - COLOR_BLUE); //8
24 static const l_int32 L_ALPHA_SHIFT =
25 8 * (sizeof(l_uint32) - 1 - L_ALPHA_CHANNEL); //0
27 composeRGBPixel(l_int32 rval, l_int32 gval, l_int32 bval, l_uint32 *ppixel)
29 *ppixel = (rval << L_RED_SHIFT) | (gval << L_GREEN_SHIFT) | (bval << L_BLUE_SHIFT);
30 return 0;
31 } */
33 static unsigned get_sprite_start(unsigned sprite_nr, unsigned row_nr, unsigned sprite_w, unsigned sprite_h, unsigned sprites_per_row) {
34 unsigned sprite_row = sprite_nr / sprites_per_row;
35 unsigned row_off = (row_nr * sprite_w * sprites_per_row);
36 unsigned res = (sprite_row * sprite_w * sprite_h * sprites_per_row) + row_off + ((sprite_nr % sprites_per_row) * sprite_w);
37 //printf("sprite %.2u, row %.2u : %u\n", sprite_nr, row_nr, res);
38 return res;
41 static int usage(char *a0) {
42 printf("syntax: %s [-t] [-s WxH] infile outfile\n"
43 "where WxH denotes the dimensions of a single picture in the spritesheet\n"
44 "-t, if given, means the first color in the bitmap is the transparent color\n"
45 "if -s is omitted, the file will be considered a single sprite\n"
46 "if the file extension is .c, a C file will be written, else a binary ppic\n", a0);
47 return 1;
50 #define STRSZ(lit) lit, sizeof(lit) - 1
51 #define LIT_SIZE(lit) ( sizeof(lit) - 1)
52 int main(int argc, char** argv) {
53 int w, h;
54 struct palpic pp = palpic_empty;
55 prgb pal[256];
57 struct Pix* infile;
58 struct Pix* pix32;
60 unsigned sprite_w = 0, sprite_h = 0;
61 unsigned sprite_count;
63 int c;
64 while((c = getopt(argc, argv, "s:t")) != EOF) switch(c) {
65 case 't': pp.flags |= PPF_TRANSPARENT; break;
66 case 's': {
67 char* str_w = optarg;
68 char* str_h = strchr(str_w, 'x');
69 *(str_h++) = 0;
70 sprite_w = atoi(str_w);
71 sprite_h = atoi(str_h);
73 }; break;
74 default: return usage(argv[0]);
77 if(!argv[optind] || !argv[optind+1]) return usage(argv[0]);
79 char *in_filename = argv[optind];
80 char *out_filename = argv[optind+1];
82 if(access(in_filename, R_OK) == -1) {
83 perror(in_filename);
84 return 1;
87 char* cp;
88 int to_c = (cp = strrchr(out_filename, '.')) && cp == out_filename + strlen(out_filename) - 2 && cp[1] == 'c';
89 char struct_name[256];
90 if(to_c) {
91 char *st_start = strrchr(out_filename, '/');
92 if(st_start) st_start++;
93 else st_start = out_filename;
94 size_t l = snprintf(struct_name, sizeof struct_name, "%s", st_start);
95 struct_name[l-2] = 0;
99 infile = pixRead(in_filename);
101 pixGetDimensions(infile, &w, &h, NULL);
103 if(sprite_w && sprite_h) {
104 sprite_count = (h * w) / (sprite_h * sprite_w);
105 unsigned rest = (h * w) % (sprite_h * sprite_w);
106 if(rest) {
107 printf("error: the picture dimensions are no multiple of the sprite dimensions!\n");
108 return 1;
110 if(w % sprite_w) {
111 printf("error: the picture width is no multiple of the sprite width!\n");
112 return 1;
114 } else {
115 // no sprite support
116 sprite_w = w;
117 sprite_h = h;
118 sprite_count = 1;
121 unsigned sprites_per_row = w / sprite_w;
123 /* we always align sprites vertically */
124 pp.height = sprite_h * sprite_count;
125 pp.width = sprite_w;
127 pp.spritecount = sprite_count;
129 pix32 = pixConvertTo32(infile);
130 prgb* bufptr = (prgb*) pix32->data;
132 unsigned x, y = 0, p;
133 for(y = 0; y < (unsigned) h; y++) {
134 for(x = 0; x < (unsigned) w; x++) {
135 for(p = 0; p < pp.palcount; p++)
136 if(pal[p].val == bufptr->val)
137 goto next_x;
138 pal[pp.palcount++] = *bufptr;
139 next_x:
140 bufptr++;
144 FILE* outfile = fopen(out_filename, "w");
147 unsigned sprite;
148 if(!to_c) {
149 palpic_fileformat(&pp);
150 fwrite(&pp, sizeof(pp), 1, outfile);
151 fwrite(pal, sizeof(prgb) * pp.palcount, 1, outfile);
153 for(sprite = 0; sprite < sprite_count; sprite++) {
154 for(y = 0; y < sprite_h; y++) {
155 unsigned sprite_start_y = get_sprite_start(sprite, y, sprite_w, sprite_h, sprites_per_row);
156 bufptr = &((prgb*) pix32->data)[sprite_start_y];
158 for(x = 0; x < sprite_w; x++) {
159 for(p = 0; p < pp.palcount; p++)
160 if(pal[p].val == bufptr->val) {
161 uint8_t val = p;
162 fwrite(&val, sizeof(val), 1, outfile);
163 goto success;
165 fprintf(stderr, "bug: could not find color in palette\n");
166 return 1;
167 success:
168 bufptr++;
172 } else {
173 char buf[1024];
174 snprintf(buf, sizeof(buf),
175 "#include \"palpic.h\"\n"
176 "#define PAL_COUNT %d\n"
177 "#define SPRITE_COUNT %d\n"
178 "#define WIDTH %d\n"
179 "#define HEIGHT %d\n"
180 "#define o 0\n"
182 "#define STRUCT_NAME %s\n\n"
183 "static const struct {\n"
184 "\tstruct palpic header;\n"
185 "\tprgb palette[PAL_COUNT];\n"
186 "\tuint8_t data[WIDTH * HEIGHT];\n"
187 "} STRUCT_NAME = { \n"
188 "\t{ {'p', 'P', 'i', 'C', }, 1, PAL_COUNT, SPRITE_COUNT, WIDTH, HEIGHT, %s, 0 },\n"
189 "\t{\n\t\t",
190 (int) pp.palcount,
191 (int) sprite_count,
192 (int) pp.width,
193 (int) pp.height,
194 struct_name,
195 (pp.flags & PPF_TRANSPARENT ? "PPF_TRANSPARENT" : "0")
197 fwrite(buf, strlen(buf), 1, outfile);
199 for(p = 0; p < pp.palcount; p++) {
200 if(p == 0 && pp.flags & PPF_TRANSPARENT)
201 snprintf(buf, sizeof(buf), "PRGB(%3u, %3u, %3u), ", (unsigned) 0,(unsigned) 0, (unsigned) 0);
202 else
203 snprintf(buf, sizeof(buf), "PRGB(%3u, %3u, %3u), ", (unsigned) pal[p].colors.r,(unsigned) pal[p].colors.g, (unsigned) pal[p].colors.b);
204 fwrite(buf, strlen(buf), 1, outfile);
205 //if(p % 8 == 7)
206 fwrite(STRSZ("\n\t\t"), 1, outfile);
209 snprintf(buf, sizeof(buf), "\n\t},\n\t{\n\t\t");
210 fwrite(buf, strlen(buf), 1, outfile);
211 unsigned counter = 0;
212 unsigned values_per_line = sprite_w;
213 const char *value_format = pp.palcount > 99 ? "%3u," : "%2u,";
214 const char *o_format = pp.palcount > 99 ? " o," : " o,";
215 for(sprite = 0; sprite < sprite_count; sprite++) {
216 fprintf(outfile, "/* sprite #%.3u */\n\t\t", sprite);
217 for(y = 0; y < sprite_h; y++) {
218 unsigned sprite_start_y = get_sprite_start(sprite, y, sprite_w, sprite_h, sprites_per_row);
219 assert(sprite_start_y + sprite_w <= h * w);
220 bufptr = &((prgb*) pix32->data)[sprite_start_y];
222 for(x = 0; x < sprite_w; x++) {
223 for(p = 0; p < pp.palcount; p++) {
224 if(pal[p].val == bufptr->val) {
225 if(p == 0)
226 snprintf(buf, sizeof(buf), o_format);
227 else
228 snprintf(buf, sizeof(buf), value_format, (unsigned) p);
229 fwrite(buf, strlen(buf), 1, outfile);
230 if(counter % values_per_line == values_per_line - 1)
231 fwrite(STRSZ("\n\t\t"), 1, outfile);
232 goto success2;
235 fprintf(stderr, "bug: could not find color in palette\n");
236 return 1;
237 success2:
238 bufptr++;
239 counter++;
244 fwrite(STRSZ("\n\t},\n"), 1, outfile);
246 snprintf(buf, sizeof(buf),
247 "};\n\n"
248 "#undef o\n"
249 "#undef PAL_COUNT\n"
250 "#undef SPRITE_COUNT\n"
251 "#undef WIDTH\n"
252 "#undef HEIGHT\n"
253 "#undef STRUCT_NAME\n\n");
254 fwrite(buf, strlen(buf), 1, outfile);
257 fclose(outfile);
258 pixDestroy(&pix32);
259 pixDestroy(&infile);
260 return 0;