project_files/HedgewarsMobile/Classes/UIImageExtra.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 10108 c68cf030eded
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    20 #import "UIImageExtra.h"
    20 #import "UIImageExtra.h"
    21 
    21 
    22 
    22 
    23 @implementation UIImage (extra)
    23 @implementation UIImage (extra)
    24 
    24 
    25 -(UIImage *)scaleToSize:(CGSize) size {
    25 - (UIImage *)scaleToSize:(CGSize)size {
    26     // Create a bitmap graphics context; this will also set it as the current context
    26     // Create a bitmap graphics context; this will also set it as the current context
    27     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    27     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    28     CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, 4 * size.width, colorSpace, kCGImageAlphaPremultipliedFirst);
    28     CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, 4 * size.width, colorSpace, kCGImageAlphaPremultipliedFirst);
    29 
    29 
    30     // draw the image inside the context
    30     // draw the image inside the context
    47     CFRelease(imageRef);
    47     CFRelease(imageRef);
    48 
    48 
    49     return resultImage;
    49     return resultImage;
    50 }
    50 }
    51 
    51 
    52 -(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint {
    52 - (UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint)secondImagePoint {
    53     if (secondImage == nil) {
    53     if (secondImage == nil) {
    54         DLog(@"Warning, secondImage == nil");
    54         DLog(@"Warning, secondImage == nil");
    55         return self;
    55         return self;
    56     }
    56     }
    57     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
    57     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
    88     CFRelease(imageRef);
    88     CFRelease(imageRef);
    89 
    89 
    90     return resultImage;
    90     return resultImage;
    91 }
    91 }
    92 
    92 
    93 -(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
    93 - (id)initWithContentsOfFile:(NSString *)path andCutAt:(CGRect)rect {
    94     // load image from path
    94     // load image from path
    95     UIImage *image = [[UIImage alloc] initWithContentsOfFile: path];
    95     UIImage *image = [[UIImage alloc] initWithContentsOfFile: path];
    96 
    96 
    97     if (nil != image) {
    97     if (nil != image) {
    98         // get its CGImage representation with a give size
    98         // get its CGImage representation with a give size
    99         CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect);
    99         CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect);
   100 
   100 
   101         // clean memory
   101         // clean memory
   102         [image release];
       
   103 
   102 
   104         // create a UIImage from the CGImage (memory must be allocated already)
   103         // create a UIImage from the CGImage (memory must be allocated already)
   105         UIImage *sprite = [self initWithCGImage:cgImage];
   104         UIImage *sprite = [self initWithCGImage:cgImage];
   106 
   105 
   107         // clean memory
   106         // clean memory
   113         DLog(@"error - image == nil");
   112         DLog(@"error - image == nil");
   114         return nil;
   113         return nil;
   115     }
   114     }
   116 }
   115 }
   117 
   116 
   118 -(UIImage *)cutAt:(CGRect) rect {
   117 - (UIImage *)cutAt:(CGRect)rect {
   119     CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect);
   118     CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect);
   120 
   119 
   121     UIImage *res = [UIImage imageWithCGImage:cgImage];
   120     UIImage *res = [UIImage imageWithCGImage:cgImage];
   122     CGImageRelease(cgImage);
   121     CGImageRelease(cgImage);
   123 
   122 
   124     return res;
   123     return res;
   125 }
   124 }
   126 
   125 
   127 -(UIImage *)convertToGrayScale {
   126 - (UIImage *)convertToGrayScale {
   128     // Create image rectangle with current image width/height
   127     // Create image rectangle with current image width/height
   129     CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height);
   128     CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height);
   130 
   129 
   131     // Grayscale color space
   130     // Grayscale color space
   132     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
   131     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
   195     CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
   194     CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
   196     CGContextClosePath(context);
   195     CGContextClosePath(context);
   197     CGContextRestoreGState(context);
   196     CGContextRestoreGState(context);
   198 }
   197 }
   199 
   198 
   200 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
   199 - (UIImage *)makeRoundCornersOfSize:(CGSize)sizewh {
   201     CGFloat cornerWidth = sizewh.width;
   200     CGFloat cornerWidth = sizewh.width;
   202     CGFloat cornerHeight = sizewh.height;
   201     CGFloat cornerHeight = sizewh.height;
   203     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
   202     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
   204     CGFloat w = self.size.width * screenScale;
   203     CGFloat w = self.size.width * screenScale;
   205     CGFloat h = self.size.height * screenScale;
   204     CGFloat h = self.size.height * screenScale;
   228 
   227 
   229     return resultImage;
   228     return resultImage;
   230 }
   229 }
   231 
   230 
   232 // by http://www.sixtemia.com/journal/2010/06/23/uiimage-negative-color-effect/
   231 // by http://www.sixtemia.com/journal/2010/06/23/uiimage-negative-color-effect/
   233 -(UIImage *)convertToNegative {
   232 - (UIImage *)convertToNegative {
   234     UIGraphicsBeginImageContext(self.size);
   233     UIGraphicsBeginImageContext(self.size);
   235     CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
   234     CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
   236     [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
   235     [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
   237     CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
   236     CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
   238     CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
   237     CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
   241     UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
   240     UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
   242     UIGraphicsEndImageContext();
   241     UIGraphicsEndImageContext();
   243     return result;
   242     return result;
   244 }
   243 }
   245 
   244 
   246 +(UIImage *)whiteImage:(CGSize) ofSize {
   245 + (UIImage *)whiteImage:(CGSize)ofSize {
   247     CGFloat w = ofSize.width;
   246     CGFloat w = ofSize.width;
   248     CGFloat h = ofSize.height;
   247     CGFloat h = ofSize.height;
   249     DLog(@"w: %f, h: %f", w, h);
   248     DLog(@"w: %f, h: %f", w, h);
   250 
   249 
   251     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   250     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   262     UIImage *bkgImg = [UIImage imageWithCGImage:image];
   261     UIImage *bkgImg = [UIImage imageWithCGImage:image];
   263     CGImageRelease(image);
   262     CGImageRelease(image);
   264     return bkgImg;
   263     return bkgImg;
   265 }
   264 }
   266 
   265 
   267 +(UIImage *)drawHogsRepeated:(NSInteger) manyTimes {
   266 + (UIImage *)drawHogsRepeated:(NSInteger)manyTimes {
   268     NSString *imgString = [[NSString alloc] initWithFormat:@"%@/hedgehog.png",[[NSBundle mainBundle] resourcePath]];
   267     NSString *imgString = [[NSString alloc] initWithFormat:@"%@/hedgehog.png",[[NSBundle mainBundle] resourcePath]];
   269     UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:imgString];
   268     UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:imgString];
   270     [imgString release];
       
   271     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
   269     CGFloat screenScale = [[UIScreen mainScreen] safeScale];
   272     int w = hogSprite.size.width * screenScale;
   270     int w = hogSprite.size.width * screenScale;
   273     int h = hogSprite.size.height * screenScale;
   271     int h = hogSprite.size.height * screenScale;
   274     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   272     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   275     CGContextRef context = CGBitmapContextCreate(NULL, w * 3, h, 8, 4 * w * 3, colorSpace, kCGImageAlphaPremultipliedFirst);
   273     CGContextRef context = CGBitmapContextCreate(NULL, w * 3, h, 8, 4 * w * 3, colorSpace, kCGImageAlphaPremultipliedFirst);
   276 
   274 
   277     // draw the two images in the current context
   275     // draw the two images in the current context
   278     for (int i = 0; i < manyTimes; i++)
   276     for (int i = 0; i < manyTimes; i++)
   279         CGContextDrawImage(context, CGRectMake(i*8*screenScale, 0, w, h), [hogSprite CGImage]);
   277         CGContextDrawImage(context, CGRectMake(i*8*screenScale, 0, w, h), [hogSprite CGImage]);
   280     [hogSprite release];
       
   281 
   278 
   282     // Create bitmap image info from pixel data in current context
   279     // Create bitmap image info from pixel data in current context
   283     CGImageRef imageRef = CGBitmapContextCreateImage(context);
   280     CGImageRef imageRef = CGBitmapContextCreateImage(context);
   284 
   281 
   285     // Create a new UIImage object
   282     // Create a new UIImage object
   297     return resultImage;
   294     return resultImage;
   298 }
   295 }
   299 
   296 
   300 // this routine checks for the PNG size without loading it in memory
   297 // this routine checks for the PNG size without loading it in memory
   301 // https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m
   298 // https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m
   302 +(CGSize) imageSizeFromMetadataOf:(NSString *)aFileName {
   299 + (CGSize)imageSizeFromMetadataOf:(NSString *)aFileName {
   303     // File Name to C String.
   300     // File Name to C String.
   304     const char *fileName = [aFileName UTF8String];
   301     const char *fileName = [aFileName UTF8String];
   305     // source file
   302     // source file
   306     FILE *infile = fopen(fileName, "rb");
   303     FILE *infile = fopen(fileName, "rb");
   307     if (infile == NULL) {
   304     if (infile == NULL) {