How to do comparisons of bitmaps in iOS?

前端 未结 2 1020
故里飘歌
故里飘歌 2021-01-22 20:10

I have three CGLayers who\'s data I\'d like to compare.

void *a = CGBitmapContextGetData(CGLayerGetContext(layerA));
void *b = CGBitmapContextGetData(CGLayerGetC         


        
2条回答
  •  心在旅途
    2021-01-22 20:59

    Here's a naive implementation, assuming all three are the same size:

    unsigned char *a = CGBitmapContextGetData(CGLayerGetContext(layerA));
    unsigned char *b = CGBitmapContextGetData(CGLayerGetContext(layerB));
    CGContextRef context = CGLayerGetContext(layerC);
    unsigned char *c = CGBitmapContextGetData(context);
    size_t bytesPerRow = CGBitmapContextGetBytesPerRow(context);
    size_t height = CGBitmapContextGetHeight(context);
    size_t len = bytesPerRow * height;
    BOOL bitsFound = NO;
    for (int i = 0; i < len; i++) {
        if ((a[i] | b[i]) & c[i]) { bitsFound = YES; break; }
    }
    

    Since you're hankering for QuickDraw, I assume you could have written that yourself, and you know that will probably be slow.

    If you can guarantee the bitmap sizes, you could use int instead of char and operate on four bytes at a time.

    For more serious optimization, you should check out the Accelerate framework.

提交回复
热议问题