title: Count pixels in an irregular shape
body: <p>I have an existing app, written in Obj C, that works and is on the iTunes App Store. I am updating it to Swift. I am having trouble with the pixel classification and counting portion of the app. The Objective C code is below: I would appreciate any suggestions as how to proceed with the Swift 3 version.</p>
  
  <pre><code>void classifyPixel(unsigned char r, unsigned char g, unsigned char b,
                 NSUInteger *coverRed, NSUInteger *coverGreen, NSUInteger *coverYellow, NSUInteger *coverBlue, NSUInteger *coverWhite)
  {
  if ((r == 255) &amp;&amp; (g == 0) &amp;&amp; (b == 0))
      *coverRed = *coverRed + 1;
  else if ((r == 0) &amp;&amp; (g == 255) &amp;&amp; (b == 0))
      *coverGreen = *coverGreen + 1;
  else if ((r == 255) &amp;&amp; (g == 255) &amp;&amp; (b == 0))
      *coverYellow = *coverYellow +1;
  else if ((r == 0) &amp;&amp; (g == 0) &amp;&amp; (b== 255))
      *coverBlue = *coverBlue + 1;
  else if ((r == 255) &amp; (g == 255) &amp;&amp; (b == 255))
      *coverWhite = *coverWhite +1;
  }
  
  - (IBAction)computeArea:(id)sender {
  // Taken from Hugh's original code.  With some slight clean up
  UIImage *image = self.imageView.image;
  
  NSUInteger coverRed = 0;
  NSUInteger coverGreen = 0;
  NSUInteger coverYellow = 0;
  NSUInteger coverBlue = 0;
  NSUInteger coverWhite = 0;
  NSUInteger coverTotal = 0;
  
  size_t image_width = image.size.width;
  size_t image_height = image.size.height;
  
  size_t bitsPerComponent = 8;
  size_t bytesPerPixel = 4;
  
  size_t bytesPerRow = bytesPerPixel*image_width;
  
  unsigned char* raster_buffer = (unsigned char*) calloc(bitsPerComponent, bytesPerRow *image.size.height);
  if(raster_buffer !=NULL)
  {
      CGContextRef context = CGBitmapContextCreate(
                                                   (void*) raster_buffer,
                                                   image.size.width,
                                                   image.size.height,
                                                   bitsPerComponent,
                                                   bytesPerRow,
                                                   CGImageGetColorSpace(image.CGImage),
                                                   (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
  
      if (context != NULL)
      {
          CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), image.CGImage);
  
          unsigned char* row_start_p = raster_buffer;
  
          for (size_t y=0; y&lt;image_height; y++)
          {
              unsigned char* nth_pixel_p = row_start_p;
              for (size_t x=0; x&lt;image_width; x++)
              {
                  classifyPixel(nth_pixel_p[bytesPerPixel*x],
                                nth_pixel_p[bytesPerPixel*x+1],
                                nth_pixel_p[bytesPerPixel*x+2],
                                &amp;coverRed,&amp;coverGreen,&amp;coverYellow,&amp;coverBlue,&amp;coverWhite);
                  coverTotal++;
              }
              row_start_p+=bytesPerRow;
          }
      }
  
  }
  
  free(raster_buffer);
  
  float uu = coverGreen;
  float vv = coverYellow;
  float ww = coverRed;
  float xx = coverBlue;
  float yy = coverWhite;
  float zz = uu + vv + ww + xx + yy;
  
  float aa = (uu/zz)*100;
  float bb = (vv/zz)*100;
  float cc = (ww/zz)*100;
  float dd = (xx/zz)*100;
  float ee = (yy/zz)*100;
  
  totalcount = [NSNumber numberWithFloat:zz];
  
  greencountresult = [NSString stringWithFormat:@"%.1f%%",aa];
  yellowcountresult = [NSString stringWithFormat:@"%.1f%%",bb];
  redcountresult = [NSString stringWithFormat:@"%.1f%%",cc];
  bluecountresult = [NSString stringWithFormat:@"%.1f%%",dd];
  whitecountresult = [NSString stringWithFormat:@"%.1f%%",ee];
  
  NSLog(@"Area compute! Red %lu Green %lu Yellow %lu Blue %lu White %lu\n",(unsigned long)coverRed,(unsigned long)coverGreen,(unsigned long)coverYellow,(unsigned long)coverBlue,(unsigned long)coverWhite);
  }
  </code></pre>