1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 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:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 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. */
9 #import <AppKit/NSImageRep.h>
10 #import <AppKit/NSBitmapImageRep.h>
11 #import <AppKit/NSPDFImageRep.h>
12 #import <AppKit/NSPasteboard.h>
13 #import <AppKit/NSGraphicsContextFunctions.h>
14 #import <AppKit/NSRaise.h>
16 @implementation NSImageRep
18 static NSMutableArray *_registeredClasses=nil;
21 if(self==[NSImageRep class]){
22 _registeredClasses=[NSMutableArray new];
23 [_registeredClasses addObject:[NSBitmapImageRep class]];
25 * // PDF is not ready for primetime
26 * [_registeredClasses addObject:[NSPDFImageRep class]];
32 +(NSArray *)registeredImageRepClasses {
33 return _registeredClasses;
36 +(void)registerImageRepClass:(Class)class {
37 [_registeredClasses addObject:class];
40 +(void)unregisterImageRepClass:(Class)class {
41 [_registeredClasses removeObjectIdenticalTo:class];
44 +(NSArray *)imageFileTypes {
45 NSMutableSet *result=[NSMutableSet set];
46 int i,count=[_registeredClasses count];
49 Class cls=[_registeredClasses objectAtIndex:i];
51 [result addObjectsFromArray:[cls imageUnfilteredFileTypes]];
54 return [result allObjects];
57 +(NSArray *)imageUnfilteredFileTypes {
58 return [NSArray array];
61 +(NSArray *)imagePasteboardTypes {
62 NSMutableSet *result=[NSMutableSet set];
63 int i,count=[_registeredClasses count];
66 Class cls=[_registeredClasses objectAtIndex:i];
68 [result addObjectsFromArray:[cls imageUnfilteredPasteboardTypes]];
71 return [result allObjects];
74 +(NSArray *)imageUnfilteredPasteboardTypes {
75 return [NSArray array];
78 +(BOOL)canInitWithData:(NSData *)data {
82 +(BOOL)canInitWithPasteboard:(NSPasteboard *)pasteboard {
83 NSString *available=[pasteboard availableTypeFromArray:[self imageUnfilteredPasteboardTypes]];
85 return (available!=nil)?YES:NO;
88 +(Class)imageRepClassForData:(NSData *)data {
89 int count=[_registeredClasses count];
92 Class check=[_registeredClasses objectAtIndex:count];
94 if([check canInitWithData:data])
101 +(Class)imageRepClassForFileType:(NSString *)type {
102 int count=[_registeredClasses count];
105 Class checkClass=[_registeredClasses objectAtIndex:count];
106 NSArray *types=[checkClass imageUnfilteredFileTypes];
108 for(NSString *checkType in types)
109 if([checkType caseInsensitiveCompare:type]==NSOrderedSame)
116 +(Class)imageRepClassForPasteboardType:(NSString *)type {
117 int count=[_registeredClasses count];
120 Class checkClass=[_registeredClasses objectAtIndex:count];
121 NSArray *types=[checkClass imageUnfilteredPasteboardTypes];
123 if([types containsObject:type])
130 +(NSArray *)imageRepsWithContentsOfFile:(NSString *)path {
131 // Try to guess which class to use from the path extension
132 NSString *type=[path pathExtension];
133 Class class=[self imageRepClassForFileType:type];
135 // Else use the content of the file to guess the class to use
136 NSData *data=[NSData dataWithContentsOfFile:path];
142 if(self==[NSImageRep class]){
143 class=[self imageRepClassForData:data];
145 return [class imageRepsWithData:data];
147 return [class imageRepsWithContentsOfFile:path];
150 +(NSArray *)imageRepsWithContentsOfURL:(NSURL *)url {
152 NSData *data=[NSData dataWithContentsOfURL:url];
157 if(self==[NSImageRep class]){
158 if((class=[self imageRepClassForData:data])==nil)
162 return [class imageRepsWithData:data];
165 +(NSArray *)imageRepsWithPasteboard:(NSPasteboard *)pasteboard {
166 NSArray *imageTypes=[self imagePasteboardTypes];
167 NSArray *pbTypes=[pasteboard types];
169 for(NSString *check in pbTypes){
170 if([imageTypes containsObject:check]){
171 NSData *data=[pasteboard dataForType:check];
174 if((class=[self imageRepClassForData:data])!=nil){
177 if((result=[class imageRepsWithData:data])!=nil)
186 +imageRepWithContentsOfFile:(NSString *)path {
188 NSData *data=[NSData dataWithContentsOfFile:path];
193 if(self==[NSImageRep class]){
194 NSString *type=[path pathExtension];
196 if((class=[self imageRepClassForFileType:type])==nil)
200 return [class imageRepWithData:data];
203 +imageRepWithContentsOfURL:(NSURL *)url {
205 NSData *data=[NSData dataWithContentsOfURL:url];
210 if(self==[NSImageRep class]){
211 if((class=[self imageRepClassForData:data])==nil)
215 return [class imageRepWithData:data];
218 +imageRepWithPasteboard:(NSPasteboard *)pasteboard {
219 NSArray *imageTypes=[self imagePasteboardTypes];
220 NSArray *pbTypes=[pasteboard types];
222 for(NSString *check in pbTypes){
223 if([imageTypes containsObject:check]){
224 NSData *data=[pasteboard dataForType:check];
227 if((class=[self imageRepClassForData:data])!=nil){
230 if((result=[class imageRepWithData:data])!=nil)
239 -copyWithZone:(NSZone *)zone {
240 NSImageRep *result=NSCopyObject(self,0,zone);
241 result->_colorSpaceName=[_colorSpaceName copy];
265 -(NSString *)colorSpaceName {
266 return _colorSpaceName;
269 -(NSInteger)bitsPerSample {
270 return _bitsPerSample;
273 -(void)setSize:(NSSize)value {
277 -(void)setPixelsWide:(int)value {
281 -(void)setPixelsHigh:(int)value {
285 -(void)setOpaque:(BOOL)value {
289 -(void)setAlpha:(BOOL)value {
293 -(void)setColorSpaceName:(NSString *)value {
295 [_colorSpaceName release];
296 _colorSpaceName=value;
299 -(void)setBitsPerSample:(int)value {
300 _bitsPerSample=value;
308 -(BOOL)drawAtPoint:(NSPoint)point {
309 CGContextRef context=NSCurrentGraphicsPort();
312 CGContextSaveGState(context);
313 CGContextTranslateCTM(context,point.x,point.y);
315 CGContextRestoreGState(context);
320 -(BOOL)drawInRect:(NSRect)rect {
321 CGContextRef context=NSCurrentGraphicsPort();
322 NSSize size=[self size];
325 CGContextSaveGState(context);
326 CGContextTranslateCTM(context,rect.origin.x,rect.origin.y);
327 CGContextScaleCTM(context,rect.size.width/size.width,rect.size.height/size.height);
329 CGContextRestoreGState(context);
334 -(NSString *)description {
335 return [NSString stringWithFormat:@"<%@[0x%lx] size: { %f, %f } colorSpace: %@ (%dx%d @ %d bps) alpha: %@ opaque: %@>",
336 [self class], self, _size.width, _size.height, _colorSpaceName, _pixelsWide, _pixelsHigh, _bitsPerSample,
337 _hasAlpha ? @"YES" : @"NO", _isOpaque ? @"YES" : @"NO"];