author | koda |
Sun, 02 Oct 2011 00:57:04 +0200 | |
changeset 6078 | 8c0cc07731e5 |
parent 5503 | d8632f589008 |
child 6080 | ce02ddfe8aa1 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
4976 | 3 |
* Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com> |
3829 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
* |
|
18 |
* File created on 08/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "UIImageExtra.h" |
|
23 |
||
24 |
||
25 |
@implementation UIImage (extra) |
|
3697 | 26 |
|
3547 | 27 |
-(UIImage *)scaleToSize:(CGSize) size { |
3573 | 28 |
DLog(@"warning - this is a very expensive operation, you should avoid using it"); |
3697 | 29 |
|
3573 | 30 |
// Create a bitmap graphics context; this will also set it as the current context |
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
|
31 |
UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]); |
3697 | 32 |
|
3573 | 33 |
// Draw the scaled image in the current context |
34 |
[self drawInRect:CGRectMake(0, 0, size.width, size.height)]; |
|
3697 | 35 |
|
3573 | 36 |
// Create a new image from current context |
37 |
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); |
|
3697 | 38 |
|
3573 | 39 |
// Pop the current context from the stack |
40 |
UIGraphicsEndImageContext(); |
|
3697 | 41 |
|
3573 | 42 |
// Return our new scaled image (autoreleased) |
43 |
return scaledImage; |
|
3547 | 44 |
} |
45 |
||
46 |
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint { |
|
3778 | 47 |
if (secondImage == nil) { |
48 |
DLog(@"Warning, secondImage == nil"); |
|
49 |
return self; |
|
50 |
} |
|
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
|
51 |
CGFloat screenScale = [[UIScreen mainScreen] scale]; |
4478 | 52 |
int w = self.size.width * screenScale; |
53 |
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
|
54 |
|
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
55 |
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
|
56 |
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
|
57 |
return self; |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
58 |
} |
3778 | 59 |
|
3573 | 60 |
// 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
|
61 |
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
|
62 |
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
|
63 |
|
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
64 |
// 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
|
65 |
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
|
66 |
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
|
67 |
|
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
68 |
// 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
|
69 |
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
|
70 |
|
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
71 |
// Create a new UIImage object |
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
|
72 |
UIImage *resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp]; |
3697 | 73 |
|
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
74 |
// 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
|
75 |
CGColorSpaceRelease(colorSpace); |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
76 |
CGContextRelease(context); |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
77 |
CFRelease(imageRef); |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
78 |
|
3547 | 79 |
return resultImage; |
80 |
} |
|
81 |
||
82 |
-(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect { |
|
83 |
// load image from path |
|
84 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: path]; |
|
3697 | 85 |
|
3547 | 86 |
if (nil != image) { |
87 |
// get its CGImage representation with a give size |
|
3621 | 88 |
CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect); |
3697 | 89 |
|
3547 | 90 |
// clean memory |
91 |
[image release]; |
|
3697 | 92 |
|
3547 | 93 |
// create a UIImage from the CGImage (memory must be allocated already) |
3621 | 94 |
UIImage *sprite = [self initWithCGImage:cgImage]; |
3697 | 95 |
|
3547 | 96 |
// clean memory |
3621 | 97 |
CGImageRelease(cgImage); |
3547 | 98 |
|
99 |
// return resulting image |
|
100 |
return sprite; |
|
101 |
} else { |
|
102 |
DLog(@"error - image == nil"); |
|
103 |
return nil; |
|
104 |
} |
|
105 |
} |
|
106 |
||
3621 | 107 |
-(UIImage *)cutAt:(CGRect) rect { |
108 |
CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect); |
|
3697 | 109 |
|
3621 | 110 |
UIImage *res = [UIImage imageWithCGImage:cgImage]; |
111 |
CGImageRelease(cgImage); |
|
3697 | 112 |
|
3621 | 113 |
return res; |
114 |
} |
|
115 |
||
3547 | 116 |
-(UIImage *)convertToGrayScale { |
3573 | 117 |
// Create image rectangle with current image width/height |
118 |
CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height); |
|
3697 | 119 |
|
3573 | 120 |
// Grayscale color space |
121 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); |
|
3697 | 122 |
|
3573 | 123 |
// Create bitmap content with current image size and grayscale colorspace |
124 |
CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone); |
|
3697 | 125 |
|
3573 | 126 |
// Draw image into current context, with specified rectangle |
127 |
// using previously defined context (with grayscale colorspace) |
|
128 |
CGContextDrawImage(context, imageRect, [self CGImage]); |
|
3697 | 129 |
|
3573 | 130 |
// Create bitmap image info from pixel data in current context |
131 |
CGImageRef imageRef = CGBitmapContextCreateImage(context); |
|
3697 | 132 |
|
133 |
// Create a new UIImage object |
|
3573 | 134 |
UIImage *newImage = [UIImage imageWithCGImage:imageRef]; |
3697 | 135 |
|
3573 | 136 |
// Release colorspace, context and bitmap information |
3978 | 137 |
CFRelease(imageRef); |
3573 | 138 |
CGContextRelease(context); |
3978 | 139 |
CGColorSpaceRelease(colorSpace); |
3697 | 140 |
|
3573 | 141 |
// Return the new grayscale image |
142 |
return newImage; |
|
3547 | 143 |
} |
144 |
||
145 |
// by http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html turned into a category by koda |
|
146 |
-(UIImage*) maskImageWith:(UIImage *)maskImage { |
|
3573 | 147 |
// prepare the reference image |
148 |
CGImageRef maskRef = [maskImage CGImage]; |
|
3697 | 149 |
|
3573 | 150 |
// create the mask using parameters of the mask reference |
3547 | 151 |
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), |
152 |
CGImageGetHeight(maskRef), |
|
153 |
CGImageGetBitsPerComponent(maskRef), |
|
154 |
CGImageGetBitsPerPixel(maskRef), |
|
155 |
CGImageGetBytesPerRow(maskRef), |
|
156 |
CGImageGetDataProvider(maskRef), NULL, false); |
|
3697 | 157 |
|
3573 | 158 |
// create an image in the current context |
3547 | 159 |
CGImageRef masked = CGImageCreateWithMask([self CGImage], mask); |
160 |
CGImageRelease(mask); |
|
3697 | 161 |
|
3547 | 162 |
UIImage* retImage = [UIImage imageWithCGImage:masked]; |
163 |
CGImageRelease(masked); |
|
3697 | 164 |
|
3547 | 165 |
return retImage; |
166 |
} |
|
167 |
||
168 |
// by http://blog.sallarp.com/iphone-uiimage-round-corners/ turned into a category by koda |
|
3573 | 169 |
void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) { |
170 |
CGFloat fw, fh; |
|
3547 | 171 |
if (ovalWidth == 0 || ovalHeight == 0) { |
172 |
CGContextAddRect(context, rect); |
|
173 |
return; |
|
174 |
} |
|
175 |
CGContextSaveGState(context); |
|
176 |
CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect)); |
|
177 |
CGContextScaleCTM (context, ovalWidth, ovalHeight); |
|
178 |
fw = CGRectGetWidth (rect) / ovalWidth; |
|
179 |
fh = CGRectGetHeight (rect) / ovalHeight; |
|
180 |
CGContextMoveToPoint(context, fw, fh/2); |
|
181 |
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); |
|
182 |
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); |
|
183 |
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); |
|
184 |
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); |
|
185 |
CGContextClosePath(context); |
|
186 |
CGContextRestoreGState(context); |
|
187 |
} |
|
188 |
||
3697 | 189 |
-(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh { |
3573 | 190 |
CGFloat cornerWidth = sizewh.width; |
191 |
CGFloat cornerHeight = sizewh.height; |
|
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
|
192 |
CGFloat screenScale = [[UIScreen mainScreen] scale]; |
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
193 |
CGFloat w = self.size.width * screenScale; |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
194 |
CGFloat h = self.size.height * screenScale; |
3697 | 195 |
|
3547 | 196 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
197 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
3697 | 198 |
|
3547 | 199 |
CGContextBeginPath(context); |
200 |
CGRect rect = CGRectMake(0, 0, w, h); |
|
201 |
addRoundedRectToPath(context, rect, cornerWidth, cornerHeight); |
|
202 |
CGContextClosePath(context); |
|
203 |
CGContextClip(context); |
|
3697 | 204 |
|
3573 | 205 |
CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]); |
3697 | 206 |
|
3547 | 207 |
CGImageRef imageMasked = CGBitmapContextCreateImage(context); |
208 |
CGContextRelease(context); |
|
209 |
CGColorSpaceRelease(colorSpace); |
|
3697 | 210 |
|
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
|
211 |
UIImage *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
|
212 |
|
3547 | 213 |
CGImageRelease(imageMasked); |
3697 | 214 |
|
3547 | 215 |
return newImage; |
216 |
} |
|
217 |
||
3903 | 218 |
// by http://www.sixtemia.com/journal/2010/06/23/uiimage-negative-color-effect/ |
219 |
-(UIImage *)convertToNegative { |
|
220 |
UIGraphicsBeginImageContext(self.size); |
|
221 |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy); |
|
222 |
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)]; |
|
223 |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference); |
|
224 |
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor); |
|
225 |
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
|
226 |
// create an image from the current contex (not thread safe) |
3903 | 227 |
UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); |
228 |
UIGraphicsEndImageContext(); |
|
229 |
return result; |
|
230 |
} |
|
231 |
||
3910
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
232 |
+(UIImage *)whiteImage:(CGSize) ofSize { |
4356 | 233 |
CGFloat w = ofSize.width; |
234 |
CGFloat h = ofSize.height; |
|
4478 | 235 |
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
|
236 |
|
4356 | 237 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
238 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
239 |
||
240 |
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
|
241 |
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
|
242 |
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
|
243 |
|
4356 | 244 |
CGImageRef image = CGBitmapContextCreateImage(context); |
245 |
CGContextRelease(context); |
|
246 |
CGColorSpaceRelease(colorSpace); |
|
247 |
||
248 |
UIImage *bkgImg = [UIImage imageWithCGImage:image]; |
|
249 |
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
|
250 |
return bkgImg; |
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
251 |
} |
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
252 |
|
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
|
253 |
// 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
|
254 |
// 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
|
255 |
+(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
|
256 |
// 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
|
257 |
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
|
258 |
// 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
|
259 |
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
|
260 |
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
|
261 |
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
|
262 |
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
|
263 |
} |
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
|
264 |
|
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
|
265 |
// 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
|
266 |
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
|
267 |
// 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
|
268 |
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
|
269 |
// 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
|
270 |
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
|
271 |
|
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
|
272 |
// 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
|
273 |
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
|
274 |
|
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
|
275 |
// 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
|
276 |
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
|
277 |
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
|
278 |
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
|
279 |
} |
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
|
280 |
|
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
|
281 |
// 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
|
282 |
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
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
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
|
287 |
} |
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
|
288 |
|
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
|
289 |
// 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
|
290 |
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
|
291 |
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
|
292 |
|
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
|
293 |
// 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
|
294 |
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
|
295 |
} |
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
|
296 |
|
3547 | 297 |
@end |