弗洛伊德-斯坦伯格抖动算法
这是一个真实的魔法技术。它愚弄了你的眼睛和大脑,让你以为自己看到的颜色要比实际的多。
一般来说,抖动是通过增加人工噪声去减少一个图像的颜色空间,主旨在于,一个区域的光量应该保持一致。
弗洛伊德-斯坦伯格抖动算法对周围的像素使用非均匀分布的量化误差达到抖动的目的。这就意味着要先将中心像素四舍五入为0或1,而后将残差加入其周围的像素中。
以上你看到的三张图片都是灰阶抖动的,它们全部都是只由两种颜色的噪音组成,而其余的信息,当然是因为你的大脑在转喽。
算法实现
#pragma mark - 分配内存
uint32_t* oldImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
uint32_t* newImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
#pragma mark - 创建context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();// 色彩范围的容器
CGContextRef oldContext = CGBitmapContextCreate(oldImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace,kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextDrawImage(oldContext, CGRectMake(0, 0, imageWidth, imageHeight), self.CGImage);
CGContextRef newContext = CGBitmapContextCreate(newImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace,kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextDrawImage(newContext, CGRectMake(0, 0, imageWidth, imageHeight), self.CGImage);
#pragma mark -遍历像素计算残差
//残差
int eRgb[3];
if (nearColor == 0) {
newptr[3] = 0;
newptr[2] = 0;
newptr[1] = 0;
newptr[0] = 255;
eRgb[0] = r;
eRgb[1] = g;
eRgb[2] = b;
} else {
newptr[3] = 255;
newptr[2] = 255;
newptr[1] = 255;
newptr[0] = 255;
eRgb[0] = r-255;
eRgb[1] = g-255;
eRgb[2] = b-255;
}
//残差 16分之 7、5、3、1
float rate1 = 0.4375;
float rate2 = 0.3125;
float rate3 = 0.1875;
float rate4 = 0.0625;
uint32_t rgb1 = [self getPixel:oldImageBuf width:imageWidth height:imageHeight row:row column:column+1 rate:rate1 eRgb:eRgb];
uint32_t rgb2 = [self getPixel:oldImageBuf width:imageWidth height:imageHeight row:row+1 column:column rate:rate2 eRgb:eRgb];
uint32_t rgb3 = [self getPixel:oldImageBuf width:imageWidth height:imageHeight row:row+1 column:column-1 rate:rate3 eRgb:eRgb];
uint32_t rgb4 = [self getPixel:oldImageBuf width:imageWidth height:imageHeight row:row+1 column:column+1 rate:rate4 eRgb:eRgb];
[self setPixel:oldImageBuf width:imageWidth height:imageHeight row:row column:column+1 value:rgb1];
[self setPixel:oldImageBuf width:imageWidth height:imageHeight row:row+1 column:column value:rgb2];
[self setPixel:oldImageBuf width:imageWidth height:imageHeight row:row+1 column:column-1 value:rgb3];
[self setPixel:oldImageBuf width:imageWidth height:imageHeight row:row+1 column:column+1 value:rgb4];
#pragma mark - 获取像素
- (uint32_t)getPixel:(uint32_t*)imageBuf width:(int)width height:(int)height row:(int)row column:(int)column rate:(float)rate eRgb:(int *)eRgb {
if (row < 0 || row >= height || column < 0 || column >= width) {
return 0xFFFFFFFF;
}
int index = row * width + column;
uint32_t *ptr = imageBuf + index;
uint8_t* newptr = (uint8_t*)ptr;
uint8_t r = newptr[3];
uint8_t g = newptr[2];
uint8_t b = newptr[1];
uint8_t a = newptr[0];
int er = eRgb[0];
int eg = eRgb[1];
int eb = eRgb[2];
r = clamp(r + (int)(rate*er));
g = clamp(g + (int)(rate*eg));
b = clamp(b + (int)(rate*eb));
return (r << 24) + (g << 16) + (b << 8) + a;
}
#pragma mark - 设置像素
- (void)setPixel:(uint32_t*)imageBuf width:(int)width height:(int)height row:(int)row column:(int)column value:(uint32_t)value {
if (row < 0 || row >= height || column < 0 || column >= width) {
return;
}
int index = row * width + column;
uint32_t *ptr = imageBuf + index;
uint8_t* newptr = (uint8_t*)ptr;
int r = (value & 0xFF000000) >> 24;
int g = (value & 0x00FF0000) >> 16;
int b = (value & 0x0000FF00) >> 8;
int a = value & 0x000000FF;
newptr[3] = r;
newptr[2] = g;
newptr[1] = b;
newptr[0] = a;
}
总结
图片抖动算法就是获取当前点的像素与相邻的点的像素比较,并进行相应处理的过程,是一个区域的光量保持一致