- (UIImage *) convertToGreyscale:(UIImage *)i {
int kRed =1;
int kGreen =2;
int kBlue =4;
int colors =kGreen;
int m_width= i.size.width;
int m_height= i.size.height;
uint32_t*rgbImage = (uint32_t *) malloc(m_width * m_height *sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDevice
RGB();
CGContextRefcontext = CGBitmapContextCreate(rgbImage, m_width, m_height, 8,m_width * 4, colorSpace, kCGBitmapByteOrder32Litt
le |kCGImageAlphaNoneSkipLas
t);
CGContextSetInterpolatio
nQuality(context,kCGInterpolationHigh);
CGContextSetShouldAntial
ias(context, NO);
CGContextDrawImage(context, CGRectMake(0, 0, m_width, m_height), [iCGImage]);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
// nowconvert to grayscale
uint8_t*m_imageData = (uint8_t *) malloc(m_width * m_height);
for(int y =0; y < m_height; y++) {
for(int x = 0; x < m_width; x++) {
uint32_t rgbPixel=rgbImage[y*m_width+x];
uint32_t sum=0,count=0;
if (colors & kRed) {sum +=(rgbPixel>>24)&255;count++;}
if (colors & kGreen) {sum +=(rgbPixel>>16)&255;count++;}
if (colors & kBlue) {sum +=(rgbPixel>>8)&255;count++;}
m_imageData[y*m_width+x]=sum/count;
}
}
free(rgbImage);
// convertfrom a gray scale image back into a UIImage
uint8_t*result = (uint8_t *) calloc(m_width * m_height *sizeof(uint32_t),1);
// processthe image back to rgb
for(int i =0; i < m_height * m_width; i++) {
result[i*4]=0;
int val=m_imageData[i];
result[i*4+1]=val;
result[i*4+2]=val;
result[i*4+3]=val;
}
// create aUIImage
colorSpace =CGColorSpaceCreateDevice
RGB();
context =CGBitmapContextCreate(result, m_width, m_height, 8, m_width *sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Litt
le |kCGImageAlphaNoneSkipLas
t);
CGImageRefimage = CGBitmapContextCreateIma
ge(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage*resultUIImage = [UIImage imageWithCGImage:image];
CGImageRelease(image);
// make surethe data will be released by giving it to an autoreleasedNSData
[NSDatadataWithBytesNoCopy:result length:m_width * m_height];
returnresultUIImage;
}
}