home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / graphics / utility / mgif35s / file.c next >
Encoding:
C/C++ Source or Header  |  1991-06-14  |  5.2 KB  |  272 lines

  1. #undef FULL_SCREEN        /* def to write entire screen, not just wxh */
  2.  
  3.  
  4. /*
  5.  *    file.c - stuff for file operations (flicker file write, etc)
  6.  */
  7.  
  8. static char *sccsid  = "@(#) file.c 1.0 91/6/15 rosenkra\0\0                ";
  9.  
  10. #include <stdio.h>
  11. #include <osbind.h>
  12. #include "mgif.h"
  13.  
  14. #define reg_t        register
  15. #define DBG(x)        /*printf x*/
  16.  
  17.  
  18. #define NCHUNK        400
  19. #define FLMAGSZ        10
  20. #define FLHDRSZ        256
  21. #define MAGIC        "FLICKER91a"
  22.  
  23.  
  24. /*------------------------------*/
  25. /*    write_fl        */
  26. /*------------------------------*/
  27. int write_fl (fname, pscrn, w, h)
  28. char   *fname;
  29. int    *pscrn;
  30. int    w;
  31. int    h;
  32. {
  33.     char           *pbuf;
  34.     register long    i;
  35.     register long    j;
  36.     int        fd;
  37.     int        ival;
  38.     FILE           *stream;
  39.     char        magic[FLMAGSZ+10];
  40.     char        header[FLHDRSZ+10];
  41.     char           *pheader;
  42.     char           *pchar;
  43.     int           *pint;
  44.     long           *plong;
  45.     int        nbytes;
  46.  
  47.  
  48.     /*
  49.      *   open the .fl file...
  50.      */
  51.     if ((stream = fopenb (fname, "w")) == (FILE *) NULL)
  52.     {
  53.         printf ("Error openning %s.\n",
  54.             fname);
  55.         return (0);
  56.     }
  57.     fd = fileno (stream);
  58.  
  59.  
  60.     /*
  61.      *   set up header. start with magic. there is no real header (yet).
  62.      *   just write nulls in unused portions
  63.      *
  64.      *    offset    size    item
  65.      *    ------    ----    -----------------------------------------
  66.      *    0    2    width (pixels)
  67.      *    2    2    height (pixels)
  68.      */
  69.     strcpy (magic, MAGIC);
  70.     /* align to word boundary */
  71.     if ((long) (&header[0]) & 1L)    pheader = (char *) (&header[1]);
  72.     else                pheader = (char *) (&header[0]);
  73.     for (i = 0; i < FLHDRSZ; i++)
  74.         pheader[i] = 0;
  75.     if (w > 640)
  76.         w = 640;
  77.     if (h > 400)
  78.         h = 400;
  79.     pint = (int *) (&pheader[0]);    *pint = w;
  80.     pint = (int *) (&pheader[2]);    *pint = h;
  81.  
  82.  
  83.     /*
  84.      *   write .fl header...
  85.      */
  86.     write (fd, magic, (int) FLMAGSZ);
  87.     write (fd, header, (int) FLHDRSZ);
  88.  
  89.  
  90. #ifdef FULL_SCREEN
  91.     /*
  92.      *   write the screens, sequentially. each screen is 32000 and there
  93.      *   are 3 screens for a total of 96000 bytes. work with NCHUNK bytes
  94.      *   at a time.
  95.      */
  96.     pbuf = (char *) pscrn;
  97.     for (i = 0L; i < 96000L/NCHUNK; i++)    /* number of writes */
  98.     {
  99.         write (fd, pbuf, (int) NCHUNK);
  100.  
  101.         pbuf += (long) NCHUNK;
  102.     }
  103. #else
  104.     /*
  105.      *   only write what we need to the file for images smaller than
  106.      *   the screen...
  107.      */
  108.     if ((int) w % 8)
  109.         w += 8;
  110.     nbytes = (w >> 3);
  111.     pbuf = (char *) pscrn;
  112.     for (i = 0L; i < h; i++)
  113.     {
  114.         write (fd, pbuf, nbytes);
  115.         pbuf += 80L;
  116.     }
  117.  
  118.     pbuf = (char *) ((long) pscrn + 32000L);
  119.     for (i = 0L; i < h; i++)
  120.     {
  121.         write (fd, pbuf, nbytes);
  122.         pbuf += 80L;
  123.     }
  124.  
  125.     pbuf = (char *) ((long) pscrn + 64000L);
  126.     for (i = 0L; i < h; i++)
  127.     {
  128.         write (fd, pbuf, nbytes);
  129.         pbuf += 80L;
  130.     }
  131. #endif
  132.  
  133.     fclose (stream);
  134.  
  135.     return (1);
  136. }
  137.  
  138.  
  139.  
  140.  
  141.  
  142. /*------------------------------*/
  143. /*    read_fl            */
  144. /*------------------------------*/
  145. int read_fl (fname, pscrn, w, h)
  146. char   *fname;                /* file name (in) */
  147. int    *pscrn;                /* where to put screen data (out) */
  148. int    *w;                /* size of image, pixels (out) */
  149. int    *h;
  150. {
  151.  
  152. /*
  153.  *    read the flicker file. return 0 if error else 1.
  154.  */
  155.  
  156.     reg_t char     *pbuf;
  157.     reg_t int      *pint;
  158.     reg_t int    nbytes;
  159.     reg_t int    nlines;
  160.     reg_t long    ii;
  161.     int        fd;
  162.     FILE           *stream;
  163.     char        magic[FLMAGSZ+10];
  164.     char        header[FLHDRSZ+10];
  165.     char           *pheader;
  166.  
  167.  
  168.     DBG (("enter read_fl...\n"));
  169.  
  170.     /*
  171.      *   open the .fl file, binary...
  172.      */
  173.     if ((stream = fopenb (fname, "r")) == (FILE *) NULL)
  174.     {
  175.         printf ("Error openning %s.\n", fname);
  176.         return (0);
  177.     }
  178.     fd = fileno (stream);
  179.  
  180.  
  181.  
  182.     /*
  183.      *   read .fl magic
  184.      */
  185.     read (fd, magic, (int) FLMAGSZ);
  186.     if (strncmp (magic, MAGIC, 10))
  187.     {
  188.         printf ("Bad magic here...\n");
  189.         fclose (stream);
  190.         return (0);
  191.     }
  192.  
  193.  
  194.  
  195.     /*
  196.      *   read .fl header:
  197.      *
  198.      *    offset    size    item
  199.      *    ------    ----    -----------------------------------------
  200.      *    0    2    width (pixels)
  201.      *    2    2    height (pixels)
  202.      */
  203.     if (header & 1)        pheader = (char *) (&header[1]);
  204.     else            pheader = (char *) (&header[0]);
  205.  
  206.     read (fd, pheader, (int) FLHDRSZ);
  207.  
  208.     pint = &pheader[0];    *w = *pint;
  209.     pint = &pheader[2];    *h = *pint;
  210.  
  211.  
  212.  
  213. #ifdef FULL_SCREEN
  214.     /*
  215.      *   read the screens, sequentially. each screen is 32000 and there
  216.      *   are 3 screens for a total of 96000 bytes. work with NCHUNK bytes
  217.      *   at a time.
  218.      */
  219.     pbuf = (char *) pscrn;
  220.     for (ii = 0L; ii < 96000L/NCHUNK; ii++)    /* number of reads */
  221.     {
  222.         read (fd, pbuf, (int) NCHUNK);
  223.  
  224.         pbuf += (long) NCHUNK;
  225.     }
  226. #else
  227.     /*
  228.      *   this is space-saver version. we only need the actual pixels,
  229.      *   not the whole screen saved. find actual byte count for files
  230.      *   with w not evenly divisible by 8.
  231.      */
  232.     if ((int) *w % 8)
  233.         nbytes = ((*w + 8) >> 3);
  234.     else
  235.         nbytes = (*w >> 3);
  236.     nlines = *h;
  237.  
  238.     /* screen 1 */
  239.     for (pbuf = (char *) pscrn, ii = 0L; ii < nlines; ii++)
  240.     {
  241.         read (fd, pbuf, nbytes);
  242.         pbuf += 80L;        /* 640x400 screen has 80 bytes/line */
  243.     }
  244.  
  245.     /* screen 2 */
  246.     for (pbuf = (char *) ((long) pscrn + 32000L), ii = 0L; ii < nlines; ii++)
  247.     {
  248.         read (fd, pbuf, nbytes);
  249.         pbuf += 80L;
  250.     }
  251.  
  252.     /* screen 3 */
  253.     for (pbuf = (char *) ((long) pscrn + 64000L), ii = 0L; ii < nlines; ii++)
  254.     {
  255.         read (fd, pbuf, nbytes);
  256.         pbuf += 80L;
  257.     }
  258. #endif
  259.  
  260.  
  261.     /*
  262.      *   done. return success...
  263.      */
  264.     fclose (stream);
  265.     DBG (("read_fl returning...\n"));
  266.  
  267.     return (1);
  268. }
  269.  
  270.  
  271.  
  272.