Skip to content

Instantly share code, notes, and snippets.

@kdrnic
Created April 10, 2018 03:14
Show Gist options
  • Select an option

  • Save kdrnic/a393b67cded4a65c4c92d5a77f933fcc to your computer and use it in GitHub Desktop.

Select an option

Save kdrnic/a393b67cded4a65c4c92d5a77f933fcc to your computer and use it in GitHub Desktop.
#include <allegro.h>
#include <stdio.h>
#include <string.h>
void usage()
{
printf("bitmap conversion tool for truecolor to paletted\nusage:\nbmpconv in <directory>\nbmpconv out <directory>");
}
int w = 0, h = 0, x = 0;
PALETTE pal;
int callback1(const char *fn, int attrib, void *param)
{
char *ext = get_extension(fn);
BITMAP *bmp;
if(strcmp(ext, "bmp") && strcmp(ext, "pcx")) return 0;
bmp = load_bitmap(fn, pal);
w += bmp->w;
if(bmp->h > h) h = bmp->h;
destroy_bitmap(bmp);
return 0;
}
BITMAP *res;
FILE *txt;
int callback2(const char *fn, int attrib, void *param)
{
char *ext = get_extension(fn);
BITMAP *bmp;
if(strcmp(ext, "bmp") && strcmp(ext, "pcx")) return 0;
bmp = load_bitmap(fn, pal);
blit(bmp, res, 0, 0, x, 0, bmp->w, bmp->h);
fprintf(txt, "%s %d %d %d\n", get_filename(fn), x, bmp->w, bmp->h);
x += bmp->w;
destroy_bitmap(bmp);
printf("processed \"%s\"\n", fn);
return 0;
}
int main(int argc, char **argv)
{
int in;
int out;
char wildc[1024];
if(argc != 3)
{
usage();
return 1;
}
in = strcmp(argv[1], "in") == 0;
out = strcmp(argv[1], "out") == 0;
if(!(in || out))
{
usage();
return 1;
}
allegro_init();
if(in) set_color_depth(32);
else if(out) set_color_depth(8);
printf("%d %d\n", in, out);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
strcpy(wildc, argv[2]);
strcat(wildc, "\\");
fix_filename_slashes(wildc);
if(in)
{
strcat(wildc, "*");
for_each_file_ex(wildc, FA_NONE, 0, &callback1, 0);
res = create_bitmap(w, h);
txt = fopen("tmp.txt", "w");
for_each_file_ex(wildc, FA_NONE, 0, &callback2, 0);
fclose(txt);
save_bitmap("tmp.bmp", res, 0);
allegro_exit();
}
if(out)
{
char the_fn[512];
int x, w, h;
txt = fopen("tmp.txt", "r");
res = load_bitmap("tmp.bmp", pal);
if((!res) || (!txt))
{
printf("can't load files\n");
return 1;
}
while(fscanf(txt, "%s %d %d %d", the_fn, &x, &w, &h) == 4)
{
char fn[1024];
BITMAP *bmp = create_bitmap(w, h);
blit(res, bmp, x, 0, 0, 0, w, h);
strcpy(fn, wildc);
strcat(fn, the_fn);
fix_filename_slashes(fn);
save_bitmap(fn, bmp, pal);
printf("processed \"%s\"\n", fn);
}
fclose(txt);
allegro_exit();
}
return 0;
}
END_OF_MAIN()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment