From: avm@doc.ic.ac.uk (Alex V Melidis)
Newsgroups: comp.sys.mac.programmer
Subject: CopyBits() : pmTable field initialisation for PixMap
Date: 18 Nov 1992 14:48:30 -0000
Organization: Department of Computing, Imperial College, University of London, UK.
Distribution: world
NNTP-Posting-Host: oak22.doc.ic.ac.uk



This question is about copying bits from an off-screen 1 or 8-bit deep
PixMap to a window using the CopyBits() call in ThinkC.

The situation is as follows : 
- The image contained in the PixMap exists in memory before the PixMap
  is created and is updated without using QuickDraw routines, hence
  there is no off-screen drawing TO a PixMap and there is no color
  grafport associated with it.
  A color window is created (with NewCWindow()) for displaying the contents
  of that image. The image is displayed by using CopyBits() between the
  off-screen PixMap and the window's PixMap.
  
  PROBLEM : After using NewPixMap() to create the off-screen pixmap
 	    and setting some of its fields to appropriate values (code
	    follows) I need to set the pmTable field to the color table
	    to be used for the off-screen PixMap :
			:
			:
	pmap = NewPixMap();
	HLock(pmap);
	(*pmap)->baseAddr = (Ptr) start_of_image_data;
	(*pmap)->rowBytes = (precision == 1 ?
			     ((width - 1) / 32 + 1) * 4 :
			     0x8000 | width);
	/* width is the no of pixels in each row of the image */
	SetRect((*pmap)->bounds, 0, 0, width, height);
	/* height is no of rows in image */

	/* AND NOW THE PROBLEM */

	if ((*pmap)->rowBytes & 0x8000)
	{
		(*pmap)->pmTable = ..................... ???
	}

	HUnlock(pmap);
			:
			:
	/* for this I have so far tried two alternatives :

	1) get color table from PixMap of window to which the bits
	   will be copied

	   (*pmap)->pmTable = (*(window_record->port.portPixMap))->pmTable;

	where window_record is a CWindowPeek
	
	2) get color table from main device

	  main_device = GetMainDevice();
	  HLock(main_device);
	  (*pmap)->pmTable = (*((*main_device)->gdPmap))->pmTable;
	  HUnlock(main_device);

	where main_device is a GDHandle

	but none of these ways works consistently : some times CopyBits()
	crashes taking the application with it (but the Mac survives), but
	the usual thing is for the Mac to freeze completely (with some
	color changes to the screen sometimes).

	The code for CopyBits() is
			:
			:
	win_pmap = window_record->port.portPixMap;
	SelectWindow(window_record);
	GetPort(&saveport);
	SetPort(window);
        HLock(pmap);
	HLock(win_pmap);

	if ((*pmap)->rowBytes & 0x8000)
	{
		CopyBits(*pmap, *win_pmap,
			 &(*pmap)->bounds, &(*pmap)->bounds,
			 transparent, NULL);
	}
	else
	{
		CopyBits(*pmap, *win_pmap,
                         &(*pmap)->bounds, &(*pmap)->bounds,
                         srcCopy, NULL);
	}
	
	HUnlock(win_pmap);
	HUnlock(pmap);
	SetPort(saveport);
			:
			:

	Please note that omission of setting the pmTable field results in
	a working program that uses greyscales for color.

	I will be very grateful for any helpful hint, as I am still new to
	ToolBox programming and quite stuck.
	
	If I have not provided enough information in the above description,
	please let me know. I may be also contacted by email.

				Thanks in advance,
						Alex.


-- 
Alexander V Melidis,		INTERNET : avm@doc.ic.ac.uk
Department Of Computing,
Imperial College,
LONDON, SW7 2BZ