计算不规则形状的像素

时间:2017-04-07 22:37:02

标签: objective-c pixels

我有一个用Obj C编写的现有应用程序,可以在iTunes App Store上运行。我正在更新它到Swift。我在应用程序的像素分类和计数部分遇到问题。 Objective C代码如下:如果您继续使用Swift 3版本,我将不胜感激。

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 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<image_height; y++)
        {
            unsigned char* nth_pixel_p = row_start_p;
            for (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);
}

0 个答案:

没有答案