ProjectBuilder (OS X)/Rotate.c

/*
    File:       Rotate.c
 
    Contains:   Rotates a bitmap 90 degrees
 
    Written by:     
 
    Copyright:  Copyright © 1984-1999 by Apple Computer, Inc., All Rights Reserved.
 
    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.
                
    Change History (most recent first):
        6/2003      MK          Updated for ProjectBuilder
        
        08/2000     JM              Carbonized, non-Carbon code is commented out
                                                        for demonstration purposes.
        7/14/1999   KG              Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
#include "CarbonPrefix.h"
 
#include <Carbon/Carbon.h>
 
#define BIT00       0x01
#define BIT01       0x02
#define BIT02       0x04
#define BIT03       0x08
#define BIT04       0x10
#define BIT05       0x20
#define BIT06       0x40
#define BIT07       0x80
 
void rotate();
 
BitMap      *macBitMap;
 
void rotate()
{
    Ptr             srcPtr, destPtr, destColPtr, myBaseAddr;
    BitMap          *newBitmap;
    short           destRowBytes, bitchar, srcByte, srcH, srcV, srccols, srcrows, i;
    Rect            destRect;
 
    /* rotate boundary rectangle */
    srcH = macBitMap->bounds.right - macBitMap->bounds.left;
    srcV = macBitMap->bounds.bottom - macBitMap->bounds.top;
    SetRect(&destRect, macBitMap->bounds.left, macBitMap->bounds.top,
            macBitMap->bounds.left+srcV, macBitMap->bounds.top+srcH);
 
    /* allocate destination buffer */
    destRowBytes  = (srcV+7) >> 3;
    newBitmap = (BitMap *) NewPtrClear(sizeof(BitMap));
    myBaseAddr = (Ptr) NewPtrClear(destRowBytes*srcH);
 
    /* set-up src to rotated destination scan */
    srccols = macBitMap->rowBytes;
    srcrows = srcV;
    srcPtr = macBitMap->baseAddr;
    bitchar = BIT00;
    destColPtr= myBaseAddr + destRowBytes - 1;
    destPtr   = destColPtr;
 
    /* scan src row and rotate into dest col */
    while (srcrows-- > 0) {
        for (i=0; i<srccols; i++) {
            srcByte = *(srcPtr++);
            if (srcByte & BIT07) *destPtr |= bitchar;
            destPtr += destRowBytes;
            if (srcByte & BIT06) *destPtr |= bitchar;
            destPtr += destRowBytes;
            if (srcByte & BIT05) *destPtr |= bitchar;
            destPtr += destRowBytes;
            if (srcByte & BIT04) *destPtr |= bitchar;
            destPtr += destRowBytes;
            if (srcByte & BIT03) *destPtr |= bitchar;
            destPtr += destRowBytes;
            if (srcByte & BIT02) *destPtr |= bitchar;
            destPtr += destRowBytes;
            if (srcByte & BIT01) *destPtr |= bitchar;
            destPtr += destRowBytes;
            if (srcByte & BIT00) *destPtr |= bitchar;
            destPtr += destRowBytes;
        }
        if (bitchar==BIT07) {
            bitchar = BIT00;
            destColPtr--;
        } else {
            bitchar <<= 1;
        }
 
        destPtr = destColPtr;
    }
    
    /* remove src bitmap (portbits for offscreen port) */
    DisposePtr((Ptr)macBitMap->baseAddr);
    DisposePtr((Ptr)macBitMap);
    
    /* store new src in object */
    macBitMap   = (BitMap *)newBitmap;
    macBitMap->baseAddr = (Ptr) myBaseAddr;
    macBitMap->rowBytes = destRowBytes;
    macBitMap->bounds   = destRect;
}
 
int main()
{
    WindowPtr       mainWinPtr;
    OSErr           error;
    //SysEnvRec     theWorld;       //not available in carbon
    long            result;         //used to check QD Version
    Rect            windRect, ovalRect, myBounds;
    short           myRowBytes;
    RgnHandle       rgnHandle = NewRgn();
    
    /* Make sure ColorQD exists. */
    /*error = SysEnvirons(1, &theWorld);
    if (theWorld.hasColorQD == false) {
        SysBeep(50);
        ExitToShell();
    }*/
    
    error = Gestalt(gestaltQuickdrawVersion, &result);
    if (error != noErr || result < gestalt8BitQD) {
        SysBeep(50);
        ExitToShell();
    }
    
    /* Initialize all the needed managers. */
    /*InitGraf(&qd.thePort);
    InitFonts();
    InitWindows();
    InitMenus();
    TEInit();
    InitDialogs(nil);*/
    InitCursor();
 
    /* Define output window with an inset clip region. */
    SetRect(&windRect, 100, 100, 404, 404);
    mainWinPtr = NewWindow(nil, &windRect, "\pJohn", true, documentProc, (WindowPtr) -1, false, 0);
    //SetPort(mainWinPtr);
    SetPortWindowPort( mainWinPtr );
 
    SetRect(&windRect, 0, 0, 304, 304);
    EraseRect(&windRect);
    SetRect(&ovalRect, 15, 15, 60, 120);
    PaintOval(&ovalRect);
    MoveTo(10,140);
    DrawString("\pHi there.");
    MoveTo(0,0);
    LineTo(303,303);
    MoveTo(0,303);
    LineTo(303,0);
 
    /* rotate boundary rectangle */
    myBounds = windRect;
    myRowBytes = (myBounds.right - myBounds.left + 7) >> 3;
    macBitMap = (BitMap *) NewPtrClear(sizeof(BitMap));
    macBitMap->baseAddr = (Ptr) NewPtrClear(myRowBytes * (myBounds.bottom - myBounds.top));
    macBitMap->bounds = myBounds;
    macBitMap->rowBytes = myRowBytes;
 
    //CopyBits(&(*mainWinPtr).portBits, macBitMap, &windRect, &macBitMap->bounds, 0, nil);
    CopyBits(GetPortBitMapForCopyBits(GetWindowPort(mainWinPtr)), macBitMap, &windRect, &macBitMap->bounds, 0, nil);
    QDFlushPortBuffer(GetWindowPort(mainWinPtr), GetPortVisibleRegion(GetWindowPort(mainWinPtr), rgnHandle));
 
    /* Wait until user clicks button. */
    do {
    } while (!Button());
 
    rotate();
    //CopyBits(macBitMap, &(*mainWinPtr).portBits, &macBitMap->bounds, &windRect, 0, nil);
    CopyBits(macBitMap, GetPortBitMapForCopyBits(GetWindowPort(mainWinPtr)), &windRect, &macBitMap->bounds, 0, nil);
    QDFlushPortBuffer(GetWindowPort(mainWinPtr), GetPortVisibleRegion(GetWindowPort(mainWinPtr), rgnHandle));
    
    /* Wait for button to be up */
    do {
    } while (Button());
    /* Wait until user clicks button. */
    do {
    } while (!Button());
    
    DisposeRgn(rgnHandle);
        
        return 0;
}