home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / utils / fbm2fl03.lha / fppcolor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-05  |  4.2 KB  |  179 lines

  1. /****************************************************************
  2.  * fppcolor.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 "fpfli.h"
  19.  
  20. static int work[FLI_MAX_COLORS];
  21. static long int mem_color[FLI_MAX_COLORS];
  22. static int change[FLI_MAX_COLORS];
  23.  
  24. /****************************************************************
  25.  * make_color_chunk
  26.  ****************************************************************/
  27.  
  28. int
  29. make_color_chunk
  30. (
  31. LONG color[],            /* array of colors */
  32. int first_flag
  33. )
  34. {
  35.     LONG rgb_value;
  36.     int red, green, blue, used_count;
  37.     int skip_count,size_count,help,packets,change_count;
  38.     int i, j, ipos;
  39.  
  40.     used_count = 0;
  41.     change_count = 0;
  42.  
  43.     if (first_flag == 1)            /* 1st frame */
  44.     {
  45.     if (map_color_flag == 1)
  46.     {
  47.         for (j=0; j < FLI_MAX_COLORS; j++)
  48.         {
  49.         change[j]=1;
  50.         change_count++;
  51.         color[j]=map_color[j];
  52.         if (color[j] >= 0) used_count++;
  53.         }
  54.     }
  55.     else
  56.     {
  57.         for (j=0; j < FLI_MAX_COLORS; j++)
  58.         {
  59.         change[j]=1;
  60.         change_count++;
  61.         mem_color[j]=color[j];
  62.         if (color[j] >= 0) used_count++;
  63.         }
  64.     }
  65.     }
  66.     else
  67.     {
  68.     if (map_color_flag == 1) return(0);
  69.  
  70.     for (j=0; j < FLI_MAX_COLORS; j++)
  71.     {
  72.         if (color[j] >= 0)                /* used */
  73.         {
  74.         used_count++;
  75.         if (mem_color[j] != color[j])
  76.         {
  77.             change[j]=1;
  78.             change_count++;
  79.             mem_color[j]=color[j];
  80.         }
  81.         }
  82.     }
  83.     }
  84.  
  85.     fprintf(stdout," Used colors: %d   new: %d\n",used_count,change_count);
  86.  
  87.     if (change_count == 0) return(0);    /* no new colors  -> no color_chunk */
  88.  
  89.     if (change[FLI_MAX_COLORS - 1] == 1)
  90.         work[FLI_MAX_COLORS-1]=1;
  91.     else
  92.         work[FLI_MAX_COLORS-1]=-1;
  93.  
  94.     for (i=(FLI_MAX_COLORS-2); i >= 0; i--)
  95.     {
  96.         if (change[i] == 1)
  97.         work[i]=(work[i+1] > 0) ? (work[i+1]+1) : 1;
  98.     else
  99.         work[i]=(work[i+1] < 0) ? (work[i+1]-1) : -1;
  100.         /* fprintf(stdout," color: %d  %d\n",i,work[i]); */
  101.     }
  102.  
  103.     ipos=8;            /* 4 bytes for size of chunk */
  104.                 /* 2 bytes for type of chunk */
  105.                 /* 2 bytes for number of packets */
  106.     i=0;
  107.     skip_count=0;
  108.     packets=0;
  109.  
  110.     while (i < FLI_MAX_COLORS)
  111.     {
  112.         /* fprintf(stdout," color: %d  %d\n",i,work[i]); */
  113.         if (work[i] < 0)
  114.     {
  115.         skip_count=-work[i];
  116.         i += skip_count;
  117.     }
  118.     else
  119.     {
  120.             size_count=work[i];
  121.         help = i + size_count;
  122.         if (size_count == 256) size_count=0;
  123.  
  124.         /* fprintf(stdout," skip: %d  size: %d\n",skip_count, size_count);
  125.         */
  126.             add_bytes(color_chunk_buffer, &ipos, skip_count, IOM_UBYTE);
  127.             add_bytes(color_chunk_buffer, &ipos, size_count, IOM_UBYTE);
  128.  
  129.             while (i < help)
  130.         {
  131.         rgb_value=color[i];
  132.         if (rgb_value != -1)
  133.         {
  134.             red=rgb_value % 256;
  135.             rgb_value=(rgb_value - red)/256;
  136.             green=rgb_value % 256;
  137.             rgb_value=(rgb_value - green)/256;
  138.             blue=rgb_value % 256;
  139.  
  140.             /* printf(" %d   %d %d %d\n",i,red,green,blue); */
  141.             if (old_format_flag == 1)
  142.               {
  143.             red = red >> 2;
  144.             green = green >> 2;
  145.             blue = blue >> 2;
  146.               }
  147.         }
  148.         else
  149.         {
  150.             red=0;
  151.             green=0;
  152.             blue=0;
  153.         }        
  154.  
  155.                 add_bytes(color_chunk_buffer, &ipos, red, IOM_UBYTE);
  156.                 add_bytes(color_chunk_buffer, &ipos, green,IOM_UBYTE);
  157.                 add_bytes(color_chunk_buffer, &ipos, blue, IOM_UBYTE);
  158.         i++;
  159.             }
  160.         packets++;
  161.     }
  162.     }
  163.  
  164.     if (packets == 0) return(0);    /* no packets -> no color_chunk */
  165.  
  166.     if ((ipos % 2) == 1)        /* add single pad byte to even size */
  167.     add_bytes(color_chunk_buffer, &ipos, 0x0000, IOM_UBYTE);
  168.  
  169.     help=0;
  170.     add_bytes(color_chunk_buffer, &help, ipos, IOM_LONG);
  171.     if (old_format_flag == 1)
  172.       add_bytes(color_chunk_buffer, &help, FLI_64_COLOR, IOM_UWORD);
  173.     else
  174.       add_bytes(color_chunk_buffer, &help, FLI_256_COLOR, IOM_UWORD);
  175.     add_bytes(color_chunk_buffer, &help, packets, IOM_UWORD);
  176.  
  177.     return(ipos);
  178. }
  179.