Is it possible to make a NSBitmapImageRep with 24 bpp and no alpha channel?

浪子不回头ぞ 提交于 2019-12-18 07:02:46

问题


I don't understand really well the premultiplied alpha.

I need a NSBitmapImageRep without alpha channel (I don't need a particular bpp).

My problem is that this code give me errors:

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.

Okay, I know this combination it's not supported but I need something like this and I don't find a solution.


回答1:


Nope. The formats that Quartz supports are listed here, and like the error message you got said, 24-bit without alpha is not one of them. Since AppKit's drawing APIs are built on top of Quartz, that list applies to AppKit, too.

The best you can do is fill your context with a solid color, such as [NSColor blackColor], before you draw whatever you want to draw. You'll still have an alpha channel in the context, but no actual transparency.




回答2:


To Clarify, NSBitmapImageRep does supports 24 Bits/pixel. However, NSGraphicsContext does not support this format. Apparently an alpha channel is always required by the Quartz drawing system.



来源:https://stackoverflow.com/questions/8704577/is-it-possible-to-make-a-nsbitmapimagerep-with-24-bpp-and-no-alpha-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!