apple2-converters/a2pic.c

204 lines
3.1 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
void
usage (char *s)
{
fprintf (stderr, "Usage: %s [file]\n", s);
}
void
process (FILE *f)
{
int maj;
int min;
1999-12-06 00:28:07 +00:00
int color = 0;
static int lines[] = {
0x000,
0x080,
0x100,
0x180,
0x200,
0x280,
0x300,
0x380,
0x028,
0x0a8,
0x128,
0x1a8,
0x228,
0x2a8,
0x328,
0x3a8,
0x050,
0x0d0,
0x150,
0x1d0,
0x250,
0x2d0,
0x350,
0x3d0
};
unsigned char buf[8192];
fread (buf, sizeof (unsigned char), 8192, f);
1999-12-06 00:28:07 +00:00
for (maj = 0; maj < 24; maj++) {
for (min = lines[maj]; min < lines[maj] + 8192; min += 1024) {
int x;
for (x = 0; x < 40; x++)
if ((buf[min + x] & 0x80) &&
(buf[min + x] != 0xff) &&
(buf[min + x] != 0x80)) {
color = 1;
goto colorknown;
}
}
}
colorknown:
printf ("/* XPM */\n");
printf ("static char *apple2[] = {\n");
1999-12-06 00:28:07 +00:00
if (color)
printf ("\"280 192 6 1\",\n");
else
printf ("\"280 192 2 1\",\n");
1999-11-08 21:17:14 +00:00
1999-12-06 00:28:07 +00:00
printf ("\"0 c #000000\",\n");
printf ("\"1 c #ffffff\",\n");
1999-11-08 21:17:14 +00:00
1999-12-06 00:28:07 +00:00
if (color) {
printf ("\"g c #40ff40\",\n");
printf ("\"p c #c000c0\",\n");
printf ("\"r c #ff4040\",\n");
printf ("\"b c #4040ff\",\n");
}
1999-11-08 21:17:14 +00:00
1999-12-06 00:28:07 +00:00
if (color)
fprintf (stderr, "(color)\n");
1999-11-08 21:17:14 +00:00
for (maj = 0; maj < 24; maj++) {
for (min = lines[maj]; min < lines[maj] + 8192; min += 1024) {
int x;
1999-11-08 21:17:14 +00:00
char bits[280];
printf ("\"");
1999-11-08 21:17:14 +00:00
for (x = 0; x < 280; x++) {
bits[x] = (buf[min + x/7] & (1 << (x%7))) != 0;
}
#define HIGH(x) (buf[min + (x)/7] & (1 << 7))
1999-12-06 00:28:07 +00:00
if (color && (bits[0] != bits[1])) {
1999-11-08 21:17:14 +00:00
if (HIGH (0)) {
if (bits[0])
1999-11-08 23:07:39 +00:00
printf ("b");
1999-11-08 21:17:14 +00:00
else
printf ("r");
} else {
if (bits[0])
1999-11-08 23:07:39 +00:00
printf ("p");
1999-11-08 21:17:14 +00:00
else
printf ("g");
}
} else {
if (bits[0])
printf ("1");
else
printf ("0");
}
1999-11-08 21:17:14 +00:00
for (x = 1; x < 279; x++) {
1999-12-06 00:28:07 +00:00
if (color &&
bits[x] != bits[x-1] &&
1999-11-08 21:17:14 +00:00
bits[x] != bits[x+1]) {
if (x % 2 == 0) {
if (HIGH (x)) {
if (bits[x])
1999-11-08 23:07:39 +00:00
printf ("b");
1999-11-08 21:17:14 +00:00
else
printf ("r");
} else {
if (bits[x])
1999-11-08 23:07:39 +00:00
printf ("p");
1999-11-08 21:17:14 +00:00
else
printf ("g");
}
} else {
if (HIGH (x)) {
if (bits[x])
1999-11-08 23:07:39 +00:00
printf ("r");
1999-11-08 21:17:14 +00:00
else
printf ("b");
} else {
if (bits[x])
1999-11-08 23:07:39 +00:00
printf ("g");
1999-11-08 21:17:14 +00:00
else
printf ("p");
}
}
} else {
if (bits[x])
printf ("1");
else
printf ("0");
}
}
1999-12-06 00:28:07 +00:00
if (color && (bits[278] != bits[279])) {
1999-11-08 21:17:14 +00:00
if (HIGH (279)) {
if (bits[279])
1999-11-08 23:07:39 +00:00
printf ("r");
1999-11-08 21:17:14 +00:00
else
printf ("b");
} else {
if (bits[279])
1999-11-08 23:07:39 +00:00
printf ("g");
1999-11-08 21:17:14 +00:00
else
printf ("p");
}
} else {
if (bits[279])
printf ("1");
else
printf ("0");
}
printf ("\",\n");
}
}
printf ("};\n");
}
int
main (int argc, char **argv)
{
if (argc < 2) {
process (stdin);
} else if (argc == 2) {
FILE *f = fopen (argv[1], "r");
if (f) {
process (f);
fclose (f);
} else {
fprintf (stderr, "%s: %s: %s\n", argv[0], argv[1],
strerror (errno));
exit (EXIT_FAILURE);
}
} else {
usage (argv[0]);
exit (EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}