2013-06-22 3 views
2

UIImage를 8 비트로 변환하고 싶습니다. 나는 이것을 시도했지만 이미지 라이브러리를 사용하려고 할 때 나중에 메시지를 얻고 있기 때문에 제대로했는지 확신 할 수 없다. 8 비트가 아니라는 것을 보여주는 leptonica 라이브러리. 아무도 내가 올바르게 또는 코드를 수행하는 방법을 말해 줄 수 있습니까?UIImage를 8 비트로 변환하십시오.

감사합니다.

CODE

CGImageRef myCGImage = image.CGImage; 
CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(myCGImage)); 
const UInt8 *imageData = CFDataGetBytePtr(data); 
+0

원시 픽셀 데이터에 액세스하려면 [this] (http://stackoverflow.com/questions/6073259/getest-rgb-pixel-data-from-cgimage)를 보시기 바랍니다. 변환을 수행해야합니다. 8 비트. 형식이 미리 알려지면 매우 간단합니다. RGBA는 R, G, B 채널의 평균을 찾습니다. –

+0

그는 회색 음영을 의미한다고 생각합니다. Leptonica는 이미지 처리 라이브러리입니다. 대부분의 이미지 처리 라이브러리는 회색 음영 이미지와 함께 작동합니다. –

답변

6

알파 채널없이 이미지에 대한 작동 코드를 다음과 같습니다 Original image Grayscale image

: 내가 그레이 스케일 이미지에 RGB 이미지를 변환이 코드와

CGImageRef c = [[UIImage imageNamed:@"100_3077"] CGImage]; 

    size_t bitsPerPixel = CGImageGetBitsPerPixel(c); 
    size_t bitsPerComponent = CGImageGetBitsPerComponent(c); 
    size_t width = CGImageGetWidth(c); 
    size_t height = CGImageGetHeight(c); 

    CGImageAlphaInfo a = CGImageGetAlphaInfo(c); 

    NSAssert(bitsPerPixel == 32 && bitsPerComponent == 8 && a == kCGImageAlphaNoneSkipLast, @"unsupported image type supplied"); 

    CGContextRef targetImage = CGBitmapContextCreate(NULL, width, height, 8, 1 * CGImageGetWidth(c), CGColorSpaceCreateDeviceGray(), kCGImageAlphaNone); 

    UInt32 *sourceData = (UInt32*)[((__bridge_transfer NSData*) CGDataProviderCopyData(CGImageGetDataProvider(c))) bytes]; 
    UInt32 *sourceDataPtr; 

    UInt8 *targetData = CGBitmapContextGetData(targetImage); 

    UInt8 r,g,b; 
    uint offset; 
    for (uint y = 0; y < height; y++) 
    { 
     for (uint x = 0; x < width; x++) 
     { 
      offset = y * width + x; 

      if (offset+2 < width * height) 
      { 
       sourceDataPtr = &sourceData[y * width + x]; 

       r = sourceDataPtr[0+0]; 
       g = sourceDataPtr[0+1]; 
       b = sourceDataPtr[0+2]; 

       targetData[y * width + x] = (r+g+b)/3; 
      } 
     } 
    } 

    CGImageRef newImageRef = CGBitmapContextCreateImage(targetImage); 
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; 

    CGContextRelease(targetImage); 
    CGImageRelease(newImageRef); 

호프가 도움이 되었으면

+0

http://asia.olympus-imaging.com/products/dslr/e510/sample/images/sample_03.jpg에서이 작업을 시도했으며 이미지의 맨 아래 부분에 액세스 위반이 있습니다. – tia

+0

흠, 게시 한 이미지로 시도했지만 동일한 오류가 발생했습니다. 마지막 픽셀과 뭔가 다른 것 같습니다 ... 코드를 업데이트 했으므로 이미지 경계를 다 쓸 수 없습니다. – Lukas

관련 문제