如何从 NSBitmapImageRep 创建具有透明度的 8 位 PNG?
我有一个 32 位 NSBitmapImageRep,它有一个基本上为 1 位值的 Alpha 通道(像素要么打开,要么关闭)。
我想将此位图保存为具有透明度的 8 位 PNG 文件。如果我使用 NSBitmapImageRep
的 -representationUsingType:properties:
方法并传入 NSPNGFileType
,则会创建一个 32 位 PNG,这不是我想。
我知道可以读取 8 位 PNG,它们在预览中打开没有任何问题,但是是否可以使用任何内置的 Mac OS X API 写入这种类型的 PNG 文件?如果有必要,我很乐意使用 Core Image 甚至 QuickTime。粗略地检查 CGImage 文档并没有发现任何明显的东西。
编辑: 我已经开始对这个问题进行悬赏,如果有人可以提供使用 32 位 NSBitmapImageRep 并编写具有 1 位透明度的 256 色 PNG 的工作源代码,那么它就是你的。
I have a 32-bit NSBitmapImageRep
which has an alpha channel with essentially 1-bit values (the pixels are either on or off).
I want to save this bitmap to an 8-bit PNG file with transparency. If I use the -representationUsingType:properties:
method of NSBitmapImageRep
and pass in NSPNGFileType
, a 32-bit PNG is created, which is not what I want.
I know that 8-bit PNGs can be read, they open in Preview with no problems, but is it possible to write this type of PNG file using any built-in Mac OS X APIs? I'm happy to drop down to Core Image or even QuickTime if necessary. A cursory examination of the CGImage
docs didn't reveal anything obvious.
EDIT:
I've started a bounty on this question, if someone can provide working source code that takes a 32-bit NSBitmapImageRep
and writes a 256-color PNG with 1-bit transparency, it's yours.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
pnglib
怎么样?它确实非常轻巧且易于使用。How about
pnglib
? It's really lightweight and easy to use.pngnq (以及新的 pngquant ,它可以实现更高的质量),因此您可以将其包含在您的程序中。无需作为单独的任务生成。
pngnq (and new pngquant which achieves higher quality), so you can just include it in your program. No need to spawn as separate task.
使用较低级别 API 的一个很好的参考是 Programming With Quartz
下面的代码基于该书中的示例。
注意:这是未经测试的代码,仅作为起点......
上下文创建函数:
用法为:
A great reference for working with lower level APIs is Programming With Quartz
Some of the code below is based on examples from that book.
Note: This is un-tested code meant to be a starting point only....
Context Creation Function:
Usage would be:
要尝试的一件事是创建一个 8 位的 NSBitmapImageRep,然后将数据复制到其中。这实际上需要大量工作,因为您必须自己计算颜色索引表。
One thing to try would be creating a NSBitmapImageRep with 8 bits, then copying the data to it.This would actually be a lot of work, as you would have to compute the color index table yourself.
CGImageDestination
是您进行低级图像写入的人,但我不知道它是否支持该特定功能。CGImageDestination
is your man for low-level image writing, but I don't know if it supports that specific ability.