home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / netpbm_src.lzh / NETPBM / PPM / ppmtomap.c < prev    next >
C/C++ Source or Header  |  1996-11-18  |  4KB  |  148 lines

  1. /* ppmtomap.c - read a portable pixmap and produce a minimal size portable
  2. ** pixmap containing all colors.
  3. **
  4. ** Based on ppmtogif.c
  5. **
  6. ** By Marcel Wijkstra < wijkstra@fwi.uva.nl>
  7. **
  8. **
  9. ** Copyright (C) 1989 by Jef Poskanzer.
  10. **
  11. ** Permission to use, copy, modify, and distribute this software and its
  12. ** documentation for any purpose and without fee is hereby granted, provided
  13. ** that the above copyright notice appear in all copies and that both that
  14. ** copyright notice and this permission notice appear in supporting
  15. ** documentation.  This software is provided "as is" without express or
  16. ** implied warranty.
  17. **
  18. ** The Graphics Interchange Format(c) is the Copyright property of
  19. ** CompuServe Incorporated.  GIF(sm) is a Service Mark property of
  20. ** CompuServe Incorporated.
  21. */
  22.  
  23. #include <math.h>
  24. #include "ppm.h"
  25. #include "ppmcmap.h"
  26.  
  27. #define MAXCOLORS 65536 /* May be smaller, may be larger (?) */
  28.  
  29. static int Red[MAXCOLORS],Green[MAXCOLORS],Blue[MAXCOLORS];
  30.  
  31. int
  32. main( argc, argv )
  33.     int argc;
  34.     char* argv[];
  35.     {
  36.     FILE* ifp;
  37.     pixel **pixels;
  38.     pixel *pixelrow;
  39.     int colors;
  40.     int argn, rows, cols, i,j,k,l, BitsPerPixel;
  41.     int sort, square;
  42.     char *mapfile;
  43.     pixval maxval;
  44.     colorhist_vector chv;
  45.     char* usage = "[-sort] [-square] [ppmfile]";
  46.  
  47.  
  48.     ppm_init( &argc, argv );
  49.  
  50.     argn = 1;
  51.     sort = 0;
  52.     square = 0;
  53.  
  54.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  55.         {
  56.         if ( pm_keymatch( argv[argn], "-sort", 3 ) )
  57.             sort = 1;
  58.         else if ( pm_keymatch( argv[argn], "-square", 3 ) )
  59.             square = 1;
  60.         else
  61.             pm_usage( usage );
  62.         ++argn;
  63.         }
  64.  
  65.     if ( argn < argc )
  66.     {
  67.     ifp = pm_openr( argv[argn] );
  68.     ++argn;
  69.     }
  70.     else
  71.     ifp = stdin;
  72.  
  73.     if ( argn != argc )
  74.     pm_usage( usage );
  75.  
  76.     pixels = ppm_readppm( ifp, &cols, &rows, &maxval );
  77.  
  78.     pm_close( ifp );
  79.  
  80.     /* Figure out the colormap. */
  81.     chv = ppm_computecolorhist( pixels, cols, rows, MAXCOLORS, &colors );
  82.  
  83.     if ( chv == (colorhist_vector) 0 )
  84.     pm_error(
  85.         "too many colors - try doing a 'ppmquant %d'", MAXCOLORS );
  86.     pm_message( "%d colors found", colors );
  87.  
  88.     for ( i = 0; i < colors; ++i )
  89.         {
  90.             Red[i] = PPM_GETR( chv[i].color );
  91.             Green[i] = PPM_GETG( chv[i].color );
  92.             Blue[i] = PPM_GETB( chv[i].color );
  93.         }
  94.  
  95.     /* Sort the colormap */
  96.     if (sort) {
  97.       pm_message("sorting colormap");
  98.       for (i=0;i<colors;i++)
  99.         for (j=i+1;j<colors;j++)
  100.       l=maxval+1;
  101.           if (((Red[i]*l)+Green[i])*l+Blue[i] >
  102.               ((Red[j]*l)+Green[j])*l+Blue[j]) {
  103.             k=Red[i]; Red[i]=Red[j]; Red[j]=k;
  104.             k=Green[i]; Green[i]=Green[j]; Green[j]=k;
  105.             k=Blue[i]; Blue[i]=Blue[j]; Blue[j]=k; } }
  106.  
  107. #if 0
  108.     ppm_init(&argc, argv);
  109. #endif
  110.  
  111.     if (!square) {
  112.  
  113.       pixelrow = ((pixel*) pm_allocrow( colors, sizeof(pixel) ));
  114.  
  115.       for (i=0;i<colors;i++)
  116.         PPM_ASSIGN(pixelrow[i],
  117.         (pixval)Red[i], (pixval)Green[i], (pixval)Blue[i]);
  118.  
  119.       ppm_writeppminit(stdout, colors, 1, maxval, 0);
  120.       ppm_writeppmrow(stdout, pixelrow, colors, maxval, 0); }
  121.  
  122.     else {
  123.  
  124.       cols = (int)sqrt((float)colors);
  125.       rows = colors/cols;
  126.       ppm_writeppminit(stdout, cols, rows, maxval, 0);
  127.       pixelrow = ((pixel*) pm_allocrow(cols, sizeof(pixel) ));
  128.  
  129.       for (i=0;i<rows;i++) {
  130.         for (j=0;j<cols;j++) {
  131.       k=i*cols+j;
  132.           PPM_ASSIGN(pixelrow[j],
  133.         (pixval)Red[k], (pixval)Green[k], (pixval)Blue[k]); }
  134.         ppm_writeppmrow(stdout, pixelrow, cols, maxval, 0); }
  135.  
  136.       if (colors%cols) {
  137.     k=i*cols;
  138.         for (j=0;j<colors%cols;j++,k++)
  139.           PPM_ASSIGN(pixelrow[j],
  140.         (pixval)Red[k], (pixval)Green[k], (pixval)Blue[k]);
  141.         for (;j<cols;j++)
  142.           PPM_ASSIGN(pixelrow[j],
  143.         (pixval)Red[k], (pixval)Green[k], (pixval)Blue[k]);
  144.         ppm_writeppmrow(stdout, pixelrow, cols, maxval, 0); } }
  145.       
  146.     pm_close(stdout);
  147. }
  148.