Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!uunet!wuarchive!texbell!texsun!newstop!sun!falk
From: falk@sun.Eng.Sun.COM (Ed Falk)
Newsgroups: comp.graphics
Subject: Re: loading rasterfiles on SunView canvases
Keywords: Raster, SunView, canvas
Message-ID: <121244@sun.Eng.Sun.COM>
Date: 14 Aug 89 02:10:16 GMT
References: <1425@hydra.gatech.EDU>
Organization: Sun Microsystems, Inc. - Mtn View, CA
Lines: 123

In article <1425@hydra.gatech.EDU>, mephdbo@prism.gatech.EDU (d. majumder) writes:
> 
> 
> i had a question. In SunView environment what are the
> steps to be taken while loading a set of rasterfiles onto
> a given canvas, one after another. the rasterfiles can
> be mono, 8, 16, ..256 color files. By steps I mean what
> aspects of the canvas should be set, reset, destroyed or 
> created (particularly the colormaps). I do not want to
> destroy the canvas or hide it. I was trying it out and 
> it seems earlier picture's colormap can screwup the next
> picture. I am still a suntools novice. <8-{

Well, if the colormaps are different from image to image, then there's
not much you can do about keeping one colormap from screwing up the
next image.  The best you can hope for is this sequence:

	clear canvas
	load new colormap
	load new image

although if color 0 is different between the two images, then the color
of the cleared canvas will still change when you load the new colormap.

Here's the sequence I use:

	struct rasterfile header ;
	colormap_t	colormap ;
	Pixrect		*newpr ;
	int		length ;
	char		line[CMS_NAMESIZE] ;

	window_release_event_lock(canvas) ;
	window_set(canvas, WIN_CURSOR, hglass_curs, 0) ;

	colormap.type = RMT_NONE;
	colormap.map[0] = 0;

	if (pr_load_header(infile, &header) ||
	    header.ras_depth > pw->pw_pixrect->pr_depth ||
	    pr_load_colormap(infile, &header, &colormap)) {
	{
	  /* handle error */
	}

	newpr = pr_load_image(infile, &header, &colormap);
	if (!newpr)
	{
	  /* handle error */
	}

	if (newpr->pr_depth == 1  &&  pw->pw_pixrect->pr_depth > 1 )
	{
		unsigned char r[2],g[2],b[2] ;
		pw_setcmsname(pw, CMS_MONOCHROME);
		cms_monochromeload(r,g,b) ;
		pw_putcolormap(pw, 0, 2, r, g, b);
	}
	else if (cm.type == RMT_EQUAL_RGB)
	{
		{
		  register int	j=colormap.length - 1 ;
		  length=1 ;
		  while(j >= 1)
		  {
		    length <<= 1 ;
		    j >>= 1 ;
		  }
		}
		sprintf(line,"show-%02d",length) ;
		pw_setcmsname(pw, line) ;
		pw_putcolormap(pw, 0, length,
			colormap.map[0], colormap.map[1], colormap.map[2]);
	}

	if( (int) window_get(canvas, CANVAS_WIDTH) != newpr->pr_size.x  ||
	    (int) window_get(canvas, CANVAS_HEIGHT) != newpr->pr_size.y )
	  window_set(canvas,
		CANVAS_WIDTH, newpr->pr_size.x,
		CANVAS_HEIGHT, newpr->pr_size.y, 0);
	}

	pw_write(pw, 0, 0, newpr->pr_size.x, newpr->pr_size.y, 
		PIX_SRC, newpr, 0, 0);

	window_set(canvas, WIN_CURSOR, norm_curs, 0) ;

notes:
 o the first two statements are at the start of the event proc that load the
   new image.  They allow for the fact that the image load may take a
   while and your tool shouldn't monopolize the input focus.

 o 'pw' is the canvas_pixwin() of the canvas.  pw->pw_pixrect->pr_depth
   is the number of bits deep this pixwin is.  Your program should
   refuse to load images deeper than this (as my sample code does).
   When loading a 1-bit image into an 8-bit canvas, you should set
   the colormap segment name to "monochrome" (this is defined
   in  as CMS_MONOCHROME.  cms_monochromeload
   is also defined in cms_mono.h.

 o the variable "length" is colormap.length rounded up to the nearest
   power of two.  This is done because rasterfile colormaps don't
   neccessarily have a power-of-two length but pw_putcolormap and
   pw_putattributes act funny if the colormap length is not a power of
   two.

 o next, if the new image is a different size than the current canvas size,
   I resize the canvas.  You could also resize the window if you want at
   this point.  (The application this code is stolen from uses scrollbars).

 o Finally, you also need to make sure that the first and last colors in
   your colormap are different from each other.  This is due to a feature
   in pixwins that insists that the first and last color are different, and
   if not, will change one of them.  What I usually do if start and finish
   colors are the same is to "adjust" the blue component of color zero by
   one before writing the table out.  This adjustment will not be visible
   to the naked eye.

-- 
		-ed falk, sun microsystems, sun!falk, falk@sun.com

  "If you wrapped yourself in the flag like George Bush does, you'd
  be worried about flag-burning too"