forked from julapy/ofxFlashLite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathofxFlashLibraryLoaderIOS.mm
More file actions
103 lines (75 loc) · 2.72 KB
/
Copy pathofxFlashLibraryLoaderIOS.mm
File metadata and controls
103 lines (75 loc) · 2.72 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* ofxFlashLibraryLoaderIOS.mm
* emptyExample
*
* Created by lukasz karluk on 9/02/11.
* Copyright 2011 __MyCompanyName__. All rights reserved.
*
*/
#include "ofxFlashLibraryLoaderIOS.h"
#ifdef TARGET_OF_IPHONE
#include "ofxiPhone.h"
#include "ofxiPhoneExtras.h"
ofxFlashLibraryLoaderIOS* ofxFlashLibraryLoaderIOS :: _instance = NULL;
ofBaseDraws* ofxFlashLibraryLoaderIOS :: loadImage( string path )
{
NSString* imagePath;
UIImage* image;
imagePath = ofxStringToNSString( ofToDataPath( path ) );
image = [ [ UIImage alloc ] initWithContentsOfFile : imagePath ];
ofTexture* tex = new ofTexture();
convertUIImageToOFTexture( image, *tex );
[ image release ];
return tex;
}
ofBaseDraws* ofxFlashLibraryLoaderIOS :: loadVideo( string videoPath )
{
return NULL;
}
/////////////////////////////////////////////
// CONVERTION.
/////////////////////////////////////////////
bool ofxFlashLibraryLoaderIOS :: convertUIImageToOFTexture( void* uiImagePtr, ofTexture &outTexture, int targetWidth, int targetHeight )
{
UIImage* uiImage;
uiImage = (UIImage*)uiImagePtr;
if(!uiImage) return false;
CGContextRef spriteContext;
CGImageRef cgImage = uiImage.CGImage;
int bytesPerPixel = CGImageGetBitsPerPixel(cgImage)/8;
if(bytesPerPixel == 3) bytesPerPixel = 4;
int width = targetWidth > 0 ? targetWidth : CGImageGetWidth(cgImage);
int height = targetHeight > 0 ? targetHeight : CGImageGetHeight(cgImage);
// Allocated memory needed for the bitmap context
GLubyte *pixels = (GLubyte *) malloc(width * height * bytesPerPixel);
// Uses the bitmatp creation function provided by the Core Graphics framework.
spriteContext = CGBitmapContextCreate(pixels, width, height, CGImageGetBitsPerComponent(cgImage), width * bytesPerPixel, CGImageGetColorSpace(cgImage), bytesPerPixel == 4 ? kCGImageAlphaPremultipliedLast : kCGImageAlphaNone);
if(spriteContext == NULL) {
ofLog(OF_LOG_ERROR, "iPhoneUIImageToOFImage - CGBitmapContextCreate returned NULL");
free(pixels);
return false;
}
// After you create the context, you can draw the sprite image to the context.
ofLog(OF_LOG_VERBOSE, "about to CGContextDrawImage");
CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), cgImage);
// You don't need the context at this point, so you need to release it to avoid memory leaks.
ofLog(OF_LOG_VERBOSE, "about to CGContextRelease");
CGContextRelease(spriteContext);
int glMode;
switch(bytesPerPixel) {
case 1:
glMode = GL_LUMINANCE;
break;
case 3:
glMode = GL_RGB;
break;
case 4:
default:
glMode = GL_RGBA; break;
}
outTexture.allocate(width, height, glMode);
outTexture.loadData(pixels, width, height, glMode);
free(pixels);
return true;
}
#endif