Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is possible and I previously tried two paths:</p> <ol> <li><p>Convert to Grayscale then apply pixel by pixel conversion to B&amp;W.<br/> --Problem with this is I don't get good results with images with transparency.</p></li> <li><p>If you're not very strict, given an RGBA image, get the pixel RGB average and convert to B&amp;W with a supplied threshold and retain its transparency. Technically this is still RGBA but more of Black, White and Transparency.</p></li> </ol> <p>e.g.</p> <pre><code> UIImage *originalImage = [UIImage imageNamed:@"option_bluetooth.png"]; unsigned char *pixelBuffer = [self getPixelData:originalImage.CGImage]; size_t length = originalImage.size.width * originalImage.size.height * 4; CGFloat intensity; int bw; //50% threshold const CGFloat THRESHOLD = 0.5; for (int index = 0; index &lt; length; index += 4) { intensity = (pixelBuffer[index] + pixelBuffer[index + 1] + pixelBuffer[index + 2]) / 3. / 255.; if (intensity &gt; THRESHOLD) { bw = 255; } else { bw = 0; } pixelBuffer[index] = bw; pixelBuffer[index + 1] = bw; pixelBuffer[index + 2] = bw; } CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB(); CGContextRef bitmapContext=CGBitmapContextCreate(pixelBuffer, originalImage.size.width, originalImage.size.height, 8, 4*originalImage.size.width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault); CFRelease(colorSpace); free(pixelBuffer); CGImageRef cgImage=CGBitmapContextCreateImage(bitmapContext); CGContextRelease(bitmapContext); UIImage *bwImage = [UIImage imageWithCGImage:cgImage]; </code></pre> <p>I get the pixel data by writing to an offscreen context (the new way of getting the raw data which Apple suggests does not work for me)</p> <p>e.g.</p> <pre><code> CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); unsigned char *rawData = malloc(imageHeight * imageWidth * 4); CGContextRef offscreenContext = CGBitmapContextCreate(rawData, imageWidth, imageHeight, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault); CGColorSpaceRelease(colorSpace); CGContextDrawImage(offscreenContext, CGRectMake(0, 0, imageWidth, imageHeight), cgCropped); CGContextRelease(offscreenContext); </code></pre> <p>Here's the code to get Pixel Data</p> <pre><code>+ (unsigned char *) getPixelData: (CGImageRef) cgCropped { size_t imageWidth = CGImageGetWidth(cgCropped); size_t imageHeight = CGImageGetHeight(cgCropped); size_t bitsPerComponent = 8; size_t bytesPerPixel = 4; size_t bytesPerRow = bytesPerPixel * imageWidth; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); unsigned char *rawData = malloc(imageHeight * imageWidth * 4); CGContextRef offscreenContext = CGBitmapContextCreate(rawData, imageWidth, imageHeight, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault); CGColorSpaceRelease(colorSpace); CGContextDrawImage(offscreenContext, CGRectMake(0, 0, imageWidth, imageHeight), cgCropped); CGContextRelease(offscreenContext); return rawData; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload