project_files/HedgewarsMobile/Classes/UIImageExtra.m
author koda
Tue, 23 Aug 2011 03:46:17 +0200
changeset 5662 99083392cd4f
parent 5503 d8632f589008
child 6078 8c0cc07731e5
permissions -rw-r--r--
FREE AT LAST!!! SDL came around a (mostly) sane way for implementing rotation events, so we can scrap all the workaround code that has been added to workaround it!! Also this allows us to use proper (internal) multitasking handling and can simplify optional settings and other yet unexplored features. Yay!
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     1
/*
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4478
diff changeset
     3
 * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     4
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     8
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    12
 * GNU General Public License for more details.
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    13
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    14
 * You should have received a copy of the GNU General Public License
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    15
 * along with this program; if not, write to the Free Software
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    17
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    18
 * File created on 08/04/2010.
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    19
 */
81db3c85784b headers ftw, also right project file
koda
parents: 3778
diff changeset
    20
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
#import "UIImageExtra.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
@implementation UIImage (extra)
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    26
4446
3093791dc620 enable support for retina display in the ios port
koda
parents: 4356
diff changeset
    27
CGFloat getScreenScale(void) {
5503
d8632f589008 use -Os --ffast-math on ios everywhere, plus other sdk3.2 compatibility changes
koda
parents: 4976
diff changeset
    28
    float scale = 1.0f;
4446
3093791dc620 enable support for retina display in the ios port
koda
parents: 4356
diff changeset
    29
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
5503
d8632f589008 use -Os --ffast-math on ios everywhere, plus other sdk3.2 compatibility changes
koda
parents: 4976
diff changeset
    30
        scale = [[UIScreen mainScreen] scale];
d8632f589008 use -Os --ffast-math on ios everywhere, plus other sdk3.2 compatibility changes
koda
parents: 4976
diff changeset
    31
    return scale;
4446
3093791dc620 enable support for retina display in the ios port
koda
parents: 4356
diff changeset
    32
}
3093791dc620 enable support for retina display in the ios port
koda
parents: 4356
diff changeset
    33
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
-(UIImage *)scaleToSize:(CGSize) size {
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    35
    DLog(@"warning - this is a very expensive operation, you should avoid using it");
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    36
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    37
    // Create a bitmap graphics context; this will also set it as the current context
5503
d8632f589008 use -Os --ffast-math on ios everywhere, plus other sdk3.2 compatibility changes
koda
parents: 4976
diff changeset
    38
    if (UIGraphicsBeginImageContextWithOptions != NULL)
4476
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
    39
        UIGraphicsBeginImageContextWithOptions(size, NO, getScreenScale());
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
    40
    else
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
    41
        UIGraphicsBeginImageContext(size);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    42
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    43
    // Draw the scaled image in the current context
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    44
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    45
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    46
    // Create a new image from current context
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    47
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    48
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    49
    // Pop the current context from the stack
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    50
    UIGraphicsEndImageContext();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    51
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    52
    // Return our new scaled image (autoreleased)
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    53
    return scaledImage;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
3778
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    57
    if (secondImage == nil) {
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    58
        DLog(@"Warning, secondImage == nil");
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    59
        return self;
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    60
    }
4476
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
    61
    CGFloat screenScale = getScreenScale();
4478
05029b4d8490 code cleanup
koda
parents: 4476
diff changeset
    62
    int w = self.size.width * screenScale;
05029b4d8490 code cleanup
koda
parents: 4476
diff changeset
    63
    int h = self.size.height * screenScale;
3948
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    64
    
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    65
    if (w == 0 || h == 0) {
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    66
        DLog(@"Can have 0 dimesions");
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    67
        return self;
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    68
    }
3778
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    69
    
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    70
    // Create a bitmap graphics context; this will also set it as the current context
3948
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    71
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    72
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    73
    
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    74
    // draw the two images in the current context
4476
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
    75
    CGContextDrawImage(context, CGRectMake(0, 0, self.size.width*screenScale, self.size.height*screenScale), [self CGImage]);
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
    76
    CGContextDrawImage(context, CGRectMake(secondImagePoint.x*screenScale, secondImagePoint.y*screenScale, secondImage.size.width*screenScale, secondImage.size.height*screenScale), [secondImage CGImage]);
3948
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    77
    
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    78
    // Create bitmap image info from pixel data in current context
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    79
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    80
    
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    81
    // Create a new UIImage object
4476
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
    82
    UIImage *resultImage;
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
    83
    if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
    84
        resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
    85
    else
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
    86
        resultImage = [UIImage imageWithCGImage:imageRef];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    87
3948
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    88
    // Release colorspace, context and bitmap information
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    89
    CGColorSpaceRelease(colorSpace);
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    90
    CGContextRelease(context);
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    91
    CFRelease(imageRef);
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    92
   
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
    return resultImage;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
-(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
    // load image from path
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
    UIImage *image = [[UIImage alloc] initWithContentsOfFile: path];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    99
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
    if (nil != image) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
        // get its CGImage representation with a give size
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   102
        CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   103
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
        // clean memory
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
        [image release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   106
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
        // create a UIImage from the CGImage (memory must be allocated already)
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   108
        UIImage *sprite = [self initWithCGImage:cgImage];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   109
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
        // clean memory
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   111
        CGImageRelease(cgImage);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
        // return resulting image
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
        return sprite;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
        DLog(@"error - image == nil");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
        return nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   121
-(UIImage *)cutAt:(CGRect) rect {
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   122
    CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   123
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   124
    UIImage *res = [UIImage imageWithCGImage:cgImage];
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   125
    CGImageRelease(cgImage);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   126
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   127
    return res;
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   128
}
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   129
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
-(UIImage *)convertToGrayScale {
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   131
    // Create image rectangle with current image width/height
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   132
    CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   133
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   134
    // Grayscale color space
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   135
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   136
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   137
    // Create bitmap content with current image size and grayscale colorspace
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   138
    CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   139
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   140
    // Draw image into current context, with specified rectangle
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   141
    // using previously defined context (with grayscale colorspace)
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   142
    CGContextDrawImage(context, imageRect, [self CGImage]);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   143
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   144
    // Create bitmap image info from pixel data in current context
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   145
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   146
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   147
    // Create a new UIImage object
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   148
    UIImage *newImage = [UIImage imageWithCGImage:imageRef];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   149
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   150
    // Release colorspace, context and bitmap information
3978
9660600e43cb fix some glitches
koda
parents: 3948
diff changeset
   151
    CFRelease(imageRef);
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   152
    CGContextRelease(context);
3978
9660600e43cb fix some glitches
koda
parents: 3948
diff changeset
   153
    CGColorSpaceRelease(colorSpace);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   154
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   155
    // Return the new grayscale image
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   156
    return newImage;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
// by http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html turned into a category by koda
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
-(UIImage*) maskImageWith:(UIImage *)maskImage {
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   161
    // prepare the reference image
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   162
    CGImageRef maskRef = [maskImage CGImage];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   163
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   164
    // create the mask using parameters of the mask reference
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
                                        CGImageGetHeight(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
                                        CGImageGetBitsPerComponent(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
                                        CGImageGetBitsPerPixel(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
                                        CGImageGetBytesPerRow(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
                                        CGImageGetDataProvider(maskRef), NULL, false);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   171
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   172
    // create an image in the current context
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
    CGImageRef masked = CGImageCreateWithMask([self CGImage], mask);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   174
    CGImageRelease(mask);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   175
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
    UIImage* retImage = [UIImage imageWithCGImage:masked];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   177
    CGImageRelease(masked);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   178
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   179
    return retImage;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   180
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   181
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   182
// by http://blog.sallarp.com/iphone-uiimage-round-corners/ turned into a category by koda
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   183
void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) {
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   184
    CGFloat fw, fh;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   185
    if (ovalWidth == 0 || ovalHeight == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   186
        CGContextAddRect(context, rect);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   187
        return;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   188
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   189
    CGContextSaveGState(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   190
    CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   191
    CGContextScaleCTM (context, ovalWidth, ovalHeight);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   192
    fw = CGRectGetWidth (rect) / ovalWidth;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   193
    fh = CGRectGetHeight (rect) / ovalHeight;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   194
    CGContextMoveToPoint(context, fw, fh/2);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   195
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   196
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   197
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   198
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   199
    CGContextClosePath(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   200
    CGContextRestoreGState(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   201
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   202
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   203
-(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   204
    CGFloat cornerWidth = sizewh.width;
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   205
    CGFloat cornerHeight = sizewh.height;
4476
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
   206
    CGFloat screenScale = getScreenScale();
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
   207
    CGFloat w = self.size.width * screenScale;
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
   208
    CGFloat h = self.size.height * screenScale;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   209
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   210
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   211
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   212
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   213
    CGContextBeginPath(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   214
    CGRect rect = CGRectMake(0, 0, w, h);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   215
    addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   216
    CGContextClosePath(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   217
    CGContextClip(context);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   218
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   219
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   220
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   221
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   222
    CGContextRelease(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   223
    CGColorSpaceRelease(colorSpace);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   224
4461
2f4f5d649bcd add a simple check to prevent loading sounds when device is muted
koda
parents: 4446
diff changeset
   225
    UIImage *newImage;
2f4f5d649bcd add a simple check to prevent loading sounds when device is muted
koda
parents: 4446
diff changeset
   226
    if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
4476
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
   227
        newImage = [UIImage imageWithCGImage:imageMasked scale:screenScale orientation:UIImageOrientationUp];
4461
2f4f5d649bcd add a simple check to prevent loading sounds when device is muted
koda
parents: 4446
diff changeset
   228
    else
2f4f5d649bcd add a simple check to prevent loading sounds when device is muted
koda
parents: 4446
diff changeset
   229
        newImage = [UIImage imageWithCGImage:imageMasked];
2f4f5d649bcd add a simple check to prevent loading sounds when device is muted
koda
parents: 4446
diff changeset
   230
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   231
    CGImageRelease(imageMasked);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   232
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   233
    return newImage;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   234
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   235
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   236
// by http://www.sixtemia.com/journal/2010/06/23/uiimage-negative-color-effect/
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   237
-(UIImage *)convertToNegative {
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   238
    UIGraphicsBeginImageContext(self.size);
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   239
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   240
    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   241
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   242
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   243
    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, self.size.width, self.size.height));
3948
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
   244
    // create an image from the current contex (not thread safe)
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   245
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   246
    UIGraphicsEndImageContext();
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   247
    return result;
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   248
}
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   249
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents: 3903
diff changeset
   250
+(UIImage *)whiteImage:(CGSize) ofSize {
4356
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   251
    CGFloat w = ofSize.width;
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   252
    CGFloat h = ofSize.height;
4478
05029b4d8490 code cleanup
koda
parents: 4476
diff changeset
   253
    DLog(@"w: %f, h: %f", w, h);
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents: 3903
diff changeset
   254
4356
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   255
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   256
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   257
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   258
    CGContextBeginPath(context);
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents: 3903
diff changeset
   259
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents: 3903
diff changeset
   260
    CGContextFillRect(context,CGRectMake(0,0,ofSize.width,ofSize.height));
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents: 3903
diff changeset
   261
4356
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   262
    CGImageRef image = CGBitmapContextCreateImage(context);
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   263
    CGContextRelease(context);
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   264
    CGColorSpaceRelease(colorSpace);
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   265
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   266
    UIImage *bkgImg = [UIImage imageWithCGImage:image];
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   267
    CGImageRelease(image);
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents: 3903
diff changeset
   268
    return bkgImg;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents: 3903
diff changeset
   269
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents: 3903
diff changeset
   270
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   271
@end