home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / x / animutil / xflick.lha / xflick / read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-10  |  13.6 KB  |  567 lines

  1. /*
  2.                xflick - Ron Schnell, March, 1991
  3.  
  4.            This code is provided as is, with no warrantees, expressed
  5.                or implied.  I believe this code to be free of encumbrance,
  6.            and offer it to the public domain.  I ask, however, that
  7.            this paragraph and my name be retained in any modified
  8.            versions of the file you may make, and that you notify me
  9.            of any improvements you make to the code.
  10.  
  11.            Ron Schnell (ronnie@sos.com)
  12. */
  13.  
  14. /*
  15.     The following changes are from Michael Pall
  16.     (pall@rz.uni-karlsruhe.de) Mar 25-28 1991:
  17.  
  18.     Lots of bugfixes and changes to the structure of the files.
  19.     The interpretation part is now in this file.
  20. */
  21.  
  22. /*    This is read.c, the functions which read in FLI structures in 
  23.       a machine independent format.  */
  24.  
  25.  
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. #include "xflick.h"
  30.  
  31. #define TRUE 1
  32. #define FALSE 0
  33.  
  34. #define DEBUGGING 0
  35.  
  36. extern int verbose;
  37.  
  38. void
  39. read_flihead(fd, header)
  40. int fd;
  41. struct fli_header *header;
  42. {
  43.     unsigned char *buf = (unsigned char *)malloc(SIZE_HEADER);
  44.     unsigned char *sbf;
  45.     int i;
  46.  
  47.     sbf = buf;
  48.     (void) read(fd, buf, SIZE_HEADER);
  49.     header->fhd_size = get_long(buf);
  50.     buf += LONGSIZE;
  51.  
  52.     header->fhd_magic = get_short(buf);
  53.     buf += SHORTSIZE;
  54.  
  55.     header->fhd_frames = get_short(buf);
  56.     buf += SHORTSIZE;
  57.  
  58.     header->fhd_width = get_short(buf);
  59.     buf += SHORTSIZE;
  60.  
  61.     header->fhd_height = get_short(buf);
  62.     buf += SHORTSIZE;
  63.  
  64.     header->fhd_gap = get_short(buf);
  65.     buf += SHORTSIZE;   /* nothing */
  66.  
  67.     header->fhd_flags = get_short(buf);
  68.     buf += SHORTSIZE;   /* flags */
  69.  
  70.     header->fhd_speed = get_short(buf);
  71.     buf += SHORTSIZE;
  72.  
  73.     header->fhd_next = get_long(buf);
  74.     buf += LONGSIZE;
  75.  
  76.     header->fhd_frit = get_long(buf);
  77.     buf += LONGSIZE;
  78.  
  79.     bcopy(buf, header->fhd_expand, FHD_EXPAND_SIZE);
  80.  
  81.     free(sbf);
  82.  
  83.     if (verbose) {
  84.       fprintf(stderr, "header->fhd_size: %d\n", header->fhd_size);
  85.       fprintf(stderr, "header->fhd_magic: 0x%x\n", header->fhd_magic);
  86.       fprintf(stderr, "header->fhd_frames: %d\n", header->fhd_frames);
  87.       fprintf(stderr, "header->fhd_width: %d\n", header->fhd_width);
  88.       fprintf(stderr, "header->fhd_height: %d\n", header->fhd_height);
  89.       fprintf(stderr, "header->fhd_gap: 0x%x\n", header->fhd_gap);
  90.       fprintf(stderr, "header->fhd_flags: 0x%x\n", header->fhd_flags);
  91.       fprintf(stderr, "header->fhd_speed: %d\n", header->fhd_speed);
  92.       fprintf(stderr, "header->fhd_next: %d\n", header->fhd_next);
  93.       fprintf(stderr, "header->fhd_frit: 0x%x\n", header->fhd_frit);
  94.       fprintf(stderr, "header->fhd_expand: ");
  95.       for (i = 0; i < FHD_EXPAND_SIZE; i++)
  96.     fprintf(stderr, "%x ", 0xff & (unsigned int)header->fhd_expand[i]);
  97.       fprintf(stderr, "\n");
  98.     }
  99. }
  100.  
  101. void
  102. read_fheader(fd, fheader)
  103. int fd;
  104. struct frame_header *fheader;
  105. {
  106.     unsigned char data[SIZE_FHEADER];
  107.     unsigned char *buf = data;
  108.     int ret;
  109.     unsigned char tmp;
  110.     int syncNotFound = TRUE;
  111.     int i;
  112.  
  113.     /* Syncronize to the magic word */
  114.     i = 0;
  115.     do {
  116.       ret = read(fd, &tmp, 1);
  117.       ++i;
  118.       if (verbose)
  119.     fprintf(stderr, "read1 i: %d tmp: 0x%x sync: %d\n",
  120.         i, 0xff & (unsigned int)tmp, syncNotFound);
  121.       if (tmp == (unsigned char)0xfa) {
  122.     ret = read(fd, &tmp, 1);
  123.     ++i;
  124.     if (tmp == (unsigned char)0xf1)
  125.       syncNotFound = FALSE;
  126.     else
  127.       syncNotFound = TRUE;
  128.     if (verbose)
  129.       fprintf(stderr, "read2 i: %d tmp: 0x%x sync: %d\n",
  130.           i, 0xff & (unsigned int)tmp, syncNotFound);
  131.       }
  132.     } while((syncNotFound == TRUE) && (ret > 0));
  133.  
  134.     if (ret < 0) {
  135.       fprintf(stderr,
  136. "Error reading input file looking for frame header magic number. i: %d\n", i);
  137.       exit(-1);
  138.     }
  139.     
  140.     if (verbose)
  141.       fprintf(stderr, "After Sync i: %d sync: %d ret: %d\n",
  142.           i, syncNotFound, ret);
  143.  
  144.     if ((ret = lseek(fd, -6, SEEK_CUR)) < 0) {
  145.       perror("Seeking to begining of frame");
  146.       exit(-1);
  147.     }
  148.  
  149.     (void) read(fd, buf, SIZE_FHEADER);
  150.  
  151.     fheader->fr_size = get_long(buf);
  152.     buf += LONGSIZE;
  153.     fheader->fr_magic = get_short(buf);
  154.     buf += SHORTSIZE;
  155.     fheader->fr_chunks = get_short(buf);
  156.     bcopy(buf, fheader->fr_expand, FR_EXPAND_SIZE);
  157.  
  158.  
  159.     if (verbose) {
  160.       fprintf(stderr, "fheader->fr_size: %d\n", fheader->fr_size);
  161.       fprintf(stderr, "fheader->fr_magic: 0x%x\n", fheader->fr_magic);
  162.       fprintf(stderr, "fheader->fr_chunks: %d\n", fheader->fr_chunks);
  163.       fprintf(stderr, "fheader->fr_expand: ");
  164.       for (i = 0; i < FR_EXPAND_SIZE; i++)
  165.     fprintf(stderr, "%d ", 0xff & (unsigned int)fheader->fr_expand[i]);
  166.       fprintf(stderr, "\n");
  167.     }
  168.  
  169. }
  170.  
  171. void
  172. read_chunk(fd, chunk)
  173. int fd;
  174. struct chunk_header *chunk;
  175. {
  176.     unsigned char data[SIZE_CHEADER];
  177.     unsigned char *buf = data;
  178.  
  179.     (void) read(fd, buf, SIZE_CHEADER);
  180.     chunk->ch_bytes = get_long(buf);
  181.     buf += LONGSIZE;
  182.     chunk->ch_type = get_short(buf);
  183.  
  184.     if (verbose) {
  185.       fprintf(stderr, "chunk->ch_bytes: %d\n", chunk->ch_bytes);
  186.       fprintf(stderr, "chunk->ch_type: %d\n", chunk->ch_type);
  187.     }
  188. }
  189.  
  190. void
  191. interpret_fli(fd,header,data,notfirst,func)
  192. int fd;            /* The file descriptor */
  193. struct fli_header *header;    /* The header of the file */
  194. unsigned char *data;    /* A pointer to the pixel data */
  195. int notfirst;        /* Is this not the first time through? */
  196. void (*func)();        /* The function that stores or displays the chunks */
  197. {
  198.     int i, j, s, l, k;            /* Random look/status ints */
  199.     short ndiff;        /* To hold the # lines changed from last frame */
  200.     struct frame_header fheader;    /* To hold each frame header */
  201.     struct chunk_header chunk;        /* To hold each chunk header */
  202.     unsigned char *buf=(unsigned char *)0;
  203.                     /* malloc'd space to hold each chunk */
  204.     int bufalloc;            /* Space already allocated for buf */
  205.     int skchange, skcount, sicount;  /* Various pixel/data index's */
  206.     unsigned char *cptr,*dptr,*eptr;     /* data pointers into *buf */
  207.     int npack;                /* Number of packets in this chunk */
  208.     unsigned char by, by0, by1;        /* To hold a byte */
  209.     int curx, cury;            /* The current x & y coords */
  210.     short wwidth, wheight;        /* Width and height of the window */
  211.     int topy,bottomy,leftx,rightx;  /* For optimizing FLI_LC chunks */
  212.  
  213.     static long twopos;            /* To keep the file offset of frame # 2 */
  214.  
  215.     wwidth = header->fhd_width;
  216.     wheight = header->fhd_height;
  217.  
  218.     for (i=0; i <= header->fhd_frames; i++)
  219.     {
  220.  
  221.     /* The following stuff ist only needed if we display the frames
  222.        while we interpret the file */
  223.  
  224.     /* If this is the first time through, we display frame # 1,
  225.        otherwise, we skip right to frame two */
  226.  
  227.     if ((notfirst) && (i==0))
  228.     {
  229.         (void) lseek(fd, twopos, 0);
  230.         continue;
  231.     }
  232.  
  233.     /* If this is the first time through and we are at frame #2,
  234.        save our file offset for the next loop through */
  235.         
  236.     if (!(notfirst) && (i==1))
  237.         twopos = lseek(fd, 0, 1);
  238.         
  239.     /* Each frame has a header */
  240.  
  241.     read_fheader(fd, &fheader);
  242.  
  243.     for (j=0; j < fheader.fr_chunks; j++)
  244.     {
  245.         /* Each chunk also has a header */
  246.         read_chunk(fd, &chunk);
  247.  
  248.         /* Malloc the space to hold whole chunk, unless it
  249.            is a COPY chunk. Malloc/Realloc only if necessary */
  250.  
  251.         if (chunk.ch_type != FLI_COPY) {
  252.         if (!buf)
  253.         {
  254.             buf = (unsigned char *)malloc(chunk.ch_bytes);
  255.             bufalloc = chunk.ch_bytes;
  256.         }
  257.         else
  258.         {
  259.             if (bufalloc < chunk.ch_bytes)
  260.             {
  261.             buf = (unsigned char *)realloc(buf, chunk.ch_bytes);
  262.             bufalloc = chunk.ch_bytes;
  263.             }
  264.         }
  265.           }    
  266.  
  267.         /* A COPY chunk means 64k of uncompressed image data,
  268.            read it directly into the image */
  269.  
  270.         if (chunk.ch_type == FLI_COPY)
  271.         s = read(fd, data, wwidth*wheight);
  272.         else
  273.         s = read(fd, buf, chunk.ch_bytes -  6);
  274.  
  275.         if (s < 0) {
  276.         fprintf(stderr, "s: %d fd: %d data: 0x%x chunk.ch_bytes: %d\n",
  277.             s, fd, data, chunk.ch_bytes);
  278.         xferror("Error reading image data.");
  279.           }
  280.  
  281.         cptr = buf;
  282.  
  283.         switch (chunk.ch_type) {
  284.         case FLI_COPY:
  285.         (*func)(FLI_COPY, i, data, 0,0, 0,0, wwidth,wheight);
  286.         break;
  287.  
  288.         /* A BLACK chunk */
  289.  
  290.         case FLI_BLACK:
  291.         bzero(data, wwidth * wheight);
  292.         (*func)(FLI_BLACK, i, data, 0,0, 0,0, wwidth,wheight);
  293.         break;
  294.  
  295.         /* A COLOR chunk:  A compressed colormap to be stored or
  296.            changed */
  297.  
  298.         case FLI_256_COLOR:
  299.         npack = get_short(cptr);
  300.         cptr += SHORTSIZE;
  301.  
  302.         for (l=0; l < npack; l++)
  303.         {
  304.             skcount = *(cptr++);     /* # colors not to change */
  305.             skchange = *(cptr++);    /* # of colors to change */
  306.             if (!skchange)           /* (0 means all) */
  307.             skchange=MAXCOL;
  308.         
  309.             /* The colors are stored in the right 6 bits
  310.                of the byte.  We then convert it to a short. */
  311.  
  312.             (*func)(FLI_256_COLOR, i, cptr, skcount, 
  313.                 skchange, 0,0, 0,0);
  314.             cptr += skchange * 3;
  315.         }
  316.         break;
  317.  
  318.         case FLI_COLOR:
  319.         npack = get_short(cptr);
  320.         cptr += SHORTSIZE;
  321.  
  322.         for (l=0; l < npack; l++)
  323.         {
  324.             skcount = *(cptr++);     /* # colors not to change */
  325.             skchange = *(cptr++);    /* # of colors to change */
  326.             if (!skchange)           /* (0 means all) */
  327.             skchange=MAXCOL;
  328.         
  329.             /* The colors are stored in the right 6 bits
  330.                of the byte.  We then convert it to a short. */
  331.  
  332.             (*func)(FLI_COLOR, i, cptr, skcount, skchange, 0,0, 0,0);
  333.             cptr += skchange * 3;
  334.         }
  335.         break;
  336.  
  337.  
  338.         /* Line compressed, and bytewise/run-length compressed
  339.            chunks are similar.  BRUN chunks always start at the
  340.            top, while LC chunks can skip lines.  Also, LC chunks
  341.                can skip bytes on each line.  The sign of the
  342.            byte also has opposite meanings for the two! */
  343.  
  344.         case FLI_LC:
  345.         curx = 0;
  346.         cury = get_short(cptr);     /* Lines at top to skip */
  347.         cptr += SHORTSIZE;
  348.         ndiff = get_short(cptr);    /* # of lines different */
  349.         cptr += SHORTSIZE;
  350.  
  351.         topy = cury;
  352.         bottomy = cury + ndiff;
  353.  
  354.             leftx = wwidth;
  355.             rightx = 0;
  356.     
  357.         for (k=0; k < ndiff; k++)    /* Go through each line */
  358.         {
  359.             npack = *(cptr++);       /* # packets this line */
  360.  
  361.             for (l=0; l < npack; l++)
  362.             {
  363.             skcount = *(cptr++);    /* # bytes to skip */
  364.             
  365.             if (l == 0 && skcount < leftx)
  366.                 leftx = skcount;
  367.  
  368.             curx += skcount;
  369.  
  370.             /* The number of bytes in this packet to change */
  371.  
  372.             sicount = *(cptr++);
  373.  
  374.             /* Positive, for LC means bytes the changes follow,
  375.                for BRUN means one byte to follow, spread it
  376.                through. */
  377.  
  378.             if (sicount < 128)
  379.             {
  380.                 bcopy(cptr, data + ((wwidth * cury) + curx),
  381.                   sicount);
  382.                 cptr += sicount;
  383.                 curx += sicount;
  384.             }
  385.  
  386.             else
  387.  
  388.             /* Negative, the opposite */
  389.  
  390.             {
  391.                 sicount = 256 - sicount;
  392.                 by = *(cptr++);
  393.                 dptr = data + wwidth * cury + curx;
  394.                 eptr = dptr + sicount;
  395.                 do
  396.                 *(dptr++) = by;
  397.                 while (dptr < eptr);
  398.                 curx += sicount;
  399.             }
  400.             }
  401.  
  402.             if (curx > rightx)
  403.             rightx = curx;
  404.  
  405.             /* Next line */
  406.             cury++;
  407.             curx = 0;
  408.         }
  409.         
  410.         (*func)(FLI_LC, i, data, leftx,topy, leftx,topy,
  411.             rightx-leftx,bottomy-topy);
  412.         break;
  413.  
  414.         case FLI_DELTA:
  415.         curx = 0;
  416.         cury = 0;
  417.  
  418.         by0 = *(cptr++);
  419.         by1 = *(cptr++);
  420.         ndiff = by0 + 256 * by1;     /* # of lines different */
  421.  
  422.         /* printf(" -- -- ndiff: %d  frame: %d\n",ndiff,i); */
  423.  
  424.         topy = 0;
  425.         bottomy = 0;
  426.  
  427.             leftx = wwidth;
  428.             rightx = 0;
  429.     
  430.         k=0;
  431.         while (k < ndiff)            /* Go through each line */
  432.         {
  433.             by0 = *(cptr++);
  434.             by1 = *(cptr++);
  435.             npack = by0 + 256 * by1;
  436.             if (npack >= 32768) npack -= 65536;
  437.  
  438.             /* printf(" -- npack: %d  cury: %d\n",npack,cury); */
  439.  
  440.             if (npack < 0)        /* skip lines */
  441.             {
  442.             if (cury == 0)
  443.             {
  444.                 topy = -npack;
  445.             }
  446.             cury -= npack;
  447.             continue;
  448.             }
  449.  
  450.             bottomy = cury+1;
  451.  
  452.             for (l=0; l < npack; l++)
  453.             {
  454.             skcount = *(cptr++);    /* # bytes to skip */
  455.             
  456.             if (l == 0 && skcount < leftx)
  457.                 leftx = skcount;
  458.  
  459.             curx += skcount;
  460.             /* printf("pack: %d  curx: %d  skip: %d\n",
  461.                     l,curx,skcount); */
  462.  
  463.             /* The number of words in this packet to change */
  464.  
  465.             sicount = *(cptr++);
  466.  
  467.             /* Positive, for DELTA means sicount words (16 bit) 
  468.                that change follow */
  469.             /* printf("sicount: %d\n",sicount); */
  470.  
  471.             if (sicount < 128)
  472.             {
  473.                 bcopy(cptr, data + ((wwidth * cury) + curx),
  474.                   2 * sicount);
  475.                 cptr += 2 * sicount;
  476.                 curx += 2 * sicount;
  477.             }
  478.  
  479.             else
  480.  
  481.             /* Negative, the opposite */
  482.  
  483.             {
  484.                 sicount = 256 - sicount;
  485.                 by0 = *(cptr++);
  486.                 by1 = *(cptr++);
  487.                 dptr = data + wwidth * cury + curx;
  488.                 eptr = dptr + 2 * sicount;
  489.                 do
  490.                 {
  491.                 *(dptr++) = by0;
  492.                 *(dptr++) = by1;
  493.                 }
  494.                 while (dptr < eptr);
  495.                 curx += 2 * sicount;
  496.             }
  497.             }
  498.  
  499.             if (curx > rightx)
  500.             rightx = curx;
  501.  
  502.             /* Next line */
  503.             cury++;
  504.             curx = 0;
  505.             k++;
  506.         }
  507.  
  508.         /* printf(" top: %d  bot: %d    left: %d   right: %d\n",
  509.             topy, bottomy, leftx, rightx); */
  510.         (*func)(FLI_DELTA, i, data, leftx,topy, leftx,topy,
  511.             rightx-leftx,bottomy-topy);
  512.         break;
  513.  
  514.         case FLI_BRUN:
  515.         dptr = data;
  516.     
  517.         for (k=0; k < wheight; k++)    /* Go through each line */
  518.         {
  519.             npack = *(cptr++);       /* # packets this line */
  520.  
  521.             for (l=0; l < npack; l++)
  522.             {
  523.             /* The number of bytes in this packet to change */
  524.  
  525.             sicount = *(cptr++);
  526.  
  527.             /* Positive, for LC means bytes the changes follow,
  528.                for BRUN means one byte to follow, spread it
  529.                through. */
  530.  
  531.             if (sicount < 128)
  532.             {
  533.                 by = *cptr++;
  534.                 eptr = dptr + sicount;
  535.                 while (dptr < eptr)
  536.                 *(dptr++) = by;
  537.             }
  538.  
  539.             /* Negative, the opposite */
  540.  
  541.             else
  542.  
  543.             {
  544.                 sicount = 256 - sicount;
  545.                 bcopy(cptr, dptr, sicount);
  546.                 cptr += sicount;
  547.                 dptr += sicount;
  548.             }
  549.             }
  550.         }
  551.  
  552.         (*func)(FLI_BRUN, i, data, 0,0, 0,0, wwidth,wheight);
  553.         break;
  554.         }
  555.  
  556.     }
  557.     
  558.     /* Send a FLI_SYNC after each frame to get accurate timing */
  559.     (*func)(FLI_SYNC, i, (unsigned char *)0, 0,0, 0,0, 0,0);
  560.     }
  561.  
  562.     /* Only free the buffer if we allocated something */
  563.     if (!buf)
  564.     free(buf);
  565.  
  566. }
  567.