cocoaTouch/otherSrc/UIImageExtra.m
author koda
Sun, 16 May 2010 17:23:30 +0000
changeset 3463 23c50be687a9
parent 3361 cfc6cd502f85
child 3492 07256e1ad559
permissions -rw-r--r--
update sdl functions to latest revision add a grayscale utility to uiimage implement a preliminary support for chatting revert rotation changes for engine lots of code cleanup restored main event loop in hwengine fix some sdl bindings
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3352
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
     1
//
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
     2
//  UIImageExtra.m
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
     3
//  HedgewarsMobile
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
     4
//
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
     5
//  Created by Vittorio on 08/04/10.
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
     7
//
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
     8
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
     9
#import "UIImageExtra.h"
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    10
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    11
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    12
@implementation UIImage (extra)
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    13
 
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    14
-(UIImage *)scaleToSize:(CGSize) size {
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    15
  // Create a bitmap graphics context
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    16
  // This will also set it as the current context
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    17
  UIGraphicsBeginImageContext(size);
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    18
 
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    19
  // Draw the scaled image in the current context
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    20
  [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    21
 
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    22
  // Create a new image from current context
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    23
  UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    24
 
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    25
  // Pop the current context from the stack
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    26
  UIGraphicsEndImageContext();
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    27
 
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    28
  // Return our new scaled image (autoreleased)
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    29
  return scaledImage;
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    30
}
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    31
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    32
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    33
    // create a contex of size of the background image
3361
cfc6cd502f85 buttons for number of hogs in game config
koda
parents: 3352
diff changeset
    34
    return [self mergeWith:secondImage atPoint:secondImagePoint atSize:self.size];
cfc6cd502f85 buttons for number of hogs in game config
koda
parents: 3352
diff changeset
    35
}
cfc6cd502f85 buttons for number of hogs in game config
koda
parents: 3352
diff changeset
    36
cfc6cd502f85 buttons for number of hogs in game config
koda
parents: 3352
diff changeset
    37
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint atSize:(CGSize) resultingSize {
cfc6cd502f85 buttons for number of hogs in game config
koda
parents: 3352
diff changeset
    38
    UIGraphicsBeginImageContext(resultingSize);
3352
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    39
    
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    40
    // drav the background image
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    41
    [self drawAtPoint:CGPointMake(0,0)];
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    42
    
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    43
    // draw the image on top of the first image
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    44
    [secondImage drawAtPoint:secondImagePoint];
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    45
    
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    46
    // create an image from the current contex (not thread safe)
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    47
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    48
    
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    49
    // free drawing contex
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    50
    UIGraphicsEndImageContext();
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    51
    
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    52
    // return the resulting autoreleased image
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    53
    return resultImage;
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    54
}
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    55
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    56
-(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    57
    // load image from path
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    58
    UIImage *image = [[UIImage alloc] initWithContentsOfFile: path];
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    59
    
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    60
    if (nil != image) {
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    61
        // get its CGImage representation with a give size
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    62
        CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], rect);
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    63
    
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    64
        // clean memory
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    65
        [image release];
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    66
    
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    67
        // create a UIImage from the CGImage (memory must be allocated already)
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    68
        UIImage *sprite = [self initWithCGImage:cgImgage];
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    69
    
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    70
        // clean memory
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    71
        CGImageRelease(cgImgage);
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    72
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    73
        // return resulting image
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    74
        return sprite;
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    75
    } else {
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    76
        NSLog(@"initWithContentsOfFile: andCutAt: FAILED");
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    77
        return nil;
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    78
    }
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
    79
}
3463
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    80
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    81
-(UIImage *)convertImageToGrayScale:(UIImage *)image {
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    82
  // Create image rectangle with current image width/height
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    83
  CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    84
 
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    85
  // Grayscale color space
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    86
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    87
 
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    88
  // Create bitmap content with current image size and grayscale colorspace
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    89
  CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    90
 
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    91
  // Draw image into current context, with specified rectangle
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    92
  // using previously defined context (with grayscale colorspace)
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    93
  CGContextDrawImage(context, imageRect, [image CGImage]);
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    94
 
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    95
  // Create bitmap image info from pixel data in current context
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    96
  CGImageRef imageRef = CGBitmapContextCreateImage(context);
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    97
 
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    98
  // Create a new UIImage object  
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
    99
  UIImage *newImage = [UIImage imageWithCGImage:imageRef];
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
   100
 
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
   101
  // Release colorspace, context and bitmap information
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
   102
  CGColorSpaceRelease(colorSpace);
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
   103
  CGContextRelease(context);
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
   104
  CFRelease(imageRef);
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
   105
 
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
   106
  // Return the new grayscale image
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
   107
  return newImage;
23c50be687a9 update sdl functions to latest revision
koda
parents: 3361
diff changeset
   108
}
3352
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
   109
 
ac5d14a35482 complete previews on the team settings
koda
parents:
diff changeset
   110
@end