#import "ImageUtil.h"
#include <sys/time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
// 1返回一个使用RGBA通道的位图上下文
static CGContextRef CreateRGBABitmapContext (CGImageRef inImage)
{
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void *bitmapData; //内存空间的指针,该内存空间的大小等于图像使用RGB通道所占用的字节数。
int bitmapByteCount;
int bitmapBytesPerRow;
size_t pixelsWide = CGImageGetWidth(inImage); //获取横向的像素点的个数
size_t pixelsHigh = CGImageGetHeight(inImage);
bitmapBytesPerRow = (pixelsWide * 4); //每一行的像素点占用的字节数,每个像素点的ARGB四个通道各占8个bit(0-255)的空间
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); //计算整张图占用的字节数
colorSpace = CGColorSpaceCreateDevice
//分配足够容纳图片字节数的内存空间
bitmapData = malloc( bitmapByteCount );
context = CGBitmapContextCreate (bitmapData,
pixelsWide,
pixelsHigh,
8,
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipli
CGColorSpaceRelease( colorSpace );
return context;
}
// 2返回一个指针,该指针指向一个数组,数组中的每四个元素都是图像上的一个像素点的RGBA的数值(0-255),用无符号的char是因为它正好的取值范围就是0-255
static unsigned char *RequestImagePixelData(UIImage *inImage)
{
CGImageRef img = [inImage CGImage];
CGSize size = [inImage size];
CGContextRef cgctx = CreateRGBABitmapContext(img);
CGRect rect = {{0,0},{size.width, size.height}};
CGContextDrawImage(cgctx, rect, img);
unsigned char *data = CGBitmapContextGetData (cgctx);
CGContextRelease(cgctx);
return data;
}
//3修改RGB的值
static void changeRGBA(int *red,int *green,int *blue,int *alpha, const float* f){
}
#pragma mark -
@implementation ImageUtil
#pragma mark -
+ (UIImage*)processImage:(UIImage*)inImage withColorMatrix:(const float*) f
{
unsigned char *imgPixel = RequestImagePixelData(inImage);
CGImageRef inImageRef = [inImage CGImage];
GLuint w = CGImageGetWidth(inImageRef);
GLuint h = CGImageGetHeight(inImageRef);
int wOff = 0;
int pixOff = 0;
for(GLuint y = 0;y< h;y++)
{
pixOff = wOff;
for (GLuint x = 0; x<w; x++)
{
int red = (unsigned char)imgPixel[pixOff];
int green = (unsigned char)imgPixel[pixOff+1];
int blue = (unsigned char)imgPixel[pixOff+2];
imgPixel[pixOff] = red;
imgPixel[pixOff+1] = green;
imgPixel[pixOff+2] = blue;
pixOff += 4;
}
wOff += w * 4;
}
NSInteger dataLength = w*h* 4;
CGDataProviderRef provider = CGDataProviderCreateWith
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * w;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDevice
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefaul
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefaul
//创建要输出的图像
CGImageRef imageRef = CGImageCreate(w, h,
UIImage *my_Image = [UIImage imageWithCGImage:imageRef];
CFRelease(imageRef);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
return my_Image;
}
//-----------颜色处理-----
#ifndef ImageProcessing_ColorMatrix_h
#define ImageProcessing_ColorMatrix_h
//ColorMatrix From Android
//lomo
const float colormatrix_lomo[] = {
//黑白
const float colormatrix_heibai[] = {
//旧化
const float colormatrix_huajiu[] = {
//哥特
const float colormatrix_gete[] = {
//锐色
const float colormatrix_ruise[] = {
//淡雅
const float colormatrix_danya[] = {
//酒红
const float colormatrix_jiuhong[] = {
//清宁
const float colormatrix_qingning[] = {
//浪漫
const float colormatrix_langman[] = {
//光晕
const float colormatrix_guangyun[] = {
//蓝调
const float colormatrix_landiao[] = {
};
//梦幻
const float colormatrix_menghuan[] = {
};
//夜色
const float colormatrix_yese[] = {
};
#endif
#ifndef ImageProcessing_ColorMatrix_h
#define ImageProcessing_ColorMatrix_h
//ColorMatrix From Android
//lomo
const float colormatrix_lomo[] = {
//黑白
const float colormatrix_heibai[] = {
//旧化
const float colormatrix_huajiu[] = {
//哥特
const float colormatrix_gete[] = {
//锐色
const float colormatrix_ruise[] = {
//淡雅
const float colormatrix_danya[] = {
//酒红
const float colormatrix_jiuhong[] = {
//清宁
const float colormatrix_qingning[] = {
//浪漫
const float colormatrix_langman[] = {
//光晕
const float colormatrix_guangyun[] = {
//蓝调
const float colormatrix_landiao[] = {
};
//梦幻
const float colormatrix_menghuan[] = {
};
//夜色
const float colormatrix_yese[] = {
};
#endif
#ifndef ImageProcessing_ColorMatrix_h
#define ImageProcessing_ColorMatrix_h
//ColorMatrix From Android
//lomo
const float colormatrix_lomo[] = {
//黑白
const float colormatrix_heibai[] = {
//旧化
const float colormatrix_huajiu[] = {
//哥特
const float colormatrix_gete[] = {
//锐色
const float colormatrix_ruise[] = {
//淡雅
const float colormatrix_danya[] = {
//酒红
const float colormatrix_jiuhong[] = {
//清宁
const float colormatrix_qingning[] = {
//浪漫
const float colormatrix_langman[] = {
//光晕
const float colormatrix_guangyun[] = {
//蓝调
const float colormatrix_landiao[] = {
};
//梦幻
const float colormatrix_menghuan[] = {
};
//夜色
const float colormatrix_yese[] = {
};
#endif
----调用-----
outImage = [ImageUtil processImage:_tempImage withColorMatrix:colormatrix_landiao];