Reputation: 51
Is there any example of passing bitmap data into iOS and returning a modfified copy. I am trying to convert a bitmap into jpeg - similar to this example in Windows http://blog.magicalhobo.com/2011/09/12/air-3-native-extension-imagepro cessor/ but it crashed every time.
I am pretty sure I am doing something basic wrong but not been an iOS / Objective C programmer I am really just trying magic incantations and I can't find any good examples. Any help appreciated?
Sean
P.S. Below is my attempt:
FREObject encodeJPEG(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
{
FREObject objectBitmapData = argv[0];
FREBitmapData bitmapData;
FREAcquireBitmapData(objectBitmapData, &bitmapData);
int width = bitmapData.width;
int height = bitmapData.height;
int stride = bitmapData.lineStride32 * 4;
uint32_t* input = bitmapData.bits32;
FREReleaseBitmapData(objectBitmapData);
UIImage *myImage = [UIImage imageWithData:input];
NSData *jpgData = UIImageJPEGRepresentation(myImage, 0.9);
FREObject objectByteArray = argv[1];
FREByteArray byteArray;
FREObject length;
FRENewObjectFromUint32(jpgData.length, &length);
FRESetObjectProperty(objectByteArray, (const uint8_t*) "length", length, NULL);
FREAcquireByteArray(objectByteArray, &byteArray);
memcpy(byteArray.bytes, jpgData.bytes, jpgData.length);
FREReleaseByteArray(objectByteArray);
return NULL;
}
Upvotes: 2
Views: 3337
Reputation: 1
Exchange the position of Red and Blue Pixels
the format you get from iOS is : 0xAARRGGBB
and the one expected to show on an AIR app is : 0xAABBGGRR
Upvotes: 0
Reputation: 51
Finale version - seems to work ok - encodes ok and I think cleans up after itself ok.
Sean
FREObject encodeJPEG( FREContext ctx, void* funcData, uint32_t argc, FREObject argv[] )
{
FREObject objectBitmapData = argv[ 0 ];
FREBitmapData2 bitmapData;
FREAcquireBitmapData2( objectBitmapData, &bitmapData );
int width = bitmapData.width;
int height = bitmapData.height;
int stride = bitmapData.lineStride32 * 4;
uint32_t* input = bitmapData.bits32;
// make data provider from buffer
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bitmapData.bits32, (width * height * 4), NULL);
// set up for CGImage creation
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo;
if( bitmapData.hasAlpha )
{
if( bitmapData.isPremultiplied )
bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst;
else
bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaFirst;
}
else
{
bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst;
}
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// make UIImage from CGImage
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
NSData* jpgData = UIImageJPEGRepresentation( myImage, 0.9 );
FREReleaseBitmapData( objectBitmapData );
FREObject objectByteArray = argv[ 1 ];
FREByteArray byteArray;
FREObject length;
FRENewObjectFromUint32( jpgData.length, &length );
FRESetObjectProperty( objectByteArray, ( const uint8_t* ) "length", length, NULL );
FREAcquireByteArray( objectByteArray, &byteArray );
memcpy( byteArray.bytes, jpgData.bytes, jpgData.length );
FREReleaseByteArray( objectByteArray );
// release bits
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
return NULL;
}
Upvotes: 3
Reputation: 51
This kinda works except reds are blues in jpeg and not sure I am releasing everything ok.
Thats tomorrows battle.
Sean
FREObject encodeJPEG( FREContext ctx, void* funcData, uint32_t argc, FREObject argv[] )
{
FREObject objectBitmapData = argv[ 0 ];
FREBitmapData2 bitmapData;
FREAcquireBitmapData2( objectBitmapData, &bitmapData );
int width = bitmapData.width;
int height = bitmapData.height;
int stride = bitmapData.lineStride32 * 4;
uint32_t* input = bitmapData.bits32;
// make data provider from buffer
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bitmapData.bits32, (width * height * 4), NULL);
// set up for CGImage creation
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// make UIImage from CGImage
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
NSData* jpgData = UIImageJPEGRepresentation( myImage, 0.9 );
FREReleaseBitmapData( objectBitmapData );
FREObject objectByteArray = argv[ 1 ];
FREByteArray byteArray;
FREObject length;
FRENewObjectFromUint32( jpgData.length, &length );
FRESetObjectProperty( objectByteArray, ( const uint8_t* ) "length", length, NULL );
FREAcquireByteArray( objectByteArray, &byteArray );
memcpy( byteArray.bytes, jpgData.bytes, jpgData.length );
FREReleaseByteArray( objectByteArray );
return NULL;
}
Upvotes: 0