//
//  ImageFilter.m
//  Eagle
//
//  Created by Richard Kettering on 10/16/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "ImageFilter.h"
#include <string.h>

@implementation ImageFilter

- (NSImage *)filterImage:(NSImage *)srcImage
{
    NSBitmapImageRep *srcImageRep = [NSBitmapImageRep
									 imageRepWithData:[srcImage TIFFRepresentation]];
	
    int w = [srcImageRep pixelsWide];
    int h = [srcImageRep pixelsHigh];
    int x, y;
	
	int nColorChannels = 3; // number of color channels
	int destRowByteWidth = 2 * w * nColorChannels;
	int srcRowByteWidth = w * nColorChannels;
	int onePixel = nColorChannels;

	
    NSImage *destImage = [[NSImage alloc] initWithSize:NSMakeSize(w,h)];
	
    NSBitmapImageRep *destImageRep = [[[NSBitmapImageRep alloc] 
									   initWithBitmapDataPlanes:NULL
									   pixelsWide:w * 2
									   pixelsHigh:h * 2
									   bitsPerSample:8 
									   samplesPerPixel:nColorChannels
									   hasAlpha:NO
									   isPlanar:NO
									   colorSpaceName:NSCalibratedRGBColorSpace
									   bytesPerRow: destRowByteWidth
									   bitsPerPixel:8 * nColorChannels] autorelease];
	
    unsigned char *srcData = [srcImageRep bitmapData];
    unsigned char *destData = [destImageRep bitmapData];
    unsigned char *srcPxl, *dstPxl;
    int n = [srcImageRep bitsPerPixel] / 8;
	
    for ( y = 0; y < h; y++ ) {
        for ( x = 0; x < w; x++ ) {
            srcPxl = srcData + n * (y * w + x);       
			dstPxl = destData + (2 * y * destRowByteWidth) + (2 * nColorChannels * x);

			
//		first:				|Then 
//		. . . --\ CC		|S T U  --\ 1 2
//		. C . --/ CC		|V C W  --/ 3 4
//		. . .				|X Y Z
//							| IF V==S==T => 1=S
//							| IF T==U==W => 2=U
//							| IF V==X==Y => 3=X
//							| IF W==Z==Y => 4=Z			
			
			
			
 			//first do nearest neighbor interpolation
			memcpy(dstPxl,srcPxl,nColorChannels);
			memcpy(dstPxl + nColorChannels ,srcPxl,nColorChannels); //second pixel
            memcpy(dstPxl + destRowByteWidth,srcPxl,nColorChannels); //second row, first pixel
			memcpy(dstPxl + destRowByteWidth + nColorChannels,srcPxl,nColorChannels); //second row, second pixel
			
			/*if( (y < (h-1)) && y > 0 ){
				if (x < (w-1) && x > 0 ){
					//now add smoothing
					//  IF V==S==T => 1=S
					if( (*(srcPxl - onePixel) == *(srcPxl - onePixel - srcRowByteWidth)) &&
					   (*(srcPxl - onePixel - srcRowByteWidth) == *(srcPxl - srcRowByteWidth)) ){
						
						if( (*(srcPxl - onePixel+1) == *(srcPxl - onePixel - srcRowByteWidth+1)) &&
						   (*(srcPxl - onePixel - srcRowByteWidth+1) == *(srcPxl - srcRowByteWidth+1)) ){
						
						memcpy(dstPxl, srcPxl - onePixel - srcRowByteWidth, nColorChannels);
						}
					}
					
					//  IF T==U==W => 2=U
					if( (*(srcPxl - srcRowByteWidth) == *(srcPxl + nColorChannels - srcRowByteWidth)) &&
					   (*(srcPxl + nColorChannels - srcRowByteWidth) == *(srcPxl + nColorChannels))  ){
						
						if( (*(srcPxl - srcRowByteWidth+1) == *(srcPxl + nColorChannels - srcRowByteWidth+1)) &&
						   (*(srcPxl + nColorChannels - srcRowByteWidth+1) == *(srcPxl + nColorChannels+1))  ){

						
						memcpy(dstPxl + nColorChannels, srcPxl + nColorChannels - srcRowByteWidth, nColorChannels);
					}
					}
					
					//  IF V==X==Y => 3=X
					if( (*(srcPxl - nColorChannels) == *(srcPxl - nColorChannels + srcRowByteWidth)) &&
					   (*(srcPxl - nColorChannels + srcRowByteWidth) == *(srcPxl + srcRowByteWidth))  ){
					 if( (*(srcPxl - nColorChannels+1) == *(srcPxl - nColorChannels + srcRowByteWidth+1)) &&
					 (*(srcPxl - nColorChannels + srcRowByteWidth+1) == *(srcPxl + srcRowByteWidth+1))  ){
						
						
						memcpy(dstPxl + destRowByteWidth, srcPxl - nColorChannels + srcRowByteWidth, nColorChannels);
					 }
					}

					//  IF W==Z==Y => 4=Z	
					if( (*(srcPxl + nColorChannels) == *(srcPxl + nColorChannels + srcRowByteWidth)) &&
					   (*(srcPxl + nColorChannels + srcRowByteWidth) == *(srcPxl + srcRowByteWidth))  ){
						if( (*(srcPxl + nColorChannels+1) == *(srcPxl + nColorChannels + srcRowByteWidth+1)) &&
						   (*(srcPxl + nColorChannels + srcRowByteWidth+1) == *(srcPxl + srcRowByteWidth+1))  ){
						memcpy(dstPxl + nColorChannels + destRowByteWidth, srcPxl + nColorChannels + srcRowByteWidth, nColorChannels);
						}
					}
				}
			}*/
        }
    }
    
    [destImage addRepresentation:destImageRep];
    return destImage;
}

@end
