title: iPhone Image Processing--matrix convolution
body: <p>I am implementing a matrix convolution blur on the iPhone.  The following code converts the UIImage supplied as an argument of the blur function into a CGImageRef, and then stores the RGBA values in a standard C char array.  </p>
  
  <pre><code>    CGImageRef imageRef = imgRef.CGImage;
  int width = imgRef.size.width;
  int height = imgRef.size.height;
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  unsigned char *pixels = malloc((height) * (width) * 4);
  NSUInteger bytesPerPixel = 4;
  NSUInteger bytesPerRow = bytesPerPixel * (width);
  NSUInteger bitsPerComponent = 8;
  CGContextRef context = CGBitmapContextCreate(pixels, width, height,
                                               bitsPerComponent, bytesPerRow, colorSpace,
                                               kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
      CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
  CGContextRelease(context);
  </code></pre>
  
  <p>Then the pixels values stored in the pixels array are convolved, and stored in another array. </p>
  
  <pre><code>unsigned char *results = malloc((height) * (width) * 4);
  </code></pre>
  
  <p>Finally, these augmented pixel values are changed back into a CGImageRef, converted to a UIImage, and the returned at the end of the function with the following code.</p>
  
  <pre><code>    context = CGBitmapContextCreate(results, width, height,
                                  bitsPerComponent, bytesPerRow, colorSpace,
                                  kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
  
  CGImageRef finalImage = CGBitmapContextCreateImage(context);
  
  
  UIImage *newImage  = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
  CGImageRelease(finalImage);
  
  NSLog(@"edges found");
  free(results);
  free(pixels);
  CGColorSpaceRelease(colorSpace);
  
  
  return newImage;
  </code></pre>
  
  <p>This works perfectly, once.  Then, once the image is put through the filter again, very odd, unprecedented pixel values representing input pixel values that don't exist, are returned.  Is there any reason why this should work the first time, but then not afterward?  Beneath is the entirety of the function.</p>
  
  <pre><code>    -(UIImage*) blur:(UIImage*)imgRef {
  CGImageRef imageRef = imgRef.CGImage;
  int width = imgRef.size.width;
  int height = imgRef.size.height;
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  unsigned char *pixels = malloc((height) * (width) * 4);
  NSUInteger bytesPerPixel = 4;
  NSUInteger bytesPerRow = bytesPerPixel * (width);
  NSUInteger bitsPerComponent = 8;
  CGContextRef context = CGBitmapContextCreate(pixels, width, height,
                                               bitsPerComponent, bytesPerRow, colorSpace,
                                               kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
      CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
  CGContextRelease(context);
  
  
  height = imgRef.size.height;
  width = imgRef.size.width;
  float matrix[] = {1,1,1,1,1,1,1,1,1};
  float divisor = 9;
  float shift = 0;
  
  
  unsigned char *results = malloc((height) * (width) * 4);
  
  
  for(int y = 1; y &lt; height; y++){
      for(int x = 1; x &lt; width; x++){
          float red = 0;
          float green = 0;
          float blue = 0;
          int multiplier=1;
  
          if(y&gt;0 &amp;&amp; x&gt;0){
          int index = (y-1)*width + x;
          red = matrix[0]*multiplier*(float)pixels[4*(index-1)] +
          matrix[1]*multiplier*(float)pixels[4*(index)] +
          matrix[2]*multiplier*(float)pixels[4*(index+1)];
          green = matrix[0]*multiplier*(float)pixels[4*(index-1)+1] +
          matrix[1]*multiplier*(float)pixels[4*(index)+1] +
          matrix[2]*multiplier*(float)pixels[4*(index+1)+1];
          blue = matrix[0]*multiplier*(float)pixels[4*(index-1)+2] +
          matrix[1]*multiplier*(float)pixels[4*(index)+2] +
          matrix[2]*multiplier*(float)pixels[4*(index+1)+2];
  
          index = (y)*width + x;
  
          red = red+ matrix[3]*multiplier*(float)pixels[4*(index-1)] +
          matrix[4]*multiplier*(float)pixels[4*(index)] +
          matrix[5]*multiplier*(float)pixels[4*(index+1)];
          green = green + matrix[3]*multiplier*(float)pixels[4*(index-1)+1] +
          matrix[4]*multiplier*(float)pixels[4*(index)+1] +
          matrix[5]*multiplier*(float)pixels[4*(index+1)+1];
          blue = blue + matrix[3]*multiplier*(float)pixels[4*(index-1)+2] +
          matrix[4]*multiplier*(float)pixels[4*(index)+2] +
          matrix[5]*multiplier*(float)pixels[4*(index+1)+2];
  
  
          index = (y+1)*width + x;
          red = red+ matrix[6]*multiplier*(float)pixels[4*(index-1)] +
          matrix[7]*multiplier*(float)pixels[4*(index)] +
          matrix[8]*multiplier*(float)pixels[4*(index+1)];
          green = green + matrix[6]*multiplier*(float)pixels[4*(index-1)+1] +
          matrix[7]*multiplier*(float)pixels[4*(index)+1] +
          matrix[8]*multiplier*(float)pixels[4*(index+1)+1];
          blue = blue + matrix[6]*multiplier*(float)pixels[4*(index-1)+2] +
          matrix[7]*multiplier*(float)pixels[4*(index)+2] +
          matrix[8]*multiplier*(float)pixels[4*(index+1)+2];
  
          red = red/divisor+shift;
          green = green/divisor+shift;
          blue = blue/divisor+shift;
  
          if(red&lt;0){
              red=0;
          }
          if(green&lt;0){
              green=0;
          }
          if(blue&lt;0){
              blue=0;
          }
  
          if(red&gt;255){
              red=255;
          }
          if(green&gt;255){
              green=255;
          }
          if(blue&gt;255){
              blue=255;
          }
  
          int realPos = 4*(y*imgRef.size.width + x);
  
          results[realPos] = red;
          results[realPos + 1] = green;
          results[realPos + 2] = blue;
          results[realPos + 3] = 1;
  
              }else {
           int realPos = 4*((y)*(imgRef.size.width) + (x));
           results[realPos] = 0;
           results[realPos + 1] = 0;
           results[realPos + 2] = 0;
           results[realPos + 3] = 1;
           }
  
      }
  }
  
  context = CGBitmapContextCreate(results, width, height,
                                  bitsPerComponent, bytesPerRow, colorSpace,
                                  kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
  
  CGImageRef finalImage = CGBitmapContextCreateImage(context);
  
  
  UIImage *newImage  = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
  CGImageRelease(finalImage);
  
  free(results);
  free(pixels);
  CGColorSpaceRelease(colorSpace);
  
  
  return newImage;}
  </code></pre>
  
  <p>THANKS!!!</p>