author | nemo |
Sat, 05 Nov 2011 20:09:48 -0400 | |
changeset 6294 | 34aa727d4a25 |
parent 6209 | 074ab6ebcb3e |
child 6700 | e04da46ee43c |
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 { |
6209 | 28 |
// Create a bitmap graphics context; this will also set it as the current context |
29 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
|
30 |
CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, 4 * size.width, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
3697 | 31 |
|
6209 | 32 |
// draw the image inside the context |
33 |
CGFloat screenScale = [[UIScreen mainScreen] safeScale]; |
|
34 |
CGContextDrawImage(context, CGRectMake(0, 0, size.width*screenScale, size.height*screenScale), self.CGImage); |
|
3697 | 35 |
|
6209 | 36 |
// Create bitmap image info from pixel data in current context |
37 |
CGImageRef imageRef = CGBitmapContextCreateImage(context); |
|
3697 | 38 |
|
6209 | 39 |
// Create a new UIImage object |
40 |
UIImage *resultImage; |
|
41 |
if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
|
42 |
resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp]; |
|
43 |
else |
|
44 |
resultImage = [UIImage imageWithCGImage:imageRef]; |
|
3697 | 45 |
|
6209 | 46 |
// Release colorspace, context and bitmap information |
47 |
CGColorSpaceRelease(colorSpace); |
|
48 |
CGContextRelease(context); |
|
49 |
CFRelease(imageRef); |
|
3697 | 50 |
|
6209 | 51 |
return resultImage; |
3547 | 52 |
} |
53 |
||
54 |
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint { |
|
3778 | 55 |
if (secondImage == nil) { |
56 |
DLog(@"Warning, secondImage == nil"); |
|
57 |
return self; |
|
58 |
} |
|
6209 | 59 |
CGFloat screenScale = [[UIScreen mainScreen] safeScale]; |
4478 | 60 |
int w = self.size.width * screenScale; |
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 | 68 |
|
3573 | 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 | 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 | 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 | 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 | 81 |
UIImage *resultImage; |
82 |
if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
|
83 |
resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp]; |
|
84 |
else |
|
85 |
resultImage = [UIImage imageWithCGImage:imageRef]; |
|
3697 | 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 | 91 |
|
3547 | 92 |
return resultImage; |
93 |
} |
|
94 |
||
95 |
-(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect { |
|
96 |
// load image from path |
|
97 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: path]; |
|
3697 | 98 |
|
3547 | 99 |
if (nil != image) { |
100 |
// get its CGImage representation with a give size |
|
3621 | 101 |
CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect); |
3697 | 102 |
|
3547 | 103 |
// clean memory |
104 |
[image release]; |
|
3697 | 105 |
|
3547 | 106 |
// create a UIImage from the CGImage (memory must be allocated already) |
3621 | 107 |
UIImage *sprite = [self initWithCGImage:cgImage]; |
3697 | 108 |
|
3547 | 109 |
// clean memory |
3621 | 110 |
CGImageRelease(cgImage); |
3547 | 111 |
|
112 |
// return resulting image |
|
113 |
return sprite; |
|
114 |
} else { |
|
115 |
DLog(@"error - image == nil"); |
|
116 |
return nil; |
|
117 |
} |
|
118 |
} |
|
119 |
||
3621 | 120 |
-(UIImage *)cutAt:(CGRect) rect { |
121 |
CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect); |
|
3697 | 122 |
|
3621 | 123 |
UIImage *res = [UIImage imageWithCGImage:cgImage]; |
124 |
CGImageRelease(cgImage); |
|
3697 | 125 |
|
3621 | 126 |
return res; |
127 |
} |
|
128 |
||
3547 | 129 |
-(UIImage *)convertToGrayScale { |
3573 | 130 |
// Create image rectangle with current image width/height |
131 |
CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height); |
|
3697 | 132 |
|
3573 | 133 |
// Grayscale color space |
134 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); |
|
3697 | 135 |
|
3573 | 136 |
// Create bitmap content with current image size and grayscale colorspace |
137 |
CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone); |
|
3697 | 138 |
|
3573 | 139 |
// Draw image into current context, with specified rectangle |
140 |
// using previously defined context (with grayscale colorspace) |
|
141 |
CGContextDrawImage(context, imageRect, [self CGImage]); |
|
3697 | 142 |
|
3573 | 143 |
// Create bitmap image info from pixel data in current context |
144 |
CGImageRef imageRef = CGBitmapContextCreateImage(context); |
|
3697 | 145 |
|
146 |
// Create a new UIImage object |
|
3573 | 147 |
UIImage *newImage = [UIImage imageWithCGImage:imageRef]; |
3697 | 148 |
|
3573 | 149 |
// Release colorspace, context and bitmap information |
3978 | 150 |
CFRelease(imageRef); |
3573 | 151 |
CGContextRelease(context); |
3978 | 152 |
CGColorSpaceRelease(colorSpace); |
3697 | 153 |
|
3573 | 154 |
// Return the new grayscale image |
155 |
return newImage; |
|
3547 | 156 |
} |
157 |
||
158 |
// by http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html turned into a category by koda |
|
159 |
-(UIImage*) maskImageWith:(UIImage *)maskImage { |
|
3573 | 160 |
// prepare the reference image |
161 |
CGImageRef maskRef = [maskImage CGImage]; |
|
3697 | 162 |
|
3573 | 163 |
// create the mask using parameters of the mask reference |
3547 | 164 |
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), |
165 |
CGImageGetHeight(maskRef), |
|
166 |
CGImageGetBitsPerComponent(maskRef), |
|
167 |
CGImageGetBitsPerPixel(maskRef), |
|
168 |
CGImageGetBytesPerRow(maskRef), |
|
169 |
CGImageGetDataProvider(maskRef), NULL, false); |
|
3697 | 170 |
|
3573 | 171 |
// create an image in the current context |
3547 | 172 |
CGImageRef masked = CGImageCreateWithMask([self CGImage], mask); |
173 |
CGImageRelease(mask); |
|
3697 | 174 |
|
3547 | 175 |
UIImage* retImage = [UIImage imageWithCGImage:masked]; |
176 |
CGImageRelease(masked); |
|
3697 | 177 |
|
3547 | 178 |
return retImage; |
179 |
} |
|
180 |
||
181 |
// by http://blog.sallarp.com/iphone-uiimage-round-corners/ turned into a category by koda |
|
3573 | 182 |
void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) { |
183 |
CGFloat fw, fh; |
|
3547 | 184 |
if (ovalWidth == 0 || ovalHeight == 0) { |
185 |
CGContextAddRect(context, rect); |
|
186 |
return; |
|
187 |
} |
|
188 |
CGContextSaveGState(context); |
|
189 |
CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect)); |
|
190 |
CGContextScaleCTM (context, ovalWidth, ovalHeight); |
|
191 |
fw = CGRectGetWidth (rect) / ovalWidth; |
|
192 |
fh = CGRectGetHeight (rect) / ovalHeight; |
|
193 |
CGContextMoveToPoint(context, fw, fh/2); |
|
194 |
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); |
|
195 |
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); |
|
196 |
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); |
|
197 |
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); |
|
198 |
CGContextClosePath(context); |
|
199 |
CGContextRestoreGState(context); |
|
200 |
} |
|
201 |
||
3697 | 202 |
-(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh { |
3573 | 203 |
CGFloat cornerWidth = sizewh.width; |
204 |
CGFloat cornerHeight = sizewh.height; |
|
6209 | 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 | 208 |
|
3547 | 209 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
210 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
3697 | 211 |
|
3547 | 212 |
CGContextBeginPath(context); |
213 |
CGRect rect = CGRectMake(0, 0, w, h); |
|
214 |
addRoundedRectToPath(context, rect, cornerWidth, cornerHeight); |
|
215 |
CGContextClosePath(context); |
|
216 |
CGContextClip(context); |
|
3697 | 217 |
|
3573 | 218 |
CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]); |
3697 | 219 |
|
3547 | 220 |
CGImageRef imageMasked = CGBitmapContextCreateImage(context); |
221 |
CGContextRelease(context); |
|
222 |
CGColorSpaceRelease(colorSpace); |
|
3697 | 223 |
|
6209 | 224 |
UIImage *resultImage; |
225 |
if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
|
226 |
resultImage = [UIImage imageWithCGImage:imageMasked scale:screenScale orientation:UIImageOrientationUp]; |
|
227 |
else |
|
228 |
resultImage = [UIImage imageWithCGImage:imageMasked]; |
|
3547 | 229 |
CGImageRelease(imageMasked); |
3697 | 230 |
|
6209 | 231 |
return resultImage; |
3547 | 232 |
} |
233 |
||
3903 | 234 |
// by http://www.sixtemia.com/journal/2010/06/23/uiimage-negative-color-effect/ |
235 |
-(UIImage *)convertToNegative { |
|
236 |
UIGraphicsBeginImageContext(self.size); |
|
237 |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy); |
|
238 |
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)]; |
|
239 |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference); |
|
240 |
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor); |
|
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 | 243 |
UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); |
244 |
UIGraphicsEndImageContext(); |
|
245 |
return result; |
|
246 |
} |
|
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 | 249 |
CGFloat w = ofSize.width; |
250 |
CGFloat h = ofSize.height; |
|
4478 | 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 | 253 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
254 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
255 |
||
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 | 260 |
CGImageRef image = CGBitmapContextCreateImage(context); |
261 |
CGContextRelease(context); |
|
262 |
CGColorSpaceRelease(colorSpace); |
|
263 |
||
264 |
UIImage *bkgImg = [UIImage imageWithCGImage:image]; |
|
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 | 269 |
+(UIImage *)drawHogsRepeated:(NSInteger) manyTimes { |
270 |
NSString *imgString = [[NSString alloc] initWithFormat:@"%@/hedgehog.png",[[NSBundle mainBundle] resourcePath]]; |
|
271 |
UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:imgString]; |
|
272 |
[imgString release]; |
|
273 |
CGFloat screenScale = [[UIScreen mainScreen] safeScale]; |
|
274 |
int w = hogSprite.size.width * screenScale; |
|
275 |
int h = hogSprite.size.height * screenScale; |
|
276 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
|
277 |
CGContextRef context = CGBitmapContextCreate(NULL, w * 3, h, 8, 4 * w * 3, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
278 |
||
279 |
// draw the two images in the current context |
|
280 |
for (int i = 0; i < manyTimes; i++) |
|
281 |
CGContextDrawImage(context, CGRectMake(i*8*screenScale, 0, w, h), [hogSprite CGImage]); |
|
282 |
[hogSprite release]; |
|
283 |
||
284 |
// Create bitmap image info from pixel data in current context |
|
285 |
CGImageRef imageRef = CGBitmapContextCreateImage(context); |
|
286 |
||
287 |
// Create a new UIImage object |
|
288 |
UIImage *resultImage; |
|
289 |
if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
|
290 |
resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp]; |
|
291 |
else |
|
292 |
resultImage = [UIImage imageWithCGImage:imageRef]; |
|
293 |
||
294 |
// Release colorspace, context and bitmap information |
|
295 |
CGColorSpaceRelease(colorSpace); |
|
296 |
CGContextRelease(context); |
|
297 |
CFRelease(imageRef); |
|
298 |
||
299 |
return resultImage; |
|
300 |
} |
|
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 | 346 |
@end |