我正在将一些图像绘制代码从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上生成预期的图像。我已经将其转换为如何使用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的上下文中是不相关的?
发布于 2011-12-20 22:53:52
听起来stride相当于rowBytes或bytesPerRow。这个值很重要,因为它不一定等于width * bytesPerPixel,因为可能会将行填充到优化的偏移量。
Cairo代码所做的事情并不完全清楚,而且看起来也不太正确。无论哪种方式,如果没有步长部分,循环就没有意义了,因为它精确地复制了字节。
Cairo代码中的循环复制一行字节,然后跳过下一行数据。
https://stackoverflow.com/questions/8576120
复制相似问题