2011-06-18 142 views
0

我正在mp應用程序中進行圖像處理。我從圖像中獲得了像素顏色,並通過觸摸將其應用於圖像上。我的代碼獲取像素顏色,但它以藍色更改整個圖像,並在圖像處理中應用該藍色。我被困在代碼中。但不知道我的代碼中出了什麼問題,請你幫幫我。RGB在iPhone中的圖像處理應用程序

我的代碼是:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [touches anyObject]; 
CGPoint coordinateTouch = [touch locationInView:[self view]];//where image was tapped 

if (value == YES) { 
    self.lastColor = [self getPixelColorAtLocation:coordinateTouch]; 
    value =NO; 
} 

NSLog(@"color %@",lastColor); 
//[pickedColorDelegate pickedColor:(UIColor*)self.lastColor]; 



ListPoint point; 
point.x = coordinateTouch.x; 
point.y = coordinateTouch.y; 

button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.backgroundColor = [UIColor whiteColor]; 
button.frame = CGRectMake(coordinateTouch.x-5, coordinateTouch.y-5, 2, 2); 
//[descImageView addSubview:button]; 

[bgImage addSubview:button]; 

// Make image blurred on ImageView 
if(bgImage.image) 
{ 

    CGImageRef imgRef = [[bgImage image] CGImage]; 
    CFDataRef dataRef = CGDataProviderCopyData(CGImageGetDataProvider(imgRef)); 
    const unsigned char *sourceBytesPtr = CFDataGetBytePtr(dataRef); 
    int len = CFDataGetLength(dataRef); 
    NSLog(@"length = %d, width = %d, height = %d, bytes per row = %d, bit per pixels = %d", 
      len, CGImageGetWidth(imgRef), CGImageGetHeight(imgRef), CGImageGetBytesPerRow(imgRef), CGImageGetBitsPerPixel(imgRef)); 

    int width = CGImageGetWidth(imgRef); 
    int height = CGImageGetHeight(imgRef); 
    int widthstep = CGImageGetBytesPerRow(imgRef); 
    unsigned char *pixelData = (unsigned char *)malloc(len); 
    double wFrame = bgImage.frame.size.width; 
    double hFrame = bgImage.frame.size.height; 

    Image_Correction(sourceBytesPtr, pixelData, widthstep, width, height, wFrame, hFrame, point); 

    NSLog(@"finish"); 


    NSData *data = [NSData dataWithBytes:pixelData length:len]; 

    NSLog(@"1"); 
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data); 

    NSLog(@"2"); 
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB(); 

    NSLog(@"3"); 
    CGImageRef imageRef = CGImageCreate(width, height, 8, CGImageGetBitsPerPixel(imgRef), CGImageGetBytesPerRow(imgRef), 
             colorSpace2,kCGImageAlphaNoneSkipFirst|kCGBitmapByteOrder32Host, 
             provider, NULL, false, kCGRenderingIntentDefault); 

    NSLog(@"Start processing image"); 
    UIImage *ret = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:UIImageOrientationUp]; 
    CGImageRelease(imageRef); 
    CGDataProviderRelease(provider); 
    CGColorSpaceRelease(colorSpace2); 
    CFRelease(dataRef); 
    free(pixelData); 
    NSLog(@"4"); 
    bgImage.image = ret; 
    [button removeFromSuperview]; 
} 
} 




- (UIColor*) getPixelColorAtLocation:(CGPoint)point { 


UIColor* color = nil; 
CGImageRef inImage = self.image.CGImage; 
// Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue 
CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage]; 
if (cgctx == NULL) { return nil; /* error */ } 

size_t w = CGImageGetWidth(inImage); 
size_t h = CGImageGetHeight(inImage); 
CGRect rect = {{0,0},{w,h}}; 

// Draw the image to the bitmap context. Once we draw, the memory 
// allocated for the context for rendering will then contain the 
// raw image data in the specified color space. 
CGContextDrawImage(cgctx, rect, inImage); 

// Now we can get a pointer to the image data associated with the bitmap 
// context. 
unsigned char* data = CGBitmapContextGetData (cgctx); 
if (data != NULL) { 
    //offset locates the pixel in the data from x,y. 
    //4 for 4 bytes of data per pixel, w is width of one row of data. 
    int offset = 4*((w*round(point.y))+round(point.x)); 
    alpha = data[offset]; 
    red = data[offset+1]; 
    green = data[offset+2]; 
    blue = data[offset+3]; 
    NSLog(@"offset: %i colors: RGB A %i %i %i %i",offset,red,green,blue,alpha); 
    color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)]; 
} 

// When finished, release the context 
CGContextRelease(cgctx); 
// Free image data memory for the context 
if (data) { free(data); } 

return color; 
} 





- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage { 

CGContextRef context = NULL; 
CGColorSpaceRef colorSpace; 
void *   bitmapData; 
int    bitmapByteCount; 
int    bitmapBytesPerRow; 

// Get image width, height. We'll use the entire image. 
size_t pixelsWide = CGImageGetWidth(inImage); 
size_t pixelsHigh = CGImageGetHeight(inImage); 

// Declare the number of bytes per row. Each pixel in the bitmap in this 
// example is represented by 4 bytes; 8 bits each of red, green, blue, and 
// alpha. 
bitmapBytesPerRow = (pixelsWide * 4); 
bitmapByteCount  = (bitmapBytesPerRow * pixelsHigh); 

// Use the generic RGB color space. 
colorSpace = CGColorSpaceCreateDeviceRGB(); 

if (colorSpace == NULL) 
{ 
    fprintf(stderr, "Error allocating color space\n"); 
    return NULL; 
} 

// Allocate memory for image data. This is the destination in memory 
// where any drawing to the bitmap context will be rendered. 
bitmapData = malloc(bitmapByteCount); 
if (bitmapData == NULL) 
{ 
    fprintf (stderr, "Memory not allocated!"); 
    CGColorSpaceRelease(colorSpace); 
    return NULL; 
} 

// Create the bitmap context. We want pre-multiplied ARGB, 8-bits 
// per component. Regardless of what the source image format is 
// (CMYK, Grayscale, and so on) it will be converted over to the format 
// specified here by CGBitmapContextCreate. 
context = CGBitmapContextCreate (bitmapData, 
           pixelsWide, 
           pixelsHigh, 
           8,  // bits per component 
           bitmapBytesPerRow, 
           colorSpace, 
           kCGImageAlphaPremultipliedFirst); 
if (context == NULL) 
{ 
    free (bitmapData); 
    fprintf (stderr, "Context not created!"); 
} 

// Make sure and release colorspace before returning 
CGColorSpaceRelease(colorSpace); 

return context; 
} 



int Image_Correction(const unsigned char *pImage, unsigned char *rImage, int widthstep, int nW, int nH, double wFrame, double hFrame, ListPoint point)    

{ 
double ratiox = nW/wFrame; 
double ratioy = nH/hFrame; 
double newW, newH, ratio; 
if(ratioy > ratiox) 
{ 
    newH = hFrame; 
    newW = nW/ratioy; 
    ratio = ratioy; 
} 
else 
{ 
    newH = nH/ratiox; 
    newW = wFrame; 
    ratio = ratiox; 
} 
NSLog(@"new H, W = %f, %f", newW, newH); 
NSLog(@"ratiox = %f; ratioy = %f", ratiox, ratioy); 

ListPoint real_point; 
real_point.x = (point.x - wFrame/2 + newW/2) *ratio; 
real_point.y = (point.y - hFrame/2 + newH/2)*ratio; 

for(int h = 0; h < nH; h++) 
{ 
    for(int k = 0; k < nW; k++) 
    { 
     rImage[h*widthstep + k*4 + 0] = pImage[h*widthstep + k*4 + 0]; 
     rImage[h*widthstep + k*4 + 1] = pImage[h*widthstep + k*4 + 1]; 
     rImage[h*widthstep + k*4 + 2] = pImage[h*widthstep + k*4 + 2]; 
     rImage[h*widthstep + k*4 + 3] = pImage[h*widthstep + k*4 + 3]; 
    } 
} 

// Modify this parameter to change Blurred area 
int iBlurredArea = 6; 
for(int h = -ratio*iBlurredArea; h <= ratio*iBlurredArea; h++) 
    for(int k = -ratio*iBlurredArea; k <= ratio*iBlurredArea; k++) 
    { 
     int tempx = real_point.x + k; 
     int tempy = real_point.y + h; 
     if (((tempy - 3) > 0)&&((tempy+3) >0)&&((tempx - 3) > 0)&&((tempx + 3) >0)) 
     { 
      double sumR = 0; 
      double sumG = 0; 
      double sumB = 0; 
      double sumA = 0; 
      double count = 0; 
      for(int m = -3; m < 4; m++) 
       for (int n = -3; n < 4; n++) 
       {      
        sumR = red;//sumR + pImage[(tempy + m)*widthstep + (tempx + n)*4 + 0]; 
        sumG = green;//sumG + pImage[(tempy + m)*widthstep + (tempx + n)*4 + 1]; 
        sumB = blue;//sumB + pImage[(tempy + m)*widthstep + (tempx + n)*4 + 2]; 
        sumA = alpha;//sumA + pImage[(tempy + m)*widthstep + (tempx + n)*4 + 3]; 
        count++; 
       } 



      rImage[tempy*widthstep + tempx*4 + 0] = red;//sumR/count; 
      rImage[tempy*widthstep + tempx*4 + 1] = green;//sumG/count; 
      rImage[tempy*widthstep + tempx*4 + 2] = blue;//sumB/count; 
      rImage[tempy*widthstep + tempx*4 + 3] = alpha;//sumA/count; 
     } 
    } 
return 1; 
} 

THX爲看到這個代碼..我覺得我做錯了什麼。 Thx提前。

+1

對於如此龐大的代碼牆,你會得到很少的迴應。如果您想增加獲得答案的機會,您需要將其降低爲解釋問題的最小示例。 –

+0

你想要做什麼,這是什麼問題? –

+0

我想這樣做..但可能會從代碼錯過的東西..然後呢? –

回答

0

這似乎適用於我。

UIImage* modifyImage(UIImage* image) 
{ 
    size_t w = image.size.width; 
    size_t h = image.size.height; 
    CGFloat scale = image.scale; 

    // Create the bitmap context 
    UIGraphicsBeginImageContext(CGSizeMake(w*scale, h*scale)); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // NOTE you may have to setup a rotation here based on image.imageOrientation 
    // but I didn't need to consider that for my images. 
    CGContextScaleCTM(context, scale, scale); 
    [image drawInRect:CGRectMake(0, 0, w, h)]; 

    unsigned char* data = CGBitmapContextGetData (context); 
    if (data != NULL) { 
     size_t height = CGBitmapContextGetHeight(context); 
     size_t width = CGBitmapContextGetWidth(context); 
     size_t bytesPerRow = CGBitmapContextGetBytesPerRow(context); 

     for (int y = 0; y < height; y++) { 
      for (int x = 0; x < width; x++) { 
       // Not sure why the color info is in BGRA format 
       // Look at CGBitmapContextGetBitmapInfo(context) if this format isn't working for you 
       int offset = y * bytesPerRow + x * 4; 
       unsigned char* blue = &data[offset]; 
       unsigned char* green = &data[offset+1]; 
       unsigned char* red = &data[offset+2]; 
       unsigned char* alpha = &data[offset+3]; 

       int newRed = ...; // color calculation code here 
       int newGreen = ...; 
       int newBlue = ...; 

       // Assuming you don't want to change the original alpha value. 
       *red = (newRed * *alpha)/255; 
       *green = (newGreen * *alpha)/255; 
       *blue = (newBlue * *alpha)/255; 
      }    
     } 
    } 

    CGImageRef newImage = CGBitmapContextCreateImage(context); 

    UIImage *done = [UIImage imageWithCGImage:newImage scale:image.scale orientation: image.imageOrientation]; 
    CGImageRelease(newImage); 
    UIGraphicsEndImageContext(); 

    return done; 
}