Image Processing in iOS (I)-Basic Filter

Source: Internet
Author: User

I recently made some sorting and pulled up the code. I found it was more than two months ago.

Here we will discuss Image Processing Based on the RGBA model, which will transform each pixel.

The code exists in the form of a category of UIImage:


[Cpp]
Typedef struct _ singleRGBA
{
Unsigned char red;
Unsigned char green;
Unsigned char blue;
Unsigned char alpha;
} RGBA;
 
@ Interface UIImage (ImageFilter)


First, we need to obtain the bitmap information of the target image, transform each pixel, and then generate the image.

[Cpp]
-(UIImage *) applyFilter :( FilterFunction) filter context :( void *) context
{
CGImageRef inImage = self. CGImage;
CFDataRef m_DataRef = CGDataProviderCopyData (CGImageGetDataProvider (inImage ));
UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr (m_DataRef );

Int length = CFDataGetLength (m_DataRef );

For (int I = 0; I <length; I + = 4 ){
Filter (m_PixelBuf, I, context );
}

CGContextRef ctx = CGBitmapContextCreate (m_PixelBuf,
CGImageGetWidth (inImage ),
CGImageGetHeight (inImage ),
CGImageGetBitsPerComponent (inImage ),
CGImageGetBytesPerRow (inImage ),
CGImageGetColorSpace (inImage ),
CGImageGetBitmapInfo (inImage)
);

CGImageRef imageRef = CGBitmapContextCreateImage (ctx );
CGContextRelease (ctx );
UIImage * finalImage = [UIImage imageWithCGImage: imageRef];
CGImageRelease (imageRef );
CFRelease (m_DataRef );

Return finalImage;
}

The FilterFunction declaration is as follows:

[Cpp]
Typedef void (* FilterFunction) (UInt8 * pixelBuf, UInt32 offset, void * context );

On this basis, we can separate each transformation operation, such as brightness adjustment, contrast, tone, transparency, and so on:

[Cpp]
Void filterOpacity (UInt8 * pixelBuf, UInt32 offset, void * context)
{
Double val = * (double *) context );

Int a = offset + 3;

Int alpha = pixelBuf [a];

PixelBuf [a] = SAFECOLOR (alpha * val );
}
 
Void filterBrightness (UInt8 * pixelBuf, UInt32 offset, void * context)
{
Double t = * (double *) context );

Int r = offset;
Int g = offset + 1;
Int B = offset + 2;

Int red = pixelBuf [r];
Int green = pixelBuf [g];
Int blue = pixelBuf [B];

PixelBuf [r] = SAFECOLOR (red * t );
PixelBuf [g] = SAFECOLOR (green * t );
PixelBuf [B] = SAFECOLOR (blue * t );
}
 
Void filterSaturation (UInt8 * pixelBuf, UInt32 offset, void * context)
{
Double t = * (double *) context); // t (-[0, 2]

Int r = offset;
Int g = offset + 1;
Int B = offset + 2;

Int red = pixelBuf [r];
Int green = pixelBuf [g];
Int blue = pixelBuf [B];

Red = red * (0.3086 * (1-t) + t) + green * (0.6094 * (1-t) + blue * (0.0820 * (1-t ));
Green = red * (0.3086 * (1-t) + green * (0.6094 * (1-t) + t) + blue * (0.0820 * (1-t ));
Blue = red * (0.3086 * (1-t) + green * (0.6094 * (1-t) + blue * (0.0820 * (1-t) + t );

PixelBuf [r] = SAFECOLOR (red );
PixelBuf [g] = SAFECOLOR (green );
PixelBuf [B] = SAFECOLOR (blue );
}
 
Void filterContrast (UInt8 * pixelBuf, UInt32 offset, void * context)
{
Double t = * (double *) context); // t (-[0, 10]

Int r = offset;
Int g = offset + 1;
Int B = offset + 2;

Int red = pixelBuf [r];
Int green = pixelBuf [g];
Int blue = pixelBuf [B];

Red = red * t + 128 * (1-t );
Green = green * t + 128 * (1-t );
Blue = blue * t + 128 * (1-t );

PixelBuf [r] = SAFECOLOR (red );
PixelBuf [g] = SAFECOLOR (green );
PixelBuf [B] = SAFECOLOR (blue );
}
 
Void filterPosterize (UInt8 * pixelBuf, UInt32 offset, void * context)
{
Double levels = * (double *) context );
If (levels = 0) levels = 1; // avoid divide by zero
Int step = 255/levels;

Int r = offset;
Int g = offset + 1;
Int B = offset + 2;

Int red = pixelBuf [r];
Int green = pixelBuf [g];
Int blue = pixelBuf [B];

PixelBuf [r] = SAFECOLOR (red/step) * step );
PixelBuf [g] = SAFECOLOR (green/step) * step );
PixelBuf [B] = SAFECOLOR (blue/step) * step );
}
 
Void filterDesaturate (UInt8 * pixelBuf, UInt32 offset, void * context)
{
Int r = offset;
Int g = offset + 1;
Int B = offset + 2;

Int red = pixelBuf [r];
Int green = pixelBuf [g];
Int blue = pixelBuf [B];

Red = red * 0.3086 + green * 0.6094 + blue * 0.0820;
Green = red * 0.3086 + green * 0.6094 + blue * 0.0820;
Blue = red * 0.3086 + green * 0.6094 + blue * 0.0820;

PixelBuf [r] = SAFECOLOR (red );
PixelBuf [g] = SAFECOLOR (green );
PixelBuf [B] = SAFECOLOR (blue );
}
 
Void filterInvert (UInt8 * pixelBuf, UInt32 offset, void * context)
{
Int r = offset;
Int g = offset + 1;
Int B = offset + 2;

Int red = pixelBuf [r];
Int green = pixelBuf [g];
Int blue = pixelBuf [B];

PixelBuf [r] = SAFECOLOR (255-red );
PixelBuf [g] = SAFECOLOR (255-green );
PixelBuf [B] = SAFECOLOR (255-blue );
}
 
Void filterTint (UInt8 * pixelBuf, UInt32 offset, void * context)
{
RGBA * rgbaArray = (RGBA *) context;
RGBA maxRGBA = rgbaArray [0];
RGBA minRGBA = rgbaArray [1];

Int r = offset;
Int g = offset + 1;
Int B = offset + 2;

Int red = pixelBuf [r];
Int green = pixelBuf [g];
Int blue = pixelBuf [B];

PixelBuf [r] = SAFECOLOR (red-minRGBA. red) * (255.0/(maxRGBA. red-minRGBA. red )));
PixelBuf [g] = SAFECOLOR (green-minRGBA. green) * (255.0/(maxRGBA. green-minRGBA. green )));
PixelBuf [B] = SAFECOLOR (blue-minRGBA. blue) * (255.0/(maxRGBA. blue-minRGBA. blue )));
}

The SAFECOLOR macro is as follows:

[Cpp]
# Define SAFECOLOR (color) MIN (255, MAX (0, color ))

Finally, I took a handsome Andy photo and tried to avoid infringement of the copyright.
The source image is as follows:


You can obtain four processed images by using the following four transformations:


[Cpp]
Return [originImage changeOpacityByFactor: 0.5];

[Cpp]
Return [originImage changeBrightnessByFactor: 1.2];

[Cpp]
Return [originImage changeSaturationByFactor: 2.0];

[Cpp]
Return [originImage tintWithMaxRGBA :( RGBA) {190,190,230} minRGBA :( RGBA) {50, 35, 10}];

 

 

 


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.