2012-09-26 51 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

此代码首先将图像转换为单通道灰度。如果您想要在三通道图像上完成抖动,您可以将图像分成三个通道并调用三次(每个通道一次)。