forked from jfahrenkrug/VertexHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PNGLoader.cpp
39 lines (31 loc) · 1.19 KB
/
PNGLoader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* PNGLoader.cpp
* VertexHelper
*
* Created by Peter Siroki on 2010.06.21.
*
*/
#include "PNGLoader.h"
// Currently it uses CoreGraphics,
// libpng would be preferable on other platforms
// On Mac it would just bloat the build unnecessarily
#import <ApplicationServices/ApplicationServices.h>
extern "C" void loadPNG(const char *fn, ImageDesc *output)
{
CGDataProviderRef data = CGDataProviderCreateWithFilename(fn);
CGImageRef img = CGImageCreateWithPNGDataProvider(data, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(data);
int width, height, pitch;
width = output->width = CGImageGetWidth(img);
height = output->height = CGImageGetHeight(img);
pitch = output->pitch = width*4;
UInt8 *bits = (UInt8*)malloc(width * height * 4);
CGContextRef bitmapContext = CGBitmapContextCreate(bits, width, height, 8, pitch,
CGImageGetColorSpace(img), kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(bitmapContext, 0, output->height);
CGContextScaleCTM(bitmapContext, 1.0, -1.0);
CGContextDrawImage(bitmapContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), img);
CGContextRelease(bitmapContext);
output->data = bits;
CGImageRelease(img);
}