ofImage/ofTexture -> UIImage?

Does anyone know how convert an ofImage or ofTexture to a UIImage?

I thought perhaps the simplest way would be to somehow get the pixels and convert them into an NSData object ( [NSData dataWithBytes] ), but I didn’t have any luck there.

Looking at the ofxiPhone methods for going from a UIImage to an ofImage, it seems like CoreGraphics methods get used. Wondering if anyone can help.

Thanks,
-Aaron

I’ve gone the other way with NSBitmapImageRep which apparently isn’t available on iOS. A quick search turned up the below so maybe looking for an iOS equivalent may lead to something.

http://www.iphonedevsdk.com/forum/iphone-sdk-development/1128-getting-data-uiimage.html

Original got marked as spam so hopefully this isn’t a dupe

I’ve gone the other way with NSBitmapImageRep which apparently isn’t available on iOS. A quick search turned up this

http://tinyurl.com/69rovjj

so maybe looking for an iOS equivalent may lead to something.

Hey there – I’m actually more interested in getting an ofImage or ofTexture out of an NSImage… I just posted about UIImage because I figured there were more people out there doing stuff on iPhone.

Any chance you have a code snippet handy for converting the NSImage to ofImage/ofTexture?

Thanks!

-Aaron

This is based off a semi-large project to connect a PSEye directly to OF but here are the important parts

https://github.com/jvcleave/PS3EyeWindow/blob/master/src/macam/MyCameraCentral.mm

Objective-C class that holds the NSBitmapImageRep

  
imageRep=[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL	//Set up just to avoid a NIL imageRep  
													 pixelsWide:cameraWidth  
													 pixelsHigh:cameraHeight  
												  bitsPerSample:8	  
												samplesPerPixel:3  
													   hasAlpha:NO  
													   isPlanar:NO  
												 colorSpaceName:NSDeviceRGBColorSpace  
													bytesPerRow:0  
												   bitsPerPixel:0];  

then when the image is updated I just send the pixels to my OF Class (myWrapper)

  
- (void) imageReady:(id)cam   
{  
	//NSLog(@"MyCameraCentral:imageReady");  
	myWrapper->onImageReady([imageRep bitmapData]);  
	[driver setImageBuffer:[driver imageBuffer] bpp:[driver imageBufferBPP] rowBytes:[driver imageBufferRowBytes]];  
}  

the OF based class (PS3EyeWrapper) then receives the pixels
https://github.com/jvcleave/PS3EyeWindow/blob/master/src/PS3EyeWrapper.mm

  
void PS3EyeWrapper::onImageReady (unsigned char * cameraPixels )   
{  
	pixels = cameraPixels;  
	frameNew = true;  
	//NSLog(@"PS3EyeWrapper::onImageReady");  
}  

and finally testApp takes them and puts them into an ofImage
https://github.com/jvcleave/PS3EyeWindow/blob/master/src/testApp.cpp

  
void testApp::update() {  
	if(wrapper.isFrameNew())  
	{  
		timer.tick();  
	}  
	ofSetWindowTitle("FPS:" + ofToString(ofGetFrameRate()));  
	cameraImage.setFromPixels(wrapper.pixels, wrapper.cameraWidth, wrapper.cameraHeight, OF_IMAGE_COLOR);  
}  

Looking again at my code I don’t use an NSImage but your should be able to get an NSBitmapImageRep from an NSImage with how this page describes it

The easiest way to do this is to use TIFFRepresentation? on the NSImage, then make a new NSBitmapImageRep from that.

http://www.cocoadev.com/index.pl?NSImage

Yea you were right on about NSBitmapImageRep. Definitely was the key. Here is an Obj-C function I wrote that takes the unsigned char pixels from an oFImage and converts it to NSData (which could easily be used to initialize an NSImage) and then writes it to a .png on the desktop.

  
  
- (void)savePngFrom:(unsigned char *)pixels withWidth:(int)width andHeight:(int)height {  
      
    NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&pixels pixelsWide:width pixelsHigh:height bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:0 bitsPerPixel:0];  
    // create an NSData object from the NSBitmapImageRep  
    NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:.7] forKey:NSImageCompressionFactor];  
    NSData *imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];  
    // write the image to the desktop  
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);  
    NSString * desktopPath = [paths objectAtIndex:0];  
    [imageData writeToFile:[NSString stringWithFormat:@"%@/aaron.jpg", desktopPath] atomically:NO];   
      
    [imageRep release];  
}  
  

Hey guys,

So I ran into this thread working on my own project. I’m taking frames from OpenCV and doing some pixel manipulation and finishing with ofImages. So this post is right up my alley and also my first oF ios app.

@Aaron I’m trying out your code by creating a new Obj-C class called ImageToData;

  
  
//  ImageToData.h  
#import <Foundation/Foundation.h>  
#import "imageLayering.h"  
@interface ImageToData : NSObject  
- (void)saveJpgFrom:(unsigned char *)pixels withWidth:(int)width andHeight:(int)height;  
@end  
imageLayering *myImage;  
//  
//  ImageToData.m  
#import "ImageToData.h"  
@implementation ImageToData  
- (void)saveJpgFrom:(unsigned char *)pixels withWidth:(int)width andHeight:(int)height {  
    NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&pixels pixelsWide:width pixelsHigh:height bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:0 bitsPerPixel:0];  
    // create an NSData object from the NSBitmapImageRep  
    NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:.7] forKey:NSImageCompressionFactor];  
    NSData *imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];  
    NSLog(@"Success");    
    [imageRep release];  
}  
@end  
// TestApp.h  
#include "ImageToData.h"  
ImageToData *imageData;  
// TestApp.cpp  
[imageData saveJpgFrom: myImage.finalDiffImg.getPixels() withWidth: width andHeight: height];  
  

So I’m getting errors saying that the imageRep is an undeclared variable ‘imageRep’.

Im also unsure if I’m using the code correctly.

Any help would be greatly appreciated.

Cheers,

JP