July 2008 Archives

Ever since I got the video feed working on my Packbot OCU application the reds and the blues have been swapped, giving the video feed a weird spacey look and totally cramping my style. Apparently the data is sent this way because leaving it in BGR format reduces the load for the Packbot's onboard computer. In other languages correcting this problem is a simple task, all you have to do is get the raw data and swap the red and blue components for each pixel. However I wasn't exactly clear how to retrieve the raw image data from a CGImageRef based on the Apple documentation. Using some creative search terms in Google this weekend I was able to piece together what I needed to get a proper ARGB image. The following code may not be the best way to do this, but it definitely works.

- (CGImageRef) CreateARGBImageWithABGRImage: (CGImageRef) abgrImageRef {

// the data retrieved from the image ref has 4 bytes per pixel (ABGR).

CFDataRef abgrData = CGDataProviderCopyData(CGImageGetDataProvider(abgrImageRef));

UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(abgrData);

int length = CFDataGetLength(abgrData);


// abgr to rgba

// swap the blue and red components for each pixel...

UInt8 tmpByte = 0;

for (int index = 0; index < length; index+= 4) {

tmpByte = pixelData[index + 1];

pixelData[index + 1] = pixelData[index + 3];

pixelData[index + 3] = tmpByte;

}

// grab the bgra image info

size_t width = CGImageGetWidth(abgrImageRef);

size_t height = CGImageGetHeight(abgrImageRef);

size_t bitsPerComponent = CGImageGetBitsPerComponent(abgrImageRef);

size_t bitsPerPixel = CGImageGetBitsPerPixel(abgrImageRef);

size_t bytesPerRow = CGImageGetBytesPerRow(abgrImageRef);

CGColorSpaceRef colorspace = CGImageGetColorSpace(abgrImageRef);

CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(abgrImageRef);

// create the argb image

CFDataRef argbData = CFDataCreate(NULL, pixelData, length);

CGDataProviderRef provider = CGDataProviderCreateWithCFData(argbData);

CGImageRef argbImageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, 

colorspace, bitmapInfo, provider, NULL, true, kCGRenderingIntentDefault);

// release what we can

CFRelease(abgrData);

CFRelease(argbData);

CGDataProviderRelease(provider);

// return the pretty new image

return argbImageRef;

}


So there you have it. I hope this helps someone else. Suggestions for improvements on this are certainly welcome.

I had wanted to do something like this since I got the iPhone last summer. At the moment it's a rough draft of sorts. The current interface shows the video feed from the Packbot, and provides a directional pad for driving the robot and adjusting the flippers. I feel like I have the basic code needed to communicate with the Packbot, but what's really interesting are the possibilities introduced by the iPhone's unique interface, so that's the next step. I plan to experiment with the iPhone's accelerometers, and with the multi-touch interface for interacting with the robot. In the YouTube video below you can see some promising initial results of the work:


Here's some of the press that this got on the bigger blogs around the net:


I'd like to thank Jeff Craighead, Kevin Pratt, and Greg Higley for helping me out in various ways while working on this project.
At last I have a super fancy blogging getup! I've been meaning to do this for a while, but kept putting it off. I'm hoping to use this space as a means to solidify my thoughts, perhaps get input on them, and by putting my thoughts out in the open I think it will encourage me to follow through slightly more than I have in the past.

As I set this blog up the first thing I notice is how much iWeb does not like to play with anything that wasn't created in iWeb. All I want to do is have an external link pointing to this blog in the navigation menu, and it can't be done without editing the HTML after i publish the site. It's easy enough to do, but the whole reason I went with iWeb was so I wouldn't have to worry about touching HTML. I think in time I will have to transition over to a site creation app that is a little less picky.

So here's to keeping this thing up to date, and getting a reasonably usable and informative site.