home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
-
- /*
- * ppmtppi1.c - Reads a portable pixmap (ppm) file on stdin and
- * writes a Degas PI1 file on stdout.
- *
- * Copyright (C) 1990, Steve Belczyk
- */
-
- char *progname;
-
- int pal[16][3]; /* Degas palette */
-
- /* This is the ST's video RAM */
- short screen[16000];
-
- /* RAWBITS flag */
- int rawbits;
-
- /* Picture resolution */
- int xres, yres;
-
- main (argc, argv)
- int argc;
- char *argv[];
- {
- char magic[10];
- int i, maxpix;
-
- progname = argv[0];
-
- /* Check for bogus arguments */
- if (argc != 1)
- {
- fprintf (stderr, "usage: %s <ppmfile >pi1file\n",
- progname);
- exit (-1);
- }
-
- /* Read ppm header, validate fields */
- GetPPMHeader (magic, &xres, &yres, &maxpix);
-
- if ( (magic[0] != 'P') && (magic[0] != 'p') )
- {
- fprintf (stderr, "Not a ppm file.\n");
- exit (-1);
- }
- if (magic[1] == '3')
- {
- rawbits = 0;
- }
- else if (magic[1] == '6')
- {
- rawbits = 1;
- }
- else
- {
- fprintf (stderr, "%s: Not a ppm file.\n", progname);
- exit (-1);
- }
-
- if ( (xres > 320) || (yres > 200) )
- {
- fprintf (stderr,
- "%s: Resolution greater than 320x200. Sorry.\n",
- progname);
- exit (-1);
- }
-
- /* Clear the bitmap */
- for (i=0; i<16000; screen[i++]=0);
-
- /* Read the PPM data */
- ReadPPM();
-
- /* Write the PI1 file */
- WritePI1();
- }
-
- WritePI1 ()
- {
- int i, w;
-
- PutWord (0); /* Low resolution */
-
- /* Write palette */
- for (i=0; i<16; i++)
- {
- w = (0xe0 & pal[i][0]) << 3;
- w |= (0xe0 & pal[i][1]) >> 1;
- w |= (0xe0 & pal[i][2]) >> 5;
- PutWord (w);
- }
-
- /* Write video RAM */
- WriteScreen();
- }
-
- SetPixel (x, y, c)
- int x, y, c;
- {
- int index, bit, plane;
-
- /* In the next few statements, the bit operations are a little
- quicker, but the arithmetic versions are easier to read and
- maybe more portable. Please try swapping them if you have
- trouble on your machine. */
-
- /* index = (80 * y) + 4 * (x / 16); */
- index = (y << 6) + (y << 4) + ((x >> 4) << 2);
-
- /* bit = 0x8000 >> (x % 16); */
- bit = 0x8000 >> (x & 0x0f);
-
- for (plane=0; plane<4; plane++)
- {
- if (c & (1 << plane))
- {
- screen[index+plane] |= bit;
- }
- }
- }
-
- WriteScreen ()
- {
- int i;
-
- for (i=0; i<16000; i++)
- {
- PutWord (screen[i]);
- }
- }
-
- GetString (s)
- char *s;
- {
- int i;
- char c;
-
- /* Skip leading white space */
- do
- {
- c = GetChar();
- } while ( (c == ' ') || (c == '\t') || (c == '\n') );
-
- /* Build string */
- i = 0;
- do
- {
- s[i++] = c;
- if (i > 8) break;
- c = GetChar();
- } while ( (c != ' ') && (c != '\t') && (c != '\n') );
-
- s[i] = 0;
- return;
- }
-
- int GetChar()
- {
- int c;
-
- c = getchar();
-
- if (c == EOF)
- {
- fprintf (stderr, "%s: Premature EOF.\n", progname);
- exit (-1);
- }
-
- do
- {
- if (c == '#') /* Comment character */
- {
- do /* Skip to end-of-line */
- {
- c = getchar();
- if (c == EOF)
- {
- fprintf (stderr,
- "%s: Premature EOF.\n",
- progname);
- exit (-1);
- }
- } while (c != '\n');
-
- c = getchar();
- }
- } while (c == '#'); /* In case there's another comment */
-
- return (c);
- }
-
- GetPPMHeader (magic, xres, yres, maxpix)
- char *magic;
- int *xres, *yres, *maxpix;
- {
- char s[10];
-
- GetString (magic);
-
- GetString (s);
- *xres = atoi(s);
-
- GetString (s);
- *yres = atoi(s);
-
- GetString (s);
- *maxpix = atoi(s);
- }
-
- ReadPPM ()
- {
- int x, y, n, r, g, b, i;
-
- n = 0; /* counts colors */
-
- for (y=0; y<yres; y++)
- {
- for (x=0; x<xres; x++)
- {
- if (rawbits)
- {
- r = 0xff & getchar();
- g = 0xff & getchar();
- b = 0xff & getchar();
- }
- else
- {
- scanf ("%d %d %d", &r, &g, &b);
- }
- i = FindColor (&n, r, g, b);
- SetPixel (x, y, i);
- }
- }
- }
-
- int FindColor (n, r, g, b)
- int *n, r, g, b;
- {
- int i;
-
- r = 0xe0 & r;
- g = 0xe0 & g;
- b = 0xe0 & b;
-
- for (i=0; i<(*n); i++)
- {
- if ( (r == pal[i][0]) &&
- (g == pal[i][1]) &&
- (b == pal[i][2]) )
- {
- return (i);
- }
- }
-
- if ((*n) > 15)
- {
- fprintf (stderr, "%s: More than 16 colors.\n", progname);
- exit (-1);
- }
-
- pal[*n][0] = r;
- pal[*n][1] = g;
- pal[*n][2] = b;
-
- i = (*n);
- (*n)++;
-
- return (i);
- }
-
- PutWord (w)
- int w;
- {
- char c0, c1;
-
- c0 = 0xff & (w >> 8);
- c1 = 0xff & w;
-
- putchar (c0);
- putchar (c1);
- }
-