2012-09-26 60 views
2

我正在編寫圖像編輯程序,並且我需要抖動任意24位RGB圖像的功能(我已經着手使用CoreGraphics等等)轉換成3位色彩通道的圖像,然後顯示出來。我建立了我的矩陣等,但我還沒有接到下面的代碼,除了一個簡單的模式被應用到圖像的任何結果:實現有序抖動(每個通道RGB 24位RGB到3位)

- (CGImageRef) ditherImageTo16Colours:(CGImageRef)image withDitheringMatrixType:(SQUBayerDitheringMatrix) matrix { 
    if(image == NULL) { 
     NSLog(@"Image is NULL!"); 
     return NULL; 
    } 

    unsigned int imageWidth = CGImageGetWidth(image); 
    unsigned int imageHeight = CGImageGetHeight(image); 

    NSLog(@"Image size: %u x %u", imageWidth, imageHeight); 

    CGContextRef context = CGBitmapContextCreate(NULL, 
               imageWidth, 
               imageHeight, 
               8, 
               4 * (imageWidth), 
               CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), 
               kCGImageAlphaNoneSkipLast); 

    CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image); // draw it 
    CGImageRelease(image); // get rid of the image, we don't want it anymore. 


    unsigned char *imageData = CGBitmapContextGetData(context); 

    unsigned char ditheringModulusType[0x04] = {0x02, 0x03, 0x04, 0x08}; 
    unsigned char ditheringModulus = ditheringModulusType[matrix]; 

    unsigned int red; 
    unsigned int green; 
    unsigned int blue; 

    uint32_t *memoryBuffer; 
    memoryBuffer = (uint32_t *) malloc((imageHeight * imageWidth) * 4); 

    unsigned int thresholds[0x03] = {256/8, 256/8, 256/8}; 

    for(int y = 0; y < imageHeight; y++) { 
     for(int x = 0; x < imageWidth; x++) { 
      // fetch the colour components, add the dither value to them 
      red = (imageData[((y * imageWidth) * 4) + (x << 0x02)]); 
      green = (imageData[((y * imageWidth) * 4) + (x << 0x02) + 1]); 
      blue = (imageData[((y * imageWidth) * 4) + (x << 0x02) + 2]); 

      if(red > 36 && red < 238) { 
       red += SQUBayer117_matrix[x % ditheringModulus][y % ditheringModulus]; 
      } if(green > 36 && green < 238) { 
       green += SQUBayer117_matrix[x % ditheringModulus][y % ditheringModulus]; 
      } if(blue > 36 && blue < 238) { 
       blue += SQUBayer117_matrix[x % ditheringModulus][y % ditheringModulus]; 
      } 

//   memoryBuffer[(y * imageWidth) + x] = (0xFF0000 + ((x >> 0x1) << 0x08) + (y >> 2)); 
      memoryBuffer[(y * imageWidth) + x] = find_closest_palette_colour(((red & 0xFF) << 0x10) | ((green & 0xFF) << 0x08) | (blue & 0xFF)); 
     } 
    } 

    //CGContextRelease(context); 
    context = CGBitmapContextCreate(memoryBuffer, 
            imageWidth, 
            imageHeight, 
            8, 
            4 * (imageWidth), 
            CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), 
            kCGImageAlphaNoneSkipLast); 

    NSLog(@"Created context from buffer: %@", context); 

    CGImageRef result = CGBitmapContextCreateImage(context); 
    return result; 
} 

注意find_closest_palette_colour不會做任何事情,除了返回原來的顏色現在進行測試。

我試圖從維基百科實現僞代碼示例,我現在沒有真正得到任何東西。

任何人都知道如何解決這個問題?

回答

0

使用,我這裏提供的代碼:https://stackoverflow.com/a/17900812/342646

此代碼首先將圖像轉換爲單通道灰度。如果您想要在三通道圖像上完成抖動,您可以將圖像分成三個通道並調用三次(每個通道一次)。