Thanks for the info Walter, it’s the first CL response I’ve got regarding enterprise features!
First off per your comment, it would be useful with enterprise to have a coronaLabs sanctioned function / var to get at the SDK’s openGL back /buffers to copy pixels out (which were composed by standard app lua code). It would be nice to be able to get at / have a CL defined function / protocol to get at (interface/read/write/or even point to) an SDK lua side display.newImage() buffer as well of course - but I digress… (I can throw out 5 more useful, generic enterprise <–> sdk funcs that would be helpful if you have a lotta spare time!)
Without any info on getting at the apps openGL buffer, I ended up with the following enterprise code hot mess to grab an x,y width, height area of the apps screen when it is called from the apps lua side code.
Took a while, and 8-10 totally different buffer reading approaches until a modified version of “Brad Larsens” example from stack overflow was able to get at the corona sdk opengl buffer (Probably a bug that i can at all). But hey, it works, and I get 70% jpg compression (instead of the default) iOS jpg compression.
Probably make the compression % a variable in the future, but as they say, “it’s good enough for government work”. (I really hope you don’t “fix” this and my code breaks Walter! I’m offering this with the intent you get an idea of what hoops we jump through to get at some things…)
Disclaimer: I’m a freshman native coder, so the following is probably buggy, deprecated, and ugly. And it gets bad gas mileage. Lot’s of commented out things that didn’t work and debug print statements…
PluginLibrary::saveJPG( lua_State *L )
{
NSLog(@"-saveJPG called.");
int num = lua_gettop( L);
NSLog(@" num args: : %d", num);
NSLog(@" --------------");
// Argument 1 passed from lua code… the source filename – not used with final technique, read from screen…
NSLog(@" --------------");
int ret = lua_isstring( L, 1);
NSLog(@" arg1 is string: %d", ret);
const char *sourceFilename = lua_tostring( L, 1 );
NSLog(@" source fileName : %s", sourceFilename);
// Argument 2… the dest filename, code saves a jpg out from screen with this name
NSLog(@" --------------");
ret = lua_isstring( L, 2);
NSLog(@" arg2 is string: %d", ret);
const char *destFilename = lua_tostring( L, 2 );
NSLog(@" dest fileName : %s", destFilename);
ret = lua_isnumber( L, 3);
NSLog(@" arg3 is number: %d", ret);
int x1 = lua_tonumber( L, 3); // Top Left
NSLog(@" arg3 is number: %d", x1);
ret = lua_isnumber( L, 4);
NSLog(@" arg4 is number: %d", ret);
int y1 = lua_tonumber( L, 4); // Top Left
NSLog(@" arg4 is number: %d", y1);
ret = lua_isnumber( L, 5);
NSLog(@" arg5 is number: %d", ret);
int x2 = lua_tonumber( L, 5);
NSLog(@" arg5 is number: %d", x2);
ret = lua_isnumber( L, 6);
NSLog(@" arg6 is number: %d", ret);
int y2 = lua_tonumber( L, 6);
NSLog(@" arg6 is number: %d", y2);
if( 1) // Brad Larson example
{
id<CoronaRuntime> runtime = (id<CoronaRuntime>)CoronaLuaGetContext( L );
EAGLContext *coronaContext = nil;
coronaContext = [EAGLContext currentContext];
GLint width;
GLint height;
// glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
// glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
width = x2-x1; // passed in args
height = y2-y1;
NSLog(@“GL w,h == %d %d”,width,height);
NSInteger myDataLength = width * height * 4;
// eaglLayer.drawableProperties = @{ kEAGLDrawablePropertyRetainedBacking: [NSNumber numberWithBool:YES], kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8 };
// [runtime.appWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
// [runtime.appViewController.view.layer renderInContext:UIGraphicsGetCurrentContext()];
// [runtime.appViewController.view.layer renderInContext:coronaContext];
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(x1,y1, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, myDataLength, NULL);
// CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// then make the uiimage from that
UIImage *viewImage = [UIImage imageWithCGImage:imageRef];
// Save the file
NSString *jpgBasename = [[NSString alloc] initWithUTF8String:destFilename]; // Passed in arg
NSString *jpgPath = [NSTemporaryDirectory() stringByAppendingPathComponent:jpgBasename];
NSLog(@“JPG Path: %@”,jpgPath);
[UIImageJPEGRepresentation(viewImage, 0.7) writeToFile:jpgPath atomically:YES];
NSLog(@“JPEG written (6)?!”); // 6th different technique to get at gl buffer…
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);
free(buffer);
// 3. Restore Corona’s context
[EAGLContext setCurrentContext:coronaContext];
}
return 0;
}