13 enum { AS_GNU, AS_NASM };
15 int csprite(struct image *img, int x, int y, int xsz, int ysz);
16 int proc_sheet(const char *fname);
17 void print_usage(const char *argv0);
19 int tile_xsz, tile_ysz;
23 int fbpitch, fbwidth = 320;
24 const char *name = "sprite";
28 int main(int argc, char **argv)
33 for(i=1; i<argc; i++) {
34 if(argv[i][0] == '-') {
35 if(strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "-size") == 0) {
36 if(sscanf(argv[++i], "%dx%d", &tile_xsz, &tile_ysz) != 2 ||
37 tile_xsz <= 0 || tile_ysz <= 0) {
38 fprintf(stderr, "%s must be followed by WIDTHxHEIGHT\n", argv[i - 1]);
42 } else if(strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "-rect") == 0) {
44 if(sscanf(argv[++i], "%dx%d+%d+%d", &rect.w, &rect.h, &rect.x, &rect.y) < 2 || rect.w <= 0 || rect.h <= 0) {
45 fprintf(stderr, "%s must be followed by WIDTHxHEIGHT+X+Y\n", argv[i - 1]);
49 } else if(strcmp(argv[i], "-coffset") == 0) {
50 cmap_offs = strtol(argv[++i], &endp, 10);
51 if(endp == argv[i] || cmap_offs < 0 || cmap_offs >= 256) {
52 fprintf(stderr, "-coffset must be followed by a valid colormap offset\n");
56 } else if(strcmp(argv[i], "-fbwidth") == 0) {
57 fbwidth = atoi(argv[++i]);
59 fprintf(stderr, "-fbwidth must be followed by a positive number\n");
63 } else if(strcmp(argv[i], "-fbpitch") == 0) {
64 fbpitch = atoi(argv[++i]);
66 fprintf(stderr, "-fbpitch must be followed by a positive number\n");
70 } else if(strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "-name") == 0) {
73 fprintf(stderr, "%s must be followed by a name\n", argv[i - 1]);
77 } else if(strcmp(argv[i], "-k") == 0 || strcmp(argv[i], "-key") == 0) {
78 ckey = strtol(argv[++i], &endp, 10);
79 if(endp == argv[i] || ckey < 0) {
80 fprintf(stderr, "%s must be followed by a valid color key\n", argv[i - 1]);
84 } else if(strcmp(argv[i], "-conv565") == 0) {
87 } else if(strcmp(argv[i], "-gas") == 0) {
90 } else if(strcmp(argv[i], "-nasm") == 0) {
93 } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
98 fprintf(stderr, "invalid option: %s\n", argv[i]);
104 if(proc_sheet(argv[i]) == -1) {
113 /* prototype of generated function is (void *fb, int x, int y, int idx) */
114 const char *prefixfmt[] = {
115 /* GNU assembler template */
121 "\tmov 12(%%esp), %%eax\n"
124 "\tadd 8(%%esp), %%eax\n"
125 "\tadd 4(%%esp), %%eax\n"
126 "\tmov %%eax, %%edx\n"
127 "\tmov 16(%%esp), %%eax\n"
128 "\tjmp *tiletab(,%%eax,4)\n\n"
132 /* TODO hardcoding the 16bpp changes for now, generalize later
133 * and while we're at it, let's get rid of the mul too ...
140 "\tmov eax, [esp + 12]\n"
147 "\tadd eax, [esp + 8]\n"
148 "\tadd eax, [esp + 8]\n"
149 "\tadd eax, [esp + 4]\n"
151 "\tmov eax, [esp + 16]\n"
152 "\tjmp [titletab + eax * 4]\n\n"
156 int proc_sheet(const char *fname)
158 int i, j, num_xtiles, num_ytiles, xsz, ysz, tidx;
161 if(load_image(&img, fname) == -1) {
162 fprintf(stderr, "failed to load image: %s\n", fname);
167 if(conv_image_rgb565(&tmp, &img) == -1) {
168 fprintf(stderr, "failed to convert image to 16bpp 565: %s\n", fname);
176 if(!fbpitch) fbpitch = fbwidth * img.bpp / 8;
184 num_xtiles = num_ytiles = 1;
188 num_xtiles = rect.w / tile_xsz;
189 num_ytiles = rect.h / tile_ysz;
194 printf(prefixfmt[asyntax], name, name, name, name, fbpitch);
195 for(i=0; i<num_ytiles*num_xtiles; i++) {
196 if(asyntax == AS_GNU) {
197 printf("\t.long tile%d\n", i);
199 printf("\tdd tile%d\n", i);
205 for(i=0; i<num_ytiles; i++) {
206 for(j=0; j<num_xtiles; j++) {
207 printf("tile%d:\n", tidx++);
208 csprite(&img, rect.x + j * xsz, rect.y + i * ysz, xsz, ysz);
224 unsigned char op, val;
228 int csprite(struct image *img, int x, int y, int xsz, int ysz)
230 int i, j, numops, mode, new_mode, start, skip_acc, lenbytes, pixsz = img->bpp / 8;
231 unsigned char *pptr = img->pixels + y * img->scansz + x * pixsz;
232 struct csop *ops, *optr;
234 ops = optr = alloca((xsz + 1) * ysz * sizeof *ops);
236 for(i=0; i<ysz; i++) {
241 optr++->op = CSOP_ENDL;
244 for(j=0; j<xsz; j++) {
245 if(memcmp(pptr, &ckey, pixsz) == 0) {
246 new_mode = CSOP_SKIP;
248 new_mode = CSOP_COPY;
251 if(new_mode != mode) {
255 optr->len = j - start;
263 pptr += img->scansz - xsz * pixsz;
268 optr->len = xsz - start;
274 pptr = img->pixels + y * img->scansz + x * img->bpp / 8;
277 /* edx points to dest */
278 for(i=0; i<numops; i++) {
281 skip_acc += optr->len;
282 pptr += optr->len * pixsz;
286 skip_acc += fbwidth - xsz;
287 pptr += img->scansz - xsz * pixsz;
292 if(asyntax == AS_GNU) {
293 printf("\tadd $%d, %%edx\n", skip_acc * pixsz);
295 printf("\tadd edx, %d\n", skip_acc * pixsz);
300 lenbytes = optr->len * pixsz;
301 for(j=0; j<lenbytes / 4; j++) {
302 if(asyntax == AS_GNU) {
303 printf("\tmovl $0x%x, %d(%%edx)\n", *(uint32_t*)pptr, j * 4);
305 printf("\tmov dword [edx + %d], 0x%x\n", j * 4, *(uint32_t*)pptr);
310 switch(lenbytes % 4) {
312 if(asyntax == AS_GNU) {
313 printf("\tmovb $0x%x, %d(%%edx)\n", (unsigned int)*pptr++, j++);
315 printf("\tmov byte [edx + %d], 0x%x\n", j++, (unsigned int)*pptr++);
318 if(asyntax == AS_GNU) {
319 printf("\tmovw $0x%x, %d(%%edx)\n", (unsigned int)*(uint16_t*)pptr, j);
321 printf("\tmov word [edx + %d], 0x%x\n", j, (unsigned int)*(uint16_t*)pptr);
327 if(asyntax == AS_GNU) {
328 printf("\tmovb $0x%x, %d(%%edx)\n", (unsigned int)*pptr++, j++);
330 printf("\tmov byte [edx + %d], 0x%x\n", j++, (unsigned int)*pptr++);
335 skip_acc = optr->len;
339 printf("\t%c invalid op: %d\n", asyntax == AS_GNU ? '#' : ';', optr->op);
348 void print_usage(const char *argv0)
350 printf("Usage: %s [options] <spritesheet>\n", argv0);
351 printf("Options:\n");
352 printf(" -s,-size <WxH>: tile size (default: whole image)\n");
353 printf(" -r,-rect <WxH+X+Y>: use rectangle of the input image (default: whole image)\n");
354 printf(" -coffset <offs>: colormap offset [0, 255] (default: 0)\n");
355 printf(" -fbpitch <pitch>: target framebuffer pitch (scanline size in bytes)\n");
356 printf(" -k,-key <color>: color-key for transparency (default: 0)\n");
357 printf(" -conv565: convert image to 16bpp 565 before processing\n");
358 printf(" -gas: output GNU assembler code (default)\n");
359 printf(" -nasm: output NASM-compatible code\n");
360 printf(" -h: print usage and exit\n");