home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume14 / ataritoppm / part01 / sputoppm.c < prev   
Encoding:
C/C++ Source or Header  |  1990-07-15  |  2.1 KB  |  150 lines

  1. #include <stdio.h>
  2.  
  3. /*
  4.  *  sputoppm.c - Reads an uncompressed Spectrum 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. int pal[200][48];        /* Spectrum palettes, three per row */
  14. short screen[16000];        /* Simulates the Atari's video RAM */
  15.  
  16. main (argc, argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.     int x, y;
  21.  
  22.     /* Check for bogus arguments */
  23.     if (argc > 1)
  24.     {
  25.         fprintf (stderr, "usage: %s <spufile >ppmfile\n",
  26.              argv[0]);
  27.         exit (-1);
  28.     }
  29.  
  30.     /* Read the SPU file */
  31.     ReadSPU();
  32.  
  33.     /* Write PPM header */
  34. #ifdef RAWBITS
  35.     printf ("P6\n320 200\n255\n"); /* Magic, resolution, max pixel */
  36. #else
  37.     printf ("P3\n320 200\n255\n");
  38. #endif
  39.  
  40.     /* Convert and write */
  41.     for (y=0; y<200; y++)
  42.     {
  43.         for (x=0; x<320; x++)
  44.         {
  45.             DoPixel (x, y);
  46.         }
  47.     }
  48. }
  49.  
  50. ReadSPU ()
  51. {
  52.     int i, j;
  53.  
  54.     /* Read the screen data */
  55.     for (i=0; i<16000; i++)
  56.     {
  57.         screen[i] = GetWord();
  58.     }
  59.  
  60.     /* Read the palettes */
  61.     for (i=1; i<200; i++)
  62.     {
  63.         for (j=0; j<48; j++)
  64.         {
  65.             pal[i][j] = GetWord();
  66.         }
  67.     }
  68.  
  69.     /* Clear the first palette line */
  70.     for (i=0; i<48; pal[0][i++]=0);
  71. }
  72.  
  73. int GetWord()
  74. {
  75.     int w;
  76.  
  77.     w  = (0xff & getchar()) << 8;
  78.     w |=  0xff & getchar();
  79.  
  80.     return (w);
  81. }
  82.  
  83. DoPixel (x, y)
  84. int x, y;
  85. {
  86.     int c, x1;
  87.  
  88.     c = GetPixel (x, y);
  89.  
  90.     /* Compute palette index */
  91.     x1 = 10 * c;
  92.  
  93.     if (1 & c)
  94.     {
  95.         x1 -= 5;
  96.     }
  97.     else
  98.     {
  99.         x1++;
  100.     }
  101.  
  102.     if ( (x >= x1) && (x < (x1+160)) ) c += 16;
  103.     if (x >= (x1+160)) c += 32;
  104.  
  105.     /* Write the proper color */
  106.     DoColor (pal[y][c]);
  107. }
  108.  
  109. DoColor (p)
  110. int p;
  111. {
  112.     int r, g, b;
  113.  
  114.     r = (0x700 & p) >> 3;
  115.     g = (0x070 & p) << 1;
  116.     b = (0x007 & p) << 5;
  117.  
  118. #ifdef RAWBITS
  119.     putchar (0xff & r);
  120.     putchar (0xff & g);
  121.     putchar (0xff & b);
  122. #else
  123.     printf ("%d %d %d\n", r, g, b);
  124. #endif
  125. }
  126.  
  127. int GetPixel (x, y)
  128. int x, y;
  129. {
  130.     int c, index, bit, plane;
  131.  
  132.     c = 0;
  133.  
  134. /*    index = (80*y) + 4*(x/16);    */
  135.     index = (y << 6) + (y << 4) + ((x >> 4) << 2);
  136.  
  137. /*    bit = 0x8000 >> (x % 16);    */
  138.     bit = 0x8000 >> (x & 0x0f);
  139.  
  140.     for (plane=0; plane<4; plane++)
  141.     {
  142.         if (bit & screen[index+plane])
  143.         {
  144.             c |= (1 << plane);
  145.         }
  146.     }
  147.     return (c);
  148. }
  149.  
  150.