//**********************************************************************************************************************
//
//  BXBitmapContext.h
//
//  Author:		Peter Baumgartner, peter@baumgartner.com
//  Copyright:	©2009 by IMAGINE GbR. All rights reserved.
//	Abstract:	Creates a CGContextRef wrapped around a externally supplied buffer.
//
//**********************************************************************************************************************


// This class is only available with 10.5.sdk or newer...

#if defined(MAC_OS_X_VERSION_10_5) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)

//#import <Carbon/Carbon.h>
#import <QuickTime/QuickTime.h>


//----------------------------------------------------------------------------------------------------------------------


@interface BXBitmapContext : NSObject
{
	UInt8* _buffer;
	BOOL _ownsBuffer;
	NSUInteger _width;
	NSUInteger _height;
	NSUInteger _rowBytes;
	CGColorSpaceRef _colorspace;
	CGContextRef _context;
}

// Properties...

@property (readonly) UInt8* buffer;
@property (readonly) NSUInteger width;
@property (readonly) NSUInteger height;
@property (readonly) NSUInteger rowBytes;
@property (readonly) CGColorSpaceRef colorspace;
@property (readonly) CGContextRef context;
@property (readonly) CGImageRef image; // autoreleased!
//@property (readonly) ImageDescriptionHandle imageDescription;

// Creates a CGContextRef wrapped around a externally supplied buffer. If you supply a NULL pointer for the buffer, 
// the class will allocate its own buffer and free it in dealloc. If inProfile is supplied an appropriate colorspace
// is created. If inProfile is NULL a generic RGB colorspace is created...

+ (BXBitmapContext*) bitmapContextWithBuffer:(UInt8*)inBuffer width:(NSUInteger)inWidth height:(NSUInteger)inHeight rowBytes:(NSUInteger)inRowbytes colorspace:(CGColorSpaceRef)inColorSpace;
- (id) initWithBuffer:(UInt8*)inBuffer width:(NSUInteger)inWidth height:(NSUInteger)inHeight rowBytes:(NSUInteger)inRowbytes colorspace:(CGColorSpaceRef)inColorSpace;

// Fills the whole bitmap with the specified color...

- (void) eraseToColor:(NSColor*)inColor;

// Saves the bitmap to a png file...

- (BOOL) savePNGFileAtPath:(NSString*)inPath;

// Color corrects the bitmap in place. If all params are 1.0 the color of the image is not modified...

- (void) colorCorrectWithRedFactor:(double)r greenFactor:(double)g blueFactor:(double)b saturation:(double)s gamma:(double)y;

// Fast in-place subsampling algorithm that provides low quality downscaling of an image...

- (void) subSampleWithFactor:(NSUInteger)inFactor;

#if !__LP64__
- (ImageDescriptionHandle) imageDescription;
#endif

- (void) flipVertical;
- (void) flipHorizotal;
- (void) fillAlphaWithValue:(UInt8)inValue;

@end


//----------------------------------------------------------------------------------------------------------------------


#endif

