source: trunk/src/image/codec/ios-image.cpp @ 1513

Last change on this file since 1513 was 1513, checked in by sam, 7 years ago

core: replace usage of sin() or std::sin() with lol::sin() where appropriate.

  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined __APPLE__ && defined __MACH__ && defined __arm__
16
17#import <UIKit/UIKit.h>
18
19#include "core.h"
20#include "../../image/image-private.h"
21
22using namespace std;
23
24namespace lol
25{
26
27/*
28 * Image implementation class
29 */
30
31DECLARE_IMAGE_LOADER(IosImageData, 100)
32{
33public:
34    virtual bool Open(char const *);
35    virtual bool Close();
36
37    virtual void *GetData() const;
38
39private:
40    uint8_t *pixels;
41};
42
43/*
44 * Public Image class
45 */
46
47bool IosImageData::Open(char const *path)
48{
49    NSString *fullpath = [NSString stringWithUTF8String:path];
50    NSArray *chunks = [fullpath componentsSeparatedByString: @"/"];
51    NSString *filename = [chunks objectAtIndex: [chunks count] - 1];
52    chunks = [filename componentsSeparatedByString: @"."];
53    NSString *prefix = [chunks objectAtIndex: 0];
54    NSString *mypath = [[NSBundle mainBundle] pathForResource:prefix ofType:@"png"];
55    NSData *pngdata = [[NSData alloc] initWithContentsOfFile:mypath];
56    UIImage *image = [[UIImage alloc] initWithData:pngdata];
57    if (!image)
58    {
59#if !LOL_RELEASE
60        Log::Error("could not load %s\n", path);
61#endif
62        return false;
63    }
64
65    int w = CGImageGetWidth(image.CGImage);
66    int h = CGImageGetHeight(image.CGImage);
67    size = ivec2(w, h);
68    format = FORMAT_RGBA;
69
70    CGColorSpaceRef cspace = CGColorSpaceCreateDeviceRGB();
71    pixels = (uint8_t *)malloc(w * h * 4);
72    CGContextRef ctx =
73            CGBitmapContextCreate(pixels, w, h, 8, 4 * w, cspace,
74                    kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
75    CGColorSpaceRelease(cspace);
76    CGContextClearRect(ctx, CGRectMake(0, 0, w, h));
77    CGContextTranslateCTM(ctx, 0, h - h);
78    CGContextDrawImage(ctx, CGRectMake(0, 0, w, h), image.CGImage);
79    CGContextRelease(ctx);
80    [image release];
81    [pngdata release];
82
83    return true;
84}
85
86bool IosImageData::Close()
87{
88    free(pixels);
89
90    return true;
91}
92
93void * IosImageData::GetData() const
94{
95    return pixels;
96}
97
98} /* namespace lol */
99
100#endif /* defined __APPLE__ && defined __MACH__ && defined __arm__ */
101
Note: See TracBrowser for help on using the repository browser.