Help optimize Brightness algorithm for THIS method (Not using OpenGL)
Hi guys,
I'm really new to image processing and I've spent last 2 days surfing on the web searching for a sample code or tutorial that can help me to create a method to modify the Brithness of a UIImage by using a UISlider.
I'm surprised how it's difficult to find some tutorial about that. I've searched here at the forum,google and everywhere but I didn't find a complete tutorial for manage brightness on uiimage.
I know that apple provided the "Image Processing" sample that can do it with OpenGL but it's really difficult to understand for me and I simply can't find how to implement all that code in my app and use it for a UIImage.
So I've find on the web this code that "Should" allow you to modify brightess of an UIImage without use OpenGL.
I've implemented it in my project but it doesn't work correctly, it leave on the image a lot of stripes and the results is horrible... yes the brithness features work but the images pixel are damnaged by the algorith.
Anyone could test this code and help me to optimize it?
- (UIImage *) brightness{
int level= put here an int value that go from -255 to 255;
CGImageRef inImage = inputImage; //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;
}
Hi Dany ,
Thank you very much to share your code...
I was tryng to test it but compiler gave me an error and I've seen that the RED portion of code above has a missing part of code...
Am I correct??
Thank you very much to share your code...
I was tryng to test it but compiler gave me an error and I've seen that the RED portion of code above has a missing part of code...
Am I correct??
Yes I did it as you can see below I have modified the inputimage with an UIImage taken from a UIIMageView called "foto" and I'm also passing the int value as a parameter of method by a UISlider.
Code:
- (UIImage *) brightness:(int)value{
int level=value;//; put here an int value that go from -255 to 255;
CGImageRef inImage = foto.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];
[foto setImage:[UIImage imageWithCGImage:imageRef]];
CGContextRelease(ctx);
CGImageRelease(imageRef);
CFRelease(m_DataRef);
//return rawImage;
}
But there is an error with the sintax. If you see the RED part of code you'll see that it ends with two "))" but the second ")" doesn't have a start... So I suppose that red parts is a part of an expression wrongly deleted with the copy and past process:
Above is:
Both the pieces of code above use the same logic to increase brightness... They only differ on how they create the new modified UIImage... So I don't think they will be different on how the image looks. I believe the problem could lie on your input values, aren't they too high, or too low ?
Try it a couple times with hardcoded values as 10, 20 , 50 etc to see if the resulting image looks better, then try modifying the UISlider values to match those. Remember that once you reach pixel values >255 or <0, you lose data permanently and cannot go back to your original image appearance.
Yes I did it as you can see below I have modified the inputimage with an UIImage taken from a UIIMageView called "foto" and I'm also passing the int value as a parameter of method by a UISlider.
Code:
- (UIImage *) brightness:(int)value{
int level=value;//; put here an int value that go from -255 to 255;
CGImageRef inImage = foto.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];
[foto setImage:[UIImage imageWithCGImage:imageRef]];
CGContextRelease(ctx);
CGImageRelease(imageRef);
CFRelease(m_DataRef);
//return rawImage;
}
But there is an error with the sintax. If you see the RED part of code you'll see that it ends with two "))" but the second ")" doesn't have a start... So I suppose that red parts is a part of an expression wrongly deleted with the copy and past process:
Above is:
Thank you!
Any code will be appreciated for testing
nobre84:
I've tryed to modify the scale of uislader with +/-255 then descreased the range to +/-100, then +/-50 and finally +/-25....
I don't know why but the image don't modify the brightness correctly...
Now I've tryed the code on a RGB normal coloured image...
But I need to made possible the BRIGHTNESS regulation on a GRAYSCALE image so I was thinking about a very dirty way...
what About to put two UIImageView, One totally white and the other totally pure black and then with positive value of the slider set the transparency of the white imageview and with negative value set the black imageview....
I've tryed on photoshop with the same image 2 test:
1) put a white level over the photo and set the transparency of the lavel at 20%
2) modify the Brightness of the photo directly with ctrl+U at a level of 20
that'exactly the same result... also with other percentage.
What do you think about it?
Once done to save the result for me it will be enough a screenshot taken by code programmatically....
Do you think I can obtain the same result??? (remember that I'm using grayscale image)....
BTW:
An amazing solution will be use the code in GLImageProcessig sample code from apple... but it's reallt really to complicated for me to understand...
And also I don't have Idea on how use that code with a UIImage that is in a ImageView ...
is EXACTLY the same thing no? (just one in a loop and the other isnot).
Im working on this too at themoment.. will put in my filters here once completed.
he said that his code not work, so i posted my working code that i use on one of my app
i hurry, so i take the code, deleted few lines (that i use for other things) and posted (infact i not deleted the line that get error to nobre)
really i not red his code
ps: scuse my english
However when i have 10 minutes i will read all and try to resolve it.
But both of the code give the same result.. adjusting the brightness not really working on a color image. Because you are just saturating each colors in the the pixel till reach other 0 or 255.
Im still studying this.. if anybody else got it first, pls post... thanks.