GraphicsUtils.m
changeset 2 3d3dcc3116d5
parent 1 e55a17cdabd2
child 3 8fad19466c59
     1.1 --- a/GraphicsUtils.m	Thu Mar 20 09:05:58 2008 -0700
     1.2 +++ b/GraphicsUtils.m	Wed Apr 02 14:45:33 2008 -0700
     1.3 @@ -11,6 +11,27 @@
     1.4  @implementation NSImage (MYUtilities)
     1.5  
     1.6  
     1.7 +- (NSBitmapImageRep*) my_bitmapRep
     1.8 +{
     1.9 +    NSSize max = {0,0};
    1.10 +    NSBitmapImageRep *bestRep = nil;
    1.11 +    for( NSImageRep *rep in self.representations )
    1.12 +        if( [rep isKindOfClass: [NSBitmapImageRep class]] ) {
    1.13 +            NSSize size = [rep size];
    1.14 +            if( size.width > max.width || size.height > max.height ) {
    1.15 +                bestRep = (NSBitmapImageRep*)rep;
    1.16 +                max = size;
    1.17 +            }
    1.18 +        }
    1.19 +    if( ! bestRep ) {
    1.20 +        NSImage *tiffImage = [[NSImage alloc] initWithData:[self TIFFRepresentation]];
    1.21 +        bestRep = [[tiffImage representations] objectAtIndex:0];
    1.22 +        [tiffImage autorelease];
    1.23 +    }
    1.24 +    return bestRep;
    1.25 +}
    1.26 +
    1.27 +
    1.28  - (NSSize) my_sizeOfLargestRep
    1.29  {
    1.30      NSArray *reps = [self representations];
    1.31 @@ -51,13 +72,16 @@
    1.32  
    1.33  - (NSData*) my_JPEGData
    1.34  {
    1.35 -    NSImage *tiffImage = [[NSImage alloc] initWithData:[self TIFFRepresentation]];
    1.36 -    NSBitmapImageRep *rep = [[tiffImage representations] objectAtIndex:0];
    1.37 +    return [self my_dataInFormat: NSJPEGFileType quality: 0.75f];
    1.38 +}
    1.39 +
    1.40 +
    1.41 +- (NSData*) my_dataInFormat: (NSBitmapImageFileType)format quality: (float)quality
    1.42 +{
    1.43 +    NSBitmapImageRep *rep = self.my_bitmapRep;
    1.44      NSDictionary *props = [NSDictionary dictionaryWithObjectsAndKeys:
    1.45 -        [NSNumber numberWithFloat: 0.75f], NSImageCompressionFactor, nil];
    1.46 -    NSData *jpeg = [rep representationUsingType: NSJPEGFileType properties: props];
    1.47 -    [tiffImage release];
    1.48 -    return jpeg;
    1.49 +                           [NSNumber numberWithFloat: quality], NSImageCompressionFactor, nil];
    1.50 +    return [rep representationUsingType: format properties: props];
    1.51  }
    1.52  
    1.53  
    1.54 @@ -188,3 +212,32 @@
    1.55  }
    1.56  
    1.57  @end
    1.58 +
    1.59 +
    1.60 +
    1.61 +NSArray* OpenWindowsWithDelegateClass( Class klass )
    1.62 +{
    1.63 +    NSMutableArray *windows = $marray();
    1.64 +    for( NSWindow *window in [NSApp windows] ) {
    1.65 +        id delegate = window.delegate;
    1.66 +        if( (window.isVisible || window.isMiniaturized) && [delegate isKindOfClass: klass] ) 
    1.67 +            [windows addObject: window];
    1.68 +    }
    1.69 +    return windows;
    1.70 +}    
    1.71 +
    1.72 +
    1.73 +
    1.74 +NSRect PinRect( NSRect r, NSRect container )
    1.75 +{
    1.76 +    // Push r's origin inside container, and limit its size to the container's:
    1.77 +    r = NSMakeRect(MAX(r.origin.x, container.origin.x),
    1.78 +                   MAX(r.origin.y, container.origin.y),
    1.79 +                   MIN(r.size.width, container.size.width),
    1.80 +                   MIN(r.size.height, container.size.height));
    1.81 +    // Push r's outside edges into the container:
    1.82 +    r.origin.x -= MAX(0, NSMaxX(r)-NSMaxX(container));
    1.83 +    r.origin.y -= MAX(0, NSMaxY(r)-NSMaxY(container));
    1.84 +    return r;
    1.85 +    
    1.86 +}