// TestBitmapDrawSpeed.m
// App to measure bitmap drawing speed (to & from the graphics context)

#import <AppKit/AppKit.h>

#define kTestImageSize  {512,512}

#define kTestLoopCount  500


int main()
{
    NSAutoreleasePool *autoreleasePool;    
    NSRect imageBounds = {{0,0}, kTestImageSize};
    NSImage *image;
    NSBitmapImageRep *bitmap;
    NSGraphicsContext *graphicsContext;
    NSDate *date;
    int loopCounter;
    NSTimeInterval draw32TimeInterval, draw24TimeInterval, readTimeInterval;

    autoreleasePool = [[NSAutoreleasePool alloc] init];
    [NSApplication sharedApplication];

    image = [[[NSImage alloc] initWithSize: imageBounds.size] autorelease];
    
    if (!image)
        goto EXIT;
    
    [image lockFocus];

    graphicsContext = [NSGraphicsContext currentContext];
    
    // Bitmap drawing - 32-bit
    
    bitmap = 
        [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
                                    pixelsWide: imageBounds.size.width
                                    pixelsHigh: imageBounds.size.height
                                    bitsPerSample: 8
                                    samplesPerPixel: 4
                                    hasAlpha: YES
                                    isPlanar: NO
                                    colorSpaceName: NSDeviceRGBColorSpace
                                    bytesPerRow: 0
                                    bitsPerPixel: 0]
                                autorelease];
                                
    if (!bitmap)
        goto EXIT;
    
    date = [NSDate date];
    
    loopCounter = kTestLoopCount;
    
    while (loopCounter--)
    {
        NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init];
    
        [bitmap drawAtPoint: NSZeroPoint];
        
        [autoreleasePool release];
    }
    
    draw32TimeInterval = -[date timeIntervalSinceNow];
    
    // Bitmap drawing - 24-bit
    
    bitmap = 
        [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
                                    pixelsWide: imageBounds.size.width
                                    pixelsHigh: imageBounds.size.height
                                    bitsPerSample: 8
                                    samplesPerPixel: 3
                                    hasAlpha: NO
                                    isPlanar: NO
                                    colorSpaceName: NSDeviceRGBColorSpace
                                    bytesPerRow: 0
                                    bitsPerPixel: 0]
                                autorelease];
                                
    if (!bitmap)
        goto EXIT;
    
    date = [NSDate date];
    
    loopCounter = kTestLoopCount;
    
    while (loopCounter--)
    {
        NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init];
    
        [bitmap drawAtPoint: NSZeroPoint];
        
        [autoreleasePool release];
    }
    
    draw24TimeInterval = -[date timeIntervalSinceNow];

    // Reading bitmap from graphics context
    
    date = [NSDate date];
    
    loopCounter = kTestLoopCount;
    
    while (loopCounter--)
    {
        NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init];
    
        [graphicsContext GSReadRect: imageBounds];
       
        [autoreleasePool release];
    }

    readTimeInterval = -[date timeIntervalSinceNow];

    [image unlockFocus];

    NSLog(@"Speed test for image of size: %@", NSStringFromSize(imageBounds.size));
    NSLog(@" %d 32-bit draws: %f sec.", kTestLoopCount, (float) draw32TimeInterval);
    NSLog(@" %d 24-bit draws: %f sec.", kTestLoopCount, (float) draw24TimeInterval);
    NSLog(@" %d reads: %f sec.", kTestLoopCount, (float) readTimeInterval);

EXIT:
    [autoreleasePool release];
    
    return 0;
}

