Convert png to pixelbuffer and use vimage to calculate color distribution histograms. Then check the alpha channel histogram. vimage is much faster than through pixels one by one.
CVPixelBufferRef pxbuffer = NULL; vImagePixelCount histogramA[256]; vImagePixelCount histogramR[256]; vImagePixelCount histogramG[256]; vImagePixelCount histogramB[256]; vImagePixelCount *histogram[4]; histogram[0] = histogramA; histogram[1] = histogramR; histogram[2] = histogramG; histogram[3] = histogramB; vImage_Buffer vbuff; vbuff.height = CVPixelBufferGetHeight(pxbuffer); vbuff.width = CVPixelBufferGetWidth(pxbuffer); vbuff.rowBytes = CVPixelBufferGetBytesPerRow(pxbuffer); vbuff.data = pxbuffer; vImage_Error err=vImageHistogramCalculation_ARGB8888 (&vbuff,histogram, 0); if (err != kvImageNoError) NSLog(@"%ld", err); BOOL transparent; for(int i=1;i<256;i++){ if(histogram[0][i]>0) transparent=YES; }
vimage assumes that the colors are ordered by ARGB in the buffer. If you have something else, for example. BGRA, you just check the histogram [3] [i].
Even faster, it is possible to first split the buffer into four planar buffers using vImageConvert_ARGB8888toPlanar8, and then simply perform histogram calculation in the alpha buffer using vImageHistogramCalculation_Planar8.
If you are on iOS7 and later you can open png as a CGImage and use vImageBuffer_initWithCGImage to convert it directly to a vimage buffer (see session 703 at WWDC 2014).
Finally, an alternative would be to use Core Image to calculate the histogram.
Sten
source share