大家好!这是我在这里的第一篇文章。
我在objective-c方面很新,所以也许我的问题并不难,但这对我来说是个问题。我在网上搜索并没有找到任何对我有用的提示......我正在 Xcode 中用 Objective-c 编写一个程序。我需要读取并显示一个pgm文件(P5,每个像素两个字节)。为此,我正在尝试子类化NSImageRep
,但我不知道如何为该文件创建正确的位图以及如何绘制它。到目前为止,以下是我的代码:
标题:
@interface CTPWTPGMImageRep : NSImageRep
@property (readonly)NSInteger width;
@property (readonly)NSInteger height;
@property (readonly)NSInteger maxValue;
+ (void)load;
+ (NSArray *)imageUnfilteredTypes;
+ (NSArray *)imageUnfilteredFileTypes;
+ (BOOL)canInitWithData:(NSData *)data;
+ (id)imageRepWithContentsOfFile:(NSString*)file;
+ (id)imageRepWithData:(NSData*)pgmData;
- (id)initWithData:(NSData *)data;
- (BOOL)draw;
@end
和实施:
#import "CTPWTPGMImageRep.h"
@implementation CTPWTPGMImageRep
@synthesize width;
@synthesize height;
@synthesize maxValue;
#pragma mark - class methods
+(void) load
{
NSLog(@"Called 'load' method for CTPWTPGMImageRep");
[NSImageRep registerImageRepClass:[CTPWTPGMImageRep class]];
}
+ (NSArray *)imageUnfilteredTypes
{
// This is a UTI
NSLog(@"imageUnfilteredTypes called");
static NSArray *types = nil;
if (!types) {
types = [[NSArray alloc] initWithObjects:@"public.unix-executable", @"public.data", @"public.item", @"public.executable", nil];
}
return types;
}
+ (NSArray *)imageUnfilteredFileTypes
{
// This is a filename suffix
NSLog(@"imageUnfilteredFileTypes called");
static NSArray *types = nil;
if (!types)
types = [[NSArray alloc] initWithObjects:@"pgm", @"PGM", nil];
return types;
}
+ (BOOL)canInitWithData:(NSData *)data;
{
// FIX IT
NSLog(@"canInitWithData called");
if ([data length] >= 2) // First two bytes for magic number magic number
{
NSString *magicNumber = @"P5";
const unsigned char *mNum = (const unsigned char *)[magicNumber UTF8String];
unsigned char aBuffer[2];
[data getBytes:aBuffer length:2];
if(memcmp(mNum, aBuffer, 2) == 0)
{
NSLog(@"canInitWithData: YES");
return YES;
}
}
NSLog(@"canInitWithData: NO");
// end
return NO;
}
+ (id)imageRepWithContentsOfFile:(NSString*)file {
NSLog(@"imageRepWithContentsOfFile called");
NSData* data = [NSData dataWithContentsOfFile:file];
if (data)
return [CTPWTPGMImageRep imageRepWithData:data];
return nil;
}
+ (id)imageRepWithData:(NSData*)pgmData {
NSLog(@"imageRepWithData called");
return [[self alloc] initWithData:pgmData];
}
#pragma mark - instance methods
- (id)initWithData:(NSData *)data;
{
NSLog(@"initWithData called");
self = [super init];
if (!self)
{
return nil;
}
if ([data length] >= 2) {
NSString *magicNumberP5 = @"P5";
const unsigned char *mnP5 = (const unsigned char *)[magicNumberP5 UTF8String];
unsigned char headerBuffer[20];
[data getBytes:headerBuffer length:2];
if(memcmp(mnP5, headerBuffer, 2) == 0)
{
NSArray *pgmParameters = [self calculatePgmParameters:data beginingByte:3];
width = [[pgmParameters objectAtIndex:0] integerValue];
height = [[pgmParameters objectAtIndex:1] integerValue];
maxValue = [[pgmParameters objectAtIndex:2] integerValue];
if (width <= 0 || height <= 0)
{
NSLog(@"Invalid image size: Both width and height must be > 0");
return nil;
}
[self setPixelsWide:width];
[self setPixelsHigh:height];
[self setSize:NSMakeSize(width, height)];
[self setColorSpaceName:NSDeviceWhiteColorSpace];
[self setBitsPerSample:16];
[self setAlpha:NO];
[self setOpaque:NO];
//What to do here?
//CTPWTPGMImageRep *imageRep =
[NSBitmapImageRep alloc] initWithBitmapDataPlanes:];
//if (imageRep) {
/* code to populate the pixel map */
//}
}
else
{
NSLog(@"It is not supported pgm file format.");
}
}
return self;
//return imageRep;
}
- (BOOL)draw
{
NSLog(@"draw method1 called");
return NO;
}
有趣的是我的canInitWithData:
方法从未被调用过。你能给我一个提示如何智能读取位图吗?我认为我需要使用initWithBitmapDataPlanes:pixelsWide:pixelsHigh:bitsPerSample:samplesPerPixel:hasAlpha:isPlanar:colorSpaceName:bytesPerRow:bitsPerPixel:
但我不知道如何使用它。如何从我的NSData
对象创建智能创建(无符号字符**)平面?我需要吗?
我可以使用NSDeviceWhiteColorSpace
具有白色和 alpha 成分的吗(我不需要 alpha)
[self setColorSpaceName:NSDeviceWhiteColorSpace];
最困难的部分 - 我完全不知道如何实现绘图方法。有什么建议或提示吗?
预先感谢您的帮助。
编辑:
好的。现在我根据 NSGod 的指示实现了:
@implementation CTPWTPGMImageRep
//@synthesize width;
//@synthesize height;
#pragma mark - class methods
+(void) load
{
NSLog(@"Called 'load' method for CTPWTPGMImageRep");
[NSBitmapImageRep registerImageRepClass:[CTPWTPGMImageRep class]];
}
+ (NSArray *)imageUnfilteredTypes
{
// This is a UTI
NSLog(@"imageUnfilteredTypes called");
static NSArray *types = nil;
if (!types) {
types = [[NSArray alloc] initWithObjects:@"public.unix-executable", @"public.data", @"public.item", @"public.executable", nil];
}
return types;
}
+ (NSArray *)imageUnfilteredFileTypes
{
// This is a filename suffix
NSLog(@"imageUnfilteredFileTypes called");
static NSArray *types = nil;
if (!types)
types = [[NSArray alloc] initWithObjects:@"pgm", nil];
return types;
}
+ (NSArray *)imageRepsWithData:(NSData *)data {
NSLog(@"imageRepsWithData called");
id imageRep = [[self class] imageRepWithData:data];
return [NSArray arrayWithObject:imageRep];
}
- (id)initWithData:(NSData *)data {
NSLog(@"initWithData called");
CTPWTPGMImageRep *imageRep = [[self class] imageRepWithData:data];
if (imageRep == nil) {
return nil;
}
return self;
}
#pragma mark - instance methods
+ (id)imageRepWithData:(NSData *)data {
NSLog(@"imageRepWithData called");
if (data.length < 2) return nil;
NSString *magicNumberP5 = @"P5";
const unsigned char *mnP5 = (const unsigned char *)[magicNumberP5 UTF8String];
unsigned char headerBuffer[2];
[data getBytes:headerBuffer length:2];
if (memcmp(mnP5, headerBuffer, 2) != 0) {
NSLog(@"It is not supported pgm file format.");
return nil;
}
NSArray *pgmParameters = [self calculatePgmParameters:data beginingByte:3];
NSInteger width = [[pgmParameters objectAtIndex:0] integerValue]; // width in pixels
NSInteger height = [[pgmParameters objectAtIndex:1] integerValue]; // height in pixels
NSUInteger imageLength = width * height * 2; // two bytes per pixel
// imageData contains bytes of Bitmap only. Without header
NSData *imageData = [data subdataWithRange:
NSMakeRange(data.length - imageLength, imageLength)];
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)CFBridgingRetain(imageData));
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray); // kCGColorSpaceGenericGrayGamma2_2
CGImageRef imageRef = CGImageCreate(width,
height,
16,
16,
width * 2,
colorSpace,
kCGImageAlphaNone,
provider,
NULL,
false,
kCGRenderingIntentDefault);
CGColorSpaceRelease(colorSpace);
CGDataProviderRelease(provider);
if (imageRef == NULL) {
NSLog(@"CGImageCreate() failed!");
}
CTPWTPGMImageRep *imageRep = [[CTPWTPGMImageRep alloc] initWithCGImage:imageRef];
return imageRep;
}
正如你所看到的,我留下了扩展值在 0-255 之间的部分,因为我的像素值在 0-65535 之间。
但它不起作用。当我从面板中选择一个 pgm 文件时,什么也没有发生。贝娄是我的 openPanel 代码:
- (IBAction)showOpenPanel:(id)sender
{
NSLog(@"showPanel method called");
__block NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setAllowedFileTypes:[NSImage imageFileTypes]];
[panel beginSheetModalForWindow:[pgmImageView window] completionHandler:^ (NSInteger result) {
if (result == NSOKButton) {
CTPWTPGMImageRep *pgmImage = [[CTPWTPGMImageRep alloc] initWithData:[NSData dataWithContentsOfURL:[panel URL]]];
// NSLog(@"Bits per pixel: %ld",[pgmImage bitsPerPixel]); // BUG HERE!
NSImage *image = [[NSImage alloc] init];
[image addRepresentation:pgmImage];
[pgmImageView setImage:image];
}
panel = nil; // prevent strong ref cycle
}];
}
此外,当我取消注释代码行// NSLog(@"Bits per pixel: %ld",[pgmImage bitsPerPixel]); // BUG HERE!
只是为了检查我的 Xcode 冻结片刻,我得到 EXC_BAD_ACCESS :
AppKit`__75-[NSBitmapImageRep _withoutChangingBackingPerformBlockUsingBackingCGImage:]_block_invoke_0:
0x7fff8b4823e8: pushq %rbp
0x7fff8b4823e9: movq %rsp, %rbp
0x7fff8b4823ec: pushq %r15
0x7fff8b4823ee: pushq %r14
0x7fff8b4823f0: pushq %r13
0x7fff8b4823f2: pushq %r12
0x7fff8b4823f4: pushq %rbx
0x7fff8b4823f5: subq $312, %rsp
0x7fff8b4823fc: movq %rsi, %rbx
0x7fff8b4823ff: movq %rdi, %r15
0x7fff8b482402: movq 10625679(%rip), %rax
0x7fff8b482409: movq (%rax), %rax
0x7fff8b48240c: movq %rax, -48(%rbp)
0x7fff8b482410: movq %rbx, %rdi
0x7fff8b482413: callq 0x7fff8b383148 ; BIRBackingType //EXC_BAD_ACCESS (code=2, adress=...)
有什么帮助吗???我不知道出了什么问题...