home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / msdos / utils / fbm2fl03.lha / fppmake.c < prev    next >
C/C++ Source or Header  |  1993-10-05  |  6KB  |  262 lines

  1. /****************************************************************
  2.  * fppmake.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. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <memory.h>
  21. #include "fpfli.h"
  22.  
  23. #define MAXLEN 512
  24.  
  25. #define SEEK_SET 0
  26.  
  27. unsigned char *pixel_chunk;
  28.  
  29. static unsigned char file_header[FLI_FILE_HEADER_SIZE];
  30. static LONG color1[FLI_MAX_COLORS];
  31. static LONG color2[FLI_MAX_COLORS];
  32. static LONG color3[FLI_MAX_COLORS];
  33.  
  34. /****************************************************************
  35.  * make_fli
  36.  ****************************************************************/
  37.  
  38. int make_fli()
  39. {
  40.     char image_file_name[MAXLEN];
  41.     char first_name[MAXLEN];
  42.     char last_name[MAXLEN];
  43.     struct stat statbuf;
  44.     int i, nframes, help, irun;
  45.     int file_size, frame_size, mem_size_one;
  46.     int read_flag, first_flag;
  47.     UBYTE *pb_run[3];
  48.     UBYTE *first_pixel, *last_pixel, *curr_pixel, *pre_pixel, *prepre_pixel;
  49.     LONG *curr_color, *first_color, *last_color;
  50.  
  51.     /* ........ first test the list file ...... */
  52.  
  53.     fprintf(stdout,"scanning list file...\n");
  54.  
  55.     for (i=0; i < FLI_MAX_FRAMES; i++)
  56.     {
  57.     if (! get_next_line(input, image_file_name, MAXLEN))
  58.         break;
  59.  
  60.     if (stat(image_file_name, &statbuf) != 0)
  61.     {
  62.         fprintf
  63.         (stderr,"flimake: %d. file %s not found\n",i,image_file_name);
  64.         goto exit_error;
  65.     }
  66.     if (i == 0) strcpy(first_name, image_file_name);
  67.     strcpy(last_name, image_file_name);
  68.     }
  69.     nframes=i;
  70.  
  71.     fprintf(stdout," %d frames\n",nframes);
  72.     rewind(input);
  73.  
  74.     /* ....... prepare pointers ....... */
  75.  
  76.     first_pixel = big_buffer;
  77.     last_pixel = first_pixel + fli_size;
  78.     pb_run[0] = last_pixel + fli_size;
  79.     pb_run[1] = pb_run[0] + fli_size;
  80.     pb_run[2] = pb_run[1] + fli_size;
  81.  
  82.     first_color = color1;
  83.     last_color = color2;
  84.  
  85.     /* ............. read first and last frame ........ */
  86.  
  87.     if (!get_image(first_name, first_pixel, first_color, 0)) goto exit_error;
  88.     if (nframes > 1)
  89.     {
  90.     if (!get_image(last_name, last_pixel, last_color, 0)) goto exit_error;
  91.     }
  92.     else
  93.     {
  94.     last_pixel = first_pixel;
  95.     last_color = first_color;
  96.     }
  97.  
  98.     /* ............. write dummy header ........ */
  99.  
  100.     memset(&file_header, 0, FLI_FILE_HEADER_SIZE);
  101.  
  102.     if (fwrite(file_header, FLI_FILE_HEADER_SIZE, 1, output) != 1)
  103.     {
  104.         fprintf(stderr," write error\n");
  105.     goto exit_error;
  106.     }
  107.  
  108.     file_size=FLI_FILE_HEADER_SIZE;
  109.  
  110.     /* ................... loop ........................ */
  111.  
  112.     curr_pixel = last_pixel;
  113.     pre_pixel = last_pixel;
  114.     mem_size_one=0;
  115.  
  116.     for (i=1; i <= nframes; i++)
  117.     {
  118.         if (! get_next_line(input, image_file_name, MAXLEN)) break;
  119.     fprintf(stdout,"\n Frame %d\n",i);
  120.  
  121.     irun = i % 3;
  122.  
  123.     prepre_pixel = pre_pixel;
  124.     pre_pixel = curr_pixel;
  125.  
  126.     if (i == 1)
  127.     {
  128.         read_flag = 0;
  129.         first_flag = 1;
  130.         curr_pixel = first_pixel;
  131.         curr_color = first_color;
  132.     }
  133.     else if (i == nframes)
  134.     {
  135.         read_flag = 0;
  136.         first_flag = 0;
  137.         curr_pixel = last_pixel;
  138.         curr_color = last_color;
  139.     }
  140.     else
  141.     {
  142.         read_flag = 1;
  143.         first_flag = 0;
  144.         curr_pixel = pb_run[irun];
  145.         curr_color = color3;
  146.     }
  147.  
  148.     if (read_flag == 1)
  149.     {
  150.         /* fprintf(stdout," Reading image .....\n"); */
  151.             if (!get_image (image_file_name, curr_pixel, curr_color, 0))
  152.         {
  153.         fprintf(stderr," Error reading bitmap from file %s\n",
  154.                 image_file_name);
  155.         goto exit_error;
  156.         }
  157.     }
  158.  
  159.     /* fprintf(stdout," Writing frame.....\n"); */
  160.     frame_size=fli_write_frame(prepre_pixel, pre_pixel, curr_pixel,
  161.             curr_color, first_flag);
  162.  
  163.     if (frame_size == 0)
  164.     {
  165.         fprintf(stderr," Error writing frame %d\n",i);
  166.         goto exit_error;
  167.     }
  168.     file_size += frame_size;
  169.  
  170.     if (i == 1)
  171.         mem_size_one=frame_size;
  172.     }
  173.  
  174.     /* ................. close loop ................... */
  175.  
  176.     fprintf(stdout,"\n Close loop\n");
  177.  
  178.     prepre_pixel = pre_pixel;
  179.     pre_pixel = curr_pixel;
  180.  
  181.     frame_size=fli_write_frame(prepre_pixel, pre_pixel, first_pixel,
  182.             first_color, 0);
  183.  
  184.     if (frame_size == 0)
  185.     {
  186.     fprintf(stderr," Error writing frame\n");
  187.     goto exit_error;
  188.     }
  189.  
  190.     file_size += frame_size;
  191.  
  192.     /* ..................... write actual header ......... */
  193.  
  194.     help=0;
  195.  
  196.     add_bytes(file_header, &help, file_size, IOM_LONG);
  197.     if (old_format_flag == 1)
  198.       add_bytes(file_header, &help, FLI_FILE_OLD_MAGIC, IOM_UWORD);
  199.     else
  200.       add_bytes(file_header, &help, FLI_FILE_MAGIC, IOM_UWORD);
  201.     add_bytes(file_header, &help, nframes, IOM_UWORD);
  202.     add_bytes(file_header, &help, fli_width, IOM_UWORD);
  203.     add_bytes(file_header, &help, fli_height, IOM_UWORD);
  204.     add_bytes(file_header, &help, 0x0008, IOM_UWORD);
  205.     add_bytes(file_header, &help, 0x0000, IOM_UWORD);
  206.     add_bytes(file_header, &help, fli_speed, IOM_UWORD);
  207.  
  208.     help=0x0050;
  209.     add_bytes(file_header, &help, 0x0080, IOM_LONG);
  210.     add_bytes(file_header, &help, (0x0080+mem_size_one), IOM_LONG);
  211.  
  212.     if (fseek(output, 0L, SEEK_SET) != 0)
  213.     {
  214.         fprintf(stderr," fseek error\n");
  215.     goto exit_error;
  216.     }
  217.  
  218.     if (fwrite(file_header, help, 1, output) != 1)
  219.     {
  220.         fprintf(stderr," write error\n");
  221.     goto exit_error;
  222.     }
  223.  
  224.     return(nframes);
  225.  
  226. exit_error:
  227.     exitialise(1);
  228.     exit(1);
  229.     return(0);
  230. }
  231.  
  232. /****************************************************************
  233.  * get_next_line
  234.  ****************************************************************/
  235.  
  236. int
  237. get_next_line
  238. (
  239. FILE *fp,            /* file pointer */
  240. char buff[],
  241. int len
  242. )
  243. {
  244.     int c, i;
  245.  
  246.     i=0;
  247.     while (1)
  248.     {
  249.         c=getc(fp);
  250.     if (i < len) buff[i]=c;
  251.     if ((c == '\n') || (c == EOF))
  252.     {
  253.         if (i < len)
  254.         buff[i]='\0';
  255.         else
  256.         buff[len-1]='\0';
  257.         return(i);
  258.     }
  259.     i++;
  260.     }
  261. }
  262.