home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fbm / src / fbpalet.c < prev    next >
C/C++ Source or Header  |  1990-06-24  |  6KB  |  203 lines

  1. /*****************************************************************
  2.  * fbpalet.c: FBM Release 1.0 25-Feb-90 Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989,1990 by Michael Mauldin.  Permission is granted
  5.  * to use this file in whole or in part for any purpose, educational,
  6.  * recreational or commercial, provided that this copyright notice
  7.  * is retained unchanged.  This software is available to all free of
  8.  * charge by anonymous FTP and in the UUNET archives.
  9.  *
  10.  * fbpalet.c: Replace one image's color map with another image's.
  11.  *          The color index plane is not modifed by this action.
  12.  *
  13.  *          Includes option for removing duplicates.  That is
  14.  *          Suppose both color 1 and color 127 are both <0,0,0>
  15.  *          (pure black)...with the -d option, all uses of color
  16.  *          127 get converted to color 1.  The colormap is not
  17.  *          modified by this action.
  18.  *
  19.  * USAGE
  20.  *     % fbpalet < image1 > image2
  21.  *
  22.  * EDITLOG
  23.  *     LastEditDate = Mon Jun 25 00:03:55 1990 - Michael Mauldin
  24.  *     LastFileName = /usr2/mlm/src/misc/fbm/fbpalet.c
  25.  *
  26.  * HISTORY
  27.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  28.  *    Package for Release 1.0
  29.  *
  30.  * 01-May-90  Michael Mauldin (mlm) at Carnegie-Mellon University
  31.  *     Created.
  32.  *****************************************************************/
  33.  
  34. # include <stdio.h>
  35. # include <math.h>
  36. # include "fbm.h"
  37.  
  38. # define USAGE \
  39.   "Usage: fbpalet [ -m<mapname ] [ -d ] [ -<type> ] < image > image"
  40.  
  41. #ifndef lint
  42. static char *fbmid =
  43. "$FBM fbpalet.c <1.0> 25-Jun-90  (C) 1989,1990 by Michael Mauldin, source \
  44. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  45. #endif
  46.  
  47. int debug = 0;
  48. int removedup = 0;
  49. double expon = 1.0;
  50.  
  51. main (argc, argv)
  52. char *argv[];
  53. { FBM image;
  54.   FBM mapimage;
  55.   int outtype = DEF_8BIT;
  56.   char *fname = NULL;
  57.   char *mapname = NULL;
  58.  
  59.   /* Get the options */
  60.   while (--argc > 0 && (*++argv)[0] == '-')
  61.   { while (*++(*argv))
  62.     { switch (**argv)
  63.       {
  64.     case 'D':    debug++; break;
  65.     case 'd':    removedup++; break;
  66.         case 'm':    mapname = *argv+1; SKIPARG; break;
  67.     case 'A':    outtype = FMT_ATK; break;
  68.     case 'B':    outtype = FMT_FACE; break;
  69.     case 'F':    outtype = FMT_FBM; break;
  70.     case 'G':    outtype = FMT_GIF; break;
  71.     case 'I':    outtype = FMT_IFF; break;
  72.     case 'L':    outtype = FMT_LEAF; break;
  73.     case 'M':    outtype = FMT_MCP; break;
  74.     case 'P':    outtype = FMT_PBM; break;
  75.     case 'S':    outtype = FMT_SUN; break;
  76.     case 'T':    outtype = FMT_TIFF; break;
  77.     case 'X':    outtype = FMT_X11; break;
  78.     case 'Z':    outtype = FMT_PCX; break;
  79.     default:        fprintf (stderr, "%s\n", USAGE);
  80.                         exit (1);
  81.       }
  82.     }
  83.   }
  84.  
  85.   /* Clear the memory pointers so alloc_fbm won't be confused */
  86.   image.cm  = image.bm  = (unsigned char *) NULL;
  87.   mapimage.cm = mapimage.bm = (unsigned char *) NULL;
  88.  
  89.   /* Read the image */
  90.   if (read_bitmap (&image, fname))
  91.   { 
  92.     if (image.hdr.planes == 1 && image.hdr.clrlen == 0)
  93.     { fprintf (stderr, "fbpalet only works on color images\n");
  94.     }
  95.  
  96.     /* Read the mapimage */
  97.     if (mapname && read_bitmap (&mapimage, mapname))
  98.     { 
  99.       if (mapimage.hdr.planes == 1 && mapimage.hdr.clrlen == 0)
  100.       { fprintf (stderr, "fbpalet only works on mapped color images\n");
  101.         exit (0);
  102.       }
  103.  
  104.       if (dup_clr (&mapimage, &image) &&
  105.           write_bitmap (&image, stdout, outtype))
  106.       { exit (0); }
  107.     }
  108.  
  109.     /* Still call dup_clr to transform duplicate colors */
  110.     else if (mapname == NULL)
  111.     { if (dup_clr (NULL, &image) &&
  112.           write_bitmap (&image, stdout, outtype))
  113.       { exit (0); }
  114.     }
  115.     
  116.   }
  117.  
  118.   exit (1);
  119. }
  120.  
  121. /****************************************************************
  122.  * dup_clr: Copy colormap from input to output
  123.  *        Duplicates are removed, even if input image is NULL.
  124.  ****************************************************************/
  125.  
  126. dup_clr (input, output)
  127. FBM *input, *output;
  128. { register int i, j, clrlen;
  129.   register unsigned char *ic, *oc;
  130.   unsigned char *r, *g, *b;
  131.   unsigned char cmap[256];
  132.   int changes = 0, colors;
  133.  
  134.   /*-------- First copy over the new colormap --------*/
  135.   if (input)
  136.   { clrlen = input->hdr.clrlen;
  137.  
  138.     /* If output image has another color map, free the old one */
  139.     if (output->hdr.clrlen != input->hdr.clrlen)
  140.     { if (output->hdr.clrlen > input->hdr.clrlen)
  141.       { fprintf (stderr,
  142.         "fbpalet: warning, new color map is smaller (%d vs %d)\n",
  143.         input->hdr.clrlen, output->hdr.clrlen);
  144.       }
  145.  
  146.       if (output->hdr.clrlen > 0) free (output->cm);
  147.       output->cm = (unsigned char *) malloc (input->hdr.clrlen);
  148.     }
  149.  
  150.     /* Replace colormap */
  151.     output->hdr.clrlen = clrlen;
  152.     ic = input->cm;
  153.     oc = output->cm;
  154.   
  155.     for (i=0; i < clrlen; i++)
  156.     { *oc++ = *ic++; }
  157.   }
  158.  
  159.   /*-------- Now map duplicate colors to lowest index --------*/
  160.  
  161.   if (!removedup) return (1);
  162.  
  163.   clrlen = output->hdr.clrlen;
  164.   colors = clrlen / 3;
  165.  
  166.   /* CMAP will compute transform of body of image */
  167.   for (i=0; i<colors; i++) { cmap[i] = i; }
  168.  
  169.   r = output->cm;
  170.   g = r + colors;
  171.   b = g + colors;
  172.  
  173.   for (i=1; i<colors; i++)
  174.   { for (j=0; j<i; j++)
  175.     { if (r[i] == r[j] && g[i] == g[j] && b[i] == b[j])
  176.       { cmap[i] = j;
  177.  
  178.     if (debug)
  179.     { fprintf (stderr, "Dup: %3d -> %3d <%3d,%3d,%3d>\n",
  180.            i, j, r[i], g[i], b[i]);
  181.     }
  182.  
  183.         changes++;
  184.     break;
  185.       }
  186.     }
  187.   }
  188.  
  189.   if (changes)
  190.   { fprintf (stderr, "fbpalet: %d duplicated colormap entries\n", changes); }
  191.  
  192.   /* Change indices of duplicated colors */
  193.   if (changes)
  194.   { ic = output->bm;
  195.     oc = ic + output->hdr.plnlen * output->hdr.planes;
  196.  
  197.     for (; ic<oc; ic++)
  198.     { *ic = cmap[*ic]; }
  199.   }
  200.  
  201.   return (1);
  202. }
  203.