应用 CIFilter 会破坏 .BMP 文件中的数据
我似乎正陷入困境,试图了解在 OSX 的 Cocoa 应用程序中表示图像的所有不同方式。
我的应用程序读取图像,对其应用 CIFilters,然后保存输出。直到今天早上,这对于我扔给它的所有图像都运行良好。但是,我发现一些 BMP 文件在我尝试对它们应用任何 CIFilter 时会生成空的透明图像。
其中一张图片是《龙腾世纪 2》加载程序的广告之一(今天早上我刚刚在随机图像上测试我的应用程序); http://www.johnwordsworth.com/wp-content/uploads /2011/08/hires_en.bmp
具体来说,我的代码执行以下操作。
- 使用 imageWithCGImage 加载 CIImage(initWithContentsOfURL 也会出现同样的问题)。
- 将多个 CIFilter 应用于 CIImage,同时将当前图像存储在我的 AIImage 容器类中。
- 通过将 NSCIImageRep 添加到 NSImage 来预览图像。
- 使用 NSBitmapImageRep / initWithCIImage 和representationUsingType 保存图像。
这个过程适用于我扔给它的 99% 的文件(到目前为止所有 JPG、PNG、TIFF),只是不适用于某些 BMP 文件。如果我跳过第 2 步,预览和保存的图像将正常显示。但是,如果我打开步骤 2,生成的图像始终是空白且透明的。
代码相当大,但以下是我认为相关的片段...
AIImage 加载方法
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:imagePath], nil);
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, nil);
CFDictionaryRef dictionaryRef = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil);
self.imageProperties = [NSMutableDictionary dictionaryWithDictionary:((NSDictionary *)dictionaryRef)];
self.imageData = [CIImage imageWithCGImage:imageRef];
AIImageResize 方法
NSAffineTransform *transform = [NSAffineTransform transform];
[transform scaleXBy:(targetSize.width / sourceRect.size.width) yBy:(targetSize.height / sourceRect.size.height)];
CIFilter *transformFilter = [CIFilter filterWithName:@"CIAffineTransform"];
[transformFilter setValue:transform forKey:@"inputTransform"];
[transformFilter setValue:currentImage forKey:@"inputImage"];
currentImage = [transformFilter valueForKey:@"outputImage"];
aiImage.imageData = currentImage;
CIImagePreview 方法
NSCIImageRep *imageRep = [[NSCIImageRep alloc] initWithCIImage:ciImage];
NSImage *nsImage = [[[NSImage alloc] initWithSize:ciImage.extent.size] autorelease];
[nsImage addRepresentation:imageRep];
感谢您的查看。任何建议将不胜感激。
I seem to be tying myself up in knots trying to read into all of the different ways you can represent images in a Cocoa app for OSX.
My app reads in an image, applies CIFilters to it and then saves the output. Until this morning, this worked fine for all of the images that I've thrown at it. However, I've found some BMP files that produce empty, transparent images as soon as I try to apply any CIFilter to them.
One such image is one of the adverts from the Dragon Age 2 loader (I was just testing my app on random images this morning); http://www.johnwordsworth.com/wp-content/uploads/2011/08/hires_en.bmp
Specifically, my code does the following.
- Load a CIImage using imageWithCGImage (the same problem occurs with initWithContentsOfURL).
- Apply a number of CIFilters to the CIImage, all the while storing the current image in my AIImage container class.
- Previews the image by adding an NSCIImageRep to an NSImage.
- Saves the image using NSBitmapImageRep / initWithCIImage and then representationUsingType.
This process works with 99% of the files I've thrown at it (all JPGs, PNGs, TIFFs so far), just not with certain BMP files. If I skip step 2, the preview and saved image come out OK. However, if I turn step 2 on, the image produced is always blank and transparent.
The code is quite large, but here are what I believe to be the relevant snippets...
AIImage Loading Method
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:imagePath], nil);
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, nil);
CFDictionaryRef dictionaryRef = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil);
self.imageProperties = [NSMutableDictionary dictionaryWithDictionary:((NSDictionary *)dictionaryRef)];
self.imageData = [CIImage imageWithCGImage:imageRef];
AIImageResize Method
NSAffineTransform *transform = [NSAffineTransform transform];
[transform scaleXBy:(targetSize.width / sourceRect.size.width) yBy:(targetSize.height / sourceRect.size.height)];
CIFilter *transformFilter = [CIFilter filterWithName:@"CIAffineTransform"];
[transformFilter setValue:transform forKey:@"inputTransform"];
[transformFilter setValue:currentImage forKey:@"inputImage"];
currentImage = [transformFilter valueForKey:@"outputImage"];
aiImage.imageData = currentImage;
CIImagePreview Method
NSCIImageRep *imageRep = [[NSCIImageRep alloc] initWithCIImage:ciImage];
NSImage *nsImage = [[[NSImage alloc] initWithSize:ciImage.extent.size] autorelease];
[nsImage addRepresentation:imageRep];
Thanks for looking. Any advice would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论