Newsgroups: sci.image.processing
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!news.mathworks.com!uhog.mit.edu!bloom-beacon.mit.edu!eru.mt.luth.se!news.luth.se!sunic!sics.se!roland
From: roland@sics.se (Roland Karlsson)
Subject: Re: XV -viewer & Color
In-Reply-To: drosos@pcdrosos.abrdr.dreo.dnd.ca's message of 14 Feb 1995 18:19:59 GMT
Message-ID: <ROLAND.95Feb17151242@zith.sics.se>
Sender: news@sics.se (Dr News)
Organization: Swedish Institute of Computer Science, Kista
References: <3hq91u$elh@news.service.uci.edu> <3hqs8f$2ei@nouvelles.dreo.dnd.ca>
Date: Fri, 17 Feb 1995 14:12:42 GMT
Lines: 210


>can anyone tell me how to use xv to display a grey-scale image in some kind
>of rainbow scale. I.e. pixel value 1 is purple & pixel value 255 is red.
>The standard colormap option doesn't seem to do much.


Here is a pbm-plus program that do nearly what you want.  Actually, it
is more complicated than you want.  This programs assures that black
is black and white is white.  So, a hack is needed to simplify it.

--- ppmtone.1 ---

.TH ppmtone 1 "23 November 1993"
.IX ppmtone
.SH NAME
ppmtone - makes a toned picture from a portable pixmap
.SH SYNOPSIS
.B ppmsoft
.RB [ color1 ]
.RB [ color2 ]
.SH DESCRIPTION
Reads a portable pixmap from standard input.
Tones the picture and writes a portable pixmap on standard output.
.SH OPTIONS
.PP
.TP
.B color[12]
The color specifications tells how to tone the monochrome/color picture.
It can be of two different types. E.g., rgb:a0/10/00 or skyblue.
The color of a monochrome picture maps to black - darkest color - lightest
color - white.
.SH EXAMPLES
Toning a nice monochrome winter landscape.
.br
% ppmtone skyeblue tan4 < winter.pgm > tonedwinter.ppm

Toning a color picture of a cat, making it brown and gray.
.br
% ppmtone gray50   tan4 < cat.ppm    > tonedcat.ppm

.SH BUGS
Takes a long time to run.
.SH "SEE ALSO"
pbm(5) pgm(5) ppm(5) xco(1) xcolors(1)
.SH AUTHOR
Copyright (C) 1993 by Roland Karlsson (roland@sics.se)
.\" Permission to use, copy, modify, and distribute this software and its
.\" documentation for any purpose and without fee is hereby granted, provided
.\" that the above copyright notice appear in all copies and that both that
.\" copyright notice and this permission notice appear in supporting
.\" documentation.  This software is provided "as is" without express or
.\" implied warranty.

--- ppmtone.c ---

/* ppmgraycode.c - filter for converting to graycode
**
** Copyright (C) 1993 by Roland Karlsson (roland@sics.se)
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include <stdio.h>
#include <ppm.h>

/* Toning of a portable graymap.

   Compile thus:
   > gcc -O2 ppmtone.c -lppm -lpbm -lpgm

   The input  is a (max 256 level) portable pixmap.
   The output is a (max 256 level) portable pixmap.

   Options: see input argument processing below. */

main(argc,argv)
     int argc;
     char *argv[];
{
  int size, xsize, ysize, format, y, x;
  pixval maxvalue;
  pixel *in;
  pixel *out;
  pixval r[2], g[2], b[2];
  pixval m[2];
  int i, j;

  double R[4], G[4], B[4], M[4];
  char *Name[4];

  ppm_init(&argc,argv);
  ppm_readppminit(stdin,&xsize,&ysize,&maxvalue,&format);

  if (maxvalue > 255)
    {
      fprintf(stderr,"Number of levels are > 256\n");
      pm_close(stdin);
      exit(-1);
    }

  size = xsize*ysize;

  R[0] = G[0] = B[0] = M[0] = 0;	Name[0]="black";
  R[3] = G[3] = B[3] = M[3] = maxvalue;	Name[3]="white";

  switch(argc)
    {
    case 1:
      R[1] = G[1] = B[1] = M[1] = 0;	Name[1]="black";
      R[2] = G[2] = B[2] = M[2] = 0;	Name[2]="black";
      break;
    case 2:
      R[1] = G[1] = B[1] = M[1] = 0;	Name[1]="black";
      parsecolor(argv[1],&r[0], &g[0], &b[0],maxvalue);
      R[2] = r[0];
      G[2] = g[0];
      B[2] = b[0];
      M[2] = (R[2]+G[2]+B[2])/3;
      Name[2]=argv[1];
      break;
    case 3:
      for (j=0; j<2; j++)
	{
	  parsecolor(argv[j+1],&r[j], &g[j], &b[j],maxvalue);
	  m[j] = (r[j]+g[j]+b[j])/3;
	}
      i = m[1] < m[0];
      for (j=1; j<3; j++)
	{
	  R[j] = r[i];
	  G[j] = g[i];
	  B[j] = b[i];
	  M[j] = (R[j]+G[j]+B[j])/3;
	  Name[j]=argv[i+1];
	  i = !i;
	}
      break;
    default:
      fprintf(stderr,"usage: %s [color-1 [color-2]]\n", argv[0]);
      exit(-1);
    }

  for (j=0; j<4; j++)
    {
      fprintf(stderr,
	      "%d: (%3.0f+%3.0f+%3.0f)/3 = %6.2f (%2x %2x %2x  %2x) %s\n",
	      j, R[j], G[j], B[j], M[j],
	      (int)R[j], (int)G[j], (int)B[j], (int)M[j],
	      Name[j]);
    }

  ppm_writeppminit(stdout,xsize,ysize,maxvalue,0);
  in = ppm_allocrow(xsize);
  out = ppm_allocrow(xsize);

  for (y=0; y < ysize; y++)
    {
      ppm_readppmrow(stdin,in,xsize,maxvalue,format);

      for (x=0; x < xsize; x++)
	{
	  double val;

	  val = PPM_GETR(in[x]);
	  for (i=1; i<3; i++) if (val < M[i]) break;
	  val -= M[i-1];
	  PPM_GETR(out[x]) = R[i-1] + ( (R[i]-R[i-1])/(M[i]-M[i-1]) ) * val;

	  val = PPM_GETG(in[x]);
	  for (i=1; i<3; i++) if (val < M[i]) break;
	  val -= M[i-1];
	  PPM_GETG(out[x]) = G[i-1] + ( (G[i]-G[i-1])/(M[i]-M[i-1]) ) * val;

	  val = PPM_GETB(in[x]);
	  for (i=1; i<3; i++) if (val < M[i]) break;
	  val -= M[i-1];
	  PPM_GETB(out[x]) = B[i-1] + ( (B[i]-B[i-1])/(M[i]-M[i-1]) ) * val;
	}

      ppm_writeppmrow(stdout,out,xsize,maxvalue,0);
    }

  pm_close(stdin);
  pm_close(stdout);

  exit(0);
}

parsecolor(str, r, g, b, max)
     char *str;
     pixval *r, *g, *b, max;

{
  pixel p = ppm_parsecolor(str,max);

  *r = PPM_GETR(p);
  *g = PPM_GETG(p);
  *b = PPM_GETB(p);
}
--
Roland Karlsson (roland@sics.se)
----------------------------------------------------------------------------
SICS, PO Box 1263,       Tel: +46 8 752 15 40   Telex:    812 6154 7011 SICS
S-164 28 KISTA, SWEDEN   Fax: +46 8 751 72 30   http://www.sics.se/~roland/
----------------------------------------------------------------------------
