Iphone CGBitmapContextCreate和interlaced?形象

Iphone CGBitmapContextCreate和interlaced?形象,iphone,objective-c,cocoa,quartz-graphics,Iphone,Objective C,Cocoa,Quartz Graphics,我正在将一些图像绘制代码从Cairo转换为Quartz,我正在慢慢地取得进展,并在学习Quartz的过程中遇到了图像格式的问题 在Cairo版本中,其工作原理如下: unsigned short *d = (unsigned short*)imageSurface->get_data(); int stride = imageSurface->get_stride() >> 1; int height = imageHeight; int width = imageWi

我正在将一些图像绘制代码从Cairo转换为Quartz,我正在慢慢地取得进展,并在学习Quartz的过程中遇到了图像格式的问题

在Cairo版本中,其工作原理如下:

unsigned short *d = (unsigned short*)imageSurface->get_data();
int stride = imageSurface->get_stride() >> 1;

int height = imageHeight;
int width = imageWidth;
do {

    d = *p++; // p = raw image data
    width --;

    if( width == 0 ) {
        height --;
        width = imageWidth;
        d += stride;
    }

} while( height );
现在,这将在Cairo::ImageSurface上生成预期的图像。我已经将此转换为如何使用石英,并且它正在取得进展,但我不确定哪里出了问题:

NSInteger pixelLen = (width * height) * 8;
unsigned char *d = (unsigned char*)malloc(pixelLen);
unsigned char *rawPixels = d;

int height = imageHeight;
int width = imageWidth;
do {

    d = *p++; // p = raw image data
    width --;

    if( width == 0 ) {
        height --;
        width = imageWidth;
    }

} while( height );

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rawPixels, imageWidth, imageHeight, 8, tileSize * sizeof(int), colorSpace, kCGBitmapByteOrderDefault);
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage *resultUIImage = [UIImage imageWithCGImage:image];
CGImageRelease(image);
现在这显然是朝着正确的方向发展,因为它产生了一些看起来有点像所需图像的东西,但它在一行中创建了图像的4个副本,每个都有不同的像素填充,所以我假设这是一个隔行扫描的图像,我对图像格式不太了解,我需要以某种方式组合它们来创建一个完整的图像,但我不知道如何使用石英


我认为步幅与问题有关,但据我所知,这是从一行像素到另一行像素的字节距离,这在石英背景下是不相关的。

听起来步幅与行字节或字节相对应。此值很重要,因为它不一定等于宽度*字节/像素,因为行可能会填充到优化偏移

Cairo代码正在做什么并不完全清楚,而且看起来也不太正确。不管怎样,没有跨步部分,循环都没有意义,因为它是字节的精确副本


Cairo代码中的循环是复制一行字节,然后跳过下一行数据。

Hi,感谢您提供的信息。这开始变得更有意义了。从Cairo文档中可以看出,stride是指从图像数据的一行开始到下一行开始的距离(以字节为单位)。这听起来像你的答案是正确的,它将是从CGBitmapContextGetBytesPerRow返回的值。我已经相应地更新了代码,但它仍然不正确,因为结果图像被挤压到右侧,这让我认为它是8位或16位图像,而不是32位图像?如何创建8位或16位位图?Cairo代码似乎每像素使用2个字节,它一次复制一个无符号的短16位。您的代码每像素仅复制一个字节。。。