home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / ppm / ppmtoacad.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  13KB  |  388 lines

  1. /*
  2.  
  3.       Convert a portable pixmap to an AutoCAD slide or DXB file
  4.  
  5.     Author:
  6.             John Walker
  7.             Autodesk SA
  8.             Avenue des Champs-Montants 14b
  9.             CH-2074 MARIN
  10.             Switzerland
  11.             Usenet: kelvin@Autodesk.com
  12.             Fax:    038/33 88 15
  13.             Voice:  038/33 76 33
  14.  
  15.     Permission  to  use, copy, modify, and distribute this software and
  16.     its documentation  for  any  purpose  and  without  fee  is  hereby
  17.     granted,  without any conditions or restrictions.  This software is
  18.     provided "as is" without express or implied warranty.
  19.  
  20. */
  21.  
  22. #include <stdio.h>
  23. #include "ppm.h"
  24. #include "ppmcmap.h"
  25.  
  26. #define TRUE     1
  27. #define FALSE    0
  28.  
  29. #define EOS     '\0'
  30.  
  31. #define MAXHIST         32767         /* Colour histogram maximum size */
  32.  
  33. static pixel **pixels;                /* Input pixel map */
  34. static colorhash_table cht;           /* Colour hash table */
  35. static int curcol = -1;               /* Current slide output colour */
  36. static int polymode = FALSE;          /* Output filled polygons ? */
  37. static int dxbmode = FALSE;           /* Output .dxb format ? */
  38. static int bgcol = -1;                /* Screen background colour */
  39. static double aspect = 1.0;           /* Pixel aspect ratio correction */
  40. static int gamut = 256;               /* Output colour gamut */
  41.  
  42. #include "autocad.h"                  /* AutoCAD standard colour assignments */
  43.  
  44. /* prototypes */
  45. static void outrun ARGS((int colour, int ysize, int y, int xstart, int xend));
  46. static void slideout ARGS((int xdots, int ydots, int ncolours,
  47.         unsigned char *red, unsigned char *green, unsigned char *blue));
  48.  
  49.  
  50. /*  OUTRUN  --  Output a run of pixels. */
  51.  
  52. static void outrun(colour, ysize, y, xstart, xend)
  53.   int colour, ysize, y, xstart, xend;
  54. {
  55.     if (colour == 0) {
  56.         return;                       /* Let screen background handle this */
  57.     }
  58.  
  59.     if (curcol != colour) {
  60.         if (dxbmode) {
  61.             putchar(136);
  62.             (void) pm_writelittleshort(stdout, colour);
  63.         } else {
  64.             (void) pm_writelittleshort(stdout, 0xFF00 | colour);
  65.         }
  66.         curcol = colour;
  67.     }
  68.     if (polymode) {
  69.         int v, yb = (ysize - y) + 1, yspan = 1;
  70.  
  71.         /* Since  we're emitting filled polygons,  let's scan downward
  72.            in the pixmap and see if we can extend the run on this line
  73.            vertically  as  well.   If  so, emit a polygon that handles
  74.            both the horizontal and vertical run and clear  the  pixels
  75.            in the subsequent lines to the background colour. */
  76.  
  77.         for (v = y + 1; v <= ysize; v++) {
  78.             int j, mismatch = FALSE;
  79.  
  80.             for (j = xstart; j <= xend; j++) {
  81.                 if (PPM_GETR(pixels[y][j]) != PPM_GETR(pixels[v][j])) {
  82.                     mismatch = TRUE;
  83.                     break;
  84.                 }
  85.             }
  86.             if (mismatch) {
  87.                 break;
  88.             }
  89.             for (j = xstart; j <= xend; j++) {
  90.                 PPM_ASSIGN(pixels[v][j], 0, 0, 0);
  91.             }
  92.         }
  93.         yspan = v - y;
  94.  
  95.         if (dxbmode) {
  96.             putchar(11);              /* Solid */
  97.             (void) pm_writelittleshort(
  98.                 stdout, (int) (xstart * aspect + 0.4999));
  99.             (void) pm_writelittleshort(stdout, yb);
  100.  
  101.             (void) pm_writelittleshort(
  102.                 stdout, (int) ((xend + 1) * aspect + 0.4999));
  103.             (void) pm_writelittleshort(stdout, yb);
  104.  
  105.             (void) pm_writelittleshort(
  106.                 stdout, (int) (xstart * aspect + 0.4999));
  107.             (void) pm_writelittleshort(stdout, yb - yspan);
  108.  
  109.             (void) pm_writelittleshort(
  110.                 stdout, (int) ((xend + 1) * aspect + 0.4999));
  111.             (void) pm_writelittleshort(stdout, yb - yspan);
  112.         } else {
  113.             (void) pm_writelittleshort(stdout, 0xFD00); /* Solid fill header */
  114.             (void) pm_writelittleshort(stdout, 4);      /* Vertices to follow */
  115.             (void) pm_writelittleshort(stdout, -2);     /* Fill type */
  116.  
  117.             (void) pm_writelittleshort(stdout, 0xFD00); /* Solid fill vertex */
  118.             (void) pm_writelittleshort(stdout, xstart);
  119.             (void) pm_writelittleshort(stdout, yb);
  120.  
  121.             (void) pm_writelittleshort(stdout, 0xFD00); /* Solid fill vertex */
  122.             (void) pm_writelittleshort(stdout, xend + 1);
  123.             (void) pm_writelittleshort(stdout, yb);
  124.  
  125.             (void) pm_writelittleshort(stdout, 0xFD00); /* Solid fill vertex */
  126.             (void) pm_writelittleshort(stdout, xend + 1);
  127.             (void) pm_writelittleshort(stdout, yb - yspan);
  128.  
  129.             (void) pm_writelittleshort(stdout, 0xFD00); /* Solid fill vertex */
  130.             (void) pm_writelittleshort(stdout, xstart);
  131.             (void) pm_writelittleshort(stdout, yb - yspan);
  132.  
  133.             (void) pm_writelittleshort(stdout, 0xFD00); /* Solid fill trailer */
  134.             (void) pm_writelittleshort(stdout, 4); /* Vertices that precede */
  135.             (void) pm_writelittleshort(stdout, -2);     /* Fill type */
  136.         }
  137.     } else {
  138.         (void) pm_writelittleshort(stdout, xstart);     /* Vector:  From X */
  139.         (void) pm_writelittleshort(stdout, ysize - y);  /*          From Y */
  140.         (void) pm_writelittleshort(stdout, xend);       /*          To   X */
  141.         (void) pm_writelittleshort(stdout, ysize - y);  /*          To   Y */
  142.     }
  143. }
  144.  
  145. /*  SLIDEOUT  --  Write an AutoCAD slide.  */
  146.  
  147. static void slideout(xdots, ydots, ncolours, red, green, blue)
  148.   int xdots, ydots, ncolours;
  149.   unsigned char *red, *green, *blue;
  150. {
  151.     static char sldhead[18] = "AutoCAD Slide\r\n\32";
  152.     static char dxbhead[20] = "AutoCAD DXB 1.0\r\n\32";
  153.     unsigned char *acadmap;
  154.     int i, xsize, ysize;
  155.  
  156.     /* If the user has specified a non-black screen background colour,
  157.        swap the screen background colour into colour  index  zero  and
  158.        move  black into the slot previously occupied by the background
  159.        colour. */
  160.  
  161.     if (bgcol > 0) {
  162.         acadcol[0][0] = acadcol[bgcol][0];
  163.         acadcol[0][1] = acadcol[bgcol][1];
  164.         acadcol[0][2] = acadcol[bgcol][2];
  165.         acadcol[bgcol][0] = acadcol[bgcol][1] = acadcol[bgcol][2] = 0;
  166.     }
  167.  
  168.     acadmap = (unsigned char *) pm_allocrow(ncolours, sizeof(unsigned char));
  169.     xsize = polymode ? xdots : (xdots - 1);
  170.     ysize = polymode ? ydots : (ydots - 1);
  171.     if (dxbmode) {
  172.         fwrite(dxbhead, 19, 1, stdout); /* DXB file header */
  173.         putchar(135);                 /* Number mode */
  174.         (void) pm_writelittleshort(stdout, 0);        /* ...short integers */
  175.     } else {
  176.         fwrite(sldhead, 17, 1, stdout); /* Slide file header */
  177.         putchar(86);                  /* Number format indicator */
  178.         putchar(2);                   /* File level number */
  179.         (void) pm_writelittleshort(stdout, xsize); /* Max X co-ordinate value */
  180.         (void) pm_writelittleshort(stdout, ysize); /* Max Y co-ordinate value */
  181.                                       /* Aspect ratio indicator */
  182.         (void) pm_writelittlelong(
  183.             stdout, (long) ((((double) xsize) / ysize) * aspect * 1E7));
  184.         (void) pm_writelittleshort(stdout, 2);        /* Polygon fill type */
  185.         (void) pm_writelittleshort(stdout, 0x1234);   /* Byte order indicator */
  186.     }
  187.  
  188.     /* Now  build  a  mapping  from  the  image's computed colour map
  189.        indices to the closest representation  of  each  colour  within
  190.        AutoCAD's colour repertoire. */
  191.  
  192.     for (i = 0; i < ncolours; i++) {
  193.         int best, j;
  194.         long dist = 3 * 256 * 256;
  195.  
  196.         for (j = 0; j < gamut; j++) {
  197.             long dr = red[i] - acadcol[j][0],
  198.                  dg = green[i] - acadcol[j][1],
  199.                  db = blue[i] - acadcol[j][2];
  200.             long tdist = dr * dr + dg * dg + db * db;
  201.  
  202.             if (tdist < dist) {
  203.                 dist = tdist;
  204.                 best = j;
  205.             }
  206.         }
  207.         acadmap[i] = best;
  208.     }
  209.  
  210.     /* Swoop  over the entire map and replace each  RGB pixel with its
  211.        closest  AutoCAD  colour  representation.   We  do  this  in  a
  212.        separate  pass  since it avoids repetitive mapping of pixels as
  213.        we examine them for compression. */
  214.  
  215.     for (i = 0; i < ydots; i++) {
  216.         int x;
  217.  
  218.         for (x = 0; x < xdots; x++) {
  219.             PPM_ASSIGN(pixels[i][x],
  220.                 acadmap[ppm_lookupcolor(cht, &pixels[i][x])], 0 ,0);
  221.         }
  222.     }
  223.  
  224.     /* Output a run-length encoded expression of the pixmap as AutoCAD
  225.        vectors. */
  226.  
  227.     for (i = 0; i < ydots; i++) {
  228.         int x, rx, rpix = -1, nrun = 0;
  229.  
  230.         for (x = 0; x < xdots; x++) {
  231.             int pix = PPM_GETR(pixels[i][x]);
  232.  
  233.             if (pix != rpix) {
  234.                 if (nrun > 0) {
  235.                     if (rpix > 0) {
  236.                         outrun(rpix, ydots - 1, i, rx, x - 1);
  237.                     }
  238.                 }
  239.                 rpix = pix;
  240.                 rx = x;
  241.                 nrun = 1;
  242.             }
  243.         }
  244.         if ((nrun > 0) && (rpix > 0)) {
  245.             outrun(rpix, ydots - 1, i, rx, xdots - 1);
  246.         }
  247.     }
  248.     if (dxbmode) {
  249.         putchar(0);                   /* DXB end sentinel */
  250.     } else {
  251.         (void) pm_writelittleshort(stdout, 0xFC00); /* End of file marker */
  252.     }
  253.     pm_freerow((char *) acadmap);
  254. }
  255.  
  256. /*  Main program.  */
  257.  
  258. int main(argc, argv)
  259.   int argc;
  260.   char* argv[];
  261. {
  262.     FILE *ifp;
  263.     int argn, rows, cols, ncolours, i;
  264.     int aspectspec = FALSE;
  265.     pixval maxval;
  266.     colorhist_vector chv;
  267.     unsigned char *Red, *Green, *Blue;
  268.     char *usage =
  269.         "[-poly] [-dxb] [-white] [-background <col>]\n\
  270.                   [-aspect <f>] [-8] [ppmfile]";
  271.  
  272.  
  273.     ppm_init(&argc, argv);
  274.  
  275.     argn = 1;
  276.     while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != EOS) {
  277.         if (pm_keymatch(argv[argn], "-dxb", 2)) {
  278.             dxbmode = polymode = TRUE;
  279.         } else if (pm_keymatch(argv[argn], "-poly", 2)) {
  280.             polymode = TRUE;
  281.         } else if (pm_keymatch(argv[argn], "-white", 2)) {
  282.             if (bgcol >= 0) {
  283.                 pm_error("already specified a background colour");
  284.             }
  285.             bgcol = 7;
  286.         } else if (pm_keymatch(argv[argn], "-background", 2)) {
  287.             if (bgcol >= 0) {
  288.                 pm_error("already specified a background colour");
  289.             }
  290.             argn++;
  291.             if ((argn == argc) || (sscanf(argv[argn], "%d", &bgcol) != 1))
  292.                 pm_usage(usage);
  293.             if (bgcol < 0) {
  294.                 pm_error("background colour must be >= 0 and <= 255");
  295.             }
  296.         } else if (pm_keymatch(argv[argn], "-aspect", 2)) {
  297.             if (aspectspec) {
  298.                 pm_error("already specified an aspect ratio");
  299.             }
  300.             argn++;
  301.             if ((argn == argc) || (sscanf(argv[argn], "%lf", &aspect) != 1))
  302.                 pm_usage(usage);
  303.             if (aspect <= 0.0) {
  304.                 pm_error("aspect ratio must be greater than 0");
  305.             }
  306.             aspectspec = TRUE;
  307.         } else if (pm_keymatch(argv[argn], "-8", 2)) {
  308.             gamut = 8;
  309.         } else {
  310.             pm_usage(usage);
  311.         }
  312.         argn++;
  313.     }
  314.  
  315.     if (argn < argc) {
  316.         ifp = pm_openr(argv[argn]);
  317.         argn++;
  318.     } else {
  319.         ifp = stdin;
  320.     }
  321.  
  322.     if (argn != argc) {
  323.         pm_usage(usage);
  324.     }
  325.  
  326.     pixels = ppm_readppm(ifp, &cols, &rows, &maxval);
  327.  
  328.     pm_close(ifp);
  329.  
  330.     /* Figure out the colormap.  Logic for squeezing depth to limit the
  331.        number of colours in the image was swiped from ppmquant.c. */
  332.  
  333.     while (TRUE) {
  334.         int row, col;
  335.         pixval newmaxval;
  336.         pixel *pP;
  337.  
  338.         pm_message("computing colourmap...");
  339.         chv = ppm_computecolorhist(pixels, cols, rows, MAXHIST, &ncolours);
  340.         if (chv != (colorhist_vector) 0)
  341.             break;
  342.         newmaxval = maxval / 2;
  343.         pm_message(
  344.         "scaling colours from maxval=%d to maxval=%d to improve clustering...",
  345.                    maxval, newmaxval );
  346.         for (row = 0; row < rows; ++row) {
  347.             for (col = 0, pP = pixels[row]; col < cols; ++col, ++pP) {
  348.                 PPM_DEPTH(*pP, *pP, maxval, newmaxval);
  349.             }
  350.         }
  351.         maxval = newmaxval;
  352.     }
  353.     pm_message("%d colours found", ncolours);
  354.  
  355.     /* Scale the colour map derived for the PPM file into one compatible
  356.        with AutoCAD's convention of 8 bit intensities. */
  357.  
  358.     if (maxval != 255) {
  359.         pm_message("maxval is not 255 - automatically rescaling colours");
  360.     }
  361.     Red = (unsigned char *) pm_allocrow(ncolours, sizeof(unsigned char));
  362.     Green = (unsigned char *) pm_allocrow(ncolours, sizeof(unsigned char));
  363.     Blue = (unsigned char *) pm_allocrow(ncolours, sizeof(unsigned char));
  364.  
  365.     for (i = 0; i < ncolours; ++i) {
  366.         if ( maxval == 255 ) {
  367.             Red[i] = PPM_GETR(chv[i].color);
  368.             Green[i] = PPM_GETG(chv[i].color);
  369.             Blue[i] = PPM_GETB( chv[i].color );
  370.         } else {
  371.             Red[i] = ((int) PPM_GETR(chv[i].color) * 255) / maxval;
  372.             Green[i] = ((int) PPM_GETG(chv[i].color) * 255) / maxval;
  373.             Blue[i] = ((int) PPM_GETB(chv[i].color) * 255) / maxval;
  374.         }
  375.     }
  376.  
  377.     /* And make a hash table for fast lookup. */
  378.  
  379.     cht = ppm_colorhisttocolorhash(chv, ncolours);
  380.     ppm_freecolorhist(chv);
  381.  
  382.     slideout(cols, rows, ncolours, Red, Green, Blue);
  383.     pm_freerow((char *) Red);
  384.     pm_freerow((char *) Green);
  385.     pm_freerow((char *) Blue);
  386.     exit(0);
  387. }
  388.