GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: An iPhoto plugin to export photos to Gallery.
Homepage: http://zwily.com/iphoto
Clone URL: git://github.com/zwily/iphototogallery.git
iphototogallery / Source / NSBitmapImageRep+sizing.m
100644 75 lines (63 sloc) 2.002 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// NSBitmapImageRep+sizing.m
// iPhotoToGallery
//
// Created by Zachary Wily on Sun Nov 02 2003.
// Copyright (c) 2003 __MyCompanyName__. All rights reserved.
//
 
#import "NSBitmapImageRep+sizing.h"
 
@implementation NSBitmapImageRep(Sizing)
 
- (NSBitmapImageRep *)representationWithSize:(NSSize)size
{
    NSRect rect = NSMakeRect(0, 0, size.width, size.height);
    NSImage *image = [[[NSImage alloc] initWithSize:size] autorelease];
    NSBitmapImageRep *outRep;
    [image lockFocus];
    [[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
    [[NSGraphicsContext currentContext] setShouldAntialias:YES];
    [self drawInRect:rect];
    outRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:rect] autorelease];
    [image unlockFocus];
    return outRep;
}
 
- (NSBitmapImageRep *)representationScaledTo:(NSSize)size
{
    NSSize originalSize = [self size];
    
    int old_x = [self pixelsWide];
    int old_y = [self pixelsHigh];
    int new_x = size.width;
    int new_y = size.height;
    int good_x;
    int good_y;
        
    float aspect = (float)old_x / (float)old_y;
        
    if (aspect >= 1) {
        good_x = new_x;
        good_y = new_x / aspect;
            
        if (good_y > new_y) {
            good_y = new_y;
            good_x = new_y * aspect;
        }
    }
    else {
        good_y = new_y;
        good_x = aspect * new_y;
            
        if (good_x > new_x) {
            good_x = new_x;
            good_y = new_x / aspect;
        }
    }
    // Don't go any bigger!
    if ((good_x > old_x) || (good_y > old_y)) {
        good_x = old_x;
        good_y = old_y;
    }
    NSLog(@"scaled %dx%d (aspect: %.2f) to %dx%d (aspect: %.2f) (target: %dx%d)\n",
            old_x, old_y, aspect, good_x, good_y, (float)good_x / (float)good_y, new_x, new_y);
    
    if (good_x != old_x) {
        return [self representationWithSize:NSMakeSize(good_x, good_y)];
    }
    else {
        return self;
    }
}
 
@end