Iphone 奇怪的图像生成问题
从iphone上的原始数据创建CGImageRef时,我遇到了一个非常奇怪的问题。我试图从一个perlin噪波生成函数中创建一个CGImageRef,该函数取x、y、z和t,并返回一个介于-1和1之间的值。对于每个像素,我得到介于-1和1之间的噪波值,将其转换为介于0和255之间的字符,并按照以下方式从数据中创建CGImageRefIphone 奇怪的图像生成问题,iphone,core-graphics,cgimage,perlin-noise,Iphone,Core Graphics,Cgimage,Perlin Noise,从iphone上的原始数据创建CGImageRef时,我遇到了一个非常奇怪的问题。我试图从一个perlin噪波生成函数中创建一个CGImageRef,该函数取x、y、z和t,并返回一个介于-1和1之间的值。对于每个像素,我得到介于-1和1之间的噪波值,将其转换为介于0和255之间的字符,并按照以下方式从数据中创建CGImageRef int width = (int)frame.size.width; int height = (int)frame.size.height; char* rgba
int width = (int)frame.size.width;
int height = (int)frame.size.height;
char* rgba = (char*)malloc(width * height * 4);
//set pixel data
int i = 0;
for(float x = 0.0; x < width; x+=1.0) {
for (float y = 0.0; y < height; y+=1.0) {
float perlinF = ABS([perlinGenerator perlinNoiseX:x y:y z:0.0 t:0.0]);
char perlin = (char)( perlinF * 255.0 );
rgba[4*i] = perlin;
rgba[4*i+1] = perlin;
rgba[4*i+2] = perlin;
rgba[4*i+3] = 255;
i++;
}
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(
rgba,
width,
height,
8, // bitsPerComponent
4*width, // bytesPerRow
colorSpace,
kCGImageAlphaNoneSkipLast);
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
int-width=(int)frame.size.width;
int height=(int)frame.size.height;
char*rgba=(char*)malloc(宽*高*4);
//设置像素数据
int i=0;
对于(浮动x=0.0;x
如果我将这个CGImageRef绘制到CALayer,我会得到以下结果
水平伪影是由我的屏幕截图(不是在实际图像中)创建的,但是将图像分割为3个相同部分的2条垂直线是问题所在!是什么原因导致图像重复3次,并出现这两个垂直不连续?!更奇怪的是,如果我在不创建CGImageRef的情况下手动渲染柏林噪波,就像这样
for (CGFloat x = 0.0; x<size.width; x+=1.0) {
for (CGFloat y=0.0; y< size.height; y+=1.0) {
float perlinF = ABS([perlinGenerator perlinNoiseX:x y:y z:0.0 t:0.0]);
char perlin = (char)( perlinF * 255.0 );
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, (float)perlin / 255.0);
CGContextFillRect(context, CGRectMake(x, y, 1.0, 1.0));
}
}
for(CGFloat x=0.0;x由于CGBitmapContextCreate假定图像缓冲区是按行而不是按列索引的,因此需要将x和y交换为循环
int i = 0;
for (float y = 0.0; y < height; y+=1.0) {
for(float x = 0.0; x < width; x+=1.0) {
float perlinF = ABS([perlinGenerator perlinNoiseX:x y:y z:0.0 t:0.0]);
char perlin = (char)( perlinF * 255.0 );
rgba[4*i] = perlin;
rgba[4*i+1] = perlin;
rgba[4*i+2] = perlin;
rgba[4*i+3] = 255;
i++;
}
}
inti=0;
对于(浮动y=0.0;y<高度;y+=1.0){
对于(浮动x=0.0;x
你说得对!简单的错误…谢谢你的注意!因为其他渲染是逐像素进行的,所以for循环的顺序并不重要。太棒了