home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / raytrace / vort / vort / lib / writmapl.c < prev    next >
C/C++ Source or Header  |  1992-07-23  |  4KB  |  186 lines

  1. #include <stdio.h>
  2. #include "vort.h"
  3.  
  4. /*
  5.  * writemappeda
  6.  *
  7.  *    writes a scanline of colour map indexes from line to file out.
  8.  */
  9. static int
  10. writemappeda(out, line, alpha)
  11.     image        *out;
  12.     unsigned char    *line, *alpha;
  13. {
  14.     register int    x;
  15.     unsigned char    indx, jndx, i, run, count, lbuf[127], abuf[127];
  16.  
  17.     switch (imagetype(out)) {
  18.     case PIX_CMAP:
  19.         for (x = 0; x < imagewidth(out); x++)
  20.             if (writebyte(out, line[x]) == EOF)
  21.                 return(0);
  22.         break;
  23.     case PIX_RLECMAP:
  24.         indx = line[0];
  25.         count = 0;
  26.         run = 0;
  27.         for (x = 1; x < imagewidth(out); x++) {
  28.             if (indx != line[x]) {
  29.                 if (run != 0) {
  30.                     if (writebyte(out, run) == EOF)
  31.                         return(0);
  32.                     writebyte(out, indx);
  33.                     run = 0;
  34.                 } else {
  35.                     if (count == 127) {
  36.                         if (writebyte(out, 255) == EOF)
  37.                             return(0);
  38.                         for (i = 0; i != 127; i++)
  39.                             writebyte(out, lbuf[i]);
  40.                         count = 0;
  41.                     }
  42.                     lbuf[count] = indx;
  43.                     count++;
  44.                 }
  45.             } else if (run == 127) {
  46.                 if (writebyte(out, 127) == EOF)
  47.                     return(0);
  48.                 writebyte(out, indx);
  49.                 run = 0;
  50.             } else {
  51.                 if (count != 0) {
  52.                     if (writebyte(out, count | 0x80) == EOF)
  53.                         return(0);
  54.                     for (i = 0; i != count; i++)
  55.                         writebyte(out, lbuf[i]);
  56.                     count = 0;
  57.                 }
  58.                 run++;
  59.             }
  60.             indx = line[x];
  61.         }
  62.         if (count != 0) {
  63.             if (writebyte(out, count | 0x80) == EOF)
  64.                 return(0);
  65.             for (i = 0; i != count; i++)
  66.                 writebyte(out, lbuf[i]);
  67.         }
  68.  
  69.         if (writebyte(out, run) == EOF)
  70.             return(0);
  71.         writebyte(out, indx);
  72.         break;
  73.     case PIX_ACMAP:
  74.         for (x = 0; x < imagewidth(out); x++) {
  75.             if (writebyte(out, line[x]) == EOF)
  76.                 return(0);
  77.             if (alpha == (unsigned char *)NULL) {
  78.                 if (writebyte(out, alpha[x]) == EOF)
  79.                     return(0);
  80.             } else {
  81.                 if (writebyte(out, 0xff) == EOF)
  82.                     return(0);
  83.             }
  84.         }
  85.         break;
  86.     case PIX_RLEACMAP:
  87.         indx = line[0];
  88.         if (alpha != (unsigned char *)NULL)
  89.             jndx = alpha[0];
  90.         else
  91.             jndx = 0xff;
  92.         count = 0;
  93.         run = 0;
  94.         for (x = 1; x < imagewidth(out); x++) {
  95.             if (indx != line[x] || (alpha != (unsigned char *)NULL && jndx != alpha[x])) {
  96.                 if (run != 0) {
  97.                     if (writebyte(out, run) == EOF)
  98.                         return(0);
  99.                     writebyte(out, indx);
  100.                     writebyte(out, jndx);
  101.                     run = 0;
  102.                 } else {
  103.                     if (count == 127) {
  104.                         if (writebyte(out, 255) == EOF)
  105.                             return(0);
  106.                         for (i = 0; i != 127; i++) {
  107.                             writebyte(out, lbuf[i]);
  108.                             writebyte(out, abuf[i]);
  109.                         }
  110.                         count = 0;
  111.                     }
  112.                     lbuf[count] = indx;
  113.                     abuf[count] = jndx;
  114.                     count++;
  115.                 }
  116.             } else if (run == 127) {
  117.                 if (writebyte(out, 127) == EOF)
  118.                     return(0);
  119.                 writebyte(out, indx);
  120.                 writebyte(out, jndx);
  121.                 run = 0;
  122.             } else {
  123.                 if (count != 0) {
  124.                     if (writebyte(out, count | 0x80) == EOF)
  125.                         return(0);
  126.                     for (i = 0; i != count; i++) {
  127.                         writebyte(out, lbuf[i]);
  128.                         writebyte(out, abuf[i]);
  129.                     }
  130.                     count = 0;
  131.                 }
  132.                 run++;
  133.             }
  134.             indx = line[x];
  135.             if (alpha != (unsigned char *)NULL)
  136.                 jndx = alpha[x];
  137.         }
  138.         if (count != 0) {
  139.             if (writebyte(out, count | 0x80) == EOF)
  140.                 return(0);
  141.             for (i = 0; i != count; i++) {
  142.                 writebyte(out, lbuf[i]);
  143.                 writebyte(out, abuf[i]);
  144.             }
  145.         }
  146.  
  147.         if (writebyte(out, run) == EOF)
  148.             return(0);
  149.         writebyte(out, indx);
  150.         writebyte(out, jndx);
  151.         break;
  152.     default:
  153.         fprintf(stderr, "writemappeda: bad file format.\n");
  154.         return(0);
  155.     }
  156.  
  157.     return(1);
  158. }
  159.  
  160. /*
  161.  * writemappedline
  162.  *
  163.  *    writes a scanline of colour map indexes from line to file out.
  164.  */
  165. int
  166. writemappedline(out, line)
  167.     image        *out;
  168.     unsigned char    *line;
  169. {
  170.     return(writemappeda(out, line, (unsigned char *)NULL));
  171. }
  172.  
  173. /*
  174.  * writemappedaline
  175.  *
  176.  *    writes a scanline of colour map indexes and it associated alpha
  177.  * information to file out.
  178.  */
  179. int
  180. writemappedaline(out, line, alpha)
  181.     image        *out;
  182.     unsigned char    *line, *alpha;
  183. {
  184.     return(writemappeda(out, line, alpha));
  185. }
  186.