//
//  AppDelegate.m
//  getIconTest
//
//  Created by GREIF Software on 2014/08/29.
//  Copyright (c) 2014 GREIF Software. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
	// Insert code here to initialize your application
}

- (IBAction)Button0Pushed:(id)sender {
	[self getIcon];
}

- (void)getIcon {
	NSOpenPanel *openPanel	= [NSOpenPanel openPanel];
	[openPanel setCanChooseFiles:true];  // File Select
	[openPanel setCanChooseDirectories:false];  // Folder Select
	[openPanel setResolvesAliases:true];
	[openPanel setAllowsMultipleSelection:false];
	NSInteger pressedButton = [openPanel runModal];
	if (pressedButton == NSOKButton) {
		NSURL *filePath = [openPanel URL];  // get file path
		[self getIconImageFrom:[filePath path] with:0];  // specify kind (ex. 0 = 1024x1024)
	} else if (pressedButton == NSCancelButton) {
		NSLog(@"Cancel");
	} else {
		// error
	}
}

- (void)getIconImageFrom:(NSString *)path with:(NSInteger)kind {
// ------------------------------------------------------
	NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
	NSImage *image = [workspace iconForFile:path];
	
	NSArray *bitmapImages = [NSBitmapImageRep imageRepsWithData:[image TIFFRepresentation]];
	NSBitmapImageRep *bitmapImageRep = [bitmapImages objectAtIndex:kind];
	
	unsigned char *rawData = [bitmapImageRep bitmapData];
	NSInteger bytesPerRow = [bitmapImageRep bytesPerRow];
	NSUInteger width = [bitmapImageRep pixelsWide];
	NSUInteger height = [bitmapImageRep pixelsHigh];
	
/*	int byteIndex = 0;
	for (int i = 0; i < width*height; i++) {
		int red   = rawData[byteIndex+0];
		int green = rawData[byteIndex+1];
		int blue  = rawData[byteIndex+2];
		int alpha = rawData[byteIndex+3];
		if (mask) {
			rawData[byteIndex+0] = ~ alpha;
			rawData[byteIndex+1] = ~ alpha;
			rawData[byteIndex+2] = ~ alpha;
			rawData[byteIndex+3] = ~ alpha;
		} else {
			rawData[byteIndex+0] = alpha;
			rawData[byteIndex+1] = red;
			rawData[byteIndex+2] = green;
			rawData[byteIndex+3] = blue;
		}
		byteIndex += 4;
	}*/
	
//	destPic = REALBuildPictureFromBuffer(width, height, kRBPixelXRGB32, rawData, bytesPerRow);  // Only Plug-in
//	return destPic;  // Only Plug-in
// ------------------------------------------------------
	NSLog(@"%@",bitmapImages);
	[[image TIFFRepresentation] writeToFile:@"/Users/hoge/Desktop/test.tif" atomically:YES];
	
	size_t bufferLength = width * height * 4;
	CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, rawData, bufferLength, NULL);
	size_t bitsPerComponent = 8;
	size_t bitsPerPixel = 32;
	CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
	CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
	CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
	CGImageRef iref = CGImageCreate(width,
									height,
									bitsPerComponent,
									bitsPerPixel,
									bytesPerRow,
									colorSpaceRef,
									bitmapInfo,
									provider,   // data provider
									NULL,       // decode
									YES,        // should interpolate
									renderingIntent);
	NSImage *image2 = [[NSImage alloc] initWithCGImage:iref size:NSMakeSize(width, height)];
	
	NSImage *image3 = [[NSImage alloc] initWithData:[image2 TIFFRepresentation]];
	[imgWell setImage:image3];
}

@end
