Cocoa in the Shell

by Nyx0uf

NYXImagesUtilities becomes NYXImagesKit

Jan 11, 2012

Few months ago I released NYXImagesUtilities, a set of categories to perform various operations on UIImage objects such as resizing, applying filters and more. Since then I didn't update it, but today I'm proud to announce that I've been working a lot on it lately to add new stuff and to focus on performances. For this occasion I decided to rename it NYXImagesKit

Introduction


The main idea was to focus on performances, how could I improve them ?

It turned out that Apple brought Accelerate.framework on iOS 4 so we can take advantages of vDSP functions. And it's even better on iOS 5, because we now have access to Core Image and vImage APIs in Accelerate.

The second idea was to add some functionalities, especially filters because there were only 3, now there are 11 !

The code still requires at least iOS 4.2, and is fully ARC enabled. The license has not changed and is Simplified BSD.

So, what's new ?!


I. UIImage+Enchancing


This is a new category which works only on iOS 5 because it uses Core Image. It is composed of two methods :

-(UIImage*)autoEnhance;
-(UIImage*)redEyeCorrection;

The names are pretty straitforward to guess what these methods do.

II. New filters


There are 8 new filters, let's review them with Sarah Shahi as a sample image.

Sarah Shahi

1. Brighten

This filter allows you to brighten or darken an image.

UIImage* brightened = [myImage brightenWithValue:13.0f];

Sarah Shahi brightened

The value should be in the range -255, 255.

2. Contrast adjustment

UIImage* contrasted = [myImage contrastAdjustmentWithValue:37.0f];

Sarah Shahi contrasted

The value should be in the range -255, 255.

3. Edge detection

This filter is used to detect edges on an image.

UIImage* edge = [myImage edgeDetectionWithBias:0];

Sarah Shahi edge detection

The bias value is only used on iOS 5.

4. Emboss

UIImage* emboss = [myImage embossWithBias:0];

Sarah Shahi embossed

The bias value is only used on iOS 5.

5. Gamma correction

UIImage* gammaCorrected = [myImage gammaCorrectionWithValue:2.0f];

Sarah Shahi gamma corrected

Good values for the gamma correction are in the range 0.01, 8

6. Invert

This filter inverts the colors of an image.

UIImage* inverted = [myImage invert];

Sarah Shahi inverted

7. Sharpen

UIImage* sharpened = [myImage sharpenWithBias:0];

Sarah Shahi sharpened

The bias value is only used on iOS 5.

8. Unsharpen

UIImage* unsharpened = [myImage unsharpenWithBias:0];

Sarah Shahi unsharpened

The bias value is only used on iOS 5.

III. New rotation methods


There are 2 new methods to rotate an image but they are only availabe on iOS 5 because they use vImage.

-(UIImage*)rotateImagePixelsInDegrees:(float)degrees;
-(UIImage*)rotateImagePixelsInRadians:(float)radians;

The difference between the other rotation methods is that the dimensions of the image won't change (here 640x960), it's the content that is being rotated, so the image is being truncated.

Sarah Shahi rotated

IV. Saving to Photos.app


I added a simple method in UIImage+Saving to save an UIImage to the Photos application.

[myImage saveToPhotosAlbum];


What changed ?


So now let's look at what has changed.

I. Blurring


I'm no longer using Jeff's code to perform the gaussian blur, instead I rewrote the method using vDSP for iOS 4, and vImage for iOS 5.

Here is a quick benchmark on an iPhone 4S with the same image. With the old method the average time to blur the image was 4.0s, so it was very very slow.

On iOS 4 with vDSP it took about 0.67s, 6x times faster.

On iOS 5 with vImage it took about 0.19s, 21x times faster.

So it's a very big win for this method.

II. Sepia filter


For iOS 4, I rewrote it using vDSP, it is more than 1.6x faster than before, so it's a bit better.

The big win is on iOS 5, because it uses Core Image. As a result it is 1.6x faster than the iOS 4 method, that's 2.5x faster than my original method. The other great thing is that the sepia tone from Core Image are nicer than with the manual method.

Sarah Shahi sepia

Conclusion


I hope you 'll like this new release, and feel free to contribute !

As before, you can get the code on github.