It's actually much easier than you're thinking.
First of all, I would recommend making CTPWTPGMImageRep
a subclass of NSBitmapImageRep
rather than NSImageRep
. That will take care of the "hardest" problem, since there will be no need to implement a custom draw
method, as NSBitmapImageRep
s already know how to draw themselves. (In OS X 10.5 and later, NSBitmapImageRep
is basically a direct wrapper around CoreGraphics CGImageRef
s).
I'm not that familiar with the PGM format, but what you'll basically be doing is creating a representation of the image in the closest destination format that matches the source format. To use a specific example, we'll take the PGM example FEEP image from Wikipedia.
P2
# Shows the word "FEEP" (example from Netpbm main page on PGM)
24 7
15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0
0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0
0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
The closest native image to be able to portray the values in the example image would be a single channel grayscale image, 8 bits per pixel, no alpha. The plan then is to create a CGImageRef
with the specified settings, and then use NSBitmapImageRep
's initWithCGImage:
method to initialize the custom subclass.
I would first override the following two methods to both rely on the singular +imageRepWithData:
:
+ (NSArray *)imageRepsWithData:(NSData *)data {
id imageRep = [[self class] imageRepWithData:data];
return [NSArray arrayWithObject:imageRep];
}
- (id)initWithData:(NSData *)data {
CTPWTPGMImageRep *imageRep = [[self class] imageRepWithData:data];
if (imageRep == nil) {
[self release];
return nil;
}
self = [imageRep retain];
return self;
}
For me, it was necessary to implement the +imageRepsWithData:
method to call the singular method before the image was able to be loaded properly.
I would then change the singular +imageRepWithData:
method be as follows:
+ (id)imageRepWithData:(NSData *)data {
if (data.length < 2) return nil;
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) {
NSLog(@"It is not supported pgm file format.");
return nil;
}
NSArray *pgmParameters = [self calculatePgmParameters:data beginingByte:3];
NSUInteger width = [[pgmParameters objectAtIndex:0] integerValue];
NSUInteger height = [[pgmParameters objectAtIndex:1] integerValue];
NSUInteger maxValue = [[pgmParameters objectAtIndex:2] integerValue];
NSUInteger imageLength = width * height * 1;
NSData *imageData = [data subdataWithRange:
NSMakeRange(data.length - imageLength, imageLength)];
const UInt8 *imageDataBytes = [imageData bytes];
UInt8 *expandedImageDataBytes = malloc(imageLength);
for (NSUInteger i = 0; i < imageLength; i++) {
expandedImageDataBytes[i] = 255 * (imageDataBytes[i] / (CGFloat)maxValue);
}
NSData *expandedImageData = [NSData dataWithBytes:expandedImageDataBytes
length:imageLength];
free(expandedImageDataBytes);
CGDataProviderRef provider = CGDataProviderCreateWithCFData(
(CFDataRef)expandedImageData);
CGColorSpaceRef colorSpace =
CGColorSpaceCreateWithName(kCGColorSpaceGenericGrayGamma2_2);
CGImageRef imageRef = CGImageCreate(width,
height,
8,
8,
width * 1,
colorSpace,
kCGImageAlphaNone,
provider,
NULL,
false,
kCGRenderingIntentDefault);
CGColorSpaceRelease(colorSpace);
CGDataProviderRelease(provider);
if (imageRef == NULL) {
NSLog(@"CGImageCreate() failed!");
}
CTPWTPGMImageRep *imageRep = [[[CTPWTPGMImageRep alloc]
initWithCGImage:imageRef] autorelease];
CGImageRelease(imageRef);
return imageRep;
}
As you can see, we need to loop through the bytes in the original image and create a second copy of these bytes with the full expanded range between 0 and 255.
To make use of this image rep, you can call it like the following (being sure to use NSImage
's initWithData:
method):
// if it hasn't been done already:
[NSImageRep registerImageRepClass:[CTPWTPGMImageRep class]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"feep" ofType:@"pgm"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSImage *image = [[[NSImage alloc] initWithData:data] autorelease];
[self.imageView setImage:image];
One more note about your +imageUnfilteredFileTypes
method: filename extensions are case insensitive, so there's no need to specify both lowercase and uppercase @"PGM"
, you can just do lowercase:
+ (NSArray *)imageUnfilteredFileTypes {
static NSArray *types = nil;
if (!types) types = [[NSArray alloc] initWithObjects:@"pgm", nil];
return types;
}