mirror of
https://github.com/munki/munki.git
synced 2026-01-07 06:59:57 -06:00
git-svn-id: http://munki.googlecode.com/svn/trunk@227 a4e17f2e-e282-11dd-95e1-755cbddbdd66
77 lines
2.1 KiB
Objective-C
77 lines
2.1 KiB
Objective-C
//
|
|
// ScaledImageView.m
|
|
//
|
|
// Created by Greg Neagle on 5/27/09.
|
|
//
|
|
// Copyright 2009 Greg Neagle.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
|
|
|
|
#import "ScaledImageView.h"
|
|
|
|
|
|
@implementation ScaledImageView
|
|
|
|
-(void)drawRect:(NSRect *)rect {
|
|
|
|
NSRect dstRect = [self bounds];
|
|
|
|
float sourceWidth = [[self image] size].width;
|
|
float sourceHeight = [[self image] size].height;
|
|
float targetWidth = dstRect.size.width;
|
|
float targetHeight = dstRect.size.height;
|
|
|
|
// Calculate aspect ratios
|
|
float sourceRatio = sourceWidth / sourceHeight;
|
|
float targetRatio = targetWidth / targetHeight;
|
|
|
|
// Determine what side of the source image to use for proportional scaling
|
|
BOOL scaleWidth = (sourceRatio <= targetRatio);
|
|
|
|
// Proportionally scale source image
|
|
float scalingFactor, scaledWidth, scaledHeight;
|
|
if (scaleWidth) {
|
|
scalingFactor = 1.0 / sourceRatio;
|
|
scaledWidth = targetWidth;
|
|
scaledHeight = round(targetWidth * scalingFactor);
|
|
} else {
|
|
scalingFactor = sourceRatio;
|
|
scaledWidth = round(targetHeight * scalingFactor);
|
|
scaledHeight = targetHeight;
|
|
}
|
|
float scaleFactor = scaledHeight / sourceHeight;
|
|
|
|
// Calculate compositing rectangles
|
|
NSRect sourceRect;
|
|
float destX, destY;
|
|
|
|
// Crop from center
|
|
destX = round((scaledWidth - targetWidth) / 2.0);
|
|
destY = round((scaledHeight - targetHeight) / 2.0);
|
|
|
|
sourceRect = NSMakeRect(destX / scaleFactor, destY / scaleFactor,
|
|
targetWidth / scaleFactor, targetHeight / scaleFactor);
|
|
|
|
|
|
[[self image] drawInRect:dstRect
|
|
fromRect:sourceRect
|
|
operation:NSCompositeSourceOver
|
|
fraction:1.0];
|
|
|
|
}
|
|
|
|
|
|
@end
|