CollectPictColors.c

/*
    File:       CollectPictColors.c
 
    Contains:   This application uses the Picture Utilities package     
                and Jon Zap's KnowsPict application to demonstrate      
                two methods of collecting colors used by Pict       
                resources.  In this program, you'll see different
                results for each method.  With the Pict Util package,
                the routine, GetPictInfo, returns a colortable with                 
                the number of colors requested, but only the colors 
                used in the picture or it's pixmap(s) image data are
                stored in the requested colors.  As for the         
                remaining requested colors not used by the picture  
                but stored in the picture's pixmap(s) colortable,   
                they are set to black.  In Jon's application this   
                doesn't occur.  All the colors used by the picture, 
                including those stored in the picture's pixmap(s)   
                colortable are returned.  To use Jon's routines,    
                simply call CollectColors with the appropriate      
                parameters.                     
 
    Written by: Edgar Lee           
 
    Copyright:  Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                7/8/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                02/20/92    Edgar Lee       Created
 
*/
/* Constant Declarations */
#include "CLUTBuilder.h"
#include <MacTypes.h>
#include <MacMemory.h>
#include <Quickdraw.h>
#include <Fonts.h>
#include <Windows.h>
#include <Menus.h>
#include <TextEdit.h>
#include <Events.h>
#include <Dialogs.h>
#include <PictUtils.h>
#include <Resources.h>
 
 
#define WWIDTH      470
#define WHEIGHT     330
 
#define WLEFT       (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
#define WTOP        (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
 
void initMac();
void createWindow();
void drawPictureColors();
void drawColors();
void doEventLoop();
 
void main()
{
    initMac();
    
    createWindow();
    
    doEventLoop();
}
 
void initMac()
{
    MaxApplZone();
 
    InitGraf( &qd.thePort );
    InitFonts();
    InitWindows();
    InitMenus();
    TEInit();
    InitDialogs( nil );
    InitCursor();
    FlushEvents( 0, everyEvent );
}
 
void createWindow()
{
    Rect        rect;
    WindowPtr   window;
    
    SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
    window = NewCWindow( 0L, &rect, "\pCollect Pict Colors", true, documentProc,
                            (WindowPtr)-1L, true, 0L );                     
    SetPort( window );
    
    TextFont( kFontIDGeneva );
    TextSize( 9 );
    TextMode( srcCopy );
}
 
void drawPictureColors()
{
    Rect        rect;
    PicHandle   pict;
    CTabHandle  ctable;
    short       depth, directFlag;
    PictInfo    thePictInfo;
    CTabHandle  CollectColors();
    
    /* Load the pict resource. */
    pict = (PicHandle)GetResource( 'PICT', 128 );
    
    /* Make sure it's not purgeable. */
    HNoPurge( (Handle)pict );
    
    /* Set a black background. */
    SetRect( &rect, 0, 0, WWIDTH, WHEIGHT );
    ForeColor( blackColor );
    PaintRect( &rect );
    
    /* See what CollectColors returns. */
    ctable = CollectColors( pict, &depth, &directFlag );
    drawColors( ctable, 20, "\pCOLLECTCOLORS Colortable" );
    
    /* Draw the picture. */
    rect = (**pict).picFrame;
    OffsetRect( &rect, -rect.left + 160, -rect.top + 70 );
    DrawPicture( pict, &rect );
    
    /* Frame the picture. */
    InsetRect( &rect, -2, -2 );
    FrameRect( &rect );
    
    /* Now see what GetPictInfo returns. */
    GetPictInfo( pict, &thePictInfo, returnColorTable, 256, medianMethod, 0 );
    drawColors( thePictInfo.theColorTable, rect.right + 15, "\pGETPICTINFO Colortable" );
 
    /* Release the resource. */
    ReleaseResource( (Handle)pict );
    
    /* Release the colortable. */
    DisposeCTable( ctable );
}
 
void drawColors( ctable, offset, string )
CTabHandle  ctable;
int         offset;
Str255      string;
{
    int         i;
    int         col, row;
    Rect        rect;
 
    ForeColor( whiteColor );
    BackColor( blackColor );
        
    MoveTo( offset, 20 );
    DrawString( string );
    
    if (ctable != nil)
    {
        for (i = 0; i < 256; i++)
        {
            col = offset + ((i % 8) * 16);
            row = 30 + ((i / 8) * 9);
            
            SetRect( &rect, col, row, col + 10, row + 3 );
            RGBForeColor( &(**ctable).ctTable[i].rgb );
            PaintRect( &rect ); 
            
            ForeColor( whiteColor );
            InsetRect( &rect, -2, -2 );
            FrameRect( &rect );
        }
    }
    else
    {
        MoveTo( 12, offset );
        DrawString( "\p-Sorry could not load PICT resource." );
    }
}
 
void doEventLoop()
{
    EventRecord event;
    WindowPtr   window;
    short       clickArea;
    Rect        screenRect;
 
    for (;;)
    {
        if (WaitNextEvent( everyEvent, &event, 0, nil ))
        {
            if (event.what == mouseDown)
            {
                clickArea = FindWindow( event.where, &window );
                
                if (clickArea == inDrag)
                {
                    screenRect = (**GetGrayRgn()).rgnBBox;
                    DragWindow( window, event.where, &screenRect );
                }
                else if (clickArea == inContent)
                {
                    if (window != FrontWindow())
                        SelectWindow( window );
                }
                else if (clickArea == inGoAway)
                    if (TrackGoAway( window, event.where ))
                        return;
            }
            else if (event.what == updateEvt)
            {
                window = (WindowPtr)event.message;  
                SetPort( window );
                
                BeginUpdate( window );
                drawPictureColors();
                EndUpdate( window );
            }
        }
    }
}