[comp.sys.mac.programmer] Palette Manager question

CXT105@psuvm.psu.edu (Christopher Tate) (04/17/91)

I've got a bizarre little application with a bizarre little problem.
Basically what I'm doing is twiddling bits in an off-screen CGrafPort
directly and then blasting the result into a window with CopyBits.  So
far, so good; the CopyBits works, etc.  However, I've got a problem with
the Palette Manager.

I want to use (say) n shades of grey in my window; unfortunately, for rather
exotic reasons it is essential that these shades of grey be represented in
the offscreen CGrafPort by the pixel values 0 through n-1.  I can't seem to
get the Palette Manger to do what I want it to.  What I *want* is to set up
a palette with my desired colors in it, and have those colors appear when I
CopyBits the image into the window.  What's happening is that I'm getting
the first n colors in the default system palette, not my greyscales.

If I use the Color Manager to SetEntries() on the first n colors, then I get
exactly what I want; unfortunately, I'm not sure how to go about saving and
restoring the original color map if I do that -- it gets *very* ugly on the
rest of the screen!

Do I have to (somehow) associate the palette I want to use with the CGrafPort
as well as with the Window?  Do I have to muck about with CLUT's?  How should
I go about resetting the original palette/CLUT/whatever when my application
quits?  Etc....

-------
Christopher Tate                   |
                                   | "An operating system, like a government,
cxt105@psuvm.psu.edu               |  performs no useful function by itself."
{...}!psuvax1!psuvm.bitnet!cxt105  |
cxt105@psuvm.bitnet                |

gerhard@cs.arizona.edu (Gerhard Mehldau) (04/17/91)

[Since this is a FAQ, I thought I'd post the solution.]

> I've got a bizarre little application with a bizarre little problem.
> Basically what I'm doing is twiddling bits in an off-screen CGrafPort
> directly and then blasting the result into a window with CopyBits.  So
> far, so good; the CopyBits works, etc.  However, I've got a problem with
> the Palette Manager.

> I want to use (say) n shades of grey in my window; unfortunately, for rather
> exotic reasons it is essential that these shades of grey be represented in
> the offscreen CGrafPort by the pixel values 0 through n-1.  I can't seem to
> get the Palette Manger to do what I want it to.  What I *want* is to set up
> a palette with my desired colors in it, and have those colors appear when I
> CopyBits the image into the window.  What's happening is that I'm getting
> the first n colors in the default system palette, not my greyscales.

Sounds all too familiar...

What you need to do is create a 'clut' resource for your application, do

    CTabHandle gTable;

    gTable = GetCTable(kColorTable);
    SetPalette((WindowPtr)(-1),
               NewPalette((*gTable)->ctSize+1,gTable,pmTolerant+pmExplicit,0),
               TRUE);

at the beginning of your program (before you create any windows) and then
use "gTable" when you create your offscreen GWorlds -- and everything should
work as you expect it.  Note that instead of using the resource, you could
also create the CLUT on the fly.

> If I use the Color Manager to SetEntries() on the first n colors, then I get
> exactly what I want; unfortunately, I'm not sure how to go about saving and
> restoring the original color map if I do that -- it gets *very* ugly on the
> rest of the screen!

Don't do this -- you'll have to do it every time there is a context switch
(i.e., when a DA is called, or when your application is put in the background),
it looks _very_ bad, and it is a real hassle.

> Do I have to (somehow) associate the palette I want to use with the CGrafPort
> as well as with the Window?  Do I have to muck about with CLUT's?  How should
> I go about resetting the original palette/CLUT/whatever when my application
> quits?  Etc....

Yes, and the easiest way to do it is to use the new GWorld calls in 32-bit
QuickDraw -- all you need to do is pass in "gTable" when you create the
offscreen GWorld, as in

    Rect rect;
    GWorldPtr pixmap;

    NewGWorld(&pixmap,gDepth,&rect,gTable,NULL,0L);

This way you don't have to worry about saving/restoring CLUTs; 32-bit QuickDraw
automagically takes care of it for you.

Hope this helps.

- Gerhard

-- 
-> Gerhard Mehldau
   Dept. of Computer Science	internet: gerhard@cs.arizona.edu
   University of Arizona	uucp:     {cmcl2,noao,uunet}!arizona!gerhard
   Tucson, AZ 85721, U.S.A.	voice:    +1 (602) 621-4632

CXT105@psuvm.psu.edu (Christopher Tate) (04/17/91)

The answer I got was quite useful, as far as it goes, but....

In article <2019@optima.cs.arizona.edu>, gerhard@cs.arizona.edu (Gerhard
Mehldau) says:

> [ ... ] the easiest way to do it is to use the new GWorld calls in 32-bit
>QuickDraw -- all you need to do is pass in "gTable" when you create the
>offscreen GWorld, as in
>
>    Rect rect;
>    GWorldPtr pixmap;
>
>    NewGWorld(&pixmap,gDepth,&rect,gTable,NULL,0L);
>
>This way you don't have to worry about saving/restoring CLUTs; 32-bit
>QuickDraw
>automagically takes care of it for you.

Unfortunately, I can't find documentation for the 32-bit Quickdraw calls
*anywhere*.  Is this yet another truly useful facility which is only
documented in Inside Macintosh VI?  The only things I've been able to
scrounge up are some not-very-useful files under /pub/dts/mac/docs or some
such at apple.apple.com, and the tech note called "So many bitmaps, so
little time," which doesn't tell me what I need to know.

Where are the 32-bit Quickdraw calls documented, and how can I get hold of
them?  I'm getting pretty sick of all this "only available on the developer
CD's" stuff....

-------
Christopher Tate                   |
                                   | "Idle lawyers tend to become politicians,
cxt105@psuvm.psu.edu               |    so there is a certain social value
{...}!psuvax1!psuvm.bitnet!cxt105  |          in keeping them busy."
cxt105@psuvm.bitnet                |