//
//  AppDelegate.m
//  RotetImage3test
//
//  Created by GREIF Software on 2014/08/03.
//  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)Button000Pushed:(id)sender {
	[self RotateImage:0];
}
- (IBAction)Button090Pushed:(id)sender {
	[self RotateImage:1];
}
- (IBAction)Button180Pushed:(id)sender {
	[self RotateImage:2];
}
- (IBAction)Button270Pushed:(id)sender {
	[self RotateImage:3];
}


- (void)RotateImage:(NSInteger)mode {
	NSImage *testImage = [NSImage imageNamed:@"RB-66C.jpg"];
	if ( ![testImage isValid] ) {
		NSLog(@"Image Not Found");
		return;
	}
	CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)[testImage TIFFRepresentation], NULL);
	CGImageRef image_ref = CGImageSourceCreateImageAtIndex(source, 0, NULL);
	double pdWidth = 160, pdHeight = 131;
// ------------------------------------------------------
	
//	REALpicture destPic = 0;  // Only Plug-in ~
//	REALpictureDescription pictureDesc;
//	double pdWidth, pdHeight;
	
//	REALLockPictureDescription(sourcePic, &pictureDesc);
//	pdWidth  = pictureDesc.width;
//	pdHeight = pictureDesc.height;
//	REALUnlockPictureDescription(sourcePic);  // ~ Only Plug-in
	
	double ww, hh, wc, hc, rr;
	if (mode == 1) {  // Right 90 Degree
		ww = pdHeight;
		hh = pdWidth;
		wc = 0;
		hc = hh;
		rr = -90.0;
	} else if (mode == 2) {  // 180 Degree
		ww = pdWidth;
		hh = pdHeight;
		wc = ww;
		hc = hh;
		rr = 180.0;
	} else if (mode == 3) {  // Left 90 Degree
		ww = pdHeight;
		hh = pdWidth;
		wc = ww;
		hc = 0;
		rr = 90.0;
	} else {  // No Rotate
		ww = pdWidth;
		hh = pdHeight;
		wc = 0.0;
		hc = 0.0;
		rr = 0.0;
	}
	
//	CGImageRef image_ref = REALCopyPictureCGImage(sourcePic);  // Only Plug-in
//	CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(image_ref);  // Only Plug-in
	CGColorSpaceRef colorSpace = CGImageGetColorSpace(image_ref);
//	NSLog(@"%@",colorSpace);
//	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	unsigned char *rawData = (unsigned char*) calloc(ww * hh * 4, sizeof(unsigned char));
	NSUInteger bytesPerRow = 4 * ww;
	NSUInteger bitsPerComponent = 8;
	CGContextRef context = CGBitmapContextCreate(rawData, ww, hh,
							bitsPerComponent, bytesPerRow, colorSpace,
//							alphaInfo | kCGBitmapByteOrder32Big);  // Only Plug-in
							kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);  // for Debug
	CGColorSpaceRelease(colorSpace);
	CGContextSetBlendMode(context, kCGBlendModeCopy);
	
	CGContextTranslateCTM(context, wc, hc);
	CGContextRotateCTM(context, rr/360.0*(M_PI*2.0));
	CGRect rect = CGRectMake(0, 0, pdWidth, pdHeight);
	CGContextDrawImage(context, rect, image_ref);
	CGImageRelease(image_ref);
	
//	destPic = REALBuildPictureFromBuffer(ww, hh, kRBPixelXRGB32, rawData, bytesPerRow);  // Only Plug-in
	
//	CGContextRelease(context);  // Only Plug-in
	free(rawData);
	CGImageRelease(image_ref);
	
//	return destPic;  // Only Plug-in
	
// ------------------------------------------------------
	CGImageRef image_ref2 = CGBitmapContextCreateImage(context);
	NSSize size = NSMakeSize(ww, hh);
	NSImage *testImage2 = [[NSImage alloc] initWithCGImage:image_ref2 size:size];
	[imgWell setImage:testImage2];
	
	CGContextRelease(context);  // for Debug

}

@end
