Tristan
Tristan

Reputation: 3068

8-bitify image in iOS

In an application I'm working on in iOS, I need to get a picture from the user, then convert it to that blocky, pixelated 8-bit style that we all love. I assume I use some Quartz/CoreImage (I've got my Mac and iOS dev mixed up methinks) filter to active this? I've never used any of these, so any help would be appreciated.

Upvotes: 2

Views: 2158

Answers (2)

Rob Keniger
Rob Keniger

Reputation: 46020

You could use Core Image, specifically the CIColorPosterize and CIPixellate filters. Core Image has the advantage that it takes advantage of the GPU and will be a LOT faster than manually manipulating the bitmap.

A simple example would be something like:

NSURL* imageURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForImageResource:@"yourSourceImage"]];
CIImage* inputImage = [[CIImage alloc] initWithContentsOfURL:imageURL];

CIFilter* posterize = [CIFilter filterWithName:@"CIColorPosterize"];
[posterize setDefaults];
[posterize setValue:[NSNumber numberWithDouble:8.0] forKey:@"inputLevels"];
[posterize setValue:inputImage forKey:@"inputImage"];

CIFilter* pixellate = [CIFilter filterWithName:@"CIPixellate"];
[pixellate setDefaults];
[pixellate setValue:[NSNumber numberWithDouble:4.0] forKey:@"inputScale"];
[pixellate setValue:[posterize valueForKey:@"outputImage"] forKey:@"inputImage"];

CIImage* outputImage = [pixellate valueForKey:@"outputImage"];
[inputImage release];

//do something with outputImage

You should play around with the "Core Image Fun House" app which you can find in /Developer/Applications/Graphics Tools to obtain values for the filters that give you the result you want.

You should bear in mind that Core Image is only available as of iOS 5.

Upvotes: 4

justin
justin

Reputation: 104698

This process is very simple to write yourself. You can use CoreGraphics to render to a bitmap using a CGImage and a CGBitmapContext. Then manipulate the pixel values directly.

8 bit representations would either:

  • use CLUTs/color maps
  • or RRRGGGBB format

The former is more complicated. If this is new to you, then begin by converting the pixel values to approximate RRRGGGBB. Assuming you know how to bitshift and create a bitmap via CoreGraphics (plenty of examples exist), you're all set!

Upvotes: 1

Related Questions