Macos 如何创建NSImage的8位、4位和1位表示形式
我用下面的代码创建了32位NSImageMacos 如何创建NSImage的8位、4位和1位表示形式,macos,cocoa,nsimage,nsbitmapimagerep,Macos,Cocoa,Nsimage,Nsbitmapimagerep,我用下面的代码创建了32位NSImage NSBitmapImageRep *sourceRep = [[NSBitmapImageRep alloc] initWithData: imageData]; // create a new bitmap representation scaled down NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc]
NSBitmapImageRep *sourceRep = [[NSBitmapImageRep alloc] initWithData: imageData];
// create a new bitmap representation scaled down
NSBitmapImageRep *newRep =
[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: NULL
pixelsWide: imageSize
pixelsHigh: imageSize
bitsPerSample: 8
samplesPerPixel: 4
hasAlpha: YES
isPlanar: NO
colorSpaceName: NSCalibratedRGBColorSpace
bytesPerRow: 0
bitsPerPixel: 0];
// save the graphics context, create a bitmap context and set it as current
[NSGraphicsContext saveGraphicsState] ;
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep: newRep];
[NSGraphicsContext setCurrentContext: context] ;
// draw the bitmap image representation in it and restore the context
[sourceRep drawInRect: NSMakeRect(0.0f, 0.0f, imageSize, imageSize)] ;
[NSGraphicsContext restoreGraphicsState] ;
// set the size of the new bitmap representation
[newRep setSize: NSMakeSize(imageSize,imageSize)] ;
NSDictionary *imageProps2 = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:1.0], kCGImageDestinationLossyCompressionQuality,
nil];
imageData = [newRep representationUsingType: NSPNGFileType properties: imageProps2];
NSImage *bitImage = [[NSImage alloc]initWithData:imageData];
现在我需要创建8位(256色)、4位(16色)、1位(黑白)
NSBitmapImageRep
表示。我现在想做什么?很遗憾,我相信你不能使用核心图形。图形上下文不支持包含这几个位的任何内容
文档中有一个列表
显然,碳对它有(有?)支持,正如这里提到的,他们也哀叹可可对它缺乏支持:
事实证明,Cocoa/Quartz基本上不支持将图像下采样为8位颜色。它支持绘制它们,支持上采样,但不支持相反的方式。我想这是苹果公司有意设计的,它将索引图像作为一种标准的图形数据类型——毕竟,32位颜色要简单得多,对吗?是的,但是8位仍然有一些有用的用途。所以怎么办?一种可能性是使用Carbon,因为General/QuickDraw的General/GWorld支持下采样等
从这个评论可能太长了 看起来这是不可能的。。。cocoa的所有绘图部件似乎都希望使用24位颜色空间。。。我能够制作一个8比特
NSBitmapImageRep
,但它是灰度的
所以我想我们必须弄清楚为什么会这样。如果您希望能够使用由某些类型的表示支持的NSImages,我认为这是不可能的
如果您想简单地降低采样(更改为与任何像素最接近的24/32位值),这是非常可能的;这将提供8位图像的外观
如果您希望能够以良好的抖动/索引颜色写入这些文件,那么我认为最好的选择是写入支持所需内容的图像格式(如写入256色GIF)
如果出于某种原因,您想自己进行下采样,那么目前有两个问题:
glPixelMapuiv
将索引颜色映射到字节值,然后使用glDrawPixels
显示字节缓冲区
所以。。。我想,如果您发表评论并说明您为什么要尝试这样做,我们可能会提供帮助。不幸的是,Cocoa似乎不支持对调色板图像进行操作 我已经说过了,我的结论是PNG是不可能的
NSGIFFileType
是一个硬编码的例外,图形上下文比位图表示更受欢迎(例如,RGBA仅支持预乘alpha)
要解决此问题,请使用and then或编写PNG文件。您考虑过核心图像过滤器吗?@peterhosey,对许可证或运行时注意事项有任何限制吗?像10.6版的内置库一样必须可用吗?@GradyPlayer:我没有任何这样的限制,没有。(我只是为了假期的赏金问题;我不需要这个工作什么的。)@PeterHosey哇,你真是太好了,假期快乐。@GradyPlayer:谢谢你,祝你节日快乐,也是。胡乱猜测:从32位图标生成旧图标表示,例如
'icl8'
,'icl4'
,和'ICN#'
。