使用 Cocoa 的 Photoshop 插件 - 如何获取预览

发布于 2024-07-27 12:36:56 字数 1047 浏览 7 评论 0原文

我正在 Mac 上实现一个 Photoshop 插件,并且使用 Cocoa 来实现。 到目前为止一切正常,但下一步是提供“预览”图像,作为我的插件窗口的一部分,但我陷入了困境。 我是一个 n00b Obj-C 程序员,这并没有真正帮助:-)

到目前为止我已经得到了这样的东西:

int dataSize = gFilterRecord->bigDocumentData->wholeSize32.v *
               gFilterRecord->bigDocumentData->wholeSize32.h *
               gFilterRecord->planes; 

NSData *inData = [[NSData alloc] initWithBytesNoCopy:gFilterRecord->inData length:dataSize freeWhenDone:NO];
NSLog(@"LoadImageFromSDK : Data created");
NSImage *imageTmp = [[NSImage alloc] initWithData:inData];
NSLog(@"LoadImageFromSDK : Image created");

//Save to PNG file as a test of this image creation
[[imageTmp TIFFRepresentation] writeToFile:@"/tmp/imageTmp.tif" atomically:YES];
NSLog(@"LoadImageFromSDK : Wrote image to disk");

目前,它严重崩溃了:
09/07/22 10:23:32 AM Adob​​e Photoshop Elements[46628] *** NSCopyMemoryPages(0x0, 0x245f4000, 2265088) 失败

我可能错误地计算了 inData 的大小。 帮助?

另外, NSImage 是否能够正确解释该图像数据块? 或者我应该放弃它,只对 NSImage 进行逐像素映射?

I'm implementing a Photoshop plugin on the Mac, and I'm doing it using Cocoa. Doing ok so far, but the next step is to provide a "preview" image, as a part of my plugin window, and I'm stuck. I'm a n00b Obj-C programmer, which isn't really helping :-)

So far I've got something like this:

int dataSize = gFilterRecord->bigDocumentData->wholeSize32.v *
               gFilterRecord->bigDocumentData->wholeSize32.h *
               gFilterRecord->planes; 

NSData *inData = [[NSData alloc] initWithBytesNoCopy:gFilterRecord->inData length:dataSize freeWhenDone:NO];
NSLog(@"LoadImageFromSDK : Data created");
NSImage *imageTmp = [[NSImage alloc] initWithData:inData];
NSLog(@"LoadImageFromSDK : Image created");

//Save to PNG file as a test of this image creation
[[imageTmp TIFFRepresentation] writeToFile:@"/tmp/imageTmp.tif" atomically:YES];
NSLog(@"LoadImageFromSDK : Wrote image to disk");

At the moment, it crashes horribly on:
09/07/22 10:23:32 AM Adobe Photoshop Elements[46628] *** NSCopyMemoryPages(0x0, 0x245f4000, 2265088) failed

I'm probably calculating the size of inData incorrectly. Help?

Also, is NSImage going to be able to interpret that image data blob correctly? Or should I give it up and just do a pixel-by-pixel mapping into the NSImage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情仇皆在手 2024-08-03 12:36:56

好吧,弄清楚这一点比我预想的要痛苦得多。 另外,感谢 NVidia 发布此 PDF,这是对 Photoshop SDK 过滤器记录的解释比实际的 SDK 文档更好。

除了用于调试目的的示例 tif 文件之外,此代码实际上读取 inData 并生成一个可用的 NSImage(大量日志记录只是为了弄清楚它在做什么,请随意删除)。

NSLog(@"Entering LoadImageFromSDK");

unsigned char *bitmapPlanes[4];    
bitmapPlanes[0] = (unsigned char *) (gFilterRecord->inData);

NSLog(@"Params to create bitmap");
NSLog(@"pixelsWide = %d", gFilterRecord->bigDocumentData->imageSize32.h );
NSLog(@"pixelsHigh = %d", gFilterRecord->bigDocumentData->imageSize32.v );
NSLog(@"bitsPerSample = %d", gFilterRecord->depth );
NSLog(@"samplesPerPixel = %d", gFilterRecord->planes );
NSLog(@"hasAlpha = %d", NO );
NSLog(@"isPlanar = %d", NO );
NSLog(@"colorSpaceName = %@", mapImageModeToColorSpace(gFilterRecord->imageMode) );
NSLog(@"bytesPerRow = %d", gFilterRecord->inRowBytes );
NSLog(@"bitsPerPixel = %d", gFilterRecord->depth*gFilterRecord->planes );    

NSBitmapImageRep *bitmapTmp = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:bitmapPlanes
                                                                      pixelsWide:gFilterRecord->bigDocumentData->imageSize32.h
                                                                      pixelsHigh:gFilterRecord->bigDocumentData->imageSize32.v
                                                                   bitsPerSample:gFilterRecord->depth
                                                                 samplesPerPixel:gFilterRecord->planes
                                                                        hasAlpha:NO 
                                                                        isPlanar:NO 
                                                                  colorSpaceName:mapImageModeToColorSpace(gFilterRecord->imageMode) 
                                                                     bytesPerRow:gFilterRecord->inRowBytes
                                                                    bitsPerPixel:gFilterRecord->depth*gFilterRecord->planes];

NSLog(@"LoadImageFromSDK : Bitmap created = %@", bitmapTmp);

[[bitmapTmp TIFFRepresentation] writeToFile:@"/Users/someuser/temp/sample.tif" atomically:YES];


NSImage *imageTmp = [[NSImage alloc] initWithSize:[bitmapTmp size]];
[imageTmp addRepresentation:bitmapTmp];
NSLog(@"LoadImageFromSDK : Image created = %@", imageTmp);

Ok, that was way more painful to figure out than I had anticipated. Also, kudos to NVidia for posting this PDF which is a better explanation of the Photoshop SDK filter record than the actual SDK docs.

This code actually reads the inData and produces a usable NSImage (lots of logging just to figure out what it is doing, feel free to remove) in addition to the sample tif file for debugging purposes.

NSLog(@"Entering LoadImageFromSDK");

unsigned char *bitmapPlanes[4];    
bitmapPlanes[0] = (unsigned char *) (gFilterRecord->inData);

NSLog(@"Params to create bitmap");
NSLog(@"pixelsWide = %d", gFilterRecord->bigDocumentData->imageSize32.h );
NSLog(@"pixelsHigh = %d", gFilterRecord->bigDocumentData->imageSize32.v );
NSLog(@"bitsPerSample = %d", gFilterRecord->depth );
NSLog(@"samplesPerPixel = %d", gFilterRecord->planes );
NSLog(@"hasAlpha = %d", NO );
NSLog(@"isPlanar = %d", NO );
NSLog(@"colorSpaceName = %@", mapImageModeToColorSpace(gFilterRecord->imageMode) );
NSLog(@"bytesPerRow = %d", gFilterRecord->inRowBytes );
NSLog(@"bitsPerPixel = %d", gFilterRecord->depth*gFilterRecord->planes );    

NSBitmapImageRep *bitmapTmp = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:bitmapPlanes
                                                                      pixelsWide:gFilterRecord->bigDocumentData->imageSize32.h
                                                                      pixelsHigh:gFilterRecord->bigDocumentData->imageSize32.v
                                                                   bitsPerSample:gFilterRecord->depth
                                                                 samplesPerPixel:gFilterRecord->planes
                                                                        hasAlpha:NO 
                                                                        isPlanar:NO 
                                                                  colorSpaceName:mapImageModeToColorSpace(gFilterRecord->imageMode) 
                                                                     bytesPerRow:gFilterRecord->inRowBytes
                                                                    bitsPerPixel:gFilterRecord->depth*gFilterRecord->planes];

NSLog(@"LoadImageFromSDK : Bitmap created = %@", bitmapTmp);

[[bitmapTmp TIFFRepresentation] writeToFile:@"/Users/someuser/temp/sample.tif" atomically:YES];


NSImage *imageTmp = [[NSImage alloc] initWithSize:[bitmapTmp size]];
[imageTmp addRepresentation:bitmapTmp];
NSLog(@"LoadImageFromSDK : Image created = %@", imageTmp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文