home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume14 / ataritoppm / part01 / pi1toppm.c next >
Encoding:
C/C++ Source or Header  |  1990-07-15  |  2.0 KB  |  133 lines

  1. #include <stdio.h>
  2.  
  3. /*
  4.  *  pi1toppm.c - Reads a Degas PI1 file on stdin and
  5.  *  writes a portable pixmap (ppm) file on stdout.
  6.  *
  7.  *  Copyright (C) 1990, Steve Belczyk
  8.  */
  9.  
  10. /* Remove the following definition if you don't want raw ppm files */
  11. #define RAWBITS
  12.  
  13. char *progname;
  14.  
  15. int pal[16][3];            /* Degas palettes */
  16. short screen[16000];        /* Simulates the Atari's video RAM */
  17.  
  18. main (argc, argv)
  19. int argc;
  20. char *argv[];
  21. {
  22.     int x, y;
  23.  
  24.     progname = argv[0];
  25.  
  26.     /* Check for bogus arguments */
  27.     if (argc > 1)
  28.     {
  29.         fprintf (stderr, "usage: %s <pi1file >ppmfile\n",
  30.              progname);
  31.         exit (-1);
  32.     }
  33.  
  34.     /* Read the PI1 file */
  35.     ReadPI1();
  36.  
  37.     /* Write PPM header */
  38. #ifdef RAWBITS
  39.     printf ("P6\n320 200\n255\n"); /* Magic, resolution, max pixel */
  40. #else
  41.     printf ("P3\n320 200\n255\n");
  42. #endif
  43.  
  44.     /* Convert and write */
  45.     for (y=0; y<200; y++)
  46.     {
  47.         for (x=0; x<320; x++)
  48.         {
  49.             DoPixel (x, y);
  50.         }
  51.     }
  52. }
  53.  
  54. ReadPI1 ()
  55. {
  56.     int i, j;
  57.  
  58.     /* Check resolution word */
  59.     i = GetWord();
  60.     if (i != 0)
  61.     {
  62.         fprintf (stderr, "%s: Not a PI1 file\n", progname);
  63.         exit (-1);
  64.     }
  65.  
  66.     /* Read the palette */
  67.     for (i=0; i<16; i++)
  68.     {
  69.         j = GetWord();
  70.         pal[i][0] = (0x700 & j) >> 3;
  71.         pal[i][1] = (0x070 & j) << 1;
  72.         pal[i][2] = (0x007 & j) << 5;
  73.     }
  74.  
  75.     /* Read the screen data */
  76.     for (i=0; i<16000; i++)
  77.     {
  78.         screen[i] = GetWord();
  79.     }
  80. }
  81.  
  82. int GetWord()
  83. {
  84.     int w;
  85.  
  86.     w  = (0xff & getchar()) << 8;
  87.     w |=  0xff & getchar();
  88.  
  89.     return (w);
  90. }
  91.  
  92. DoPixel (x, y)
  93. int x, y;
  94. {
  95.     int c;
  96.  
  97.     c = GetPixel (x, y);
  98.  
  99. #ifdef RAWBITS
  100.     putchar (0xff & pal[c][0]);
  101.     putchar (0xff & pal[c][1]);
  102.     putchar (0xff & pal[c][2]);
  103. #else
  104.     printf ("%d %d %d\n", (0xff & pal[c][0]),
  105.                   (0xff & pal[c][1]),
  106.                   (0xff & pal[c][2]));
  107. #endif
  108. }
  109.  
  110. int GetPixel (x, y)
  111. int x, y;
  112. {
  113.     int c, index, bit, plane;
  114.  
  115.     c = 0;
  116.  
  117. /*    index = (80*y) + 4*(x/16);    */
  118.     index = (y << 6) + (y << 4) + ((x >> 4) << 2);
  119.  
  120. /*    bit = 0x8000 >> (x % 16);    */
  121.     bit = 0x8000 >> (x & 0x0f);
  122.  
  123.     for (plane=0; plane<4; plane++)
  124.     {
  125.         if (bit & screen[index+plane])
  126.         {
  127.             c |= (1 << plane);
  128.         }
  129.     }
  130.     return (c);
  131. }
  132.  
  133.