--- ppm/bmp.h.orig	Mon Oct  4 18:12:07 1993
+++ ppm/bmp.h	Mon Jan 20 00:00:00 1997
@@ -109,2 +112,4 @@
 	}
+
+	if( bitcount == 24 ) lenrgb=0;
 
--- ppm/bmptoppm.c.orig	Mon Oct  4 18:12:07 1993
+++ ppm/bmptoppm.c	Mon Jan 20 00:00:00 1997
@@ -350,2 +353,4 @@
 
+	if (cBitCount == 24) return 0;
+
 	for (i = 0; i < ncolors; i++)
@@ -387,12 +392,19 @@
 
-	if ((b = pm_bitinit(fp, "r")) == (BITSTREAM) 0)
+	if (cBitCount == 24)
 	{
-		return -1;
-	}
+		for (x = 0; x < cx; x++, row++)
+		{
+			pixval  b,g,r;
 
-	for (x = 0; x < cx; x++, row++)
-	{
-		unsigned long   v;
+			b = (pixval) GetByte(fp);
+			g = (pixval) GetByte(fp);
+			r = (pixval) GetByte(fp);
+			nbyte += 3;
 
-		if ((rc = pm_bitread(b, cBitCount, &v)) == -1)
+			PPM_ASSIGN(*row, r, g, b);
+		}
+	}
+	else 
+	{
+		if ((b = pm_bitinit(fp, "r")) == (BITSTREAM) 0)
 		{
@@ -400,12 +412,21 @@
 		}
-		nbyte += rc;
 
-		PPM_ASSIGN(*row, R[v], G[v], B[v]);
-	}
+		for (x = 0; x < cx; x++, row++)
+		{
+			unsigned long   v;
 
-	if ((rc = pm_bitfini(b)) != 0)
-	{
-		return -1;
-	}
+			if ((rc = pm_bitread(b, cBitCount, &v)) == -1)
+			{
+				return -1;
+			}
+			nbyte += rc;
+
+			PPM_ASSIGN(*row, R[v], G[v], B[v]);
+		}
 
+		if ((rc = pm_bitfini(b)) != 0)
+		{
+			return -1;
+		}
+	}
 	/*
--- ppm/ppmtobmp.c.orig	Mon Oct  4 18:12:36 1993
+++ ppm/ppmtobmp.c	Mon Jan 20 00:00:00 1997
@@ -77,3 +80,3 @@
 static void BMPEncode ARGS((FILE *fp, int class, int x, int y, pixel **pixels,
-    int colors, colorhash_table cht, pixval *R, pixval *G, pixval *B));
+    int bpp, int colors, colorhash_table cht, pixval *R, pixval *G, pixval *B));
 static void
@@ -255,15 +258,19 @@
 
-        for (i = 0; i < colors; i++)
-        {
-                nbyte += BMPwritergb(fp,class,R[i],G[i],B[i]);
-        }
+	if (bpp != 24)
+	{
 
-        ncolors = (1 << bpp);
-
-        for (; i < ncolors; i++)
-        {
-                nbyte += BMPwritergb(fp,class,0,0,0);
-        }
+		for (i = 0; i < colors; i++)
+		{
+			nbyte += BMPwritergb(fp,class,R[i],G[i],B[i]);
+		}
+
+		ncolors = (1 << bpp);
+
+		for (; i < ncolors; i++)
+		{
+			nbyte += BMPwritergb(fp,class,0,0,0);
+		}
+	}
 
-        return nbyte;
+	return nbyte;
 }
@@ -286,22 +293,34 @@
 
-        if ((b = pm_bitinit(fp, "w")) == (BITSTREAM) 0)
-        {
-                return -1;
-        }
-
-        for (x = 0; x < cx; x++, row++)
-        {
-                if ((rc = pm_bitwrite(b, bpp, ppm_lookupcolor(cht, row))) == -1)
-                {
-                        return -1;
-                }
-                nbyte += rc;
-        }
-
-        if ((rc = pm_bitfini(b)) == -1)
-        {
-                return -1;
-        }
-        nbyte += rc;
-
+	if (bpp != 24)
+	{
+		if ((b = pm_bitinit(fp, "w")) == (BITSTREAM) 0)
+		{
+			return -1;
+		}
+
+		for (x = 0; x < cx; x++, row++)
+		{
+			if ((rc = pm_bitwrite(b, bpp, ppm_lookupcolor(cht, row))) == -1)
+			{
+				return -1;
+			}
+			nbyte += rc;
+		}
+
+		if ((rc = pm_bitfini(b)) == -1)
+		{
+			return -1;
+		}
+		nbyte += rc;
+	}
+	else
+	{
+		for (x = 0; x < cx; x++, row++)
+		{
+			PutByte(fp, PPM_GETB(*row));
+			PutByte(fp, PPM_GETG(*row));
+			PutByte(fp, PPM_GETR(*row));
+			nbyte += 3;
+		}
+	}
         /*
@@ -396,3 +415,3 @@
 static void
-BMPEncode(fp, class, x, y, pixels, colors, cht, R, G, B)
+BMPEncode(fp, class, x, y, pixels, bpp, colors, cht, R, G, B)
         FILE           *fp;
@@ -402,2 +421,3 @@
         pixel         **pixels;
+	int		bpp;	/* bits per pixel */
         int             colors; /* number of valid entries in R,G,B */
@@ -408,32 +428,4 @@
 {
-        int             bpp;    /* bits per pixel */
         unsigned long   nbyte = 0;
 
-        bpp = colorstobpp(colors);
-
-        /*
-         * I have found empirically at least one BMP-displaying program
-         * that can't deal with (for instance) using 3 bits per pixel.
-         * I have seen no programs that can deal with using 3 bits per
-         * pixel.  I have seen programs which can deal with 1, 4, and
-         * 8 bits per pixel.
-         *
-         * Based on this, I adjust actual the number of bits per pixel
-         * as follows.  If anyone knows better, PLEASE tell me!
-         */
-        switch(bpp)
-        {
-        case 2:
-        case 3:
-                bpp = 4;
-                break;
-        case 5:
-        case 6:
-        case 7:
-                bpp = 8;
-                break;
-        }
-
-        pm_message("Using %d bits per pixel", bpp);
-
         nbyte += BMPwritefileheader(fp, class, bpp, x, y);
@@ -519,3 +511,3 @@
         FILE           *ifp = stdin;
-        char           *usage = "[-windows] [-os2] [ppmfile]";
+        char           *usage = "[-windows] [-os2] [-1/4/8/24bits] [ppmfile]";
         int             class = C_OS2;
@@ -526,2 +518,4 @@
         int             colors;
+	int		maxcolors = MAXCOLORS;
+        int		bpp = 0;
         int             i;
@@ -547,2 +541,10 @@
                         class = C_OS2;
+                else if (pm_keymatch(argv[argn], "-24bits", 3))
+                        bpp = 24, maxcolors = 256;
+                else if (pm_keymatch(argv[argn], "-8bits", 2))
+                        bpp = 8, maxcolors = 256;
+                else if (pm_keymatch(argv[argn], "-4bits", 2))
+                        bpp = 4, maxcolors = 16;
+                else if (pm_keymatch(argv[argn], "-1bit", 2))
+                        bpp = 1, maxcolors = 2;
                 else
@@ -587,36 +589,82 @@
         /* Figure out the colormap. */
-        pm_message("computing colormap...");
-        chv = ppm_computecolorhist(pixels, cols, rows, MAXCOLORS, &colors);
+	chv = (colorhist_vector) 0;
+        if(bpp != 24)
+	{
+		pm_message("computing colormap...");
+		chv = ppm_computecolorhist(pixels, cols, rows, maxcolors, &colors);
+	}
         if (chv == (colorhist_vector) 0)
-                pm_error("too many colors - try doing a 'ppmquant %d'"
-                        , MAXCOLORS);
-        pm_message("%d colors found", colors);
-
-        /*
-         * Now turn the ppm colormap into the appropriate GIF
-         * colormap.
-         */
-        if (maxval > 255)
-        {
-                pm_message("maxval is not 255 - automatically rescaling colors");
-        }
-        for (i = 0; i < colors; ++i)
-        {
-                if (maxval == 255)
-                {
-                        Red[i] = PPM_GETR(chv[i].color);
-                        Green[i] = PPM_GETG(chv[i].color);
-                        Blue[i] = PPM_GETB(chv[i].color);
-                }
-                else
-                {
-                        Red[i] = (pixval) PPM_GETR(chv[i].color) * 255 / maxval;
-                        Green[i] = (pixval) PPM_GETG(chv[i].color) * 255 / maxval;
-                        Blue[i] = (pixval) PPM_GETB(chv[i].color) * 255 / maxval;
-                }
-        }
+	{
+		if (bpp == 0)
+		{
+			pm_message("over 256 colors found");
+			bpp = 24;
+		}
+		else if (bpp != 24)
+			pm_error("too many colors - try doing a 'ppmquant %d'"
+				, maxcolors);
+	}
+	else
+	{
+		pm_message("%d colors found", colors);
+
+		/*
+		 * I have found empirically at least one BMP-displaying program
+		 * that can't deal with (for instance) using 3 bits per pixel.
+		 * I have seen no programs that can deal with using 3 bits per
+		 * pixel.  I have seen programs which can deal with 1, 4, and
+		 * 8 bits per pixel.
+		 *
+		 * Based on this, I adjust actual the number of bits per pixel
+		 * as follows.  If anyone knows better, PLEASE tell me!
+		 */
+
+		if (!bpp)
+		{
+			bpp = colorstobpp(colors);
+
+			switch(bpp)
+			{
+			case 2:
+			case 3:
+				bpp = 4;
+				break;
+			case 5:
+			case 6:
+			case 7:
+				bpp = 8;
+				break;
+			}
+		}
+
+		/*
+		 * Now turn the ppm colormap into the appropriate GIF
+		 * colormap.
+		 */
+		if (maxval > 255)
+		{
+			pm_message("maxval is not 255 - automatically rescaling colors");
+		}
+		for (i = 0; i < colors; ++i)
+		{
+			if (maxval == 255)
+			{
+				Red[i] = PPM_GETR(chv[i].color);
+				Green[i] = PPM_GETG(chv[i].color);
+				Blue[i] = PPM_GETB(chv[i].color);
+			}
+			else
+			{
+				Red[i] = (pixval) PPM_GETR(chv[i].color) * 255 / maxval;
+				Green[i] = (pixval) PPM_GETG(chv[i].color) * 255 / maxval;
+				Blue[i] = (pixval) PPM_GETB(chv[i].color) * 255 / maxval;
+			}
+		}
+
+		/* And make a hash table for fast lookup. */
+		cht = ppm_colorhisttocolorhash(chv, colors);
+		ppm_freecolorhist(chv);
+	}
 
-        /* And make a hash table for fast lookup. */
-        cht = ppm_colorhisttocolorhash(chv, colors);
-        ppm_freecolorhist(chv);
+	pm_message("Using %d bits per pixel", bpp);
 
@@ -624,3 +672,3 @@
         BMPEncode(stdout, class
-                , cols, rows, pixels, colors, cht
+                , cols, rows, pixels, bpp, colors, cht
                 ,Red, Green, Blue);
