1//
2//  PXNearestNeighborScaleAlgorithm.m
3//  Pixen-XCode
4//
5//  Created by Ian Henderson on Thu Jun 10 2004.
6//  Copyright (c) 2004 Open Sword Group. All rights reserved.
7//
8
9#import "PXNearestNeighborScaleAlgorithm.h"
10#import "PXCanvas.h"
11#import "PXLayer.h"
12
13@implementation PXNearestNeighborScaleAlgorithm
14
15- (NSString *)name
16{
17	return @"Nearest Neighbor";
18}
19
20- (NSString *)algorithmInfo
21{
22	return NSLocalizedString(@"NEAREST_NEIGHBOR_INFO", "Nearest Neighbor Info Here");
23}
24
25- (BOOL)canScaleCanvas:canvas toSize:(NSSize)size
26{
27	if (canvas == nil || size.width == 0 || size.height == 0) {
28		return NO;
29	}
30	return YES;
31}
32
33- (void)scaleCanvas:canvas toSize:(NSSize)size
34{
35	if (canvas == nil) {
36		return;
37	}
38	NSEnumerator *layerEnumerator = [[canvas layers] objectEnumerator];
39	PXLayer *layer, *layerCopy;
40	int x, y;
41	float xScale = size.width / [canvas size].width;
42	float yScale = size.height / [canvas size].height;
43
44	NSPoint currentPoint;
45	while ( (layer = [layerEnumerator nextObject]) ) {
46		layerCopy = [[layer copy] autorelease];
47		[layer setSize:size];
48		for (x=0; x<size.width; x++) {
49			for (y=0; y<size.height; y++) {
50				currentPoint = NSMakePoint((int)(x/xScale),(int)(y/yScale));
51				[layer setColor:[layerCopy colorAtPoint:currentPoint] atPoint:NSMakePoint(x, y)];
52			}
53		}
54	}
55	[canvas layersChanged];
56    [canvas canvasShouldRedraw:nil];
57}
58
59@end
60