2012-01-02 57 views
3

我不太了解预乘alpha。是否有可能使24 bpp和没有alpha通道的NSBitmapImageRep?

我需要一个没有alpha通道的NSBitmapImageRep(我不需要特定的bpp)。

我的问题是,这个代码给我的错误:

NSSize imageSize = NSMakeSize(200, 200); 

//create a non-alpha RGB image rep with the same dimensions as the image 
    NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc] 
         initWithBitmapDataPlanes:NULL 
         pixelsWide:imageSize.width 
         pixelsHigh:imageSize.height 
         bitsPerSample:8 
         samplesPerPixel:3 
         hasAlpha:NO 
         isPlanar:NO 
         bitmapFormat:NSAlphaNonpremultipliedBitmapFormat 
         bytesPerRow:(3 * imageSize.width) 
         bitsPerPixel:24]; 

//lock focus on the bitmap 

    NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap]; 
    [NSGraphicsContext saveGraphicsState]; 
    [NSGraphicsContext setCurrentContext:context]; 

//draw the image into the bitmap 

[prueba drawAtPoint:NSMakePoint(0, 0) withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSFont systemFontOfSize:24], NSFontAttributeName, nil]]; 

[NSGraphicsContext restoreGraphicsState]; 

//get the TIFF data 
    NSData* tiffData = [bitmap TIFFRepresentation]; 

//do something with TIFF data 
    NSError *error = nil; 
    [tiffData writeToFile:@"/Users/Paul/test.tif" options:NSDataWritingAtomic error:&error]; 

Error: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 640 bytes/row.

好吧,我知道这个组合它不支持,但我需要的是这样的,我没有找到一个解决方案。

+1

您使用'saveGraphicsState'和'restoreGraphicsState'没有意义:您可以保存在一个上下文中,然后在不同的上下文中恢复,而不必先前保存在上下文中。图形状态是每个上下文的,而不是其他方式。 – 2012-01-02 20:36:48

回答

2

没有。 The formats that Quartz supports are listed here,就像你说的错误信息,没有alpha的24位不是其中之一。由于AppKit的绘图API建立在Quartz之上,因此该列表也适用于AppKit。

在绘制任何你想绘制的东西之前,你可以做的最好的事情是用纯色填充你的上下文,比如[NSColor blackColor]。上下文中仍然有一个Alpha通道,但没有实际的透明度。

+0

好吧,我真的需要一个没有alpha通道的tiff图像,这很重要,因为我使用了一个OCR引擎“_tesseract_”,它需要一个没有alpha通道的tiff图像。 NSImage,NSBitmapImageRep等。他们都使用alpha通道。那么,请给我任何解决方案? – PabloLerma 2012-01-03 11:31:18

+0

@PabloLerma:使用Quartz/AppKit以外的东西生成图像,或使用外部工具将图像转换为所需的格式。 – 2012-01-03 16:35:39

+0

有什么想法?我正在寻找** Magick ++ **,但我不知道其他工具 – PabloLerma 2012-01-03 20:37:17

0

要说明,NSBitmapImageRep支持24位/像素。但是,NSGraphicsContext不支持这种格式。显然,Quartz绘图系统总是需要一个alpha通道。

+0

但是,如果NSBitmapImageRep支持24位/像素,为什么我会收到错误? – PabloLerma 2012-01-02 23:01:33

+0

我猜NSBitmapImageRep支持24位/像素,但NSGraphicsContext不支持它(如错误消息所述)。 – 2012-01-02 23:29:48