//
// Created by Douglas Brenner on 9/16/12.
// Copyright (c) 2012 Douglas Brenner. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *myImage;
@end
@implementation ViewController
-(IBAction)loadimage:(id)sender {
// Name is already loaded
//NSLog(@"%@", fileName.text);
UIImage *raw = [UIImage imageNamed:fileName.text];
UIImage *image = [self imageWithGrayScaleImage:raw];
// CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
[_myImage setImage:image];
_myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:_myImage];
}
-(void)serialloadimage {
fileName.text = @"EDGE.JPG";
UIImage *raw = [UIImage imageNamed:fileName.text];
UIImage *image = [self imageWithGrayScaleImage:raw];
// CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
[_myImage setImage:image];
_myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:_myImage];
}
-(IBAction)close:(id)sender {
[fileName resignFirstResponder];
}
- (UIImage*)imageWithGrayScaleImage:(UIImage*)inputImg
{
//Creating image rectangle
//CGRect imageRect = CGRectMake(0, 0, inputImg.size.width / (CGFloat)[inputImg scale], inputImg.size.height / (CGFloat)[inputImg scale]);
CGRect imageRect = CGRectMake(0, 0, inputImg.size.width, inputImg.size.height);
//Allocating memory for pixels
int width = imageRect.size.width;
int height = imageRect.size.height;
uint32_t *pixels = (uint32_t*)malloc(width * height * sizeof(uint32_t));
//Clearing the memory to preserve any transparency.(Alpha)
memset(pixels, 0, width * height * sizeof(uint32_t));
//Creating a context with RGBA pixels
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
//Drawing the bitmap to the context which will fill the pixels memory
CGContextDrawImage(context, imageRect, [inputImg CGImage]);
//Indices as ARGB or RGBA
const int RED = 1;
const int GREEN = 2;
const int BLUE = 3;
uint32_t grayPixel;
uint8_t* rgbaPixel;
uint32_t *edge;
int *filterout;
int filter[10];
edge = (uint32_t*)calloc(height, sizeof(uint32_t));;
filterout = (int*)calloc(height, sizeof(int));
int i;
for(i = 0; i < 5; i++)
filter[i] = -1;
for(i = 5; i < 10; i++)
filter[i] = 1;
FILE *edgedata;
edgedata = fopen("/Users/doug/edge.txt","w");
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
rgbaPixel = (uint8_t*)&pixels[y * width + x];
//Calculating the grayScale value
grayPixel = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];
//Setting the pixel to gray
rgbaPixel[RED] = grayPixel;
rgbaPixel[GREEN] = grayPixel;
rgbaPixel[BLUE] = grayPixel;
edge[x] += grayPixel;
}
}
for (int x = 0; x < width - 10; x++)
for(i = 0; i < 10; i++){
filterout[x+5] += filter[i]*(int)edge[x + i];
//printf("%d %d %\n", filterout[x+5], filter[i], edge[x])
}
for (int x = 0; x < width; x++)
fprintf(edgedata,"%d\n", filterout[x]);
fclose(edgedata);
//Creating new CGImage from the context with modified pixels
CGImageRef newCGImage = CGBitmapContextCreateImage(context);
//Releasing resources to free up memory
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(pixels);
//Creating UIImage for return value
//UIImage* newUIImage = [UIImage imageWithCGImage:newCGImage scale:(CGFloat)[inputImg scale] orientation:UIImageOrientationUp];
UIImage* newUIImage = [UIImage imageWithCGImage:newCGImage];
//Releasing the CGImage
CGImageRelease(newCGImage);
return newUIImage;
}
- (void)viewDidLoad
{
[self serialloadimage];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
@end