objective c - Count pixels in an irregular shape -


i have existing app, written in obj c, works , on itunes app store. updating swift. having trouble pixel classification , counting portion of app. objective c code below: appreciate suggestions how proceed swift 3 version.

void classifypixel(unsigned char r, unsigned char g, unsigned char b,                nsuinteger *coverred, nsuinteger *covergreen, nsuinteger *coveryellow, nsuinteger *coverblue, nsuinteger *coverwhite) { if ((r == 255) && (g == 0) && (b == 0))     *coverred = *coverred + 1; else if ((r == 0) && (g == 255) && (b == 0))     *covergreen = *covergreen + 1; else if ((r == 255) && (g == 255) && (b == 0))     *coveryellow = *coveryellow +1; else if ((r == 0) && (g == 0) && (b== 255))     *coverblue = *coverblue + 1; else if ((r == 255) & (g == 255) && (b == 255))     *coverwhite = *coverwhite +1; }  - (ibaction)computearea:(id)sender { // taken hugh's original code.  slight clean 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;          (size_t y=0; y<image_height; y++)         {             unsigned char* nth_pixel_p = row_start_p;             (size_t x=0; x<image_width; x++)             {                 classifypixel(nth_pixel_p[bytesperpixel*x],                               nth_pixel_p[bytesperpixel*x+1],                               nth_pixel_p[bytesperpixel*x+2],                               &coverred,&covergreen,&coveryellow,&coverblue,&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); } 


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -