project_files/HedgewarsMobile/Classes/UIImageExtra.m
author koda
Fri, 17 Feb 2012 18:23:36 +0100
changeset 6700 e04da46ee43c
parent 6209 074ab6ebcb3e
child 6832 fae8fd118da9
permissions -rw-r--r--
the most important commit of the year
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
6700
e04da46ee43c the most important commit of the year
koda
parents: 6209
diff changeset
     3
 * Copyright (c) 2009-2012 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
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
-(UIImage *)scaleToSize:(CGSize) size {
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    28
    // Create a bitmap graphics context; this will also set it as the current context
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    29
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    30
    CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, 4 * size.width, colorSpace, kCGImageAlphaPremultipliedFirst);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    31
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    32
    // draw the image inside the context
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    33
    CGFloat screenScale = [[UIScreen mainScreen] safeScale];
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    34
    CGContextDrawImage(context, CGRectMake(0, 0, size.width*screenScale, size.height*screenScale), self.CGImage);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    35
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    36
    // Create bitmap image info from pixel data in current context
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    37
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    38
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    39
    // Create a new UIImage object
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    40
    UIImage *resultImage;
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    41
    if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    42
        resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    43
    else
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    44
        resultImage = [UIImage imageWithCGImage:imageRef];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    45
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    46
    // Release colorspace, context and bitmap information
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    47
    CGColorSpaceRelease(colorSpace);
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    48
    CGContextRelease(context);
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    49
    CFRelease(imageRef);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    50
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    51
    return resultImage;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
3778
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    55
    if (secondImage == nil) {
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    56
        DLog(@"Warning, secondImage == nil");
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    57
        return self;
2e61bb50cc57 add a nice group of hogs in lobby page
koda
parents: 3697
diff changeset
    58
    }
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    59
    CGFloat screenScale = [[UIScreen mainScreen] safeScale];
4478
05029b4d8490 code cleanup
koda
parents: 4476
diff changeset
    60
    int w = self.size.width * screenScale;
05029b4d8490 code cleanup
koda
parents: 4476
diff changeset
    61
    int h = self.size.height * screenScale;
6080
ce02ddfe8aa1 on ipad2 and above animate the little hogs on game config and fix the hat display imaging
koda
parents: 6078
diff changeset
    62
    int yOffset = self.size.height - secondImage.size.height + secondImagePoint.y;
ce02ddfe8aa1 on ipad2 and above animate the little hogs on game config and fix the hat display imaging
koda
parents: 6078
diff changeset
    63
3948
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    64
    if (w == 0 || h == 0) {
6080
ce02ddfe8aa1 on ipad2 and above animate the little hogs on game config and fix the hat display imaging
koda
parents: 6078
diff changeset
    65
        DLog(@"Cannot have 0 dimesions");
3948
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    66
        return self;
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    67
    }
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    68
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
    69
    // 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
    70
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
6080
ce02ddfe8aa1 on ipad2 and above animate the little hogs on game config and fix the hat display imaging
koda
parents: 6078
diff changeset
    71
    CGContextRef context = CGBitmapContextCreate(NULL, w, h+yOffset, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    72
3948
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    73
    // 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
    74
    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
    75
    CGContextDrawImage(context, CGRectMake(secondImagePoint.x*screenScale, secondImagePoint.y*screenScale, secondImage.size.width*screenScale, secondImage.size.height*screenScale), [secondImage CGImage]);
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    76
3948
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    77
    // 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
    78
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    79
3948
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    80
    // Create a new UIImage object
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    81
    UIImage *resultImage;
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    82
    if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    83
        resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    84
    else
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    85
        resultImage = [UIImage imageWithCGImage:imageRef];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    86
3948
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    87
    // 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
    88
    CGColorSpaceRelease(colorSpace);
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    89
    CGContextRelease(context);
24daa33a3114 some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents: 3910
diff changeset
    90
    CFRelease(imageRef);
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
    91
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
    return resultImage;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
-(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
    // load image from path
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
    UIImage *image = [[UIImage alloc] initWithContentsOfFile: path];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
    98
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
    if (nil != image) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
        // get its CGImage representation with a give size
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   101
        CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   102
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
        // clean memory
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
        [image release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   105
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
        // create a UIImage from the CGImage (memory must be allocated already)
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   107
        UIImage *sprite = [self initWithCGImage:cgImage];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   108
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
        // clean memory
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   110
        CGImageRelease(cgImage);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
        // return resulting image
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
        return sprite;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
        DLog(@"error - image == nil");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
        return nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   120
-(UIImage *)cutAt:(CGRect) rect {
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   121
    CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   122
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   123
    UIImage *res = [UIImage imageWithCGImage:cgImage];
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   124
    CGImageRelease(cgImage);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   125
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   126
    return res;
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   127
}
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3573
diff changeset
   128
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
-(UIImage *)convertToGrayScale {
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   130
    // Create image rectangle with current image width/height
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   131
    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
   132
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   133
    // Grayscale color space
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   134
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   135
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   136
    // Create bitmap content with current image size and grayscale colorspace
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   137
    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
   138
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   139
    // Draw image into current context, with specified rectangle
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   140
    // using previously defined context (with grayscale colorspace)
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   141
    CGContextDrawImage(context, imageRect, [self CGImage]);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   142
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   143
    // Create bitmap image info from pixel data in current context
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   144
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   145
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   146
    // Create a new UIImage object
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   147
    UIImage *newImage = [UIImage imageWithCGImage:imageRef];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   148
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   149
    // Release colorspace, context and bitmap information
3978
9660600e43cb fix some glitches
koda
parents: 3948
diff changeset
   150
    CFRelease(imageRef);
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   151
    CGContextRelease(context);
3978
9660600e43cb fix some glitches
koda
parents: 3948
diff changeset
   152
    CGColorSpaceRelease(colorSpace);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   153
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   154
    // Return the new grayscale image
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   155
    return newImage;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
// 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
   159
-(UIImage*) maskImageWith:(UIImage *)maskImage {
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   160
    // prepare the reference image
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   161
    CGImageRef maskRef = [maskImage CGImage];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   162
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   163
    // create the mask using parameters of the mask reference
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
                                        CGImageGetHeight(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
                                        CGImageGetBitsPerComponent(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
                                        CGImageGetBitsPerPixel(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
                                        CGImageGetBytesPerRow(maskRef),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
                                        CGImageGetDataProvider(maskRef), NULL, false);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   170
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   171
    // create an image in the current context
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
    CGImageRef masked = CGImageCreateWithMask([self CGImage], mask);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
    CGImageRelease(mask);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   174
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   175
    UIImage* retImage = [UIImage imageWithCGImage:masked];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
    CGImageRelease(masked);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   177
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   178
    return retImage;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   179
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   180
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   181
// 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
   182
void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) {
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   183
    CGFloat fw, fh;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   184
    if (ovalWidth == 0 || ovalHeight == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   185
        CGContextAddRect(context, rect);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   186
        return;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   187
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   188
    CGContextSaveGState(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   189
    CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   190
    CGContextScaleCTM (context, ovalWidth, ovalHeight);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   191
    fw = CGRectGetWidth (rect) / ovalWidth;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   192
    fh = CGRectGetHeight (rect) / ovalHeight;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   193
    CGContextMoveToPoint(context, fw, fh/2);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   194
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   195
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   196
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   197
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   198
    CGContextClosePath(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   199
    CGContextRestoreGState(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   200
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   201
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   202
-(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   203
    CGFloat cornerWidth = sizewh.width;
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   204
    CGFloat cornerHeight = sizewh.height;
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   205
    CGFloat screenScale = [[UIScreen mainScreen] safeScale];
4476
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
   206
    CGFloat w = self.size.width * screenScale;
4bf74e158f44 team selection completely refactored, now has animation and more performance
koda
parents: 4461
diff changeset
   207
    CGFloat h = self.size.height * screenScale;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   208
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   209
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   210
    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
   211
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   212
    CGContextBeginPath(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   213
    CGRect rect = CGRectMake(0, 0, w, h);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   214
    addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   215
    CGContextClosePath(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   216
    CGContextClip(context);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   217
3573
c84067629035 complete configuration for schemes
koda
parents: 3547
diff changeset
   218
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   219
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   220
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   221
    CGContextRelease(context);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   222
    CGColorSpaceRelease(colorSpace);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   223
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   224
    UIImage *resultImage;
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   225
    if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   226
        resultImage = [UIImage imageWithCGImage:imageMasked scale:screenScale orientation:UIImageOrientationUp];
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   227
    else
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   228
        resultImage = [UIImage imageWithCGImage:imageMasked];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   229
    CGImageRelease(imageMasked);
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3621
diff changeset
   230
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   231
    return resultImage;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   232
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   233
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   234
// by http://www.sixtemia.com/journal/2010/06/23/uiimage-negative-color-effect/
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   235
-(UIImage *)convertToNegative {
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   236
    UIGraphicsBeginImageContext(self.size);
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   237
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   238
    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   239
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   240
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   241
    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
   242
    // create an image from the current contex (not thread safe)
3903
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   243
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   244
    UIGraphicsEndImageContext();
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   245
    return result;
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   246
}
db01c37494af pimp-my-savagame-table
koda
parents: 3829
diff changeset
   247
3910
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents: 3903
diff changeset
   248
+(UIImage *)whiteImage:(CGSize) ofSize {
4356
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   249
    CGFloat w = ofSize.width;
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   250
    CGFloat h = ofSize.height;
4478
05029b4d8490 code cleanup
koda
parents: 4476
diff changeset
   251
    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
   252
4356
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   253
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   254
    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
   255
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   256
    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
   257
    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
   258
    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
   259
4356
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   260
    CGImageRef image = CGBitmapContextCreateImage(context);
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   261
    CGContextRelease(context);
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   262
    CGColorSpaceRelease(colorSpace);
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   263
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   264
    UIImage *bkgImg = [UIImage imageWithCGImage:image];
d1d26f8963a3 update some files for new graphics and new ios4.2
koda
parents: 3978
diff changeset
   265
    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
   266
    return bkgImg;
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents: 3903
diff changeset
   267
}
dd47efbdec46 move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents: 3903
diff changeset
   268
6209
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   269
+(UIImage *)drawHogsRepeated:(NSInteger) manyTimes {
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   270
    NSString *imgString = [[NSString alloc] initWithFormat:@"%@/hedgehog.png",[[NSBundle mainBundle] resourcePath]];
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   271
    UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:imgString];
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   272
    [imgString release];
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   273
    CGFloat screenScale = [[UIScreen mainScreen] safeScale];
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   274
    int w = hogSprite.size.width * screenScale;
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   275
    int h = hogSprite.size.height * screenScale;
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   276
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   277
    CGContextRef context = CGBitmapContextCreate(NULL, w * 3, h, 8, 4 * w * 3, colorSpace, kCGImageAlphaPremultipliedFirst);
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   278
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   279
    // draw the two images in the current context
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   280
    for (int i = 0; i < manyTimes; i++)
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   281
        CGContextDrawImage(context, CGRectMake(i*8*screenScale, 0, w, h), [hogSprite CGImage]);
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   282
    [hogSprite release];
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   283
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   284
    // Create bitmap image info from pixel data in current context
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   285
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   286
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   287
    // Create a new UIImage object
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   288
    UIImage *resultImage;
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   289
    if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   290
        resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   291
    else
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   292
        resultImage = [UIImage imageWithCGImage:imageRef];
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   293
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   294
    // Release colorspace, context and bitmap information
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   295
    CGColorSpaceRelease(colorSpace);
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   296
    CGContextRelease(context);
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   297
    CFRelease(imageRef);
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   298
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   299
    return resultImage;
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   300
}
074ab6ebcb3e restore runtime compatibility with ios 3.2
koda
parents: 6080
diff changeset
   301
6078
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   302
// this routine checks for the PNG size without loading it in memory
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   303
// https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   304
+(CGSize) imageSizeFromMetadataOf:(NSString *)aFileName {
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   305
    // File Name to C String.
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   306
    const char *fileName = [aFileName UTF8String];
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   307
    // source file
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   308
    FILE *infile = fopen(fileName, "rb");
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   309
    if (infile == NULL) {
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   310
        DLog(@"Can't open the file: %@", aFileName);
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   311
        return CGSizeZero;
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   312
    }
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   313
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   314
    // Bytes Buffer.
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   315
    unsigned char buffer[30];
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   316
    // Grab Only First Bytes.
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   317
    fread(buffer, 1, 30, infile);
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   318
    // Close File.
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   319
    fclose(infile);
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   320
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   321
    // PNG Signature.
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   322
    unsigned char png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   323
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   324
    // Compare File signature.
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   325
    if ((int)(memcmp(&buffer[0], &png_signature[0], 8))) {
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   326
        DLog(@"The file (%@) is not a PNG file", aFileName);
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   327
        return CGSizeZero;
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   328
    }
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   329
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   330
    // Calc Sizes. Isolate only four bytes of each size (width, height).
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   331
    int width[4];
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   332
    int height[4];
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   333
    for (int d = 16; d < (16 + 4); d++) {
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   334
        width[d-16] = buffer[d];
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   335
        height[d-16] = buffer[d+4];
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   336
    }
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   337
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   338
    // Convert bytes to Long (Integer)
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   339
    long resultWidth = (width[0] << (int)24) | (width[1] << (int)16) | (width[2] << (int)8) | width[3];
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   340
    long resultHeight = (height[0] << (int)24) | (height[1] << (int)16) | (height[2] << (int)8) | height[3];
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   341
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   342
    // Return Size.
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   343
    return CGSizeMake(resultWidth,resultHeight);
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   344
}
8c0cc07731e5 headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents: 5503
diff changeset
   345
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   346
@end