CGBitmapContextCreate和隔行扫描?图片

时间:2011-12-20 13:21:35

标签: iphone objective-c cocoa quartz-graphics

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

在开罗版中,它的工作原理如下:

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上按预期生成图像。我把它转换成了如何使用Quartz并且它正在取得进展但是我不确定我哪里出错:

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个图像副本,每个都填充了不同的像素,所以我假设这是一个隔行扫描图像(我不太了解图像格式)并且我需要以某种方式将它们组合起来以创建完整的图像,但我不知道如何使用Quartz。

我认为stride与问题有关,但据我所知,这是从一行像素到另一行的字节距离,这与Quartz的上下文无关?

1 个答案:

答案 0 :(得分:0)

听起来大步对应于rowBytes或bytesPerRow。此值很重要,因为它不一定等于width * bytesPerPixel,因为行可能会填充到优化的偏移量。

开罗代码的作用并不完全明显,而且看起来也不正确。无论哪种方式,没有步幅部分,你的循环都没有意义,因为它可以精确复制字节。

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