source/CBasicApp.cp

//  This file contains the starter code for a basic PowerPlant project
/*
    Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
                ("Apple") in consideration of your agreement to the following terms, and your
                use, installation, modification or redistribution of this Apple software
                constitutes acceptance of these terms.  If you do not agree with these terms,
                please do not use, install, modify or redistribute this Apple software.
 
                In consideration of your agreement to abide by the following terms, and subject
                to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
                copyrights in this original Apple software (the "Apple Software"), to use,
                reproduce, modify and redistribute the Apple Software, with or without
                modifications, in source and/or binary forms; provided that if you redistribute
                the Apple Software in its entirety and without modifications, you must retain
                this notice and the following text and disclaimers in all such redistributions of
                the Apple Software.  Neither the name, trademarks, service marks or logos of
                Apple Computer, Inc. may be used to endorse or promote products derived from the
                Apple Software without specific prior written permission from Apple.  Except as
                expressly stated in this notice, no other rights or licenses, express or implied,
                are granted by Apple herein, including but not limited to any patent rights that
                may be infringed by your derivative works or by other works in which the Apple
                Software may be incorporated.
 
                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
                COMBINATION WITH YOUR PRODUCTS.
 
                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
#include "CBasicApp.h"
 
#include <LGrowZone.h>
#include <PP_Messages.h>
#include <PP_Resources.h>
#include <UDrawingState.h>
#include <UMemoryMgr.h>
#include <URegistrar.h>
#include <UNavServicesDialogs.h>
#include <LGAProgressBarImp.h>
 
#include <LWindow.h>
#include <LCaption.h>
#include "CTransformPane.h"
#include <LProgressBar.h>
 
#include "sWavelet_int.h"
 
#include "QTReadWriteJPEG.h"
 
#define WAVELET_32_RAW_WIDTH    640
#define WAVELET_32_RAW_HEIGHT   480
 
#define SUBTRACT_SHIFT          0
#define DO_TT6_TRACE            1
 
 
const PP_PowerPlant::ResIDT wind_SampleWindow = 128;    // EXAMPLE, create a new window
 
 
// ===========================================================================
//      ¥ Main Program
// ===========================================================================
 
 
#define SMALLTEST_TERM      (32*i+31)
 
#define ARRAYSIDE   64
#define OVERFLOW    8
 
#define BLOCKBYTES  (ARRAYSIDE*ARRAYSIDE*4*sizeof(short))
 
 
 
#define COMPARE_DIMENSION   16
#define ARRAYELEM           i
 
 
int main()
{       
    SetDebugThrow_(PP_PowerPlant::debugAction_Alert);   // Set Debugging options
    SetDebugSignal_(PP_PowerPlant::debugAction_Alert);
 
    PP_PowerPlant::InitializeHeap(3);                   // Initialize Memory Manager
                                                        // Parameter is number of Master Pointer
                                                        // blocks to allocate
    
    PP_PowerPlant::UQDGlobals::InitializeToolbox(&qd);  // Initialize standard Toolbox managers
    
    new PP_PowerPlant::LGrowZone(20000);                // Install a GrowZone function to catch
                                                        // low memory situations.
 
    CBasicApp   theApp;                                 // replace this with your App type
    theApp.Run();
    
    return 0;
}
 
 
// ---------------------------------------------------------------------------
//      ¥ CBasicApp
// ---------------------------------------------------------------------------
//  Constructor
 
CBasicApp::CBasicApp()
{
    RegisterClass_(PP_PowerPlant::LWindow);     // You must register each kind of
    RegisterClass_(PP_PowerPlant::LCaption);    // PowerPlant classes that you use
                                                // in your PPob resource.
    RegisterClass_(CTransformPane);
    RegisterClass_(CCopyBitsPane);
    RegisterClass_(LTextEditView);
    
    RegisterClass_(LCaption);
    RegisterClass_(LProgressBar);
    RegisterClass_(LGAProgressBarImp);
    RegisterClass_(LDialogBox);
    
    
    RegisterClass_(LCaption);
    RegisterClass_(LEditField);
    RegisterClass_(LPane);
    RegisterClass_(LScroller);
    RegisterClass_(LStdControl);
    RegisterClass_(LStdButton);
    RegisterClass_(LStdCheckBox);
    RegisterClass_(LStdRadioButton);
    RegisterClass_(LStdPopupMenu);
    RegisterClass_(LTextEditView);
    RegisterClass_(LView);
    RegisterClass_(LWindow);    
    
}
 
 
// ---------------------------------------------------------------------------
//      ¥ ~CBasicApp
// ---------------------------------------------------------------------------
//  Destructor
 
CBasicApp::~CBasicApp()
{
}
 
// ---------------------------------------------------------------------------
//      ¥ StartUp
// ---------------------------------------------------------------------------
//  This method lets you do something when the application starts up
//  without a document. For example, you could issue your own new command.
 
void
CBasicApp::StartUp()
{
    ObeyCommand(PP_PowerPlant::cmd_Open, nil);      // EXAMPLE, create a new window
}
 
// ---------------------------------------------------------------------------
//      ¥ ObeyCommand
// ---------------------------------------------------------------------------
//  This method lets the application respond to commands like Menu commands
 
Boolean
CBasicApp::ObeyCommand(
    PP_PowerPlant::CommandT inCommand,
    void                    *ioParam)
{
    Boolean     cmdHandled = true;
 
    switch (inCommand) {
 
        case cmd_openRaw:
            DoNew32Bit();
            break;
        
    
        // Handle command messages (defined in PP_Messages.h).
        case PP_PowerPlant::cmd_Open:
#if 1
            DoNewJPEG();
            break;
#else                                       
            PP_PowerPlant::LWindow  *theWindow =
                                    PP_PowerPlant::LWindow::CreateWindow(wind_SampleWindow, this);
            ThrowIfNil_(theWindow);
            
            // LWindow is not initially visible in PPob resource
            theWindow->Show();
            break;
#endif
        // Any that you don't handle, such as cmd_About and cmd_Quit,
        // will be passed up to LApplication
        default:
            cmdHandled = PP_PowerPlant::LApplication::ObeyCommand(inCommand, ioParam);
            break;
    }
    
    return cmdHandled;
}
 
// ---------------------------------------------------------------------------
//      ¥ FindCommandStatus
// ---------------------------------------------------------------------------
//  This method enables menu items.
 
void
CBasicApp::FindCommandStatus(
    PP_PowerPlant::CommandT inCommand,
    Boolean                 &outEnabled,
    Boolean                 &outUsesMark,
    PP_PowerPlant::Char16   &outMark,
    Str255                  outName)
{
 
    switch (inCommand) {
    
        // Return menu item status according to command messages.
        case PP_PowerPlant::cmd_Open:
        case cmd_openRaw:
            outEnabled = true;
            break;
 
        // Any that you don't handle, such as cmd_About and cmd_Quit,
        // will be passed up to LApplication
        default:
            LApplication::FindCommandStatus(inCommand, outEnabled,
                                                outUsesMark, outMark, outName);
            break;
    }
}
 
 
void CBasicApp::DoNewJPEG()
{
    UNavServicesDialogs::LFileChooser       fileChooser;
    OSType                                  types[] = {kQTFileTypeJPEG};
    LFileTypeList                           typelist(2, types);
    FSSpec                                  filespec;
    GWorldPtr                               theGWorld = nil;
    long                                    imageBytes;
    long                                    width, height;
    long                                    paddedWidth, paddedHeight;
    long                                    requiredWaveletMultiple;
    
    if (fileChooser.AskChooseOneFile(typelist, filespec)) {
    
        FailOSErr_(QTJPEG_ReadJPEG (filespec, &theGWorld));
    
        width = theGWorld->portRect.right-theGWorld->portRect.left;
        height = theGWorld->portRect.bottom-theGWorld->portRect.top;
        
        imageBytes =    (height) * (((**(theGWorld->portPixMap)).rowBytes) & 0x7fff) ;
        
        StHandleBlock       pixHandle(4);
        
        PixMapHandle        gworldPixMap = GetGWorldPixMap(theGWorld);
 
        LockPixels(gworldPixMap);
        
        Ptr pixbase = GetPixBaseAddr(gworldPixMap);     
 
        requiredWaveletMultiple = 4 << 3;       // 8 shifted by wavelet depth
 
        paddedWidth = ((width + (requiredWaveletMultiple-1)) / requiredWaveletMultiple) * requiredWaveletMultiple;
        paddedHeight = ((height + (requiredWaveletMultiple-1)) / requiredWaveletMultiple) * requiredWaveletMultiple;
 
        PadXY_32Bit((unsigned long*)pixbase, pixHandle, (**theGWorld->portPixMap).rowBytes & 0x7fff, width, height, paddedWidth, paddedHeight);
        
        PixMapHandle pmh = NewPixMap();
        
        CopyPixMap(theGWorld->portPixMap, pmh);
        
        CTabHandle saveCT = (**pmh).pmTable;
        (**pmh).pmTable = nil;
        
        NewWindowWithData(**pmh, pixHandle, paddedWidth, paddedHeight);
        
        (**pmh).pmTable = saveCT;
        
        DisposePixMap(pmh);
        
        pixHandle.Release();    // we no longer own it
        
        DisposeGWorld(theGWorld);
    }
}
 
 
 
void CBasicApp::DoNew32Bit()
{
    UNavServicesDialogs::LFileChooser       fileChooser;
    OSType                                  types[] = {'BINA', '????'};
    LFileTypeList                           typelist(2, types);
    FSSpec                                  filespec;
 
    if (fileChooser.AskChooseOneFile(typelist, filespec)) {
        LFile   datafile(filespec);
                        
        PixMap  map;
        
        map.baseAddr = nil;
        map.rowBytes = 0x8000 | (WAVELET_32_RAW_WIDTH*4);
        map.bounds.top = 0;
        map.bounds.bottom = WAVELET_32_RAW_HEIGHT;
        map.bounds.left = 0;
        map.bounds.right = WAVELET_32_RAW_WIDTH;
        map.pmVersion = 4;
        map.packType = 0;
        map.packSize = 0;
        map.hRes = 0x00480000;
        map.vRes = 0x00480000;
        map.pixelType = RGBDirect;
        map.pixelSize = 32;
        map.cmpCount = 3;
        map.cmpSize = 8;
        map.planeBytes = 0;
        map.pmTable = nil;
        map.pmReserved = 0;
 
        {
            StHandleBlock   datahandle;
 
            datafile.OpenDataFork(fsRdPerm);
            datahandle.Adopt(datafile.ReadDataFork());
            
            map.rowBytes = 0x8000 | (WAVELET_32_RAW_WIDTH+16)*4;
            
            NewWindowWithData(map, datahandle, WAVELET_32_RAW_WIDTH, WAVELET_32_RAW_HEIGHT);
            
            datahandle.Release();   // we no longer own it, the pane does
        }       
 
        
    }
 
}
 
 
void CBasicApp::NewWindowWithData(const PixMap &pmap, Handle data, long paddedX, long paddedY)
{
    long        width = pmap.bounds.right-pmap.bounds.left;
    long        height = pmap.bounds.bottom-pmap.bounds.top;
 
    LWindow     *pWin = PP_PowerPlant::LWindow::CreateWindow(wind_SampleWindow, this);
    ThrowIfNil_(pWin);
    
    CCopyBitsPane* pBitsPane = dynamic_cast<CCopyBitsPane*>(pWin->FindPaneByID('BMap'));
    FailNIL_(pBitsPane);
    
    pBitsPane->SetPixelData(pmap, data, paddedX, paddedY);
 
    LCommander::SwitchTarget(pBitsPane);
 
    pWin->ResizeWindowTo(width, height);
 
    // LWindow is not initially visible in PPob resource
    pWin->Show();
    
}
 
 
void
 CBasicApp::ShowAboutBox()
{
    PP_PowerPlant::LWindow  *theWindow =
                            PP_PowerPlant::LWindow::CreateWindow(1102, this);
    ThrowIfNil_(theWindow);
    
    // LWindow is not initially visible in PPob resource
    theWindow->Show();
}