UXDE dot Net
Advertisement

Image filter Processing Implementation in Xcode for iPhone Using GLImageProcessing iOS Developer Library

By -

For More Source Codes Visit Image Cropping Application Source Code for iOS

 

Sharpness

Note : Here we will provide content related to GLImageProcessing if you want application which is already developed then you need to comment you will receive application in your mail and share your knowledge more enhancement about these filters.

  • We will provide you  GLImageProcessing Sample Application according to rule given above in the note.
  • GLImageProcessing sample application explains how to implement simple image processing filter using Opengl ES 1.1
  • This Application Covers following Techniques of Image Processing filters.

     (1) Contrast:

Contrast

 (2) Brightness :

      brightness

 (3)Saturation :

Saturation

(4) Hue :

(5) Sharpness :

  • Further Details:

  • In the code snippet section you can see how to setup OpenGL ES View and use it for applying filter to an image.
  • To use this application just get the source code by commenting and get source code in your email and then open the project and build the project.use Slider to apply a filter and each filter is applied at a time.
  • The Source codes the following Resource files.
  1. View Controller.h
  2. View Controller.m
  • These Controllers Redraws the image when any filter is applied or as a result UI Actions.
  1. EagleView.h
  2. EagleView.m
  • This Class Wraps this layer class for CoreAnimation into UIView Subclass.
  1. Debug.h
  2. Debug.c
  • Debug Class Debugs the real time errors.
  1. Imaging.h
  2. Imaging.c
  • Image Processing Class Using OpenGL ES1.1
  1. Main.m
  • Starting of the Program of Image filter Processing.
  1. Texture.h
  2. Texture.m
  • Loads the Image Using CGImage to create OPenGL 2B image Texture.

Code  :

  • Main:


#import <UIKit/UIKit.h>int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
  • Class EaglView.h:

#import <UIKit/UIKit.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>@interface EAGLView : UIView {@private
// The pixel dimensions of the backbuffer
GLint backingWidth;
GLint backingHeight;EAGLContext *context;
IBOutlet UISlider *slider;
IBOutlet UITabBar *tabBar;// OpenGL names for the renderbuffer and framebuffer used to render to this view
GLuint viewRenderbuffer, viewFramebuffer;
}@property (nonatomic, retain) UISlider *slider;
@property (nonatomic, retain) UITabBar *tabBar;- (void)drawView;@end

 

  • Class EaglView.m

#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>
#import “EAGLView.h”
#import “Imaging.h”// A class extension to declare private methods
@interface EAGLView ()@property (nonatomic, retain) EAGLContext *context;@end@implementation EAGLView@synthesize context;
@synthesize slider;
@synthesize tabBar;// You must implement this method
+ (Class)layerClass
{
return [CAEAGLLayer class];
}// The GL view is stored in the nib file. When it’s unarchived it’s sent -initWithCoder:
– (id)initWithCoder:(NSCoder*)coder
{
if ((self = [super initWithCoder:coder]))
{
// Get the layer
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = YES;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
nil];context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!context || ![EAGLContext setCurrentContext:context])
{
[self release];
return nil;
}
// Create system framebuffer object. The backing will be allocated in -reshapeFramebuffer
glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);// Perform additional one-time GL initialization
initGL();
}
return self;
}- (void)drawView
{
int mode = self.tabBar.selectedItem.tag;
float value = self.slider.value;// This application only creates a single GL context, so it is already current here.
// If there are multiple contexts, ensure the correct one is current before drawing.
drawGL(backingWidth, backingHeight, value, mode);// This application only creates a single (color) renderbuffer, so it is already bound here.
// If there are multiple renderbuffers (for example color and depth), ensure the correct one is bound here.
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}- (void)reshapeFramebuffer
{
// Allocate GL color buffer backing, matching the current layer size
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);// This application only needs color. If depth and/or stencil are needed, ensure they are also resized here.
rt_assert(GL_FRAMEBUFFER_COMPLETE_OES == glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
glCheckError();
}- (void)layoutSubviews
{
[self reshapeFramebuffer];
[self drawView];
}- (void)dealloc
{
if ([EAGLContext currentContext] == context)
[EAGLContext setCurrentContext:nil];self.context = nil;
self.slider = nil;
self.tabBar = nil;
[super dealloc];
}@end

 

  • Class ViewController.h

#import <UIKit/UIKit.h>@interface ViewController : UIViewController
{
IBOutlet UISlider *slider;
IBOutlet UITabBar *tabBar;
}@property (nonatomic, retain) UISlider *slider;
@property (nonatomic, retain) UITabBar *tabBar;- (IBAction)sliderAction:(id)sender;@end

 

  • Class ViewController.m:

 

#import “ViewController.h”
#import “EAGLView.h”#define DEG2RAD (M_PI/180.0f)// These enums match the button tags in the nib
enum {
BUTTON_BRIGHTNESS,
BUTTON_CONTRAST,
BUTTON_SATURATION,
BUTTON_HUE,
BUTTON_SHARPNESS,
NUM_BUTTONS
};@implementation ViewController@synthesize slider;
@synthesize tabBar;- (void)viewDidLoad
{
int b, i;// Select first tab by default
tabBar.selectedItem = [tabBar.items objectAtIndex:0];// Create a bitmap context for rendering the tabBar buttons
// Usually, button images are loaded from disk, but these simple shapes can be procedurally generated.
// UITabBar only needs the alpha channel of these images.
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil, 30, 30, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
CGImageRef theCGImage;// Draw with white round strokes
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 2.0);for (b = 0; b < NUM_BUTTONS; b++)
{
CGContextClearRect(context, CGRectMake(0, 0, 30, 30));switch(b)
{
case BUTTON_BRIGHTNESS:
{
const CGFloat line[8*4] = {
15.0, 6.0, 15.0, 4.0,
15.0,24.0, 15.0,26.0,
6.0,15.0, 4.0,15.0,
24.0,15.0, 26.0,15.0,
21.5,21.5, 23.0,23.0,
8.5, 8.5, 7.0, 7.0,
21.5, 8.5, 23.0, 7.0,
8.5,21.5, 7.0,23.0,
};// A circle with eight rays around it
CGContextStrokeEllipseInRect(context, CGRectMake(10.5, 10.5, 9.0, 9.0));
for (i = 0; i < 8; i++)
{
CGContextMoveToPoint(context, line[i*4+0], line[i*4+1]);
CGContextAddLineToPoint(context, line[i*4+2], line[i*4+3]);
CGContextStrokePath(context);
}
break;
}
case BUTTON_CONTRAST:
{
// A circle with the right half filled
CGContextStrokeEllipseInRect(context, CGRectMake(4.0, 4.0, 22.0, 22.0));
CGContextAddArc(context, 15.0, 15.0, 11.0, -M_PI/2.0, M_PI/2.0, false);
CGContextFillPath(context);
break;
}
case BUTTON_SATURATION:
{
CGGradientRef gradient;
const CGFloat stripe[3][12] = {
{ 0.3,0.3,0.3,0.15, 1.0,0.0,0.0,0.70, 5, 5, 7, 25 },
{ 0.5,0.5,0.5,0.25, 0.0,1.0,0.0,0.75, 12, 5, 6, 25 },
{ 0.2,0.2,0.2,0.10, 0.0,0.0,1.0,0.65, 18, 5, 7, 25 },
};// Red/Green/Blue gradients, inside a rounded rect
for (i = 0; i < 3; i++)
{
gradient = CGGradientCreateWithColorComponents(colorSpace, stripe[i], NULL, 2);
CGContextSaveGState(context);
CGContextClipToRect(context, CGRectMake(stripe[i][8], stripe[i][9], stripe[i][10], stripe[i][11]));
CGContextDrawLinearGradient(context, gradient, CGPointMake(15, 5), CGPointMake(15, 25), 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
}CGContextMoveToPoint(context, 4, 15);
CGContextAddArcToPoint(context, 4, 4, 15, 4, 4);
CGContextAddArcToPoint(context, 26, 4, 26, 15, 4);
CGContextAddArcToPoint(context, 26, 26, 15, 26, 4);
CGContextAddArcToPoint(context, 4, 26, 4, 15, 4);
CGContextClosePath(context);
CGContextStrokePath(context);
break;
}
case BUTTON_HUE:
{
CGGradientRef gradient;
CGFloat hue[8];
const int angle = 4;

// A radial gradient, inside a circle
for (i = 0; i < 360; i+=angle)
{
float x = cosf((i+angle*0.5)*DEG2RAD)*10+15;
float y = sinf((i+angle*0.5)*DEG2RAD)*10+15;
float r = (i )/180.0; if (r > 1.0) r = 2.0-r;
float g = (i+120)/180.0; if (g > 2.0) g = g-2.0; else if (g > 1.0) g = 2.0-g;
float b = (i+240)/180.0; if (b > 3.0) b = 4.0-b; else if (b > 2.0) b = b-2.0; else b = 2.0-b;
float a = (i+ 90)/180.0; if (a > 2.0) a = a-2.0; else if (a > 1.0) a = 2.0-a;
hue[0] = hue[4] = r;
hue[1] = hue[5] = g;
hue[2] = hue[6] = b;
hue[3] = a*0.5;
hue[7] = a*0.75;

gradient = CGGradientCreateWithColorComponents(colorSpace, hue, NULL, 2);
CGContextSaveGState(context);
CGContextMoveToPoint(context, 15, 15);
CGContextAddArc(context, 15, 15, 10, i*DEG2RAD, (i+angle)*DEG2RAD, false);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, CGPointMake(x, y), CGPointMake(15, 15), 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
}

CGContextStrokeEllipseInRect(context, CGRectMake(4.0, 4.0, 22.0, 22.0));
break;
}
case BUTTON_SHARPNESS:
{
int x, y;

// A gradient checkerboard, inside a rounded rect
for (x = 5; x < 25; x+=2)
{
float b = (x – 5)/19.0*0.5+0.375;
if (b > 0.75) b = 0.75;
else if (b < 0.5) b = 0.5;

for (y = 5; y < 25; y+=2)
{
float k = ((x ^ y) & 2) ? b : 1.0-b;
CGContextSetRGBFillColor(context, k, k, k, k);
CGContextFillRect(context, CGRectMake(x, y, 2, 2));
}
}

CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextMoveToPoint(context, 4, 15);
CGContextAddArcToPoint(context, 4, 4, 15, 4, 4);
CGContextAddArcToPoint(context, 26, 4, 26, 15, 4);
CGContextAddArcToPoint(context, 26, 26, 15, 26, 4);
CGContextAddArcToPoint(context, 4, 26, 4, 15, 4);
CGContextClosePath(context);
CGContextStrokePath(context);
break;
}
}
theCGImage = CGBitmapContextCreateImage(context);
((UITabBarItem *)[tabBar.items objectAtIndex:b]).image = [UIImage imageWithCGImage:theCGImage];
CGImageRelease(theCGImage);
}

CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
}

- (void)dealloc
{
self.slider = nil;
self.tabBar = nil;
[super dealloc];
}

- (void)sliderAction:(id)sender
{
// Redraw the view with the new settings
[((EAGLView*)self.view) drawView];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
// Recenter the slider (this application does not accumulate multiple filters)
[self.slider setValue:1.0 animated:YES];
// Redraw the view with the new settings
[((EAGLView*)self.view) drawView];
}

@end

  • Debug Section Debug.c:

#include <OpenGLES/ES1/gl.h>
#include <OpenGLES/ES1/glext.h>
#include “Debug.h”// Debug utility: validate the current texture environment against MBX errata.
// Assert if we use a state combination not supported by the MBX hardware.
void validateTexEnv(void)
{
#if DEBUG
typedef struct {
GLint combine;
GLint src[3];
GLint op[3];
GLint scale;
} Channel;typedef struct {
GLint enabled;
GLint binding;
GLint mode;
Channel rgb;
Channel a;
GLfloat color[4];
} TexEnv;// MBX supports two texture units
TexEnv unit[2];
GLint active;
int i, prev = -1;glGetIntegerv(GL_ACTIVE_TEXTURE, &active);
for (i = 0; i < 2; i++)
{
glActiveTexture(GL_TEXTURE0+i);
unit[i].enabled = glIsEnabled(GL_TEXTURE_2D);
glGetIntegerv(GL_TEXTURE_BINDING_2D, &unit[i].binding);
glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &unit[i].mode);
glGetTexEnviv(GL_TEXTURE_ENV, GL_COMBINE_RGB, &unit[i].rgb.combine);
glGetTexEnviv(GL_TEXTURE_ENV, GL_SRC0_RGB, &unit[i].rgb.src[0]);
glGetTexEnviv(GL_TEXTURE_ENV, GL_SRC1_RGB, &unit[i].rgb.src[1]);
glGetTexEnviv(GL_TEXTURE_ENV, GL_SRC2_RGB, &unit[i].rgb.src[2]);
glGetTexEnviv(GL_TEXTURE_ENV, GL_OPERAND0_RGB, &unit[i].rgb.op[0]);
glGetTexEnviv(GL_TEXTURE_ENV, GL_OPERAND1_RGB, &unit[i].rgb.op[1]);
glGetTexEnviv(GL_TEXTURE_ENV, GL_OPERAND2_RGB, &unit[i].rgb.op[2]);
glGetTexEnviv(GL_TEXTURE_ENV, GL_RGB_SCALE, &unit[i].rgb.scale);
glGetTexEnviv(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, &unit[i].a.combine);
glGetTexEnviv(GL_TEXTURE_ENV, GL_SRC0_ALPHA, &unit[i].a.src[0]);
glGetTexEnviv(GL_TEXTURE_ENV, GL_SRC1_ALPHA, &unit[i].a.src[1]);
glGetTexEnviv(GL_TEXTURE_ENV, GL_SRC2_ALPHA, &unit[i].a.src[2]);
glGetTexEnviv(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, &unit[i].a.op[0]);
glGetTexEnviv(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, &unit[i].a.op[1]);
glGetTexEnviv(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, &unit[i].a.op[2]);
glGetTexEnviv(GL_TEXTURE_ENV, GL_ALPHA_SCALE, &unit[i].a.scale);
glGetTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, &unit[i].color[0]);if (unit[i].enabled == 0) continue;
if (unit[i].mode != GL_COMBINE) continue;// PREVIOUS on unit 0 means PRIMARY_COLOR.
if (i == 0)
{
int j;for (j = 0; j < 3; j++)
{
if (unit[i].rgb.src[j] == GL_PREVIOUS)
unit[i].rgb.src[j] = GL_PRIMARY_COLOR;
if (unit[i].a.src[j] == GL_PREVIOUS)
unit[i].a.src[j] = GL_PRIMARY_COLOR;
}
}// If the value of COMBINE_RGB is MODULATE, only one of the two multiplicands can use an ALPHA operand.
rt_assert(!(unit[i].rgb.combine == GL_MODULATE &&
(unit[i].rgb.op[0] == GL_SRC_ALPHA || unit[i].rgb.op[0] == GL_ONE_MINUS_SRC_ALPHA) &&
(unit[i].rgb.op[1] == GL_SRC_ALPHA || unit[i].rgb.op[1] == GL_ONE_MINUS_SRC_ALPHA)));// If the value of COMBINE_RGB is INTERPOLATE and either SRC0 or SRC1 uses an ALPHA operand, SRC2 can not be CONSTANT or PRIMARY_COLOR or use an ALPHA operand.
rt_assert(!(unit[i].rgb.combine == GL_INTERPOLATE &&
(unit[i].rgb.op[0] == GL_SRC_ALPHA || unit[i].rgb.op[0] == GL_ONE_MINUS_SRC_ALPHA ||
unit[i].rgb.op[1] == GL_SRC_ALPHA || unit[i].rgb.op[1] == GL_ONE_MINUS_SRC_ALPHA) &&
(unit[i].rgb.op[2] == GL_SRC_ALPHA || unit[i].rgb.op[2] == GL_ONE_MINUS_SRC_ALPHA ||
unit[i].rgb.src[2] == GL_CONSTANT || unit[i].rgb.src[2] == GL_PRIMARY_COLOR)));// If the value of COMBINE_RGB is INTERPOLATE and SRC0 and SRC1 are CONSTANT or PRIMARY COLOR, SRC2 can not be CONSTANT or PRIMARY_COLOR or use an ALPHA operand.
rt_assert(!(unit[i].rgb.combine == GL_INTERPOLATE &&
((unit[i].rgb.src[0] == GL_CONSTANT && unit[i].rgb.src[1] == GL_CONSTANT) ||
(unit[i].rgb.src[0] == GL_PRIMARY_COLOR && unit[i].rgb.src[1] == GL_PRIMARY_COLOR)) &&
(unit[i].rgb.op[2] == GL_SRC_ALPHA || unit[i].rgb.op[2] == GL_ONE_MINUS_SRC_ALPHA ||
unit[i].rgb.src[2] == GL_CONSTANT || unit[i].rgb.src[2] == GL_PRIMARY_COLOR)));// If the value of COMBINE_RGB is DOT3_RGB or DOT3_RGBA, only one of the sources can be PRIMARY_COLOR or use an ALPHA operand.
rt_assert(!((unit[i].rgb.combine == GL_DOT3_RGB || unit[i].rgb.combine == GL_DOT3_RGBA) &&
(unit[i].rgb.src[0] == GL_PRIMARY_COLOR || unit[i].rgb.op[0] == GL_SRC_ALPHA || unit[i].rgb.op[0] == GL_ONE_MINUS_SRC_ALPHA) &&
(unit[i].rgb.src[1] == GL_PRIMARY_COLOR || unit[i].rgb.op[1] == GL_SRC_ALPHA || unit[i].rgb.op[1] == GL_ONE_MINUS_SRC_ALPHA)));// If the value of COMBINE_RGB is SUBTRACT, SCALE_RGB must be 1.0.
rt_assert(!(unit[i].rgb.combine == GL_SUBTRACT && unit[i].rgb.scale != 1));if (unit[i].rgb.combine != GL_DOT3_RGBA)
{
// If the value of COMBINE_ALPHA is MODULATE or INTERPOLATE, only one of the two multiplicands can be CONSTANT.
rt_assert(!(unit[i].a.combine == GL_MODULATE && unit[i].a.src[0] == GL_CONSTANT && unit[i].a.src[1] == GL_CONSTANT));
rt_assert(!(unit[i].a.combine == GL_INTERPOLATE && (unit[i].a.src[0] == GL_CONSTANT || unit[i].a.src[1] == GL_CONSTANT) && unit[i].a.src[2] == GL_CONSTANT));// If the value of COMBINE_ALPHA is SUBTRACT, SCALE_ALPHA must be 1.0.
rt_assert(!(unit[i].a.combine == GL_SUBTRACT && unit[i].a.scale != 1));
}// The value of TEXTURE_ENV_COLOR must be the same for all texture units that CONSTANT is used on.
if (unit[i].rgb.src[0] == GL_CONSTANT ||
(unit[i].rgb.src[1] == GL_CONSTANT && unit[i].rgb.combine != GL_REPLACE) ||
(unit[i].rgb.src[2] == GL_CONSTANT && unit[i].rgb.combine == GL_INTERPOLATE) ||
(unit[i].rgb.combine != GL_DOT3_RGBA &&
(unit[i].a.src[0] == GL_CONSTANT ||
(unit[i].a.src[1] == GL_CONSTANT && unit[i].a.combine != GL_REPLACE) ||
(unit[i].a.src[2] == GL_CONSTANT && unit[i].a.combine == GL_INTERPOLATE))))
{
if (prev >= 0)
rt_assert(!(unit[prev].color[0] != unit[i].color[0] ||
unit[prev].color[1] != unit[i].color[1] ||
unit[prev].color[2] != unit[i].color[2] ||
unit[prev].color[3] != unit[i].color[3]));
prev = i;
}
}glActiveTexture(active);
glCheckError();
#endif /* DEBUG */
}

 If you need Whole Project Please Comment:

For More Source Codes Visit Image Cropping Application Source Code for iOS

Farheen Bibi

You can find Farheen on , and .

17 Comments to Image filter Processing Implementation in Xcode for iPhone Using GLImageProcessing iOS Developer Library

Leave a Reply