home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 334.lha / DkbAnim / DumpToIFF.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-10  |  5.5 KB  |  269 lines

  1. #include <stdio.h>
  2. #include <exec/types.h>
  3. #include <intuition/intuition.h>
  4. #include <graphics/display.h>
  5. #include <iff/iff.h>
  6. #include <libraries/dos.h>
  7.  
  8. #define INT_REV 29L
  9. #define GR_REV 29L
  10.  
  11. struct IntuitionBase *IntuitionBase, *OpenLibrary();
  12. struct GfxBase *GfxBase;
  13. struct Screen *s, *OpenScreen();
  14. struct IntuiMessage *GetMsg();
  15.  
  16. #define SCREEN_WIDTH 320L
  17. #define SCREEN_HEIGHT 400L
  18. #define BUFFER_SIZE 8096
  19.  
  20. struct NewScreen MyScreen =
  21.   {
  22.   0, 0,
  23.   SCREEN_WIDTH, SCREEN_HEIGHT,
  24.   6,
  25.   0, 1,
  26.   INTERLACE | HAM,
  27.   0,
  28.   NULL,
  29.   (UBYTE *) "Show screen",
  30.   NULL,
  31.   NULL
  32.   };
  33.  
  34. UWORD ColourTbl[16] = { 0x000, 0x111, 0x222, 0x333, 0x444, 0x555, 0x666,
  35.                        0x777, 0x888, 0x999, 0xaaa, 0xbbb, 0xccc, 0xddd,
  36.                        0xeee, 0xfff };
  37.  
  38. LONG last_y = -1;
  39.  
  40. extern int Close_Threshold;
  41.  
  42. FILE *fp, *palette_file;
  43.  
  44. main (argc, argv) 
  45.  int argc;
  46.  char **argv;
  47.  {
  48.  unsigned int red, green, blue, i;
  49.  struct IntuiMessage *message;
  50.  unsigned int width, height, x, y, line_number;
  51.  unsigned char red_buffer[SCREEN_WIDTH], green_buffer[SCREEN_WIDTH],
  52.       blue_buffer[SCREEN_WIDTH];
  53.  
  54.  if ((argc < 2) || (argc > 4))
  55.    {
  56.    printf ("\nUsage:  DumpToIFF <dump_filename> <iff_file_name> [<palette_filename>]\n");
  57.    exit(0);
  58.    }
  59.  
  60.  Close_Threshold = 5;
  61.  
  62.  fp = NULL;
  63.  
  64.  IntuitionBase = (struct IntuitionBase *)
  65.                   OpenLibrary ("intuition.library",INT_REV);
  66.  if (IntuitionBase == NULL)
  67.    exit(FALSE);
  68.  
  69.  GfxBase = (struct GfxBase *)
  70.                       OpenLibrary ("graphics.library", GR_REV);
  71.  if (GfxBase == NULL)
  72.    exit(FALSE);
  73.  
  74.  if ((s = (struct Screen *) OpenScreen (&MyScreen))
  75.           == NULL)
  76.   exit (FALSE);
  77.  
  78.  ShowTitle (s, FALSE);
  79.  
  80.  
  81.  LoadRGB4 (&(s->ViewPort), ColourTbl, 16L);
  82.  SetAPen (&(s->RastPort), 7L);
  83.  RectFill (&(s -> RastPort), 0L, 0L, SCREEN_WIDTH-1, SCREEN_HEIGHT-1);
  84.  
  85.  if ((fp = fopen (argv[1], "r")) == NULL)
  86.    {
  87.    display_close();
  88.    exit(FALSE);
  89.    }
  90.  
  91.  width = (unsigned int) (getc(fp) & 0xFF);
  92.  width += ((unsigned int) (getc(fp) & 0xFF) * 256);
  93.  height = (unsigned int) (getc(fp) & 0xFF);
  94.  height += ((unsigned int) (getc(fp) & 0xFF) * 256);
  95.  
  96.  if ((width > 320) || (height > 400)) {
  97.     display_close();
  98.     printf ("\nError - picture too large\n");
  99.     exit (0);
  100.     }
  101.  
  102.  printf ("Processing...\n");
  103.  
  104.  if (argc == 4) {
  105.     if ((palette_file = fopen (argv[3], "r")) == NULL) {
  106.       display_close();
  107.       exit(FALSE);
  108.       }
  109.  
  110.    for (i = 0 ; i < 16 ; i++) {
  111.      if (fscanf (palette_file, "%d %d %d", &red, &green, &blue) != 3) {
  112.         printf ("Error reading palette file\n");
  113.         exit (1);
  114.         }
  115.      ColourTbl[i] = ((red & 0x0f) << 4) |
  116.                     ((green & 0x0f) << 4) | (blue & 0x0f);
  117.      }
  118.    }    
  119.  else {
  120.     start_recording_colours();
  121.  
  122.     for (y = 0 ; y < height ; y++) {
  123.       /* Skip over the line number.  It's not important at this point */
  124.       getc(fp);
  125.       getc(fp);
  126.  
  127.       for (x = 0 ; x < width ; x++)
  128.         red_buffer[x] = getc(fp);
  129.  
  130.       for (x = 0 ; x < width ; x++)
  131.         green_buffer[x] = getc(fp);
  132.  
  133.       for (x = 0 ; x < width ; x++)
  134.         blue_buffer[x] = getc(fp);
  135.  
  136.       for (x = 0 ; x < width ; x++)
  137.         {
  138.         red = red_buffer[x];
  139.         green = green_buffer[x];
  140.         blue = blue_buffer[x];
  141.         process (x, height - y, red, green, blue);
  142.         }
  143.       }
  144.     choose_palette();
  145.     }
  146.  
  147.  printf ("Displaying...\n");
  148.  
  149.  LoadRGB4 (&(s->ViewPort), ColourTbl, 16L);
  150.  fclose (fp);
  151.  
  152.  if ((fp = fopen (argv[1], "r")) == NULL)
  153.    {
  154.    display_close();
  155.    exit(FALSE);
  156.    }
  157.  
  158.  getc (fp);
  159.  getc (fp);
  160.  getc (fp);
  161.  getc (fp);
  162.  
  163.  for (y = 0 ; y < height ; y++) {
  164.    line_number = (unsigned int) (getc(fp) & 0xFF);
  165.    line_number += ((unsigned int) (getc(fp) & 0xFF) * 256);
  166.    for (x = 0 ; x < width ; x++)
  167.      red_buffer[x] = getc(fp);
  168.  
  169.    for (x = 0 ; x < width ; x++)
  170.      green_buffer[x] = getc(fp);
  171.  
  172.    for (x = 0 ; x < width ; x++)
  173.      blue_buffer[x] = getc(fp);
  174.  
  175.    for (x = 0 ; x < width ; x++)
  176.      {
  177.      red = red_buffer[x];
  178.      green = green_buffer[x];
  179.      blue = blue_buffer[x];
  180.      display_plot (x, line_number, red, green, blue);
  181.      }
  182.    }
  183.  
  184.  if (argc >= 3)
  185.    ConvertToIFF(argv[2]);
  186.  
  187.  printf ("Finished\n");
  188.  display_close();
  189.  }
  190.  
  191. display_close ()
  192.  {
  193.  if (fp != NULL)
  194.    fclose (fp);
  195.  CloseScreen (s);
  196.  CloseLibrary (GfxBase) ;
  197.  CloseLibrary (IntuitionBase) ;
  198. }
  199.  
  200. display_plot (x, y, new_red, new_green, new_blue)
  201.   LONG x, y, new_red, new_green, new_blue;
  202.   {
  203.   LONG colour, newline;
  204.  
  205.   new_red &= 0xFF;
  206.   new_green &= 0xFF;
  207.   new_blue &= 0xFF;
  208.  
  209.   new_red /= 16;
  210.   new_green /= 16;
  211.   new_blue /= 16;
  212.  
  213.   newline = 0;
  214.   if (last_y != y) {
  215.     newline = 1;
  216.     last_y = y;
  217.     reset_colours();
  218.     SetAPen (&(s -> RastPort), 0);
  219.     WritePixel (&(s -> RastPort), 0, y);
  220.     }
  221.  
  222.   colour = best_colour (new_red, new_blue, new_green);
  223.   SetAPen (&(s -> RastPort), colour);
  224.   WritePixel (&(s -> RastPort), x+1, y);
  225.   }
  226.  
  227.  
  228. process (x, y, new_red, new_green, new_blue)
  229.   LONG x, y, new_red, new_green, new_blue;
  230.   {
  231.   LONG newline;
  232.  
  233.   new_red &= 0xFF;
  234.   new_green &= 0xFF;
  235.   new_blue &= 0xFF;
  236.  
  237.   new_red /= 16;
  238.   new_green /= 16;
  239.   new_blue /= 16;
  240.  
  241.   newline = 0;
  242.   if (last_y != y) {
  243.     newline = 1;
  244.     last_y = y;
  245.     reset_colours();
  246.     }
  247.  
  248.   record_colours (new_red, new_green, new_blue);
  249.   }
  250.  
  251.  
  252. ConvertToIFF(file_name)
  253.    char *file_name;
  254.    {
  255.    char *buffer, *malloc();
  256.    BPTR file, Open();
  257.    BOOL PutPict();
  258.  
  259.    if ((file = Open (file_name, MODE_NEWFILE)) == 0) {
  260.       printf ("\nCannot open IFF file\n");
  261.       exit (0);
  262.       }
  263.  
  264.    buffer = malloc(BUFFER_SIZE);
  265.    if (PutPict (file, &(s->ViewPort), buffer, BUFFER_SIZE))
  266.       printf ("\nIFF write error\n");
  267.    Close (file);
  268.    }
  269.