From steinmetz!uunet!leah.Albany.EDU!rsb584 Tue Feb  2 14:51:45 1988
Received:  by kbsvax.steinmetz (1.2/1.1x Steinmetz)
	 id AA01429; Tue, 2 Feb 88 12:12:14 est
Received: from LEAH.ALBANY.EDU by uunet.UU.NET (5.54/1.14) 
	id AA03430; Tue, 2 Feb 88 12:02:08 EST
Date: Tue, 2 Feb 88 12:04:29 EST
From: steinmetz!uunet!leah.Albany.EDU!rsb584 (Raymond S Brand)
Received: by leah.Albany.EDU (5.58/1.1)
	id AA13649; Tue, 2 Feb 88 12:04:29 EST
Message-Id: <8802021704.AA13649@leah.Albany.EDU>
To: beowulf!rsbx

>From paul@sgi.SGI.COM Mon Feb  1 14:45:55 1988
Path: leah!uwmcsd1!ig!agate!aurora!ames!sgi!paul
From: paul@sgi.SGI.COM (Paul Haeberli)
Newsgroups: comp.graphics
Subject: RGB to printer CMYK conversion
Message-ID: <10258@sgi.SGI.COM>
Date: 1 Feb 88 19:45:55 GMT
Organization: Silicon Graphics Inc, Mountain View, CA
Lines: 123

Someone asked about converting from RGB to prinyer CMYK.  Here is a
simple conversion technique.

			Paul Haeberli 

/*
 *	RGBtoCMYK -
 *		This program demonstrates an easy way to convert from
 *	RGB space to printer CMY and K.  This method does grey component 
 *	replacement, so achromatic parts of the image will be printed with 
 *	only black ink.  
 *
 *	R is red.	C is cyan.
 *	G is geen.	M is magenta.
 *	B is blue.	Y is yellow.
 *			K is black.
 *
 *		    Paul Haeberli / Silicon Graphics - 1987
 */
main()
{

/* print header */
    printf("r\tg\tb\t|\tc\tm\ty\tk\n");

/* transform the corners of the color cube */
    printf("-----------------------------------------------------------\n");
    printvals(0,0,0);
    printvals(0,0,255);
    printvals(0,255,0);
    printvals(0,255,255);
    printvals(255,0,0);
    printvals(255,0,255);
    printvals(255,255,0);
    printvals(255,255,255);

/* up one edge of the color cube */
    printf("-----------------------------------------------------------\n");
    printvals(0,0,0);
    printvals(0,0,32);
    printvals(0,0,64);
    printvals(0,0,128);
    printvals(0,0,192);
    printvals(0,0,255);

/* transform a few a chromatic colors */
    printf("-----------------------------------------------------------\n");
    printvals(0,0,0);
    printvals(1,1,1);
    printvals(16,16,16);
    printvals(32,32,32);
    printvals(64,64,64);
    printvals(128,128,128);
    printvals(192,192,192);
    printvals(255,255,255);
}

printvals(r,g,b)
int r, g, b; 
{
    int c,m,y,k;

    rgb_to_cmyk(r,g,b,&c,&m,&y,&k);
    printf("%d\t%d\t%d\t|\t%d\t%d\t%d\t%d\n",r,g,b,c,m,y,k);
}

/*
 *	rgb_to_cmyk -
 *		Convert from rgb to cmyk.  This implements grey component
 *	replacement. 
 *
 *	Inputs:
 *		r intensity 0 to 255
 *		g intensity 0 to 255
 *		b intensity 0 to 255
 *
 *	Outputs:
 *		c coverage 0 to 255
 *		m coverage 0 to 255
 *		y coverage 0 to 255
 *		k coverage 0 to 255
 *
 *
 *	An input value of rgb=[255,255,255] represents white.  When this is
 *	transformed to cmyk, we get cmyk=[0,0,0,0] which represents zero 
 *	coverage. 
 *
 *	An input value of rgb=[128,128,128] represents 50 percent grey.  When
 *	this is transformed to cmyk, we get cmyk=[0,0,0,127] which represents 
 *	zero coverage by cmy, and 50 percent coverage by black. 
 *
 */
rgb_to_cmyk(r,g,b,c,m,y,k)
int r, g, b;
int *c, *m, *y, *k;
{
    int i; 

/* i is the max of r, g, and b */
    i = 0;
    if(r>i)
	i = r;
    if(g>i)
	i = g;
    if(b>i)
	i = b;

/* if r, g and b are all zero then print full black */
    if(i == 0) {
	*c = 0;
	*m = 0;
	*y = 0;
	*k = 255;
	return;
    }
    r = (255*r)/i;
    g = (255*g)/i;
    b = (255*b)/i;
    *c = 255-r;
    *m = 255-g;
    *y = 255-b;
    *k = 255-i;
}


