/* Copyright (c) 2006-2007 Christopher J. W. Lloyd Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #import #import #import #import #import @implementation NSBitmapImageRep +(NSArray *)imageUnfilteredFileTypes { return [NSArray arrayWithObjects:@"tiff",@"tif",@"png",@"jpg",@"gif",@"bmp",nil]; } +(NSArray *)imageRepsWithContentsOfFile:(NSString *)path { NSMutableArray *result=[NSMutableArray array]; NSBitmapImageRep *rep=[[[NSBitmapImageRep alloc] initWithContentsOfFile:path] autorelease]; if(rep==nil) return nil; [result addObject:rep]; return result; } +(void)getTIFFCompressionTypes:(const NSTIFFCompression **)types count:(int *)count { NSUnimplementedMethod(); } +(NSString *)localizedNameForTIFFCompressionType:(NSTIFFCompression)type { NSUnimplementedMethod(); return nil; } +(NSData *)TIFFRepresentationOfImageRepsInArray:(NSArray *)array { NSUnimplementedMethod(); return nil; } +(NSData *)TIFFRepresentationOfImageRepsInArray:(NSArray *)array usingCompression:(NSTIFFCompression)compression factor:(float)factor { NSUnimplementedMethod(); return nil; } +(NSData *)representationOfImageRepsInArray:(NSArray *)array usingType:(NSBitmapImageFileType)type properties:(NSDictionary *)properties { NSUnimplementedMethod(); return nil; } +(NSArray *)imageRepsWithData:(NSData *)data { id result=[self imageRepWithData:data]; if(result==nil) return nil; return [NSArray arrayWithObject:result]; } +imageRepWithData:(NSData *)data { return [[[self alloc] initWithData:data] autorelease]; } -initWithBitmapDataPlanes:(unsigned char **)planes pixelsWide:(int)width pixelsHigh:(int)height bitsPerSample:(int)bitsPerSample samplesPerPixel:(int)samplesPerPixel hasAlpha:(BOOL)hasAlpha isPlanar:(BOOL)isPlanar colorSpaceName:(NSString *)colorSpaceName bitmapFormat:(NSBitmapFormat)bitmapFormat bytesPerRow:(int)bytesPerRow bitsPerPixel:(int)bitsPerPixel { int i,numberOfPlanes=isPlanar?samplesPerPixel:1; _size=NSMakeSize(width,height); _colorSpaceName=[colorSpaceName copy]; _bitsPerSample=bitsPerSample; _pixelsWide=width; _pixelsHigh=height; _hasAlpha=hasAlpha; _samplesPerPixel=samplesPerPixel; _isPlanar=isPlanar; _bitmapFormat=bitmapFormat; if(bitsPerPixel!=0) _bitsPerPixel=bitsPerPixel; else _bitsPerPixel=_bitsPerSample*_samplesPerPixel; if(bytesPerRow!=0) _bytesPerRow=bytesPerRow; else _bytesPerRow=(_bitsPerPixel*_pixelsWide+7)/8; _freeWhenDone=NO; if(planes==NULL) _freeWhenDone=YES; else { int i; for(i=0;i0) _size.width*=72.0/[xres doubleValue]; if(yres!=nil && [yres doubleValue]>0) _size.height*=72.0/[yres doubleValue]; CGColorSpaceRef colorSpace=CGImageGetColorSpace(cgImage); // FIXME: _colorSpaceName=NSDeviceRGBColorSpace; _bitsPerSample=CGImageGetBitsPerComponent(cgImage); _pixelsWide=CGImageGetWidth(cgImage); _pixelsHigh=CGImageGetHeight(cgImage); switch(CGImageGetAlphaInfo(cgImage)){ case kCGImageAlphaPremultipliedLast: case kCGImageAlphaPremultipliedFirst: case kCGImageAlphaLast: case kCGImageAlphaFirst: _hasAlpha=YES; break; default: _hasAlpha=NO; break; } _samplesPerPixel=CGColorSpaceGetNumberOfComponents(colorSpace); if(_hasAlpha) _samplesPerPixel++; _isPlanar=NO; _bitmapFormat=0; if(CGImageGetBitmapInfo(cgImage)&kCGBitmapFloatComponents){ _bitmapFormat|=NSFloatingPointSamplesBitmapFormat; } switch(CGImageGetAlphaInfo(cgImage)){ case kCGImageAlphaNone: break; case kCGImageAlphaPremultipliedLast: break; case kCGImageAlphaPremultipliedFirst: _bitmapFormat|=NSAlphaFirstBitmapFormat; break; case kCGImageAlphaLast: _bitmapFormat|=NSAlphaNonpremultipliedBitmapFormat; break; case kCGImageAlphaFirst: _bitmapFormat|=NSAlphaFirstBitmapFormat; _bitmapFormat|=NSAlphaNonpremultipliedBitmapFormat; break; case kCGImageAlphaNoneSkipLast: case kCGImageAlphaNoneSkipFirst: break; } _bitsPerPixel=CGImageGetBitsPerPixel(cgImage); _bytesPerRow=CGImageGetBytesPerRow(cgImage); _freeWhenDone=YES; _bitmapPlanes=NSZoneCalloc(NULL,1,sizeof(unsigned char *)); _bitmapPlanes[0]=NSZoneCalloc(NULL,_bytesPerRow*_pixelsHigh,1); CGDataProviderRef provider=CGImageGetDataProvider(cgImage); // FIXME: inefficient NSData *bitmapData=(NSData *)CGDataProviderCopyData(provider); const unsigned char *bytes=[bitmapData bytes]; int i,length=_bytesPerRow*_pixelsHigh; for(i=0;i=0 && x<[self pixelsWide],@"x out of bounds"); NSAssert(y>=0 && y<[self pixelsHigh],@"y out of bounds"); if(_isPlanar) NSUnimplementedMethod(); else { if(_bitsPerPixel/_samplesPerPixel!=8) NSUnimplementedMethod(); unsigned char *bits=_bitmapPlanes[0]+_bytesPerRow*y+(x*_bitsPerPixel)/8; int i; for(i=0;i<_samplesPerPixel;i++){ bits[i]=pixel[i]; } } } -(NSColor *)colorAtX:(int)x y:(int)y { NSUnimplementedMethod(); return nil; } -(void)setColor:(NSColor *)color atX:(int)x y:(int)y { color=[color colorUsingColorSpaceName:[self colorSpaceName]]; NSInteger i,numberOfComponents=[color numberOfComponents]; CGFloat components[numberOfComponents]; unsigned pixels[numberOfComponents]; [color getComponents:components]; if(!_hasAlpha) numberOfComponents--; else { if(!(_bitmapFormat&NSAlphaNonpremultipliedBitmapFormat)){ // premultiplied CGFloat alpha=components[numberOfComponents-1]; for(i=0;i=1;) components[i]=components[i-1]; components[0]=alpha; } } if(_bitmapFormat&NSFloatingPointSamplesBitmapFormat){ for(i=0;i