`
jsntghf
  • 浏览: 2488887 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

简单的图像处理

    博客分类:
  • iOS
阅读更多

ImageUtil.h

 

#import <Foundation/Foundation.h>
#include <OpenGLES/ES1/gl.h>
#include <OpenGLES/ES1/glext.h>

@interface ImageUtil : NSObject 

+ (CGSize) fitSize: (CGSize)thisSize inSize: (CGSize) aSize;
+ (UIImage *) image: (UIImage *) image fitInSize: (CGSize) viewsize;

+ (UIImage *)blackWhite:(UIImage *)inImage;
+ (UIImage *)cartoon:(UIImage *)inImage;
+ (UIImage *)memory:(UIImage *)inImage;
+ (UIImage *)bopo:(UIImage *)inImage;
+ (UIImage *)scanLine:(UIImage *)inImage;

@end

 

ImageUtil.m

 

#import "ImageUtil.h"

#include <sys/time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

CGContextRef CreateRGBABitmapContext (CGImageRef inImage) {
	CGContextRef context = NULL; 
	CGColorSpaceRef colorSpace; 
	void *bitmapData; 
	int bitmapByteCount; 
	int bitmapBytesPerRow;
	size_t pixelsWide = CGImageGetWidth(inImage); 
	size_t pixelsHigh = CGImageGetHeight(inImage); 
	bitmapBytesPerRow = (pixelsWide * 4); 
	bitmapByteCount	= (bitmapBytesPerRow * pixelsHigh); 
	colorSpace = CGColorSpaceCreateDeviceRGB();
	if (colorSpace == NULL) {
		fprintf(stderr, "Error allocating color space\n"); return NULL;
	}
    
	bitmapData = malloc( bitmapByteCount ); 
	if (bitmapData == NULL) {
		fprintf (stderr, "Memory not allocated!"); 
		CGColorSpaceRelease( colorSpace ); 
		return NULL;
	}
	context = CGBitmapContextCreate(bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
	if (context == NULL) {
		free (bitmapData); 
		fprintf (stderr, "Context not created!");
	} 
	CGColorSpaceRelease(colorSpace); 
	return context;
}

unsigned char *RequestImagePixelData(UIImage *inImage) {
	CGImageRef img = [inImage CGImage]; 
	CGSize size = [inImage size];
	CGContextRef cgctx = CreateRGBABitmapContext(img); 
	
	if (cgctx == NULL) 
		return NULL;
	
	CGRect rect = {{0, 0}, {size.width, size.height}}; 
	CGContextDrawImage(cgctx, rect, img); 
	unsigned char *data = CGBitmapContextGetData (cgctx); 
	CGContextRelease(cgctx);
	return data;
}

#pragma mark -

@implementation ImageUtil

+ (CGSize) fitSize: (CGSize)thisSize inSize: (CGSize) aSize {
	CGFloat scale;
	CGSize newsize;
	
	if(thisSize.width < aSize.width && thisSize.height < aSize.height) {
		newsize = thisSize;
	} else {
		if(thisSize.width >= thisSize.height) {
			scale = aSize.width / thisSize.width;
			newsize.width = aSize.width;
			newsize.height = thisSize.height * scale;
		} else {
			scale = aSize.height / thisSize.height;
			newsize.height = aSize.height;
			newsize.width = thisSize.width * scale;
		}
	}
	return newsize;
}

+ (UIImage *) image: (UIImage *) image fitInSize: (CGSize) viewsize {
	CGSize size = [ImageUtil fitSize:image.size inSize:viewsize];
	
	UIGraphicsBeginImageContext(size);
    
	CGRect rect = CGRectMake(0, 0, size.width, size.height);
	[image drawInRect:rect];
	
	UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();  
	
	return newimg;  
}

#pragma mark -

+ (UIImage *)blackWhite:(UIImage *)inImage {
	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];
			
			int bw = (int)((red+green + blue) / 3.0);
			
			imgPixel[pixOff] = bw;
			imgPixel[pixOff + 1] = bw;
			imgPixel[pixOff + 2] = bw;
			
			pixOff += 4;
		}
		wOff += w * 4;
	}
	
	NSInteger dataLength = w * h * 4;
	CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
    
	int bitsPerComponent = 8;
	int bitsPerPixel = 32;
	int bytesPerRow = 4 * w;
	CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
	CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
	CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
	
	CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
	
	UIImage *my_Image = [UIImage imageWithCGImage:imageRef];
	
	CFRelease(imageRef);
	CGColorSpaceRelease(colorSpaceRef);
	CGDataProviderRelease(provider);
	return my_Image;
}

+ (UIImage *)cartoon:(UIImage *)inImage {
	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];
			
			int ava = (int)((red + green + blue) / 3.0);
			
			int newAva = ava > 128 ? 255 : 0;
			
			imgPixel[pixOff] = newAva;
			imgPixel[pixOff + 1] = newAva;
			imgPixel[pixOff + 2] = newAva;
			
			pixOff += 4;
		}
		wOff += w * 4;
	}
	
	NSInteger dataLength = w * h * 4;
	CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
    
	int bitsPerComponent = 8;
	int bitsPerPixel = 32;
	int bytesPerRow = 4 * w;
	CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
	CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
	CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
	
	CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
	
	UIImage *my_Image = [UIImage imageWithCGImage:imageRef];
	
	CFRelease(imageRef);
	CGColorSpaceRelease(colorSpaceRef);
	CGDataProviderRelease(provider);
	return my_Image;
}

+ (UIImage *)memory:(UIImage *)inImage {
	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];
			
			red = green = blue = ( red + green + blue ) / 3;
			
			red += red * 2;
			green = green * 2;
			
			if(red > 255)
				red = 255;
			if(green > 255)
				green = 255;
			
			imgPixel[pixOff] = red;
			imgPixel[pixOff + 1] = green;
			imgPixel[pixOff + 2] = blue;
			
			pixOff += 4;
		}
		wOff += w * 4;
	}
	
	NSInteger dataLength = w * h * 4;
	CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
    
	int bitsPerComponent = 8;
	int bitsPerPixel = 32;
	int bytesPerRow = 4 * w;
	CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
	CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
	CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
	
	CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
	
	UIImage *my_Image = [UIImage imageWithCGImage:imageRef];
	
	CFRelease(imageRef);
	CGColorSpaceRelease(colorSpaceRef);
	CGDataProviderRelease(provider);
	return my_Image;
}

+ (UIImage*)bopo:(UIImage*)inImage {
	unsigned char *imgPixel = RequestImagePixelData(inImage);
	CGImageRef inImageRef = [inImage CGImage];
	GLuint w = CGImageGetWidth(inImageRef);
	GLuint h = CGImageGetHeight(inImageRef);
	
	int i, j, m, n;
	int bRowOff;
	int width = 8;
	int height = 8;
	int centerW = width / 2;
	int centerH = height / 2;
    
	int modw = w % width;
	int modh = h % height;
	if(modw) w = w - modw;
	if(modh) h = h - modh;
	
	int br, bg, bb;
	int tr, tg, tb;
	
	double offset;
    
	NSMutableArray *wei = [[NSMutableArray alloc] init];
	for(m = 0; m < height; m++) {
		NSMutableArray *t1 = [[NSMutableArray alloc] init];
		for(n = 0; n < width; n++) {
			[t1 addObject:[NSNull null]];
		}
		[wei addObject:t1];
		[t1 release];
	}
	
	int total = 0;
	int max = (int)(pow(centerH, 2) + pow(centerW, 2));
	
	for(m = 0; m < height; m++) {
		for(n = 0; n < width; n++) {
			offset = max - (int)(pow((m - centerH), 2) + pow((n - centerW), 2));
			total += offset;
            
			[[wei objectAtIndex:m] insertObject:[NSNumber numberWithDouble:offset] atIndex:n];
		}
	}
	for(m = 0; m < height; m++) {
		for(n = 0; n < width; n++) {
			double newVal = [[[wei objectAtIndex:m] objectAtIndex:n] doubleValue] / total;
			[[wei objectAtIndex:m] replaceObjectAtIndex:n withObject:[NSNumber numberWithDouble:newVal]];
		}
	}
	bRowOff = 0;
	for(j = 0; j < h; j += height) {
		int bPixOff = bRowOff;
		
		for(i = 0; i < w; i += width) {
			int bRowOff2 = bPixOff;
			
			tr = tg = tb = 0;
			
			for(m = 0; m < height; m++) {
				int bPixOff2 = bRowOff2;
				
				for(n = 0; n < width; n++) {
					tr += 255 - imgPixel[bPixOff2];
					tg += 255 - imgPixel[bPixOff2 + 1];
					tb += 255 - imgPixel[bPixOff2 + 2];
					
					bPixOff2 += 4;
				}
				
				bRowOff2 += w * 4;
			}
			bRowOff2 = bPixOff;
			
			for(m = 0; m < height; m++) {
				int bPixOff2 = bRowOff2;
				for(n = 0; n < width; n++) {
					offset =  [[[wei objectAtIndex:m] objectAtIndex:n] doubleValue];
					br = 255 - (int)(tr * offset);
					bg = 255 - (int)(tg * offset);
					bb = 255 - (int)(tb * offset);
					
					if(br < 0)
						br = 0;
					if(bg < 0)
						bg = 0;
					if(bb < 0)
						bb = 0;
					imgPixel[bPixOff2] = br;
					imgPixel[bPixOff2 + 1] = bg;
					imgPixel[bPixOff2 + 2] = bb;
					
					bPixOff2 += 4; 
				}
				bRowOff2 += w * 4;
			}
			bPixOff += width * 4; 
		}
		bRowOff += w * height * 4;
	}
	[wei release];
	
	NSInteger dataLength = w * h * 4;
	CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
    
	int bitsPerComponent = 8;
	int bitsPerPixel = 32;
	int bytesPerRow = 4 * w;
	CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
	CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
	CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
	
	CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
	
	UIImage *my_Image = [UIImage imageWithCGImage:imageRef];
	
	CFRelease(imageRef);
	CGColorSpaceRelease(colorSpaceRef);
	CGDataProviderRelease(provider);
	return my_Image;
}

+ (UIImage *)scanLine:(UIImage *)inImage {
	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 += 2) {
		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];
			
			int newR, newG, newB;
			int rr = red * 2;
			newR = rr > 255 ? 255 : rr;
			int gg = green * 2;
			newG = gg > 255 ? 255 : gg;
			int bb = blue * 2;
			newB = bb > 255 ? 255 : bb;
			
			imgPixel[pixOff] = newR;
			imgPixel[pixOff + 1] = newG;
			imgPixel[pixOff + 2] = newB;
			
			pixOff += 4;
		}
		wOff += w * 4 * 2;
	}
	
	NSInteger dataLength = w * h * 4;
	CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
    
	int bitsPerComponent = 8;
	int bitsPerPixel = 32;
	int bytesPerRow = 4 * w;
	CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
	CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
	CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    
	CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
	
	UIImage *my_Image = [UIImage imageWithCGImage:imageRef];
	
	CFRelease(imageRef);
	CGColorSpaceRelease(colorSpaceRef);
	CGDataProviderRelease(provider);
	return my_Image;
}

@end

 

效果图:


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics