MoofEncoder/PICTEncode.cp

/*
    File:       PICTEncode.cp
 
    Contains:   
 
    Written by: Timothy Carroll 
 
    Copyright:  Copyright © 1996-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/1/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                4/2/97                      Timothy Carroll PICTEncode would double-dispose
                                            of the PICT. I wasn't NULLing out the PICT local
                                            after disposing of it explicitly in the loop.
                                            Spotlight found this.
                2/24/97     Timothy Carroll     Now explicitly include main.h
                8/15/96     Timothy Carroll Initial Release
                
 
*/
#include <Resources.h>
 
#include "Main.h"
#include "TGraphic.h"
#include "PictEncode.h"
 
 
OSStatus PICTEncode (short inputFileResNum, short outputFileResNum)
{
    OSStatus        theErr;
 
    UInt16          numPicts, loop;
 
    // we pass these to GetResInfo so that we can get the actual resource ID.
    SInt16          resID;
    ResType         resType;
    Str255          resName;
 
    // Holds the last value on the resource chain since we change the top resource a lot
    SInt16          saveResNum;
 
    // Temporarily holds the resource so we can get information on it
    Handle          pict = NULL;
    TGraphic        *compiled = NULL;
        
    saveResNum = CurResFile();
 
    UseResFile (inputFileResNum);
 
 
    // First thing is to copy the color table resource to the output.
    theErr = CopyResource (inputFileResNum, outputFileResNum,'clut', kAppColorTableResID);
    FAIL_OSERR (theErr, "\pFailed to copy the color table to the destination file")
 
 
    // get the color table
    gAppColorTable = GetCTable( kAppColorTableResID );
    FAIL_NIL (gAppColorTable, "\pFailed to open the color table")
 
    // determine the number of PICT resources
    numPicts = Count1Resources( 'PICT' );
 
    
    // get each one,
    for( loop = 1; loop <= numPicts; loop++ )
    {
        // load the pict
        UseResFile (inputFileResNum);
                    
        // we'll get the pict first so that we can get the information out of it.
        pict = Get1IndResource( 'PICT', loop );
        theErr = ResError();
        FAIL_NIL (pict, "\pFailed to load the picture")
        FAIL_OSERR (theErr, "\pFailed to load the picture")
            
        // determine its id and name
        GetResInfo( pict, &resID, &resType, resName );
        theErr = ResError();
        FAIL_OSERR( theErr, "\pFailed to get info on the resource")
        
        ReleaseResource (pict);
        pict = NULL; // We're done with this PICT, NULL it out so we don't double dispose
        
        
        // Okay, we know the PICT's resID, build the compiled graphic and write it to the output file.
        compiled = TGraphic::NewGraphic (resID);
        FAIL_NIL (compiled, "\pFailed to compile the TGraphic")
        
        UseResFile (outputFileResNum);
        
        compiled->WriteToGraphicResource();
        compiled->WriteToPICTResource();
        compiled->DisposeReference();
        
        compiled = NULL; // Make sure we don't double dispose of the compiled sprite
 
    }
    // restore the previous port and device
    
    goto cleanup;
    
error:
    
    if (theErr == noErr)
        theErr = paramErr;
cleanup:
    
    UseResFile (saveResNum);
    
    if (pict)
        ReleaseResource (pict);
    if (compiled)
        compiled->DisposeReference();
    if (gAppColorTable != NULL)
        DisposeCTable (gAppColorTable);
    gAppColorTable = NULL;
    return theErr;
}