Wednesday 15 May 2013

ios - Convert UIImage to 8 bits -


I want to convert a UIImage to 8 bit. I have tried to do this, but I'm not sure that I have corrected this because I am getting a message later when I try to use the image processing library, Leptika, which states that it is not 8 bits . Can anyone tell me that I am doing this right or how is it code to do it?

Thank you!

CODE

  CGI image reef ICGIIMAGE = image. CGImage; CFDataRef data = CGDAPproduct data (CGIMaggetDateProvider (MikeGimage)); Const. UInt8 * imageData = CFDataGetBytePtr (Data); The following code will work for images without alpha channels:  
   

CGImage RF C = [[UIImage Image Named: @ "100_3077"] CGIm]; Size_t bitspipers = cimgatebit pixel (c); Size_t bitscrimp = CGImageGetBitsPerComponent (c); Size_t width = CGImageGetWidth (c); Size_t height = CGMgate height (C); CGImageAlphaInfo a = CGMMetAlphinfo (C); NSSS (bitspixel == 32 & amp; amp; amp; amp; amp; amp;; amp; amp;; 2 bitPointcompanyent == 8 & amp; A ==KCGIZAlfaNonscapeList, @ "Unsupported image type supply"); CGContextRef target image = CGBitmapContextCreate (faucet, width, height, 8, 1 * CGMJetWidth (C), CGColorSpaceCreateDeviceGray (), KCGImageAlphaN;); UInt32 * sourceData = ((__Bridge_transferNsDTA * *) CGDTTwyPowerCopy data (CGIMaggetDateProvider (C)) Bytes]; UInt32 * sourceDataPtr; UInt8 * targetData = CGBitmapContextGetData (target image); UInt8R, G, B; For (UIT Y = 0; Y & lt; height; y ++); (UIT X = 0; XLL; width; x ++) {offset = y * width = x; if (offset + 2 & Lt; width * height] {sourceDataPtr = & amp; source.data [y * width + x]; R = sourceDataPtr [0 + 0]; G = sourceDataPtr [0 + 1]; B = sourceDataPtr [0 + 2]; target data [ Y * wide E + x] = (R + G + B) / 3;}}} CGImageRef newImageRef = CGBitmapContextCreateImage (targetImage); UIImage * newImage = [UIImage imageWithCGImage: newImageRef]; CGContextRelease (targetImage); CGImageRelease (newImageRef);

With this code I changed an RGB image in a grayscale image:  Original image  grayscale image

Hope this helps

No comments:

Post a Comment