如何为 alpha 蒙版图像创建位图上下文?

发布于 2024-11-16 07:23:21 字数 1890 浏览 6 评论 0原文

我想在代码中绘制 alpha 蒙版图像。现在我这样做:

1) 使用带有选项 CGColorSpaceCreateDeviceRGB 和 kCGImageAlphaPremultipliedFirst 的 CGBitmapContextCreate 来创建位图上下文。

2)然后我在这个上下文中绘制,仅使用白色和黑色等灰度颜色。

3) 然后,我使用 CGImageMaskCreate 从该上下文创建一个遮罩图像。

结论:我浪费了很多内存!因为根据我的理解,掩模图像只是灰度,对吧?那么为什么首先要在 ARGB 中创建上下文呢?

如何创建用于绘制蒙版图像的 CGContextRef?我的想法是使用 CGColorSpaceCreateDeviceGray ,但问题就开始了。这是我创建 ARGB 位图上下文的确切代码:

CGContextRef    context = NULL;
CGColorSpaceRef colorSpace;
uint32_t *      bitmapData;

int imageWidth = round(size.width);
int imageHeight = round(size.height);

int bitmapBytesPerRow = (imageWidth * 4);
int bitmapByteCount = (bitmapBytesPerRow * imageHeight);

colorSpace = CGColorSpaceCreateDeviceRGB();

bitmapData = malloc(bitmapByteCount);

context = CGBitmapContextCreate(bitmapData,
                                imageWidth,
                                imageHeight,
                                8,  // bits per component
                                bitmapBytesPerRow,
                                colorSpace,
                                kCGImageAlphaPremultipliedFirst);

CGColorSpaceRelease(colorSpace);

我不确定如何计算此类上下文的 bitmapBytesPerRow 。我认为它只是imageWidth?我必须为 CGBitmapContextCreate 中的每个组件的位数提供什么?

CGColorSpaceGetNumberOfComponents() 但它仅报告组件的数量。这并没有告诉我一个组件有多少字节。

另外让我紧张的是 48 是硬编码在我上面的代码中的。谁说每个组件总是 4 字节,谁说每个组件总是 8 位?我只是从那里的各种示例代码中获取了这个。每个人似乎都这样做。有用。但未来的证明?可能不会。

你会给我一些很棒的答案,让我开心。谢谢。


编辑:我找到了一个代码片段,但它很令人困惑:

CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceGray();
CGContextRef gradientBitmapContext = CGBitmapContextCreate (NULL, 1, reflectRect.size.height,8, 0, colorSpace2, kCGImageAlphaNone);

为什么每行字节数为 0?文档没有说你可以传递 0。看起来不对。

I want to draw an alpha mask image in code. Right now I do:

1) Create a bitmap context using CGBitmapContextCreate with options CGColorSpaceCreateDeviceRGB and kCGImageAlphaPremultipliedFirst.

2) Then I draw into this context, using only grayscale colors like white and black.

3) Then I create a mask image from that context, using CGImageMaskCreate.

Conclusion: I waste a lot of memory! Because from my understanding, a mask image is grayscale only, right? So why create a context in ARGB in the first place.

How can I create a CGContextRef that is intended to be used for drawing a mask image? My thoughts are to use CGColorSpaceCreateDeviceGray, but here the problems start. This is the exact code how I create my ARGB bitmap context:

CGContextRef    context = NULL;
CGColorSpaceRef colorSpace;
uint32_t *      bitmapData;

int imageWidth = round(size.width);
int imageHeight = round(size.height);

int bitmapBytesPerRow = (imageWidth * 4);
int bitmapByteCount = (bitmapBytesPerRow * imageHeight);

colorSpace = CGColorSpaceCreateDeviceRGB();

bitmapData = malloc(bitmapByteCount);

context = CGBitmapContextCreate(bitmapData,
                                imageWidth,
                                imageHeight,
                                8,  // bits per component
                                bitmapBytesPerRow,
                                colorSpace,
                                kCGImageAlphaPremultipliedFirst);

CGColorSpaceRelease(colorSpace);

I am not sure how to compute the bitmapBytesPerRow for such a context. I assume it would be just imageWidth? And what must I supply for bits per component in CGBitmapContextCreate?

There is CGColorSpaceGetNumberOfComponents() but it reports only the number of components. This does not tell me how many bytes a component has.

Also what makes me nervous is that the 4 and 8 are hard-coded in my code above. Who says it's always 4 bytes per component, and who says it's 8 bits per component? I just took this from various sample codes out there. Everyone seems to do it this way. It works. But future proof? Probably not.

You would make my day with some great answers. Thanks.


Edit: I found a code-snippet, but it is confusing:

CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceGray();
CGContextRef gradientBitmapContext = CGBitmapContextCreate (NULL, 1, reflectRect.size.height,8, 0, colorSpace2, kCGImageAlphaNone);

Why 0 for bytes per row? The documentation does not say you can pass 0. Looks wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昇り龍 2024-11-23 07:23:21

这些参数告诉系统如何处理您提供的数据或内存。您自己创建了它,因此您知道自己想要什么布局。系统可能想在幕后用它做什么(如果有的话)并不是您眼前的问题。

在这种情况下,您将为每个样本提供 8 位,仅包含 1 个分量,并且可能不想使用任何行填充,在这种情况下,您的 bytesPerRow 确实应该与图像宽度相同。

Those parameters are telling the system how to treat the data or memory you supply. You have created that yourself, so you know what layout you intend. What, if anything, the system might want to do with it behind the scenes is not your immediate problem.

In this case, you'll provide 8 bits per sample, with just the 1 component, and probably not want to use any row padding, in which case your bytesPerRow should indeed be the same as the image width.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文