Source/QuartzUtils.m
author Jens Alfke <jens@mooseyard.com>
Sun Feb 06 16:31:03 2011 -0800 (2011-02-06)
changeset 29 0b1c315ffc64
parent 21 2eb229411d73
permissions -rw-r--r--
Minor compiler-compatibility fixes.
jens@0
     1
/*  This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
jens@0
     2
    http://developer.apple.com/samplecode/GeekGameBoard/
jens@0
     3
    Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
jens@0
     4
jens@0
     5
    Redistribution and use in source and binary forms, with or without modification, are permitted
jens@0
     6
    provided that the following conditions are met:
jens@0
     7
jens@0
     8
    * Redistributions of source code must retain the above copyright notice, this list of conditions
jens@0
     9
      and the following disclaimer.
jens@0
    10
    * Redistributions in binary form must reproduce the above copyright notice, this list of
jens@0
    11
      conditions and the following disclaimer in the documentation and/or other materials provided
jens@0
    12
      with the distribution.
jens@0
    13
jens@0
    14
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
jens@0
    15
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
jens@0
    16
    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
jens@0
    17
    BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jens@0
    18
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
jens@0
    19
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
jens@0
    20
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
jens@0
    21
    THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jens@0
    22
*/
jens@0
    23
#import "QuartzUtils.h"
jens@1
    24
#import <QuartzCore/QuartzCore.h>
jens@9
    25
#import "Piece.h"
snej@25
    26
#import "GGBUtils.h"
jens@0
    27
jens@0
    28
jens@0
    29
CGColorRef kBlackColor, kWhiteColor, 
jens@0
    30
           kTranslucentGrayColor, kTranslucentLightGrayColor,
jens@10
    31
           kTranslucentWhiteColor, kAlmostInvisibleWhiteColor,
jens@0
    32
           kHighlightColor;
jens@0
    33
jens@0
    34
jens@0
    35
__attribute__((constructor))        // Makes this function run when the app loads
jens@0
    36
static void InitQuartzUtils()
jens@0
    37
{
jens@1
    38
    kBlackColor = CreateGray(0.0, 1.0);
jens@1
    39
    kWhiteColor = CreateGray(1.0, 1.0);
jens@1
    40
    kTranslucentGrayColor = CreateGray(0.0, 0.5);
jens@1
    41
    kTranslucentLightGrayColor = CreateGray(0.0, 0.25);
jens@10
    42
    kTranslucentWhiteColor = CreateGray(1, 0.25);
jens@1
    43
    kAlmostInvisibleWhiteColor = CreateGray(1, 0.05);
jens@1
    44
    kHighlightColor = CreateRGB(1, 1, 0, 0.5);
jens@0
    45
}
jens@0
    46
jens@0
    47
jens@8
    48
#if TARGET_OS_IPHONE
jens@1
    49
CGColorRef CreateGray(CGFloat gray, CGFloat alpha)
jens@1
    50
{
jens@1
    51
    CGColorSpaceRef graySpace = CGColorSpaceCreateDeviceGray();
jens@1
    52
    CGFloat components[2] = {gray,alpha};
jens@1
    53
    CGColorRef color = CGColorCreate(graySpace, components);
jens@1
    54
    CGColorSpaceRelease(graySpace);
jens@1
    55
    return color;
jens@1
    56
}
jens@1
    57
jens@1
    58
CGColorRef CreateRGB(CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha)
jens@1
    59
{
jens@1
    60
    CGColorSpaceRef rgbSpace = CGColorSpaceCreateDeviceRGB();
jens@1
    61
    CGFloat components[4] = {red,green,blue,alpha};
jens@1
    62
    CGColorRef color = CGColorCreate(rgbSpace, components);
jens@1
    63
    CGColorSpaceRelease(rgbSpace);
jens@1
    64
    return color;
jens@1
    65
}
jens@1
    66
#endif
jens@1
    67
jens@1
    68
jens@0
    69
CGImageRef CreateCGImageFromFile( NSString *path )
jens@0
    70
{
jens@8
    71
#if TARGET_OS_IPHONE
jens@1
    72
    UIImage *uiImage = [UIImage imageWithContentsOfFile: path];
jens@21
    73
    if(!uiImage) Warn(@"UIImage imageWithContentsOfFile failed on file %@",path);
jens@1
    74
    return CGImageRetain(uiImage.CGImage);
jens@1
    75
#else
jens@0
    76
    CGImageRef image = NULL;
jens@0
    77
    CFURLRef url = (CFURLRef) [NSURL fileURLWithPath: path];
jens@0
    78
    CGImageSourceRef src = CGImageSourceCreateWithURL(url, NULL);
jens@0
    79
    if( src ) {
jens@0
    80
        image = CGImageSourceCreateImageAtIndex(src, 0, NULL);
jens@0
    81
        CFRelease(src);
jens@21
    82
        if(!image) Warn(@"CGImageSourceCreateImageAtIndex failed on file %@ (ptr size=%u)",path,sizeof(void*));
jens@0
    83
    }
jens@0
    84
    return image;
jens@1
    85
#endif
jens@0
    86
}
jens@0
    87
jens@0
    88
jens@0
    89
CGImageRef GetCGImageNamed( NSString *name )
jens@0
    90
{
jens@8
    91
#if TARGET_OS_IPHONE
jens@1
    92
    name = name.lastPathComponent;
jens@1
    93
    UIImage *uiImage = [UIImage imageNamed: name];
jens@1
    94
    NSCAssert1(uiImage,@"Couldn't find bundle image resource '%@'",name);
jens@1
    95
    return uiImage.CGImage;
jens@1
    96
#else
jens@0
    97
    // For efficiency, loaded images are cached in a dictionary by name.
jens@0
    98
    static NSMutableDictionary *sMap;
jens@0
    99
    if( ! sMap )
jens@0
   100
        sMap = [[NSMutableDictionary alloc] init];
jens@0
   101
    
jens@0
   102
    CGImageRef image = (CGImageRef) [sMap objectForKey: name];
jens@0
   103
    if( ! image ) {
jens@0
   104
        // Hasn't been cached yet, so load it:
jens@0
   105
        NSString *path;
jens@0
   106
        if( [name hasPrefix: @"/"] )
jens@0
   107
            path = name;
jens@0
   108
        else {
jens@11
   109
            NSString *dir = [name stringByDeletingLastPathComponent];
jens@11
   110
            name = [name lastPathComponent];
jens@11
   111
            NSString *ext = name.pathExtension;
jens@11
   112
            name = [name stringByDeletingPathExtension];
jens@11
   113
            path = [[NSBundle mainBundle] pathForResource: name ofType: ext inDirectory: dir];
jens@11
   114
            NSCAssert3(path,@"Couldn't find bundle image resource '%@' type '%@' in '%@'",name,ext,dir);
jens@0
   115
        }
jens@0
   116
        image = CreateCGImageFromFile(path);
jens@0
   117
        NSCAssert1(image,@"Failed to load image from %@",path);
jens@0
   118
        [sMap setObject: (id)image forKey: name];
jens@0
   119
        CGImageRelease(image);
jens@0
   120
    }
jens@0
   121
    return image;
jens@1
   122
#endif
jens@0
   123
}
jens@0
   124
jens@0
   125
jens@0
   126
CGColorRef GetCGPatternNamed( NSString *name )         // can be resource name or abs. path
jens@0
   127
{
jens@0
   128
    // For efficiency, loaded patterns are cached in a dictionary by name.
jens@0
   129
    static NSMutableDictionary *sMap;
jens@0
   130
    if( ! sMap )
jens@0
   131
        sMap = [[NSMutableDictionary alloc] init];
jens@0
   132
    
jens@0
   133
    CGColorRef pattern = (CGColorRef) [sMap objectForKey: name];
jens@0
   134
    if( ! pattern ) {
jens@0
   135
        pattern = CreatePatternColor( GetCGImageNamed(name) );
jens@0
   136
        [sMap setObject: (id)pattern forKey: name];
jens@0
   137
        CGColorRelease(pattern);
jens@0
   138
    }
jens@0
   139
    return pattern;
jens@0
   140
}
jens@0
   141
jens@0
   142
jens@8
   143
#if ! TARGET_OS_IPHONE
jens@11
   144
jens@11
   145
BOOL CanGetCGImageFromPasteboard( NSPasteboard *pb )
jens@11
   146
{
jens@11
   147
    return [NSImage canInitWithPasteboard: pb] 
jens@11
   148
        || [[pb types] containsObject: @"PixadexIconPathPboardType"];
jens@11
   149
jens@11
   150
    /*if( [[pb types] containsObject: NSFilesPromisePboardType] ) {
jens@11
   151
     NSArray *fileTypes = [pb propertyListForType: NSFilesPromisePboardType];
jens@11
   152
     NSLog(@"Got file promise! Types = %@",fileTypes);
jens@11
   153
     //FIX: Check file types
jens@11
   154
     return NSDragOperationCopy;
jens@11
   155
     }*/
jens@11
   156
}    
jens@11
   157
jens@11
   158
CGImageRef GetCGImageFromPasteboard( NSPasteboard *pb, id<NSDraggingInfo>dragInfo )
jens@0
   159
{
jens@0
   160
    CGImageSourceRef src = NULL;
jens@0
   161
    NSArray *paths = [pb propertyListForType: NSFilenamesPboardType];
jens@0
   162
    if( paths.count==1 ) {
jens@0
   163
        // If a file is being dragged, read it:
jens@0
   164
        CFURLRef url = (CFURLRef) [NSURL fileURLWithPath: [paths objectAtIndex: 0]];
jens@0
   165
        src = CGImageSourceCreateWithURL(url, NULL);
jens@11
   166
/*
jens@11
   167
    } else if( dragInfo && [[pb types] containsObject:NSFilesPromisePboardType] ) {
jens@11
   168
        NSString *dropDir = NSTemporaryDirectory();
jens@11
   169
        NSArray *filenames = [dragInfo namesOfPromisedFilesDroppedAtDestination: [NSURL fileURLWithPath: dropDir]];
jens@11
   170
        NSLog(@"promised files are %@ / %@", dropDir,filenames);
jens@11
   171
        src = nil; */
jens@11
   172
    } else if( [[pb types] containsObject: @"PixadexIconPathPboardType"] ) {
jens@11
   173
        // Candybar 3 (nee Pixadex) doesn't drag out icons in any normal image type.
jens@11
   174
        // It does support file-promises, but I couldn't get those to work using the Cocoa APIs.
jens@11
   175
        // So instead I'm using its custom type that provides the path(s) to its internal ".pxicon" files.
jens@11
   176
        // The icon is really easy to get from one of these: it's just file's custom icon.
jens@11
   177
        NSArray *files = [pb propertyListForType: @"PixadexIconPathPboardType"];
jens@11
   178
        if( files.count == 1 ) {
jens@11
   179
            NSString *path = [files objectAtIndex: 0];
jens@11
   180
            NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile: path];
jens@11
   181
            for( NSImageRep *rep in icon.representations ) {
jens@11
   182
                if( [rep isKindOfClass: [NSBitmapImageRep class]] ) {
jens@11
   183
                    [rep retain];   //FIX: This leaks; but if the rep goes away, the CGImage breaks...
jens@11
   184
                    return [(NSBitmapImageRep*)rep CGImage];
jens@11
   185
                }
jens@11
   186
            }
jens@11
   187
        }
jens@0
   188
    } else {
jens@0
   189
        // Else look for an image type:
jens@0
   190
        NSString *type = [pb availableTypeFromArray: [NSImage imageUnfilteredPasteboardTypes]];
jens@0
   191
        if( type ) {
jens@0
   192
            NSData *data = [pb dataForType: type];
jens@0
   193
            src = CGImageSourceCreateWithData((CFDataRef)data, NULL);
jens@0
   194
        }
jens@0
   195
    }
jens@0
   196
    if(src) {
jens@0
   197
        CGImageRef image = CGImageSourceCreateImageAtIndex(src, 0, NULL);
jens@0
   198
        CFRelease(src);
jens@0
   199
        return image;
jens@0
   200
    } else
jens@0
   201
        return NULL;
jens@1
   202
}
jens@1
   203
#endif
jens@0
   204
jens@0
   205
jens@9
   206
CGImageRef CreateScaledImage( CGImageRef srcImage, CGFloat scale )
jens@9
   207
{
jens@9
   208
    int width = CGImageGetWidth(srcImage), height = CGImageGetHeight(srcImage);
jens@9
   209
    if( scale > 0 ) {
jens@9
   210
        if( scale >= 4.0 )
jens@9
   211
            scale /= MAX(width,height);             // interpret scale as target dimensions
jens@9
   212
        width = ceil( width * scale);
jens@9
   213
        height= ceil( height* scale);
jens@9
   214
    }
jens@9
   215
jens@9
   216
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
jens@9
   217
    CGContextRef ctx = CGBitmapContextCreate(NULL, width, height, 8, 4*width, space,
jens@9
   218
                                             kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast);
jens@9
   219
    CGColorSpaceRelease(space);
jens@9
   220
    CGContextSetInterpolationQuality(ctx,kCGInterpolationHigh);
jens@9
   221
    CGContextDrawImage(ctx, CGRectMake(0, 0, width, height), srcImage);
jens@9
   222
    CGImageRef dstImage = CGBitmapContextCreateImage(ctx);
jens@9
   223
    CGContextRelease(ctx);
jens@9
   224
    return dstImage;
jens@9
   225
}
jens@9
   226
jens@9
   227
jens@9
   228
CGImageRef GetScaledImageNamed( NSString *imageName, CGFloat scale )
jens@9
   229
{
jens@9
   230
    // For efficiency, loaded images are cached in a dictionary by name.
jens@9
   231
    static NSMutableDictionary *sMap;
jens@9
   232
    if( ! sMap )
jens@9
   233
        sMap = [[NSMutableDictionary alloc] init];
jens@9
   234
    
jens@9
   235
    NSArray *key = [NSArray arrayWithObjects: imageName, [NSNumber numberWithFloat: scale], nil];
jens@9
   236
    CGImageRef image = (CGImageRef) [sMap objectForKey: key];
jens@9
   237
    if( ! image ) {
jens@9
   238
        // Hasn't been cached yet, so load it:
jens@9
   239
        image = CreateScaledImage(GetCGImageNamed(imageName), scale);
jens@9
   240
        [sMap setObject: (id)image forKey: key];
jens@9
   241
        CGImageRelease(image);
jens@9
   242
    }
jens@9
   243
    return image;
jens@9
   244
}
jens@9
   245
jens@9
   246
jens@0
   247
float GetPixelAlpha( CGImageRef image, CGSize imageSize, CGPoint pt )
jens@0
   248
{
jens@11
   249
    NSCParameterAssert(image);
jens@8
   250
#if TARGET_OS_IPHONE
jens@1
   251
    // iPhone uses "flipped" (i.e. normal) coords, so images are wrong-way-up
jens@1
   252
    pt.y = imageSize.height - pt.y;
jens@1
   253
#endif
jens@1
   254
    
jens@0
   255
    // Trivial reject:
jens@0
   256
    if( pt.x<0 || pt.x>=imageSize.width || pt.y<0 || pt.y>=imageSize.height )
jens@0
   257
        return 0.0;
jens@0
   258
    
jens@0
   259
    // sTinyContext is a 1x1 CGBitmapContext whose pixmap stores only alpha.
jens@0
   260
    static UInt8 sPixel[1];
jens@0
   261
    static CGContextRef sTinyContext;
jens@0
   262
    if( ! sTinyContext ) {
jens@0
   263
        sTinyContext = CGBitmapContextCreate(sPixel, 1, 1, 
jens@0
   264
                                             8, 1,     // bpp, rowBytes
jens@0
   265
                                             NULL,
jens@0
   266
                                             kCGImageAlphaOnly);
jens@0
   267
        CGContextSetBlendMode(sTinyContext, kCGBlendModeCopy);
jens@0
   268
    }
jens@0
   269
    
jens@0
   270
    // Draw the image into sTinyContext, positioned so the desired point is at
jens@0
   271
    // (0,0), then examine the alpha value in the pixmap:
jens@0
   272
    CGContextDrawImage(sTinyContext, 
jens@0
   273
                       CGRectMake(-pt.x,-pt.y, imageSize.width,imageSize.height),
jens@0
   274
                       image);
jens@0
   275
    return sPixel[0] / 255.0;
jens@0
   276
}
jens@0
   277
jens@0
   278
jens@0
   279
#pragma mark -
jens@0
   280
#pragma mark PATTERNS:
jens@0
   281
jens@0
   282
jens@0
   283
// callback for CreateImagePattern.
jens@0
   284
static void drawPatternImage (void *info, CGContextRef ctx)
jens@0
   285
{
jens@0
   286
    CGImageRef image = (CGImageRef) info;
jens@0
   287
    CGContextDrawImage(ctx, 
jens@0
   288
                       CGRectMake(0,0, CGImageGetWidth(image),CGImageGetHeight(image)),
jens@0
   289
                       image);
jens@0
   290
}
jens@0
   291
jens@0
   292
// callback for CreateImagePattern.
jens@0
   293
static void releasePatternImage( void *info )
jens@0
   294
{
jens@0
   295
    CGImageRelease( (CGImageRef)info );
jens@0
   296
}
jens@0
   297
jens@0
   298
jens@0
   299
CGPatternRef CreateImagePattern( CGImageRef image )
jens@0
   300
{
jens@0
   301
    NSCParameterAssert(image);
jens@0
   302
    int width = CGImageGetWidth(image);
jens@0
   303
    int height = CGImageGetHeight(image);
jens@0
   304
    static const CGPatternCallbacks callbacks = {0, &drawPatternImage, &releasePatternImage};
jens@0
   305
    return CGPatternCreate (image,
jens@0
   306
                            CGRectMake (0, 0, width, height),
jens@0
   307
                            CGAffineTransformMake (1, 0, 0, 1, 0, 0),
jens@0
   308
                            width,
jens@0
   309
                            height,
jens@0
   310
                            kCGPatternTilingConstantSpacing,
jens@0
   311
                            true,
jens@0
   312
                            &callbacks);
jens@0
   313
}
jens@0
   314
jens@0
   315
jens@0
   316
CGColorRef CreatePatternColor( CGImageRef image )
jens@0
   317
{
jens@0
   318
    CGPatternRef pattern = CreateImagePattern(image);
jens@0
   319
    CGColorSpaceRef space = CGColorSpaceCreatePattern(NULL);
jens@0
   320
    CGFloat components[1] = {1.0};
jens@0
   321
    CGColorRef color = CGColorCreateWithPattern(space, pattern, components);
jens@0
   322
    CGColorSpaceRelease(space);
jens@0
   323
    CGPatternRelease(pattern);
jens@0
   324
    return color;
jens@0
   325
}
jens@1
   326
jens@1
   327
jens@1
   328
#pragma mark -
jens@1
   329
#pragma mark PATHS:
jens@1
   330
jens@1
   331
jens@1
   332
void AddRoundRect( CGContextRef ctx, CGRect rect, CGFloat radius )
jens@1
   333
{
jens@1
   334
    radius = MIN(radius, floorf(rect.size.width/2));
jens@1
   335
    float x0 = CGRectGetMinX(rect), y0 = CGRectGetMinY(rect),
jens@1
   336
    x1 = CGRectGetMaxX(rect), y1 = CGRectGetMaxY(rect);
jens@1
   337
    
jens@1
   338
    CGContextBeginPath(ctx);
jens@1
   339
    CGContextMoveToPoint(ctx,x0+radius,y0);
jens@1
   340
    CGContextAddArcToPoint(ctx,x1,y0, x1,y1, radius);
jens@1
   341
    CGContextAddArcToPoint(ctx,x1,y1, x0,y1, radius);
jens@1
   342
    CGContextAddArcToPoint(ctx,x0,y1, x0,y0, radius);
jens@1
   343
    CGContextAddArcToPoint(ctx,x0,y0, x1,y0, radius);
jens@1
   344
    CGContextClosePath(ctx);
jens@1
   345
}
jens@1
   346