process image brightness using uislider in iPhone - iphone

I have implemented changing the brightness of an image using uislider in iPhone without using openGlImageProcessing. I want to change the brightness of particular area of an image only not the whole image, only particular area of an image should be get brightnessed for example the central area of an image should get brightnessed or particular color from an entire image should get brightnessed. How can we change the brightness of particular portion of an image. Please help me to solve the problem, code is
CGImageRef imageRef = imageView.image.CGImage;
CFDataRef ref = CGDataProviderCopyData(CGImageGetDataProvider(imageRef));
UInt8 * buf = (UInt8 *) CFDataGetBytePtr(ref);
int length = CFDataGetLength(ref);
NSLog(#"%i",val);
float value = val/100;
for(int i=0; i<length; i+=3)
{
Byte tempR = buf[i + 1];
Byte tempG = buf[i + 2];
Byte tempB = buf[i + 3];
int outputRed = value + tempR;
int outputGreen = value + tempG;
int outputBlue = value + tempB;
if (outputRed>255) outputRed=255;
if (outputGreen>255) outputGreen=255;
if (outputBlue>255) outputBlue=255;
if (outputRed<0) outputRed=0;
if (outputGreen<0) outputGreen=0;
if (outputBlue<0) outputBlue=0;
buf[i + 1] = outputRed;
buf[i + 2] = outputGreen;
buf[i + 3] = outputBlue;
}
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
size_t bytesPerRow = CGImageGetBytesPerRow(imageRef);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
CGContextRef ctx = CGBitmapContextCreate(buf, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGImageGetAlphaInfo(imageRef));
CGImageRef img = CGBitmapContextCreateImage(ctx);
if (value == 0) {
imageView.image = image;
}
else
{
[imageView setImage:[UIImage imageWithCGImage:img]];
}
CFRelease(ref);
CGContextRelease(ctx);
CGImageRelease(img);

After lot of searching i got the answer as
//origional image
image = [UIImage imageNamed:#"images2.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
CGSize size = [image size];
[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];
//image to be brightnessed
CGRect rect = CGRectMake([image size].width / 6, [image size].height / 6 ,
([image size].width / 2), ([image size].height / 2));
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake([image size].width / 6, [image size].height / 6, ([image size].width / 2), ([image size].height / 2))];
[self.view addSubview:imageView];
[imageView release];

Related

Crash when process pixel after crop an image

After using this function to crop image:
CGImageRef imageRef = CGImageCreateWithImageInRect([self.imageInput CGImage], rect);
UIImage *result = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
I bring "result" to use for process with pixel like that:
......
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = 0;
for (NSInteger i = 0 ; i < width * height; i++) {
int R = rawData[byteIndex];
int G = rawData[byteIndex + 1];
int B = rawData[byteIndex + 2];
int test = [self getValueof:0.3* R + 0.59* G + 0.11 * B inRange:0 to:255];
rawData[byteIndex] = (char)(test);
rawData[byteIndex + 1] = (char)(test);
rawData[byteIndex + 2] = (char)(test);
byteIndex += 4;
}
ctx = CGBitmapContextCreate(rawData,
CGImageGetWidth( imageRef ),
CGImageGetHeight( imageRef ),
8,
CGImageGetBytesPerRow( imageRef ),
CGImageGetColorSpace( imageRef ),
kCGImageAlphaPremultipliedLast );
imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
CGImageRelease(imageRef);
free(rawData);
I will have crash? Anyone know about it, please help me.
update crash report: : copy_read_only: vm_copy failed: status 1.
after a long time find solution for this crash. I got an experience:
- When process bitmap on iOS: if we don't notice orientation of image, maybe we got some strange crash.
With my crash above: I have fixed it very simple: change
UIImage *result = [UIImage imageWithCGImage:imageRef];
by
UIImage *result = [UIImage imageWithCGImage:imageRef scale:self.imageInput.scale orientation:self.imageInput.imageOrientation];

How to apply corner radius to a UIView without issues?

Currently I'm applying a corner radius and shadow to multiple UIView's in a scrollview. I've noticed that adding a corner radius and shadow makes the scrollview lag like crazy whenever I scroll. How can I apply these affects without having my performance suffer?
Try by setting also the shadowPath of the layer:
view.layer.cornerRadius=6.0f;
view.layer.borderWidth=2.0f;
view.layer.borderColor=[UIColor grayColor].CGColor;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.3f;
view.layer.shadowOffset = CGSizeMake(0, 0.0f);
view.layer.masksToBounds = NO;
UIBezierPath *path = [UIBezierPath bezierPathWithRect:view.bounds];
view.layer.shadowPath = path.CGPath;
+(UIImage *)makeRoundCornerImage : (UIImage*) img : (int) cornerWidth : (int) cornerHeight
{
UIImage * newImage = nil;
if( nil != img)
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int w = img.size.width;
int h = img.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextBeginPath(context);
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
[img release];
newImage = [[UIImage imageWithCGImage:imageMasked] retain];
CGImageRelease(imageMasked);
[pool release];
}
return newImage;
}
// With the help of This method we can create the round corner of any PNG file
UIImage *imageFromFile = [UIImage imageNamed:#"FilterFields.png"];
imageFromFile = [ImageManipulator
makeRoundCornerImage:imageFromFile : 10 : 10];
[SmallTable setImage:imageFromFile];

Got gray video using kCVPixelFormatType_420YpCbCr8BiPlanarFullRange [duplicate]

After using this function to crop image:
CGImageRef imageRef = CGImageCreateWithImageInRect([self.imageInput CGImage], rect);
UIImage *result = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
I bring "result" to use for process with pixel like that:
......
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = 0;
for (NSInteger i = 0 ; i < width * height; i++) {
int R = rawData[byteIndex];
int G = rawData[byteIndex + 1];
int B = rawData[byteIndex + 2];
int test = [self getValueof:0.3* R + 0.59* G + 0.11 * B inRange:0 to:255];
rawData[byteIndex] = (char)(test);
rawData[byteIndex + 1] = (char)(test);
rawData[byteIndex + 2] = (char)(test);
byteIndex += 4;
}
ctx = CGBitmapContextCreate(rawData,
CGImageGetWidth( imageRef ),
CGImageGetHeight( imageRef ),
8,
CGImageGetBytesPerRow( imageRef ),
CGImageGetColorSpace( imageRef ),
kCGImageAlphaPremultipliedLast );
imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
CGImageRelease(imageRef);
free(rawData);
I will have crash? Anyone know about it, please help me.
update crash report: : copy_read_only: vm_copy failed: status 1.
after a long time find solution for this crash. I got an experience:
- When process bitmap on iOS: if we don't notice orientation of image, maybe we got some strange crash.
With my crash above: I have fixed it very simple: change
UIImage *result = [UIImage imageWithCGImage:imageRef];
by
UIImage *result = [UIImage imageWithCGImage:imageRef scale:self.imageInput.scale orientation:self.imageInput.imageOrientation];

How to set brightness to uiimage captured from uiimagepickercontroller

I'm using UIImagePickerController class. I have a button, when i click that button, i want image bright. I have searched online, but the code i found upon search is not working.
Here is the code i have found and used :
- (UIImage *) brightness:(UIImage *)image
{
int level= 200;//put here an int value that go from -255 to 255;
CGImageRef inImage = image.CGImage; //Input image cgi
CGContextRef ctx;
CFDataRef m_DataRef;
m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
int length = CFDataGetLength(m_DataRef);
CGImageGetBitsPerComponent(inImage), CGImageGetBitsPerPixel(inImage),CGImageGetBytesPerRow(inImage);
for (int index = 0; index < length; index += 4)
{
Byte tempR = m_PixelBuf[index + 1];
Byte tempG = m_PixelBuf[index + 2];
Byte tempB = m_PixelBuf[index + 3];
int outputRed = level + tempR;
int outputGreen = level + tempG;
int outputBlue = level + tempB;
if (outputRed>255) outputRed=255;
if (outputGreen>255) outputGreen=255;
if (outputBlue>255) outputBlue=255;
if (outputRed<0) outputRed=0;
if (outputGreen<0) outputGreen=0;
if (outputBlue<0) outputBlue=0;
m_PixelBuf[index + 1] = outputRed;
m_PixelBuf[index + 2] = outputGreen;
m_PixelBuf[index + 3] = outputBlue;
}
ctx = CGBitmapContextCreate(m_PixelBuf,
CGImageGetWidth( inImage ),
CGImageGetHeight( inImage ),
8,
CGImageGetBytesPerRow( inImage ),
CGImageGetColorSpace( inImage ),
kCGImageAlphaPremultipliedFirst );
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
CGImageRelease(imageRef);
CFRelease(m_DataRef);
return rawImage;
}
Image is still at original image's brightness, it is not changing.
In general the idea looks good, probably there is an error with updating pixel data.
But to change brightness I suggest to use CoreGraphics instead of soft processing:
CGFloat brightness = 0.5;
UIGraphicsBeginImageContext(image.size);
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
CGContextRef context = UIGraphicsGetCurrentContext();
// Original image
[image drawInRect:imageRect];
// Brightness overlay
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:brightness].CGColor);
CGContextAddRect(context, imageRect);
CGContextFillPath(context);
UIImage* resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
You can change brightness overlay color to get proper results.

How can i change uiimage color in iphone sdk xcode

I am using different images and i want to include change color option. But i cant. Any body help me?
If you want to do image tinting, see UIImage+Tint.m in kballard/MGImageUtilities. If you want wholesale color replacement (e.g. treat an image as a silhouette and change the entire color to one flat color), see UIImage+Tint.m in mattgemmell/MGImageUtilities.
Its latest and most simple way to do it.
theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[theImageView setTintColor:[UIColor redColor]];
One way to accomplish this is to desaturate your image, and add a tint on top of that image with the color you desire.
Desaturate
-(UIImage *) getImageWithUnsaturatedPixelsOfImage:(UIImage *)image {
const int RED = 1, GREEN = 2, BLUE = 3;
CGRect imageRect = CGRectMake(0, 0, image.size.width*2, image.size.height*2);
int width = imageRect.size.width, height = imageRect.size.height;
uint32_t * pixels = (uint32_t *) malloc(width*height*sizeof(uint32_t));
memset(pixels, 0, width * height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [image CGImage]);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
uint8_t * rgbaPixel = (uint8_t *) &pixels[y*width+x];
uint32_t gray = (0.3*rgbaPixel[RED]+0.59*rgbaPixel[GREEN]+0.11*rgbaPixel[BLUE]);
rgbaPixel[RED] = gray;
rgbaPixel[GREEN] = gray;
rgbaPixel[BLUE] = gray;
}
}
CGImageRef newImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(pixels);
UIImage * resultUIImage = [UIImage imageWithCGImage:newImage scale:2 orientation:0];
CGImageRelease(newImage);
return resultUIImage;
}
Overlay With Color
-(UIImage *) getImageWithTintedColor:(UIImage *)image withTint:(UIColor *)color withIntensity:(float)alpha {
CGSize size = image.size;
UIGraphicsBeginImageContextWithOptions(size, FALSE, 2);
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:1.0];
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextSetBlendMode(context, kCGBlendModeOverlay);
CGContextSetAlpha(context, alpha);
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(CGPointZero.x, CGPointZero.y, image.size.width, image.size.height));
UIImage * tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
How-To
//For a UIImageView
yourImageView.image = [self getImageWithUnsaturatedPixelsOfImage:yourImageView.image];
yourImageView.image = [atom getImageWithTintedColor:yourImageView.image withTint:[UIColor redColor] withIntensity:0.7];
//For a UIImage
yourImage = [self getImageWithUnsaturatedPixelsOfImage:yourImage];
yourImage = [atom getImageWithTintedColor:yourImageView.image withTint:[UIColor redColor] withIntensity:0.7];
You can change the color of the tint to whatever you desire.