home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / utils / fbm2fl03.lha / fppmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-16  |  6.6 KB  |  302 lines

  1. /****************************************************************
  2.  * fppmain.c
  3.  ****************************************************************/
  4.  
  5. /******
  6.   Copyright (C) 1993 by Klaus Ehrenfried. 
  7.  
  8.   Permission to use, copy, modify, and distribute this software
  9.   is hereby granted, provided that the above copyright notice appears 
  10.   in all copies and that the software is available to all free of charge. 
  11.   The author disclaims all warranties with regard to this software, 
  12.   including all implied warranties of merchant-ability and fitness. 
  13.   The code is simply distributed as it is.
  14. *******/
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18.  
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21.  
  22. #include "fpfli.h"
  23.  
  24. static int fli_file_created;
  25. static char *fliname;
  26.  
  27. /****************************************************************
  28.  * print_usage
  29.  ****************************************************************/
  30.  
  31. static int print_usage()
  32. {
  33.   fprintf(stderr,"Usage:\n");
  34.   fprintf(stderr,"  fbm2fli [options] list-file fli-file\n");
  35.   fprintf(stderr,"Valid options are:\n");
  36.   fprintf(stderr,"  -b<color>    Define border color (default = 0)\n");
  37.   fprintf(stderr,"  -D           Enable double buffer mode\n");
  38.   fprintf(stderr,"  -m<file>     Use the color table from this file\n");
  39.   fprintf(stderr,"  -O           Generate old format FLI\n");
  40.   fprintf(stderr,"  -ox<x>       Set x position - no automatic adjustment\n");
  41.   fprintf(stderr,"  -oy<y>       Set y position - no automatic adjustment\n");
  42.   fprintf(stderr,"  -rx<width>   Horizontal resolution\n");
  43.   fprintf(stderr,"  -ry<height>  Vertical resolution\n");
  44.   fprintf(stderr,"  -s<speed>    Default animation speed\n");
  45.  
  46.   return(1);
  47. }
  48.  
  49. /****************************************************************
  50.  * exitialise
  51.  ****************************************************************/
  52.  
  53. int exitialise(int error_flag)
  54. {
  55.     if (big_buffer != NULL) free(big_buffer);
  56.     if (pixel_chunk_buffer != NULL) free(pixel_chunk_buffer);
  57.  
  58.     if (output != NULL) fclose(output);
  59.     if (input != NULL) fclose(input);
  60.  
  61.     if (error_flag != 0)
  62.     {
  63.     if (fli_file_created == 1)
  64.     {
  65.         fprintf(stderr,"Remove fli-file %s\n",fliname);
  66.         (void) remove(fliname);
  67.     }
  68.     fprintf(stderr,"abnormal termination\n");
  69.     }
  70.     return(1);
  71. }
  72.  
  73. /****************************************************************
  74.  * main
  75.  ****************************************************************/
  76.  
  77. void
  78. main (int argc, char *argv[])
  79. {
  80.   FILE *fopen();
  81.   char *listfile, *ppa, *map_file;
  82.   struct stat statbuf;
  83.   char abuff[10];
  84.   int error_flag, answer_flag, big_size, max_chunk_size;
  85.   int x_resolution, y_resolution, s_speed;
  86.   int i, itest;
  87.  
  88.   listfile=NULL;
  89.   fliname=NULL;
  90.   map_file=NULL;
  91.   input=0;
  92.   output=0;
  93.  
  94.   fli_file_created=0;
  95.  
  96.   big_buffer=NULL;
  97.   pixel_chunk_buffer=NULL;
  98.  
  99.   error_flag=0;
  100.  
  101.   Xorigin=0;
  102.   Xorigin_flag=0;
  103.   Yorigin=0;
  104.   Yorigin_flag=0;
  105.  
  106.   x_resolution=-1;
  107.   y_resolution=-1;
  108.   s_speed=-1;
  109.  
  110.   border_color=0;
  111.   map_color_flag=0;
  112.   double_buffer=0;
  113.   old_format_flag=0;
  114.  
  115.   for (i=1; i < argc; i++)
  116.     {
  117.       ppa=argv[i];    
  118.       if (ppa[0] == '-')
  119.     {
  120.       switch (ppa[1])
  121.         {
  122.         case 'b':
  123.           border_color = atoi(&ppa[2]);
  124.           break;
  125.         case 'D':
  126.           double_buffer=1;
  127.           break;
  128.         case 'm':
  129.           map_file = &ppa[2];
  130.           map_color_flag=1;
  131.           break;
  132.         case 'O':
  133.           old_format_flag=1;
  134.           break;
  135.         case 'o':
  136.           if (ppa[2] == 'x')
  137.         {
  138.           Xorigin = atoi(&ppa[3]);
  139.           Xorigin_flag = 1;
  140.         }
  141.           else if (ppa[2] == 'y')
  142.         {
  143.           Yorigin = atoi(&ppa[3]);
  144.           Yorigin_flag = 1;
  145.         }
  146.           else
  147.         goto invalid_option;
  148.           break;
  149.         case 'r':
  150.           if (ppa[2] == 'x')
  151.         x_resolution = atoi(&ppa[3]);
  152.           else if (ppa[2] == 'y')
  153.         y_resolution = atoi(&ppa[3]);
  154.           else
  155.         goto invalid_option;
  156.           break;
  157.         case 's':
  158.           s_speed = atoi(&ppa[2]);
  159.           break;
  160.         invalid_option:
  161.         default:
  162.           fprintf(stderr,"Invalid option: %s\n",ppa);
  163.           exit(1);
  164.         }
  165.     }
  166.     }
  167.  
  168.     for (i=1; i < argc; i++)
  169.       {
  170.     ppa=argv[i];    
  171.     if (ppa[0] != '-')
  172.       {
  173.         if (listfile == NULL)
  174.           {
  175.         listfile=argv[i];
  176.           }
  177.         else if (fliname == NULL)
  178.           {
  179.         fliname=argv[i];
  180.           }
  181.         else
  182.           {
  183.         print_usage();
  184.         exit(1);
  185.           }
  186.       }
  187.       }
  188.  
  189.     if ((listfile == NULL) || (fliname == NULL))
  190.       {
  191.     print_usage();
  192.     exit(1);
  193.       }
  194.  
  195.     if (old_format_flag == 1)
  196.       {
  197.     fli_width=320;
  198.     fli_height=200;
  199.     fli_speed=5;
  200.       }
  201.     else
  202.       {
  203.     fli_width=640;
  204.     fli_height=480;
  205.     fli_speed=72;
  206.       }
  207.  
  208.     if (x_resolution >= 10)
  209.       {
  210.     fli_width=x_resolution;
  211.     if ((fli_width % 2) == 1) fli_width++;
  212.     if (fli_width > FLI_MAX_X) fli_width=FLI_MAX_X;
  213.       }
  214.  
  215.     if (y_resolution >= 10)
  216.       {
  217.     fli_height=y_resolution;
  218.     if (fli_height > FLI_MAX_Y) fli_height=FLI_MAX_Y;
  219.       }
  220.  
  221.     fli_size = fli_width * fli_height;
  222.  
  223.     if (s_speed >= 0)
  224.       fli_speed = s_speed;
  225.  
  226.     if (map_color_flag == 1)
  227.     {
  228.     fprintf(stdout,"Take color table from file %s\n",map_file);
  229.     get_image(map_file, (UBYTE *)0, map_color, 1);
  230.     }
  231.  
  232.     if ((input = fopen(listfile, "r")) == NULL)
  233.     {
  234.         fprintf(stderr,"Error opening list-file %s\n",listfile);
  235.     exit(1);
  236.     }
  237.  
  238.     if (stat(fliname, &statbuf) == 0)
  239.     {
  240.         fprintf(stderr,"%s already exists\n",fliname);
  241.     answer_flag=0;
  242.     while (answer_flag == 0)
  243.     {
  244.         fprintf(stdout," overwrite %s (y/n) ",fliname);
  245.         fflush(stdout);
  246.         if ((itest=get_next_line(stdin, abuff, 2)) == 1)
  247.         {
  248.         if ((abuff[0] == 'y') || (abuff[0] == 'Y'))
  249.             answer_flag=1;
  250.         else if ((abuff[0] == 'n') || (abuff[0] == 'N'))
  251.             answer_flag=2;
  252.         }
  253.         /* printf(" %d :: %d %d %d %d\n",
  254.         itest, abuff[0], abuff[1], abuff[2], abuff[3]); */
  255.     }
  256.     if (answer_flag != 1)
  257.     {
  258.         exitialise(1);
  259.         exit(1);
  260.     }
  261.     }
  262.  
  263.     if ((output = fopen(fliname, "wb")) == NULL)
  264.     {
  265.         fprintf(stderr,"Error opening fli-file %s\n",fliname);
  266.     exitialise(1);
  267.     exit(1);
  268.     }
  269.     fli_file_created = 1;
  270.  
  271.     if (border_color > 0x00FF) border_color=0x00FF;
  272.     if (border_color < 0x0000) border_color=0x0000;
  273.  
  274.     fprintf(stdout,"Resolution: %dx%d\n",fli_width,fli_height);
  275.     fprintf(stdout,"Origin:     %dx%d\n",Yorigin,Xorigin);
  276.     fprintf(stdout,"Speed:      %d\n",fli_speed);
  277.  
  278.     max_chunk_size=fli_height*(2*fli_width+10)+1024;
  279.  
  280.     pixel_chunk_buffer = malloc(max_chunk_size);
  281.     if (pixel_chunk_buffer == NULL)
  282.     {
  283.         fprintf(stderr,"Error: cannot allocate %d bytes\n",max_chunk_size);
  284.     exitialise(1);
  285.     exit(1);
  286.     }
  287.  
  288.     big_size = 5 * fli_size;
  289.     big_buffer = malloc(big_size);
  290.     if (big_buffer == NULL)
  291.     {
  292.         fprintf(stderr,"Error: cannot allocate %d bytes\n",big_size);
  293.     exitialise(1);
  294.     exit(1);
  295.     }
  296.  
  297.     make_fli();
  298.  
  299.     exitialise(0);
  300.     exit(0);
  301. }
  302.